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