Home | History | Annotate | Line # | Download | only in dump
optr.c revision 1.28
      1 /*	$NetBSD: optr.c,v 1.28 2002/08/18 08:03:35 yamt 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.28 2002/08/18 08:03:35 yamt 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 #include <sys/ucred.h>
     50 #include <sys/mount.h>
     51 
     52 #include <errno.h>
     53 #include <fstab.h>
     54 #include <grp.h>
     55 #include <signal.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <stdarg.h>
     60 #include <time.h>
     61 #include <tzfile.h>
     62 #include <unistd.h>
     63 
     64 #include "dump.h"
     65 #include "pathnames.h"
     66 
     67 #include "utmpentry.h"
     68 
     69 void	alarmcatch(int);
     70 struct fstab *allocfsent(struct fstab *);
     71 int	datesort(const void *, const void *);
     72 static	void sendmes(char *, char *);
     73 extern  gid_t egid;
     74 extern  char *time_string;
     75 extern  char default_time_string[];
     76 
     77 static void do_timestamp(time_t, char *);
     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(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[200];
    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(int dummy)
    152 {
    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("\a\a");
    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(int signo)
    179 {
    180 	int errno_save;
    181 
    182 	errno_save = errno;
    183 	msg("Interrupt received.\n");
    184 	if (query("Do you want to abort dump?"))
    185 		dumpabort(0);
    186 	errno = errno_save;
    187 }
    188 
    189 /*
    190  *	The following variables and routines manage alerting
    191  *	operators to the status of dump.
    192  *	This works much like wall(1) does.
    193  */
    194 struct	group *gp;
    195 
    196 /*
    197  *	Get the names from the group entry "operator" to notify.
    198  */
    199 void
    200 set_operators(void)
    201 {
    202 
    203 	if (!notify)		/*not going to notify*/
    204 		return;
    205 	gp = getgrnam(OPGRENT);
    206 	(void) endgrent();
    207 	if (gp == NULL) {
    208 		msg("No group entry for %s.\n", OPGRENT);
    209 		notify = 0;
    210 		return;
    211 	}
    212 }
    213 
    214 struct tm *localclock;
    215 
    216 /*
    217  *	We fork a child to do the actual broadcasting, so
    218  *	that the process control groups are not messed up
    219  */
    220 void
    221 broadcast(char	*message)
    222 {
    223 	time_t	now;
    224 	char  **np;
    225 	int	pid, s;
    226 	struct utmpentry *ep;
    227 
    228 	if (!notify || gp == NULL)
    229 		return;
    230 
    231 	/* Restore 'tty' privs for the child's use only. */
    232 	setegid(egid);
    233 	switch (pid = fork()) {
    234 	case -1:
    235 		setegid(getgid());
    236 		return;
    237 	case 0:
    238 		break;
    239 	default:
    240 		setegid(getgid());
    241 		while (wait(&s) != pid)
    242 			continue;
    243 		return;
    244 	}
    245 
    246 	now = time((time_t *)0);
    247 	localclock = localtime(&now);
    248 
    249 	(void)getutentries(NULL, &ep);
    250 
    251 	for (; ep; ep = ep->next) {
    252 		for (np = gp->gr_mem; *np; np++) {
    253 			if (strcmp(*np, ep->name) != 0)
    254 				continue;
    255 			/*
    256 			 *	Do not send messages to operators on dialups
    257 			 */
    258 			if (strcmp(ep->line, DIALUP) == 0)
    259 				continue;
    260 #ifdef DEBUG
    261 			msg("Message to %s at %s\n", *np, ep->line);
    262 #endif
    263 			sendmes(ep->line, message);
    264 		}
    265 	}
    266 	Exit(0);	/* the wait in this same routine will catch this */
    267 	/* NOTREACHED */
    268 }
    269 
    270 static void
    271 sendmes(char *tty, char *message)
    272 {
    273 	char t[50], buf[BUFSIZ];
    274 	char *cp;
    275 	int lmsg = 1;
    276 	FILE *f_tty;
    277 
    278 	if (strcspn(tty, "./") != strlen(tty))
    279 		return;
    280 
    281 	(void)strncpy(t, _PATH_DEV, sizeof(t) - 1);
    282 	(void)strncat(t, tty, sizeof(t) - sizeof(_PATH_DEV) - 1);
    283 	t[sizeof(t) - 1] = '\0';
    284 
    285 	if ((f_tty = fopen(t, "w")) != NULL) {
    286 		if (!isatty(fileno(f_tty)))
    287 			return;
    288 		setbuf(f_tty, buf);
    289 		(void) fprintf(f_tty,
    290 		    "\n\
    291 \a\a\aMessage from the dump program to all operators at %d:%02d ...\r\n\n\
    292 DUMP: NEEDS ATTENTION: ",
    293 		    localclock->tm_hour, localclock->tm_min);
    294 		for (cp = lastmsg; ; cp++) {
    295 			if (*cp == '\0') {
    296 				if (lmsg) {
    297 					cp = message;
    298 					if (*cp == '\0')
    299 						break;
    300 					lmsg = 0;
    301 				} else
    302 					break;
    303 			}
    304 			if (*cp == '\n')
    305 				(void) putc('\r', f_tty);
    306 			(void) putc(*cp, f_tty);
    307 		}
    308 		(void) fclose(f_tty);
    309 	}
    310 }
    311 
    312 /*
    313  *	print out the timestamp string to stderr.
    314  */
    315 #define STAMP_LENGTH 80
    316 
    317 static void
    318 do_timestamp(time_t thistime, char *message)
    319 {
    320 	struct tm tm_time;
    321 	char then[STAMP_LENGTH + 1];
    322 
    323 	(void) localtime_r(&thistime, &tm_time);
    324 	if (strftime(then, STAMP_LENGTH, time_string, &tm_time) == 0) {
    325 		time_string = default_time_string;
    326 		strftime(then, STAMP_LENGTH, time_string, &tm_time);
    327 		fprintf(stderr,
    328 		   "DUMP: ERROR: TIMEFORMAT too long, reverting to default\n");
    329 	}
    330 
    331 	fprintf(stderr, message, then);
    332 }
    333 
    334 
    335 /*
    336  *	print out an estimate of the amount of time left to do the dump
    337  */
    338 
    339 time_t	tschedule = 0;
    340 
    341 void
    342 timeest(void)
    343 {
    344 	time_t	tnow, deltat;
    345 
    346 	(void) time((time_t *) &tnow);
    347 	if (tnow >= tschedule) {
    348 		tschedule = tnow + 300;
    349 		if (blockswritten < 500)
    350 			return;
    351 		deltat = tstart_writing - tnow +
    352 			(1.0 * (tnow - tstart_writing))
    353 			/ blockswritten * tapesize;
    354 
    355 		msg("%3.2f%% done, finished in %ld:%02ld",
    356 		    (blockswritten * 100.0) / tapesize,
    357 		    (long)(deltat / 3600), (long)((deltat % 3600) / 60));
    358 
    359 		if (timestamp == 1)
    360 			do_timestamp(tnow + deltat, " (at %s)");
    361 
    362 		fprintf(stderr, "\n");
    363 	}
    364 }
    365 
    366 void
    367 msg(const char *fmt, ...)
    368 {
    369 	time_t  tnow;
    370 	va_list ap;
    371 
    372 	fprintf(stderr, "  ");
    373 	if (timestamp == 1) {
    374 		(void) time((time_t *) &tnow);
    375 		do_timestamp(tnow, "[%s] ");
    376 	}
    377 
    378 	(void) fprintf(stderr,"DUMP: ");
    379 #ifdef TDEBUG
    380 	(void) fprintf(stderr, "pid=%d ", getpid());
    381 #endif
    382 	va_start(ap, fmt);
    383 	(void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap);
    384 	fputs(lastmsg, stderr);
    385 	(void) fflush(stdout);
    386 	(void) fflush(stderr);
    387 	va_end(ap);
    388 }
    389 
    390 void
    391 msgtail(const char *fmt, ...)
    392 {
    393 	va_list ap;
    394 
    395 	va_start(ap, fmt);
    396 	(void) vfprintf(stderr, fmt, ap);
    397 	va_end(ap);
    398 }
    399 
    400 void
    401 quit(const char *fmt, ...)
    402 {
    403 	va_list ap;
    404 
    405 	(void) fprintf(stderr,"  DUMP: ");
    406 #ifdef TDEBUG
    407 	(void) fprintf(stderr, "pid=%d ", getpid());
    408 #endif
    409 	va_start(ap, fmt);
    410 	(void) vfprintf(stderr, fmt, ap);
    411 	va_end(ap);
    412 	(void) fflush(stdout);
    413 	(void) fflush(stderr);
    414 	dumpabort(0);
    415 }
    416 
    417 /*
    418  *	Tell the operator what has to be done;
    419  *	we don't actually do it
    420  */
    421 
    422 struct fstab *
    423 allocfsent(struct fstab *fs)
    424 {
    425 	struct fstab *new;
    426 
    427 	new = (struct fstab *)xmalloc(sizeof (*fs));
    428 	new->fs_file = xstrdup(fs->fs_file);
    429 	new->fs_type = xstrdup(fs->fs_type);
    430 	new->fs_spec = xstrdup(fs->fs_spec);
    431 	new->fs_passno = fs->fs_passno;
    432 	new->fs_freq = fs->fs_freq;
    433 	return (new);
    434 }
    435 
    436 struct	pfstab {
    437 	SLIST_ENTRY(pfstab) pf_list;
    438 	struct	fstab *pf_fstab;
    439 };
    440 
    441 static	SLIST_HEAD(, pfstab) table;
    442 
    443 void
    444 getfstab(void)
    445 {
    446 	struct fstab *fs;
    447 	struct pfstab *pf;
    448 
    449 	if (setfsent() == 0) {
    450 		msg("Can't open %s for dump table information: %s\n",
    451 		    _PATH_FSTAB, strerror(errno));
    452 		return;
    453 	}
    454 	while ((fs = getfsent()) != NULL) {
    455 		if (strcmp(fs->fs_type, FSTAB_RW) &&
    456 		    strcmp(fs->fs_type, FSTAB_RO) &&
    457 		    strcmp(fs->fs_type, FSTAB_RQ))
    458 			continue;
    459 #ifdef DUMP_LFS
    460 		if (strcmp(fs->fs_vfstype, "lfs"))
    461 			continue;
    462 #else
    463 		if (strcmp(fs->fs_vfstype, "ufs") &&
    464 		    strcmp(fs->fs_vfstype, "ffs"))
    465 			continue;
    466 #endif
    467 		fs = allocfsent(fs);
    468 		pf = (struct pfstab *)xmalloc(sizeof (*pf));
    469 		pf->pf_fstab = fs;
    470 		SLIST_INSERT_HEAD(&table, pf, pf_list);
    471 	}
    472 	(void) endfsent();
    473 }
    474 
    475 /*
    476  * Search in the fstab for a file name.
    477  * This file name can be either the special or the path file name.
    478  *
    479  * The entries in the fstab are the BLOCK special names, not the
    480  * character special names.
    481  * The caller of fstabsearch assures that the character device
    482  * is dumped (that is much faster)
    483  *
    484  * The file name can omit the leading '/'.
    485  */
    486 struct fstab *
    487 fstabsearch(const char *key)
    488 {
    489 	struct pfstab *pf;
    490 	struct fstab *fs;
    491 	char *rn;
    492 
    493 	SLIST_FOREACH(pf, &table, pf_list) {
    494 		fs = pf->pf_fstab;
    495 		if (strcmp(fs->fs_file, key) == 0 ||
    496 		    strcmp(fs->fs_spec, key) == 0)
    497 			return (fs);
    498 		rn = rawname(fs->fs_spec);
    499 		if (rn != NULL && strcmp(rn, key) == 0)
    500 			return (fs);
    501 		if (key[0] != '/') {
    502 			if (*fs->fs_spec == '/' &&
    503 			    strcmp(fs->fs_spec + 1, key) == 0)
    504 				return (fs);
    505 			if (*fs->fs_file == '/' &&
    506 			    strcmp(fs->fs_file + 1, key) == 0)
    507 				return (fs);
    508 		}
    509 	}
    510 	return (NULL);
    511 }
    512 
    513 /*
    514  * Search in the mounted file list for a file name.
    515  * This file name can be either the special or the path file name.
    516  *
    517  * The entries in the list are the BLOCK special names, not the
    518  * character special names.
    519  * The caller of mntinfosearch assures that the character device
    520  * is dumped (that is much faster)
    521  */
    522 struct statfs *
    523 mntinfosearch(const char *key)
    524 {
    525 	int i, mntbufc;
    526 	struct statfs *mntbuf, *fs;
    527 	char *rn;
    528 
    529 	if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    530 		quit("Can't get mount list: %s", strerror(errno));
    531 	for (fs = mntbuf, i = 0; i < mntbufc; i++, fs++) {
    532 		if (strcmp(fs->f_fstypename, "ufs") != 0 &&
    533 		    strcmp(fs->f_fstypename, "ffs") != 0)
    534 			continue;
    535 		if (strcmp(fs->f_mntonname, key) == 0 ||
    536 		    strcmp(fs->f_mntfromname, key) == 0)
    537 			return (fs);
    538 		rn = rawname(fs->f_mntfromname);
    539 		if (rn != NULL && strcmp(rn, key) == 0)
    540 			return (fs);
    541 	}
    542 	return (NULL);
    543 }
    544 
    545 
    546 /*
    547  *	Tell the operator what to do.
    548  *	arg:	w ==> just what to do; W ==> most recent dumps
    549  */
    550 void
    551 lastdump(char arg)
    552 {
    553 	int i;
    554 	struct fstab *dt;
    555 	struct dumpdates *dtwalk;
    556 	char *lastname, *date;
    557 	int dumpme;
    558 	time_t tnow;
    559 
    560 	(void) time(&tnow);
    561 	getfstab();		/* /etc/fstab input */
    562 	initdumptimes();	/* /etc/dumpdates input */
    563 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
    564 
    565 	if (arg == 'w')
    566 		(void) printf("Dump these file systems:\n");
    567 	else
    568 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
    569 	lastname = "??";
    570 	ITITERATE(i, dtwalk) {
    571 		if (strncmp(lastname, dtwalk->dd_name,
    572 		    sizeof(dtwalk->dd_name)) == 0)
    573 			continue;
    574 		date = (char *)ctime(&dtwalk->dd_ddate);
    575 		date[24] = '\0';
    576 		strcpy(date + 16, date + 19);	/* blast away seconds */
    577 		lastname = dtwalk->dd_name;
    578 		dt = fstabsearch(dtwalk->dd_name);
    579 		dumpme = (dt != NULL &&
    580 		    dt->fs_freq != 0 &&
    581 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
    582 		if (arg != 'w' || dumpme)
    583 			(void) printf(
    584 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
    585 			    dumpme && (arg != 'w') ? '>' : ' ',
    586 			    dtwalk->dd_name,
    587 			    dt ? dt->fs_file : "",
    588 			    dtwalk->dd_level,
    589 			    date);
    590 	}
    591 }
    592 
    593 int
    594 datesort(const void *a1, const void *a2)
    595 {
    596 	struct dumpdates *d1 = *(struct dumpdates **)a1;
    597 	struct dumpdates *d2 = *(struct dumpdates **)a2;
    598 	int diff;
    599 
    600 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
    601 	if (diff == 0)
    602 		return (d2->dd_ddate - d1->dd_ddate);
    603 	return (diff);
    604 }
    605