xref: /netbsd-src/sbin/dump/optr.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /*	$NetBSD: optr.c,v 1.42 2013/09/08 13:26:05 mlelstv 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.42 2013/09/08 13:26:05 mlelstv 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 #include <util.h>
60 
61 #include "dump.h"
62 #include "pathnames.h"
63 
64 extern  char *time_string;
65 extern  char default_time_string[];
66 
67 static void alarmcatch(int);
68 static struct fstab *allocfsent(const struct fstab *);
69 static int datesort(const void *, const void *);
70 static void do_timestamp(time_t, const char *);
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 static	int timeout;
84 static	const char *attnmessage;		/* attention message */
85 
86 int
87 query(const char *question)
88 {
89 	char	replybuffer[64];
90 	int	back, errcount;
91 	FILE	*mytty;
92 	time_t	firstprompt, when_answered;
93 
94 	firstprompt = time((time_t *)0);
95 
96 	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
97 		quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
98 	attnmessage = question;
99 	timeout = 0;
100 	alarmcatch(0);
101 	back = -1;
102 	errcount = 0;
103 	do {
104 		if (fgets(replybuffer, 63, mytty) == NULL) {
105 			clearerr(mytty);
106 			if (++errcount > 30)	/* XXX	ugly */
107 				quit("excessive operator query failures\n");
108 		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
109 			back = 1;
110 		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
111 			back = 0;
112 		} else {
113 			(void) fprintf(stderr,
114 			    "  DUMP: \"Yes\" or \"No\"?\n");
115 			(void) fprintf(stderr,
116 			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
117 		}
118 	} while (back < 0);
119 
120 	/*
121 	 *	Turn off the alarm, and reset the signal to trap out..
122 	 */
123 	(void) alarm(0);
124 	if (signal(SIGALRM, sig) == SIG_IGN)
125 		signal(SIGALRM, SIG_IGN);
126 	(void) fclose(mytty);
127 	when_answered = time((time_t *)0);
128 	/*
129 	 * Adjust the base for time estimates to ignore time we spent waiting
130 	 * for operator input.
131 	 */
132 	if (tstart_writing != 0)
133 		tstart_writing += (when_answered - firstprompt);
134 	if (tstart_volume != 0)
135 		tstart_volume += (when_answered - firstprompt);
136 	return(back);
137 }
138 
139 char lastmsg[200];
140 
141 /*
142  *	Alert the console operator, and enable the alarm clock to
143  *	sleep for 2 minutes in case nobody comes to satisfy dump
144  */
145 static void
146 alarmcatch(int dummy __unused)
147 {
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("\a\a");
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(int signo __unused)
174 {
175 	int errno_save;
176 
177 	errno_save = errno;
178 	msg("Interrupt received.\n");
179 	if (query("Do you want to abort dump?"))
180 		dumpabort(0);
181 	errno = errno_save;
182 }
183 
184 /*
185  *	Use wall(1) "-g operator" to do the actual broadcasting.
186  */
187 void
188 broadcast(const char *message)
189 {
190 	FILE	*fp;
191 	char	buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
192 
193 	if (!notify)
194 		return;
195 
196 	(void)snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
197 	if ((fp = popen(buf, "w")) == NULL)
198 		return;
199 
200 	(void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
201 	if (lastmsg[0])
202 		(void) fputs(lastmsg, fp);
203 	if (message[0])
204 		(void) fputs(message, fp);
205 
206 	(void) pclose(fp);
207 }
208 
209 /*
210  *	print out the timestamp string to stderr.
211  */
212 #define STAMP_LENGTH 80
213 
214 static void
215 do_timestamp(time_t thistime, const char *message)
216 {
217 	struct tm tm_time;
218 	char then[STAMP_LENGTH + 1];
219 
220 	(void) localtime_r(&thistime, &tm_time);
221 	if (strftime(then, STAMP_LENGTH, time_string, &tm_time) == 0) {
222 		time_string = default_time_string;
223 		strftime(then, STAMP_LENGTH, time_string, &tm_time);
224 		fprintf(stderr,
225 		   "DUMP: ERROR: TIMEFORMAT too long, reverting to default\n");
226 	}
227 
228 	fprintf(stderr, message, then);
229 }
230 
231 
232 /*
233  *	print out an estimate of the amount of time left to do the dump
234  */
235 
236 time_t	tschedule = 0;
237 
238 void
239 timeest(void)
240 {
241 	time_t	tnow, deltat;
242 
243 	(void) time((time_t *) &tnow);
244 	if (tnow >= tschedule) {
245 		tschedule = tnow + 300;
246 		if (blockswritten < 500)
247 			return;
248 		deltat = tstart_writing - tnow +
249 			(1.0 * (tnow - tstart_writing))
250 			/ blockswritten * tapesize;
251 
252 		msg("%3.2f%% done, finished in %ld:%02ld",
253 		    (blockswritten * 100.0) / tapesize,
254 		    (long)(deltat / 3600), (long)((deltat % 3600) / 60));
255 
256 		if (timestamp == 1)
257 			do_timestamp(tnow + deltat, " (at %s)");
258 
259 		fprintf(stderr, "\n");
260 	}
261 }
262 
263 void
264 msg(const char *fmt, ...)
265 {
266 	time_t  tnow;
267 	va_list ap;
268 
269 	fprintf(stderr, "  ");
270 	if (timestamp == 1) {
271 		(void) time((time_t *) &tnow);
272 		do_timestamp(tnow, "[%s] ");
273 	}
274 
275 	(void) fprintf(stderr,"DUMP: ");
276 #ifdef TDEBUG
277 	(void) fprintf(stderr, "pid=%d ", getpid());
278 #endif
279 	va_start(ap, fmt);
280 	(void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap);
281 	fputs(lastmsg, stderr);
282 	(void) fflush(stdout);
283 	(void) fflush(stderr);
284 	va_end(ap);
285 }
286 
287 void
288 msgtail(const char *fmt, ...)
289 {
290 	va_list ap;
291 
292 	va_start(ap, fmt);
293 	(void) vfprintf(stderr, fmt, ap);
294 	va_end(ap);
295 }
296 
297 void
298 quit(const char *fmt, ...)
299 {
300 	va_list ap;
301 
302 	(void) fprintf(stderr,"  DUMP: ");
303 #ifdef TDEBUG
304 	(void) fprintf(stderr, "pid=%d ", getpid());
305 #endif
306 	va_start(ap, fmt);
307 	(void) vfprintf(stderr, fmt, ap);
308 	va_end(ap);
309 	(void) fflush(stdout);
310 	(void) fflush(stderr);
311 	dumpabort(0);
312 }
313 
314 /*
315  *	Tell the operator what has to be done;
316  *	we don't actually do it
317  */
318 
319 static struct fstab *
320 allocfsent(const struct fstab *fs)
321 {
322 	struct fstab *new;
323 
324 	new = xmalloc(sizeof (*fs));
325 	new->fs_file = xstrdup(fs->fs_file);
326 	new->fs_type = xstrdup(fs->fs_type);
327 	new->fs_spec = xstrdup(fs->fs_spec);
328 	new->fs_passno = fs->fs_passno;
329 	new->fs_freq = fs->fs_freq;
330 	return (new);
331 }
332 
333 struct	pfstab {
334 	SLIST_ENTRY(pfstab) pf_list;
335 	struct	fstab *pf_fstab;
336 };
337 
338 static	SLIST_HEAD(, pfstab) table;
339 
340 void
341 getfstab(void)
342 {
343 	struct fstab *fs;
344 	struct pfstab *pf;
345 
346 	if (setfsent() == 0) {
347 		msg("Can't open %s for dump table information: %s\n",
348 		    _PATH_FSTAB, strerror(errno));
349 		return;
350 	}
351 	while ((fs = getfsent()) != NULL) {
352 		if (strcmp(fs->fs_type, FSTAB_RW) &&
353 		    strcmp(fs->fs_type, FSTAB_RO) &&
354 		    strcmp(fs->fs_type, FSTAB_RQ))
355 			continue;
356 #ifdef DUMP_LFS
357 		if (strcmp(fs->fs_vfstype, "lfs"))
358 			continue;
359 #else
360 		if (strcmp(fs->fs_vfstype, "ufs") &&
361 		    strcmp(fs->fs_vfstype, "ffs"))
362 			continue;
363 #endif
364 		fs = allocfsent(fs);
365 		pf = (struct pfstab *)xmalloc(sizeof (*pf));
366 		pf->pf_fstab = fs;
367 		SLIST_INSERT_HEAD(&table, pf, pf_list);
368 	}
369 	(void) endfsent();
370 }
371 
372 /*
373  * Search in the fstab for a file name.
374  * This file name can be either the special or the path file name.
375  *
376  * The entries in the fstab are the BLOCK special names, not the
377  * character special names.
378  * The caller of fstabsearch assures that the character device
379  * is dumped (that is much faster)
380  *
381  * The file name can omit the leading '/'.
382  */
383 struct fstab *
384 fstabsearch(const char *key)
385 {
386 	struct pfstab *pf;
387 	struct fstab *fs;
388 	const char *rn;
389 	char buf[MAXPATHLEN];
390 
391 	SLIST_FOREACH(pf, &table, pf_list) {
392 		fs = pf->pf_fstab;
393 		if (strcmp(fs->fs_file, key) == 0 ||
394 		    strcmp(fs->fs_spec, key) == 0)
395 			return (fs);
396 		rn = getdiskrawname(buf, sizeof(buf), fs->fs_spec);
397 		if (rn != NULL && strcmp(rn, key) == 0)
398 			return (fs);
399 		if (key[0] != '/') {
400 			if (*fs->fs_spec == '/' &&
401 			    strcmp(fs->fs_spec + 1, key) == 0)
402 				return (fs);
403 			if (*fs->fs_file == '/' &&
404 			    strcmp(fs->fs_file + 1, key) == 0)
405 				return (fs);
406 		}
407 	}
408 	return (NULL);
409 }
410 
411 /*
412  * Search in the mounted file list for a file name.
413  * This file name can be either the special or the path file name.
414  *
415  * The entries in the list are the BLOCK special names, not the
416  * character special names.
417  * The caller of mntinfosearch assures that the character device
418  * is dumped (that is much faster)
419  */
420 struct statvfs *
421 mntinfosearch(const char *key)
422 {
423 	int i, mntbufc;
424 	struct statvfs *mntbuf, *fs;
425 	const char *rn;
426 	char buf[MAXPATHLEN];
427 
428 	if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
429 		quit("Can't get mount list: %s", strerror(errno));
430 	for (fs = mntbuf, i = 0; i < mntbufc; i++, fs++) {
431 #ifdef DUMP_LFS
432 		if (strcmp(fs->f_fstypename, "lfs") != 0)
433 			continue;
434 #else /* ! DUMP_LFS */
435 		if (strcmp(fs->f_fstypename, "ufs") != 0 &&
436 		    strcmp(fs->f_fstypename, "ffs") != 0)
437 			continue;
438 #endif /* ! DUMP_LFS */
439 		if (strcmp(fs->f_mntonname, key) == 0 ||
440 		    strcmp(fs->f_mntfromname, key) == 0)
441 			return (fs);
442 		rn = getdiskrawname(buf, sizeof(buf), fs->f_mntfromname);
443 		if (rn != NULL && strcmp(rn, key) == 0)
444 			return (fs);
445 	}
446 	return (NULL);
447 }
448 
449 
450 /*
451  *	Tell the operator what to do.
452  *	arg:	w ==> just what to do; W ==> most recent dumps
453  */
454 void
455 lastdump(char arg)
456 {
457 	int i;
458 	struct fstab *dt;
459 	struct dumpdates *dtwalk;
460 	char *date;
461 	const char *lastname;
462 	int dumpme;
463 	time_t tnow;
464 
465 	(void) time(&tnow);
466 	getfstab();		/* /etc/fstab input */
467 	initdumptimes();	/* /etc/dumpdates input */
468 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
469 
470 	if (arg == 'w')
471 		(void) printf("Dump these file systems:\n");
472 	else
473 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
474 	lastname = "??";
475 	ITITERATE(i, dtwalk) {
476 		if (strncmp(lastname, dtwalk->dd_name,
477 		    sizeof(dtwalk->dd_name)) == 0)
478 			continue;
479 		date = (char *)ctime(&dtwalk->dd_ddate);
480 		date[24] = '\0';
481 		strcpy(date + 16, date + 19);	/* blast away seconds */
482 		lastname = dtwalk->dd_name;
483 		dt = fstabsearch(dtwalk->dd_name);
484 		dumpme = (dt != NULL &&
485 		    dt->fs_freq != 0 &&
486 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
487 		if (arg != 'w' || dumpme)
488 			(void) printf(
489 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
490 			    dumpme && (arg != 'w') ? '>' : ' ',
491 			    dtwalk->dd_name,
492 			    dt ? dt->fs_file : "",
493 			    dtwalk->dd_level,
494 			    date);
495 	}
496 }
497 
498 static int
499 datesort(const void *a1, const void *a2)
500 {
501 	const struct dumpdates *d1 = *(const struct dumpdates *const *)a1;
502 	const struct dumpdates *d2 = *(const struct dumpdates *const *)a2;
503 	int diff;
504 
505 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
506 	if (diff == 0)
507 		return (d2->dd_ddate - d1->dd_ddate);
508 	return (diff);
509 }
510