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