147082Smckusick /*- 247082Smckusick * Copyright (c) 1980, 1988 The Regents of the University of California. 347082Smckusick * All rights reserved. 447082Smckusick * 547082Smckusick * %sccs.include.redist.c% 622038Sdist */ 75319Smckusic 822038Sdist #ifndef lint 9*54043Smckusick static char sccsid[] = "@(#)optr.c 5.13 (Berkeley) 06/18/92"; 1046588Storek #endif /* not lint */ 1122038Sdist 1250497Smckusick #ifdef sunos 1350497Smckusick #include <stdio.h> 1450497Smckusick #include <ctype.h> 1546795Sbostic #include <sys/param.h> 1646588Storek #include <sys/wait.h> 1750497Smckusick #include <sys/stat.h> 1850497Smckusick #include <sys/time.h> 1950497Smckusick #else 2050497Smckusick #include <sys/param.h> 2150497Smckusick #include <sys/wait.h> 2250497Smckusick #include <stdio.h> 2350497Smckusick #endif 2446795Sbostic #include <signal.h> 2546795Sbostic #include <time.h> 2646795Sbostic #include <fstab.h> 2746588Storek #include <grp.h> 2846795Sbostic #include <utmp.h> 2946816Sbostic #include <tzfile.h> 3046795Sbostic #include <errno.h> 3146795Sbostic #ifdef __STDC__ 3246795Sbostic #include <unistd.h> 3346795Sbostic #include <stdlib.h> 3446795Sbostic #include <string.h> 3550652Storek #include <stdarg.h> 3650652Storek #else 3750652Storek #include <varargs.h> 3846795Sbostic #endif 3946795Sbostic #include "dump.h" 4039130Smckusick #include "pathnames.h" 411424Sroot 4246588Storek static void alarmcatch(); 4346588Storek static void sendmes(); 4446588Storek 451424Sroot /* 4639130Smckusick * Query the operator; This previously-fascist piece of code 4739130Smckusick * no longer requires an exact response. 481424Sroot * It is intended to protect dump aborting by inquisitive 491424Sroot * people banging on the console terminal to see what is 501424Sroot * happening which might cause dump to croak, destroying 511424Sroot * a large number of hours of work. 521424Sroot * 531424Sroot * Every 2 minutes we reprint the message, alerting others 541424Sroot * that dump needs attention. 551424Sroot */ 561424Sroot int timeout; 5721124Smckusick char *attnmessage; /* attention message */ 5846588Storek 5946588Storek int 601424Sroot query(question) 611424Sroot char *question; 621424Sroot { 631424Sroot char replybuffer[64]; 6446588Storek int back, errcount; 651424Sroot FILE *mytty; 661424Sroot 6746588Storek if ((mytty = fopen(_PATH_TTY, "r")) == NULL) 6846588Storek quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); 691424Sroot attnmessage = question; 701424Sroot timeout = 0; 711424Sroot alarmcatch(); 7246588Storek back = -1; 7346588Storek errcount = 0; 7446588Storek do { 7546588Storek if (fgets(replybuffer, 63, mytty) == NULL) { 7646588Storek clearerr(mytty); 7746588Storek if (++errcount > 30) /* XXX ugly */ 7846588Storek quit("excessive operator query failures\n"); 7939130Smckusick } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { 8046588Storek back = 1; 8139130Smckusick } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { 8246588Storek back = 0; 831424Sroot } else { 8446588Storek (void) fprintf(stderr, 8546588Storek " DUMP: \"Yes\" or \"No\"?\n"); 8646588Storek (void) fprintf(stderr, 8746588Storek " DUMP: %s: (\"yes\" or \"no\") ", question); 881424Sroot } 8946588Storek } while (back < 0); 9046588Storek 911424Sroot /* 921424Sroot * Turn off the alarm, and reset the signal to trap out.. 931424Sroot */ 94*54043Smckusick (void) alarm(0); 951424Sroot if (signal(SIGALRM, sigalrm) == SIG_IGN) 961424Sroot signal(SIGALRM, SIG_IGN); 97*54043Smckusick (void) fclose(mytty); 981424Sroot return(back); 991424Sroot } 10039130Smckusick 10139130Smckusick char lastmsg[100]; 10239130Smckusick 1031424Sroot /* 1041424Sroot * Alert the console operator, and enable the alarm clock to 1051424Sroot * sleep for 2 minutes in case nobody comes to satisfy dump 1061424Sroot */ 10746588Storek static void 1081424Sroot alarmcatch() 1091424Sroot { 11039130Smckusick if (notify == 0) { 11139130Smckusick if (timeout == 0) 11246588Storek (void) fprintf(stderr, 11346588Storek " DUMP: %s: (\"yes\" or \"no\") ", 11439130Smckusick attnmessage); 11539130Smckusick else 11639130Smckusick msgtail("\7\7"); 11739130Smckusick } else { 11839130Smckusick if (timeout) { 11939130Smckusick msgtail("\n"); 12039130Smckusick broadcast(""); /* just print last msg */ 12139130Smckusick } 12246588Storek (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 12339130Smckusick attnmessage); 12439130Smckusick } 1251424Sroot signal(SIGALRM, alarmcatch); 126*54043Smckusick (void) alarm(120); 1271424Sroot timeout = 1; 1281424Sroot } 12946588Storek 1301424Sroot /* 1311424Sroot * Here if an inquisitive operator interrupts the dump program 1321424Sroot */ 13346588Storek void 1341424Sroot interrupt() 1351424Sroot { 13621124Smckusick msg("Interrupt received.\n"); 13721124Smckusick if (query("Do you want to abort dump?")) 1381424Sroot dumpabort(); 1391424Sroot } 1401424Sroot 1411424Sroot /* 1421424Sroot * The following variables and routines manage alerting 1431424Sroot * operators to the status of dump. 1441424Sroot * This works much like wall(1) does. 1451424Sroot */ 14646588Storek struct group *gp; 1471424Sroot 1481424Sroot /* 1491424Sroot * Get the names from the group entry "operator" to notify. 1501424Sroot */ 15146588Storek void 1521424Sroot set_operators() 1531424Sroot { 1541424Sroot if (!notify) /*not going to notify*/ 1551424Sroot return; 1561424Sroot gp = getgrnam(OPGRENT); 157*54043Smckusick (void) endgrent(); 15846588Storek if (gp == NULL) { 15946588Storek msg("No group entry for %s.\n", OPGRENT); 1601424Sroot notify = 0; 1611424Sroot return; 1621424Sroot } 1631424Sroot } 1641424Sroot 1651424Sroot struct tm *localtime(); 1661424Sroot struct tm *localclock; 1671424Sroot 1681424Sroot /* 1691424Sroot * We fork a child to do the actual broadcasting, so 1701424Sroot * that the process control groups are not messed up 1711424Sroot */ 17246588Storek void 1731424Sroot broadcast(message) 1741424Sroot char *message; 1751424Sroot { 1761424Sroot time_t clock; 1771424Sroot FILE *f_utmp; 1781424Sroot struct utmp utmp; 1791424Sroot char **np; 1801424Sroot int pid, s; 1811424Sroot 18246588Storek if (!notify || gp == NULL) 18346588Storek return; 18446588Storek 1851424Sroot switch (pid = fork()) { 1861424Sroot case -1: 1871424Sroot return; 1881424Sroot case 0: 1891424Sroot break; 1901424Sroot default: 1911424Sroot while (wait(&s) != pid) 1921424Sroot continue; 1931424Sroot return; 1941424Sroot } 1951424Sroot 196*54043Smckusick clock = time((time_t *)0); 1971424Sroot localclock = localtime(&clock); 1981424Sroot 19946588Storek if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) { 20046588Storek msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno)); 2011424Sroot return; 2021424Sroot } 2031424Sroot 20446588Storek while (!feof(f_utmp)) { 205*54043Smckusick if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1) 2061424Sroot break; 2071424Sroot if (utmp.ut_name[0] == 0) 2081424Sroot continue; 20946588Storek for (np = gp->gr_mem; *np; np++) { 2101424Sroot if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0) 2111424Sroot continue; 2121424Sroot /* 2131424Sroot * Do not send messages to operators on dialups 2141424Sroot */ 2151424Sroot if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0) 2161424Sroot continue; 2171424Sroot #ifdef DEBUG 21846588Storek msg("Message to %s at %s\n", *np, utmp.ut_line); 21946588Storek #endif 2201424Sroot sendmes(utmp.ut_line, message); 2211424Sroot } 2221424Sroot } 22346588Storek (void) fclose(f_utmp); 2241424Sroot Exit(0); /* the wait in this same routine will catch this */ 2251424Sroot /* NOTREACHED */ 2261424Sroot } 2271424Sroot 22846588Storek static void 2291424Sroot sendmes(tty, message) 2305319Smckusic char *tty, *message; 2311424Sroot { 2321424Sroot char t[50], buf[BUFSIZ]; 2331424Sroot register char *cp; 23439130Smckusick int lmsg = 1; 2351424Sroot FILE *f_tty; 2361424Sroot 23746588Storek (void) strcpy(t, _PATH_DEV); 23846588Storek (void) strcat(t, tty); 2391424Sroot 24046588Storek if ((f_tty = fopen(t, "w")) != NULL) { 2411424Sroot setbuf(f_tty, buf); 24246588Storek (void) fprintf(f_tty, 24346588Storek "\n\ 24446588Storek \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\ 24546588Storek DUMP: NEEDS ATTENTION: ", 24646588Storek localclock->tm_hour, localclock->tm_min); 24739130Smckusick for (cp = lastmsg; ; cp++) { 24839130Smckusick if (*cp == '\0') { 24939130Smckusick if (lmsg) { 25039130Smckusick cp = message; 25139130Smckusick if (*cp == '\0') 25239130Smckusick break; 25339130Smckusick lmsg = 0; 25439130Smckusick } else 25539130Smckusick break; 25639130Smckusick } 25739130Smckusick if (*cp == '\n') 25846588Storek (void) putc('\r', f_tty); 25946588Storek (void) putc(*cp, f_tty); 2601424Sroot } 26146588Storek (void) fclose(f_tty); 2621424Sroot } 2631424Sroot } 2641424Sroot 2651424Sroot /* 2661424Sroot * print out an estimate of the amount of time left to do the dump 2671424Sroot */ 2681424Sroot 2691424Sroot time_t tschedule = 0; 2701424Sroot 27146588Storek void 2721424Sroot timeest() 2731424Sroot { 2741424Sroot time_t tnow, deltat; 2751424Sroot 276*54043Smckusick (void) time((time_t *) &tnow); 27746588Storek if (tnow >= tschedule) { 2781424Sroot tschedule = tnow + 300; 2791424Sroot if (blockswritten < 500) 2801424Sroot return; 2811424Sroot deltat = tstart_writing - tnow + 28246788Smckusick (1.0 * (tnow - tstart_writing)) 28346788Smckusick / blockswritten * tapesize; 2841424Sroot msg("%3.2f%% done, finished in %d:%02d\n", 28546788Smckusick (blockswritten * 100.0) / tapesize, 28646788Smckusick deltat / 3600, (deltat % 3600) / 60); 2871424Sroot } 2881424Sroot } 2891424Sroot 29050664Sbostic void 29150652Storek #if __STDC__ 29250664Sbostic msg(const char *fmt, ...) 29350652Storek #else 29450664Sbostic msg(fmt, va_alist) 29550664Sbostic char *fmt; 29650664Sbostic va_dcl 29750652Storek #endif 2981424Sroot { 29950664Sbostic va_list ap; 30046588Storek 30146588Storek (void) fprintf(stderr," DUMP: "); 3021424Sroot #ifdef TDEBUG 30346588Storek (void) fprintf(stderr, "pid=%d ", getpid()); 3041424Sroot #endif 30550664Sbostic #if __STDC__ 30650664Sbostic va_start(ap, fmt); 30750664Sbostic #else 30850664Sbostic va_start(ap); 30950664Sbostic #endif 31046588Storek (void) vfprintf(stderr, fmt, ap); 31146588Storek (void) fflush(stdout); 31246588Storek (void) fflush(stderr); 31346588Storek (void) vsprintf(lastmsg, fmt, ap); 31446588Storek va_end(ap); 3151424Sroot } 3161424Sroot 31750664Sbostic void 31850664Sbostic #if __STDC__ 31950664Sbostic msgtail(const char *fmt, ...) 32050664Sbostic #else 32150664Sbostic msgtail(fmt, va_alist) 32250664Sbostic char *fmt; 32350664Sbostic va_dcl 32450664Sbostic #endif 3251424Sroot { 32650664Sbostic va_list ap; 32750664Sbostic #if __STDC__ 32850664Sbostic va_start(ap, fmt); 32950664Sbostic #else 33050664Sbostic va_start(ap); 33150664Sbostic #endif 33246588Storek (void) vfprintf(stderr, fmt, ap); 33346588Storek va_end(ap); 3341424Sroot } 33546588Storek 33650664Sbostic void 33750664Sbostic #if __STDC__ 33850664Sbostic quit(const char *fmt, ...) 33950664Sbostic #else 34050664Sbostic quit(fmt, va_alist) 34150664Sbostic char *fmt; 34250664Sbostic va_dcl 34350664Sbostic #endif 34446588Storek { 34550664Sbostic va_list ap; 34646588Storek 34746588Storek (void) fprintf(stderr," DUMP: "); 34846588Storek #ifdef TDEBUG 34946588Storek (void) fprintf(stderr, "pid=%d ", getpid()); 35046588Storek #endif 35150664Sbostic #if __STDC__ 35250664Sbostic va_start(ap, fmt); 35350664Sbostic #else 35450664Sbostic va_start(ap); 35550664Sbostic #endif 356*54043Smckusick (void) vfprintf(stderr, fmt, ap); 35746588Storek va_end(ap); 35846588Storek (void) fflush(stdout); 35946588Storek (void) fflush(stderr); 36046588Storek dumpabort(); 36146588Storek } 36246588Storek 3631424Sroot /* 3641424Sroot * Tell the operator what has to be done; 3651424Sroot * we don't actually do it 3661424Sroot */ 3671424Sroot 36813047Ssam struct fstab * 36913047Ssam allocfsent(fs) 37013047Ssam register struct fstab *fs; 37113047Ssam { 37213047Ssam register struct fstab *new; 37313047Ssam 37413047Ssam new = (struct fstab *)malloc(sizeof (*fs)); 37546588Storek if (new == NULL || 37646588Storek (new->fs_file = strdup(fs->fs_file)) == NULL || 37746588Storek (new->fs_type = strdup(fs->fs_type)) == NULL || 37846588Storek (new->fs_spec = strdup(fs->fs_spec)) == NULL) 37946588Storek quit("%s\n", strerror(errno)); 38013047Ssam new->fs_passno = fs->fs_passno; 38113047Ssam new->fs_freq = fs->fs_freq; 38213047Ssam return (new); 38313047Ssam } 38413047Ssam 38513047Ssam struct pfstab { 38613047Ssam struct pfstab *pf_next; 38713047Ssam struct fstab *pf_fstab; 38813047Ssam }; 38913047Ssam 39046588Storek static struct pfstab *table; 39113047Ssam 39246588Storek void 3931424Sroot getfstab() 3941424Sroot { 39513047Ssam register struct fstab *fs; 39613047Ssam register struct pfstab *pf; 3971424Sroot 3981428Sroot if (setfsent() == 0) { 39946588Storek msg("Can't open %s for dump table information: %s\n", 40046588Storek _PATH_FSTAB, strerror(errno)); 40113047Ssam return; 4021424Sroot } 40313047Ssam while (fs = getfsent()) { 40413047Ssam if (strcmp(fs->fs_type, FSTAB_RW) && 40513047Ssam strcmp(fs->fs_type, FSTAB_RO) && 40613047Ssam strcmp(fs->fs_type, FSTAB_RQ)) 40713047Ssam continue; 40813047Ssam fs = allocfsent(fs); 40946588Storek if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 41046588Storek quit("%s\n", strerror(errno)); 41113047Ssam pf->pf_fstab = fs; 41213047Ssam pf->pf_next = table; 41313047Ssam table = pf; 41413047Ssam } 415*54043Smckusick (void) endfsent(); 4161424Sroot } 4171424Sroot 4181424Sroot /* 41913047Ssam * Search in the fstab for a file name. 42013047Ssam * This file name can be either the special or the path file name. 4211424Sroot * 42213047Ssam * The entries in the fstab are the BLOCK special names, not the 42313047Ssam * character special names. 42413047Ssam * The caller of fstabsearch assures that the character device 42513047Ssam * is dumped (that is much faster) 4261424Sroot * 42713047Ssam * The file name can omit the leading '/'. 4281424Sroot */ 42913047Ssam struct fstab * 43013047Ssam fstabsearch(key) 43113047Ssam char *key; 4321424Sroot { 43313047Ssam register struct pfstab *pf; 43413047Ssam register struct fstab *fs; 43513047Ssam char *rawname(); 4361424Sroot 43746588Storek for (pf = table; pf != NULL; pf = pf->pf_next) { 43813047Ssam fs = pf->pf_fstab; 43946588Storek if (strcmp(fs->fs_file, key) == 0 || 44046588Storek strcmp(fs->fs_spec, key) == 0 || 44146588Storek strcmp(rawname(fs->fs_spec), key) == 0) 44213047Ssam return (fs); 44346588Storek if (key[0] != '/') { 44413047Ssam if (*fs->fs_spec == '/' && 44513197Sroot strcmp(fs->fs_spec + 1, key) == 0) 44613047Ssam return (fs); 44713047Ssam if (*fs->fs_file == '/' && 44813197Sroot strcmp(fs->fs_file + 1, key) == 0) 44913047Ssam return (fs); 4501424Sroot } 4511424Sroot } 45246588Storek return (NULL); 4531424Sroot } 4541424Sroot 4551424Sroot /* 4561424Sroot * Tell the operator what to do 4571424Sroot */ 45846588Storek void 4591463Sroot lastdump(arg) 46046588Storek char arg; /* w ==> just what to do; W ==> most recent dumps */ 4611424Sroot { 46246788Smckusick register int i; 46346788Smckusick register struct fstab *dt; 46446788Smckusick register struct dumpdates *dtwalk; 46546788Smckusick char *lastname, *date; 46646788Smckusick int dumpme, datesort(); 46746788Smckusick time_t tnow; 4681424Sroot 469*54043Smckusick (void) time(&tnow); 4701424Sroot getfstab(); /* /etc/fstab input */ 47146788Smckusick initdumptimes(); /* /etc/dumpdates input */ 472*54043Smckusick qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 4731424Sroot 4741463Sroot if (arg == 'w') 47546588Storek (void) printf("Dump these file systems:\n"); 4761463Sroot else 47746588Storek (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 4781424Sroot lastname = "??"; 47946788Smckusick ITITERATE(i, dtwalk) { 48046788Smckusick if (strncmp(lastname, dtwalk->dd_name, 48146788Smckusick sizeof(dtwalk->dd_name)) == 0) 4821424Sroot continue; 48346788Smckusick date = (char *)ctime(&dtwalk->dd_ddate); 48446588Storek date[16] = '\0'; /* blast away seconds and year */ 48546788Smckusick lastname = dtwalk->dd_name; 48646788Smckusick dt = fstabsearch(dtwalk->dd_name); 48746588Storek dumpme = (dt != NULL && 48846588Storek dt->fs_freq != 0 && 48946816Sbostic dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY)); 49046588Storek if (arg != 'w' || dumpme) 49146588Storek (void) printf( 49246588Storek "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 49346588Storek dumpme && (arg != 'w') ? '>' : ' ', 49446788Smckusick dtwalk->dd_name, 49546588Storek dt ? dt->fs_file : "", 49646788Smckusick dtwalk->dd_level, 49746588Storek date); 4981424Sroot } 4991424Sroot } 5001424Sroot 50146788Smckusick int 50246788Smckusick datesort(a1, a2) 50346588Storek void *a1, *a2; 5041424Sroot { 50546788Smckusick struct dumpdates *d1 = *(struct dumpdates **)a1; 50646788Smckusick struct dumpdates *d2 = *(struct dumpdates **)a2; 50746588Storek int diff; 5081424Sroot 50946788Smckusick diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 5101424Sroot if (diff == 0) 51146788Smckusick return (d2->dd_ddate - d1->dd_ddate); 51246788Smckusick return (diff); 5131424Sroot } 5141424Sroot 515