optr.c revision 1.29 1 /* $NetBSD: optr.c,v 1.29 2003/03/27 13:56:47 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94";
40 #else
41 __RCSID("$NetBSD: optr.c,v 1.29 2003/03/27 13:56:47 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/wait.h>
48 #include <sys/time.h>
49 #include <sys/ucred.h>
50 #include <sys/mount.h>
51
52 #include <errno.h>
53 #include <fstab.h>
54 #include <grp.h>
55 #include <signal.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <stdarg.h>
60 #include <time.h>
61 #include <tzfile.h>
62 #include <unistd.h>
63
64 #include "dump.h"
65 #include "pathnames.h"
66
67 void alarmcatch(int);
68 struct fstab *allocfsent(struct fstab *);
69 int datesort(const void *, const void *);
70 extern char *time_string;
71 extern char default_time_string[];
72
73 static void do_timestamp(time_t, char *);
74
75 /*
76 * Query the operator; This previously-fascist piece of code
77 * no longer requires an exact response.
78 * It is intended to protect dump aborting by inquisitive
79 * people banging on the console terminal to see what is
80 * happening which might cause dump to croak, destroying
81 * a large number of hours of work.
82 *
83 * Every 2 minutes we reprint the message, alerting others
84 * that dump needs attention.
85 */
86 static int timeout;
87 static char *attnmessage; /* attention message */
88
89 int
90 query(char *question)
91 {
92 char replybuffer[64];
93 int back, errcount;
94 FILE *mytty;
95 time_t firstprompt, when_answered;
96
97 firstprompt = time((time_t *)0);
98
99 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
100 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
101 attnmessage = question;
102 timeout = 0;
103 alarmcatch(0);
104 back = -1;
105 errcount = 0;
106 do {
107 if (fgets(replybuffer, 63, mytty) == NULL) {
108 clearerr(mytty);
109 if (++errcount > 30) /* XXX ugly */
110 quit("excessive operator query failures\n");
111 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
112 back = 1;
113 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
114 back = 0;
115 } else {
116 (void) fprintf(stderr,
117 " DUMP: \"Yes\" or \"No\"?\n");
118 (void) fprintf(stderr,
119 " DUMP: %s: (\"yes\" or \"no\") ", question);
120 }
121 } while (back < 0);
122
123 /*
124 * Turn off the alarm, and reset the signal to trap out..
125 */
126 (void) alarm(0);
127 if (signal(SIGALRM, sig) == SIG_IGN)
128 signal(SIGALRM, SIG_IGN);
129 (void) fclose(mytty);
130 when_answered = time((time_t *)0);
131 /*
132 * Adjust the base for time estimates to ignore time we spent waiting
133 * for operator input.
134 */
135 if (tstart_writing != 0)
136 tstart_writing += (when_answered - firstprompt);
137 return(back);
138 }
139
140 char lastmsg[200];
141
142 /*
143 * Alert the console operator, and enable the alarm clock to
144 * sleep for 2 minutes in case nobody comes to satisfy dump
145 */
146 void
147 alarmcatch(int dummy)
148 {
149
150 if (notify == 0) {
151 if (timeout == 0)
152 (void) fprintf(stderr,
153 " DUMP: %s: (\"yes\" or \"no\") ",
154 attnmessage);
155 else
156 msgtail("\a\a");
157 } else {
158 if (timeout) {
159 msgtail("\n");
160 broadcast(""); /* just print last msg */
161 }
162 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
163 attnmessage);
164 }
165 signal(SIGALRM, alarmcatch);
166 (void) alarm(120);
167 timeout = 1;
168 }
169
170 /*
171 * Here if an inquisitive operator interrupts the dump program
172 */
173 void
174 interrupt(int signo)
175 {
176 int errno_save;
177
178 errno_save = errno;
179 msg("Interrupt received.\n");
180 if (query("Do you want to abort dump?"))
181 dumpabort(0);
182 errno = errno_save;
183 }
184
185 /*
186 * Use wall(1) "-g operator" to do the actual broadcasting.
187 */
188 void
189 broadcast(char *message)
190 {
191 FILE *fp;
192 char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
193
194 if (!notify)
195 return;
196
197 (void)snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
198 if ((fp = popen(buf, "w")) == NULL)
199 return;
200
201 (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
202 if (lastmsg[0])
203 (void) fputs(lastmsg, fp);
204 if (message[0])
205 (void) fputs(message, fp);
206
207 (void) pclose(fp);
208 }
209
210 /*
211 * print out the timestamp string to stderr.
212 */
213 #define STAMP_LENGTH 80
214
215 static void
216 do_timestamp(time_t thistime, char *message)
217 {
218 struct tm tm_time;
219 char then[STAMP_LENGTH + 1];
220
221 (void) localtime_r(&thistime, &tm_time);
222 if (strftime(then, STAMP_LENGTH, time_string, &tm_time) == 0) {
223 time_string = default_time_string;
224 strftime(then, STAMP_LENGTH, time_string, &tm_time);
225 fprintf(stderr,
226 "DUMP: ERROR: TIMEFORMAT too long, reverting to default\n");
227 }
228
229 fprintf(stderr, message, then);
230 }
231
232
233 /*
234 * print out an estimate of the amount of time left to do the dump
235 */
236
237 time_t tschedule = 0;
238
239 void
240 timeest(void)
241 {
242 time_t tnow, deltat;
243
244 (void) time((time_t *) &tnow);
245 if (tnow >= tschedule) {
246 tschedule = tnow + 300;
247 if (blockswritten < 500)
248 return;
249 deltat = tstart_writing - tnow +
250 (1.0 * (tnow - tstart_writing))
251 / blockswritten * tapesize;
252
253 msg("%3.2f%% done, finished in %ld:%02ld",
254 (blockswritten * 100.0) / tapesize,
255 (long)(deltat / 3600), (long)((deltat % 3600) / 60));
256
257 if (timestamp == 1)
258 do_timestamp(tnow + deltat, " (at %s)");
259
260 fprintf(stderr, "\n");
261 }
262 }
263
264 void
265 msg(const char *fmt, ...)
266 {
267 time_t tnow;
268 va_list ap;
269
270 fprintf(stderr, " ");
271 if (timestamp == 1) {
272 (void) time((time_t *) &tnow);
273 do_timestamp(tnow, "[%s] ");
274 }
275
276 (void) fprintf(stderr,"DUMP: ");
277 #ifdef TDEBUG
278 (void) fprintf(stderr, "pid=%d ", getpid());
279 #endif
280 va_start(ap, fmt);
281 (void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap);
282 fputs(lastmsg, stderr);
283 (void) fflush(stdout);
284 (void) fflush(stderr);
285 va_end(ap);
286 }
287
288 void
289 msgtail(const char *fmt, ...)
290 {
291 va_list ap;
292
293 va_start(ap, fmt);
294 (void) vfprintf(stderr, fmt, ap);
295 va_end(ap);
296 }
297
298 void
299 quit(const char *fmt, ...)
300 {
301 va_list ap;
302
303 (void) fprintf(stderr," DUMP: ");
304 #ifdef TDEBUG
305 (void) fprintf(stderr, "pid=%d ", getpid());
306 #endif
307 va_start(ap, fmt);
308 (void) vfprintf(stderr, fmt, ap);
309 va_end(ap);
310 (void) fflush(stdout);
311 (void) fflush(stderr);
312 dumpabort(0);
313 }
314
315 /*
316 * Tell the operator what has to be done;
317 * we don't actually do it
318 */
319
320 struct fstab *
321 allocfsent(struct fstab *fs)
322 {
323 struct fstab *new;
324
325 new = (struct fstab *)xmalloc(sizeof (*fs));
326 new->fs_file = xstrdup(fs->fs_file);
327 new->fs_type = xstrdup(fs->fs_type);
328 new->fs_spec = xstrdup(fs->fs_spec);
329 new->fs_passno = fs->fs_passno;
330 new->fs_freq = fs->fs_freq;
331 return (new);
332 }
333
334 struct pfstab {
335 SLIST_ENTRY(pfstab) pf_list;
336 struct fstab *pf_fstab;
337 };
338
339 static SLIST_HEAD(, pfstab) table;
340
341 void
342 getfstab(void)
343 {
344 struct fstab *fs;
345 struct pfstab *pf;
346
347 if (setfsent() == 0) {
348 msg("Can't open %s for dump table information: %s\n",
349 _PATH_FSTAB, strerror(errno));
350 return;
351 }
352 while ((fs = getfsent()) != NULL) {
353 if (strcmp(fs->fs_type, FSTAB_RW) &&
354 strcmp(fs->fs_type, FSTAB_RO) &&
355 strcmp(fs->fs_type, FSTAB_RQ))
356 continue;
357 #ifdef DUMP_LFS
358 if (strcmp(fs->fs_vfstype, "lfs"))
359 continue;
360 #else
361 if (strcmp(fs->fs_vfstype, "ufs") &&
362 strcmp(fs->fs_vfstype, "ffs"))
363 continue;
364 #endif
365 fs = allocfsent(fs);
366 pf = (struct pfstab *)xmalloc(sizeof (*pf));
367 pf->pf_fstab = fs;
368 SLIST_INSERT_HEAD(&table, pf, pf_list);
369 }
370 (void) endfsent();
371 }
372
373 /*
374 * Search in the fstab for a file name.
375 * This file name can be either the special or the path file name.
376 *
377 * The entries in the fstab are the BLOCK special names, not the
378 * character special names.
379 * The caller of fstabsearch assures that the character device
380 * is dumped (that is much faster)
381 *
382 * The file name can omit the leading '/'.
383 */
384 struct fstab *
385 fstabsearch(const char *key)
386 {
387 struct pfstab *pf;
388 struct fstab *fs;
389 char *rn;
390
391 SLIST_FOREACH(pf, &table, pf_list) {
392 fs = pf->pf_fstab;
393 if (strcmp(fs->fs_file, key) == 0 ||
394 strcmp(fs->fs_spec, key) == 0)
395 return (fs);
396 rn = rawname(fs->fs_spec);
397 if (rn != NULL && strcmp(rn, key) == 0)
398 return (fs);
399 if (key[0] != '/') {
400 if (*fs->fs_spec == '/' &&
401 strcmp(fs->fs_spec + 1, key) == 0)
402 return (fs);
403 if (*fs->fs_file == '/' &&
404 strcmp(fs->fs_file + 1, key) == 0)
405 return (fs);
406 }
407 }
408 return (NULL);
409 }
410
411 /*
412 * Search in the mounted file list for a file name.
413 * This file name can be either the special or the path file name.
414 *
415 * The entries in the list are the BLOCK special names, not the
416 * character special names.
417 * The caller of mntinfosearch assures that the character device
418 * is dumped (that is much faster)
419 */
420 struct statfs *
421 mntinfosearch(const char *key)
422 {
423 int i, mntbufc;
424 struct statfs *mntbuf, *fs;
425 char *rn;
426
427 if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
428 quit("Can't get mount list: %s", strerror(errno));
429 for (fs = mntbuf, i = 0; i < mntbufc; i++, fs++) {
430 if (strcmp(fs->f_fstypename, "ufs") != 0 &&
431 strcmp(fs->f_fstypename, "ffs") != 0)
432 continue;
433 if (strcmp(fs->f_mntonname, key) == 0 ||
434 strcmp(fs->f_mntfromname, key) == 0)
435 return (fs);
436 rn = rawname(fs->f_mntfromname);
437 if (rn != NULL && strcmp(rn, key) == 0)
438 return (fs);
439 }
440 return (NULL);
441 }
442
443
444 /*
445 * Tell the operator what to do.
446 * arg: w ==> just what to do; W ==> most recent dumps
447 */
448 void
449 lastdump(char arg)
450 {
451 int i;
452 struct fstab *dt;
453 struct dumpdates *dtwalk;
454 char *lastname, *date;
455 int dumpme;
456 time_t tnow;
457
458 (void) time(&tnow);
459 getfstab(); /* /etc/fstab input */
460 initdumptimes(); /* /etc/dumpdates input */
461 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
462
463 if (arg == 'w')
464 (void) printf("Dump these file systems:\n");
465 else
466 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
467 lastname = "??";
468 ITITERATE(i, dtwalk) {
469 if (strncmp(lastname, dtwalk->dd_name,
470 sizeof(dtwalk->dd_name)) == 0)
471 continue;
472 date = (char *)ctime(&dtwalk->dd_ddate);
473 date[24] = '\0';
474 strcpy(date + 16, date + 19); /* blast away seconds */
475 lastname = dtwalk->dd_name;
476 dt = fstabsearch(dtwalk->dd_name);
477 dumpme = (dt != NULL &&
478 dt->fs_freq != 0 &&
479 dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
480 if (arg != 'w' || dumpme)
481 (void) printf(
482 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
483 dumpme && (arg != 'w') ? '>' : ' ',
484 dtwalk->dd_name,
485 dt ? dt->fs_file : "",
486 dtwalk->dd_level,
487 date);
488 }
489 }
490
491 int
492 datesort(const void *a1, const void *a2)
493 {
494 struct dumpdates *d1 = *(struct dumpdates **)a1;
495 struct dumpdates *d2 = *(struct dumpdates **)a2;
496 int diff;
497
498 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
499 if (diff == 0)
500 return (d2->dd_ddate - d1->dd_ddate);
501 return (diff);
502 }
503