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