Home | History | Annotate | Line # | Download | only in dump
optr.c revision 1.12
      1 /*	$NetBSD: optr.c,v 1.12 1997/09/15 07:58:06 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.12 1997/09/15 07:58:06 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 #ifdef __STDC__
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <stdarg.h>
     58 #endif
     59 #include <tzfile.h>
     60 #ifdef __STDC__
     61 #include <unistd.h>
     62 #endif
     63 #include <utmp.h>
     64 #ifndef __STDC__
     65 #include <varargs.h>
     66 #endif
     67 
     68 #include "dump.h"
     69 #include "pathnames.h"
     70 
     71 void	alarmcatch __P((int));
     72 struct fstab *allocfsent __P((struct fstab *fs));
     73 int	datesort __P((const void *, const void *));
     74 static	void sendmes __P((char *, char *));
     75 
     76 /*
     77  *	Query the operator; This previously-fascist piece of code
     78  *	no longer requires an exact response.
     79  *	It is intended to protect dump aborting by inquisitive
     80  *	people banging on the console terminal to see what is
     81  *	happening which might cause dump to croak, destroying
     82  *	a large number of hours of work.
     83  *
     84  *	Every 2 minutes we reprint the message, alerting others
     85  *	that dump needs attention.
     86  */
     87 static	int timeout;
     88 static	char *attnmessage;		/* attention message */
     89 
     90 int
     91 query(question)
     92 	char	*question;
     93 {
     94 	char	replybuffer[64];
     95 	int	back, errcount;
     96 	FILE	*mytty;
     97 	time_t	firstprompt, when_answered;
     98 
     99 	firstprompt = time((time_t *)0);
    100 
    101 	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
    102 		quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
    103 	attnmessage = question;
    104 	timeout = 0;
    105 	alarmcatch(0);
    106 	back = -1;
    107 	errcount = 0;
    108 	do {
    109 		if (fgets(replybuffer, 63, mytty) == NULL) {
    110 			clearerr(mytty);
    111 			if (++errcount > 30)	/* XXX	ugly */
    112 				quit("excessive operator query failures\n");
    113 		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
    114 			back = 1;
    115 		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
    116 			back = 0;
    117 		} else {
    118 			(void) fprintf(stderr,
    119 			    "  DUMP: \"Yes\" or \"No\"?\n");
    120 			(void) fprintf(stderr,
    121 			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
    122 		}
    123 	} while (back < 0);
    124 
    125 	/*
    126 	 *	Turn off the alarm, and reset the signal to trap out..
    127 	 */
    128 	(void) alarm(0);
    129 	if (signal(SIGALRM, sig) == SIG_IGN)
    130 		signal(SIGALRM, SIG_IGN);
    131 	(void) fclose(mytty);
    132 	when_answered = time((time_t *)0);
    133 	/*
    134 	 * Adjust the base for time estimates to ignore time we spent waiting
    135 	 * for operator input.
    136 	 */
    137 	if (tstart_writing != 0)
    138 	    tstart_writing += (when_answered - firstprompt);
    139 	return(back);
    140 }
    141 
    142 char lastmsg[100];
    143 
    144 /*
    145  *	Alert the console operator, and enable the alarm clock to
    146  *	sleep for 2 minutes in case nobody comes to satisfy dump
    147  */
    148 void
    149 alarmcatch(dummy)
    150 	int dummy;
    151 {
    152 	if (notify == 0) {
    153 		if (timeout == 0)
    154 			(void) fprintf(stderr,
    155 			    "  DUMP: %s: (\"yes\" or \"no\") ",
    156 			    attnmessage);
    157 		else
    158 			msgtail("\7\7");
    159 	} else {
    160 		if (timeout) {
    161 			msgtail("\n");
    162 			broadcast("");		/* just print last msg */
    163 		}
    164 		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
    165 		    attnmessage);
    166 	}
    167 	signal(SIGALRM, alarmcatch);
    168 	(void) alarm(120);
    169 	timeout = 1;
    170 }
    171 
    172 /*
    173  *	Here if an inquisitive operator interrupts the dump program
    174  */
    175 void
    176 interrupt(signo)
    177 	int signo;
    178 {
    179 	msg("Interrupt received.\n");
    180 	if (query("Do you want to abort dump?"))
    181 		dumpabort(0);
    182 }
    183 
    184 /*
    185  *	The following variables and routines manage alerting
    186  *	operators to the status of dump.
    187  *	This works much like wall(1) does.
    188  */
    189 struct	group *gp;
    190 
    191 /*
    192  *	Get the names from the group entry "operator" to notify.
    193  */
    194 void
    195 set_operators()
    196 {
    197 	if (!notify)		/*not going to notify*/
    198 		return;
    199 	gp = getgrnam(OPGRENT);
    200 	(void) endgrent();
    201 	if (gp == NULL) {
    202 		msg("No group entry for %s.\n", OPGRENT);
    203 		notify = 0;
    204 		return;
    205 	}
    206 }
    207 
    208 struct tm *localclock;
    209 
    210 /*
    211  *	We fork a child to do the actual broadcasting, so
    212  *	that the process control groups are not messed up
    213  */
    214 void
    215 broadcast(message)
    216 	char	*message;
    217 {
    218 	time_t		clock;
    219 	FILE	*f_utmp;
    220 	struct	utmp	utmp;
    221 	char	**np;
    222 	int	pid, s;
    223 
    224 	if (!notify || gp == NULL)
    225 		return;
    226 
    227 	switch (pid = fork()) {
    228 	case -1:
    229 		return;
    230 	case 0:
    231 		break;
    232 	default:
    233 		while (wait(&s) != pid)
    234 			continue;
    235 		return;
    236 	}
    237 
    238 	clock = time((time_t *)0);
    239 	localclock = localtime(&clock);
    240 
    241 	if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
    242 		msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
    243 		return;
    244 	}
    245 
    246 	while (!feof(f_utmp)) {
    247 		if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
    248 			break;
    249 		if (utmp.ut_name[0] == 0)
    250 			continue;
    251 		for (np = gp->gr_mem; *np; np++) {
    252 			if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
    253 				continue;
    254 			/*
    255 			 *	Do not send messages to operators on dialups
    256 			 */
    257 			if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
    258 				continue;
    259 #ifdef DEBUG
    260 			msg("Message to %s at %s\n", *np, utmp.ut_line);
    261 #endif
    262 			sendmes(utmp.ut_line, message);
    263 		}
    264 	}
    265 	(void) fclose(f_utmp);
    266 	Exit(0);	/* the wait in this same routine will catch this */
    267 	/* NOTREACHED */
    268 }
    269 
    270 static void
    271 sendmes(tty, message)
    272 	char *tty, *message;
    273 {
    274 	char t[50], buf[BUFSIZ];
    275 	char *cp;
    276 	int lmsg = 1;
    277 	FILE *f_tty;
    278 
    279 	(void)strncpy(t, _PATH_DEV, sizeof(t) - 1);
    280 	(void)strncat(t, tty, sizeof(t) - sizeof(_PATH_DEV) - 1);
    281 	t[sizeof(t) - 1] = '\0';
    282 
    283 	if ((f_tty = fopen(t, "w")) != NULL) {
    284 		setbuf(f_tty, buf);
    285 		(void) fprintf(f_tty,
    286 		    "\n\
    287 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
    288 DUMP: NEEDS ATTENTION: ",
    289 		    localclock->tm_hour, localclock->tm_min);
    290 		for (cp = lastmsg; ; cp++) {
    291 			if (*cp == '\0') {
    292 				if (lmsg) {
    293 					cp = message;
    294 					if (*cp == '\0')
    295 						break;
    296 					lmsg = 0;
    297 				} else
    298 					break;
    299 			}
    300 			if (*cp == '\n')
    301 				(void) putc('\r', f_tty);
    302 			(void) putc(*cp, f_tty);
    303 		}
    304 		(void) fclose(f_tty);
    305 	}
    306 }
    307 
    308 /*
    309  *	print out an estimate of the amount of time left to do the dump
    310  */
    311 
    312 time_t	tschedule = 0;
    313 
    314 void
    315 timeest()
    316 {
    317 	time_t	tnow, deltat;
    318 
    319 	(void) time((time_t *) &tnow);
    320 	if (tnow >= tschedule) {
    321 		tschedule = tnow + 300;
    322 		if (blockswritten < 500)
    323 			return;
    324 		deltat = tstart_writing - tnow +
    325 			(1.0 * (tnow - tstart_writing))
    326 			/ blockswritten * tapesize;
    327 		msg("%3.2f%% done, finished in %d:%02d\n",
    328 			(blockswritten * 100.0) / tapesize,
    329 			deltat / 3600, (deltat % 3600) / 60);
    330 	}
    331 }
    332 
    333 void
    334 #if __STDC__
    335 msg(const char *fmt, ...)
    336 #else
    337 msg(fmt, va_alist)
    338 	char *fmt;
    339 	va_dcl
    340 #endif
    341 {
    342 	va_list ap;
    343 
    344 	(void) fprintf(stderr,"  DUMP: ");
    345 #ifdef TDEBUG
    346 	(void) fprintf(stderr, "pid=%d ", getpid());
    347 #endif
    348 #if __STDC__
    349 	va_start(ap, fmt);
    350 #else
    351 	va_start(ap);
    352 #endif
    353 	(void) vfprintf(stderr, fmt, ap);
    354 	(void) fflush(stdout);
    355 	(void) fflush(stderr);
    356 	(void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap);
    357 	va_end(ap);
    358 }
    359 
    360 void
    361 #if __STDC__
    362 msgtail(const char *fmt, ...)
    363 #else
    364 msgtail(fmt, va_alist)
    365 	char *fmt;
    366 	va_dcl
    367 #endif
    368 {
    369 	va_list ap;
    370 #if __STDC__
    371 	va_start(ap, fmt);
    372 #else
    373 	va_start(ap);
    374 #endif
    375 	(void) vfprintf(stderr, fmt, ap);
    376 	va_end(ap);
    377 }
    378 
    379 void
    380 #if __STDC__
    381 quit(const char *fmt, ...)
    382 #else
    383 quit(fmt, va_alist)
    384 	char *fmt;
    385 	va_dcl
    386 #endif
    387 {
    388 	va_list ap;
    389 
    390 	(void) fprintf(stderr,"  DUMP: ");
    391 #ifdef TDEBUG
    392 	(void) fprintf(stderr, "pid=%d ", getpid());
    393 #endif
    394 #if __STDC__
    395 	va_start(ap, fmt);
    396 #else
    397 	va_start(ap);
    398 #endif
    399 	(void) vfprintf(stderr, fmt, ap);
    400 	va_end(ap);
    401 	(void) fflush(stdout);
    402 	(void) fflush(stderr);
    403 	dumpabort(0);
    404 }
    405 
    406 /*
    407  *	Tell the operator what has to be done;
    408  *	we don't actually do it
    409  */
    410 
    411 struct fstab *
    412 allocfsent(fs)
    413 	struct fstab *fs;
    414 {
    415 	struct fstab *new;
    416 
    417 	new = (struct fstab *)malloc(sizeof (*fs));
    418 	if (new == NULL ||
    419 	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
    420 	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
    421 	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
    422 		quit("%s\n", strerror(errno));
    423 	new->fs_passno = fs->fs_passno;
    424 	new->fs_freq = fs->fs_freq;
    425 	return (new);
    426 }
    427 
    428 struct	pfstab {
    429 	struct	pfstab *pf_next;
    430 	struct	fstab *pf_fstab;
    431 };
    432 
    433 static	struct pfstab *table;
    434 
    435 void
    436 getfstab()
    437 {
    438 	struct fstab *fs;
    439 	struct pfstab *pf;
    440 
    441 	if (setfsent() == 0) {
    442 		msg("Can't open %s for dump table information: %s\n",
    443 		    _PATH_FSTAB, strerror(errno));
    444 		return;
    445 	}
    446 	while ((fs = getfsent()) != NULL) {
    447 		if (strcmp(fs->fs_type, FSTAB_RW) &&
    448 		    strcmp(fs->fs_type, FSTAB_RO) &&
    449 		    strcmp(fs->fs_type, FSTAB_RQ))
    450 			continue;
    451 		if (strcmp(fs->fs_vfstype, "ufs") &&
    452 		    strcmp(fs->fs_vfstype, "ffs"))
    453 			continue;
    454 		fs = allocfsent(fs);
    455 		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
    456 			quit("%s\n", strerror(errno));
    457 		pf->pf_fstab = fs;
    458 		pf->pf_next = table;
    459 		table = pf;
    460 	}
    461 	(void) endfsent();
    462 }
    463 
    464 /*
    465  * Search in the fstab for a file name.
    466  * This file name can be either the special or the path file name.
    467  *
    468  * The entries in the fstab are the BLOCK special names, not the
    469  * character special names.
    470  * The caller of fstabsearch assures that the character device
    471  * is dumped (that is much faster)
    472  *
    473  * The file name can omit the leading '/'.
    474  */
    475 struct fstab *
    476 fstabsearch(key)
    477 	char *key;
    478 {
    479 	struct pfstab *pf;
    480 	struct fstab *fs;
    481 	char *rn;
    482 
    483 	for (pf = table; pf != NULL; pf = pf->pf_next) {
    484 		fs = pf->pf_fstab;
    485 		if (strcmp(fs->fs_file, key) == 0 ||
    486 		    strcmp(fs->fs_spec, key) == 0)
    487 			return (fs);
    488 		rn = rawname(fs->fs_spec);
    489 		if (rn != NULL && strcmp(rn, key) == 0)
    490 			return (fs);
    491 		if (key[0] != '/') {
    492 			if (*fs->fs_spec == '/' &&
    493 			    strcmp(fs->fs_spec + 1, key) == 0)
    494 				return (fs);
    495 			if (*fs->fs_file == '/' &&
    496 			    strcmp(fs->fs_file + 1, key) == 0)
    497 				return (fs);
    498 		}
    499 	}
    500 	return (NULL);
    501 }
    502 
    503 /*
    504  *	Tell the operator what to do
    505  */
    506 void
    507 lastdump(arg)
    508 	char	arg;	/* w ==> just what to do; W ==> most recent dumps */
    509 {
    510 	int i;
    511 	struct fstab *dt;
    512 	struct dumpdates *dtwalk;
    513 	char *lastname, *date;
    514 	int dumpme;
    515 	time_t tnow;
    516 
    517 	(void) time(&tnow);
    518 	getfstab();		/* /etc/fstab input */
    519 	initdumptimes();	/* /etc/dumpdates input */
    520 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
    521 
    522 	if (arg == 'w')
    523 		(void) printf("Dump these file systems:\n");
    524 	else
    525 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
    526 	lastname = "??";
    527 	ITITERATE(i, dtwalk) {
    528 		if (strncmp(lastname, dtwalk->dd_name,
    529 		    sizeof(dtwalk->dd_name)) == 0)
    530 			continue;
    531 		date = (char *)ctime(&dtwalk->dd_ddate);
    532 		date[24] = '\0';
    533 		strcpy(date + 16, date + 19);	/* blast away seconds */
    534 		lastname = dtwalk->dd_name;
    535 		dt = fstabsearch(dtwalk->dd_name);
    536 		dumpme = (dt != NULL &&
    537 		    dt->fs_freq != 0 &&
    538 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
    539 		if (arg != 'w' || dumpme)
    540 			(void) printf(
    541 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
    542 			    dumpme && (arg != 'w') ? '>' : ' ',
    543 			    dtwalk->dd_name,
    544 			    dt ? dt->fs_file : "",
    545 			    dtwalk->dd_level,
    546 			    date);
    547 	}
    548 }
    549 
    550 int
    551 datesort(a1, a2)
    552 	const void *a1, *a2;
    553 {
    554 	struct dumpdates *d1 = *(struct dumpdates **)a1;
    555 	struct dumpdates *d2 = *(struct dumpdates **)a2;
    556 	int diff;
    557 
    558 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
    559 	if (diff == 0)
    560 		return (d2->dd_ddate - d1->dd_ddate);
    561 	return (diff);
    562 }
    563