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