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