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