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*55288Sbostic static char sccsid[] = "@(#)optr.c 5.14 (Berkeley) 07/16/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 */ 9454043Smckusick (void) alarm(0); 95*55288Sbostic if (signal(SIGALRM, sig) == SIG_IGN) 961424Sroot signal(SIGALRM, SIG_IGN); 9754043Smckusick (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); 12654043Smckusick (void) alarm(120); 1271424Sroot timeout = 1; 1281424Sroot } 12946588Storek 1301424Sroot /* 1311424Sroot * Here if an inquisitive operator interrupts the dump program 1321424Sroot */ 13346588Storek void 134*55288Sbostic interrupt(signo) 135*55288Sbostic int signo; 1361424Sroot { 13721124Smckusick msg("Interrupt received.\n"); 13821124Smckusick if (query("Do you want to abort dump?")) 139*55288Sbostic dumpabort(0); 1401424Sroot } 1411424Sroot 1421424Sroot /* 1431424Sroot * The following variables and routines manage alerting 1441424Sroot * operators to the status of dump. 1451424Sroot * This works much like wall(1) does. 1461424Sroot */ 14746588Storek struct group *gp; 1481424Sroot 1491424Sroot /* 1501424Sroot * Get the names from the group entry "operator" to notify. 1511424Sroot */ 15246588Storek void 1531424Sroot set_operators() 1541424Sroot { 1551424Sroot if (!notify) /*not going to notify*/ 1561424Sroot return; 1571424Sroot gp = getgrnam(OPGRENT); 15854043Smckusick (void) endgrent(); 15946588Storek if (gp == NULL) { 16046588Storek msg("No group entry for %s.\n", OPGRENT); 1611424Sroot notify = 0; 1621424Sroot return; 1631424Sroot } 1641424Sroot } 1651424Sroot 1661424Sroot struct tm *localtime(); 1671424Sroot struct tm *localclock; 1681424Sroot 1691424Sroot /* 1701424Sroot * We fork a child to do the actual broadcasting, so 1711424Sroot * that the process control groups are not messed up 1721424Sroot */ 17346588Storek void 1741424Sroot broadcast(message) 1751424Sroot char *message; 1761424Sroot { 1771424Sroot time_t clock; 1781424Sroot FILE *f_utmp; 1791424Sroot struct utmp utmp; 1801424Sroot char **np; 1811424Sroot int pid, s; 1821424Sroot 18346588Storek if (!notify || gp == NULL) 18446588Storek return; 18546588Storek 1861424Sroot switch (pid = fork()) { 1871424Sroot case -1: 1881424Sroot return; 1891424Sroot case 0: 1901424Sroot break; 1911424Sroot default: 1921424Sroot while (wait(&s) != pid) 1931424Sroot continue; 1941424Sroot return; 1951424Sroot } 1961424Sroot 19754043Smckusick clock = time((time_t *)0); 1981424Sroot localclock = localtime(&clock); 1991424Sroot 20046588Storek if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) { 20146588Storek msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno)); 2021424Sroot return; 2031424Sroot } 2041424Sroot 20546588Storek while (!feof(f_utmp)) { 20654043Smckusick if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1) 2071424Sroot break; 2081424Sroot if (utmp.ut_name[0] == 0) 2091424Sroot continue; 21046588Storek for (np = gp->gr_mem; *np; np++) { 2111424Sroot if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0) 2121424Sroot continue; 2131424Sroot /* 2141424Sroot * Do not send messages to operators on dialups 2151424Sroot */ 2161424Sroot if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0) 2171424Sroot continue; 2181424Sroot #ifdef DEBUG 21946588Storek msg("Message to %s at %s\n", *np, utmp.ut_line); 22046588Storek #endif 2211424Sroot sendmes(utmp.ut_line, message); 2221424Sroot } 2231424Sroot } 22446588Storek (void) fclose(f_utmp); 2251424Sroot Exit(0); /* the wait in this same routine will catch this */ 2261424Sroot /* NOTREACHED */ 2271424Sroot } 2281424Sroot 22946588Storek static void 2301424Sroot sendmes(tty, message) 2315319Smckusic char *tty, *message; 2321424Sroot { 2331424Sroot char t[50], buf[BUFSIZ]; 2341424Sroot register char *cp; 23539130Smckusick int lmsg = 1; 2361424Sroot FILE *f_tty; 2371424Sroot 23846588Storek (void) strcpy(t, _PATH_DEV); 23946588Storek (void) strcat(t, tty); 2401424Sroot 24146588Storek if ((f_tty = fopen(t, "w")) != NULL) { 2421424Sroot setbuf(f_tty, buf); 24346588Storek (void) fprintf(f_tty, 24446588Storek "\n\ 24546588Storek \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\ 24646588Storek DUMP: NEEDS ATTENTION: ", 24746588Storek localclock->tm_hour, localclock->tm_min); 24839130Smckusick for (cp = lastmsg; ; cp++) { 24939130Smckusick if (*cp == '\0') { 25039130Smckusick if (lmsg) { 25139130Smckusick cp = message; 25239130Smckusick if (*cp == '\0') 25339130Smckusick break; 25439130Smckusick lmsg = 0; 25539130Smckusick } else 25639130Smckusick break; 25739130Smckusick } 25839130Smckusick if (*cp == '\n') 25946588Storek (void) putc('\r', f_tty); 26046588Storek (void) putc(*cp, f_tty); 2611424Sroot } 26246588Storek (void) fclose(f_tty); 2631424Sroot } 2641424Sroot } 2651424Sroot 2661424Sroot /* 2671424Sroot * print out an estimate of the amount of time left to do the dump 2681424Sroot */ 2691424Sroot 2701424Sroot time_t tschedule = 0; 2711424Sroot 27246588Storek void 2731424Sroot timeest() 2741424Sroot { 2751424Sroot time_t tnow, deltat; 2761424Sroot 27754043Smckusick (void) time((time_t *) &tnow); 27846588Storek if (tnow >= tschedule) { 2791424Sroot tschedule = tnow + 300; 2801424Sroot if (blockswritten < 500) 2811424Sroot return; 2821424Sroot deltat = tstart_writing - tnow + 28346788Smckusick (1.0 * (tnow - tstart_writing)) 28446788Smckusick / blockswritten * tapesize; 2851424Sroot msg("%3.2f%% done, finished in %d:%02d\n", 28646788Smckusick (blockswritten * 100.0) / tapesize, 28746788Smckusick deltat / 3600, (deltat % 3600) / 60); 2881424Sroot } 2891424Sroot } 2901424Sroot 29150664Sbostic void 29250652Storek #if __STDC__ 29350664Sbostic msg(const char *fmt, ...) 29450652Storek #else 29550664Sbostic msg(fmt, va_alist) 29650664Sbostic char *fmt; 29750664Sbostic va_dcl 29850652Storek #endif 2991424Sroot { 30050664Sbostic va_list ap; 30146588Storek 30246588Storek (void) fprintf(stderr," DUMP: "); 3031424Sroot #ifdef TDEBUG 30446588Storek (void) fprintf(stderr, "pid=%d ", getpid()); 3051424Sroot #endif 30650664Sbostic #if __STDC__ 30750664Sbostic va_start(ap, fmt); 30850664Sbostic #else 30950664Sbostic va_start(ap); 31050664Sbostic #endif 31146588Storek (void) vfprintf(stderr, fmt, ap); 31246588Storek (void) fflush(stdout); 31346588Storek (void) fflush(stderr); 31446588Storek (void) vsprintf(lastmsg, fmt, ap); 31546588Storek va_end(ap); 3161424Sroot } 3171424Sroot 31850664Sbostic void 31950664Sbostic #if __STDC__ 32050664Sbostic msgtail(const char *fmt, ...) 32150664Sbostic #else 32250664Sbostic msgtail(fmt, va_alist) 32350664Sbostic char *fmt; 32450664Sbostic va_dcl 32550664Sbostic #endif 3261424Sroot { 32750664Sbostic va_list ap; 32850664Sbostic #if __STDC__ 32950664Sbostic va_start(ap, fmt); 33050664Sbostic #else 33150664Sbostic va_start(ap); 33250664Sbostic #endif 33346588Storek (void) vfprintf(stderr, fmt, ap); 33446588Storek va_end(ap); 3351424Sroot } 33646588Storek 33750664Sbostic void 33850664Sbostic #if __STDC__ 33950664Sbostic quit(const char *fmt, ...) 34050664Sbostic #else 34150664Sbostic quit(fmt, va_alist) 34250664Sbostic char *fmt; 34350664Sbostic va_dcl 34450664Sbostic #endif 34546588Storek { 34650664Sbostic va_list ap; 34746588Storek 34846588Storek (void) fprintf(stderr," DUMP: "); 34946588Storek #ifdef TDEBUG 35046588Storek (void) fprintf(stderr, "pid=%d ", getpid()); 35146588Storek #endif 35250664Sbostic #if __STDC__ 35350664Sbostic va_start(ap, fmt); 35450664Sbostic #else 35550664Sbostic va_start(ap); 35650664Sbostic #endif 35754043Smckusick (void) vfprintf(stderr, fmt, ap); 35846588Storek va_end(ap); 35946588Storek (void) fflush(stdout); 36046588Storek (void) fflush(stderr); 361*55288Sbostic dumpabort(0); 36246588Storek } 36346588Storek 3641424Sroot /* 3651424Sroot * Tell the operator what has to be done; 3661424Sroot * we don't actually do it 3671424Sroot */ 3681424Sroot 36913047Ssam struct fstab * 37013047Ssam allocfsent(fs) 37113047Ssam register struct fstab *fs; 37213047Ssam { 37313047Ssam register struct fstab *new; 37413047Ssam 37513047Ssam new = (struct fstab *)malloc(sizeof (*fs)); 37646588Storek if (new == NULL || 37746588Storek (new->fs_file = strdup(fs->fs_file)) == NULL || 37846588Storek (new->fs_type = strdup(fs->fs_type)) == NULL || 37946588Storek (new->fs_spec = strdup(fs->fs_spec)) == NULL) 38046588Storek quit("%s\n", strerror(errno)); 38113047Ssam new->fs_passno = fs->fs_passno; 38213047Ssam new->fs_freq = fs->fs_freq; 38313047Ssam return (new); 38413047Ssam } 38513047Ssam 38613047Ssam struct pfstab { 38713047Ssam struct pfstab *pf_next; 38813047Ssam struct fstab *pf_fstab; 38913047Ssam }; 39013047Ssam 39146588Storek static struct pfstab *table; 39213047Ssam 39346588Storek void 3941424Sroot getfstab() 3951424Sroot { 39613047Ssam register struct fstab *fs; 39713047Ssam register struct pfstab *pf; 3981424Sroot 3991428Sroot if (setfsent() == 0) { 40046588Storek msg("Can't open %s for dump table information: %s\n", 40146588Storek _PATH_FSTAB, strerror(errno)); 40213047Ssam return; 4031424Sroot } 40413047Ssam while (fs = getfsent()) { 40513047Ssam if (strcmp(fs->fs_type, FSTAB_RW) && 40613047Ssam strcmp(fs->fs_type, FSTAB_RO) && 40713047Ssam strcmp(fs->fs_type, FSTAB_RQ)) 40813047Ssam continue; 40913047Ssam fs = allocfsent(fs); 41046588Storek if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 41146588Storek quit("%s\n", strerror(errno)); 41213047Ssam pf->pf_fstab = fs; 41313047Ssam pf->pf_next = table; 41413047Ssam table = pf; 41513047Ssam } 41654043Smckusick (void) endfsent(); 4171424Sroot } 4181424Sroot 4191424Sroot /* 42013047Ssam * Search in the fstab for a file name. 42113047Ssam * This file name can be either the special or the path file name. 4221424Sroot * 42313047Ssam * The entries in the fstab are the BLOCK special names, not the 42413047Ssam * character special names. 42513047Ssam * The caller of fstabsearch assures that the character device 42613047Ssam * is dumped (that is much faster) 4271424Sroot * 42813047Ssam * The file name can omit the leading '/'. 4291424Sroot */ 43013047Ssam struct fstab * 43113047Ssam fstabsearch(key) 43213047Ssam char *key; 4331424Sroot { 43413047Ssam register struct pfstab *pf; 43513047Ssam register struct fstab *fs; 43613047Ssam char *rawname(); 4371424Sroot 43846588Storek for (pf = table; pf != NULL; pf = pf->pf_next) { 43913047Ssam fs = pf->pf_fstab; 44046588Storek if (strcmp(fs->fs_file, key) == 0 || 44146588Storek strcmp(fs->fs_spec, key) == 0 || 44246588Storek strcmp(rawname(fs->fs_spec), key) == 0) 44313047Ssam return (fs); 44446588Storek if (key[0] != '/') { 44513047Ssam if (*fs->fs_spec == '/' && 44613197Sroot strcmp(fs->fs_spec + 1, key) == 0) 44713047Ssam return (fs); 44813047Ssam if (*fs->fs_file == '/' && 44913197Sroot strcmp(fs->fs_file + 1, key) == 0) 45013047Ssam return (fs); 4511424Sroot } 4521424Sroot } 45346588Storek return (NULL); 4541424Sroot } 4551424Sroot 4561424Sroot /* 4571424Sroot * Tell the operator what to do 4581424Sroot */ 45946588Storek void 4601463Sroot lastdump(arg) 46146588Storek char arg; /* w ==> just what to do; W ==> most recent dumps */ 4621424Sroot { 46346788Smckusick register int i; 46446788Smckusick register struct fstab *dt; 46546788Smckusick register struct dumpdates *dtwalk; 46646788Smckusick char *lastname, *date; 46746788Smckusick int dumpme, datesort(); 46846788Smckusick time_t tnow; 4691424Sroot 47054043Smckusick (void) time(&tnow); 4711424Sroot getfstab(); /* /etc/fstab input */ 47246788Smckusick initdumptimes(); /* /etc/dumpdates input */ 47354043Smckusick qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 4741424Sroot 4751463Sroot if (arg == 'w') 47646588Storek (void) printf("Dump these file systems:\n"); 4771463Sroot else 47846588Storek (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 4791424Sroot lastname = "??"; 48046788Smckusick ITITERATE(i, dtwalk) { 48146788Smckusick if (strncmp(lastname, dtwalk->dd_name, 48246788Smckusick sizeof(dtwalk->dd_name)) == 0) 4831424Sroot continue; 48446788Smckusick date = (char *)ctime(&dtwalk->dd_ddate); 48546588Storek date[16] = '\0'; /* blast away seconds and year */ 48646788Smckusick lastname = dtwalk->dd_name; 48746788Smckusick dt = fstabsearch(dtwalk->dd_name); 48846588Storek dumpme = (dt != NULL && 48946588Storek dt->fs_freq != 0 && 49046816Sbostic dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY)); 49146588Storek if (arg != 'w' || dumpme) 49246588Storek (void) printf( 49346588Storek "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 49446588Storek dumpme && (arg != 'w') ? '>' : ' ', 49546788Smckusick dtwalk->dd_name, 49646588Storek dt ? dt->fs_file : "", 49746788Smckusick dtwalk->dd_level, 49846588Storek date); 4991424Sroot } 5001424Sroot } 5011424Sroot 50246788Smckusick int 50346788Smckusick datesort(a1, a2) 50446588Storek void *a1, *a2; 5051424Sroot { 50646788Smckusick struct dumpdates *d1 = *(struct dumpdates **)a1; 50746788Smckusick struct dumpdates *d2 = *(struct dumpdates **)a2; 50846588Storek int diff; 5091424Sroot 51046788Smckusick diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 5111424Sroot if (diff == 0) 51246788Smckusick return (d2->dd_ddate - d1->dd_ddate); 51346788Smckusick return (diff); 5141424Sroot } 5151424Sroot 516