]>
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.40 2006/03/14 11:09:51 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 disk
= strdup(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
) {
463 /* Copy passno and freq from /etc/fstab because
464 * /etc/mtab does always have them as 0 0 */
465 if (!tabfs
->mnt_passno
)
466 tabfs
->mnt_passno
= fs
->mnt_passno
;
467 if (!tabfs
->mnt_freq
)
468 tabfs
->mnt_freq
= fs
->mnt_freq
;
469 if (tabfs
->mnt_freq
> 3659)
470 quit("Dump frequency in fstab/mtab for %s is too big: %d > 3659\n",
471 tabfs
->mnt_fsname
, tabfs
->mnt_freq
);
476 if (strlen(fs
->mnt_dir
) > 1 && fs
->mnt_dir
[strlen(fs
->mnt_dir
) - 1] == '/')
477 fs
->mnt_dir
[strlen(fs
->mnt_dir
) - 1] = '\0';
478 if ((new->mnt_dir
= strdup(fs
->mnt_dir
)) == NULL
||
479 (new->mnt_type
= strdup(fs
->mnt_type
)) == NULL
||
480 (new->mnt_opts
= strdup(fs
->mnt_opts
)) == NULL
||
481 (new->mnt_fsname
= strdup(disk
)) == NULL
)
482 quit("%s\n", strerror(errno
));
483 new->mnt_passno
= fs
->mnt_passno
;
484 new->mnt_freq
= fs
->mnt_freq
;
485 if (new->mnt_freq
> 3659)
486 quit("Dump frequency in fstab/mtab for %s %s is too big: %d > 3659\n",
487 new->mnt_dir
, new->mnt_fsname
, new->mnt_freq
);
496 struct pfstab
*pfold
= NULL
;
498 char *mnttables
[] = { _PATH_MOUNTED
, _PATH_MNTTAB
, 0 };
501 for (i
= 0; mnttables
[i
]; i
++) {
502 mntfp
= setmntent(mnttables
[i
], "r");
504 msg("Can't open %s for dump table information: %s\n",
505 mnttables
[i
], strerror(errno
));
508 while ((fs
= getmntent(mntfp
)) != NULL
) {
513 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
514 quit("%s\n", strerror(errno
));
518 /* keep table in /etc/fstab order for use with -w and -W */
525 (void) endmntent(mntfp
);
530 * Search in the fstab for a file name.
531 * This file name can be either the special or the path file name.
533 * The entries in the fstab are the BLOCK special names, not the
534 * character special names.
535 * The caller of fstabsearch assures that the character device
536 * is dumped (that is much faster)
538 * The file name can omit the leading '/'.
541 fstabsearch(const char *key
)
547 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
549 if (strcmp(fs
->mnt_dir
, key
) == 0 ||
550 strcmp(fs
->mnt_fsname
, key
) == 0)
552 rn
= rawname(fs
->mnt_fsname
);
553 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
556 if (*fs
->mnt_fsname
== '/' &&
557 strcmp(fs
->mnt_fsname
+ 1, key
) == 0)
559 if (*fs
->mnt_dir
== '/' &&
560 strcmp(fs
->mnt_dir
+ 1, key
) == 0)
569 fstabsearchdir(const char *key
, char *directory
)
573 struct mntent
*found_fs
= NULL
;
574 unsigned int size
= 0;
577 if (stat(key
, &buf
) == 0 && S_ISBLK(buf
.st_mode
))
580 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
582 if (strlen(fs
->mnt_dir
) > size
&&
583 strlen(key
) > strlen(fs
->mnt_dir
) &&
584 strncmp(fs
->mnt_dir
, key
, strlen(fs
->mnt_dir
)) == 0 &&
585 (key
[strlen(fs
->mnt_dir
)] == '/' ||
586 fs
->mnt_dir
[strlen(fs
->mnt_dir
) - 1] == '/')) {
588 size
= strlen(fs
->mnt_dir
);
591 if (found_fs
!= NULL
) {
593 * Ok, we have found a fstab entry which matches the argument
594 * We have to split the argument name into:
595 * - a device name (from the fstab entry)
596 * - a directory name on this device
598 strcpy(directory
, key
+ size
);
605 print_wmsg(char arg
, int dumpme
, const char *dev
, int level
,
606 const char *mtpt
, time_t ddate
)
609 printf("checking dev %s: lvl %d, mtpt %s\n", dev
, level
, mtpt
);
611 if (!dumpme
&& arg
== 'w')
614 (void) printf("%c %8s\t(%6s) Last dump: ",
615 dumpme
&& (arg
!= 'w') ? '>' : ' ',
620 * Check ddate > 365 to avoid issues with fs in stab but not dumpdates.
621 * Not a problem, because ddate is in seconds since the epoch anyways.
623 if (level
>= 0 && ddate
> 365) {
626 date
= (char *)ctime(&ddate
);
627 d
= strchr(date
, '\n');
629 printf("Level %d, Date %s\n", level
, date
);
635 * Tell the operator what to do
638 lastdump(char arg
) /* w ==> just what to do; W ==> most recent dumps */
644 getfstab(); /* /etc/fstab input */
645 initdumptimes(0); /* dumpdates input */
646 if (ddatev
== NULL
&& table
== NULL
) {
647 (void) printf("No %s or %s file found\n",
648 _PATH_MNTTAB
, dumpdates
);
653 (void) printf("Dump these file systems:\n");
655 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
657 /* For files in dumpdates, get the last dump level and date */
658 if (ddatev
!= NULL
) {
659 struct dumpdates
*dtwalk
= NULL
;
663 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
666 ITITERATE(i
, dtwalk
) {
668 if (strncmp(lastname
, dtwalk
->dd_name
,
669 sizeof(dtwalk
->dd_name
)) == 0)
671 lastname
= dtwalk
->dd_name
;
672 if ((dt
= dtwalk
->dd_fstab
) != NULL
) {
673 /* Overload fs_freq as dump level and
674 * fs_passno as date, because we can't
675 * change struct fstab format.
676 * A positive fs_freq means this
677 * filesystem needs to be dumped.
679 * UGLY HACK: values in 0-3659 range indicate
680 * original value from /etc/fstab (maximum
683 * Values bigger than 3659 indicate that
684 * the fs is to be dumped, and the latest
685 * dump level was x-3660
687 * Negative values indicate that the fs is
688 * not to be dumped, and the latest dump
691 dt
->mnt_passno
= dtwalk
->dd_ddate
;
692 if (dt
->mnt_freq
> 0 && (dtwalk
->dd_ddate
<
693 tnow
- (dt
->mnt_freq
* 86400)))
694 dt
->mnt_freq
= 3660 + dtwalk
->dd_level
;
696 dt
->mnt_freq
= -dtwalk
->dd_level
- 1;
698 printf("%s fs_freq set to %d\n", lastname
,
705 /* print in /etc/fstab order only those filesystem types we can dump */
706 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
707 struct mntent
*dt
= pf
->pf_mntent
;
710 for (type
= fstypes
; *type
!= NULL
; type
++) {
711 if (strncmp(dt
->mnt_type
, *type
,
712 sizeof(dt
->mnt_type
)) == 0) {
713 const char *disk
= get_device_name(dt
->mnt_fsname
);
714 print_wmsg(arg
, dt
->mnt_freq
> 0,
715 disk
? disk
: dt
->mnt_fsname
,
716 (dt
->mnt_freq
< 0 ? -dt
->mnt_freq
- 1 :
717 dt
->mnt_freq
< 3660 ? -1 :
718 dt
->mnt_freq
- 3660),
725 /* print in /etc/dumpdates order if not in /etc/fstab */
726 if (ddatev
!= NULL
) {
727 struct dumpdates
*dtwalk
= NULL
;
732 ITITERATE(i
, dtwalk
) {
733 if (strncmp(lastname
, dtwalk
->dd_name
,
734 sizeof(dtwalk
->dd_name
)) == 0 ||
735 dtwalk
->dd_fstab
!= NULL
)
737 lastname
= dtwalk
->dd_name
;
738 print_wmsg(arg
, 0, dtwalk
->dd_name
,
739 dtwalk
->dd_level
, NULL
, dtwalk
->dd_ddate
);
745 datesort(const void *a1
, const void *a2
)
747 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
748 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
751 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
753 return (d2
->dd_ddate
- d1
->dd_ddate
);