]>
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.26 2001/08/16 09:37:59 stelian Exp $";
48 #include <sys/param.h>
65 #ifdef HAVE_EXT2FS_EXT2_FS_H
66 #include <ext2fs/ext2_fs.h>
68 #include <linux/ext2_fs.h>
70 #include <ext2fs/ext2fs.h>
71 #include <bsdcompat.h>
77 #include "pathnames.h"
80 static void alarmcatch
__P((int));
81 int datesort
__P((const void *, const void *));
82 static void sendmes
__P((const char *, const char *));
84 /* List of filesystem types that we can dump (same ext2 on-disk format) */
85 static char *fstypes
[] = { "ext2", "ext3", "InterMezzo", NULL
};
88 * Query the operator; This previously-fascist piece of code
89 * no longer requires an exact response.
90 * It is intended to protect dump aborting by inquisitive
91 * people banging on the console terminal to see what is
92 * happening which might cause dump to croak, destroying
93 * a large number of hours of work.
95 * Every 2 minutes we reprint the message, alerting others
96 * that dump needs attention.
99 static const char *attnmessage
; /* attention message */
102 query(const char *question
)
104 char replybuffer
[64];
107 time_t firstprompt
, when_answered
;
110 msg("%s - forced abort\n", question
);
115 firstprompt
= time(NULL
);
117 if ((mytty
= fopen(_PATH_TTY
, "r")) == NULL
)
118 quit("fopen on %s fails: %s\n", _PATH_TTY
, strerror(errno
));
119 attnmessage
= question
;
125 if (fgets(replybuffer
, 63, mytty
) == NULL
) {
127 if (++errcount
> 30) /* XXX ugly */
128 quit("excessive operator query failures\n");
129 } else if (replybuffer
[0] == 'y' || replybuffer
[0] == 'Y') {
131 } else if (replybuffer
[0] == 'n' || replybuffer
[0] == 'N') {
134 (void) fprintf(stderr
,
135 " DUMP: \"Yes\" or \"No\"?\n");
136 (void) fprintf(stderr
,
137 " DUMP: %s: (\"yes\" or \"no\") ", question
);
142 * Turn off the alarm, and reset the signal to trap out..
145 if (signal(SIGALRM
, sig
) == SIG_IGN
)
146 signal(SIGALRM
, SIG_IGN
);
147 (void) fclose(mytty
);
148 when_answered
= time(NULL
);
150 * Adjust the base for time estimates to ignore time we spent waiting
151 * for operator input.
153 if (tstart_writing
!= 0)
154 tstart_writing
+= (when_answered
- firstprompt
);
158 char lastmsg
[BUFSIZ
];
161 * Alert the console operator, and enable the alarm clock to
162 * sleep for 2 minutes in case nobody comes to satisfy dump
165 alarmcatch(int signo
)
167 int save_errno
= errno
;
170 (void) fprintf(stderr
,
171 " DUMP: %s: (\"yes\" or \"no\") ",
178 broadcast(""); /* just print last msg */
180 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
183 signal(SIGALRM
, alarmcatch
);
190 * Here if an inquisitive operator interrupts the dump program
195 msg("Interrupt received.\n");
196 if (query("Do you want to abort dump?"))
201 * The following variables and routines manage alerting
202 * operators to the status of dump.
203 * This works much like wall(1) does.
208 * Get the names from the group entry "operator" to notify.
213 if (!notify
) /*not going to notify*/
215 gp
= getgrnam(OPGRENT
);
218 msg("No group entry for %s.\n", OPGRENT
);
224 struct tm
*localclock
;
227 * We fork a child to do the actual broadcasting, so
228 * that the process control groups are not messed up
231 broadcast(const char *message
)
239 if (!notify
|| gp
== NULL
)
242 switch (pid
= fork()) {
248 while (wait(&s
) != pid
)
254 localclock
= localtime(&clock
);
256 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
257 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
261 while (!feof(f_utmp
)) {
262 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
264 if (utmp
.ut_name
[0] == 0)
266 for (np
= gp
->gr_mem
; *np
; np
++) {
267 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
270 * Do not send messages to operators on dialups
272 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
275 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
277 sendmes(utmp
.ut_line
, message
);
280 (void) fclose(f_utmp
);
281 Exit(0); /* the wait in this same routine will catch this */
286 sendmes(const char *tty
, const char *message
)
288 char t
[MAXPATHLEN
], buf
[BUFSIZ
];
289 register const char *cp
;
293 (void) strcpy(t
, _PATH_DEV
);
294 (void) strncat(t
, tty
, sizeof t
- strlen(_PATH_DEV
) - 1);
296 if ((f_tty
= fopen(t
, "w")) != NULL
) {
298 (void) fprintf(f_tty
,
300 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
301 DUMP: NEEDS ATTENTION: ",
302 localclock
->tm_hour
, localclock
->tm_min
);
303 for (cp
= lastmsg
; ; cp
++) {
307 if (!(cp
&& *cp
!= '\0'))
314 (void) putc('\r', f_tty
);
315 (void) putc(*cp
, f_tty
);
317 (void) fclose(f_tty
);
322 * print out an estimate of the amount of time left to do the dump
325 time_t tschedule
= 0;
330 time_t tnow
= time(NULL
);
332 if (tnow
>= tschedule
) {
333 char *buf
= mktimeest(tnow
);
334 tschedule
= tnow
+ 300;
336 fprintf(stderr
, " DUMP: ");
337 fwrite(buf
, strlen(buf
), 1, stderr
);
345 msg(const char *fmt
, ...)
354 (void) fprintf(stderr
," DUMP: ");
356 (void) fprintf(stderr
, "pid=%d ", getpid());
363 (void) vfprintf(stderr
, fmt
, ap
);
365 (void) fflush(stdout
);
366 (void) fflush(stderr
);
372 (void) vsnprintf(lastmsg
, sizeof(lastmsg
), fmt
, ap
);
378 msgtail(const char *fmt
, ...)
380 msgtail(fmt
, va_alist
)
391 (void) vfprintf(stderr
, fmt
, ap
);
397 quit(const char *fmt
, ...)
406 (void) fprintf(stderr
," DUMP: ");
408 (void) fprintf(stderr
, "pid=%d ", getpid());
415 (void) vfprintf(stderr
, fmt
, ap
);
417 (void) fflush(stdout
);
418 (void) fflush(stderr
);
423 * Tell the operator what has to be done;
424 * we don't actually do it
427 static struct fstab
*
428 allocfsent(struct fstab
*fs
)
430 register struct fstab
*new;
432 new = (struct fstab
*)malloc(sizeof (*fs
));
434 quit("%s\n", strerror(errno
));
435 if (strlen(fs
->fs_file
) > 1 && fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')
436 fs
->fs_file
[strlen(fs
->fs_file
) - 1] = '\0';
437 if ((new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
438 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
439 (new->fs_vfstype
= strdup(fs
->fs_vfstype
)) == NULL
||
440 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
441 quit("%s\n", strerror(errno
));
442 new->fs_passno
= fs
->fs_passno
;
443 new->fs_freq
= fs
->fs_freq
;
448 struct pfstab
*pf_next
;
449 struct dumpdates
*pf_dd
;
450 struct fstab
*pf_fstab
;
453 static struct pfstab
*table
;
460 struct pfstab
*pfold
= NULL
;
462 if (setfsent() == 0) {
463 msg("Can't open %s for dump table information: %s\n",
464 _PATH_FSTAB
, strerror(errno
));
467 while ((fs
= getfsent()) != NULL
) {
468 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
469 strcmp(fs
->fs_type
, FSTAB_RO
) &&
470 strcmp(fs
->fs_type
, FSTAB_RQ
))
473 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
474 quit("%s\n", strerror(errno
));
478 /* keep table in /etc/fstab order for use with -w and -W */
490 * Search in the fstab for a file name.
491 * This file name can be either the special or the path file name.
493 * The entries in the fstab are the BLOCK special names, not the
494 * character special names.
495 * The caller of fstabsearch assures that the character device
496 * is dumped (that is much faster)
498 * The file name can omit the leading '/'.
501 fstabsearch(const char *key
)
503 register struct pfstab
*pf
;
504 register struct fstab
*fs
;
507 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
509 if (strcmp(fs
->fs_file
, key
) == 0 ||
510 strcmp(fs
->fs_spec
, key
) == 0)
512 rn
= rawname(fs
->fs_spec
);
513 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
516 if (*fs
->fs_spec
== '/' &&
517 strcmp(fs
->fs_spec
+ 1, key
) == 0)
519 if (*fs
->fs_file
== '/' &&
520 strcmp(fs
->fs_file
+ 1, key
) == 0)
529 fstabsearchdir(const char *key
, char *directory
)
531 register struct pfstab
*pf
;
532 register struct fstab
*fs
;
533 register struct fstab
*found_fs
= NULL
;
534 unsigned int size
= 0;
537 if (stat(key
, &buf
) == 0 && S_ISBLK(buf
.st_mode
))
540 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
542 if (strlen(fs
->fs_file
) > size
&&
543 strlen(key
) > strlen(fs
->fs_file
) + 1 &&
544 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
545 (key
[strlen(fs
->fs_file
)] == '/' ||
546 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
548 size
= strlen(fs
->fs_file
);
551 if (found_fs
!= NULL
) {
553 * Ok, we have found a fstab entry which matches the argument
554 * We have to split the argument name into:
555 * - a device name (from the fstab entry)
556 * - a directory name on this device
558 strcpy(directory
, key
+ size
);
565 print_wmsg(char arg
, int dumpme
, const char *dev
, int level
,
566 const char *mtpt
, time_t ddate
)
569 printf("checking dev %s: lvl %d, mtpt %s\n", dev
, level
, mtpt
);
571 if (!dumpme
&& arg
== 'w')
574 (void) printf("%c %8s\t(%6s) Last dump: ",
575 dumpme
&& (arg
!= 'w') ? '>' : ' ',
580 * Check ddate > 365 to avoid issues with fs in stab but not dumpdates.
581 * Not a problem, because ddate is in seconds since the epoch anyways.
586 date
= (char *)ctime(&ddate
);
587 d
= strchr(date
, '\n');
589 printf("Level %c, Date %s\n", level
, date
);
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 /* For files in dumpdates, get the last dump level and date */
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 positive 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
;
644 dt
->fs_freq
= -dtwalk
->dd_level
;
646 printf("%s fs_freq set to %d\n", lastname
,
653 /* print in /etc/fstab order only those filesystem types we can dump */
654 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
655 struct fstab
*dt
= pf
->pf_fstab
;
658 for (type
= fstypes
; *type
!= NULL
; type
++) {
659 if (strncmp(dt
->fs_vfstype
, *type
,
660 sizeof(dt
->fs_vfstype
)) == 0) {
661 const char *disk
= get_device_name(dt
->fs_spec
);
662 print_wmsg(arg
, dt
->fs_freq
> 0,
663 disk
? disk
: dt
->fs_spec
,
664 dt
->fs_freq
< 0 ? -dt
->fs_freq
:
672 /* print in /etc/dumpdates order if not in /etc/fstab */
673 if (ddatev
!= NULL
) {
674 struct dumpdates
*dtwalk
= NULL
;
679 ITITERATE(i
, dtwalk
) {
680 if (strncmp(lastname
, dtwalk
->dd_name
,
681 sizeof(dtwalk
->dd_name
)) == 0 ||
682 dtwalk
->dd_fstab
!= NULL
)
684 lastname
= dtwalk
->dd_name
;
685 print_wmsg(arg
, 0, dtwalk
->dd_name
,
686 dtwalk
->dd_level
, NULL
, dtwalk
->dd_ddate
);
692 datesort(const void *a1
, const void *a2
)
694 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
695 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
698 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
700 return (d2
->dd_ddate
- d1
->dd_ddate
);