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