xref: /csrg-svn/sbin/dump/optr.c (revision 50664)
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*50664Sbostic static char sccsid[] = "@(#)optr.c	5.12 (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>
3650652Storek #include <stdarg.h>
3750652Storek #else
3850652Storek #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 
311*50664Sbostic void
31250652Storek #if __STDC__
313*50664Sbostic msg(const char *fmt, ...)
31450652Storek #else
315*50664Sbostic msg(fmt, va_alist)
316*50664Sbostic 	char *fmt;
317*50664Sbostic 	va_dcl
31850652Storek #endif
3191424Sroot {
320*50664Sbostic 	va_list ap;
32146588Storek 
32246588Storek 	(void) fprintf(stderr,"  DUMP: ");
3231424Sroot #ifdef TDEBUG
32446588Storek 	(void) fprintf(stderr, "pid=%d ", getpid());
3251424Sroot #endif
326*50664Sbostic #if __STDC__
327*50664Sbostic 	va_start(ap, fmt);
328*50664Sbostic #else
329*50664Sbostic 	va_start(ap);
330*50664Sbostic #endif
33146588Storek 	(void) vfprintf(stderr, fmt, ap);
33246588Storek 	(void) fflush(stdout);
33346588Storek 	(void) fflush(stderr);
33446588Storek 	(void) vsprintf(lastmsg, fmt, ap);
33546588Storek 	va_end(ap);
3361424Sroot }
3371424Sroot 
338*50664Sbostic void
339*50664Sbostic #if __STDC__
340*50664Sbostic msgtail(const char *fmt, ...)
341*50664Sbostic #else
342*50664Sbostic msgtail(fmt, va_alist)
343*50664Sbostic 	char *fmt;
344*50664Sbostic 	va_dcl
345*50664Sbostic #endif
3461424Sroot {
347*50664Sbostic 	va_list ap;
348*50664Sbostic #if __STDC__
349*50664Sbostic 	va_start(ap, fmt);
350*50664Sbostic #else
351*50664Sbostic 	va_start(ap);
352*50664Sbostic #endif
35346588Storek 	(void) vfprintf(stderr, fmt, ap);
35446588Storek 	va_end(ap);
3551424Sroot }
35646588Storek 
357*50664Sbostic void
358*50664Sbostic #if __STDC__
359*50664Sbostic quit(const char *fmt, ...)
360*50664Sbostic #else
361*50664Sbostic quit(fmt, va_alist)
362*50664Sbostic 	char *fmt;
363*50664Sbostic 	va_dcl
364*50664Sbostic #endif
36546588Storek {
366*50664Sbostic 	va_list ap;
36746588Storek 
36846588Storek 	(void) fprintf(stderr,"  DUMP: ");
36946588Storek #ifdef TDEBUG
37046588Storek 	(void) fprintf(stderr, "pid=%d ", getpid());
37146588Storek #endif
372*50664Sbostic #if __STDC__
373*50664Sbostic 	va_start(ap, fmt);
374*50664Sbostic #else
375*50664Sbostic 	va_start(ap);
376*50664Sbostic #endif
37746588Storek 	vfprintf(stderr, fmt, ap);
37846588Storek 	va_end(ap);
37946588Storek 	(void) fflush(stdout);
38046588Storek 	(void) fflush(stderr);
38146588Storek 	dumpabort();
38246588Storek }
38346588Storek 
3841424Sroot /*
3851424Sroot  *	Tell the operator what has to be done;
3861424Sroot  *	we don't actually do it
3871424Sroot  */
3881424Sroot 
38913047Ssam struct fstab *
39013047Ssam allocfsent(fs)
39113047Ssam 	register struct fstab *fs;
39213047Ssam {
39313047Ssam 	register struct fstab *new;
39413047Ssam 
39513047Ssam 	new = (struct fstab *)malloc(sizeof (*fs));
39646588Storek 	if (new == NULL ||
39746588Storek 	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
39846588Storek 	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
39946588Storek 	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
40046588Storek 		quit("%s\n", strerror(errno));
40113047Ssam 	new->fs_passno = fs->fs_passno;
40213047Ssam 	new->fs_freq = fs->fs_freq;
40313047Ssam 	return (new);
40413047Ssam }
40513047Ssam 
40613047Ssam struct	pfstab {
40713047Ssam 	struct	pfstab *pf_next;
40813047Ssam 	struct	fstab *pf_fstab;
40913047Ssam };
41013047Ssam 
41146588Storek static	struct pfstab *table;
41213047Ssam 
41346588Storek void
4141424Sroot getfstab()
4151424Sroot {
41613047Ssam 	register struct fstab *fs;
41713047Ssam 	register struct pfstab *pf;
4181424Sroot 
4191428Sroot 	if (setfsent() == 0) {
42046588Storek 		msg("Can't open %s for dump table information: %s\n",
42146588Storek 		    _PATH_FSTAB, strerror(errno));
42213047Ssam 		return;
4231424Sroot 	}
42413047Ssam 	while (fs = getfsent()) {
42513047Ssam 		if (strcmp(fs->fs_type, FSTAB_RW) &&
42613047Ssam 		    strcmp(fs->fs_type, FSTAB_RO) &&
42713047Ssam 		    strcmp(fs->fs_type, FSTAB_RQ))
42813047Ssam 			continue;
42913047Ssam 		fs = allocfsent(fs);
43046588Storek 		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
43146588Storek 			quit("%s\n", strerror(errno));
43213047Ssam 		pf->pf_fstab = fs;
43313047Ssam 		pf->pf_next = table;
43413047Ssam 		table = pf;
43513047Ssam 	}
43613047Ssam 	endfsent();
4371424Sroot }
4381424Sroot 
4391424Sroot /*
44013047Ssam  * Search in the fstab for a file name.
44113047Ssam  * This file name can be either the special or the path file name.
4421424Sroot  *
44313047Ssam  * The entries in the fstab are the BLOCK special names, not the
44413047Ssam  * character special names.
44513047Ssam  * The caller of fstabsearch assures that the character device
44613047Ssam  * is dumped (that is much faster)
4471424Sroot  *
44813047Ssam  * The file name can omit the leading '/'.
4491424Sroot  */
45013047Ssam struct fstab *
45113047Ssam fstabsearch(key)
45213047Ssam 	char *key;
4531424Sroot {
45413047Ssam 	register struct pfstab *pf;
45513047Ssam 	register struct fstab *fs;
45613047Ssam 	char *rawname();
4571424Sroot 
45846588Storek 	for (pf = table; pf != NULL; pf = pf->pf_next) {
45913047Ssam 		fs = pf->pf_fstab;
46046588Storek 		if (strcmp(fs->fs_file, key) == 0 ||
46146588Storek 		    strcmp(fs->fs_spec, key) == 0 ||
46246588Storek 		    strcmp(rawname(fs->fs_spec), key) == 0)
46313047Ssam 			return (fs);
46446588Storek 		if (key[0] != '/') {
46513047Ssam 			if (*fs->fs_spec == '/' &&
46613197Sroot 			    strcmp(fs->fs_spec + 1, key) == 0)
46713047Ssam 				return (fs);
46813047Ssam 			if (*fs->fs_file == '/' &&
46913197Sroot 			    strcmp(fs->fs_file + 1, key) == 0)
47013047Ssam 				return (fs);
4711424Sroot 		}
4721424Sroot 	}
47346588Storek 	return (NULL);
4741424Sroot }
4751424Sroot 
4761424Sroot /*
4771424Sroot  *	Tell the operator what to do
4781424Sroot  */
47946588Storek void
4801463Sroot lastdump(arg)
48146588Storek 	char	arg;	/* w ==> just what to do; W ==> most recent dumps */
4821424Sroot {
48346788Smckusick 	register int i;
48446788Smckusick 	register struct fstab *dt;
48546788Smckusick 	register struct dumpdates *dtwalk;
48646788Smckusick 	char *lastname, *date;
48746788Smckusick 	int dumpme, datesort();
48846788Smckusick 	time_t tnow;
4891424Sroot 
4901424Sroot 	time(&tnow);
4911424Sroot 	getfstab();		/* /etc/fstab input */
49246788Smckusick 	initdumptimes();	/* /etc/dumpdates input */
49346788Smckusick 	qsort(ddatev, nddates, sizeof(struct dumpdates *), datesort);
4941424Sroot 
4951463Sroot 	if (arg == 'w')
49646588Storek 		(void) printf("Dump these file systems:\n");
4971463Sroot 	else
49846588Storek 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
4991424Sroot 	lastname = "??";
50046788Smckusick 	ITITERATE(i, dtwalk) {
50146788Smckusick 		if (strncmp(lastname, dtwalk->dd_name,
50246788Smckusick 		    sizeof(dtwalk->dd_name)) == 0)
5031424Sroot 			continue;
50446788Smckusick 		date = (char *)ctime(&dtwalk->dd_ddate);
50546588Storek 		date[16] = '\0';	/* blast away seconds and year */
50646788Smckusick 		lastname = dtwalk->dd_name;
50746788Smckusick 		dt = fstabsearch(dtwalk->dd_name);
50846588Storek 		dumpme = (dt != NULL &&
50946588Storek 		    dt->fs_freq != 0 &&
51046816Sbostic 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
51146588Storek 		if (arg != 'w' || dumpme)
51246588Storek 			(void) printf(
51346588Storek 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
51446588Storek 			    dumpme && (arg != 'w') ? '>' : ' ',
51546788Smckusick 			    dtwalk->dd_name,
51646588Storek 			    dt ? dt->fs_file : "",
51746788Smckusick 			    dtwalk->dd_level,
51846588Storek 			    date);
5191424Sroot 	}
5201424Sroot }
5211424Sroot 
52246788Smckusick int
52346788Smckusick datesort(a1, a2)
52446588Storek 	void *a1, *a2;
5251424Sroot {
52646788Smckusick 	struct dumpdates *d1 = *(struct dumpdates **)a1;
52746788Smckusick 	struct dumpdates *d2 = *(struct dumpdates **)a2;
52846588Storek 	int diff;
5291424Sroot 
53046788Smckusick 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
5311424Sroot 	if (diff == 0)
53246788Smckusick 		return (d2->dd_ddate - d1->dd_ddate);
53346788Smckusick 	return (diff);
5341424Sroot }
5351424Sroot 
53646588Storek int max(a, b)
5375319Smckusic 	int a, b;
5381424Sroot {
53946588Storek 	return (a > b ? a : b);
5401424Sroot }
54146588Storek int min(a, b)
5425319Smckusic 	int a, b;
5431424Sroot {
54446588Storek 	return (a < b ? a : b);
5451424Sroot }
546