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