xref: /dragonfly/sbin/dump/optr.c (revision 984263bc)
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 #if 0
36 static char sccsid[] = "@(#)optr.c	8.2 (Berkeley) 1/6/94";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/sbin/dump/optr.c,v 1.9.2.5 2002/02/23 22:32:51 iedowse Exp $";
40 #endif /* not lint */
41 
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/wait.h>
45 #include <sys/time.h>
46 
47 #include <errno.h>
48 #include <fstab.h>
49 #include <grp.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <stdarg.h>
54 #include <signal.h>
55 #include <unistd.h>
56 #include <utmp.h>
57 
58 #include "dump.h"
59 #include "pathnames.h"
60 
61 void	alarmcatch __P((/* int, int */));
62 int	datesort __P((const void *, const void *));
63 
64 /*
65  *	Query the operator; This previously-fascist piece of code
66  *	no longer requires an exact response.
67  *	It is intended to protect dump aborting by inquisitive
68  *	people banging on the console terminal to see what is
69  *	happening which might cause dump to croak, destroying
70  *	a large number of hours of work.
71  *
72  *	Every 2 minutes we reprint the message, alerting others
73  *	that dump needs attention.
74  */
75 static	int timeout;
76 static	char *attnmessage;		/* attention message */
77 
78 int
79 query(question)
80 	char	*question;
81 {
82 	char	replybuffer[64];
83 	int	back, errcount;
84 	FILE	*mytty;
85 
86 	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
87 		quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
88 	attnmessage = question;
89 	timeout = 0;
90 	alarmcatch();
91 	back = -1;
92 	errcount = 0;
93 	do {
94 		if (fgets(replybuffer, 63, mytty) == NULL) {
95 			clearerr(mytty);
96 			if (++errcount > 30)	/* XXX	ugly */
97 				quit("excessive operator query failures\n");
98 		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
99 			back = 1;
100 		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
101 			back = 0;
102 		} else {
103 			(void) fprintf(stderr,
104 			    "  DUMP: \"Yes\" or \"No\"?\n");
105 			(void) fprintf(stderr,
106 			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
107 		}
108 	} while (back < 0);
109 
110 	/*
111 	 *	Turn off the alarm, and reset the signal to trap out..
112 	 */
113 	(void) alarm(0);
114 	if (signal(SIGALRM, sig) == SIG_IGN)
115 		signal(SIGALRM, SIG_IGN);
116 	(void) fclose(mytty);
117 	return(back);
118 }
119 
120 char lastmsg[BUFSIZ];
121 
122 /*
123  *	Alert the console operator, and enable the alarm clock to
124  *	sleep for 2 minutes in case nobody comes to satisfy dump
125  */
126 void
127 alarmcatch()
128 {
129 	if (notify == 0) {
130 		if (timeout == 0)
131 			(void) fprintf(stderr,
132 			    "  DUMP: %s: (\"yes\" or \"no\") ",
133 			    attnmessage);
134 		else
135 			msgtail("\a\a");
136 	} else {
137 		if (timeout) {
138 			msgtail("\n");
139 			broadcast("");		/* just print last msg */
140 		}
141 		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
142 		    attnmessage);
143 	}
144 	signal(SIGALRM, alarmcatch);
145 	(void) alarm(120);
146 	timeout = 1;
147 }
148 
149 /*
150  *	Here if an inquisitive operator interrupts the dump program
151  */
152 void
153 interrupt(signo)
154 	int signo;
155 {
156 	msg("Interrupt received.\n");
157 	if (query("Do you want to abort dump?"))
158 		dumpabort(0);
159 }
160 
161 /*
162  *	We now use wall(1) to do the actual broadcasting.
163  */
164 void
165 broadcast(message)
166 	char	*message;
167 {
168 	FILE *fp;
169 	char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
170 
171 	if (!notify)
172 		return;
173 
174 	snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
175 	if ((fp = popen(buf, "w")) == NULL)
176 		return;
177 
178 	(void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
179 	if (lastmsg[0])
180 		(void) fputs(lastmsg, fp);
181 	if (message[0])
182 		(void) fputs(message, fp);
183 
184 	(void) pclose(fp);
185 }
186 
187 /*
188  *	Print out an estimate of the amount of time left to do the dump
189  */
190 
191 time_t	tschedule = 0;
192 
193 void
194 timeest()
195 {
196 	double percent;
197 	time_t	tnow;
198 	int deltat, hours, mins;
199 
200 	(void) time(&tnow);
201 	deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
202 	    (double)(tnow - tstart_writing) / blockswritten * tapesize;
203 	percent = (blockswritten * 100.0) / tapesize;
204 	hours = deltat / 3600;
205 	mins = (deltat % 3600) / 60;
206 
207 	setproctitle("%s: pass %d: %3.2f%% done, finished in %d:%02d",
208 	    disk, passno, percent, hours, mins);
209 	if (tnow >= tschedule) {
210 		tschedule = tnow + 300;
211 		if (blockswritten < 500)
212 			return;
213 		msg("%3.2f%% done, finished in %d:%02d\n", percent, hours,
214 		    mins);
215 	}
216 }
217 
218 /*
219  * Schedule a printout of the estimate in the next call to timeest().
220  */
221 void
222 infosch(signal)
223 	int signal;
224 {
225 	tschedule = 0;
226 }
227 
228 void
229 #if __STDC__
230 msg(const char *fmt, ...)
231 #else
232 msg(fmt, va_alist)
233 	char *fmt;
234 	va_dcl
235 #endif
236 {
237 	va_list ap;
238 
239 	(void) fprintf(stderr,"  DUMP: ");
240 #ifdef TDEBUG
241 	(void) fprintf(stderr, "pid=%d ", getpid());
242 #endif
243 #if __STDC__
244 	va_start(ap, fmt);
245 #else
246 	va_start(ap);
247 #endif
248 	(void) vfprintf(stderr, fmt, ap);
249 	(void) fflush(stdout);
250 	(void) fflush(stderr);
251 	(void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
252 	va_end(ap);
253 }
254 
255 void
256 #if __STDC__
257 msgtail(const char *fmt, ...)
258 #else
259 msgtail(fmt, va_alist)
260 	char *fmt;
261 	va_dcl
262 #endif
263 {
264 	va_list ap;
265 #if __STDC__
266 	va_start(ap, fmt);
267 #else
268 	va_start(ap);
269 #endif
270 	(void) vfprintf(stderr, fmt, ap);
271 	va_end(ap);
272 }
273 
274 void
275 #if __STDC__
276 quit(const char *fmt, ...)
277 #else
278 quit(fmt, va_alist)
279 	char *fmt;
280 	va_dcl
281 #endif
282 {
283 	va_list ap;
284 
285 	(void) fprintf(stderr,"  DUMP: ");
286 #ifdef TDEBUG
287 	(void) fprintf(stderr, "pid=%d ", getpid());
288 #endif
289 #if __STDC__
290 	va_start(ap, fmt);
291 #else
292 	va_start(ap);
293 #endif
294 	(void) vfprintf(stderr, fmt, ap);
295 	va_end(ap);
296 	(void) fflush(stdout);
297 	(void) fflush(stderr);
298 	dumpabort(0);
299 }
300 
301 /*
302  *	Tell the operator what has to be done;
303  *	we don't actually do it
304  */
305 
306 struct fstab *
307 allocfsent(fs)
308 	register struct fstab *fs;
309 {
310 	register struct fstab *new;
311 
312 	new = (struct fstab *)malloc(sizeof (*fs));
313 	if (new == NULL ||
314 	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
315 	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
316 	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
317 		quit("%s\n", strerror(errno));
318 	new->fs_passno = fs->fs_passno;
319 	new->fs_freq = fs->fs_freq;
320 	return (new);
321 }
322 
323 struct	pfstab {
324 	SLIST_ENTRY(pfstab) pf_list;
325 	struct	fstab *pf_fstab;
326 };
327 
328 static	SLIST_HEAD(, pfstab) table;
329 
330 void
331 getfstab()
332 {
333 	register struct fstab *fs;
334 	register struct pfstab *pf;
335 
336 	if (setfsent() == 0) {
337 		msg("Can't open %s for dump table information: %s\n",
338 		    _PATH_FSTAB, strerror(errno));
339 		return;
340 	}
341 	while ((fs = getfsent()) != NULL) {
342 		if (strcmp(fs->fs_type, FSTAB_RW) &&
343 		    strcmp(fs->fs_type, FSTAB_RO) &&
344 		    strcmp(fs->fs_type, FSTAB_RQ))
345 			continue;
346 		fs = allocfsent(fs);
347 		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
348 			quit("%s\n", strerror(errno));
349 		pf->pf_fstab = fs;
350 		SLIST_INSERT_HEAD(&table, pf, pf_list);
351 	}
352 	(void) endfsent();
353 }
354 
355 /*
356  * Search in the fstab for a file name.
357  * This file name can be either the special or the path file name.
358  *
359  * The file name can omit the leading '/'.
360  */
361 struct fstab *
362 fstabsearch(key)
363 	char *key;
364 {
365 	register struct pfstab *pf;
366 	register struct fstab *fs;
367 	char *rn;
368 
369 	SLIST_FOREACH(pf, &table, pf_list) {
370 		fs = pf->pf_fstab;
371 		if (strcmp(fs->fs_file, key) == 0 ||
372 		    strcmp(fs->fs_spec, key) == 0)
373 			return (fs);
374 		rn = rawname(fs->fs_spec);
375 		if (rn != NULL && strcmp(rn, key) == 0)
376 			return (fs);
377 		if (key[0] != '/') {
378 			if (*fs->fs_spec == '/' &&
379 			    strcmp(fs->fs_spec + 1, key) == 0)
380 				return (fs);
381 			if (*fs->fs_file == '/' &&
382 			    strcmp(fs->fs_file + 1, key) == 0)
383 				return (fs);
384 		}
385 	}
386 	return (NULL);
387 }
388 
389 /*
390  *	Tell the operator what to do
391  */
392 void
393 lastdump(arg)
394 	char	arg;	/* w ==> just what to do; W ==> most recent dumps */
395 {
396 	register int i;
397 	register struct fstab *dt;
398 	register struct dumpdates *dtwalk;
399 	char *lastname, *date;
400 	int dumpme;
401 	time_t tnow;
402 	struct tm *tlast;
403 
404 	(void) time(&tnow);
405 	getfstab();		/* /etc/fstab input */
406 	initdumptimes();	/* /etc/dumpdates input */
407 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
408 
409 	if (arg == 'w')
410 		(void) printf("Dump these file systems:\n");
411 	else
412 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
413 	lastname = "??";
414 	ITITERATE(i, dtwalk) {
415 		if (strncmp(lastname, dtwalk->dd_name,
416 		    sizeof(dtwalk->dd_name)) == 0)
417 			continue;
418 		date = (char *)ctime(&dtwalk->dd_ddate);
419 		date[16] = '\0';	/* blast away seconds and year */
420 		lastname = dtwalk->dd_name;
421 		dt = fstabsearch(dtwalk->dd_name);
422 		dumpme = (dt != NULL && dt->fs_freq != 0);
423 		if (dumpme) {
424 		    tlast = localtime(&dtwalk->dd_ddate);
425 		    dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
426 				     - (tlast->tm_min * 60) - tlast->tm_sec
427 				     + (dt->fs_freq * 86400));
428 		};
429 		if (arg != 'w' || dumpme)
430 			(void) printf(
431 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
432 			    dumpme && (arg != 'w') ? '>' : ' ',
433 			    dtwalk->dd_name,
434 			    dt ? dt->fs_file : "",
435 			    dtwalk->dd_level,
436 			    date);
437 	}
438 }
439 
440 int
441 datesort(a1, a2)
442 	const void *a1, *a2;
443 {
444 	struct dumpdates *d1 = *(struct dumpdates **)a1;
445 	struct dumpdates *d2 = *(struct dumpdates **)a2;
446 	int diff;
447 
448 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
449 	if (diff == 0)
450 		return (d2->dd_ddate - d1->dd_ddate);
451 	return (diff);
452 }
453