xref: /csrg-svn/sbin/dump/optr.c (revision 50497)
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*50497Smckusick static char sccsid[] = "@(#)optr.c	5.9 (Berkeley) 07/23/91";
1046588Storek #endif /* not lint */
1122038Sdist 
12*50497Smckusick #ifdef sunos
13*50497Smckusick #include <stdio.h>
14*50497Smckusick #include <ctype.h>
1546795Sbostic #include <sys/param.h>
1646588Storek #include <sys/wait.h>
17*50497Smckusick #include <sys/stat.h>
18*50497Smckusick #include <sys/time.h>
19*50497Smckusick #include <sys/dir.h>
20*50497Smckusick #else
21*50497Smckusick #include <sys/param.h>
22*50497Smckusick #include <sys/wait.h>
2346795Sbostic #include <ufs/dir.h>
24*50497Smckusick #include <stdio.h>
25*50497Smckusick #endif
2646795Sbostic #include <signal.h>
2746795Sbostic #include <time.h>
2846795Sbostic #include <fstab.h>
2946588Storek #include <grp.h>
3046588Storek #include <varargs.h>
3146795Sbostic #include <utmp.h>
3246816Sbostic #include <tzfile.h>
3346795Sbostic #include <errno.h>
3446795Sbostic #ifdef __STDC__
3546795Sbostic #include <unistd.h>
3646795Sbostic #include <stdlib.h>
3746795Sbostic #include <string.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 	 */
941424Sroot 	alarm(0);
951424Sroot 	if (signal(SIGALRM, sigalrm) == SIG_IGN)
961424Sroot 		signal(SIGALRM, SIG_IGN);
971424Sroot 	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);
1261424Sroot 	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);
1571424Sroot 	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 	int	nusers;
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 
1971424Sroot 	clock = time(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 
2051424Sroot 	nusers = 0;
20646588Storek 	while (!feof(f_utmp)) {
2071424Sroot 		if (fread(&utmp, sizeof (struct utmp), 1, f_utmp) != 1)
2081424Sroot 			break;
2091424Sroot 		if (utmp.ut_name[0] == 0)
2101424Sroot 			continue;
2111424Sroot 		nusers++;
21246588Storek 		for (np = gp->gr_mem; *np; np++) {
2131424Sroot 			if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
2141424Sroot 				continue;
2151424Sroot 			/*
2161424Sroot 			 *	Do not send messages to operators on dialups
2171424Sroot 			 */
2181424Sroot 			if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
2191424Sroot 				continue;
2201424Sroot #ifdef DEBUG
22146588Storek 			msg("Message to %s at %s\n", *np, utmp.ut_line);
22246588Storek #endif
2231424Sroot 			sendmes(utmp.ut_line, message);
2241424Sroot 		}
2251424Sroot 	}
22646588Storek 	(void) fclose(f_utmp);
2271424Sroot 	Exit(0);	/* the wait in this same routine will catch this */
2281424Sroot 	/* NOTREACHED */
2291424Sroot }
2301424Sroot 
23146588Storek static void
2321424Sroot sendmes(tty, message)
2335319Smckusic 	char *tty, *message;
2341424Sroot {
2351424Sroot 	char t[50], buf[BUFSIZ];
2361424Sroot 	register char *cp;
23739130Smckusick 	int lmsg = 1;
2381424Sroot 	FILE *f_tty;
2391424Sroot 
24046588Storek 	(void) strcpy(t, _PATH_DEV);
24146588Storek 	(void) strcat(t, tty);
2421424Sroot 
24346588Storek 	if ((f_tty = fopen(t, "w")) != NULL) {
2441424Sroot 		setbuf(f_tty, buf);
24546588Storek 		(void) fprintf(f_tty,
24646588Storek 		    "\n\
24746588Storek \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
24846588Storek DUMP: NEEDS ATTENTION: ",
24946588Storek 		    localclock->tm_hour, localclock->tm_min);
25039130Smckusick 		for (cp = lastmsg; ; cp++) {
25139130Smckusick 			if (*cp == '\0') {
25239130Smckusick 				if (lmsg) {
25339130Smckusick 					cp = message;
25439130Smckusick 					if (*cp == '\0')
25539130Smckusick 						break;
25639130Smckusick 					lmsg = 0;
25739130Smckusick 				} else
25839130Smckusick 					break;
25939130Smckusick 			}
26039130Smckusick 			if (*cp == '\n')
26146588Storek 				(void) putc('\r', f_tty);
26246588Storek 			(void) putc(*cp, f_tty);
2631424Sroot 		}
26446588Storek 		(void) fclose(f_tty);
2651424Sroot 	}
2661424Sroot }
2671424Sroot 
2681424Sroot /*
2691424Sroot  *	print out an estimate of the amount of time left to do the dump
2701424Sroot  */
2711424Sroot 
2721424Sroot time_t	tschedule = 0;
2731424Sroot 
27446588Storek void
2751424Sroot timeest()
2761424Sroot {
2771424Sroot 	time_t	tnow, deltat;
2781424Sroot 
2791424Sroot 	time (&tnow);
28046588Storek 	if (tnow >= tschedule) {
2811424Sroot 		tschedule = tnow + 300;
2821424Sroot 		if (blockswritten < 500)
2831424Sroot 			return;
2841424Sroot 		deltat = tstart_writing - tnow +
28546788Smckusick 			(1.0 * (tnow - tstart_writing))
28646788Smckusick 			/ blockswritten * tapesize;
2871424Sroot 		msg("%3.2f%% done, finished in %d:%02d\n",
28846788Smckusick 			(blockswritten * 100.0) / tapesize,
28946788Smckusick 			deltat / 3600, (deltat % 3600) / 60);
2901424Sroot 	}
2911424Sroot }
2921424Sroot 
29346788Smckusick /*
29446788Smckusick  *	tapesize: total number of blocks estimated over all reels
29546788Smckusick  *	blockswritten:	blocks actually written, over all reels
29646788Smckusick  *	etapes:	estimated number of tapes to write
29746788Smckusick  *
29846788Smckusick  *	tsize:	blocks can write on this reel
29946788Smckusick  *	asize:	blocks written on this reel
30046788Smckusick  *	tapeno:	number of tapes written so far
30146788Smckusick  */
30246588Storek int
30346588Storek blocksontape()
3041424Sroot {
3051424Sroot 	if (tapeno == etapes)
30646788Smckusick 		return (tapesize - (etapes - 1) * tsize);
30746588Storek 	return (tsize);
3081424Sroot }
3091424Sroot 
31046588Storek #ifdef lint
31146588Storek 
31246588Storek /* VARARGS1 */
31346588Storek void msg(fmt) char *fmt; { strcpy(lastmsg, fmt); }
31446588Storek 
31546588Storek /* VARARGS1 */
31646588Storek void msgtail(fmt) char *fmt; { fmt = fmt; }
31746588Storek 
31846588Storek void quit(fmt) char *fmt; { msg(fmt); dumpabort(); }
31946588Storek 
32046588Storek #else /* lint */
32146588Storek 
32246588Storek void
32346588Storek msg(va_alist)
32446588Storek 	va_dcl
3251424Sroot {
32646588Storek 	va_list ap;
32746588Storek 	char *fmt;
32846588Storek 
32946588Storek 	(void) fprintf(stderr,"  DUMP: ");
3301424Sroot #ifdef TDEBUG
33146588Storek 	(void) fprintf(stderr, "pid=%d ", getpid());
3321424Sroot #endif
33346588Storek 	va_start(ap);
33446588Storek 	fmt = va_arg(ap, char *);
33546588Storek 	(void) vfprintf(stderr, fmt, ap);
33646588Storek 	va_end(ap);
33746588Storek 	(void) fflush(stdout);
33846588Storek 	(void) fflush(stderr);
33946588Storek 	va_start(ap);
34046588Storek 	fmt = va_arg(ap, char *);
34146588Storek 	(void) vsprintf(lastmsg, fmt, ap);
34246588Storek 	va_end(ap);
3431424Sroot }
3441424Sroot 
34546588Storek void
34646588Storek msgtail(va_alist)
34746588Storek 	va_dcl
3481424Sroot {
34946588Storek 	va_list ap;
35046588Storek 	char *fmt;
35146588Storek 
35246588Storek 	va_start(ap);
35346588Storek 	fmt = va_arg(ap, char *);
35446588Storek 	(void) vfprintf(stderr, fmt, ap);
35546588Storek 	va_end(ap);
3561424Sroot }
35746588Storek 
35846588Storek void
35946588Storek quit(va_alist)
36046588Storek 	va_dcl
36146588Storek {
36246588Storek 	va_list ap;
36346588Storek 	char *fmt;
36446588Storek 
36546588Storek 	(void) fprintf(stderr,"  DUMP: ");
36646588Storek #ifdef TDEBUG
36746588Storek 	(void) fprintf(stderr, "pid=%d ", getpid());
36846588Storek #endif
36946588Storek 	va_start(ap);
37046588Storek 	fmt = va_arg(ap, char *);
37146588Storek 	vfprintf(stderr, fmt, ap);
37246588Storek 	va_end(ap);
37346588Storek 	(void) fflush(stdout);
37446588Storek 	(void) fflush(stderr);
37546588Storek 	dumpabort();
37646588Storek }
37746588Storek 
37846588Storek #endif /* lint */
37946588Storek 
3801424Sroot /*
3811424Sroot  *	Tell the operator what has to be done;
3821424Sroot  *	we don't actually do it
3831424Sroot  */
3841424Sroot 
38513047Ssam struct fstab *
38613047Ssam allocfsent(fs)
38713047Ssam 	register struct fstab *fs;
38813047Ssam {
38913047Ssam 	register struct fstab *new;
39013047Ssam 
39113047Ssam 	new = (struct fstab *)malloc(sizeof (*fs));
39246588Storek 	if (new == NULL ||
39346588Storek 	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
39446588Storek 	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
39546588Storek 	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
39646588Storek 		quit("%s\n", strerror(errno));
39713047Ssam 	new->fs_passno = fs->fs_passno;
39813047Ssam 	new->fs_freq = fs->fs_freq;
39913047Ssam 	return (new);
40013047Ssam }
40113047Ssam 
40213047Ssam struct	pfstab {
40313047Ssam 	struct	pfstab *pf_next;
40413047Ssam 	struct	fstab *pf_fstab;
40513047Ssam };
40613047Ssam 
40746588Storek static	struct pfstab *table;
40813047Ssam 
40946588Storek void
4101424Sroot getfstab()
4111424Sroot {
41213047Ssam 	register struct fstab *fs;
41313047Ssam 	register struct pfstab *pf;
4141424Sroot 
4151428Sroot 	if (setfsent() == 0) {
41646588Storek 		msg("Can't open %s for dump table information: %s\n",
41746588Storek 		    _PATH_FSTAB, strerror(errno));
41813047Ssam 		return;
4191424Sroot 	}
42013047Ssam 	while (fs = getfsent()) {
42113047Ssam 		if (strcmp(fs->fs_type, FSTAB_RW) &&
42213047Ssam 		    strcmp(fs->fs_type, FSTAB_RO) &&
42313047Ssam 		    strcmp(fs->fs_type, FSTAB_RQ))
42413047Ssam 			continue;
42513047Ssam 		fs = allocfsent(fs);
42646588Storek 		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
42746588Storek 			quit("%s\n", strerror(errno));
42813047Ssam 		pf->pf_fstab = fs;
42913047Ssam 		pf->pf_next = table;
43013047Ssam 		table = pf;
43113047Ssam 	}
43213047Ssam 	endfsent();
4331424Sroot }
4341424Sroot 
4351424Sroot /*
43613047Ssam  * Search in the fstab for a file name.
43713047Ssam  * This file name can be either the special or the path file name.
4381424Sroot  *
43913047Ssam  * The entries in the fstab are the BLOCK special names, not the
44013047Ssam  * character special names.
44113047Ssam  * The caller of fstabsearch assures that the character device
44213047Ssam  * is dumped (that is much faster)
4431424Sroot  *
44413047Ssam  * The file name can omit the leading '/'.
4451424Sroot  */
44613047Ssam struct fstab *
44713047Ssam fstabsearch(key)
44813047Ssam 	char *key;
4491424Sroot {
45013047Ssam 	register struct pfstab *pf;
45113047Ssam 	register struct fstab *fs;
45213047Ssam 	char *rawname();
4531424Sroot 
45446588Storek 	for (pf = table; pf != NULL; pf = pf->pf_next) {
45513047Ssam 		fs = pf->pf_fstab;
45646588Storek 		if (strcmp(fs->fs_file, key) == 0 ||
45746588Storek 		    strcmp(fs->fs_spec, key) == 0 ||
45846588Storek 		    strcmp(rawname(fs->fs_spec), key) == 0)
45913047Ssam 			return (fs);
46046588Storek 		if (key[0] != '/') {
46113047Ssam 			if (*fs->fs_spec == '/' &&
46213197Sroot 			    strcmp(fs->fs_spec + 1, key) == 0)
46313047Ssam 				return (fs);
46413047Ssam 			if (*fs->fs_file == '/' &&
46513197Sroot 			    strcmp(fs->fs_file + 1, key) == 0)
46613047Ssam 				return (fs);
4671424Sroot 		}
4681424Sroot 	}
46946588Storek 	return (NULL);
4701424Sroot }
4711424Sroot 
4721424Sroot /*
4731424Sroot  *	Tell the operator what to do
4741424Sroot  */
47546588Storek void
4761463Sroot lastdump(arg)
47746588Storek 	char	arg;	/* w ==> just what to do; W ==> most recent dumps */
4781424Sroot {
47946788Smckusick 	register int i;
48046788Smckusick 	register struct fstab *dt;
48146788Smckusick 	register struct dumpdates *dtwalk;
48246788Smckusick 	char *lastname, *date;
48346788Smckusick 	int dumpme, datesort();
48446788Smckusick 	time_t tnow;
4851424Sroot 
4861424Sroot 	time(&tnow);
4871424Sroot 	getfstab();		/* /etc/fstab input */
48846788Smckusick 	initdumptimes();	/* /etc/dumpdates input */
48946788Smckusick 	qsort(ddatev, nddates, sizeof(struct dumpdates *), datesort);
4901424Sroot 
4911463Sroot 	if (arg == 'w')
49246588Storek 		(void) printf("Dump these file systems:\n");
4931463Sroot 	else
49446588Storek 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
4951424Sroot 	lastname = "??";
49646788Smckusick 	ITITERATE(i, dtwalk) {
49746788Smckusick 		if (strncmp(lastname, dtwalk->dd_name,
49846788Smckusick 		    sizeof(dtwalk->dd_name)) == 0)
4991424Sroot 			continue;
50046788Smckusick 		date = (char *)ctime(&dtwalk->dd_ddate);
50146588Storek 		date[16] = '\0';	/* blast away seconds and year */
50246788Smckusick 		lastname = dtwalk->dd_name;
50346788Smckusick 		dt = fstabsearch(dtwalk->dd_name);
50446588Storek 		dumpme = (dt != NULL &&
50546588Storek 		    dt->fs_freq != 0 &&
50646816Sbostic 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
50746588Storek 		if (arg != 'w' || dumpme)
50846588Storek 			(void) printf(
50946588Storek 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
51046588Storek 			    dumpme && (arg != 'w') ? '>' : ' ',
51146788Smckusick 			    dtwalk->dd_name,
51246588Storek 			    dt ? dt->fs_file : "",
51346788Smckusick 			    dtwalk->dd_level,
51446588Storek 			    date);
5151424Sroot 	}
5161424Sroot }
5171424Sroot 
51846788Smckusick int
51946788Smckusick datesort(a1, a2)
52046588Storek 	void *a1, *a2;
5211424Sroot {
52246788Smckusick 	struct dumpdates *d1 = *(struct dumpdates **)a1;
52346788Smckusick 	struct dumpdates *d2 = *(struct dumpdates **)a2;
52446588Storek 	int diff;
5251424Sroot 
52646788Smckusick 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
5271424Sroot 	if (diff == 0)
52846788Smckusick 		return (d2->dd_ddate - d1->dd_ddate);
52946788Smckusick 	return (diff);
5301424Sroot }
5311424Sroot 
53246588Storek int max(a, b)
5335319Smckusic 	int a, b;
5341424Sroot {
53546588Storek 	return (a > b ? a : b);
5361424Sroot }
53746588Storek int min(a, b)
5385319Smckusic 	int a, b;
5391424Sroot {
54046588Storek 	return (a < b ? a : b);
5411424Sroot }
542