xref: /netbsd-src/sbin/dump/optr.c (revision 7788a0781fe6ff2cce37368b4578a7ade0850cb1)
1 /*	$NetBSD: optr.c,v 1.41 2013/06/15 01:27:19 christos 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.41 2013/06/15 01:27:19 christos 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 	char buf[MAXPATHLEN];
324 
325 	new = xmalloc(sizeof (*fs));
326 	new->fs_file = xstrdup(fs->fs_file);
327 	new->fs_type = xstrdup(fs->fs_type);
328 
329 	if (getfsspecname(buf, sizeof(buf), fs->fs_spec) == NULL)
330 		msg("%s (%s)", buf, strerror(errno));
331 	new->fs_spec = xstrdup(buf);
332 	new->fs_passno = fs->fs_passno;
333 	new->fs_freq = fs->fs_freq;
334 	return (new);
335 }
336 
337 struct	pfstab {
338 	SLIST_ENTRY(pfstab) pf_list;
339 	struct	fstab *pf_fstab;
340 };
341 
342 static	SLIST_HEAD(, pfstab) table;
343 
344 void
345 getfstab(void)
346 {
347 	struct fstab *fs;
348 	struct pfstab *pf;
349 
350 	if (setfsent() == 0) {
351 		msg("Can't open %s for dump table information: %s\n",
352 		    _PATH_FSTAB, strerror(errno));
353 		return;
354 	}
355 	while ((fs = getfsent()) != NULL) {
356 		if (strcmp(fs->fs_type, FSTAB_RW) &&
357 		    strcmp(fs->fs_type, FSTAB_RO) &&
358 		    strcmp(fs->fs_type, FSTAB_RQ))
359 			continue;
360 #ifdef DUMP_LFS
361 		if (strcmp(fs->fs_vfstype, "lfs"))
362 			continue;
363 #else
364 		if (strcmp(fs->fs_vfstype, "ufs") &&
365 		    strcmp(fs->fs_vfstype, "ffs"))
366 			continue;
367 #endif
368 		fs = allocfsent(fs);
369 		pf = (struct pfstab *)xmalloc(sizeof (*pf));
370 		pf->pf_fstab = fs;
371 		SLIST_INSERT_HEAD(&table, pf, pf_list);
372 	}
373 	(void) endfsent();
374 }
375 
376 /*
377  * Search in the fstab for a file name.
378  * This file name can be either the special or the path file name.
379  *
380  * The entries in the fstab are the BLOCK special names, not the
381  * character special names.
382  * The caller of fstabsearch assures that the character device
383  * is dumped (that is much faster)
384  *
385  * The file name can omit the leading '/'.
386  */
387 struct fstab *
388 fstabsearch(const char *key)
389 {
390 	struct pfstab *pf;
391 	struct fstab *fs;
392 	const char *rn;
393 	char buf[MAXPATHLEN];
394 
395 	SLIST_FOREACH(pf, &table, pf_list) {
396 		fs = pf->pf_fstab;
397 		if (strcmp(fs->fs_file, key) == 0 ||
398 		    strcmp(fs->fs_spec, key) == 0)
399 			return (fs);
400 		rn = getdiskrawname(buf, sizeof(buf), fs->fs_spec);
401 		if (rn != NULL && strcmp(rn, key) == 0)
402 			return (fs);
403 		if (key[0] != '/') {
404 			if (*fs->fs_spec == '/' &&
405 			    strcmp(fs->fs_spec + 1, key) == 0)
406 				return (fs);
407 			if (*fs->fs_file == '/' &&
408 			    strcmp(fs->fs_file + 1, key) == 0)
409 				return (fs);
410 		}
411 	}
412 	return (NULL);
413 }
414 
415 /*
416  * Search in the mounted file list for a file name.
417  * This file name can be either the special or the path file name.
418  *
419  * The entries in the list are the BLOCK special names, not the
420  * character special names.
421  * The caller of mntinfosearch assures that the character device
422  * is dumped (that is much faster)
423  */
424 struct statvfs *
425 mntinfosearch(const char *key)
426 {
427 	int i, mntbufc;
428 	struct statvfs *mntbuf, *fs;
429 	const char *rn;
430 	char buf[MAXPATHLEN];
431 
432 	if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
433 		quit("Can't get mount list: %s", strerror(errno));
434 	for (fs = mntbuf, i = 0; i < mntbufc; i++, fs++) {
435 #ifdef DUMP_LFS
436 		if (strcmp(fs->f_fstypename, "lfs") != 0)
437 			continue;
438 #else /* ! DUMP_LFS */
439 		if (strcmp(fs->f_fstypename, "ufs") != 0 &&
440 		    strcmp(fs->f_fstypename, "ffs") != 0)
441 			continue;
442 #endif /* ! DUMP_LFS */
443 		if (strcmp(fs->f_mntonname, key) == 0 ||
444 		    strcmp(fs->f_mntfromname, key) == 0)
445 			return (fs);
446 		rn = getdiskrawname(buf, sizeof(buf), fs->f_mntfromname);
447 		if (rn != NULL && strcmp(rn, key) == 0)
448 			return (fs);
449 	}
450 	return (NULL);
451 }
452 
453 
454 /*
455  *	Tell the operator what to do.
456  *	arg:	w ==> just what to do; W ==> most recent dumps
457  */
458 void
459 lastdump(char arg)
460 {
461 	int i;
462 	struct fstab *dt;
463 	struct dumpdates *dtwalk;
464 	char *date;
465 	const char *lastname;
466 	int dumpme;
467 	time_t tnow;
468 
469 	(void) time(&tnow);
470 	getfstab();		/* /etc/fstab input */
471 	initdumptimes();	/* /etc/dumpdates input */
472 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
473 
474 	if (arg == 'w')
475 		(void) printf("Dump these file systems:\n");
476 	else
477 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
478 	lastname = "??";
479 	ITITERATE(i, dtwalk) {
480 		if (strncmp(lastname, dtwalk->dd_name,
481 		    sizeof(dtwalk->dd_name)) == 0)
482 			continue;
483 		date = (char *)ctime(&dtwalk->dd_ddate);
484 		date[24] = '\0';
485 		strcpy(date + 16, date + 19);	/* blast away seconds */
486 		lastname = dtwalk->dd_name;
487 		dt = fstabsearch(dtwalk->dd_name);
488 		dumpme = (dt != NULL &&
489 		    dt->fs_freq != 0 &&
490 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
491 		if (arg != 'w' || dumpme)
492 			(void) printf(
493 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
494 			    dumpme && (arg != 'w') ? '>' : ' ',
495 			    dtwalk->dd_name,
496 			    dt ? dt->fs_file : "",
497 			    dtwalk->dd_level,
498 			    date);
499 	}
500 }
501 
502 static int
503 datesort(const void *a1, const void *a2)
504 {
505 	const struct dumpdates *d1 = *(const struct dumpdates *const *)a1;
506 	const struct dumpdates *d2 = *(const struct dumpdates *const *)a2;
507 	int diff;
508 
509 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
510 	if (diff == 0)
511 		return (d2->dd_ddate - d1->dd_ddate);
512 	return (diff);
513 }
514