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