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