]>
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.23 2001/06/18 10:58:28 stelian Exp $";
48 #include <sys/param.h>
64 #ifdef HAVE_EXT2FS_EXT2_FS_H
65 #include <ext2fs/ext2_fs.h>
67 #include <linux/ext2_fs.h>
69 #include <ext2fs/ext2fs.h>
70 #include <bsdcompat.h>
76 #include "pathnames.h"
79 static void alarmcatch
__P((int));
80 int datesort
__P((const void *, const void *));
81 static void sendmes
__P((const char *, const char *));
83 /* List of filesystem types that we can dump (same ext2 on-disk format) */
84 static char *fstypes
[] = { "ext2", "ext3", "InterMezzo", NULL
};
87 * Query the operator; This previously-fascist piece of code
88 * no longer requires an exact response.
89 * It is intended to protect dump aborting by inquisitive
90 * people banging on the console terminal to see what is
91 * happening which might cause dump to croak, destroying
92 * a large number of hours of work.
94 * Every 2 minutes we reprint the message, alerting others
95 * that dump needs attention.
98 static const char *attnmessage
; /* attention message */
101 query(const char *question
)
103 char replybuffer
[64];
106 time_t firstprompt
, when_answered
;
108 firstprompt
= time(NULL
);
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
);
141 when_answered
= time(NULL
);
143 * Adjust the base for time estimates to ignore time we spent waiting
144 * for operator input.
146 if (tstart_writing
!= 0)
147 tstart_writing
+= (when_answered
- firstprompt
);
151 char lastmsg
[BUFSIZ
];
154 * Alert the console operator, and enable the alarm clock to
155 * sleep for 2 minutes in case nobody comes to satisfy dump
158 alarmcatch(int signo
)
160 int save_errno
= errno
;
163 (void) fprintf(stderr
,
164 " DUMP: %s: (\"yes\" or \"no\") ",
171 broadcast(""); /* just print last msg */
173 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
176 signal(SIGALRM
, alarmcatch
);
183 * Here if an inquisitive operator interrupts the dump program
188 msg("Interrupt received.\n");
189 if (query("Do you want to abort dump?"))
194 * The following variables and routines manage alerting
195 * operators to the status of dump.
196 * This works much like wall(1) does.
201 * Get the names from the group entry "operator" to notify.
206 if (!notify
) /*not going to notify*/
208 gp
= getgrnam(OPGRENT
);
211 msg("No group entry for %s.\n", OPGRENT
);
217 struct tm
*localclock
;
220 * We fork a child to do the actual broadcasting, so
221 * that the process control groups are not messed up
224 broadcast(const char *message
)
232 if (!notify
|| gp
== NULL
)
235 switch (pid
= fork()) {
241 while (wait(&s
) != pid
)
247 localclock
= localtime(&clock
);
249 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
250 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
254 while (!feof(f_utmp
)) {
255 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
257 if (utmp
.ut_name
[0] == 0)
259 for (np
= gp
->gr_mem
; *np
; np
++) {
260 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
263 * Do not send messages to operators on dialups
265 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
268 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
270 sendmes(utmp
.ut_line
, message
);
273 (void) fclose(f_utmp
);
274 Exit(0); /* the wait in this same routine will catch this */
279 sendmes(const char *tty
, const char *message
)
281 char t
[MAXPATHLEN
], buf
[BUFSIZ
];
282 register const char *cp
;
286 (void) strcpy(t
, _PATH_DEV
);
287 (void) strncat(t
, tty
, sizeof t
- strlen(_PATH_DEV
) - 1);
289 if ((f_tty
= fopen(t
, "w")) != NULL
) {
291 (void) fprintf(f_tty
,
293 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
294 DUMP: NEEDS ATTENTION: ",
295 localclock
->tm_hour
, localclock
->tm_min
);
296 for (cp
= lastmsg
; ; cp
++) {
300 if (!(cp
&& *cp
!= '\0'))
307 (void) putc('\r', f_tty
);
308 (void) putc(*cp
, f_tty
);
310 (void) fclose(f_tty
);
315 * print out an estimate of the amount of time left to do the dump
318 time_t tschedule
= 0;
323 time_t tnow
= time(NULL
);
325 if (tnow
>= tschedule
) {
326 char *buf
= mktimeest(tnow
);
327 tschedule
= tnow
+ 300;
329 fprintf(stderr
, " DUMP: ");
330 fwrite(buf
, strlen(buf
), 1, stderr
);
338 msg(const char *fmt
, ...)
347 (void) fprintf(stderr
," DUMP: ");
349 (void) fprintf(stderr
, "pid=%d ", getpid());
356 (void) vfprintf(stderr
, fmt
, ap
);
358 (void) fflush(stdout
);
359 (void) fflush(stderr
);
365 (void) vsnprintf(lastmsg
, sizeof(lastmsg
), fmt
, ap
);
371 msgtail(const char *fmt
, ...)
373 msgtail(fmt
, va_alist
)
384 (void) vfprintf(stderr
, fmt
, ap
);
390 quit(const char *fmt
, ...)
399 (void) fprintf(stderr
," DUMP: ");
401 (void) fprintf(stderr
, "pid=%d ", getpid());
408 (void) vfprintf(stderr
, fmt
, ap
);
410 (void) fflush(stdout
);
411 (void) fflush(stderr
);
416 * Tell the operator what has to be done;
417 * we don't actually do it
420 static struct fstab
*
421 allocfsent(struct fstab
*fs
)
423 register struct fstab
*new;
425 new = (struct fstab
*)malloc(sizeof (*fs
));
427 quit("%s\n", strerror(errno
));
428 if (strlen(fs
->fs_file
) > 1 && fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')
429 fs
->fs_file
[strlen(fs
->fs_file
) - 1] = '\0';
430 if ((new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
431 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
432 (new->fs_vfstype
= strdup(fs
->fs_vfstype
)) == NULL
||
433 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
434 quit("%s\n", strerror(errno
));
435 new->fs_passno
= fs
->fs_passno
;
436 new->fs_freq
= fs
->fs_freq
;
441 struct pfstab
*pf_next
;
442 struct dumpdates
*pf_dd
;
443 struct fstab
*pf_fstab
;
446 static struct pfstab
*table
;
453 struct pfstab
*pfold
= NULL
;
455 if (setfsent() == 0) {
456 msg("Can't open %s for dump table information: %s\n",
457 _PATH_FSTAB
, strerror(errno
));
460 while ((fs
= getfsent()) != NULL
) {
461 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
462 strcmp(fs
->fs_type
, FSTAB_RO
) &&
463 strcmp(fs
->fs_type
, FSTAB_RQ
))
467 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
468 quit("%s\n", strerror(errno
));
472 /* keep table in /etc/fstab order for use with -w and -W */
484 * Search in the fstab for a file name.
485 * This file name can be either the special or the path file name.
487 * The entries in the fstab are the BLOCK special names, not the
488 * character special names.
489 * The caller of fstabsearch assures that the character device
490 * is dumped (that is much faster)
492 * The file name can omit the leading '/'.
495 fstabsearch(const char *key
)
497 register struct pfstab
*pf
;
498 register struct fstab
*fs
;
501 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
503 if (strcmp(fs
->fs_file
, key
) == 0 ||
504 strcmp(fs
->fs_spec
, key
) == 0)
506 rn
= rawname(fs
->fs_spec
);
507 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
510 if (*fs
->fs_spec
== '/' &&
511 strcmp(fs
->fs_spec
+ 1, key
) == 0)
513 if (*fs
->fs_file
== '/' &&
514 strcmp(fs
->fs_file
+ 1, key
) == 0)
523 fstabsearchdir(const char *key
, char *directory
)
525 register struct pfstab
*pf
;
526 register struct fstab
*fs
;
527 register struct fstab
*found_fs
= NULL
;
528 unsigned int size
= 0;
531 if (stat(key
, &buf
) == 0 && S_ISBLK(buf
.st_mode
))
534 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
536 if (strlen(fs
->fs_file
) > size
&&
537 strlen(key
) > strlen(fs
->fs_file
) + 1 &&
538 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
539 (key
[strlen(fs
->fs_file
)] == '/' ||
540 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
542 size
= strlen(fs
->fs_file
);
545 if (found_fs
!= NULL
) {
547 * Ok, we have found a fstab entry which matches the argument
548 * We have to split the argument name into:
549 * - a device name (from the fstab entry)
550 * - a directory name on this device
552 strcpy(directory
, key
+ size
);
559 print_wmsg(char arg
, int dumpme
, const char *dev
, int level
,
560 const char *mtpt
, time_t ddate
)
566 date
= (char *)ctime(&ddate
);
567 d
= strchr(date
, '\n');
571 if (!dumpme
&& arg
== 'w')
574 (void) printf("%c %8s\t(%6s) Last dump: ",
575 dumpme
&& (arg
!= 'w') ? '>' : ' ',
580 printf("Level %c, Date %s\n",
587 * Tell the operator what to do
590 lastdump(char arg
) /* w ==> just what to do; W ==> most recent dumps */
596 getfstab(); /* /etc/fstab input */
597 initdumptimes(0); /* dumpdates input */
598 if (ddatev
== NULL
&& table
== NULL
) {
599 (void) printf("No %s or %s file found\n",
600 _PATH_FSTAB
, _PATH_DUMPDATES
);
605 (void) printf("Dump these file systems:\n");
607 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
609 if (ddatev
!= NULL
) {
610 struct dumpdates
*dtwalk
= NULL
;
614 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
617 ITITERATE(i
, dtwalk
) {
619 if (strncmp(lastname
, dtwalk
->dd_name
,
620 sizeof(dtwalk
->dd_name
)) == 0)
622 lastname
= dtwalk
->dd_name
;
623 if ((dt
= dtwalk
->dd_fstab
) != NULL
) {
624 /* Overload fs_freq as dump level and
625 * fs_passno as date, because we can't
626 * change struct fstab format.
627 * A negative fs_freq means this
628 * filesystem needs to be dumped.
630 dt
->fs_passno
= dtwalk
->dd_ddate
;
631 if (dt
->fs_freq
> 0 && (dtwalk
->dd_ddate
<
632 tnow
- (dt
->fs_freq
* 86400)))
633 dt
->fs_freq
= -dtwalk
->dd_level
- 1;
635 dt
->fs_freq
= dtwalk
->dd_level
+ 1;
641 /* print in /etc/fstab order only those filesystem types we can dump */
642 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
643 struct fstab
*dt
= pf
->pf_fstab
;
646 for (type
= fstypes
; *type
!= NULL
; type
++) {
647 if (strncmp(dt
->fs_vfstype
, *type
,
648 sizeof(dt
->fs_vfstype
)) == 0) {
649 const char *disk
= get_device_name(dt
->fs_spec
);
650 print_wmsg(arg
, dt
->fs_freq
< 0,
651 disk
? disk
: dt
->fs_spec
,
652 dt
->fs_freq
< 0 ? -dt
->fs_freq
- 1 : dt
->fs_freq
- 1,
659 /* print in /etc/dumpdates order if not in /etc/fstab */
660 if (ddatev
!= NULL
) {
661 struct dumpdates
*dtwalk
= NULL
;
666 ITITERATE(i
, dtwalk
) {
667 if (strncmp(lastname
, dtwalk
->dd_name
,
668 sizeof(dtwalk
->dd_name
)) == 0 ||
669 dtwalk
->dd_fstab
!= NULL
)
671 lastname
= dtwalk
->dd_name
;
672 print_wmsg(arg
, 0, dtwalk
->dd_name
,
673 dtwalk
->dd_level
, NULL
, dtwalk
->dd_ddate
);
679 datesort(const void *a1
, const void *a2
)
681 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
682 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
685 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
687 return (d2
->dd_ddate
- d1
->dd_ddate
);