]>
git.wh0rd.org - dump.git/blob - dump/optr.c
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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.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.21 2001/03/19 13:22:48 stelian Exp $";
48 #include <sys/param.h>
64 #include <linux/ext2_fs.h>
65 #include <ext2fs/ext2fs.h>
66 #include <bsdcompat.h>
72 #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 *));
79 /* List of filesystem types that we can dump (same ext2 on-disk format) */
80 static char *fstypes
[] = { "ext2", "ext3", "InterMezzo", NULL
};
83 * Query the operator; This previously-fascist piece of code
84 * no longer requires an exact response.
85 * It is intended to protect dump aborting by inquisitive
86 * people banging on the console terminal to see what is
87 * happening which might cause dump to croak, destroying
88 * a large number of hours of work.
90 * Every 2 minutes we reprint the message, alerting others
91 * that dump needs attention.
94 static const char *attnmessage
; /* attention message */
97 query(const char *question
)
102 time_t firstprompt
, when_answered
;
105 (void)time4(&(firstprompt
));
107 (void)time((time_t *)&(firstprompt
));
110 if ((mytty
= fopen(_PATH_TTY
, "r")) == NULL
)
111 quit("fopen on %s fails: %s\n", _PATH_TTY
, strerror(errno
));
112 attnmessage
= question
;
118 if (fgets(replybuffer
, 63, mytty
) == NULL
) {
120 if (++errcount
> 30) /* XXX ugly */
121 quit("excessive operator query failures\n");
122 } else if (replybuffer
[0] == 'y' || replybuffer
[0] == 'Y') {
124 } else if (replybuffer
[0] == 'n' || replybuffer
[0] == 'N') {
127 (void) fprintf(stderr
,
128 " DUMP: \"Yes\" or \"No\"?\n");
129 (void) fprintf(stderr
,
130 " DUMP: %s: (\"yes\" or \"no\") ", question
);
135 * Turn off the alarm, and reset the signal to trap out..
138 if (signal(SIGALRM
, sig
) == SIG_IGN
)
139 signal(SIGALRM
, SIG_IGN
);
140 (void) fclose(mytty
);
142 (void)time4(&(when_answered
));
144 (void)time((time_t *)&(when_answered
));
147 * Adjust the base for time estimates to ignore time we spent waiting
148 * for operator input.
150 if ((tstart_writing
!= 0) && (when_answered
!= (time_t)-1) && (firstprompt
!= (time_t)-1))
151 tstart_writing
+= (when_answered
- firstprompt
);
155 char lastmsg
[BUFSIZ
];
158 * Alert the console operator, and enable the alarm clock to
159 * sleep for 2 minutes in case nobody comes to satisfy dump
162 alarmcatch(int signo
)
164 int save_errno
= errno
;
167 (void) fprintf(stderr
,
168 " DUMP: %s: (\"yes\" or \"no\") ",
175 broadcast(""); /* just print last msg */
177 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
180 signal(SIGALRM
, alarmcatch
);
187 * Here if an inquisitive operator interrupts the dump program
192 msg("Interrupt received.\n");
193 if (query("Do you want to abort dump?"))
198 * The following variables and routines manage alerting
199 * operators to the status of dump.
200 * This works much like wall(1) does.
205 * Get the names from the group entry "operator" to notify.
210 if (!notify
) /*not going to notify*/
212 gp
= getgrnam(OPGRENT
);
215 msg("No group entry for %s.\n", OPGRENT
);
221 struct tm
*localclock
;
224 * We fork a child to do the actual broadcasting, so
225 * that the process control groups are not messed up
228 broadcast(const char *message
)
236 if (!notify
|| gp
== NULL
)
239 switch (pid
= fork()) {
245 while (wait(&s
) != pid
)
250 clock
= time((time_t *)0);
251 localclock
= localtime(&clock
);
253 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
254 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
258 while (!feof(f_utmp
)) {
259 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
261 if (utmp
.ut_name
[0] == 0)
263 for (np
= gp
->gr_mem
; *np
; np
++) {
264 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
267 * Do not send messages to operators on dialups
269 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
272 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
274 sendmes(utmp
.ut_line
, message
);
277 (void) fclose(f_utmp
);
278 Exit(0); /* the wait in this same routine will catch this */
283 sendmes(const char *tty
, const char *message
)
285 char t
[MAXPATHLEN
], buf
[BUFSIZ
];
286 register const char *cp
;
290 (void) strcpy(t
, _PATH_DEV
);
291 (void) strncat(t
, tty
, sizeof t
- strlen(_PATH_DEV
) - 1);
293 if ((f_tty
= fopen(t
, "w")) != NULL
) {
295 (void) fprintf(f_tty
,
297 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
298 DUMP: NEEDS ATTENTION: ",
299 localclock
->tm_hour
, localclock
->tm_min
);
300 for (cp
= lastmsg
; ; cp
++) {
304 if (!(cp
&& *cp
!= '\0'))
311 (void) putc('\r', f_tty
);
312 (void) putc(*cp
, f_tty
);
314 (void) fclose(f_tty
);
319 * print out an estimate of the amount of time left to do the dump
322 time_t tschedule
= 0;
332 (void) time((time_t *) &tnow
);
334 if (tnow
>= tschedule
) {
335 char *buf
= mktimeest(tnow
);
336 tschedule
= tnow
+ 300;
338 fprintf(stderr
, " DUMP: ");
339 fwrite(buf
, strlen(buf
), 1, stderr
);
347 msg(const char *fmt
, ...)
356 (void) fprintf(stderr
," DUMP: ");
358 (void) fprintf(stderr
, "pid=%d ", getpid());
365 (void) vfprintf(stderr
, fmt
, ap
);
367 (void) fflush(stdout
);
368 (void) fflush(stderr
);
374 (void) vsnprintf(lastmsg
, sizeof(lastmsg
), fmt
, ap
);
380 msgtail(const char *fmt
, ...)
382 msgtail(fmt
, va_alist
)
393 (void) vfprintf(stderr
, fmt
, ap
);
399 quit(const char *fmt
, ...)
408 (void) fprintf(stderr
," DUMP: ");
410 (void) fprintf(stderr
, "pid=%d ", getpid());
417 (void) vfprintf(stderr
, fmt
, ap
);
419 (void) fflush(stdout
);
420 (void) fflush(stderr
);
425 * Tell the operator what has to be done;
426 * we don't actually do it
429 static struct fstab
*
430 allocfsent(struct fstab
*fs
)
432 register struct fstab
*new;
434 new = (struct fstab
*)malloc(sizeof (*fs
));
436 quit("%s\n", strerror(errno
));
437 if (strlen(fs
->fs_file
) > 1 && fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')
438 fs
->fs_file
[strlen(fs
->fs_file
) - 1] = '\0';
439 if ((new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
440 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
441 (new->fs_vfstype
= strdup(fs
->fs_vfstype
)) == NULL
||
442 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
443 quit("%s\n", strerror(errno
));
444 new->fs_passno
= fs
->fs_passno
;
445 new->fs_freq
= fs
->fs_freq
;
450 struct pfstab
*pf_next
;
451 struct dumpdates
*pf_dd
;
452 struct fstab
*pf_fstab
;
455 static struct pfstab
*table
;
462 struct pfstab
*pfold
= NULL
;
464 if (setfsent() == 0) {
465 msg("Can't open %s for dump table information: %s\n",
466 _PATH_FSTAB
, strerror(errno
));
469 while ((fs
= getfsent()) != NULL
) {
470 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
471 strcmp(fs
->fs_type
, FSTAB_RO
) &&
472 strcmp(fs
->fs_type
, FSTAB_RQ
))
476 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
477 quit("%s\n", strerror(errno
));
481 /* keep table in /etc/fstab order for use with -w and -W */
493 * Search in the fstab for a file name.
494 * This file name can be either the special or the path file name.
496 * The entries in the fstab are the BLOCK special names, not the
497 * character special names.
498 * The caller of fstabsearch assures that the character device
499 * is dumped (that is much faster)
501 * The file name can omit the leading '/'.
504 fstabsearch(const char *key
)
506 register struct pfstab
*pf
;
507 register struct fstab
*fs
;
510 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
512 if (strcmp(fs
->fs_file
, key
) == 0 ||
513 strcmp(fs
->fs_spec
, key
) == 0)
515 rn
= rawname(fs
->fs_spec
);
516 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
519 if (*fs
->fs_spec
== '/' &&
520 strcmp(fs
->fs_spec
+ 1, key
) == 0)
522 if (*fs
->fs_file
== '/' &&
523 strcmp(fs
->fs_file
+ 1, key
) == 0)
532 fstabsearchdir(const char *key
, char *directory
)
534 register struct pfstab
*pf
;
535 register struct fstab
*fs
;
536 register struct fstab
*found_fs
= NULL
;
537 unsigned int size
= 0;
540 if (stat(key
, &buf
) == 0 && S_ISBLK(buf
.st_mode
))
543 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
545 if (strlen(fs
->fs_file
) > size
&&
546 strlen(key
) > strlen(fs
->fs_file
) + 1 &&
547 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
548 (key
[strlen(fs
->fs_file
)] == '/' ||
549 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
551 size
= strlen(fs
->fs_file
);
554 if (found_fs
!= NULL
) {
556 * Ok, we have found a fstab entry which matches the argument
557 * We have to split the argument name into:
558 * - a device name (from the fstab entry)
559 * - a directory name on this device
561 strcpy(directory
, key
+ size
);
568 print_wmsg(char arg
, int dumpme
, const char *dev
, int level
,
569 const char *mtpt
, time_t ddate
)
575 date
= (char *)ctime(&ddate
);
576 d
= strchr(date
, '\n');
580 if (!dumpme
&& arg
== 'w')
583 (void) printf("%c %8s\t(%6s) Last dump: ",
584 dumpme
&& (arg
!= 'w') ? '>' : ' ',
589 printf("Level %c, Date %s\n",
596 * Tell the operator what to do
599 lastdump(char arg
) /* w ==> just what to do; W ==> most recent dumps */
605 getfstab(); /* /etc/fstab input */
606 initdumptimes(0); /* dumpdates input */
607 if (ddatev
== NULL
&& table
== NULL
) {
608 (void) printf("No %s or %s file found\n",
609 _PATH_FSTAB
, _PATH_DUMPDATES
);
614 (void) printf("Dump these file systems:\n");
616 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
618 if (ddatev
!= NULL
) {
619 struct dumpdates
*dtwalk
= NULL
;
623 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
626 ITITERATE(i
, dtwalk
) {
628 if (strncmp(lastname
, dtwalk
->dd_name
,
629 sizeof(dtwalk
->dd_name
)) == 0)
631 lastname
= dtwalk
->dd_name
;
632 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 (dt
->fs_freq
> 0 && (dtwalk
->dd_ddate
<
641 tnow
- (dt
->fs_freq
* 86400)))
642 dt
->fs_freq
= -dtwalk
->dd_level
- 1;
644 dt
->fs_freq
= dtwalk
->dd_level
+ 1;
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,
660 disk
? disk
: dt
->fs_spec
,
661 dt
->fs_freq
< 0 ? -dt
->fs_freq
- 1 : dt
->fs_freq
- 1,
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
);