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