147082Smckusick /*-
261484Sbostic * Copyright (c) 1980, 1988, 1993
361484Sbostic * The Regents of the University of California. All rights reserved.
447082Smckusick *
547082Smckusick * %sccs.include.redist.c%
622038Sdist */
75319Smckusic
822038Sdist #ifndef lint
9*65541Smckusick static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 01/06/94";
1046588Storek #endif /* not lint */
1122038Sdist
1246795Sbostic #include <sys/param.h>
1346588Storek #include <sys/wait.h>
1450497Smckusick #include <sys/time.h>
1557725Smckusick
1657725Smckusick #include <errno.h>
1746795Sbostic #include <fstab.h>
1846588Storek #include <grp.h>
1957725Smckusick #include <signal.h>
2057725Smckusick #include <stdio.h>
2146795Sbostic #ifdef __STDC__
2246795Sbostic #include <stdlib.h>
2346795Sbostic #include <string.h>
2450652Storek #include <stdarg.h>
2557725Smckusick #endif
2657725Smckusick #include <tzfile.h>
2757725Smckusick #ifdef __STDC__
2857725Smckusick #include <unistd.h>
2957725Smckusick #endif
3057725Smckusick #include <utmp.h>
3157725Smckusick #ifndef __STDC__
3250652Storek #include <varargs.h>
3346795Sbostic #endif
3457725Smckusick
3546795Sbostic #include "dump.h"
3639130Smckusick #include "pathnames.h"
371424Sroot
3857725Smckusick void alarmcatch __P((/* int, int */));
3957725Smckusick int datesort __P((const void *, const void *));
4057725Smckusick static void sendmes __P((char *, char *));
4146588Storek
421424Sroot /*
4339130Smckusick * Query the operator; This previously-fascist piece of code
4439130Smckusick * no longer requires an exact response.
451424Sroot * It is intended to protect dump aborting by inquisitive
461424Sroot * people banging on the console terminal to see what is
471424Sroot * happening which might cause dump to croak, destroying
481424Sroot * a large number of hours of work.
491424Sroot *
501424Sroot * Every 2 minutes we reprint the message, alerting others
511424Sroot * that dump needs attention.
521424Sroot */
5357725Smckusick static int timeout;
5457725Smckusick static char *attnmessage; /* attention message */
5546588Storek
5646588Storek int
query(question)571424Sroot query(question)
581424Sroot char *question;
591424Sroot {
601424Sroot char replybuffer[64];
6146588Storek int back, errcount;
621424Sroot FILE *mytty;
631424Sroot
6446588Storek if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
6546588Storek quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
661424Sroot attnmessage = question;
671424Sroot timeout = 0;
681424Sroot alarmcatch();
6946588Storek back = -1;
7046588Storek errcount = 0;
7146588Storek do {
7246588Storek if (fgets(replybuffer, 63, mytty) == NULL) {
7346588Storek clearerr(mytty);
7446588Storek if (++errcount > 30) /* XXX ugly */
7546588Storek quit("excessive operator query failures\n");
7639130Smckusick } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
7746588Storek back = 1;
7839130Smckusick } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
7946588Storek back = 0;
801424Sroot } else {
8146588Storek (void) fprintf(stderr,
8246588Storek " DUMP: \"Yes\" or \"No\"?\n");
8346588Storek (void) fprintf(stderr,
8446588Storek " DUMP: %s: (\"yes\" or \"no\") ", question);
851424Sroot }
8646588Storek } while (back < 0);
8746588Storek
881424Sroot /*
891424Sroot * Turn off the alarm, and reset the signal to trap out..
901424Sroot */
9154043Smckusick (void) alarm(0);
9255288Sbostic if (signal(SIGALRM, sig) == SIG_IGN)
931424Sroot signal(SIGALRM, SIG_IGN);
9454043Smckusick (void) fclose(mytty);
951424Sroot return(back);
961424Sroot }
9739130Smckusick
9839130Smckusick char lastmsg[100];
9939130Smckusick
1001424Sroot /*
1011424Sroot * Alert the console operator, and enable the alarm clock to
1021424Sroot * sleep for 2 minutes in case nobody comes to satisfy dump
1031424Sroot */
10457725Smckusick void
alarmcatch()1051424Sroot alarmcatch()
1061424Sroot {
10739130Smckusick if (notify == 0) {
10839130Smckusick if (timeout == 0)
10946588Storek (void) fprintf(stderr,
11046588Storek " DUMP: %s: (\"yes\" or \"no\") ",
11139130Smckusick attnmessage);
11239130Smckusick else
11339130Smckusick msgtail("\7\7");
11439130Smckusick } else {
11539130Smckusick if (timeout) {
11639130Smckusick msgtail("\n");
11739130Smckusick broadcast(""); /* just print last msg */
11839130Smckusick }
11946588Storek (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
12039130Smckusick attnmessage);
12139130Smckusick }
1221424Sroot signal(SIGALRM, alarmcatch);
12354043Smckusick (void) alarm(120);
1241424Sroot timeout = 1;
1251424Sroot }
12646588Storek
1271424Sroot /*
1281424Sroot * Here if an inquisitive operator interrupts the dump program
1291424Sroot */
13046588Storek void
interrupt(signo)13155288Sbostic interrupt(signo)
13255288Sbostic int signo;
1331424Sroot {
13421124Smckusick msg("Interrupt received.\n");
13521124Smckusick if (query("Do you want to abort dump?"))
13655288Sbostic dumpabort(0);
1371424Sroot }
1381424Sroot
1391424Sroot /*
1401424Sroot * The following variables and routines manage alerting
1411424Sroot * operators to the status of dump.
1421424Sroot * This works much like wall(1) does.
1431424Sroot */
14446588Storek struct group *gp;
1451424Sroot
1461424Sroot /*
1471424Sroot * Get the names from the group entry "operator" to notify.
1481424Sroot */
14946588Storek void
set_operators()1501424Sroot set_operators()
1511424Sroot {
1521424Sroot if (!notify) /*not going to notify*/
1531424Sroot return;
1541424Sroot gp = getgrnam(OPGRENT);
15554043Smckusick (void) endgrent();
15646588Storek if (gp == NULL) {
15746588Storek msg("No group entry for %s.\n", OPGRENT);
1581424Sroot notify = 0;
1591424Sroot return;
1601424Sroot }
1611424Sroot }
1621424Sroot
1631424Sroot struct tm *localclock;
1641424Sroot
1651424Sroot /*
1661424Sroot * We fork a child to do the actual broadcasting, so
1671424Sroot * that the process control groups are not messed up
1681424Sroot */
16946588Storek void
broadcast(message)1701424Sroot broadcast(message)
1711424Sroot char *message;
1721424Sroot {
1731424Sroot time_t clock;
1741424Sroot FILE *f_utmp;
1751424Sroot struct utmp utmp;
1761424Sroot char **np;
1771424Sroot int pid, s;
1781424Sroot
17946588Storek if (!notify || gp == NULL)
18046588Storek return;
18146588Storek
1821424Sroot switch (pid = fork()) {
1831424Sroot case -1:
1841424Sroot return;
1851424Sroot case 0:
1861424Sroot break;
1871424Sroot default:
1881424Sroot while (wait(&s) != pid)
1891424Sroot continue;
1901424Sroot return;
1911424Sroot }
1921424Sroot
19354043Smckusick clock = time((time_t *)0);
1941424Sroot localclock = localtime(&clock);
1951424Sroot
19646588Storek if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
19746588Storek msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
1981424Sroot return;
1991424Sroot }
2001424Sroot
20146588Storek while (!feof(f_utmp)) {
20254043Smckusick if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
2031424Sroot break;
2041424Sroot if (utmp.ut_name[0] == 0)
2051424Sroot continue;
20646588Storek for (np = gp->gr_mem; *np; np++) {
2071424Sroot if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
2081424Sroot continue;
2091424Sroot /*
2101424Sroot * Do not send messages to operators on dialups
2111424Sroot */
2121424Sroot if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
2131424Sroot continue;
2141424Sroot #ifdef DEBUG
21546588Storek msg("Message to %s at %s\n", *np, utmp.ut_line);
21646588Storek #endif
2171424Sroot sendmes(utmp.ut_line, message);
2181424Sroot }
2191424Sroot }
22046588Storek (void) fclose(f_utmp);
2211424Sroot Exit(0); /* the wait in this same routine will catch this */
2221424Sroot /* NOTREACHED */
2231424Sroot }
2241424Sroot
22546588Storek static void
sendmes(tty,message)2261424Sroot sendmes(tty, message)
2275319Smckusic char *tty, *message;
2281424Sroot {
2291424Sroot char t[50], buf[BUFSIZ];
2301424Sroot register char *cp;
23139130Smckusick int lmsg = 1;
2321424Sroot FILE *f_tty;
2331424Sroot
23446588Storek (void) strcpy(t, _PATH_DEV);
23546588Storek (void) strcat(t, tty);
2361424Sroot
23746588Storek if ((f_tty = fopen(t, "w")) != NULL) {
2381424Sroot setbuf(f_tty, buf);
23946588Storek (void) fprintf(f_tty,
24046588Storek "\n\
24146588Storek \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
24246588Storek DUMP: NEEDS ATTENTION: ",
24346588Storek localclock->tm_hour, localclock->tm_min);
24439130Smckusick for (cp = lastmsg; ; cp++) {
24539130Smckusick if (*cp == '\0') {
24639130Smckusick if (lmsg) {
24739130Smckusick cp = message;
24839130Smckusick if (*cp == '\0')
24939130Smckusick break;
25039130Smckusick lmsg = 0;
25139130Smckusick } else
25239130Smckusick break;
25339130Smckusick }
25439130Smckusick if (*cp == '\n')
25546588Storek (void) putc('\r', f_tty);
25646588Storek (void) putc(*cp, f_tty);
2571424Sroot }
25846588Storek (void) fclose(f_tty);
2591424Sroot }
2601424Sroot }
2611424Sroot
2621424Sroot /*
2631424Sroot * print out an estimate of the amount of time left to do the dump
2641424Sroot */
2651424Sroot
2661424Sroot time_t tschedule = 0;
2671424Sroot
26846588Storek void
timeest()2691424Sroot timeest()
2701424Sroot {
2711424Sroot time_t tnow, deltat;
2721424Sroot
27354043Smckusick (void) time((time_t *) &tnow);
27446588Storek if (tnow >= tschedule) {
2751424Sroot tschedule = tnow + 300;
2761424Sroot if (blockswritten < 500)
2771424Sroot return;
2781424Sroot deltat = tstart_writing - tnow +
27946788Smckusick (1.0 * (tnow - tstart_writing))
28046788Smckusick / blockswritten * tapesize;
2811424Sroot msg("%3.2f%% done, finished in %d:%02d\n",
28246788Smckusick (blockswritten * 100.0) / tapesize,
28346788Smckusick deltat / 3600, (deltat % 3600) / 60);
2841424Sroot }
2851424Sroot }
2861424Sroot
28750664Sbostic void
28850652Storek #if __STDC__
msg(const char * fmt,...)28950664Sbostic msg(const char *fmt, ...)
29050652Storek #else
29150664Sbostic msg(fmt, va_alist)
29250664Sbostic char *fmt;
29350664Sbostic va_dcl
29450652Storek #endif
2951424Sroot {
29650664Sbostic va_list ap;
29746588Storek
29846588Storek (void) fprintf(stderr," DUMP: ");
2991424Sroot #ifdef TDEBUG
30046588Storek (void) fprintf(stderr, "pid=%d ", getpid());
3011424Sroot #endif
30250664Sbostic #if __STDC__
30350664Sbostic va_start(ap, fmt);
30450664Sbostic #else
30550664Sbostic va_start(ap);
30650664Sbostic #endif
30746588Storek (void) vfprintf(stderr, fmt, ap);
30846588Storek (void) fflush(stdout);
30946588Storek (void) fflush(stderr);
31046588Storek (void) vsprintf(lastmsg, fmt, ap);
31146588Storek va_end(ap);
3121424Sroot }
3131424Sroot
31450664Sbostic void
31550664Sbostic #if __STDC__
msgtail(const char * fmt,...)31650664Sbostic msgtail(const char *fmt, ...)
31750664Sbostic #else
31850664Sbostic msgtail(fmt, va_alist)
31950664Sbostic char *fmt;
32050664Sbostic va_dcl
32150664Sbostic #endif
3221424Sroot {
32350664Sbostic va_list ap;
32450664Sbostic #if __STDC__
32550664Sbostic va_start(ap, fmt);
32650664Sbostic #else
32750664Sbostic va_start(ap);
32850664Sbostic #endif
32946588Storek (void) vfprintf(stderr, fmt, ap);
33046588Storek va_end(ap);
3311424Sroot }
33246588Storek
33350664Sbostic void
33450664Sbostic #if __STDC__
quit(const char * fmt,...)33550664Sbostic quit(const char *fmt, ...)
33650664Sbostic #else
33750664Sbostic quit(fmt, va_alist)
33850664Sbostic char *fmt;
33950664Sbostic va_dcl
34050664Sbostic #endif
34146588Storek {
34250664Sbostic va_list ap;
34346588Storek
34446588Storek (void) fprintf(stderr," DUMP: ");
34546588Storek #ifdef TDEBUG
34646588Storek (void) fprintf(stderr, "pid=%d ", getpid());
34746588Storek #endif
34850664Sbostic #if __STDC__
34950664Sbostic va_start(ap, fmt);
35050664Sbostic #else
35150664Sbostic va_start(ap);
35250664Sbostic #endif
35354043Smckusick (void) vfprintf(stderr, fmt, ap);
35446588Storek va_end(ap);
35546588Storek (void) fflush(stdout);
35646588Storek (void) fflush(stderr);
35755288Sbostic dumpabort(0);
35846588Storek }
35946588Storek
3601424Sroot /*
3611424Sroot * Tell the operator what has to be done;
3621424Sroot * we don't actually do it
3631424Sroot */
3641424Sroot
36513047Ssam struct fstab *
allocfsent(fs)36613047Ssam allocfsent(fs)
36713047Ssam register struct fstab *fs;
36813047Ssam {
36913047Ssam register struct fstab *new;
37013047Ssam
37113047Ssam new = (struct fstab *)malloc(sizeof (*fs));
37246588Storek if (new == NULL ||
37346588Storek (new->fs_file = strdup(fs->fs_file)) == NULL ||
37446588Storek (new->fs_type = strdup(fs->fs_type)) == NULL ||
37546588Storek (new->fs_spec = strdup(fs->fs_spec)) == NULL)
37646588Storek quit("%s\n", strerror(errno));
37713047Ssam new->fs_passno = fs->fs_passno;
37813047Ssam new->fs_freq = fs->fs_freq;
37913047Ssam return (new);
38013047Ssam }
38113047Ssam
38213047Ssam struct pfstab {
38313047Ssam struct pfstab *pf_next;
38413047Ssam struct fstab *pf_fstab;
38513047Ssam };
38613047Ssam
38746588Storek static struct pfstab *table;
38813047Ssam
38946588Storek void
getfstab()3901424Sroot getfstab()
3911424Sroot {
39213047Ssam register struct fstab *fs;
39313047Ssam register struct pfstab *pf;
3941424Sroot
3951428Sroot if (setfsent() == 0) {
39646588Storek msg("Can't open %s for dump table information: %s\n",
39746588Storek _PATH_FSTAB, strerror(errno));
39813047Ssam return;
3991424Sroot }
40059922Storek while ((fs = getfsent()) != NULL) {
40113047Ssam if (strcmp(fs->fs_type, FSTAB_RW) &&
40213047Ssam strcmp(fs->fs_type, FSTAB_RO) &&
40313047Ssam strcmp(fs->fs_type, FSTAB_RQ))
40413047Ssam continue;
40513047Ssam fs = allocfsent(fs);
40646588Storek if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
40746588Storek quit("%s\n", strerror(errno));
40813047Ssam pf->pf_fstab = fs;
40913047Ssam pf->pf_next = table;
41013047Ssam table = pf;
41113047Ssam }
41254043Smckusick (void) endfsent();
4131424Sroot }
4141424Sroot
4151424Sroot /*
41613047Ssam * Search in the fstab for a file name.
41713047Ssam * This file name can be either the special or the path file name.
4181424Sroot *
41913047Ssam * The entries in the fstab are the BLOCK special names, not the
42013047Ssam * character special names.
42113047Ssam * The caller of fstabsearch assures that the character device
42213047Ssam * is dumped (that is much faster)
4231424Sroot *
42413047Ssam * The file name can omit the leading '/'.
4251424Sroot */
42613047Ssam struct fstab *
fstabsearch(key)42713047Ssam fstabsearch(key)
42813047Ssam char *key;
4291424Sroot {
43013047Ssam register struct pfstab *pf;
43113047Ssam register struct fstab *fs;
432*65541Smckusick char *rn;
4331424Sroot
43446588Storek for (pf = table; pf != NULL; pf = pf->pf_next) {
43513047Ssam fs = pf->pf_fstab;
43646588Storek if (strcmp(fs->fs_file, key) == 0 ||
437*65541Smckusick strcmp(fs->fs_spec, key) == 0)
43813047Ssam return (fs);
439*65541Smckusick rn = rawname(fs->fs_spec);
440*65541Smckusick if (rn != NULL && strcmp(rn, key) == 0)
441*65541Smckusick return (fs);
44246588Storek if (key[0] != '/') {
44313047Ssam if (*fs->fs_spec == '/' &&
44413197Sroot strcmp(fs->fs_spec + 1, key) == 0)
44513047Ssam return (fs);
44613047Ssam if (*fs->fs_file == '/' &&
44713197Sroot strcmp(fs->fs_file + 1, key) == 0)
44813047Ssam return (fs);
4491424Sroot }
4501424Sroot }
45146588Storek return (NULL);
4521424Sroot }
4531424Sroot
4541424Sroot /*
4551424Sroot * Tell the operator what to do
4561424Sroot */
45746588Storek void
lastdump(arg)4581463Sroot lastdump(arg)
45946588Storek char arg; /* w ==> just what to do; W ==> most recent dumps */
4601424Sroot {
46146788Smckusick register int i;
46246788Smckusick register struct fstab *dt;
46346788Smckusick register struct dumpdates *dtwalk;
46446788Smckusick char *lastname, *date;
46557725Smckusick int dumpme;
46646788Smckusick time_t tnow;
4671424Sroot
46854043Smckusick (void) time(&tnow);
4691424Sroot getfstab(); /* /etc/fstab input */
47046788Smckusick initdumptimes(); /* /etc/dumpdates input */
47154043Smckusick qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
4721424Sroot
4731463Sroot if (arg == 'w')
47446588Storek (void) printf("Dump these file systems:\n");
4751463Sroot else
47646588Storek (void) printf("Last dump(s) done (Dump '>' file systems):\n");
4771424Sroot lastname = "??";
47846788Smckusick ITITERATE(i, dtwalk) {
47946788Smckusick if (strncmp(lastname, dtwalk->dd_name,
48046788Smckusick sizeof(dtwalk->dd_name)) == 0)
4811424Sroot continue;
48246788Smckusick date = (char *)ctime(&dtwalk->dd_ddate);
48346588Storek date[16] = '\0'; /* blast away seconds and year */
48446788Smckusick lastname = dtwalk->dd_name;
48546788Smckusick dt = fstabsearch(dtwalk->dd_name);
48646588Storek dumpme = (dt != NULL &&
48746588Storek dt->fs_freq != 0 &&
48846816Sbostic dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
48946588Storek if (arg != 'w' || dumpme)
49046588Storek (void) printf(
49146588Storek "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
49246588Storek dumpme && (arg != 'w') ? '>' : ' ',
49346788Smckusick dtwalk->dd_name,
49446588Storek dt ? dt->fs_file : "",
49546788Smckusick dtwalk->dd_level,
49646588Storek date);
4971424Sroot }
4981424Sroot }
4991424Sroot
50046788Smckusick int
datesort(a1,a2)50146788Smckusick datesort(a1, a2)
50257725Smckusick const void *a1, *a2;
5031424Sroot {
50446788Smckusick struct dumpdates *d1 = *(struct dumpdates **)a1;
50546788Smckusick struct dumpdates *d2 = *(struct dumpdates **)a2;
50646588Storek int diff;
5071424Sroot
50846788Smckusick diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
5091424Sroot if (diff == 0)
51046788Smckusick return (d2->dd_ddate - d1->dd_ddate);
51146788Smckusick return (diff);
5121424Sroot }
513