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
10 * Copyright (c) 1980, 1988, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
39 static const char rcsid[] =
40 "$Id: optr.c,v 1.39 2004/07/05 15:12:45 stelian Exp $";
44 #include <sys/param.h>
62 #ifdef HAVE_EXT2FS_EXT2_FS_H
63 #include <ext2fs/ext2_fs.h>
65 #include <linux/ext2_fs.h>
67 #include <ext2fs/ext2fs.h>
68 #include <bsdcompat.h>
73 #include "pathnames.h"
76 static void alarmcatch __P((int));
77 int datesort __P((const void *, const void *));
78 static void sendmes __P((const char *, const char *));
80 /* List of filesystem types that we can dump (same ext2 on-disk format) */
81 static char *fstypes[] = { "ext2", "ext3", "InterMezzo", NULL };
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.
91 * Every 2 minutes we reprint the message, alerting others
92 * that dump needs attention.
95 static const char *attnmessage; /* attention message */
98 query(const char *question)
100 char replybuffer[64];
103 time_t firstprompt, when_answered;
106 msg("%s - forced abort\n", question);
111 firstprompt = time(NULL);
113 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
114 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
115 attnmessage = question;
121 if (fgets(replybuffer, 63, mytty) == NULL) {
123 if (++errcount > 30) /* XXX ugly */
124 quit("excessive operator query failures\n");
125 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
127 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
130 (void) fprintf(stderr,
131 " DUMP: \"Yes\" or \"No\"?\n");
132 (void) fprintf(stderr,
133 " DUMP: %s: (\"yes\" or \"no\") ", question);
138 * Turn off the alarm, and reset the signal to trap out..
141 if (signal(SIGALRM, sig) == SIG_IGN)
142 signal(SIGALRM, SIG_IGN);
143 (void) fclose(mytty);
144 when_answered = time(NULL);
146 * Adjust the base for time estimates to ignore time we spent waiting
147 * for operator input.
149 if (tstart_writing != 0)
150 tstart_writing += (when_answered - firstprompt);
154 char lastmsg[BUFSIZ];
157 * Alert the console operator, and enable the alarm clock to
158 * sleep for 2 minutes in case nobody comes to satisfy dump
161 alarmcatch(UNUSED(int signo))
163 int save_errno = errno;
166 (void) fprintf(stderr,
167 " DUMP: %s: (\"yes\" or \"no\") ",
174 broadcast(""); /* just print last msg */
176 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
179 signal(SIGALRM, alarmcatch);
186 * Here if an inquisitive operator interrupts the dump program
189 interrupt(UNUSED(int signo))
191 msg("Interrupt received.\n");
192 if (query("Do you want to abort dump?"))
197 * The following variables and routines manage alerting
198 * operators to the status of dump.
199 * This works much like wall(1) does.
204 * Get the names from the group entry "operator" to notify.
209 if (!notify) /*not going to notify*/
211 gp = getgrnam(OPGRENT);
214 msg("No group entry for %s.\n", OPGRENT);
220 struct tm *localclock;
223 * We fork a child to do the actual broadcasting, so
224 * that the process control groups are not messed up
227 broadcast(const char *message)
235 if (!notify || gp == NULL)
238 switch (pid = fork()) {
244 while (wait(&s) != pid)
250 localclock = localtime(&clock);
252 if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
253 msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
257 while (!feof(f_utmp)) {
258 if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
260 if (utmp.ut_name[0] == 0)
262 for (np = gp->gr_mem; *np; np++) {
263 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
266 * Do not send messages to operators on dialups
268 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
271 msg("Message to %s at %s\n", *np, utmp.ut_line);
273 sendmes(utmp.ut_line, message);
276 (void) fclose(f_utmp);
277 Exit(0); /* the wait in this same routine will catch this */
282 sendmes(const char *tty, const char *message)
284 char t[MAXPATHLEN], buf[BUFSIZ];
289 (void) strcpy(t, _PATH_DEV);
290 (void) strncat(t, tty, sizeof t - strlen(_PATH_DEV) - 1);
292 if ((f_tty = fopen(t, "w")) != NULL) {
294 (void) fprintf(f_tty,
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++) {
303 if (!(cp && *cp != '\0'))
310 (void) putc('\r', f_tty);
311 (void) putc(*cp, f_tty);
313 (void) fclose(f_tty);
318 * print out an estimate of the amount of time left to do the dump
321 time_t tschedule = 0;
326 time_t tnow = time(NULL);
328 if (tnow >= tschedule) {
329 char *buf = mktimeest(tnow);
330 tschedule = tnow + 300;
332 fprintf(stderr, " DUMP: ");
333 fwrite(buf, strlen(buf), 1, stderr);
341 msg(const char *fmt, ...)
350 (void) fprintf(stderr," DUMP: ");
352 (void) fprintf(stderr, "pid=%d ", getpid());
359 (void) vfprintf(stderr, fmt, ap);
361 (void) fflush(stdout);
362 (void) fflush(stderr);
368 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
374 msgtail(const char *fmt, ...)
376 msgtail(fmt, va_alist)
387 (void) vfprintf(stderr, fmt, ap);
393 quit(const char *fmt, ...)
402 (void) fprintf(stderr," DUMP: ");
404 (void) fprintf(stderr, "pid=%d ", getpid());
411 (void) vfprintf(stderr, fmt, ap);
413 (void) fflush(stdout);
414 (void) fflush(stderr);
419 * Tell the operator what has to be done;
420 * we don't actually do it
424 struct pfstab *pf_next;
425 struct dumpdates *pf_dd;
426 struct mntent *pf_mntent;
429 static struct pfstab *table;
431 static struct mntent *
432 allocfsent(struct mntent *fs)
436 struct stat buf, tabbuf;
437 struct pfstab *tabpf;
438 struct mntent *tabfs;
440 new = (struct mntent *)malloc(sizeof (*fs));
442 quit("%s\n", strerror(errno));
444 /* Translade UUID=, LABEL= ... */
445 disk = get_device_name(fs->mnt_fsname);
447 disk = strdup(fs->mnt_fsname);
449 /* Discard non block devices */
450 if (stat(disk, &buf) != 0 || !S_ISBLK(buf.st_mode)) {
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) {
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;
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;
490 struct pfstab *pfold = NULL;
492 char *mnttables[] = { _PATH_MOUNTED, _PATH_MNTTAB, 0 };
495 for (i = 0; mnttables[i]; i++) {
496 mntfp = setmntent(mnttables[i], "r");
498 msg("Can't open %s for dump table information: %s\n",
499 mnttables[i], strerror(errno));
502 while ((fs = getmntent(mntfp)) != NULL) {
507 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
508 quit("%s\n", strerror(errno));
512 /* keep table in /etc/fstab order for use with -w and -W */
519 (void) endmntent(mntfp);
524 * Search in the fstab for a file name.
525 * This file name can be either the special or the path file name.
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)
532 * The file name can omit the leading '/'.
535 fstabsearch(const char *key)
541 for (pf = table; pf != NULL; pf = pf->pf_next) {
543 if (strcmp(fs->mnt_dir, key) == 0 ||
544 strcmp(fs->mnt_fsname, key) == 0)
546 rn = rawname(fs->mnt_fsname);
547 if (rn != NULL && strcmp(rn, key) == 0)
550 if (*fs->mnt_fsname == '/' &&
551 strcmp(fs->mnt_fsname + 1, key) == 0)
553 if (*fs->mnt_dir == '/' &&
554 strcmp(fs->mnt_dir + 1, key) == 0)
563 fstabsearchdir(const char *key, char *directory)
567 struct mntent *found_fs = NULL;
568 unsigned int size = 0;
571 if (stat(key, &buf) == 0 && S_ISBLK(buf.st_mode))
574 for (pf = table; pf != NULL; pf = pf->pf_next) {
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] == '/')) {
582 size = strlen(fs->mnt_dir);
585 if (found_fs != NULL) {
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
592 strcpy(directory, key + size);
599 print_wmsg(char arg, int dumpme, const char *dev, int level,
600 const char *mtpt, time_t ddate)
603 printf("checking dev %s: lvl %d, mtpt %s\n", dev, level, mtpt);
605 if (!dumpme && arg == 'w')
608 (void) printf("%c %8s\t(%6s) Last dump: ",
609 dumpme && (arg != 'w') ? '>' : ' ',
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.
620 date = (char *)ctime(&ddate);
621 d = strchr(date, '\n');
623 printf("Level %d, Date %s\n", level, date);
629 * Tell the operator what to do
632 lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */
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);
647 (void) printf("Dump these file systems:\n");
649 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
651 /* For files in dumpdates, get the last dump level and date */
652 if (ddatev != NULL) {
653 struct dumpdates *dtwalk = NULL;
657 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
660 ITITERATE(i, dtwalk) {
662 if (strncmp(lastname, dtwalk->dd_name,
663 sizeof(dtwalk->dd_name)) == 0)
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.
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;
678 dt->mnt_freq = -dtwalk->dd_level;
680 printf("%s fs_freq set to %d\n", lastname,
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;
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 :
706 /* print in /etc/dumpdates order if not in /etc/fstab */
707 if (ddatev != NULL) {
708 struct dumpdates *dtwalk = NULL;
713 ITITERATE(i, dtwalk) {
714 if (strncmp(lastname, dtwalk->dd_name,
715 sizeof(dtwalk->dd_name)) == 0 ||
716 dtwalk->dd_fstab != NULL)
718 lastname = dtwalk->dd_name;
719 print_wmsg(arg, 0, dtwalk->dd_name,
720 dtwalk->dd_level, NULL, dtwalk->dd_ddate);
726 datesort(const void *a1, const void *a2)
728 struct dumpdates *d1 = *(struct dumpdates **)a1;
729 struct dumpdates *d2 = *(struct dumpdates **)a2;
732 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
734 return (d2->dd_ddate - d1->dd_ddate);