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