Home | History | Annotate | Line # | Download | only in dump
optr.c revision 1.16
      1 /*	$NetBSD: optr.c,v 1.16 2001/05/28 00:41:14 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)optr.c	8.2 (Berkeley) 1/6/94";
     40 #else
     41 __RCSID("$NetBSD: optr.c,v 1.16 2001/05/28 00:41:14 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/wait.h>
     47 #include <sys/time.h>
     48 
     49 #include <errno.h>
     50 #include <fstab.h>
     51 #include <grp.h>
     52 #include <signal.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <stdarg.h>
     57 #include <time.h>
     58 #include <tzfile.h>
     59 #include <unistd.h>
     60 #include <utmp.h>
     61 
     62 #include "dump.h"
     63 #include "pathnames.h"
     64 
     65 void	alarmcatch(int);
     66 struct fstab *allocfsent(struct fstab *);
     67 int	datesort(const void *, const void *);
     68 static	void sendmes(char *, char *);
     69 
     70 /*
     71  *	Query the operator; This previously-fascist piece of code
     72  *	no longer requires an exact response.
     73  *	It is intended to protect dump aborting by inquisitive
     74  *	people banging on the console terminal to see what is
     75  *	happening which might cause dump to croak, destroying
     76  *	a large number of hours of work.
     77  *
     78  *	Every 2 minutes we reprint the message, alerting others
     79  *	that dump needs attention.
     80  */
     81 static	int timeout;
     82 static	char *attnmessage;		/* attention message */
     83 
     84 int
     85 query(char *question)
     86 {
     87 	char	replybuffer[64];
     88 	int	back, errcount;
     89 	FILE	*mytty;
     90 	time_t	firstprompt, when_answered;
     91 
     92 	firstprompt = time((time_t *)0);
     93 
     94 	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
     95 		quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
     96 	attnmessage = question;
     97 	timeout = 0;
     98 	alarmcatch(0);
     99 	back = -1;
    100 	errcount = 0;
    101 	do {
    102 		if (fgets(replybuffer, 63, mytty) == NULL) {
    103 			clearerr(mytty);
    104 			if (++errcount > 30)	/* XXX	ugly */
    105 				quit("excessive operator query failures\n");
    106 		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
    107 			back = 1;
    108 		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
    109 			back = 0;
    110 		} else {
    111 			(void) fprintf(stderr,
    112 			    "  DUMP: \"Yes\" or \"No\"?\n");
    113 			(void) fprintf(stderr,
    114 			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
    115 		}
    116 	} while (back < 0);
    117 
    118 	/*
    119 	 *	Turn off the alarm, and reset the signal to trap out..
    120 	 */
    121 	(void) alarm(0);
    122 	if (signal(SIGALRM, sig) == SIG_IGN)
    123 		signal(SIGALRM, SIG_IGN);
    124 	(void) fclose(mytty);
    125 	when_answered = time((time_t *)0);
    126 	/*
    127 	 * Adjust the base for time estimates to ignore time we spent waiting
    128 	 * for operator input.
    129 	 */
    130 	if (tstart_writing != 0)
    131 	    tstart_writing += (when_answered - firstprompt);
    132 	return(back);
    133 }
    134 
    135 char lastmsg[100];
    136 
    137 /*
    138  *	Alert the console operator, and enable the alarm clock to
    139  *	sleep for 2 minutes in case nobody comes to satisfy dump
    140  */
    141 void
    142 alarmcatch(int dummy)
    143 {
    144 
    145 	if (notify == 0) {
    146 		if (timeout == 0)
    147 			(void) fprintf(stderr,
    148 			    "  DUMP: %s: (\"yes\" or \"no\") ",
    149 			    attnmessage);
    150 		else
    151 			msgtail("\7\7");
    152 	} else {
    153 		if (timeout) {
    154 			msgtail("\n");
    155 			broadcast("");		/* just print last msg */
    156 		}
    157 		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
    158 		    attnmessage);
    159 	}
    160 	signal(SIGALRM, alarmcatch);
    161 	(void) alarm(120);
    162 	timeout = 1;
    163 }
    164 
    165 /*
    166  *	Here if an inquisitive operator interrupts the dump program
    167  */
    168 void
    169 interrupt(int signo)
    170 {
    171 
    172 	msg("Interrupt received.\n");
    173 	if (query("Do you want to abort dump?"))
    174 		dumpabort(0);
    175 }
    176 
    177 /*
    178  *	The following variables and routines manage alerting
    179  *	operators to the status of dump.
    180  *	This works much like wall(1) does.
    181  */
    182 struct	group *gp;
    183 
    184 /*
    185  *	Get the names from the group entry "operator" to notify.
    186  */
    187 void
    188 set_operators(void)
    189 {
    190 
    191 	if (!notify)		/*not going to notify*/
    192 		return;
    193 	gp = getgrnam(OPGRENT);
    194 	(void) endgrent();
    195 	if (gp == NULL) {
    196 		msg("No group entry for %s.\n", OPGRENT);
    197 		notify = 0;
    198 		return;
    199 	}
    200 }
    201 
    202 struct tm *localclock;
    203 
    204 /*
    205  *	We fork a child to do the actual broadcasting, so
    206  *	that the process control groups are not messed up
    207  */
    208 void
    209 broadcast(char	*message)
    210 {
    211 	time_t		clock;
    212 	FILE	*f_utmp;
    213 	struct	utmp	utmp;
    214 	char	**np;
    215 	int	pid, s;
    216 
    217 	if (!notify || gp == NULL)
    218 		return;
    219 
    220 	switch (pid = fork()) {
    221 	case -1:
    222 		return;
    223 	case 0:
    224 		break;
    225 	default:
    226 		while (wait(&s) != pid)
    227 			continue;
    228 		return;
    229 	}
    230 
    231 	clock = time((time_t *)0);
    232 	localclock = localtime(&clock);
    233 
    234 	if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
    235 		msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
    236 		return;
    237 	}
    238 
    239 	while (!feof(f_utmp)) {
    240 		if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
    241 			break;
    242 		if (utmp.ut_name[0] == 0)
    243 			continue;
    244 		for (np = gp->gr_mem; *np; np++) {
    245 			if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
    246 				continue;
    247 			/*
    248 			 *	Do not send messages to operators on dialups
    249 			 */
    250 			if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
    251 				continue;
    252 #ifdef DEBUG
    253 			msg("Message to %s at %s\n", *np, utmp.ut_line);
    254 #endif
    255 			sendmes(utmp.ut_line, message);
    256 		}
    257 	}
    258 	(void) fclose(f_utmp);
    259 	Exit(0);	/* the wait in this same routine will catch this */
    260 	/* NOTREACHED */
    261 }
    262 
    263 static void
    264 sendmes(char *tty, char *message)
    265 {
    266 	char t[50], buf[BUFSIZ];
    267 	char *cp;
    268 	int lmsg = 1;
    269 	FILE *f_tty;
    270 
    271 	(void)strncpy(t, _PATH_DEV, sizeof(t) - 1);
    272 	(void)strncat(t, tty, sizeof(t) - sizeof(_PATH_DEV) - 1);
    273 	t[sizeof(t) - 1] = '\0';
    274 
    275 	if ((f_tty = fopen(t, "w")) != NULL) {
    276 		setbuf(f_tty, buf);
    277 		(void) fprintf(f_tty,
    278 		    "\n\
    279 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
    280 DUMP: NEEDS ATTENTION: ",
    281 		    localclock->tm_hour, localclock->tm_min);
    282 		for (cp = lastmsg; ; cp++) {
    283 			if (*cp == '\0') {
    284 				if (lmsg) {
    285 					cp = message;
    286 					if (*cp == '\0')
    287 						break;
    288 					lmsg = 0;
    289 				} else
    290 					break;
    291 			}
    292 			if (*cp == '\n')
    293 				(void) putc('\r', f_tty);
    294 			(void) putc(*cp, f_tty);
    295 		}
    296 		(void) fclose(f_tty);
    297 	}
    298 }
    299 
    300 /*
    301  *	print out an estimate of the amount of time left to do the dump
    302  */
    303 
    304 time_t	tschedule = 0;
    305 
    306 void
    307 timeest(void)
    308 {
    309 	time_t	tnow, deltat;
    310 
    311 	(void) time((time_t *) &tnow);
    312 	if (tnow >= tschedule) {
    313 		tschedule = tnow + 300;
    314 		if (blockswritten < 500)
    315 			return;
    316 		deltat = tstart_writing - tnow +
    317 			(1.0 * (tnow - tstart_writing))
    318 			/ blockswritten * tapesize;
    319 		msg("%3.2f%% done, finished in %ld:%02ld\n",
    320 		    (blockswritten * 100.0) / tapesize,
    321 		    (long)(deltat / 3600), (long)((deltat % 3600) / 60));
    322 	}
    323 }
    324 
    325 void
    326 msg(const char *fmt, ...)
    327 {
    328 	va_list ap;
    329 
    330 	(void) fprintf(stderr,"  DUMP: ");
    331 #ifdef TDEBUG
    332 	(void) fprintf(stderr, "pid=%d ", getpid());
    333 #endif
    334 	va_start(ap, fmt);
    335 	(void) vfprintf(stderr, fmt, ap);
    336 	(void) fflush(stdout);
    337 	(void) fflush(stderr);
    338 	(void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap);
    339 	va_end(ap);
    340 }
    341 
    342 void
    343 msgtail(const char *fmt, ...)
    344 {
    345 	va_list ap;
    346 
    347 	va_start(ap, fmt);
    348 	(void) vfprintf(stderr, fmt, ap);
    349 	va_end(ap);
    350 }
    351 
    352 void
    353 quit(const char *fmt, ...)
    354 {
    355 	va_list ap;
    356 
    357 	(void) fprintf(stderr,"  DUMP: ");
    358 #ifdef TDEBUG
    359 	(void) fprintf(stderr, "pid=%d ", getpid());
    360 #endif
    361 	va_start(ap, fmt);
    362 	(void) vfprintf(stderr, fmt, ap);
    363 	va_end(ap);
    364 	(void) fflush(stdout);
    365 	(void) fflush(stderr);
    366 	dumpabort(0);
    367 }
    368 
    369 /*
    370  *	Tell the operator what has to be done;
    371  *	we don't actually do it
    372  */
    373 
    374 struct fstab *
    375 allocfsent(struct fstab *fs)
    376 {
    377 	struct fstab *new;
    378 
    379 	new = (struct fstab *)malloc(sizeof (*fs));
    380 	if (new == NULL ||
    381 	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
    382 	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
    383 	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
    384 		quit("%s\n", strerror(errno));
    385 	new->fs_passno = fs->fs_passno;
    386 	new->fs_freq = fs->fs_freq;
    387 	return (new);
    388 }
    389 
    390 struct	pfstab {
    391 	struct	pfstab *pf_next;
    392 	struct	fstab *pf_fstab;
    393 };
    394 
    395 static	struct pfstab *table;
    396 
    397 void
    398 getfstab(void)
    399 {
    400 	struct fstab *fs;
    401 	struct pfstab *pf;
    402 
    403 	if (setfsent() == 0) {
    404 		msg("Can't open %s for dump table information: %s\n",
    405 		    _PATH_FSTAB, strerror(errno));
    406 		return;
    407 	}
    408 	while ((fs = getfsent()) != NULL) {
    409 		if (strcmp(fs->fs_type, FSTAB_RW) &&
    410 		    strcmp(fs->fs_type, FSTAB_RO) &&
    411 		    strcmp(fs->fs_type, FSTAB_RQ))
    412 			continue;
    413 		if (strcmp(fs->fs_vfstype, "ufs") &&
    414 		    strcmp(fs->fs_vfstype, "ffs"))
    415 			continue;
    416 		fs = allocfsent(fs);
    417 		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
    418 			quit("%s\n", strerror(errno));
    419 		pf->pf_fstab = fs;
    420 		pf->pf_next = table;
    421 		table = pf;
    422 	}
    423 	(void) endfsent();
    424 }
    425 
    426 /*
    427  * Search in the fstab for a file name.
    428  * This file name can be either the special or the path file name.
    429  *
    430  * The entries in the fstab are the BLOCK special names, not the
    431  * character special names.
    432  * The caller of fstabsearch assures that the character device
    433  * is dumped (that is much faster)
    434  *
    435  * The file name can omit the leading '/'.
    436  */
    437 struct fstab *
    438 fstabsearch(char *key)
    439 {
    440 	struct pfstab *pf;
    441 	struct fstab *fs;
    442 	char *rn;
    443 
    444 	for (pf = table; pf != NULL; pf = pf->pf_next) {
    445 		fs = pf->pf_fstab;
    446 		if (strcmp(fs->fs_file, key) == 0 ||
    447 		    strcmp(fs->fs_spec, key) == 0)
    448 			return (fs);
    449 		rn = rawname(fs->fs_spec);
    450 		if (rn != NULL && strcmp(rn, key) == 0)
    451 			return (fs);
    452 		if (key[0] != '/') {
    453 			if (*fs->fs_spec == '/' &&
    454 			    strcmp(fs->fs_spec + 1, key) == 0)
    455 				return (fs);
    456 			if (*fs->fs_file == '/' &&
    457 			    strcmp(fs->fs_file + 1, key) == 0)
    458 				return (fs);
    459 		}
    460 	}
    461 	return (NULL);
    462 }
    463 
    464 /*
    465  *	Tell the operator what to do.
    466  *	arg:	w ==> just what to do; W ==> most recent dumps
    467  */
    468 void
    469 lastdump(char arg)
    470 {
    471 	int i;
    472 	struct fstab *dt;
    473 	struct dumpdates *dtwalk;
    474 	char *lastname, *date;
    475 	int dumpme;
    476 	time_t tnow;
    477 
    478 	(void) time(&tnow);
    479 	getfstab();		/* /etc/fstab input */
    480 	initdumptimes();	/* /etc/dumpdates input */
    481 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
    482 
    483 	if (arg == 'w')
    484 		(void) printf("Dump these file systems:\n");
    485 	else
    486 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
    487 	lastname = "??";
    488 	ITITERATE(i, dtwalk) {
    489 		if (strncmp(lastname, dtwalk->dd_name,
    490 		    sizeof(dtwalk->dd_name)) == 0)
    491 			continue;
    492 		date = (char *)ctime(&dtwalk->dd_ddate);
    493 		date[24] = '\0';
    494 		strcpy(date + 16, date + 19);	/* blast away seconds */
    495 		lastname = dtwalk->dd_name;
    496 		dt = fstabsearch(dtwalk->dd_name);
    497 		dumpme = (dt != NULL &&
    498 		    dt->fs_freq != 0 &&
    499 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
    500 		if (arg != 'w' || dumpme)
    501 			(void) printf(
    502 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
    503 			    dumpme && (arg != 'w') ? '>' : ' ',
    504 			    dtwalk->dd_name,
    505 			    dt ? dt->fs_file : "",
    506 			    dtwalk->dd_level,
    507 			    date);
    508 	}
    509 }
    510 
    511 int
    512 datesort(const void *a1, const void *a2)
    513 {
    514 	struct dumpdates *d1 = *(struct dumpdates **)a1;
    515 	struct dumpdates *d2 = *(struct dumpdates **)a2;
    516 	int diff;
    517 
    518 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
    519 	if (diff == 0)
    520 		return (d2->dd_ddate - d1->dd_ddate);
    521 	return (diff);
    522 }
    523