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@cybercable.fr>, 1999-2000
6 * Stelian Pop <pop@cybercable.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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. 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.
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
43 static const char rcsid[] =
44 "$Id: optr.c,v 1.14 2000/11/10 14:42:25 stelian Exp $";
47 #include <sys/param.h>
63 #include <linux/ext2_fs.h>
64 #include <ext2fs/ext2fs.h>
65 #include <bsdcompat.h>
70 #include "pathnames.h"
73 static void alarmcatch __P((int));
74 int datesort __P((const void *, const void *));
75 static void sendmes __P((const char *, const char *));
77 /* List of filesystem types that we can dump (same ext2 on-disk format) */
78 static char *fstypes[] = { "ext2", "ext3", "InterMezzo", NULL };
81 * Query the operator; This previously-fascist piece of code
82 * no longer requires an exact response.
83 * It is intended to protect dump aborting by inquisitive
84 * people banging on the console terminal to see what is
85 * happening which might cause dump to croak, destroying
86 * a large number of hours of work.
88 * Every 2 minutes we reprint the message, alerting others
89 * that dump needs attention.
92 static const char *attnmessage; /* attention message */
95 query(const char *question)
100 time_t firstprompt, when_answered;
103 (void)time4(&(firstprompt));
105 (void)time((time_t *)&(firstprompt));
108 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
109 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
110 attnmessage = question;
116 if (fgets(replybuffer, 63, mytty) == NULL) {
118 if (++errcount > 30) /* XXX ugly */
119 quit("excessive operator query failures\n");
120 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
122 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
125 (void) fprintf(stderr,
126 " DUMP: \"Yes\" or \"No\"?\n");
127 (void) fprintf(stderr,
128 " DUMP: %s: (\"yes\" or \"no\") ", question);
133 * Turn off the alarm, and reset the signal to trap out..
136 if (signal(SIGALRM, sig) == SIG_IGN)
137 signal(SIGALRM, SIG_IGN);
138 (void) fclose(mytty);
140 (void)time4(&(when_answered));
142 (void)time((time_t *)&(when_answered));
145 * Adjust the base for time estimates to ignore time we spent waiting
146 * for operator input.
148 if ((tstart_writing != 0) && (when_answered != (time_t)-1) && (firstprompt != (time_t)-1))
149 tstart_writing += (when_answered - firstprompt);
153 char lastmsg[BUFSIZ];
156 * Alert the console operator, and enable the alarm clock to
157 * sleep for 2 minutes in case nobody comes to satisfy dump
160 alarmcatch(int signo)
162 int save_errno = errno;
165 (void) fprintf(stderr,
166 " DUMP: %s: (\"yes\" or \"no\") ",
173 broadcast(""); /* just print last msg */
175 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
178 signal(SIGALRM, alarmcatch);
185 * Here if an inquisitive operator interrupts the dump program
190 msg("Interrupt received.\n");
191 if (query("Do you want to abort dump?"))
196 * The following variables and routines manage alerting
197 * operators to the status of dump.
198 * This works much like wall(1) does.
203 * Get the names from the group entry "operator" to notify.
208 if (!notify) /*not going to notify*/
210 gp = getgrnam(OPGRENT);
213 msg("No group entry for %s.\n", OPGRENT);
219 struct tm *localclock;
222 * We fork a child to do the actual broadcasting, so
223 * that the process control groups are not messed up
226 broadcast(const char *message)
234 if (!notify || gp == NULL)
237 switch (pid = fork()) {
243 while (wait(&s) != pid)
248 clock = time((time_t *)0);
249 localclock = localtime(&clock);
251 if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
252 msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
256 while (!feof(f_utmp)) {
257 if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
259 if (utmp.ut_name[0] == 0)
261 for (np = gp->gr_mem; *np; np++) {
262 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
265 * Do not send messages to operators on dialups
267 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
270 msg("Message to %s at %s\n", *np, utmp.ut_line);
272 sendmes(utmp.ut_line, message);
275 (void) fclose(f_utmp);
276 Exit(0); /* the wait in this same routine will catch this */
281 sendmes(const char *tty, const char *message)
283 char t[MAXPATHLEN], buf[BUFSIZ];
284 register const char *cp;
288 (void) strcpy(t, _PATH_DEV);
289 (void) strncat(t, tty, sizeof t - strlen(_PATH_DEV) - 1);
291 if ((f_tty = fopen(t, "w")) != NULL) {
293 (void) fprintf(f_tty,
295 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
296 DUMP: NEEDS ATTENTION: ",
297 localclock->tm_hour, localclock->tm_min);
298 for (cp = lastmsg; ; cp++) {
302 if (!(cp && *cp != '\0'))
309 (void) putc('\r', f_tty);
310 (void) putc(*cp, f_tty);
312 (void) fclose(f_tty);
317 * print out an estimate of the amount of time left to do the dump
320 time_t tschedule = 0;
330 (void) time((time_t *) &tnow);
332 if (tnow >= tschedule) {
333 tschedule = tnow + 300;
334 if (blockswritten < 500)
336 if (blockswritten > tapesize)
337 tapesize = blockswritten;
338 deltat = tstart_writing - tnow +
339 (1.0 * (tnow - tstart_writing))
340 / blockswritten * tapesize;
341 msg("%3.2f%% done, finished in %d:%02d\n",
342 (blockswritten * 100.0) / tapesize,
343 deltat / 3600, (deltat % 3600) / 60);
349 msg(const char *fmt, ...)
358 (void) fprintf(stderr," DUMP: ");
360 (void) fprintf(stderr, "pid=%d ", getpid());
367 (void) vfprintf(stderr, fmt, ap);
369 (void) fflush(stdout);
370 (void) fflush(stderr);
376 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
382 msgtail(const char *fmt, ...)
384 msgtail(fmt, va_alist)
395 (void) vfprintf(stderr, fmt, ap);
401 quit(const char *fmt, ...)
410 (void) fprintf(stderr," DUMP: ");
412 (void) fprintf(stderr, "pid=%d ", getpid());
419 (void) vfprintf(stderr, fmt, ap);
421 (void) fflush(stdout);
422 (void) fflush(stderr);
427 * Tell the operator what has to be done;
428 * we don't actually do it
431 static struct fstab *
432 allocfsent(struct fstab *fs)
434 register struct fstab *new;
436 new = (struct fstab *)malloc(sizeof (*fs));
438 quit("%s\n", strerror(errno));
439 if (strlen(fs->fs_file) > 1 && fs->fs_file[strlen(fs->fs_file) - 1] == '/')
440 fs->fs_file[strlen(fs->fs_file) - 1] = '\0';
441 if ((new->fs_file = strdup(fs->fs_file)) == NULL ||
442 (new->fs_type = strdup(fs->fs_type)) == NULL ||
443 (new->fs_vfstype = strdup(fs->fs_vfstype)) == NULL ||
444 (new->fs_spec = strdup(fs->fs_spec)) == NULL)
445 quit("%s\n", strerror(errno));
446 new->fs_passno = fs->fs_passno;
447 new->fs_freq = fs->fs_freq;
452 struct pfstab *pf_next;
453 struct dumpdates *pf_dd;
454 struct fstab *pf_fstab;
457 static struct pfstab *table;
464 struct pfstab *pfold = NULL;
466 if (setfsent() == 0) {
467 msg("Can't open %s for dump table information: %s\n",
468 _PATH_FSTAB, strerror(errno));
471 while ((fs = getfsent()) != NULL) {
472 if (strcmp(fs->fs_type, FSTAB_RW) &&
473 strcmp(fs->fs_type, FSTAB_RO) &&
474 strcmp(fs->fs_type, FSTAB_RQ))
478 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
479 quit("%s\n", strerror(errno));
483 /* keep table in /etc/fstab order for use with -w and -W */
495 * Search in the fstab for a file name.
496 * This file name can be either the special or the path file name.
498 * The entries in the fstab are the BLOCK special names, not the
499 * character special names.
500 * The caller of fstabsearch assures that the character device
501 * is dumped (that is much faster)
503 * The file name can omit the leading '/'.
506 fstabsearch(const char *key)
508 register struct pfstab *pf;
509 register struct fstab *fs;
512 for (pf = table; pf != NULL; pf = pf->pf_next) {
514 if (strcmp(fs->fs_file, key) == 0 ||
515 strcmp(fs->fs_spec, key) == 0)
517 rn = rawname(fs->fs_spec);
518 if (rn != NULL && strcmp(rn, key) == 0)
521 if (*fs->fs_spec == '/' &&
522 strcmp(fs->fs_spec + 1, key) == 0)
524 if (*fs->fs_file == '/' &&
525 strcmp(fs->fs_file + 1, key) == 0)
534 fstabsearchdir(const char *key, char *directory)
536 register struct pfstab *pf;
537 register struct fstab *fs;
538 register struct fstab *found_fs = NULL;
539 unsigned int size = 0;
542 if (stat(key, &buf) == 0 && S_ISBLK(buf.st_mode))
545 for (pf = table; pf != NULL; pf = pf->pf_next) {
547 if (strlen(fs->fs_file) > size &&
548 strlen(key) > strlen(fs->fs_file) + 1 &&
549 strncmp(fs->fs_file, key, strlen(fs->fs_file)) == 0 &&
550 (key[strlen(fs->fs_file)] == '/' ||
551 fs->fs_file[strlen(fs->fs_file) - 1] == '/')) {
553 size = strlen(fs->fs_file);
556 if (found_fs != NULL) {
558 * Ok, we have found a fstab entry which matches the argument
559 * We have to split the argument name into:
560 * - a device name (from the fstab entry)
561 * - a directory name on this device
563 strcpy(directory, key + size);
570 print_wmsg(char arg, int dumpme, const char *dev, int level,
571 const char *mtpt, time_t ddate)
576 date = (char *)ctime(&ddate);
577 //date[16] = '\0'; /* blast away seconds and year */
579 if (!dumpme && arg == 'w')
582 (void) printf("%c %8s\t(%6s) Last dump: ",
583 dumpme && (arg != 'w') ? '>' : ' ',
588 printf("Level %c, Date %s\n",
595 * Tell the operator what to do
598 lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */
604 getfstab(); /* /etc/fstab input */
605 initdumptimes(0); /* dumpdates input */
606 if (ddatev == NULL && table == NULL) {
607 (void) printf("No %s or %s file found\n",
608 _PATH_FSTAB, _PATH_DUMPDATES);
613 (void) printf("Dump these file systems:\n");
615 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
617 if (ddatev != NULL) {
618 struct dumpdates *dtwalk = NULL;
622 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
625 ITITERATE(i, dtwalk) {
627 if (strncmp(lastname, dtwalk->dd_name,
628 sizeof(dtwalk->dd_name)) == 0)
630 lastname = dtwalk->dd_name;
631 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.
639 dt->fs_passno = dtwalk->dd_ddate;
640 if (dtwalk->dd_ddate <
641 tnow - (dt->fs_freq * 86400))
642 dt->fs_freq = -dtwalk->dd_level;
644 dt->fs_freq = dtwalk->dd_level;
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;
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 || !dt->fs_passno,
660 disk ? disk : dt->fs_spec,
661 dt->fs_freq < 0 ? -dt->fs_freq : dt->fs_freq,
668 /* print in /etc/dumpdates order if not in /etc/fstab */
669 if (ddatev != NULL) {
670 struct dumpdates *dtwalk = NULL;
675 ITITERATE(i, dtwalk) {
676 if (strncmp(lastname, dtwalk->dd_name,
677 sizeof(dtwalk->dd_name)) == 0 ||
678 dtwalk->dd_fstab != NULL)
680 lastname = dtwalk->dd_name;
681 print_wmsg(arg, 0, dtwalk->dd_name,
682 dtwalk->dd_level, NULL, dtwalk->dd_ddate);
688 datesort(const void *a1, const void *a2)
690 struct dumpdates *d1 = *(struct dumpdates **)a1;
691 struct dumpdates *d2 = *(struct dumpdates **)a2;
694 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
696 return (d2->dd_ddate - d1->dd_ddate);