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
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
44 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94";
46 static const char rcsid[] =
47 "$Id: optr.c,v 1.4 1999/10/11 13:08:08 stelian Exp $";
50 #include <sys/param.h>
66 #include <linux/ext2_fs.h>
67 #include <ext2fs/ext2fs.h>
68 #include <bsdcompat.h>
73 #include "pathnames.h"
75 static void alarmcatch __P((int));
76 int datesort __P((const void *, const void *));
77 static void sendmes __P((const char *, const char *));
80 * Query the operator; This previously-fascist piece of code
81 * no longer requires an exact response.
82 * It is intended to protect dump aborting by inquisitive
83 * people banging on the console terminal to see what is
84 * happening which might cause dump to croak, destroying
85 * a large number of hours of work.
87 * Every 2 minutes we reprint the message, alerting others
88 * that dump needs attention.
91 static const char *attnmessage; /* attention message */
94 query(const char *question)
99 time_t firstprompt, when_answered;
102 (void)time4(&(firstprompt));
104 (void)time((time_t *)&(firstprompt));
107 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
108 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
109 attnmessage = question;
115 if (fgets(replybuffer, 63, mytty) == NULL) {
117 if (++errcount > 30) /* XXX ugly */
118 quit("excessive operator query failures\n");
119 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
121 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
124 (void) fprintf(stderr,
125 " DUMP: \"Yes\" or \"No\"?\n");
126 (void) fprintf(stderr,
127 " DUMP: %s: (\"yes\" or \"no\") ", question);
132 * Turn off the alarm, and reset the signal to trap out..
135 if (signal(SIGALRM, sig) == SIG_IGN)
136 signal(SIGALRM, SIG_IGN);
137 (void) fclose(mytty);
139 (void)time4(&(when_answered));
141 (void)time((time_t *)&(when_answered));
144 * Adjust the base for time estimates to ignore time we spent waiting
145 * for operator input.
147 if ((tstart_writing != 0) && (when_answered != (time_t)-1) && (firstprompt != (time_t)-1))
148 tstart_writing += (when_answered - firstprompt);
152 char lastmsg[BUFSIZ];
155 * Alert the console operator, and enable the alarm clock to
156 * sleep for 2 minutes in case nobody comes to satisfy dump
159 alarmcatch(int signo)
161 int save_errno = errno;
164 (void) fprintf(stderr,
165 " DUMP: %s: (\"yes\" or \"no\") ",
172 broadcast(""); /* just print last msg */
174 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
177 signal(SIGALRM, alarmcatch);
184 * Here if an inquisitive operator interrupts the dump program
189 msg("Interrupt received.\n");
190 if (query("Do you want to abort dump?"))
195 * The following variables and routines manage alerting
196 * operators to the status of dump.
197 * This works much like wall(1) does.
202 * Get the names from the group entry "operator" to notify.
207 if (!notify) /*not going to notify*/
209 gp = getgrnam(OPGRENT);
212 msg("No group entry for %s.\n", OPGRENT);
218 struct tm *localclock;
221 * We fork a child to do the actual broadcasting, so
222 * that the process control groups are not messed up
225 broadcast(const char *message)
233 if (!notify || gp == NULL)
236 switch (pid = fork()) {
242 while (wait(&s) != pid)
247 clock = time((time_t *)0);
248 localclock = localtime(&clock);
250 if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
251 msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
255 while (!feof(f_utmp)) {
256 if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
258 if (utmp.ut_name[0] == 0)
260 for (np = gp->gr_mem; *np; np++) {
261 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
264 * Do not send messages to operators on dialups
266 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
269 msg("Message to %s at %s\n", *np, utmp.ut_line);
271 sendmes(utmp.ut_line, message);
274 (void) fclose(f_utmp);
275 Exit(0); /* the wait in this same routine will catch this */
280 sendmes(const char *tty, const char *message)
282 char t[MAXPATHLEN], buf[BUFSIZ];
283 register const char *cp;
287 (void) strcpy(t, _PATH_DEV);
288 (void) strncat(t, tty, sizeof t - strlen(_PATH_DEV) - 1);
290 if ((f_tty = fopen(t, "w")) != NULL) {
292 (void) fprintf(f_tty,
294 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
295 DUMP: NEEDS ATTENTION: ",
296 localclock->tm_hour, localclock->tm_min);
297 for (cp = lastmsg; ; cp++) {
301 if (!(cp && *cp != '\0'))
308 (void) putc('\r', f_tty);
309 (void) putc(*cp, f_tty);
311 (void) fclose(f_tty);
316 * print out an estimate of the amount of time left to do the dump
319 time_t tschedule = 0;
329 (void) time((time_t *) &tnow);
331 if (tnow >= tschedule) {
332 tschedule = tnow + 300;
333 if (blockswritten < 500)
335 deltat = tstart_writing - tnow +
336 (1.0 * (tnow - tstart_writing))
337 / blockswritten * tapesize;
338 msg("%3.2f%% done, finished in %d:%02d\n",
339 (blockswritten * 100.0) / tapesize,
340 deltat / 3600, (deltat % 3600) / 60);
346 msg(const char *fmt, ...)
355 (void) fprintf(stderr," DUMP: ");
357 (void) fprintf(stderr, "pid=%d ", getpid());
364 (void) vfprintf(stderr, fmt, ap);
365 (void) fflush(stdout);
366 (void) fflush(stderr);
367 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
373 msgtail(const char *fmt, ...)
375 msgtail(fmt, va_alist)
386 (void) vfprintf(stderr, fmt, ap);
392 quit(const char *fmt, ...)
401 (void) fprintf(stderr," DUMP: ");
403 (void) fprintf(stderr, "pid=%d ", getpid());
410 (void) vfprintf(stderr, fmt, ap);
412 (void) fflush(stdout);
413 (void) fflush(stderr);
418 * Tell the operator what has to be done;
419 * we don't actually do it
422 static struct fstab *
423 allocfsent(struct fstab *fs)
425 register struct fstab *new;
427 new = (struct fstab *)malloc(sizeof (*fs));
429 (new->fs_file = strdup(fs->fs_file)) == NULL ||
430 (new->fs_type = strdup(fs->fs_type)) == NULL ||
431 (new->fs_spec = strdup(fs->fs_spec)) == NULL)
432 quit("%s\n", strerror(errno));
433 new->fs_passno = fs->fs_passno;
434 new->fs_freq = fs->fs_freq;
439 struct pfstab *pf_next;
440 struct fstab *pf_fstab;
443 static struct pfstab *table;
448 register struct fstab *fs;
449 register struct pfstab *pf;
451 if (setfsent() == 0) {
452 msg("Can't open %s for dump table information: %s\n",
453 _PATH_FSTAB, strerror(errno));
456 while ((fs = getfsent()) != NULL) {
457 if (strcmp(fs->fs_type, FSTAB_RW) &&
458 strcmp(fs->fs_type, FSTAB_RO) &&
459 strcmp(fs->fs_type, FSTAB_RQ))
462 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
463 quit("%s\n", strerror(errno));
472 * Search in the fstab for a file name.
473 * This file name can be either the special or the path file name.
475 * The entries in the fstab are the BLOCK special names, not the
476 * character special names.
477 * The caller of fstabsearch assures that the character device
478 * is dumped (that is much faster)
480 * The file name can omit the leading '/'.
483 fstabsearch(const char *key)
485 register struct pfstab *pf;
486 register struct fstab *fs;
489 for (pf = table; pf != NULL; pf = pf->pf_next) {
491 if (strcmp(fs->fs_file, key) == 0 ||
492 strcmp(fs->fs_spec, key) == 0)
494 rn = rawname(fs->fs_spec);
495 if (rn != NULL && strcmp(rn, key) == 0)
498 if (*fs->fs_spec == '/' &&
499 strcmp(fs->fs_spec + 1, key) == 0)
501 if (*fs->fs_file == '/' &&
502 strcmp(fs->fs_file + 1, key) == 0)
511 fstabsearchdir(const char *key, char *directory)
513 register struct pfstab *pf;
514 register struct fstab *fs;
515 register struct fstab *found_fs = NULL;
516 unsigned int size = 0;
519 if (stat(key, &buf) == 0 && S_ISBLK(buf.st_mode))
522 for (pf = table; pf != NULL; pf = pf->pf_next) {
524 if (strlen(fs->fs_file) > size &&
525 strlen(key) > strlen(fs->fs_file) + 2 &&
526 strncmp(fs->fs_file, key, strlen(fs->fs_file)) == 0 &&
527 (key[strlen(fs->fs_file)] == '/' ||
528 fs->fs_file[strlen(fs->fs_file) - 1] == '/')) {
530 size = strlen(fs->fs_file);
533 if (found_fs != NULL) {
535 * Ok, we have found a fstab entry which matches the argument
536 * We have to split the argument name into:
537 * - a device name (from the fstab entry)
538 * - a directory name on this device
540 strcpy(directory, key + size);
547 * Tell the operator what to do
550 lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */
553 register struct fstab *dt;
554 register struct dumpdates *dtwalk=NULL;
555 char *lastname, *date;
560 getfstab(); /* /etc/fstab input */
561 initdumptimes(0); /* dumpdates input */
562 if (ddatev != NULL) {
563 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
566 (void) printf("Dump these file systems:\n");
568 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
570 ITITERATE(i, dtwalk) {
571 if (strncmp(lastname, dtwalk->dd_name,
572 sizeof(dtwalk->dd_name)) == 0)
574 date = (char *)ctime(&dtwalk->dd_ddate);
575 date[16] = '\0'; /* blast away seconds and year */
576 lastname = dtwalk->dd_name;
577 dt = fstabsearch(dtwalk->dd_name);
578 dumpme = (dt != NULL &&
580 dtwalk->dd_ddate < tnow - (dt->fs_freq * 86400));
581 if (arg != 'w' || dumpme)
583 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
584 dumpme && (arg != 'w') ? '>' : ' ',
586 dt ? dt->fs_file : "",
594 datesort(const void *a1, const void *a2)
596 struct dumpdates *d1 = *(struct dumpdates **)a1;
597 struct dumpdates *d2 = *(struct dumpdates **)a2;
600 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
602 return (d2->dd_ddate - d1->dd_ddate);