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