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*50652Storek static char sccsid[] = "@(#)optr.c 5.11 (Berkeley) 07/29/91"; 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 #include <sys/dir.h> 2050497Smckusick #else 2150497Smckusick #include <sys/param.h> 2250497Smckusick #include <sys/wait.h> 2350497Smckusick #include <stdio.h> 2450497Smckusick #endif 2546795Sbostic #include <signal.h> 2646795Sbostic #include <time.h> 2746795Sbostic #include <fstab.h> 2846588Storek #include <grp.h> 2946795Sbostic #include <utmp.h> 3046816Sbostic #include <tzfile.h> 3146795Sbostic #include <errno.h> 3246795Sbostic #ifdef __STDC__ 3346795Sbostic #include <unistd.h> 3446795Sbostic #include <stdlib.h> 3546795Sbostic #include <string.h> 36*50652Storek #include <stdarg.h> 37*50652Storek #else 38*50652Storek #include <varargs.h> 3946795Sbostic #endif 4046795Sbostic #include "dump.h" 4139130Smckusick #include "pathnames.h" 421424Sroot 4346588Storek static void alarmcatch(); 4446588Storek static void sendmes(); 4546588Storek 461424Sroot /* 4739130Smckusick * Query the operator; This previously-fascist piece of code 4839130Smckusick * no longer requires an exact response. 491424Sroot * It is intended to protect dump aborting by inquisitive 501424Sroot * people banging on the console terminal to see what is 511424Sroot * happening which might cause dump to croak, destroying 521424Sroot * a large number of hours of work. 531424Sroot * 541424Sroot * Every 2 minutes we reprint the message, alerting others 551424Sroot * that dump needs attention. 561424Sroot */ 571424Sroot int timeout; 5821124Smckusick char *attnmessage; /* attention message */ 5946588Storek 6046588Storek int 611424Sroot query(question) 621424Sroot char *question; 631424Sroot { 641424Sroot char replybuffer[64]; 6546588Storek int back, errcount; 661424Sroot FILE *mytty; 671424Sroot 6846588Storek if ((mytty = fopen(_PATH_TTY, "r")) == NULL) 6946588Storek quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); 701424Sroot attnmessage = question; 711424Sroot timeout = 0; 721424Sroot alarmcatch(); 7346588Storek back = -1; 7446588Storek errcount = 0; 7546588Storek do { 7646588Storek if (fgets(replybuffer, 63, mytty) == NULL) { 7746588Storek clearerr(mytty); 7846588Storek if (++errcount > 30) /* XXX ugly */ 7946588Storek quit("excessive operator query failures\n"); 8039130Smckusick } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { 8146588Storek back = 1; 8239130Smckusick } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { 8346588Storek back = 0; 841424Sroot } else { 8546588Storek (void) fprintf(stderr, 8646588Storek " DUMP: \"Yes\" or \"No\"?\n"); 8746588Storek (void) fprintf(stderr, 8846588Storek " DUMP: %s: (\"yes\" or \"no\") ", question); 891424Sroot } 9046588Storek } while (back < 0); 9146588Storek 921424Sroot /* 931424Sroot * Turn off the alarm, and reset the signal to trap out.. 941424Sroot */ 951424Sroot alarm(0); 961424Sroot if (signal(SIGALRM, sigalrm) == SIG_IGN) 971424Sroot signal(SIGALRM, SIG_IGN); 981424Sroot fclose(mytty); 991424Sroot return(back); 1001424Sroot } 10139130Smckusick 10239130Smckusick char lastmsg[100]; 10339130Smckusick 1041424Sroot /* 1051424Sroot * Alert the console operator, and enable the alarm clock to 1061424Sroot * sleep for 2 minutes in case nobody comes to satisfy dump 1071424Sroot */ 10846588Storek static void 1091424Sroot alarmcatch() 1101424Sroot { 11139130Smckusick if (notify == 0) { 11239130Smckusick if (timeout == 0) 11346588Storek (void) fprintf(stderr, 11446588Storek " DUMP: %s: (\"yes\" or \"no\") ", 11539130Smckusick attnmessage); 11639130Smckusick else 11739130Smckusick msgtail("\7\7"); 11839130Smckusick } else { 11939130Smckusick if (timeout) { 12039130Smckusick msgtail("\n"); 12139130Smckusick broadcast(""); /* just print last msg */ 12239130Smckusick } 12346588Storek (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 12439130Smckusick attnmessage); 12539130Smckusick } 1261424Sroot signal(SIGALRM, alarmcatch); 1271424Sroot alarm(120); 1281424Sroot timeout = 1; 1291424Sroot } 13046588Storek 1311424Sroot /* 1321424Sroot * Here if an inquisitive operator interrupts the dump program 1331424Sroot */ 13446588Storek void 1351424Sroot interrupt() 1361424Sroot { 13721124Smckusick msg("Interrupt received.\n"); 13821124Smckusick if (query("Do you want to abort dump?")) 1391424Sroot dumpabort(); 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); 1581424Sroot 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 int nusers; 1811424Sroot char **np; 1821424Sroot int pid, s; 1831424Sroot 18446588Storek if (!notify || gp == NULL) 18546588Storek return; 18646588Storek 1871424Sroot switch (pid = fork()) { 1881424Sroot case -1: 1891424Sroot return; 1901424Sroot case 0: 1911424Sroot break; 1921424Sroot default: 1931424Sroot while (wait(&s) != pid) 1941424Sroot continue; 1951424Sroot return; 1961424Sroot } 1971424Sroot 1981424Sroot clock = time(0); 1991424Sroot localclock = localtime(&clock); 2001424Sroot 20146588Storek if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) { 20246588Storek msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno)); 2031424Sroot return; 2041424Sroot } 2051424Sroot 2061424Sroot nusers = 0; 20746588Storek while (!feof(f_utmp)) { 2081424Sroot if (fread(&utmp, sizeof (struct utmp), 1, f_utmp) != 1) 2091424Sroot break; 2101424Sroot if (utmp.ut_name[0] == 0) 2111424Sroot continue; 2121424Sroot nusers++; 21346588Storek for (np = gp->gr_mem; *np; np++) { 2141424Sroot if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0) 2151424Sroot continue; 2161424Sroot /* 2171424Sroot * Do not send messages to operators on dialups 2181424Sroot */ 2191424Sroot if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0) 2201424Sroot continue; 2211424Sroot #ifdef DEBUG 22246588Storek msg("Message to %s at %s\n", *np, utmp.ut_line); 22346588Storek #endif 2241424Sroot sendmes(utmp.ut_line, message); 2251424Sroot } 2261424Sroot } 22746588Storek (void) fclose(f_utmp); 2281424Sroot Exit(0); /* the wait in this same routine will catch this */ 2291424Sroot /* NOTREACHED */ 2301424Sroot } 2311424Sroot 23246588Storek static void 2331424Sroot sendmes(tty, message) 2345319Smckusic char *tty, *message; 2351424Sroot { 2361424Sroot char t[50], buf[BUFSIZ]; 2371424Sroot register char *cp; 23839130Smckusick int lmsg = 1; 2391424Sroot FILE *f_tty; 2401424Sroot 24146588Storek (void) strcpy(t, _PATH_DEV); 24246588Storek (void) strcat(t, tty); 2431424Sroot 24446588Storek if ((f_tty = fopen(t, "w")) != NULL) { 2451424Sroot setbuf(f_tty, buf); 24646588Storek (void) fprintf(f_tty, 24746588Storek "\n\ 24846588Storek \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\ 24946588Storek DUMP: NEEDS ATTENTION: ", 25046588Storek localclock->tm_hour, localclock->tm_min); 25139130Smckusick for (cp = lastmsg; ; cp++) { 25239130Smckusick if (*cp == '\0') { 25339130Smckusick if (lmsg) { 25439130Smckusick cp = message; 25539130Smckusick if (*cp == '\0') 25639130Smckusick break; 25739130Smckusick lmsg = 0; 25839130Smckusick } else 25939130Smckusick break; 26039130Smckusick } 26139130Smckusick if (*cp == '\n') 26246588Storek (void) putc('\r', f_tty); 26346588Storek (void) putc(*cp, f_tty); 2641424Sroot } 26546588Storek (void) fclose(f_tty); 2661424Sroot } 2671424Sroot } 2681424Sroot 2691424Sroot /* 2701424Sroot * print out an estimate of the amount of time left to do the dump 2711424Sroot */ 2721424Sroot 2731424Sroot time_t tschedule = 0; 2741424Sroot 27546588Storek void 2761424Sroot timeest() 2771424Sroot { 2781424Sroot time_t tnow, deltat; 2791424Sroot 2801424Sroot time (&tnow); 28146588Storek if (tnow >= tschedule) { 2821424Sroot tschedule = tnow + 300; 2831424Sroot if (blockswritten < 500) 2841424Sroot return; 2851424Sroot deltat = tstart_writing - tnow + 28646788Smckusick (1.0 * (tnow - tstart_writing)) 28746788Smckusick / blockswritten * tapesize; 2881424Sroot msg("%3.2f%% done, finished in %d:%02d\n", 28946788Smckusick (blockswritten * 100.0) / tapesize, 29046788Smckusick deltat / 3600, (deltat % 3600) / 60); 2911424Sroot } 2921424Sroot } 2931424Sroot 29446788Smckusick /* 29546788Smckusick * tapesize: total number of blocks estimated over all reels 29646788Smckusick * blockswritten: blocks actually written, over all reels 29746788Smckusick * etapes: estimated number of tapes to write 29846788Smckusick * 29946788Smckusick * tsize: blocks can write on this reel 30046788Smckusick * asize: blocks written on this reel 30146788Smckusick * tapeno: number of tapes written so far 30246788Smckusick */ 30346588Storek int 30446588Storek blocksontape() 3051424Sroot { 3061424Sroot if (tapeno == etapes) 30746788Smckusick return (tapesize - (etapes - 1) * tsize); 30846588Storek return (tsize); 3091424Sroot } 3101424Sroot 31146588Storek #ifdef lint 31246588Storek 31346588Storek /* VARARGS1 */ 31446588Storek void msg(fmt) char *fmt; { strcpy(lastmsg, fmt); } 31546588Storek 31646588Storek /* VARARGS1 */ 31746588Storek void msgtail(fmt) char *fmt; { fmt = fmt; } 31846588Storek 31946588Storek void quit(fmt) char *fmt; { msg(fmt); dumpabort(); } 32046588Storek 32146588Storek #else /* lint */ 32246588Storek 323*50652Storek /* 324*50652Storek * We have three of these. Yes, this is horribly ugly, but at least 325*50652Storek * we only have to go through it once. 326*50652Storek */ 327*50652Storek #if __STDC__ 328*50652Storek #define MSG_FDECL(func) void func(const char *fmt, ...) 329*50652Storek #define MSG_VDECL va_list ap 330*50652Storek #define MSG_START va_start(ap, fmt) 331*50652Storek #else 332*50652Storek #define MSG_FDECL(func) void func(va_alist) va_dcl 333*50652Storek #define MSG_VDECL char *fmt; va_list ap 334*50652Storek #define MSG_START va_start(ap); fmt = va_arg(ap, char *) 335*50652Storek #endif 336*50652Storek 337*50652Storek MSG_FDECL(msg) 3381424Sroot { 339*50652Storek MSG_VDECL; 34046588Storek 34146588Storek (void) fprintf(stderr," DUMP: "); 3421424Sroot #ifdef TDEBUG 34346588Storek (void) fprintf(stderr, "pid=%d ", getpid()); 3441424Sroot #endif 345*50652Storek MSG_START; 34646588Storek (void) vfprintf(stderr, fmt, ap); 34746588Storek va_end(ap); 34846588Storek (void) fflush(stdout); 34946588Storek (void) fflush(stderr); 350*50652Storek MSG_START; 35146588Storek (void) vsprintf(lastmsg, fmt, ap); 35246588Storek va_end(ap); 3531424Sroot } 3541424Sroot 355*50652Storek MSG_FDECL(msgtail) 3561424Sroot { 357*50652Storek MSG_VDECL; 35846588Storek 359*50652Storek MSG_START; 36046588Storek (void) vfprintf(stderr, fmt, ap); 36146588Storek va_end(ap); 3621424Sroot } 36346588Storek 364*50652Storek MSG_FDECL(quit) 36546588Storek { 366*50652Storek MSG_VDECL; 36746588Storek 36846588Storek (void) fprintf(stderr," DUMP: "); 36946588Storek #ifdef TDEBUG 37046588Storek (void) fprintf(stderr, "pid=%d ", getpid()); 37146588Storek #endif 372*50652Storek MSG_START; 37346588Storek vfprintf(stderr, fmt, ap); 37446588Storek va_end(ap); 37546588Storek (void) fflush(stdout); 37646588Storek (void) fflush(stderr); 37746588Storek dumpabort(); 37846588Storek } 37946588Storek 38046588Storek #endif /* lint */ 38146588Storek 3821424Sroot /* 3831424Sroot * Tell the operator what has to be done; 3841424Sroot * we don't actually do it 3851424Sroot */ 3861424Sroot 38713047Ssam struct fstab * 38813047Ssam allocfsent(fs) 38913047Ssam register struct fstab *fs; 39013047Ssam { 39113047Ssam register struct fstab *new; 39213047Ssam 39313047Ssam new = (struct fstab *)malloc(sizeof (*fs)); 39446588Storek if (new == NULL || 39546588Storek (new->fs_file = strdup(fs->fs_file)) == NULL || 39646588Storek (new->fs_type = strdup(fs->fs_type)) == NULL || 39746588Storek (new->fs_spec = strdup(fs->fs_spec)) == NULL) 39846588Storek quit("%s\n", strerror(errno)); 39913047Ssam new->fs_passno = fs->fs_passno; 40013047Ssam new->fs_freq = fs->fs_freq; 40113047Ssam return (new); 40213047Ssam } 40313047Ssam 40413047Ssam struct pfstab { 40513047Ssam struct pfstab *pf_next; 40613047Ssam struct fstab *pf_fstab; 40713047Ssam }; 40813047Ssam 40946588Storek static struct pfstab *table; 41013047Ssam 41146588Storek void 4121424Sroot getfstab() 4131424Sroot { 41413047Ssam register struct fstab *fs; 41513047Ssam register struct pfstab *pf; 4161424Sroot 4171428Sroot if (setfsent() == 0) { 41846588Storek msg("Can't open %s for dump table information: %s\n", 41946588Storek _PATH_FSTAB, strerror(errno)); 42013047Ssam return; 4211424Sroot } 42213047Ssam while (fs = getfsent()) { 42313047Ssam if (strcmp(fs->fs_type, FSTAB_RW) && 42413047Ssam strcmp(fs->fs_type, FSTAB_RO) && 42513047Ssam strcmp(fs->fs_type, FSTAB_RQ)) 42613047Ssam continue; 42713047Ssam fs = allocfsent(fs); 42846588Storek if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 42946588Storek quit("%s\n", strerror(errno)); 43013047Ssam pf->pf_fstab = fs; 43113047Ssam pf->pf_next = table; 43213047Ssam table = pf; 43313047Ssam } 43413047Ssam endfsent(); 4351424Sroot } 4361424Sroot 4371424Sroot /* 43813047Ssam * Search in the fstab for a file name. 43913047Ssam * This file name can be either the special or the path file name. 4401424Sroot * 44113047Ssam * The entries in the fstab are the BLOCK special names, not the 44213047Ssam * character special names. 44313047Ssam * The caller of fstabsearch assures that the character device 44413047Ssam * is dumped (that is much faster) 4451424Sroot * 44613047Ssam * The file name can omit the leading '/'. 4471424Sroot */ 44813047Ssam struct fstab * 44913047Ssam fstabsearch(key) 45013047Ssam char *key; 4511424Sroot { 45213047Ssam register struct pfstab *pf; 45313047Ssam register struct fstab *fs; 45413047Ssam char *rawname(); 4551424Sroot 45646588Storek for (pf = table; pf != NULL; pf = pf->pf_next) { 45713047Ssam fs = pf->pf_fstab; 45846588Storek if (strcmp(fs->fs_file, key) == 0 || 45946588Storek strcmp(fs->fs_spec, key) == 0 || 46046588Storek strcmp(rawname(fs->fs_spec), key) == 0) 46113047Ssam return (fs); 46246588Storek if (key[0] != '/') { 46313047Ssam if (*fs->fs_spec == '/' && 46413197Sroot strcmp(fs->fs_spec + 1, key) == 0) 46513047Ssam return (fs); 46613047Ssam if (*fs->fs_file == '/' && 46713197Sroot strcmp(fs->fs_file + 1, key) == 0) 46813047Ssam return (fs); 4691424Sroot } 4701424Sroot } 47146588Storek return (NULL); 4721424Sroot } 4731424Sroot 4741424Sroot /* 4751424Sroot * Tell the operator what to do 4761424Sroot */ 47746588Storek void 4781463Sroot lastdump(arg) 47946588Storek char arg; /* w ==> just what to do; W ==> most recent dumps */ 4801424Sroot { 48146788Smckusick register int i; 48246788Smckusick register struct fstab *dt; 48346788Smckusick register struct dumpdates *dtwalk; 48446788Smckusick char *lastname, *date; 48546788Smckusick int dumpme, datesort(); 48646788Smckusick time_t tnow; 4871424Sroot 4881424Sroot time(&tnow); 4891424Sroot getfstab(); /* /etc/fstab input */ 49046788Smckusick initdumptimes(); /* /etc/dumpdates input */ 49146788Smckusick qsort(ddatev, nddates, sizeof(struct dumpdates *), datesort); 4921424Sroot 4931463Sroot if (arg == 'w') 49446588Storek (void) printf("Dump these file systems:\n"); 4951463Sroot else 49646588Storek (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 4971424Sroot lastname = "??"; 49846788Smckusick ITITERATE(i, dtwalk) { 49946788Smckusick if (strncmp(lastname, dtwalk->dd_name, 50046788Smckusick sizeof(dtwalk->dd_name)) == 0) 5011424Sroot continue; 50246788Smckusick date = (char *)ctime(&dtwalk->dd_ddate); 50346588Storek date[16] = '\0'; /* blast away seconds and year */ 50446788Smckusick lastname = dtwalk->dd_name; 50546788Smckusick dt = fstabsearch(dtwalk->dd_name); 50646588Storek dumpme = (dt != NULL && 50746588Storek dt->fs_freq != 0 && 50846816Sbostic dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY)); 50946588Storek if (arg != 'w' || dumpme) 51046588Storek (void) printf( 51146588Storek "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 51246588Storek dumpme && (arg != 'w') ? '>' : ' ', 51346788Smckusick dtwalk->dd_name, 51446588Storek dt ? dt->fs_file : "", 51546788Smckusick dtwalk->dd_level, 51646588Storek date); 5171424Sroot } 5181424Sroot } 5191424Sroot 52046788Smckusick int 52146788Smckusick datesort(a1, a2) 52246588Storek void *a1, *a2; 5231424Sroot { 52446788Smckusick struct dumpdates *d1 = *(struct dumpdates **)a1; 52546788Smckusick struct dumpdates *d2 = *(struct dumpdates **)a2; 52646588Storek int diff; 5271424Sroot 52846788Smckusick diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 5291424Sroot if (diff == 0) 53046788Smckusick return (d2->dd_ddate - d1->dd_ddate); 53146788Smckusick return (diff); 5321424Sroot } 5331424Sroot 53446588Storek int max(a, b) 5355319Smckusic int a, b; 5361424Sroot { 53746588Storek return (a > b ? a : b); 5381424Sroot } 53946588Storek int min(a, b) 5405319Smckusic int a, b; 5411424Sroot { 54246588Storek return (a < b ? a : b); 5431424Sroot } 544