Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1980, 1988 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | static char sccsid[] = "@(#)dumpoptr.c 5.8 (Berkeley) 3/7/91"; | |
78ed81a3 | 36 | static char rcsid[] = "$Header: /b/source/CVS/src/sbin/dump/dumpoptr.c,v 1.4 1993/04/20 09:30:45 mycroft Exp $"; |
15637ed4 RG |
37 | #endif /* not lint */ |
38 | ||
39 | #include <sys/param.h> | |
40 | #include <sys/wait.h> | |
41 | #include <ufs/dir.h> | |
42 | #include <signal.h> | |
43 | #include <time.h> | |
44 | #include <fstab.h> | |
45 | #include <grp.h> | |
46 | #include <varargs.h> | |
47 | #include <utmp.h> | |
48 | #include <tzfile.h> | |
49 | #include <errno.h> | |
50 | #include <stdio.h> | |
51 | #ifdef __STDC__ | |
52 | #include <unistd.h> | |
53 | #include <stdlib.h> | |
54 | #include <string.h> | |
55 | #endif | |
56 | #include "dump.h" | |
57 | #include "pathnames.h" | |
58 | ||
59 | static void alarmcatch(); | |
60 | static void sendmes(); | |
61 | ||
62 | /* | |
63 | * Query the operator; This previously-fascist piece of code | |
64 | * no longer requires an exact response. | |
65 | * It is intended to protect dump aborting by inquisitive | |
66 | * people banging on the console terminal to see what is | |
67 | * happening which might cause dump to croak, destroying | |
68 | * a large number of hours of work. | |
69 | * | |
70 | * Every 2 minutes we reprint the message, alerting others | |
71 | * that dump needs attention. | |
72 | */ | |
73 | int timeout; | |
74 | char *attnmessage; /* attention message */ | |
75 | ||
76 | int | |
77 | query(question) | |
78 | char *question; | |
79 | { | |
80 | char replybuffer[64]; | |
81 | int back, errcount; | |
82 | FILE *mytty; | |
83 | ||
84 | if ((mytty = fopen(_PATH_TTY, "r")) == NULL) | |
85 | quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); | |
86 | attnmessage = question; | |
87 | timeout = 0; | |
88 | alarmcatch(); | |
89 | back = -1; | |
90 | errcount = 0; | |
91 | do { | |
92 | if (fgets(replybuffer, 63, mytty) == NULL) { | |
93 | clearerr(mytty); | |
94 | if (++errcount > 30) /* XXX ugly */ | |
95 | quit("excessive operator query failures\n"); | |
96 | } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { | |
97 | back = 1; | |
98 | } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { | |
99 | back = 0; | |
100 | } else { | |
101 | (void) fprintf(stderr, | |
102 | " DUMP: \"Yes\" or \"No\"?\n"); | |
103 | (void) fprintf(stderr, | |
104 | " DUMP: %s: (\"yes\" or \"no\") ", question); | |
105 | } | |
106 | } while (back < 0); | |
107 | ||
108 | /* | |
109 | * Turn off the alarm, and reset the signal to trap out.. | |
110 | */ | |
111 | alarm(0); | |
112 | if (signal(SIGALRM, sigalrm) == SIG_IGN) | |
113 | signal(SIGALRM, SIG_IGN); | |
114 | fclose(mytty); | |
115 | return(back); | |
116 | } | |
117 | ||
118 | char lastmsg[100]; | |
119 | ||
120 | /* | |
121 | * Alert the console operator, and enable the alarm clock to | |
122 | * sleep for 2 minutes in case nobody comes to satisfy dump | |
123 | */ | |
124 | static void | |
125 | alarmcatch() | |
126 | { | |
127 | if (notify == 0) { | |
128 | if (timeout == 0) | |
129 | (void) fprintf(stderr, | |
130 | " DUMP: %s: (\"yes\" or \"no\") ", | |
131 | attnmessage); | |
132 | else | |
133 | msgtail("\7\7"); | |
134 | } else { | |
135 | if (timeout) { | |
136 | msgtail("\n"); | |
137 | broadcast(""); /* just print last msg */ | |
138 | } | |
139 | (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", | |
140 | attnmessage); | |
141 | } | |
142 | signal(SIGALRM, alarmcatch); | |
143 | alarm(120); | |
144 | timeout = 1; | |
145 | } | |
146 | ||
147 | /* | |
148 | * Here if an inquisitive operator interrupts the dump program | |
149 | */ | |
150 | void | |
151 | interrupt() | |
152 | { | |
153 | msg("Interrupt received.\n"); | |
154 | if (query("Do you want to abort dump?")) | |
155 | dumpabort(); | |
156 | } | |
157 | ||
158 | /* | |
159 | * The following variables and routines manage alerting | |
160 | * operators to the status of dump. | |
161 | * This works much like wall(1) does. | |
162 | */ | |
163 | struct group *gp; | |
164 | ||
165 | /* | |
166 | * Get the names from the group entry "operator" to notify. | |
167 | */ | |
168 | void | |
169 | set_operators() | |
170 | { | |
171 | if (!notify) /*not going to notify*/ | |
172 | return; | |
173 | gp = getgrnam(OPGRENT); | |
174 | endgrent(); | |
175 | if (gp == NULL) { | |
176 | msg("No group entry for %s.\n", OPGRENT); | |
177 | notify = 0; | |
178 | return; | |
179 | } | |
180 | } | |
181 | ||
182 | struct tm *localtime(); | |
183 | struct tm *localclock; | |
184 | ||
185 | /* | |
186 | * We fork a child to do the actual broadcasting, so | |
187 | * that the process control groups are not messed up | |
188 | */ | |
189 | void | |
190 | broadcast(message) | |
191 | char *message; | |
192 | { | |
193 | time_t clock; | |
194 | FILE *f_utmp; | |
195 | struct utmp utmp; | |
196 | int nusers; | |
197 | char **np; | |
198 | int pid, s; | |
199 | ||
200 | if (!notify || gp == NULL) | |
201 | return; | |
202 | ||
203 | switch (pid = fork()) { | |
204 | case -1: | |
205 | return; | |
206 | case 0: | |
207 | break; | |
208 | default: | |
209 | while (wait(&s) != pid) | |
210 | continue; | |
211 | return; | |
212 | } | |
213 | ||
214 | clock = time(0); | |
215 | localclock = localtime(&clock); | |
216 | ||
217 | if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) { | |
218 | msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno)); | |
219 | return; | |
220 | } | |
221 | ||
222 | nusers = 0; | |
223 | while (!feof(f_utmp)) { | |
224 | if (fread(&utmp, sizeof (struct utmp), 1, f_utmp) != 1) | |
225 | break; | |
226 | if (utmp.ut_name[0] == 0) | |
227 | continue; | |
228 | nusers++; | |
229 | for (np = gp->gr_mem; *np; np++) { | |
230 | if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0) | |
231 | continue; | |
232 | /* | |
233 | * Do not send messages to operators on dialups | |
234 | */ | |
235 | if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0) | |
236 | continue; | |
237 | #ifdef DEBUG | |
238 | msg("Message to %s at %s\n", *np, utmp.ut_line); | |
239 | #endif | |
240 | sendmes(utmp.ut_line, message); | |
241 | } | |
242 | } | |
243 | (void) fclose(f_utmp); | |
244 | Exit(0); /* the wait in this same routine will catch this */ | |
245 | /* NOTREACHED */ | |
246 | } | |
247 | ||
248 | static void | |
249 | sendmes(tty, message) | |
250 | char *tty, *message; | |
251 | { | |
252 | char t[50], buf[BUFSIZ]; | |
253 | register char *cp; | |
254 | int lmsg = 1; | |
255 | FILE *f_tty; | |
256 | ||
257 | (void) strcpy(t, _PATH_DEV); | |
258 | (void) strcat(t, tty); | |
259 | ||
260 | if ((f_tty = fopen(t, "w")) != NULL) { | |
261 | setbuf(f_tty, buf); | |
262 | (void) fprintf(f_tty, | |
263 | "\n\ | |
264 | \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\ | |
265 | DUMP: NEEDS ATTENTION: ", | |
266 | localclock->tm_hour, localclock->tm_min); | |
267 | for (cp = lastmsg; ; cp++) { | |
268 | if (*cp == '\0') { | |
269 | if (lmsg) { | |
270 | cp = message; | |
271 | if (*cp == '\0') | |
272 | break; | |
273 | lmsg = 0; | |
274 | } else | |
275 | break; | |
276 | } | |
277 | if (*cp == '\n') | |
278 | (void) putc('\r', f_tty); | |
279 | (void) putc(*cp, f_tty); | |
280 | } | |
281 | (void) fclose(f_tty); | |
282 | } | |
283 | } | |
284 | ||
285 | /* | |
286 | * print out an estimate of the amount of time left to do the dump | |
287 | */ | |
288 | ||
289 | time_t tschedule = 0; | |
290 | ||
291 | void | |
292 | timeest() | |
293 | { | |
294 | time_t tnow, deltat; | |
295 | ||
296 | time (&tnow); | |
297 | if (tnow >= tschedule) { | |
298 | tschedule = tnow + 300; | |
299 | if (blockswritten < 500) | |
300 | return; | |
301 | deltat = tstart_writing - tnow + | |
302 | (1.0 * (tnow - tstart_writing)) | |
303 | / blockswritten * tapesize; | |
304 | msg("%3.2f%% done, finished in %d:%02d\n", | |
305 | (blockswritten * 100.0) / tapesize, | |
306 | deltat / 3600, (deltat % 3600) / 60); | |
307 | } | |
308 | } | |
309 | ||
310 | /* | |
311 | * tapesize: total number of blocks estimated over all reels | |
312 | * blockswritten: blocks actually written, over all reels | |
313 | * etapes: estimated number of tapes to write | |
314 | * | |
315 | * tsize: blocks can write on this reel | |
316 | * asize: blocks written on this reel | |
317 | * tapeno: number of tapes written so far | |
318 | */ | |
319 | int | |
320 | blocksontape() | |
321 | { | |
322 | if (tapeno == etapes) | |
323 | return (tapesize - (etapes - 1) * tsize); | |
324 | return (tsize); | |
325 | } | |
326 | ||
327 | #ifdef lint | |
328 | ||
329 | /* VARARGS1 */ | |
330 | void msg(fmt) char *fmt; { strcpy(lastmsg, fmt); } | |
331 | ||
332 | /* VARARGS1 */ | |
333 | void msgtail(fmt) char *fmt; { fmt = fmt; } | |
334 | ||
335 | void quit(fmt) char *fmt; { msg(fmt); dumpabort(); } | |
336 | ||
337 | #else /* lint */ | |
338 | ||
339 | void | |
340 | msg(va_alist) | |
341 | va_dcl | |
342 | { | |
343 | va_list ap; | |
344 | char *fmt; | |
345 | ||
346 | (void) fprintf(stderr," DUMP: "); | |
347 | #ifdef TDEBUG | |
348 | (void) fprintf(stderr, "pid=%d ", getpid()); | |
349 | #endif | |
350 | va_start(ap); | |
351 | fmt = va_arg(ap, char *); | |
352 | (void) vfprintf(stderr, fmt, ap); | |
353 | va_end(ap); | |
354 | (void) fflush(stdout); | |
355 | (void) fflush(stderr); | |
356 | va_start(ap); | |
357 | fmt = va_arg(ap, char *); | |
358 | (void) vsprintf(lastmsg, fmt, ap); | |
359 | va_end(ap); | |
360 | } | |
361 | ||
362 | void | |
363 | msgtail(va_alist) | |
364 | va_dcl | |
365 | { | |
366 | va_list ap; | |
367 | char *fmt; | |
368 | ||
369 | va_start(ap); | |
370 | fmt = va_arg(ap, char *); | |
371 | (void) vfprintf(stderr, fmt, ap); | |
372 | va_end(ap); | |
373 | } | |
374 | ||
375 | void | |
376 | quit(va_alist) | |
377 | va_dcl | |
378 | { | |
379 | va_list ap; | |
380 | char *fmt; | |
381 | ||
382 | (void) fprintf(stderr," DUMP: "); | |
383 | #ifdef TDEBUG | |
384 | (void) fprintf(stderr, "pid=%d ", getpid()); | |
385 | #endif | |
386 | va_start(ap); | |
387 | fmt = va_arg(ap, char *); | |
388 | vfprintf(stderr, fmt, ap); | |
389 | va_end(ap); | |
390 | (void) fflush(stdout); | |
391 | (void) fflush(stderr); | |
392 | dumpabort(); | |
393 | } | |
394 | ||
395 | #endif /* lint */ | |
396 | ||
397 | /* | |
398 | * Tell the operator what has to be done; | |
399 | * we don't actually do it | |
400 | */ | |
401 | ||
402 | struct fstab * | |
403 | allocfsent(fs) | |
404 | register struct fstab *fs; | |
405 | { | |
406 | register struct fstab *new; | |
407 | ||
408 | new = (struct fstab *)malloc(sizeof (*fs)); | |
409 | if (new == NULL || | |
410 | (new->fs_file = strdup(fs->fs_file)) == NULL || | |
411 | (new->fs_type = strdup(fs->fs_type)) == NULL || | |
412 | (new->fs_spec = strdup(fs->fs_spec)) == NULL) | |
413 | quit("%s\n", strerror(errno)); | |
414 | new->fs_passno = fs->fs_passno; | |
415 | new->fs_freq = fs->fs_freq; | |
416 | return (new); | |
417 | } | |
418 | ||
419 | struct pfstab { | |
420 | struct pfstab *pf_next; | |
421 | struct fstab *pf_fstab; | |
422 | }; | |
423 | ||
424 | static struct pfstab *table; | |
425 | ||
426 | void | |
427 | getfstab() | |
428 | { | |
429 | register struct fstab *fs; | |
430 | register struct pfstab *pf; | |
431 | ||
432 | if (setfsent() == 0) { | |
433 | msg("Can't open %s for dump table information: %s\n", | |
434 | _PATH_FSTAB, strerror(errno)); | |
435 | return; | |
436 | } | |
437 | while (fs = getfsent()) { | |
438 | if (strcmp(fs->fs_type, FSTAB_RW) && | |
439 | strcmp(fs->fs_type, FSTAB_RO) && | |
440 | strcmp(fs->fs_type, FSTAB_RQ)) | |
441 | continue; | |
442 | fs = allocfsent(fs); | |
443 | if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) | |
444 | quit("%s\n", strerror(errno)); | |
445 | pf->pf_fstab = fs; | |
446 | pf->pf_next = table; | |
447 | table = pf; | |
448 | } | |
449 | endfsent(); | |
450 | } | |
451 | ||
452 | /* | |
453 | * Search in the fstab for a file name. | |
454 | * This file name can be either the special or the path file name. | |
455 | * | |
456 | * The entries in the fstab are the BLOCK special names, not the | |
457 | * character special names. | |
458 | * The caller of fstabsearch assures that the character device | |
459 | * is dumped (that is much faster) | |
460 | * | |
461 | * The file name can omit the leading '/'. | |
462 | */ | |
463 | struct fstab * | |
464 | fstabsearch(key) | |
465 | char *key; | |
466 | { | |
467 | register struct pfstab *pf; | |
468 | register struct fstab *fs; | |
469 | char *rawname(); | |
470 | ||
471 | for (pf = table; pf != NULL; pf = pf->pf_next) { | |
78ed81a3 | 472 | char *disk; |
15637ed4 RG |
473 | fs = pf->pf_fstab; |
474 | if (strcmp(fs->fs_file, key) == 0 || | |
475 | strcmp(fs->fs_spec, key) == 0 || | |
78ed81a3 | 476 | ((disk = rawname(fs->fs_spec)) && |
477 | strcmp(disk, key) == 0)) | |
15637ed4 RG |
478 | return (fs); |
479 | if (key[0] != '/') { | |
480 | if (*fs->fs_spec == '/' && | |
481 | strcmp(fs->fs_spec + 1, key) == 0) | |
482 | return (fs); | |
483 | if (*fs->fs_file == '/' && | |
484 | strcmp(fs->fs_file + 1, key) == 0) | |
485 | return (fs); | |
486 | } | |
487 | } | |
488 | return (NULL); | |
489 | } | |
490 | ||
491 | /* | |
492 | * Tell the operator what to do | |
493 | */ | |
494 | void | |
495 | lastdump(arg) | |
496 | char arg; /* w ==> just what to do; W ==> most recent dumps */ | |
497 | { | |
498 | register int i; | |
499 | register struct fstab *dt; | |
500 | register struct dumpdates *dtwalk; | |
501 | char *lastname, *date; | |
502 | int dumpme, datesort(); | |
503 | time_t tnow; | |
504 | ||
505 | time(&tnow); | |
506 | getfstab(); /* /etc/fstab input */ | |
507 | initdumptimes(); /* /etc/dumpdates input */ | |
508 | qsort(ddatev, nddates, sizeof(struct dumpdates *), datesort); | |
509 | ||
510 | if (arg == 'w') | |
511 | (void) printf("Dump these file systems:\n"); | |
512 | else | |
513 | (void) printf("Last dump(s) done (Dump '>' file systems):\n"); | |
514 | lastname = "??"; | |
515 | ITITERATE(i, dtwalk) { | |
516 | if (strncmp(lastname, dtwalk->dd_name, | |
517 | sizeof(dtwalk->dd_name)) == 0) | |
518 | continue; | |
519 | date = (char *)ctime(&dtwalk->dd_ddate); | |
520 | date[16] = '\0'; /* blast away seconds and year */ | |
521 | lastname = dtwalk->dd_name; | |
522 | dt = fstabsearch(dtwalk->dd_name); | |
523 | dumpme = (dt != NULL && | |
524 | dt->fs_freq != 0 && | |
525 | dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY)); | |
526 | if (arg != 'w' || dumpme) | |
527 | (void) printf( | |
528 | "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", | |
529 | dumpme && (arg != 'w') ? '>' : ' ', | |
530 | dtwalk->dd_name, | |
531 | dt ? dt->fs_file : "", | |
532 | dtwalk->dd_level, | |
533 | date); | |
534 | } | |
535 | } | |
536 | ||
537 | int | |
538 | datesort(a1, a2) | |
539 | void *a1, *a2; | |
540 | { | |
541 | struct dumpdates *d1 = *(struct dumpdates **)a1; | |
542 | struct dumpdates *d2 = *(struct dumpdates **)a2; | |
543 | int diff; | |
544 | ||
545 | diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); | |
546 | if (diff == 0) | |
547 | return (d2->dd_ddate - d1->dd_ddate); | |
548 | return (diff); | |
549 | } | |
550 | ||
551 | int max(a, b) | |
552 | int a, b; | |
553 | { | |
554 | return (a > b ? a : b); | |
555 | } | |
556 | int min(a, b) | |
557 | int a, b; | |
558 | { | |
559 | return (a < b ? a : b); | |
560 | } |