1 /* $OpenBSD: optr.c,v 1.28 2004/11/04 20:10:07 deraadt Exp $ */
2 /* $NetBSD: optr.c,v 1.11 1997/05/27 08:34:36 mrg Exp $ */
3
4 /*-
5 * Copyright (c) 1980, 1988, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94";
36 #else
37 static const char rcsid[] = "$OpenBSD: optr.c,v 1.28 2004/11/04 20:10:07 deraadt Exp $";
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/wait.h>
43 #include <sys/time.h>
44
45 #include <errno.h>
46 #include <fstab.h>
47 #include <grp.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <stdarg.h>
53 #include <tzfile.h>
54 #include <unistd.h>
55 #include <utmp.h>
56
57 #include "dump.h"
58 #include "pathnames.h"
59
60 void alarmcatch(int);
61 int datesort(const void *, const void *);
62
63 /*
64 * Query the operator; This previously-fascist piece of code
65 * no longer requires an exact response.
66 * It is intended to protect dump aborting by inquisitive
67 * people banging on the console terminal to see what is
68 * happening which might cause dump to croak, destroying
69 * a large number of hours of work.
70 *
71 * Every 2 minutes we reprint the message, alerting others
72 * that dump needs attention.
73 */
74 static int timeout;
75 static char *attnmessage; /* attention message */
76
77 int
query(char * question)78 query(char *question)
79 {
80 char replybuffer[64];
81 int back, errcount;
82 FILE *mytty;
83 time_t firstprompt, when_answered;
84
85 (void) time(&firstprompt);
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 (void) time(&when_answered);
119 /*
120 * Adjust the base for time estimates to ignore time we spent waiting
121 * for operator input.
122 */
123 if (when_answered - firstprompt > 0)
124 tstart_writing += (when_answered - firstprompt);
125 return (back);
126 }
127
128 char lastmsg[BUFSIZ];
129
130 /*
131 * Alert the console operator, and enable the alarm clock to
132 * sleep for 2 minutes in case nobody comes to satisfy dump
133 * XXX not safe
134 */
135 /* ARGSUSED */
136 void
alarmcatch(int signo)137 alarmcatch(int signo)
138 {
139 int save_errno = errno;
140
141 if (notify == 0) {
142 if (timeout == 0)
143 (void) fprintf(stderr,
144 " DUMP: %s: (\"yes\" or \"no\") ",
145 attnmessage);
146 else
147 msgtail("\7\7");
148 } else {
149 if (timeout) {
150 msgtail("\n");
151 broadcast(""); /* just print last msg */
152 }
153 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
154 attnmessage);
155 }
156 signal(SIGALRM, alarmcatch);
157 (void) alarm(120);
158 timeout = 1;
159 errno = save_errno;
160 }
161
162 /*
163 * Here if an inquisitive operator interrupts the dump program
164 */
165 /* ARGSUSED */
166 void
interrupt(int signo)167 interrupt(int signo)
168 {
169 msg("Interrupt received.\n");
170 if (query("Do you want to abort dump?"))
171 dumpabort(0);
172 }
173
174 /*
175 * We now use wall(1) to do the actual broadcasting.
176 */
177 void
broadcast(char * message)178 broadcast(char *message)
179 {
180 FILE *fp;
181 char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
182
183 if (!notify)
184 return;
185
186 (void)snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
187 if ((fp = popen(buf, "w")) == NULL)
188 return;
189
190 (void) fputs("\7\7\7Message from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
191 if (lastmsg[0])
192 (void) fputs(lastmsg, fp);
193 if (message[0])
194 (void) fputs(message, fp);
195
196 (void) pclose(fp);
197 }
198
199 /*
200 * Print out an estimate of the amount of time left to do the dump
201 */
202
203 time_t tschedule = 0;
204
205 void
timeest(void)206 timeest(void)
207 {
208 time_t tnow, deltat;
209
210 (void) time((time_t *) &tnow);
211 if (tnow >= tschedule) {
212 tschedule = tnow + 300;
213 if (blockswritten < 500)
214 return;
215 deltat = tstart_writing - tnow +
216 (1.0 * (tnow - tstart_writing))
217 / blockswritten * tapesize;
218 msg("%3.2f%% done, finished in %d:%02d\n",
219 (blockswritten * 100.0) / tapesize,
220 deltat / 3600, (deltat % 3600) / 60);
221 }
222 }
223
224 void
msg(const char * fmt,...)225 msg(const char *fmt, ...)
226 {
227 va_list ap;
228
229 (void) fprintf(stderr," DUMP: ");
230 #ifdef TDEBUG
231 (void) fprintf(stderr, "pid=%d ", getpid());
232 #endif
233 va_start(ap, fmt);
234 (void) vfprintf(stderr, fmt, ap);
235 va_end(ap);
236 (void) fflush(stdout);
237 (void) fflush(stderr);
238 va_start(ap, fmt);
239 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
240 va_end(ap);
241 }
242
243 void
msgtail(const char * fmt,...)244 msgtail(const char *fmt, ...)
245 {
246 va_list ap;
247
248 va_start(ap, fmt);
249 (void) vfprintf(stderr, fmt, ap);
250 va_end(ap);
251 }
252
253 void
quit(const char * fmt,...)254 quit(const char *fmt, ...)
255 {
256 va_list ap;
257
258 (void) fprintf(stderr," DUMP: ");
259 #ifdef TDEBUG
260 (void) fprintf(stderr, "pid=%d ", getpid());
261 #endif
262 va_start(ap, fmt);
263 (void) vfprintf(stderr, fmt, ap);
264 va_end(ap);
265 (void) fflush(stdout);
266 (void) fflush(stderr);
267 dumpabort(0);
268 }
269
270 /*
271 * Tell the operator what has to be done;
272 * we don't actually do it
273 */
274
275 struct fstab *
allocfsent(struct fstab * fs)276 allocfsent(struct fstab *fs)
277 {
278 struct fstab *new;
279
280 new = (struct fstab *)malloc(sizeof(*fs));
281 if (new == NULL ||
282 (new->fs_file = strdup(fs->fs_file)) == NULL ||
283 (new->fs_type = strdup(fs->fs_type)) == NULL ||
284 (new->fs_spec = strdup(fs->fs_spec)) == NULL)
285 quit("%s\n", strerror(errno));
286 new->fs_passno = fs->fs_passno;
287 new->fs_freq = fs->fs_freq;
288 return (new);
289 }
290
291 struct pfstab {
292 struct pfstab *pf_next;
293 struct fstab *pf_fstab;
294 };
295
296 static struct pfstab *table;
297
298 void
getfstab(void)299 getfstab(void)
300 {
301 struct fstab *fs;
302 struct pfstab *pf;
303
304 if (setfsent() == 0) {
305 msg("Can't open %s for dump table information: %s\n",
306 _PATH_FSTAB, strerror(errno));
307 return;
308 }
309 while ((fs = getfsent()) != NULL) {
310 if (strcmp(fs->fs_vfstype, "ffs") &&
311 strcmp(fs->fs_vfstype, "ufs"))
312 continue;
313 if (strcmp(fs->fs_type, FSTAB_RW) &&
314 strcmp(fs->fs_type, FSTAB_RO) &&
315 strcmp(fs->fs_type, FSTAB_RQ))
316 continue;
317 fs = allocfsent(fs);
318 if ((pf = (struct pfstab *)malloc(sizeof(*pf))) == NULL)
319 quit("%s\n", strerror(errno));
320 pf->pf_fstab = fs;
321 pf->pf_next = table;
322 table = pf;
323 }
324 (void) endfsent();
325 }
326
327 /*
328 * Search in the fstab for a file name.
329 * This file name can be either the special or the path file name.
330 *
331 * The entries in the fstab are the BLOCK special names, not the
332 * character special names.
333 * The caller of fstabsearch assures that the character device
334 * is dumped (that is much faster)
335 *
336 * The file name can omit the leading '/'.
337 */
338 struct fstab *
fstabsearch(char * key)339 fstabsearch(char *key)
340 {
341 struct pfstab *pf;
342 struct fstab *fs;
343 char *rn;
344
345 for (pf = table; pf != NULL; pf = pf->pf_next) {
346 fs = pf->pf_fstab;
347 if (strcmp(fs->fs_file, key) == 0 ||
348 strcmp(fs->fs_spec, key) == 0)
349 return (fs);
350 rn = rawname(fs->fs_spec);
351 if (rn != NULL && strcmp(rn, key) == 0)
352 return (fs);
353 if (key[0] != '/') {
354 if (*fs->fs_spec == '/' &&
355 strcmp(fs->fs_spec + 1, key) == 0)
356 return (fs);
357 if (*fs->fs_file == '/' &&
358 strcmp(fs->fs_file + 1, key) == 0)
359 return (fs);
360 }
361 }
362 return (NULL);
363 }
364
365 /*
366 * Tell the operator what to do
367 * w ==> just what to do; W ==> most recent dumps
368 */
369 void
lastdump(int arg)370 lastdump(int arg)
371 {
372 int i;
373 struct fstab *dt;
374 struct dumpdates *dtwalk;
375 char *lastname, *date;
376 int dumpme;
377 time_t tnow;
378
379 (void) time(&tnow);
380 getfstab(); /* /etc/fstab input */
381 initdumptimes(); /* /etc/dumpdates input */
382 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
383
384 if (arg == 'w')
385 (void) printf("Dump these file systems:\n");
386 else
387 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
388 lastname = "??";
389 ITITERATE(i, dtwalk) {
390 if (strncmp(lastname, dtwalk->dd_name,
391 sizeof(dtwalk->dd_name)) == 0)
392 continue;
393 date = (char *)ctime(&dtwalk->dd_ddate);
394 date[16] = '\0'; /* blast away seconds and year */
395 lastname = dtwalk->dd_name;
396 dt = fstabsearch(dtwalk->dd_name);
397 dumpme = (dt != NULL &&
398 dt->fs_freq != 0 &&
399 dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
400 if (arg != 'w' || dumpme)
401 (void) printf(
402 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
403 dumpme && (arg != 'w') ? '>' : ' ',
404 dtwalk->dd_name,
405 dt ? dt->fs_file : "",
406 dtwalk->dd_level,
407 date);
408 }
409 }
410
411 int
datesort(const void * a1,const void * a2)412 datesort(const void *a1, const void *a2)
413 {
414 struct dumpdates *d1 = *(struct dumpdates **)a1;
415 struct dumpdates *d2 = *(struct dumpdates **)a2;
416 int diff;
417
418 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
419 if (diff == 0)
420 return (d2->dd_ddate - d1->dd_ddate);
421 return (diff);
422 }
423