]> git.wh0rd.org - dump.git/blob - dump/optr.c
Initial revision
[dump.git] / dump / optr.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994, 1995, 1996
5 *
6 */
7
8 /*-
9 * Copyright (c) 1980, 1988, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifndef lint
42 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/wait.h>
47 #include <sys/time.h>
48
49 #include <errno.h>
50 #include <fstab.h>
51 #include <grp.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #ifdef __STDC__
55 #include <stdlib.h>
56 #include <string.h>
57 #include <stdarg.h>
58 #endif
59 #include <tzfile.h>
60 #ifdef __STDC__
61 #include <unistd.h>
62 #endif
63 #include <utmp.h>
64 #ifndef __STDC__
65 #include <varargs.h>
66 #endif
67
68 #ifdef __linux__
69 #include <linux/ext2_fs.h>
70 #include <ext2fs/ext2fs.h>
71 #include <bsdcompat.h>
72 #endif
73
74 #include "dump.h"
75 #include "pathnames.h"
76
77 void alarmcatch __P((/* int, int */));
78 int datesort __P((const void *, const void *));
79 static void sendmes __P((char *, char *));
80
81 /*
82 * Query the operator; This previously-fascist piece of code
83 * no longer requires an exact response.
84 * It is intended to protect dump aborting by inquisitive
85 * people banging on the console terminal to see what is
86 * happening which might cause dump to croak, destroying
87 * a large number of hours of work.
88 *
89 * Every 2 minutes we reprint the message, alerting others
90 * that dump needs attention.
91 */
92 static int timeout;
93 static char *attnmessage; /* attention message */
94
95 int
96 query(question)
97 char *question;
98 {
99 char replybuffer[64];
100 int back, errcount;
101 FILE *mytty;
102
103 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
104 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
105 attnmessage = question;
106 timeout = 0;
107 alarmcatch();
108 back = -1;
109 errcount = 0;
110 do {
111 if (fgets(replybuffer, 63, mytty) == NULL) {
112 clearerr(mytty);
113 if (++errcount > 30) /* XXX ugly */
114 quit("excessive operator query failures\n");
115 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
116 back = 1;
117 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
118 back = 0;
119 } else {
120 (void) fprintf(stderr,
121 " DUMP: \"Yes\" or \"No\"?\n");
122 (void) fprintf(stderr,
123 " DUMP: %s: (\"yes\" or \"no\") ", question);
124 }
125 } while (back < 0);
126
127 /*
128 * Turn off the alarm, and reset the signal to trap out..
129 */
130 (void) alarm(0);
131 if (signal(SIGALRM, sig) == SIG_IGN)
132 signal(SIGALRM, SIG_IGN);
133 (void) fclose(mytty);
134 return(back);
135 }
136
137 char lastmsg[100];
138
139 /*
140 * Alert the console operator, and enable the alarm clock to
141 * sleep for 2 minutes in case nobody comes to satisfy dump
142 */
143 void
144 alarmcatch()
145 {
146 if (notify == 0) {
147 if (timeout == 0)
148 (void) fprintf(stderr,
149 " DUMP: %s: (\"yes\" or \"no\") ",
150 attnmessage);
151 else
152 msgtail("\7\7");
153 } else {
154 if (timeout) {
155 msgtail("\n");
156 broadcast(""); /* just print last msg */
157 }
158 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
159 attnmessage);
160 }
161 signal(SIGALRM, alarmcatch);
162 (void) alarm(120);
163 timeout = 1;
164 }
165
166 /*
167 * Here if an inquisitive operator interrupts the dump program
168 */
169 void
170 interrupt(signo)
171 int signo;
172 {
173 msg("Interrupt received.\n");
174 if (query("Do you want to abort dump?"))
175 dumpabort(0);
176 }
177
178 /*
179 * The following variables and routines manage alerting
180 * operators to the status of dump.
181 * This works much like wall(1) does.
182 */
183 struct group *gp;
184
185 /*
186 * Get the names from the group entry "operator" to notify.
187 */
188 void
189 set_operators()
190 {
191 if (!notify) /*not going to notify*/
192 return;
193 gp = getgrnam(OPGRENT);
194 (void) endgrent();
195 if (gp == NULL) {
196 msg("No group entry for %s.\n", OPGRENT);
197 notify = 0;
198 return;
199 }
200 }
201
202 struct tm *localclock;
203
204 /*
205 * We fork a child to do the actual broadcasting, so
206 * that the process control groups are not messed up
207 */
208 void
209 broadcast(message)
210 char *message;
211 {
212 time_t clock;
213 FILE *f_utmp;
214 struct utmp utmp;
215 char **np;
216 int pid, s;
217
218 if (!notify || gp == NULL)
219 return;
220
221 switch (pid = fork()) {
222 case -1:
223 return;
224 case 0:
225 break;
226 default:
227 while (wait(&s) != pid)
228 continue;
229 return;
230 }
231
232 clock = time((time_t *)0);
233 localclock = localtime(&clock);
234
235 if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
236 msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
237 return;
238 }
239
240 while (!feof(f_utmp)) {
241 if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
242 break;
243 if (utmp.ut_name[0] == 0)
244 continue;
245 for (np = gp->gr_mem; *np; np++) {
246 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
247 continue;
248 /*
249 * Do not send messages to operators on dialups
250 */
251 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
252 continue;
253 #ifdef DEBUG
254 msg("Message to %s at %s\n", *np, utmp.ut_line);
255 #endif
256 sendmes(utmp.ut_line, message);
257 }
258 }
259 (void) fclose(f_utmp);
260 Exit(0); /* the wait in this same routine will catch this */
261 /* NOTREACHED */
262 }
263
264 static void
265 sendmes(tty, message)
266 char *tty, *message;
267 {
268 char t[50], buf[BUFSIZ];
269 register char *cp;
270 int lmsg = 1;
271 FILE *f_tty;
272
273 (void) strcpy(t, _PATH_DEV);
274 (void) strcat(t, tty);
275
276 if ((f_tty = fopen(t, "w")) != NULL) {
277 setbuf(f_tty, buf);
278 (void) fprintf(f_tty,
279 "\n\
280 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
281 DUMP: NEEDS ATTENTION: ",
282 localclock->tm_hour, localclock->tm_min);
283 for (cp = lastmsg; ; cp++) {
284 if (*cp == '\0') {
285 if (lmsg) {
286 cp = message;
287 if (*cp == '\0')
288 break;
289 lmsg = 0;
290 } else
291 break;
292 }
293 if (*cp == '\n')
294 (void) putc('\r', f_tty);
295 (void) putc(*cp, f_tty);
296 }
297 (void) fclose(f_tty);
298 }
299 }
300
301 /*
302 * print out an estimate of the amount of time left to do the dump
303 */
304
305 time_t tschedule = 0;
306
307 void
308 timeest()
309 {
310 time_t tnow, deltat;
311
312 (void) time((time_t *) &tnow);
313 if (tnow >= tschedule) {
314 tschedule = tnow + 300;
315 if (blockswritten < 500)
316 return;
317 deltat = tstart_writing - tnow +
318 (1.0 * (tnow - tstart_writing))
319 / blockswritten * tapesize;
320 msg("%3.2f%% done, finished in %d:%02d\n",
321 (blockswritten * 100.0) / tapesize,
322 deltat / 3600, (deltat % 3600) / 60);
323 }
324 }
325
326 void
327 #if __STDC__
328 msg(const char *fmt, ...)
329 #else
330 msg(fmt, va_alist)
331 char *fmt;
332 va_dcl
333 #endif
334 {
335 va_list ap;
336
337 (void) fprintf(stderr," DUMP: ");
338 #ifdef TDEBUG
339 (void) fprintf(stderr, "pid=%d ", getpid());
340 #endif
341 #if __STDC__
342 va_start(ap, fmt);
343 #else
344 va_start(ap);
345 #endif
346 (void) vfprintf(stderr, fmt, ap);
347 (void) fflush(stdout);
348 (void) fflush(stderr);
349 (void) vsprintf(lastmsg, fmt, ap);
350 va_end(ap);
351 }
352
353 void
354 #if __STDC__
355 msgtail(const char *fmt, ...)
356 #else
357 msgtail(fmt, va_alist)
358 char *fmt;
359 va_dcl
360 #endif
361 {
362 va_list ap;
363 #if __STDC__
364 va_start(ap, fmt);
365 #else
366 va_start(ap);
367 #endif
368 (void) vfprintf(stderr, fmt, ap);
369 va_end(ap);
370 }
371
372 void
373 #if __STDC__
374 quit(const char *fmt, ...)
375 #else
376 quit(fmt, va_alist)
377 char *fmt;
378 va_dcl
379 #endif
380 {
381 va_list ap;
382
383 (void) fprintf(stderr," DUMP: ");
384 #ifdef TDEBUG
385 (void) fprintf(stderr, "pid=%d ", getpid());
386 #endif
387 #if __STDC__
388 va_start(ap, fmt);
389 #else
390 va_start(ap);
391 #endif
392 (void) vfprintf(stderr, fmt, ap);
393 va_end(ap);
394 (void) fflush(stdout);
395 (void) fflush(stderr);
396 dumpabort(0);
397 }
398
399 /*
400 * Tell the operator what has to be done;
401 * we don't actually do it
402 */
403
404 struct fstab *
405 allocfsent(fs)
406 register struct fstab *fs;
407 {
408 register struct fstab *new;
409
410 new = (struct fstab *)malloc(sizeof (*fs));
411 if (new == NULL ||
412 (new->fs_file = strdup(fs->fs_file)) == NULL ||
413 (new->fs_type = strdup(fs->fs_type)) == NULL ||
414 (new->fs_spec = strdup(fs->fs_spec)) == NULL)
415 quit("%s\n", strerror(errno));
416 new->fs_passno = fs->fs_passno;
417 new->fs_freq = fs->fs_freq;
418 return (new);
419 }
420
421 struct pfstab {
422 struct pfstab *pf_next;
423 struct fstab *pf_fstab;
424 };
425
426 static struct pfstab *table;
427
428 void
429 getfstab()
430 {
431 register struct fstab *fs;
432 register struct pfstab *pf;
433
434 if (setfsent() == 0) {
435 msg("Can't open %s for dump table information: %s\n",
436 _PATH_FSTAB, strerror(errno));
437 return;
438 }
439 while ((fs = getfsent()) != NULL) {
440 if (strcmp(fs->fs_type, FSTAB_RW) &&
441 strcmp(fs->fs_type, FSTAB_RO) &&
442 strcmp(fs->fs_type, FSTAB_RQ))
443 continue;
444 fs = allocfsent(fs);
445 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
446 quit("%s\n", strerror(errno));
447 pf->pf_fstab = fs;
448 pf->pf_next = table;
449 table = pf;
450 }
451 (void) endfsent();
452 }
453
454 /*
455 * Search in the fstab for a file name.
456 * This file name can be either the special or the path file name.
457 *
458 * The entries in the fstab are the BLOCK special names, not the
459 * character special names.
460 * The caller of fstabsearch assures that the character device
461 * is dumped (that is much faster)
462 *
463 * The file name can omit the leading '/'.
464 */
465 struct fstab *
466 fstabsearch(key)
467 char *key;
468 {
469 register struct pfstab *pf;
470 register struct fstab *fs;
471 char *rn;
472
473 for (pf = table; pf != NULL; pf = pf->pf_next) {
474 fs = pf->pf_fstab;
475 if (strcmp(fs->fs_file, key) == 0 ||
476 strcmp(fs->fs_spec, key) == 0)
477 return (fs);
478 rn = rawname(fs->fs_spec);
479 if (rn != NULL && strcmp(rn, key) == 0)
480 return (fs);
481 if (key[0] != '/') {
482 if (*fs->fs_spec == '/' &&
483 strcmp(fs->fs_spec + 1, key) == 0)
484 return (fs);
485 if (*fs->fs_file == '/' &&
486 strcmp(fs->fs_file + 1, key) == 0)
487 return (fs);
488 }
489 }
490 return (NULL);
491 }
492
493 #ifdef __linux__
494 struct fstab *
495 fstabsearchdir(key, directory)
496 char *key;
497 char *directory;
498 {
499 register struct pfstab *pf;
500 register struct fstab *fs;
501 register struct fstab *found_fs = NULL;
502 unsigned int size = 0;
503
504 for (pf = table; pf != NULL; pf = pf->pf_next) {
505 fs = pf->pf_fstab;
506 if (strlen(fs->fs_file) > size &&
507 strlen(key) > strlen(fs->fs_file) + 2 &&
508 strncmp(fs->fs_file, key, strlen(fs->fs_file)) == 0 &&
509 (key[strlen(fs->fs_file)] == '/' ||
510 fs->fs_file[strlen(fs->fs_file) - 1] == '/')) {
511 found_fs = fs;
512 size = strlen(fs->fs_file);
513 }
514 }
515 if (found_fs != NULL) {
516 /*
517 * Ok, we have found a fstab entry which matches the argument
518 * We have to split the argument name into:
519 * - a device name (from the fstab entry)
520 * - a directory name on this device
521 */
522 strcpy(directory, key + size);
523 }
524 return(found_fs);
525 }
526 #endif
527
528 /*
529 * Tell the operator what to do
530 */
531 void
532 lastdump(arg)
533 char arg; /* w ==> just what to do; W ==> most recent dumps */
534 {
535 register int i;
536 register struct fstab *dt;
537 register struct dumpdates *dtwalk;
538 char *lastname, *date;
539 int dumpme;
540 time_t tnow;
541
542 (void) time(&tnow);
543 getfstab(); /* /etc/fstab input */
544 initdumptimes(); /* /etc/dumpdates input */
545 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
546
547 if (arg == 'w')
548 (void) printf("Dump these file systems:\n");
549 else
550 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
551 lastname = "??";
552 ITITERATE(i, dtwalk) {
553 if (strncmp(lastname, dtwalk->dd_name,
554 sizeof(dtwalk->dd_name)) == 0)
555 continue;
556 date = (char *)ctime(&dtwalk->dd_ddate);
557 date[16] = '\0'; /* blast away seconds and year */
558 lastname = dtwalk->dd_name;
559 dt = fstabsearch(dtwalk->dd_name);
560 dumpme = (dt != NULL &&
561 dt->fs_freq != 0 &&
562 dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
563 if (arg != 'w' || dumpme)
564 (void) printf(
565 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
566 dumpme && (arg != 'w') ? '>' : ' ',
567 dtwalk->dd_name,
568 dt ? dt->fs_file : "",
569 dtwalk->dd_level,
570 date);
571 }
572 }
573
574 int
575 datesort(a1, a2)
576 const void *a1, *a2;
577 {
578 struct dumpdates *d1 = *(struct dumpdates **)a1;
579 struct dumpdates *d2 = *(struct dumpdates **)a2;
580 int diff;
581
582 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
583 if (diff == 0)
584 return (d2->dd_ddate - d1->dd_ddate);
585 return (diff);
586 }