]>
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 <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.36 2003/03/30 15:40:36 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 quit("Cannot find a disk having %s\n", 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
) {
467 if (strlen(fs
->mnt_dir
) > 1 && fs
->mnt_dir
[strlen(fs
->mnt_dir
) - 1] == '/')
468 fs
->mnt_dir
[strlen(fs
->mnt_dir
) - 1] = '\0';
469 if ((new->mnt_dir
= strdup(fs
->mnt_dir
)) == NULL
||
470 (new->mnt_type
= strdup(fs
->mnt_type
)) == NULL
||
471 (new->mnt_opts
= strdup(fs
->mnt_opts
)) == NULL
||
472 (new->mnt_fsname
= strdup(disk
)) == NULL
)
473 quit("%s\n", strerror(errno
));
474 new->mnt_passno
= fs
->mnt_passno
;
475 new->mnt_freq
= fs
->mnt_freq
;
484 struct pfstab
*pfold
= NULL
;
486 char *mnttables
[] = { _PATH_MOUNTED
, _PATH_MNTTAB
, 0 };
489 for (i
= 0; mnttables
[i
]; i
++) {
490 mntfp
= setmntent(mnttables
[i
], "r");
492 msg("Can't open %s for dump table information: %s\n",
493 mnttables
[i
], strerror(errno
));
496 while ((fs
= getmntent(mntfp
)) != NULL
) {
501 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
502 quit("%s\n", strerror(errno
));
506 /* keep table in /etc/fstab order for use with -w and -W */
513 (void) endmntent(mntfp
);
518 * Search in the fstab for a file name.
519 * This file name can be either the special or the path file name.
521 * The entries in the fstab are the BLOCK special names, not the
522 * character special names.
523 * The caller of fstabsearch assures that the character device
524 * is dumped (that is much faster)
526 * The file name can omit the leading '/'.
529 fstabsearch(const char *key
)
535 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
537 if (strcmp(fs
->mnt_dir
, key
) == 0 ||
538 strcmp(fs
->mnt_fsname
, key
) == 0)
540 rn
= rawname(fs
->mnt_fsname
);
541 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
544 if (*fs
->mnt_fsname
== '/' &&
545 strcmp(fs
->mnt_fsname
+ 1, key
) == 0)
547 if (*fs
->mnt_dir
== '/' &&
548 strcmp(fs
->mnt_dir
+ 1, key
) == 0)
557 fstabsearchdir(const char *key
, char *directory
)
561 struct mntent
*found_fs
= NULL
;
562 unsigned int size
= 0;
565 if (stat(key
, &buf
) == 0 && S_ISBLK(buf
.st_mode
))
568 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
570 if (strlen(fs
->mnt_dir
) > size
&&
571 strlen(key
) > strlen(fs
->mnt_dir
) &&
572 strncmp(fs
->mnt_dir
, key
, strlen(fs
->mnt_dir
)) == 0 &&
573 (key
[strlen(fs
->mnt_dir
)] == '/' ||
574 fs
->mnt_dir
[strlen(fs
->mnt_dir
) - 1] == '/')) {
576 size
= strlen(fs
->mnt_dir
);
579 if (found_fs
!= NULL
) {
581 * Ok, we have found a fstab entry which matches the argument
582 * We have to split the argument name into:
583 * - a device name (from the fstab entry)
584 * - a directory name on this device
586 strcpy(directory
, key
+ size
);
593 print_wmsg(char arg
, int dumpme
, const char *dev
, int level
,
594 const char *mtpt
, time_t ddate
)
597 printf("checking dev %s: lvl %d, mtpt %s\n", dev
, level
, mtpt
);
599 if (!dumpme
&& arg
== 'w')
602 (void) printf("%c %8s\t(%6s) Last dump: ",
603 dumpme
&& (arg
!= 'w') ? '>' : ' ',
608 * Check ddate > 365 to avoid issues with fs in stab but not dumpdates.
609 * Not a problem, because ddate is in seconds since the epoch anyways.
614 date
= (char *)ctime(&ddate
);
615 d
= strchr(date
, '\n');
617 printf("Level %c, Date %s\n", level
, date
);
623 * Tell the operator what to do
626 lastdump(char arg
) /* w ==> just what to do; W ==> most recent dumps */
632 getfstab(); /* /etc/fstab input */
633 initdumptimes(0); /* dumpdates input */
634 if (ddatev
== NULL
&& table
== NULL
) {
635 (void) printf("No %s or %s file found\n",
636 _PATH_MNTTAB
, dumpdates
);
641 (void) printf("Dump these file systems:\n");
643 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
645 /* For files in dumpdates, get the last dump level and date */
646 if (ddatev
!= NULL
) {
647 struct dumpdates
*dtwalk
= NULL
;
651 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
654 ITITERATE(i
, dtwalk
) {
656 if (strncmp(lastname
, dtwalk
->dd_name
,
657 sizeof(dtwalk
->dd_name
)) == 0)
659 lastname
= dtwalk
->dd_name
;
660 if ((dt
= dtwalk
->dd_fstab
) != NULL
) {
661 /* Overload fs_freq as dump level and
662 * fs_passno as date, because we can't
663 * change struct fstab format.
664 * A positive fs_freq means this
665 * filesystem needs to be dumped.
667 dt
->mnt_passno
= dtwalk
->dd_ddate
;
668 if (dt
->mnt_freq
> 0 && (dtwalk
->dd_ddate
<
669 tnow
- (dt
->mnt_freq
* 86400)))
670 dt
->mnt_freq
= dtwalk
->dd_level
;
672 dt
->mnt_freq
= -dtwalk
->dd_level
;
674 printf("%s fs_freq set to %d\n", lastname
,
681 /* print in /etc/fstab order only those filesystem types we can dump */
682 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
683 struct mntent
*dt
= pf
->pf_mntent
;
686 for (type
= fstypes
; *type
!= NULL
; type
++) {
687 if (strncmp(dt
->mnt_type
, *type
,
688 sizeof(dt
->mnt_type
)) == 0) {
689 const char *disk
= get_device_name(dt
->mnt_fsname
);
690 print_wmsg(arg
, dt
->mnt_freq
> 0,
691 disk
? disk
: dt
->mnt_fsname
,
692 dt
->mnt_freq
< 0 ? -dt
->mnt_freq
:
700 /* print in /etc/dumpdates order if not in /etc/fstab */
701 if (ddatev
!= NULL
) {
702 struct dumpdates
*dtwalk
= NULL
;
707 ITITERATE(i
, dtwalk
) {
708 if (strncmp(lastname
, dtwalk
->dd_name
,
709 sizeof(dtwalk
->dd_name
)) == 0 ||
710 dtwalk
->dd_fstab
!= NULL
)
712 lastname
= dtwalk
->dd_name
;
713 print_wmsg(arg
, 0, dtwalk
->dd_name
,
714 dtwalk
->dd_level
, NULL
, dtwalk
->dd_ddate
);
720 datesort(const void *a1
, const void *a2
)
722 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
723 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
726 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
728 return (d2
->dd_ddate
- d1
->dd_ddate
);