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