Home | History | Annotate | Line # | Download | only in dump
optr.c revision 1.13.10.3
      1 /*	$NetBSD: optr.c,v 1.13.10.3 2002/01/16 09:57:08 he 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.3 2002/01/16 09:57:08 he Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/queue.h>
     47 #include <sys/wait.h>
     48 #include <sys/time.h>
     49 
     50 #include <errno.h>
     51 #include <fstab.h>
     52 #include <grp.h>
     53 #include <signal.h>
     54 #include <stdio.h>
     55 #ifdef __STDC__
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <stdarg.h>
     59 #endif
     60 #include <time.h>
     61 #include <tzfile.h>
     62 #ifdef __STDC__
     63 #include <unistd.h>
     64 #endif
     65 #include <utmp.h>
     66 #ifndef __STDC__
     67 #include <varargs.h>
     68 #endif
     69 
     70 #include "dump.h"
     71 #include "pathnames.h"
     72 
     73 void	alarmcatch __P((int));
     74 struct fstab *allocfsent __P((struct fstab *fs));
     75 int	datesort __P((const void *, const void *));
     76 static	void sendmes __P((char *, char *));
     77 extern  gid_t egid;
     78 
     79 /*
     80  *	Query the operator; This previously-fascist piece of code
     81  *	no longer requires an exact response.
     82  *	It is intended to protect dump aborting by inquisitive
     83  *	people banging on the console terminal to see what is
     84  *	happening which might cause dump to croak, destroying
     85  *	a large number of hours of work.
     86  *
     87  *	Every 2 minutes we reprint the message, alerting others
     88  *	that dump needs attention.
     89  */
     90 static	int timeout;
     91 static	char *attnmessage;		/* attention message */
     92 
     93 int
     94 query(question)
     95 	char	*question;
     96 {
     97 	char	replybuffer[64];
     98 	int	back, errcount;
     99 	FILE	*mytty;
    100 	time_t	firstprompt, when_answered;
    101 
    102 	firstprompt = time((time_t *)0);
    103 
    104 	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
    105 		quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
    106 	attnmessage = question;
    107 	timeout = 0;
    108 	alarmcatch(0);
    109 	back = -1;
    110 	errcount = 0;
    111 	do {
    112 		if (fgets(replybuffer, 63, mytty) == NULL) {
    113 			clearerr(mytty);
    114 			if (++errcount > 30)	/* XXX	ugly */
    115 				quit("excessive operator query failures\n");
    116 		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
    117 			back = 1;
    118 		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
    119 			back = 0;
    120 		} else {
    121 			(void) fprintf(stderr,
    122 			    "  DUMP: \"Yes\" or \"No\"?\n");
    123 			(void) fprintf(stderr,
    124 			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
    125 		}
    126 	} while (back < 0);
    127 
    128 	/*
    129 	 *	Turn off the alarm, and reset the signal to trap out..
    130 	 */
    131 	(void) alarm(0);
    132 	if (signal(SIGALRM, sig) == SIG_IGN)
    133 		signal(SIGALRM, SIG_IGN);
    134 	(void) fclose(mytty);
    135 	when_answered = time((time_t *)0);
    136 	/*
    137 	 * Adjust the base for time estimates to ignore time we spent waiting
    138 	 * for operator input.
    139 	 */
    140 	if (tstart_writing != 0)
    141 	    tstart_writing += (when_answered - firstprompt);
    142 	return(back);
    143 }
    144 
    145 char lastmsg[100];
    146 
    147 /*
    148  *	Alert the console operator, and enable the alarm clock to
    149  *	sleep for 2 minutes in case nobody comes to satisfy dump
    150  */
    151 void
    152 alarmcatch(dummy)
    153 	int dummy;
    154 {
    155 	if (notify == 0) {
    156 		if (timeout == 0)
    157 			(void) fprintf(stderr,
    158 			    "  DUMP: %s: (\"yes\" or \"no\") ",
    159 			    attnmessage);
    160 		else
    161 			msgtail("\a\a");
    162 	} else {
    163 		if (timeout) {
    164 			msgtail("\n");
    165 			broadcast("");		/* just print last msg */
    166 		}
    167 		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
    168 		    attnmessage);
    169 	}
    170 	signal(SIGALRM, alarmcatch);
    171 	(void) alarm(120);
    172 	timeout = 1;
    173 }
    174 
    175 /*
    176  *	Here if an inquisitive operator interrupts the dump program
    177  */
    178 void
    179 interrupt(signo)
    180 	int signo;
    181 {
    182 	msg("Interrupt received.\n");
    183 	if (query("Do you want to abort dump?"))
    184 		dumpabort(0);
    185 }
    186 
    187 /*
    188  *	The following variables and routines manage alerting
    189  *	operators to the status of dump.
    190  *	This works much like wall(1) does.
    191  */
    192 struct	group *gp;
    193 
    194 /*
    195  *	Get the names from the group entry "operator" to notify.
    196  */
    197 void
    198 set_operators()
    199 {
    200 	if (!notify)		/*not going to notify*/
    201 		return;
    202 	gp = getgrnam(OPGRENT);
    203 	(void) endgrent();
    204 	if (gp == NULL) {
    205 		msg("No group entry for %s.\n", OPGRENT);
    206 		notify = 0;
    207 		return;
    208 	}
    209 }
    210 
    211 struct tm *localclock;
    212 
    213 /*
    214  *	We fork a child to do the actual broadcasting, so
    215  *	that the process control groups are not messed up
    216  */
    217 void
    218 broadcast(message)
    219 	char	*message;
    220 {
    221 	time_t		clock;
    222 	FILE	*f_utmp;
    223 	struct	utmp	utmp;
    224 	char	**np;
    225 	int	pid, s;
    226 
    227 	if (!notify || gp == NULL)
    228 		return;
    229 
    230 	/* Restore 'tty' privs for the child's use only. */
    231 	setegid(egid);
    232 	switch (pid = fork()) {
    233 	case -1:
    234 		setegid(getgid());
    235 		return;
    236 	case 0:
    237 		break;
    238 	default:
    239 		setegid(getgid());
    240 		while (wait(&s) != pid)
    241 			continue;
    242 		return;
    243 	}
    244 
    245 	clock = time((time_t *)0);
    246 	localclock = localtime(&clock);
    247 
    248 	if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
    249 		msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
    250 		return;
    251 	}
    252 
    253 	while (!feof(f_utmp)) {
    254 		if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
    255 			break;
    256 		if (utmp.ut_name[0] == 0)
    257 			continue;
    258 		for (np = gp->gr_mem; *np; np++) {
    259 			if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
    260 				continue;
    261 			/*
    262 			 *	Do not send messages to operators on dialups
    263 			 */
    264 			if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
    265 				continue;
    266 #ifdef DEBUG
    267 			msg("Message to %s at %s\n", *np, utmp.ut_line);
    268 #endif
    269 			sendmes(utmp.ut_line, message);
    270 		}
    271 	}
    272 	(void) fclose(f_utmp);
    273 	Exit(0);	/* the wait in this same routine will catch this */
    274 	/* NOTREACHED */
    275 }
    276 
    277 static void
    278 sendmes(tty, message)
    279 	char *tty, *message;
    280 {
    281 	char t[50], buf[BUFSIZ];
    282 	char *cp;
    283 	int lmsg = 1;
    284 	FILE *f_tty;
    285 
    286 	(void)strncpy(t, _PATH_DEV, sizeof(t) - 1);
    287 	(void)strncat(t, tty, sizeof(t) - sizeof(_PATH_DEV) - 1);
    288 	t[sizeof(t) - 1] = '\0';
    289 
    290 	if ((f_tty = fopen(t, "w")) != NULL) {
    291 		setbuf(f_tty, buf);
    292 		(void) fprintf(f_tty,
    293 		    "\n\
    294 \a\a\aMessage from the dump program to all operators at %d:%02d ...\r\n\n\
    295 DUMP: NEEDS ATTENTION: ",
    296 		    localclock->tm_hour, localclock->tm_min);
    297 		for (cp = lastmsg; ; cp++) {
    298 			if (*cp == '\0') {
    299 				if (lmsg) {
    300 					cp = message;
    301 					if (*cp == '\0')
    302 						break;
    303 					lmsg = 0;
    304 				} else
    305 					break;
    306 			}
    307 			if (*cp == '\n')
    308 				(void) putc('\r', f_tty);
    309 			(void) putc(*cp, f_tty);
    310 		}
    311 		(void) fclose(f_tty);
    312 	}
    313 }
    314 
    315 /*
    316  *	print out an estimate of the amount of time left to do the dump
    317  */
    318 
    319 time_t	tschedule = 0;
    320 
    321 void
    322 timeest()
    323 {
    324 	time_t	tnow, deltat;
    325 
    326 	(void) time((time_t *) &tnow);
    327 	if (tnow >= tschedule) {
    328 		tschedule = tnow + 300;
    329 		if (blockswritten < 500)
    330 			return;
    331 		deltat = tstart_writing - tnow +
    332 			(1.0 * (tnow - tstart_writing))
    333 			/ blockswritten * tapesize;
    334 		msg("%3.2f%% done, finished in %ld:%02ld\n",
    335 		    (blockswritten * 100.0) / tapesize,
    336 		    (long)(deltat / 3600), (long)((deltat % 3600) / 60));
    337 	}
    338 }
    339 
    340 void
    341 #if __STDC__
    342 msg(const char *fmt, ...)
    343 #else
    344 msg(fmt, va_alist)
    345 	char *fmt;
    346 	va_dcl
    347 #endif
    348 {
    349 	va_list ap;
    350 
    351 	(void) fprintf(stderr,"  DUMP: ");
    352 #ifdef TDEBUG
    353 	(void) fprintf(stderr, "pid=%d ", getpid());
    354 #endif
    355 #if __STDC__
    356 	va_start(ap, fmt);
    357 #else
    358 	va_start(ap);
    359 #endif
    360 	(void) vfprintf(stderr, fmt, ap);
    361 	(void) fflush(stdout);
    362 	(void) fflush(stderr);
    363 	(void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap);
    364 	va_end(ap);
    365 }
    366 
    367 void
    368 #if __STDC__
    369 msgtail(const char *fmt, ...)
    370 #else
    371 msgtail(fmt, va_alist)
    372 	char *fmt;
    373 	va_dcl
    374 #endif
    375 {
    376 	va_list ap;
    377 #if __STDC__
    378 	va_start(ap, fmt);
    379 #else
    380 	va_start(ap);
    381 #endif
    382 	(void) vfprintf(stderr, fmt, ap);
    383 	va_end(ap);
    384 }
    385 
    386 void
    387 #if __STDC__
    388 quit(const char *fmt, ...)
    389 #else
    390 quit(fmt, va_alist)
    391 	char *fmt;
    392 	va_dcl
    393 #endif
    394 {
    395 	va_list ap;
    396 
    397 	(void) fprintf(stderr,"  DUMP: ");
    398 #ifdef TDEBUG
    399 	(void) fprintf(stderr, "pid=%d ", getpid());
    400 #endif
    401 #if __STDC__
    402 	va_start(ap, fmt);
    403 #else
    404 	va_start(ap);
    405 #endif
    406 	(void) vfprintf(stderr, fmt, ap);
    407 	va_end(ap);
    408 	(void) fflush(stdout);
    409 	(void) fflush(stderr);
    410 	dumpabort(0);
    411 }
    412 
    413 /*
    414  *	Tell the operator what has to be done;
    415  *	we don't actually do it
    416  */
    417 
    418 struct fstab *
    419 allocfsent(fs)
    420 	struct fstab *fs;
    421 {
    422 	struct fstab *new;
    423 
    424 	new = (struct fstab *)malloc(sizeof (*fs));
    425 	if (new == NULL ||
    426 	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
    427 	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
    428 	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
    429 		quit("%s\n", strerror(errno));
    430 	new->fs_passno = fs->fs_passno;
    431 	new->fs_freq = fs->fs_freq;
    432 	return (new);
    433 }
    434 
    435 struct	pfstab {
    436 	SLIST_ENTRY(pfstab) pf_list;
    437 	struct	fstab *pf_fstab;
    438 };
    439 
    440 static	SLIST_HEAD(, pfstab) table;
    441 
    442 void
    443 getfstab()
    444 {
    445 	struct fstab *fs;
    446 	struct pfstab *pf;
    447 
    448 	if (setfsent() == 0) {
    449 		msg("Can't open %s for dump table information: %s\n",
    450 		    _PATH_FSTAB, strerror(errno));
    451 		return;
    452 	}
    453 	while ((fs = getfsent()) != NULL) {
    454 		if (strcmp(fs->fs_type, FSTAB_RW) &&
    455 		    strcmp(fs->fs_type, FSTAB_RO) &&
    456 		    strcmp(fs->fs_type, FSTAB_RQ))
    457 			continue;
    458 		if (strcmp(fs->fs_vfstype, "ufs") &&
    459 		    strcmp(fs->fs_vfstype, "ffs"))
    460 			continue;
    461 		fs = allocfsent(fs);
    462 		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
    463 			quit("%s\n", strerror(errno));
    464 		pf->pf_fstab = fs;
    465 		SLIST_INSERT_HEAD(&table, pf, pf_list);
    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 	SLIST_FOREACH(pf, &table, pf_list) {
    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