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