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