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