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