]> git.wh0rd.org - dump.git/blob - dump/optr.c
Look also in /etc/mtab for filesystems...
[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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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.33 2002/12/12 11:49:35 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 #include <time.h>
52
53 #include <errno.h>
54 #include <mntent.h>
55 #include <paths.h>
56 #include <grp.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <stdarg.h>
61 #include <unistd.h>
62 #include <utmp.h>
63 #include <sys/stat.h>
64
65 #ifdef __linux__
66 #ifdef HAVE_EXT2FS_EXT2_FS_H
67 #include <ext2fs/ext2_fs.h>
68 #else
69 #include <linux/ext2_fs.h>
70 #endif
71 #include <ext2fs/ext2fs.h>
72 #include <bsdcompat.h>
73 #include <signal.h>
74 #endif
75
76 #include "dump.h"
77 #include "pathnames.h"
78 #include "bylabel.h"
79
80 static void alarmcatch __P((int));
81 int datesort __P((const void *, const void *));
82 static void sendmes __P((const char *, const char *));
83
84 /* List of filesystem types that we can dump (same ext2 on-disk format) */
85 static char *fstypes[] = { "ext2", "ext3", "InterMezzo", NULL };
86
87 /*
88 * Query the operator; This previously-fascist piece of code
89 * no longer requires an exact response.
90 * It is intended to protect dump aborting by inquisitive
91 * people banging on the console terminal to see what is
92 * happening which might cause dump to croak, destroying
93 * a large number of hours of work.
94 *
95 * Every 2 minutes we reprint the message, alerting others
96 * that dump needs attention.
97 */
98 static int timeout;
99 static const char *attnmessage; /* attention message */
100
101 int
102 query(const char *question)
103 {
104 char replybuffer[64];
105 int back, errcount;
106 FILE *mytty;
107 time_t firstprompt, when_answered;
108
109 if (qflag) {
110 msg("%s - forced abort\n", question);
111 dumpabort(0);
112 /* NOTREACHED */
113 }
114
115 firstprompt = time(NULL);
116
117 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
118 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
119 attnmessage = question;
120 timeout = 0;
121 alarmcatch(0);
122 back = -1;
123 errcount = 0;
124 do {
125 if (fgets(replybuffer, 63, mytty) == NULL) {
126 clearerr(mytty);
127 if (++errcount > 30) /* XXX ugly */
128 quit("excessive operator query failures\n");
129 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
130 back = 1;
131 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
132 back = 0;
133 } else {
134 (void) fprintf(stderr,
135 " DUMP: \"Yes\" or \"No\"?\n");
136 (void) fprintf(stderr,
137 " DUMP: %s: (\"yes\" or \"no\") ", question);
138 }
139 } while (back < 0);
140
141 /*
142 * Turn off the alarm, and reset the signal to trap out..
143 */
144 (void) alarm(0);
145 if (signal(SIGALRM, sig) == SIG_IGN)
146 signal(SIGALRM, SIG_IGN);
147 (void) fclose(mytty);
148 when_answered = time(NULL);
149 /*
150 * Adjust the base for time estimates to ignore time we spent waiting
151 * for operator input.
152 */
153 if (tstart_writing != 0)
154 tstart_writing += (when_answered - firstprompt);
155 return(back);
156 }
157
158 char lastmsg[BUFSIZ];
159
160 /*
161 * Alert the console operator, and enable the alarm clock to
162 * sleep for 2 minutes in case nobody comes to satisfy dump
163 */
164 static void
165 alarmcatch(UNUSED(int signo))
166 {
167 int save_errno = errno;
168 if (notify == 0) {
169 if (timeout == 0)
170 (void) fprintf(stderr,
171 " DUMP: %s: (\"yes\" or \"no\") ",
172 attnmessage);
173 else
174 msgtail("\7\7");
175 } else {
176 if (timeout) {
177 msgtail("\n");
178 broadcast(""); /* just print last msg */
179 }
180 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
181 attnmessage);
182 }
183 signal(SIGALRM, alarmcatch);
184 (void) alarm(120);
185 timeout = 1;
186 errno = save_errno;
187 }
188
189 /*
190 * Here if an inquisitive operator interrupts the dump program
191 */
192 void
193 interrupt(UNUSED(int signo))
194 {
195 msg("Interrupt received.\n");
196 if (query("Do you want to abort dump?"))
197 dumpabort(0);
198 }
199
200 /*
201 * The following variables and routines manage alerting
202 * operators to the status of dump.
203 * This works much like wall(1) does.
204 */
205 struct group *gp;
206
207 /*
208 * Get the names from the group entry "operator" to notify.
209 */
210 void
211 set_operators(void)
212 {
213 if (!notify) /*not going to notify*/
214 return;
215 gp = getgrnam(OPGRENT);
216 (void) endgrent();
217 if (gp == NULL) {
218 msg("No group entry for %s.\n", OPGRENT);
219 notify = 0;
220 return;
221 }
222 }
223
224 struct tm *localclock;
225
226 /*
227 * We fork a child to do the actual broadcasting, so
228 * that the process control groups are not messed up
229 */
230 void
231 broadcast(const char *message)
232 {
233 time_t clock;
234 FILE *f_utmp;
235 struct utmp utmp;
236 char **np;
237 int pid, s;
238
239 if (!notify || gp == NULL)
240 return;
241
242 switch (pid = fork()) {
243 case -1:
244 return;
245 case 0:
246 break;
247 default:
248 while (wait(&s) != pid)
249 continue;
250 return;
251 }
252
253 clock = time(NULL);
254 localclock = localtime(&clock);
255
256 if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
257 msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
258 return;
259 }
260
261 while (!feof(f_utmp)) {
262 if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
263 break;
264 if (utmp.ut_name[0] == 0)
265 continue;
266 for (np = gp->gr_mem; *np; np++) {
267 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
268 continue;
269 /*
270 * Do not send messages to operators on dialups
271 */
272 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
273 continue;
274 #ifdef DEBUG
275 msg("Message to %s at %s\n", *np, utmp.ut_line);
276 #endif
277 sendmes(utmp.ut_line, message);
278 }
279 }
280 (void) fclose(f_utmp);
281 Exit(0); /* the wait in this same routine will catch this */
282 /* NOTREACHED */
283 }
284
285 static void
286 sendmes(const char *tty, const char *message)
287 {
288 char t[MAXPATHLEN], buf[BUFSIZ];
289 const char *cp;
290 int lmsg = 1;
291 FILE *f_tty;
292
293 (void) strcpy(t, _PATH_DEV);
294 (void) strncat(t, tty, sizeof t - strlen(_PATH_DEV) - 1);
295
296 if ((f_tty = fopen(t, "w")) != NULL) {
297 setbuf(f_tty, buf);
298 (void) fprintf(f_tty,
299 "\n\
300 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
301 DUMP: NEEDS ATTENTION: ",
302 localclock->tm_hour, localclock->tm_min);
303 for (cp = lastmsg; ; cp++) {
304 if (*cp == '\0') {
305 if (lmsg) {
306 cp = message;
307 if (!(cp && *cp != '\0'))
308 break;
309 lmsg = 0;
310 } else
311 break;
312 }
313 if (*cp == '\n')
314 (void) putc('\r', f_tty);
315 (void) putc(*cp, f_tty);
316 }
317 (void) fclose(f_tty);
318 }
319 }
320
321 /*
322 * print out an estimate of the amount of time left to do the dump
323 */
324
325 time_t tschedule = 0;
326
327 void
328 timeest(void)
329 {
330 time_t tnow = time(NULL);
331
332 if (tnow >= tschedule) {
333 char *buf = mktimeest(tnow);
334 tschedule = tnow + 300;
335 if (buf) {
336 fprintf(stderr, " DUMP: ");
337 fwrite(buf, strlen(buf), 1, stderr);
338 fflush(stderr);
339 }
340 }
341 }
342
343 void
344 #ifdef __STDC__
345 msg(const char *fmt, ...)
346 #else
347 msg(fmt, va_alist)
348 char *fmt;
349 va_dcl
350 #endif
351 {
352 va_list ap;
353
354 (void) fprintf(stderr," DUMP: ");
355 #ifdef TDEBUG
356 (void) fprintf(stderr, "pid=%d ", getpid());
357 #endif
358 #ifdef __STDC__
359 va_start(ap, fmt);
360 #else
361 va_start(ap);
362 #endif
363 (void) vfprintf(stderr, fmt, ap);
364 va_end(ap);
365 (void) fflush(stdout);
366 (void) fflush(stderr);
367 #ifdef __STDC__
368 va_start(ap, fmt);
369 #else
370 va_start(ap);
371 #endif
372 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
373 va_end(ap);
374 }
375
376 void
377 #ifdef __STDC__
378 msgtail(const char *fmt, ...)
379 #else
380 msgtail(fmt, va_alist)
381 char *fmt;
382 va_dcl
383 #endif
384 {
385 va_list ap;
386 #ifdef __STDC__
387 va_start(ap, fmt);
388 #else
389 va_start(ap);
390 #endif
391 (void) vfprintf(stderr, fmt, ap);
392 va_end(ap);
393 }
394
395 void
396 #ifdef __STDC__
397 quit(const char *fmt, ...)
398 #else
399 quit(fmt, va_alist)
400 char *fmt;
401 va_dcl
402 #endif
403 {
404 va_list ap;
405
406 (void) fprintf(stderr," DUMP: ");
407 #ifdef TDEBUG
408 (void) fprintf(stderr, "pid=%d ", getpid());
409 #endif
410 #ifdef __STDC__
411 va_start(ap, fmt);
412 #else
413 va_start(ap);
414 #endif
415 (void) vfprintf(stderr, fmt, ap);
416 va_end(ap);
417 (void) fflush(stdout);
418 (void) fflush(stderr);
419 dumpabort(0);
420 }
421
422 /*
423 * Tell the operator what has to be done;
424 * we don't actually do it
425 */
426
427 struct pfstab {
428 struct pfstab *pf_next;
429 struct dumpdates *pf_dd;
430 struct mntent *pf_mntent;
431 };
432
433 static struct pfstab *table;
434
435 static struct mntent *
436 allocfsent(struct mntent *fs)
437 {
438 struct mntent *new;
439 const char *disk;
440 struct stat buf, tabbuf;
441 struct pfstab *tabpf;
442 struct mntent *tabfs;
443
444 new = (struct mntent *)malloc(sizeof (*fs));
445 if (new == NULL)
446 quit("%s\n", strerror(errno));
447
448 /* Translade UUID=, LABEL= ... */
449 disk = get_device_name(fs->mnt_fsname);
450 if (disk == NULL)
451 quit("Cannot find a disk having %s\n", fs->mnt_fsname);
452
453 /* Discard non block devices */
454 if (stat(disk, &buf) != 0 || !S_ISBLK(buf.st_mode)) {
455 free(new);
456 return NULL;
457 }
458
459 /* Discard same major/minor devices */
460 for (tabpf = table; tabpf != NULL; tabpf = tabpf->pf_next) {
461 tabfs = tabpf->pf_mntent;
462 if (stat(tabfs->mnt_fsname, &tabbuf) != 0)
463 /* should not happen */
464 quit("Cannot access %s\n", tabfs->mnt_fsname);
465 if (tabbuf.st_rdev == buf.st_rdev) {
466 free(new);
467 return NULL;
468 }
469 }
470
471 if (strlen(fs->mnt_dir) > 1 && fs->mnt_dir[strlen(fs->mnt_dir) - 1] == '/')
472 fs->mnt_dir[strlen(fs->mnt_dir) - 1] = '\0';
473 if ((new->mnt_dir = strdup(fs->mnt_dir)) == NULL ||
474 (new->mnt_type = strdup(fs->mnt_type)) == NULL ||
475 (new->mnt_opts = strdup(fs->mnt_opts)) == NULL ||
476 (new->mnt_fsname = strdup(disk)) == NULL)
477 quit("%s\n", strerror(errno));
478 new->mnt_passno = fs->mnt_passno;
479 new->mnt_freq = fs->mnt_freq;
480 return (new);
481 }
482
483 void
484 getfstab(void)
485 {
486 struct mntent *fs;
487 struct pfstab *pf;
488 struct pfstab *pfold = NULL;
489 FILE *mntfp;
490 char *mnttables[] = { _PATH_MNTTAB, _PATH_MOUNTED, 0 };
491 int i;
492
493 for (i = 0; mnttables[i]; i++) {
494 mntfp = setmntent(mnttables[i], "r");
495 if (mntfp == NULL) {
496 msg("Can't open %s for dump table information: %s\n",
497 mnttables[i], strerror(errno));
498 continue;
499 }
500 while ((fs = getmntent(mntfp)) != NULL) {
501 fs = allocfsent(fs);
502 if (!fs)
503 continue;
504 fs->mnt_passno = 0;
505 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
506 quit("%s\n", strerror(errno));
507 pf->pf_mntent = fs;
508 pf->pf_next = NULL;
509
510 /* keep table in /etc/fstab order for use with -w and -W */
511 if (pfold) {
512 pfold->pf_next = pf;
513 pfold = pf;
514 } else
515 pfold = table = pf;
516 }
517 (void) endmntent(mntfp);
518 }
519 }
520
521 /*
522 * Search in the fstab for a file name.
523 * This file name can be either the special or the path file name.
524 *
525 * The entries in the fstab are the BLOCK special names, not the
526 * character special names.
527 * The caller of fstabsearch assures that the character device
528 * is dumped (that is much faster)
529 *
530 * The file name can omit the leading '/'.
531 */
532 struct mntent *
533 fstabsearch(const char *key)
534 {
535 struct pfstab *pf;
536 struct mntent *fs;
537 const char *rn;
538
539 for (pf = table; pf != NULL; pf = pf->pf_next) {
540 fs = pf->pf_mntent;
541 if (strcmp(fs->mnt_dir, key) == 0 ||
542 strcmp(fs->mnt_fsname, key) == 0)
543 return (fs);
544 rn = rawname(fs->mnt_fsname);
545 if (rn != NULL && strcmp(rn, key) == 0)
546 return (fs);
547 if (key[0] != '/') {
548 if (*fs->mnt_fsname == '/' &&
549 strcmp(fs->mnt_fsname + 1, key) == 0)
550 return (fs);
551 if (*fs->mnt_dir == '/' &&
552 strcmp(fs->mnt_dir + 1, key) == 0)
553 return (fs);
554 }
555 }
556 return (NULL);
557 }
558
559 #ifdef __linux__
560 struct mntent *
561 fstabsearchdir(const char *key, char *directory)
562 {
563 struct pfstab *pf;
564 struct mntent *fs;
565 struct mntent *found_fs = NULL;
566 unsigned int size = 0;
567 struct stat buf;
568
569 if (stat(key, &buf) == 0 && S_ISBLK(buf.st_mode))
570 return NULL;
571
572 for (pf = table; pf != NULL; pf = pf->pf_next) {
573 fs = pf->pf_mntent;
574 if (strlen(fs->mnt_dir) > size &&
575 strlen(key) > strlen(fs->mnt_dir) &&
576 strncmp(fs->mnt_dir, key, strlen(fs->mnt_dir)) == 0 &&
577 (key[strlen(fs->mnt_dir)] == '/' ||
578 fs->mnt_dir[strlen(fs->mnt_dir) - 1] == '/')) {
579 found_fs = fs;
580 size = strlen(fs->mnt_dir);
581 }
582 }
583 if (found_fs != NULL) {
584 /*
585 * Ok, we have found a fstab entry which matches the argument
586 * We have to split the argument name into:
587 * - a device name (from the fstab entry)
588 * - a directory name on this device
589 */
590 strcpy(directory, key + size);
591 }
592 return(found_fs);
593 }
594 #endif
595
596 static void
597 print_wmsg(char arg, int dumpme, const char *dev, int level,
598 const char *mtpt, time_t ddate)
599 {
600 #ifdef FDEBUG
601 printf("checking dev %s: lvl %d, mtpt %s\n", dev, level, mtpt);
602 #endif
603 if (!dumpme && arg == 'w')
604 return;
605
606 (void) printf("%c %8s\t(%6s) Last dump: ",
607 dumpme && (arg != 'w') ? '>' : ' ',
608 dev,
609 mtpt ? mtpt : "");
610
611 /*
612 * Check ddate > 365 to avoid issues with fs in stab but not dumpdates.
613 * Not a problem, because ddate is in seconds since the epoch anyways.
614 */
615 if (ddate > 365) {
616 char *date, *d;
617
618 date = (char *)ctime(&ddate);
619 d = strchr(date, '\n');
620 if (d) *d = '\0';
621 printf("Level %c, Date %s\n", level, date);
622 } else
623 printf("never\n");
624 }
625
626 /*
627 * Tell the operator what to do
628 */
629 void
630 lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */
631 {
632 struct pfstab *pf;
633 time_t tnow;
634
635 tnow = time(NULL);
636 getfstab(); /* /etc/fstab input */
637 initdumptimes(0); /* dumpdates input */
638 if (ddatev == NULL && table == NULL) {
639 (void) printf("No %s or %s file found\n",
640 _PATH_MNTTAB, _PATH_DUMPDATES);
641 return;
642 }
643
644 if (arg == 'w')
645 (void) printf("Dump these file systems:\n");
646 else
647 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
648
649 /* For files in dumpdates, get the last dump level and date */
650 if (ddatev != NULL) {
651 struct dumpdates *dtwalk = NULL;
652 int i;
653 char *lastname;
654
655 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
656
657 lastname = "??";
658 ITITERATE(i, dtwalk) {
659 struct mntent *dt;
660 if (strncmp(lastname, dtwalk->dd_name,
661 sizeof(dtwalk->dd_name)) == 0)
662 continue;
663 lastname = dtwalk->dd_name;
664 if ((dt = dtwalk->dd_fstab) != NULL) {
665 /* Overload fs_freq as dump level and
666 * fs_passno as date, because we can't
667 * change struct fstab format.
668 * A positive fs_freq means this
669 * filesystem needs to be dumped.
670 */
671 dt->mnt_passno = dtwalk->dd_ddate;
672 if (dt->mnt_freq > 0 && (dtwalk->dd_ddate <
673 tnow - (dt->mnt_freq * 86400)))
674 dt->mnt_freq = dtwalk->dd_level;
675 else
676 dt->mnt_freq = -dtwalk->dd_level;
677 #ifdef FDEBUG
678 printf("%s fs_freq set to %d\n", lastname,
679 dt->mnt_freq);
680 #endif
681 }
682 }
683 }
684
685 /* print in /etc/fstab order only those filesystem types we can dump */
686 for (pf = table; pf != NULL; pf = pf->pf_next) {
687 struct mntent *dt = pf->pf_mntent;
688 char **type;
689
690 for (type = fstypes; *type != NULL; type++) {
691 if (strncmp(dt->mnt_type, *type,
692 sizeof(dt->mnt_type)) == 0) {
693 const char *disk = get_device_name(dt->mnt_fsname);
694 print_wmsg(arg, dt->mnt_freq > 0,
695 disk ? disk : dt->mnt_fsname,
696 dt->mnt_freq < 0 ? -dt->mnt_freq :
697 dt->mnt_freq,
698 dt->mnt_dir,
699 dt->mnt_passno);
700 }
701 }
702 }
703
704 /* print in /etc/dumpdates order if not in /etc/fstab */
705 if (ddatev != NULL) {
706 struct dumpdates *dtwalk = NULL;
707 char *lastname;
708 int i;
709
710 lastname = "??";
711 ITITERATE(i, dtwalk) {
712 if (strncmp(lastname, dtwalk->dd_name,
713 sizeof(dtwalk->dd_name)) == 0 ||
714 dtwalk->dd_fstab != NULL)
715 continue;
716 lastname = dtwalk->dd_name;
717 print_wmsg(arg, 0, dtwalk->dd_name,
718 dtwalk->dd_level, NULL, dtwalk->dd_ddate);
719 }
720 }
721 }
722
723 int
724 datesort(const void *a1, const void *a2)
725 {
726 struct dumpdates *d1 = *(struct dumpdates **)a1;
727 struct dumpdates *d2 = *(struct dumpdates **)a2;
728 int diff;
729
730 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
731 if (diff == 0)
732 return (d2->dd_ddate - d1->dd_ddate);
733 return (diff);
734 }