optr.c revision 141965
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31#if 0
32static char sccsid[] = "@(#)optr.c	8.2 (Berkeley) 1/6/94";
33#endif
34static const char rcsid[] =
35  "$FreeBSD: head/sbin/dump/optr.c 141965 2005-02-16 06:48:35Z obrien $";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/queue.h>
40#include <sys/wait.h>
41#include <sys/time.h>
42
43#include <ufs/ufs/dinode.h>
44
45#include <errno.h>
46#include <fstab.h>
47#include <grp.h>
48#include <limits.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <stdarg.h>
53#include <signal.h>
54#include <unistd.h>
55
56#include "dump.h"
57#include "pathnames.h"
58
59void	alarmcatch(int);
60int	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 */
73static	int timeout;
74static	const char *attnmessage;		/* attention message */
75
76int
77query(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			(void) fprintf(stderr,
101			    "  DUMP: \"Yes\" or \"No\"?\n");
102			(void) fprintf(stderr,
103			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
104		}
105	} while (back < 0);
106
107	/*
108	 *	Turn off the alarm, and reset the signal to trap out..
109	 */
110	(void) alarm(0);
111	if (signal(SIGALRM, sig) == SIG_IGN)
112		signal(SIGALRM, SIG_IGN);
113	(void) fclose(mytty);
114	return(back);
115}
116
117char lastmsg[BUFSIZ];
118
119/*
120 *	Alert the console operator, and enable the alarm clock to
121 *	sleep for 2 minutes in case nobody comes to satisfy dump
122 */
123void
124alarmcatch(int sig __unused)
125{
126	if (notify == 0) {
127		if (timeout == 0)
128			(void) fprintf(stderr,
129			    "  DUMP: %s: (\"yes\" or \"no\") ",
130			    attnmessage);
131		else
132			msgtail("\a\a");
133	} else {
134		if (timeout) {
135			msgtail("\n");
136			broadcast("");		/* just print last msg */
137		}
138		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
139		    attnmessage);
140	}
141	signal(SIGALRM, alarmcatch);
142	(void) alarm(120);
143	timeout = 1;
144}
145
146/*
147 *	Here if an inquisitive operator interrupts the dump program
148 */
149void
150interrupt(int signo __unused)
151{
152	msg("Interrupt received.\n");
153	if (query("Do you want to abort dump?"))
154		dumpabort(0);
155}
156
157/*
158 *	We now use wall(1) to do the actual broadcasting.
159 */
160void
161broadcast(const char *message)
162{
163	FILE *fp;
164	char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
165
166	if (!notify)
167		return;
168
169	snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
170	if ((fp = popen(buf, "w")) == NULL)
171		return;
172
173	(void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
174	if (lastmsg[0])
175		(void) fputs(lastmsg, fp);
176	if (message[0])
177		(void) fputs(message, fp);
178
179	(void) pclose(fp);
180}
181
182/*
183 *	Print out an estimate of the amount of time left to do the dump
184 */
185
186time_t	tschedule = 0;
187
188void
189timeest(void)
190{
191	double percent;
192	time_t	tnow, tdone;
193	int deltat, hours, mins;
194
195	(void)time(&tnow);
196	if (blockswritten > tapesize) {
197		setproctitle("%s: 99.99%% done, finished soon", disk);
198		if (tnow >= tschedule) {
199			tschedule = tnow + 300;
200			msg("99.99%% done, finished soon\n");
201		}
202	} else {
203		deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
204		    (double)(tnow - tstart_writing) / blockswritten * tapesize;
205		tdone = tnow + deltat;
206		percent = (blockswritten * 100.0) / tapesize;
207		hours = deltat / 3600;
208		mins = (deltat % 3600) / 60;
209
210		setproctitle(
211		    "%s: pass %d: %3.2f%% done, finished in %d:%02d at %s",
212		    disk, passno, percent, hours, mins, ctime(&tdone));
213		if (tnow >= tschedule) {
214			tschedule = tnow + 300;
215			if (blockswritten < 500)
216				return;
217			msg("%3.2f%% done, finished in %d:%02d at %s", percent,
218			    hours, mins, ctime(&tdone));
219		}
220	}
221}
222
223/*
224 * Schedule a printout of the estimate in the next call to timeest().
225 */
226void
227infosch(int signal __unused)
228{
229	tschedule = 0;
230}
231
232void
233msg(const char *fmt, ...)
234{
235	va_list ap;
236
237	(void) fprintf(stderr,"  DUMP: ");
238#ifdef TDEBUG
239	(void) fprintf(stderr, "pid=%d ", getpid());
240#endif
241	va_start(ap, fmt);
242	(void) vfprintf(stderr, fmt, ap);
243	va_end(ap);
244	(void) fflush(stdout);
245	(void) fflush(stderr);
246	va_start(ap, fmt);
247	(void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
248	va_end(ap);
249}
250
251void
252msgtail(const char *fmt, ...)
253{
254	va_list ap;
255
256	va_start(ap, fmt);
257	(void) vfprintf(stderr, fmt, ap);
258	va_end(ap);
259}
260
261void
262quit(const char *fmt, ...)
263{
264	va_list ap;
265
266	(void) fprintf(stderr,"  DUMP: ");
267#ifdef TDEBUG
268	(void) fprintf(stderr, "pid=%d ", getpid());
269#endif
270	va_start(ap, fmt);
271	(void) vfprintf(stderr, fmt, ap);
272	va_end(ap);
273	(void) fflush(stdout);
274	(void) fflush(stderr);
275	dumpabort(0);
276}
277
278/*
279 *	Tell the operator what has to be done;
280 *	we don't actually do it
281 */
282
283struct fstab *
284allocfsent(const struct fstab *fs)
285{
286	struct fstab *new;
287
288	new = (struct fstab *)malloc(sizeof (*fs));
289	if (new == NULL ||
290	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
291	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
292	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
293		quit("%s\n", strerror(errno));
294	new->fs_passno = fs->fs_passno;
295	new->fs_freq = fs->fs_freq;
296	return (new);
297}
298
299struct	pfstab {
300	SLIST_ENTRY(pfstab) pf_list;
301	struct	fstab *pf_fstab;
302};
303
304static	SLIST_HEAD(, pfstab) table;
305
306void
307dump_getfstab(void)
308{
309	struct fstab *fs;
310	struct pfstab *pf;
311
312	if (setfsent() == 0) {
313		msg("Can't open %s for dump table information: %s\n",
314		    _PATH_FSTAB, strerror(errno));
315		return;
316	}
317	while ((fs = getfsent()) != NULL) {
318		if (strcmp(fs->fs_type, FSTAB_RW) &&
319		    strcmp(fs->fs_type, FSTAB_RO) &&
320		    strcmp(fs->fs_type, FSTAB_RQ))
321			continue;
322		fs = allocfsent(fs);
323		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
324			quit("%s\n", strerror(errno));
325		pf->pf_fstab = fs;
326		SLIST_INSERT_HEAD(&table, pf, pf_list);
327	}
328	(void) endfsent();
329}
330
331/*
332 * Search in the fstab for a file name.
333 * This file name can be either the special or the path file name.
334 *
335 * The file name can omit the leading '/'.
336 */
337struct fstab *
338fstabsearch(const char *key)
339{
340	struct pfstab *pf;
341	struct fstab *fs;
342	char *rn;
343
344	SLIST_FOREACH(pf, &table, pf_list) {
345		fs = pf->pf_fstab;
346		if (strcmp(fs->fs_file, key) == 0 ||
347		    strcmp(fs->fs_spec, key) == 0)
348			return (fs);
349		rn = rawname(fs->fs_spec);
350		if (rn != NULL && strcmp(rn, key) == 0)
351			return (fs);
352		if (key[0] != '/') {
353			if (*fs->fs_spec == '/' &&
354			    strcmp(fs->fs_spec + 1, key) == 0)
355				return (fs);
356			if (*fs->fs_file == '/' &&
357			    strcmp(fs->fs_file + 1, key) == 0)
358				return (fs);
359		}
360	}
361	return (NULL);
362}
363
364/*
365 *	Tell the operator what to do
366 */
367void
368lastdump(int arg)	/* w ==> just what to do; W ==> most recent dumps */
369{
370	int i;
371	struct fstab *dt;
372	struct dumpdates *dtwalk;
373	char *lastname, *date;
374	int dumpme;
375	time_t tnow;
376	struct tm *tlast;
377
378	(void) time(&tnow);
379	dump_getfstab();	/* /etc/fstab input */
380	initdumptimes();	/* /etc/dumpdates input */
381	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
382
383	if (arg == 'w')
384		(void) printf("Dump these file systems:\n");
385	else
386		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
387	lastname = "??";
388	ITITERATE(i, dtwalk) {
389		if (strncmp(lastname, dtwalk->dd_name,
390		    sizeof(dtwalk->dd_name)) == 0)
391			continue;
392		date = (char *)ctime(&dtwalk->dd_ddate);
393		date[16] = '\0';	/* blast away seconds and year */
394		lastname = dtwalk->dd_name;
395		dt = fstabsearch(dtwalk->dd_name);
396		dumpme = (dt != NULL && dt->fs_freq != 0);
397		if (dumpme) {
398		    tlast = localtime(&dtwalk->dd_ddate);
399		    dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
400				     - (tlast->tm_min * 60) - tlast->tm_sec
401				     + (dt->fs_freq * 86400));
402		};
403		if (arg != 'w' || dumpme)
404			(void) printf(
405			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
406			    dumpme && (arg != 'w') ? '>' : ' ',
407			    dtwalk->dd_name,
408			    dt ? dt->fs_file : "",
409			    dtwalk->dd_level,
410			    date);
411	}
412}
413
414int
415datesort(const void *a1, const void *a2)
416{
417	struct dumpdates *d1 = *(struct dumpdates **)a1;
418	struct dumpdates *d2 = *(struct dumpdates **)a2;
419	int diff;
420
421	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
422	if (diff == 0)
423		return (d2->dd_ddate - d1->dd_ddate);
424	return (diff);
425}
426