Home | History | Annotate | Line # | Download | only in dump
optr.c revision 1.31
      1 /*	$NetBSD: optr.c,v 1.31 2003/08/07 10:04:14 agc 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)optr.c	8.2 (Berkeley) 1/6/94";
     36 #else
     37 __RCSID("$NetBSD: optr.c,v 1.31 2003/08/07 10:04:14 agc Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/queue.h>
     43 #include <sys/wait.h>
     44 #include <sys/time.h>
     45 #include <sys/ucred.h>
     46 #include <sys/mount.h>
     47 
     48 #include <errno.h>
     49 #include <fstab.h>
     50 #include <grp.h>
     51 #include <signal.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <stdarg.h>
     56 #include <time.h>
     57 #include <tzfile.h>
     58 #include <unistd.h>
     59 
     60 #include <ufs/ufs/dinode.h>
     61 
     62 #include "dump.h"
     63 #include "pathnames.h"
     64 
     65 void	alarmcatch(int);
     66 struct fstab *allocfsent(struct fstab *);
     67 int	datesort(const void *, const void *);
     68 extern  char *time_string;
     69 extern  char default_time_string[];
     70 
     71 static void do_timestamp(time_t, char *);
     72 
     73 /*
     74  *	Query the operator; This previously-fascist piece of code
     75  *	no longer requires an exact response.
     76  *	It is intended to protect dump aborting by inquisitive
     77  *	people banging on the console terminal to see what is
     78  *	happening which might cause dump to croak, destroying
     79  *	a large number of hours of work.
     80  *
     81  *	Every 2 minutes we reprint the message, alerting others
     82  *	that dump needs attention.
     83  */
     84 static	int timeout;
     85 static	char *attnmessage;		/* attention message */
     86 
     87 int
     88 query(char *question)
     89 {
     90 	char	replybuffer[64];
     91 	int	back, errcount;
     92 	FILE	*mytty;
     93 	time_t	firstprompt, when_answered;
     94 
     95 	firstprompt = time((time_t *)0);
     96 
     97 	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
     98 		quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
     99 	attnmessage = question;
    100 	timeout = 0;
    101 	alarmcatch(0);
    102 	back = -1;
    103 	errcount = 0;
    104 	do {
    105 		if (fgets(replybuffer, 63, mytty) == NULL) {
    106 			clearerr(mytty);
    107 			if (++errcount > 30)	/* XXX	ugly */
    108 				quit("excessive operator query failures\n");
    109 		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
    110 			back = 1;
    111 		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
    112 			back = 0;
    113 		} else {
    114 			(void) fprintf(stderr,
    115 			    "  DUMP: \"Yes\" or \"No\"?\n");
    116 			(void) fprintf(stderr,
    117 			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
    118 		}
    119 	} while (back < 0);
    120 
    121 	/*
    122 	 *	Turn off the alarm, and reset the signal to trap out..
    123 	 */
    124 	(void) alarm(0);
    125 	if (signal(SIGALRM, sig) == SIG_IGN)
    126 		signal(SIGALRM, SIG_IGN);
    127 	(void) fclose(mytty);
    128 	when_answered = time((time_t *)0);
    129 	/*
    130 	 * Adjust the base for time estimates to ignore time we spent waiting
    131 	 * for operator input.
    132 	 */
    133 	if (tstart_writing != 0)
    134 	    tstart_writing += (when_answered - firstprompt);
    135 	return(back);
    136 }
    137 
    138 char lastmsg[200];
    139 
    140 /*
    141  *	Alert the console operator, and enable the alarm clock to
    142  *	sleep for 2 minutes in case nobody comes to satisfy dump
    143  */
    144 void
    145 alarmcatch(int dummy)
    146 {
    147 
    148 	if (notify == 0) {
    149 		if (timeout == 0)
    150 			(void) fprintf(stderr,
    151 			    "  DUMP: %s: (\"yes\" or \"no\") ",
    152 			    attnmessage);
    153 		else
    154 			msgtail("\a\a");
    155 	} else {
    156 		if (timeout) {
    157 			msgtail("\n");
    158 			broadcast("");		/* just print last msg */
    159 		}
    160 		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
    161 		    attnmessage);
    162 	}
    163 	signal(SIGALRM, alarmcatch);
    164 	(void) alarm(120);
    165 	timeout = 1;
    166 }
    167 
    168 /*
    169  *	Here if an inquisitive operator interrupts the dump program
    170  */
    171 void
    172 interrupt(int signo)
    173 {
    174 	int errno_save;
    175 
    176 	errno_save = errno;
    177 	msg("Interrupt received.\n");
    178 	if (query("Do you want to abort dump?"))
    179 		dumpabort(0);
    180 	errno = errno_save;
    181 }
    182 
    183 /*
    184  *	Use wall(1) "-g operator" to do the actual broadcasting.
    185  */
    186 void
    187 broadcast(char	*message)
    188 {
    189 	FILE	*fp;
    190 	char	buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
    191 
    192 	if (!notify)
    193 		return;
    194 
    195 	(void)snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
    196 	if ((fp = popen(buf, "w")) == NULL)
    197 		return;
    198 
    199 	(void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
    200 	if (lastmsg[0])
    201 		(void) fputs(lastmsg, fp);
    202 	if (message[0])
    203 		(void) fputs(message, fp);
    204 
    205 	(void) pclose(fp);
    206 }
    207 
    208 /*
    209  *	print out the timestamp string to stderr.
    210  */
    211 #define STAMP_LENGTH 80
    212 
    213 static void
    214 do_timestamp(time_t thistime, char *message)
    215 {
    216 	struct tm tm_time;
    217 	char then[STAMP_LENGTH + 1];
    218 
    219 	(void) localtime_r(&thistime, &tm_time);
    220 	if (strftime(then, STAMP_LENGTH, time_string, &tm_time) == 0) {
    221 		time_string = default_time_string;
    222 		strftime(then, STAMP_LENGTH, time_string, &tm_time);
    223 		fprintf(stderr,
    224 		   "DUMP: ERROR: TIMEFORMAT too long, reverting to default\n");
    225 	}
    226 
    227 	fprintf(stderr, message, then);
    228 }
    229 
    230 
    231 /*
    232  *	print out an estimate of the amount of time left to do the dump
    233  */
    234 
    235 time_t	tschedule = 0;
    236 
    237 void
    238 timeest(void)
    239 {
    240 	time_t	tnow, deltat;
    241 
    242 	(void) time((time_t *) &tnow);
    243 	if (tnow >= tschedule) {
    244 		tschedule = tnow + 300;
    245 		if (blockswritten < 500)
    246 			return;
    247 		deltat = tstart_writing - tnow +
    248 			(1.0 * (tnow - tstart_writing))
    249 			/ blockswritten * tapesize;
    250 
    251 		msg("%3.2f%% done, finished in %ld:%02ld",
    252 		    (blockswritten * 100.0) / tapesize,
    253 		    (long)(deltat / 3600), (long)((deltat % 3600) / 60));
    254 
    255 		if (timestamp == 1)
    256 			do_timestamp(tnow + deltat, " (at %s)");
    257 
    258 		fprintf(stderr, "\n");
    259 	}
    260 }
    261 
    262 void
    263 msg(const char *fmt, ...)
    264 {
    265 	time_t  tnow;
    266 	va_list ap;
    267 
    268 	fprintf(stderr, "  ");
    269 	if (timestamp == 1) {
    270 		(void) time((time_t *) &tnow);
    271 		do_timestamp(tnow, "[%s] ");
    272 	}
    273 
    274 	(void) fprintf(stderr,"DUMP: ");
    275 #ifdef TDEBUG
    276 	(void) fprintf(stderr, "pid=%d ", getpid());
    277 #endif
    278 	va_start(ap, fmt);
    279 	(void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap);
    280 	fputs(lastmsg, stderr);
    281 	(void) fflush(stdout);
    282 	(void) fflush(stderr);
    283 	va_end(ap);
    284 }
    285 
    286 void
    287 msgtail(const char *fmt, ...)
    288 {
    289 	va_list ap;
    290 
    291 	va_start(ap, fmt);
    292 	(void) vfprintf(stderr, fmt, ap);
    293 	va_end(ap);
    294 }
    295 
    296 void
    297 quit(const char *fmt, ...)
    298 {
    299 	va_list ap;
    300 
    301 	(void) fprintf(stderr,"  DUMP: ");
    302 #ifdef TDEBUG
    303 	(void) fprintf(stderr, "pid=%d ", getpid());
    304 #endif
    305 	va_start(ap, fmt);
    306 	(void) vfprintf(stderr, fmt, ap);
    307 	va_end(ap);
    308 	(void) fflush(stdout);
    309 	(void) fflush(stderr);
    310 	dumpabort(0);
    311 }
    312 
    313 /*
    314  *	Tell the operator what has to be done;
    315  *	we don't actually do it
    316  */
    317 
    318 struct fstab *
    319 allocfsent(struct fstab *fs)
    320 {
    321 	struct fstab *new;
    322 
    323 	new = (struct fstab *)xmalloc(sizeof (*fs));
    324 	new->fs_file = xstrdup(fs->fs_file);
    325 	new->fs_type = xstrdup(fs->fs_type);
    326 	new->fs_spec = xstrdup(fs->fs_spec);
    327 	new->fs_passno = fs->fs_passno;
    328 	new->fs_freq = fs->fs_freq;
    329 	return (new);
    330 }
    331 
    332 struct	pfstab {
    333 	SLIST_ENTRY(pfstab) pf_list;
    334 	struct	fstab *pf_fstab;
    335 };
    336 
    337 static	SLIST_HEAD(, pfstab) table;
    338 
    339 void
    340 getfstab(void)
    341 {
    342 	struct fstab *fs;
    343 	struct pfstab *pf;
    344 
    345 	if (setfsent() == 0) {
    346 		msg("Can't open %s for dump table information: %s\n",
    347 		    _PATH_FSTAB, strerror(errno));
    348 		return;
    349 	}
    350 	while ((fs = getfsent()) != NULL) {
    351 		if (strcmp(fs->fs_type, FSTAB_RW) &&
    352 		    strcmp(fs->fs_type, FSTAB_RO) &&
    353 		    strcmp(fs->fs_type, FSTAB_RQ))
    354 			continue;
    355 #ifdef DUMP_LFS
    356 		if (strcmp(fs->fs_vfstype, "lfs"))
    357 			continue;
    358 #else
    359 		if (strcmp(fs->fs_vfstype, "ufs") &&
    360 		    strcmp(fs->fs_vfstype, "ffs"))
    361 			continue;
    362 #endif
    363 		fs = allocfsent(fs);
    364 		pf = (struct pfstab *)xmalloc(sizeof (*pf));
    365 		pf->pf_fstab = fs;
    366 		SLIST_INSERT_HEAD(&table, pf, pf_list);
    367 	}
    368 	(void) endfsent();
    369 }
    370 
    371 /*
    372  * Search in the fstab for a file name.
    373  * This file name can be either the special or the path file name.
    374  *
    375  * The entries in the fstab are the BLOCK special names, not the
    376  * character special names.
    377  * The caller of fstabsearch assures that the character device
    378  * is dumped (that is much faster)
    379  *
    380  * The file name can omit the leading '/'.
    381  */
    382 struct fstab *
    383 fstabsearch(const char *key)
    384 {
    385 	struct pfstab *pf;
    386 	struct fstab *fs;
    387 	char *rn;
    388 
    389 	SLIST_FOREACH(pf, &table, pf_list) {
    390 		fs = pf->pf_fstab;
    391 		if (strcmp(fs->fs_file, key) == 0 ||
    392 		    strcmp(fs->fs_spec, key) == 0)
    393 			return (fs);
    394 		rn = rawname(fs->fs_spec);
    395 		if (rn != NULL && strcmp(rn, key) == 0)
    396 			return (fs);
    397 		if (key[0] != '/') {
    398 			if (*fs->fs_spec == '/' &&
    399 			    strcmp(fs->fs_spec + 1, key) == 0)
    400 				return (fs);
    401 			if (*fs->fs_file == '/' &&
    402 			    strcmp(fs->fs_file + 1, key) == 0)
    403 				return (fs);
    404 		}
    405 	}
    406 	return (NULL);
    407 }
    408 
    409 /*
    410  * Search in the mounted file list for a file name.
    411  * This file name can be either the special or the path file name.
    412  *
    413  * The entries in the list are the BLOCK special names, not the
    414  * character special names.
    415  * The caller of mntinfosearch assures that the character device
    416  * is dumped (that is much faster)
    417  */
    418 struct statfs *
    419 mntinfosearch(const char *key)
    420 {
    421 	int i, mntbufc;
    422 	struct statfs *mntbuf, *fs;
    423 	char *rn;
    424 
    425 	if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    426 		quit("Can't get mount list: %s", strerror(errno));
    427 	for (fs = mntbuf, i = 0; i < mntbufc; i++, fs++) {
    428 		if (strcmp(fs->f_fstypename, "ufs") != 0 &&
    429 		    strcmp(fs->f_fstypename, "ffs") != 0)
    430 			continue;
    431 		if (strcmp(fs->f_mntonname, key) == 0 ||
    432 		    strcmp(fs->f_mntfromname, key) == 0)
    433 			return (fs);
    434 		rn = rawname(fs->f_mntfromname);
    435 		if (rn != NULL && strcmp(rn, key) == 0)
    436 			return (fs);
    437 	}
    438 	return (NULL);
    439 }
    440 
    441 
    442 /*
    443  *	Tell the operator what to do.
    444  *	arg:	w ==> just what to do; W ==> most recent dumps
    445  */
    446 void
    447 lastdump(char arg)
    448 {
    449 	int i;
    450 	struct fstab *dt;
    451 	struct dumpdates *dtwalk;
    452 	char *lastname, *date;
    453 	int dumpme;
    454 	time_t tnow;
    455 
    456 	(void) time(&tnow);
    457 	getfstab();		/* /etc/fstab input */
    458 	initdumptimes();	/* /etc/dumpdates input */
    459 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
    460 
    461 	if (arg == 'w')
    462 		(void) printf("Dump these file systems:\n");
    463 	else
    464 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
    465 	lastname = "??";
    466 	ITITERATE(i, dtwalk) {
    467 		if (strncmp(lastname, dtwalk->dd_name,
    468 		    sizeof(dtwalk->dd_name)) == 0)
    469 			continue;
    470 		date = (char *)ctime(&dtwalk->dd_ddate);
    471 		date[24] = '\0';
    472 		strcpy(date + 16, date + 19);	/* blast away seconds */
    473 		lastname = dtwalk->dd_name;
    474 		dt = fstabsearch(dtwalk->dd_name);
    475 		dumpme = (dt != NULL &&
    476 		    dt->fs_freq != 0 &&
    477 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
    478 		if (arg != 'w' || dumpme)
    479 			(void) printf(
    480 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
    481 			    dumpme && (arg != 'w') ? '>' : ' ',
    482 			    dtwalk->dd_name,
    483 			    dt ? dt->fs_file : "",
    484 			    dtwalk->dd_level,
    485 			    date);
    486 	}
    487 }
    488 
    489 int
    490 datesort(const void *a1, const void *a2)
    491 {
    492 	struct dumpdates *d1 = *(struct dumpdates **)a1;
    493 	struct dumpdates *d2 = *(struct dumpdates **)a2;
    494 	int diff;
    495 
    496 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
    497 	if (diff == 0)
    498 		return (d2->dd_ddate - d1->dd_ddate);
    499 	return (diff);
    500 }
    501