]>
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@cybercable.fr>, 1999-2000
9 * Copyright (c) 1980, 1988, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char rcsid
[] =
43 "$Id: optr.c,v 1.13 2000/11/10 11:54:52 stelian Exp $";
46 #include <sys/param.h>
62 #include <linux/ext2_fs.h>
63 #include <ext2fs/ext2fs.h>
64 #include <bsdcompat.h>
69 #include "pathnames.h"
72 static void alarmcatch
__P((int));
73 int datesort
__P((const void *, const void *));
74 static void sendmes
__P((const char *, const char *));
76 /* List of filesystem types that we can dump (same ext2 on-disk format) */
77 static char *fstypes
[] = { "ext2", "ext3", "InterMezzo", NULL
};
80 * Query the operator; This previously-fascist piece of code
81 * no longer requires an exact response.
82 * It is intended to protect dump aborting by inquisitive
83 * people banging on the console terminal to see what is
84 * happening which might cause dump to croak, destroying
85 * a large number of hours of work.
87 * Every 2 minutes we reprint the message, alerting others
88 * that dump needs attention.
91 static const char *attnmessage
; /* attention message */
94 query(const char *question
)
99 time_t firstprompt
, when_answered
;
102 (void)time4(&(firstprompt
));
104 (void)time((time_t *)&(firstprompt
));
107 if ((mytty
= fopen(_PATH_TTY
, "r")) == NULL
)
108 quit("fopen on %s fails: %s\n", _PATH_TTY
, strerror(errno
));
109 attnmessage
= question
;
115 if (fgets(replybuffer
, 63, mytty
) == NULL
) {
117 if (++errcount
> 30) /* XXX ugly */
118 quit("excessive operator query failures\n");
119 } else if (replybuffer
[0] == 'y' || replybuffer
[0] == 'Y') {
121 } else if (replybuffer
[0] == 'n' || replybuffer
[0] == 'N') {
124 (void) fprintf(stderr
,
125 " DUMP: \"Yes\" or \"No\"?\n");
126 (void) fprintf(stderr
,
127 " DUMP: %s: (\"yes\" or \"no\") ", question
);
132 * Turn off the alarm, and reset the signal to trap out..
135 if (signal(SIGALRM
, sig
) == SIG_IGN
)
136 signal(SIGALRM
, SIG_IGN
);
137 (void) fclose(mytty
);
139 (void)time4(&(when_answered
));
141 (void)time((time_t *)&(when_answered
));
144 * Adjust the base for time estimates to ignore time we spent waiting
145 * for operator input.
147 if ((tstart_writing
!= 0) && (when_answered
!= (time_t)-1) && (firstprompt
!= (time_t)-1))
148 tstart_writing
+= (when_answered
- firstprompt
);
152 char lastmsg
[BUFSIZ
];
155 * Alert the console operator, and enable the alarm clock to
156 * sleep for 2 minutes in case nobody comes to satisfy dump
159 alarmcatch(int signo
)
161 int save_errno
= errno
;
164 (void) fprintf(stderr
,
165 " DUMP: %s: (\"yes\" or \"no\") ",
172 broadcast(""); /* just print last msg */
174 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
177 signal(SIGALRM
, alarmcatch
);
184 * Here if an inquisitive operator interrupts the dump program
189 msg("Interrupt received.\n");
190 if (query("Do you want to abort dump?"))
195 * The following variables and routines manage alerting
196 * operators to the status of dump.
197 * This works much like wall(1) does.
202 * Get the names from the group entry "operator" to notify.
207 if (!notify
) /*not going to notify*/
209 gp
= getgrnam(OPGRENT
);
212 msg("No group entry for %s.\n", OPGRENT
);
218 struct tm
*localclock
;
221 * We fork a child to do the actual broadcasting, so
222 * that the process control groups are not messed up
225 broadcast(const char *message
)
233 if (!notify
|| gp
== NULL
)
236 switch (pid
= fork()) {
242 while (wait(&s
) != pid
)
247 clock
= time((time_t *)0);
248 localclock
= localtime(&clock
);
250 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
251 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
255 while (!feof(f_utmp
)) {
256 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
258 if (utmp
.ut_name
[0] == 0)
260 for (np
= gp
->gr_mem
; *np
; np
++) {
261 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
264 * Do not send messages to operators on dialups
266 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
269 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
271 sendmes(utmp
.ut_line
, message
);
274 (void) fclose(f_utmp
);
275 Exit(0); /* the wait in this same routine will catch this */
280 sendmes(const char *tty
, const char *message
)
282 char t
[MAXPATHLEN
], buf
[BUFSIZ
];
283 register const char *cp
;
287 (void) strcpy(t
, _PATH_DEV
);
288 (void) strncat(t
, tty
, sizeof t
- strlen(_PATH_DEV
) - 1);
290 if ((f_tty
= fopen(t
, "w")) != NULL
) {
292 (void) fprintf(f_tty
,
294 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
295 DUMP: NEEDS ATTENTION: ",
296 localclock
->tm_hour
, localclock
->tm_min
);
297 for (cp
= lastmsg
; ; cp
++) {
301 if (!(cp
&& *cp
!= '\0'))
308 (void) putc('\r', f_tty
);
309 (void) putc(*cp
, f_tty
);
311 (void) fclose(f_tty
);
316 * print out an estimate of the amount of time left to do the dump
319 time_t tschedule
= 0;
329 (void) time((time_t *) &tnow
);
331 if (tnow
>= tschedule
) {
332 tschedule
= tnow
+ 300;
333 if (blockswritten
< 500)
335 if (blockswritten
> tapesize
)
336 tapesize
= blockswritten
;
337 deltat
= tstart_writing
- tnow
+
338 (1.0 * (tnow
- tstart_writing
))
339 / blockswritten
* tapesize
;
340 msg("%3.2f%% done, finished in %d:%02d\n",
341 (blockswritten
* 100.0) / tapesize
,
342 deltat
/ 3600, (deltat
% 3600) / 60);
348 msg(const char *fmt
, ...)
357 (void) fprintf(stderr
," DUMP: ");
359 (void) fprintf(stderr
, "pid=%d ", getpid());
366 (void) vfprintf(stderr
, fmt
, ap
);
368 (void) fflush(stdout
);
369 (void) fflush(stderr
);
375 (void) vsnprintf(lastmsg
, sizeof(lastmsg
), fmt
, ap
);
381 msgtail(const char *fmt
, ...)
383 msgtail(fmt
, va_alist
)
394 (void) vfprintf(stderr
, fmt
, ap
);
400 quit(const char *fmt
, ...)
409 (void) fprintf(stderr
," DUMP: ");
411 (void) fprintf(stderr
, "pid=%d ", getpid());
418 (void) vfprintf(stderr
, fmt
, ap
);
420 (void) fflush(stdout
);
421 (void) fflush(stderr
);
426 * Tell the operator what has to be done;
427 * we don't actually do it
430 static struct fstab
*
431 allocfsent(struct fstab
*fs
)
433 register struct fstab
*new;
435 new = (struct fstab
*)malloc(sizeof (*fs
));
437 quit("%s\n", strerror(errno
));
438 if (strlen(fs
->fs_file
) > 1 && fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')
439 fs
->fs_file
[strlen(fs
->fs_file
) - 1] = '\0';
440 if ((new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
441 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
442 (new->fs_vfstype
= strdup(fs
->fs_vfstype
)) == NULL
||
443 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
444 quit("%s\n", strerror(errno
));
445 new->fs_passno
= fs
->fs_passno
;
446 new->fs_freq
= fs
->fs_freq
;
451 struct pfstab
*pf_next
;
452 struct dumpdates
*pf_dd
;
453 struct fstab
*pf_fstab
;
456 static struct pfstab
*table
;
463 struct pfstab
*pfold
= NULL
;
465 if (setfsent() == 0) {
466 msg("Can't open %s for dump table information: %s\n",
467 _PATH_FSTAB
, strerror(errno
));
470 while ((fs
= getfsent()) != NULL
) {
471 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
472 strcmp(fs
->fs_type
, FSTAB_RO
) &&
473 strcmp(fs
->fs_type
, FSTAB_RQ
))
477 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
478 quit("%s\n", strerror(errno
));
482 /* keep table in /etc/fstab order for use with -w and -W */
494 * Search in the fstab for a file name.
495 * This file name can be either the special or the path file name.
497 * The entries in the fstab are the BLOCK special names, not the
498 * character special names.
499 * The caller of fstabsearch assures that the character device
500 * is dumped (that is much faster)
502 * The file name can omit the leading '/'.
505 fstabsearch(const char *key
)
507 register struct pfstab
*pf
;
508 register struct fstab
*fs
;
511 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
513 if (strcmp(fs
->fs_file
, key
) == 0 ||
514 strcmp(fs
->fs_spec
, key
) == 0)
516 rn
= rawname(fs
->fs_spec
);
517 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
520 if (*fs
->fs_spec
== '/' &&
521 strcmp(fs
->fs_spec
+ 1, key
) == 0)
523 if (*fs
->fs_file
== '/' &&
524 strcmp(fs
->fs_file
+ 1, key
) == 0)
533 fstabsearchdir(const char *key
, char *directory
)
535 register struct pfstab
*pf
;
536 register struct fstab
*fs
;
537 register struct fstab
*found_fs
= NULL
;
538 unsigned int size
= 0;
541 if (stat(key
, &buf
) == 0 && S_ISBLK(buf
.st_mode
))
544 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
546 if (strlen(fs
->fs_file
) > size
&&
547 strlen(key
) > strlen(fs
->fs_file
) + 1 &&
548 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
549 (key
[strlen(fs
->fs_file
)] == '/' ||
550 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
552 size
= strlen(fs
->fs_file
);
555 if (found_fs
!= NULL
) {
557 * Ok, we have found a fstab entry which matches the argument
558 * We have to split the argument name into:
559 * - a device name (from the fstab entry)
560 * - a directory name on this device
562 strcpy(directory
, key
+ size
);
569 print_wmsg(char arg
, int dumpme
, const char *dev
, int level
,
570 const char *mtpt
, time_t ddate
)
575 date
= (char *)ctime(&ddate
);
576 //date[16] = '\0'; /* blast away seconds and year */
578 if (!dumpme
&& arg
== 'w')
581 (void) printf("%c %8s\t(%6s) Last dump: ",
582 dumpme
&& (arg
!= 'w') ? '>' : ' ',
587 printf("Level %c, Date %s\n",
594 * Tell the operator what to do
597 lastdump(char arg
) /* w ==> just what to do; W ==> most recent dumps */
603 getfstab(); /* /etc/fstab input */
604 initdumptimes(0); /* dumpdates input */
605 if (ddatev
== NULL
&& table
== NULL
) {
606 (void) printf("No %s or %s file found\n",
607 _PATH_FSTAB
, _PATH_DUMPDATES
);
612 (void) printf("Dump these file systems:\n");
614 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
616 if (ddatev
!= NULL
) {
617 struct dumpdates
*dtwalk
= NULL
;
621 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
624 ITITERATE(i
, dtwalk
) {
626 if (strncmp(lastname
, dtwalk
->dd_name
,
627 sizeof(dtwalk
->dd_name
)) == 0)
629 lastname
= dtwalk
->dd_name
;
630 if ((dt
= dtwalk
->dd_fstab
) != NULL
&&
632 /* Overload fs_freq as dump level and
633 * fs_passno as date, because we can't
634 * change struct fstab format.
635 * A negative fs_freq means this
636 * filesystem needs to be dumped.
638 dt
->fs_passno
= dtwalk
->dd_ddate
;
639 if (dtwalk
->dd_ddate
<
640 tnow
- (dt
->fs_freq
* 86400))
641 dt
->fs_freq
= -dtwalk
->dd_level
;
643 dt
->fs_freq
= dtwalk
->dd_level
;
649 /* print in /etc/fstab order only those filesystem types we can dump */
650 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
651 struct fstab
*dt
= pf
->pf_fstab
;
654 for (type
= fstypes
; *type
!= NULL
; type
++) {
655 if (strncmp(dt
->fs_vfstype
, *type
,
656 sizeof(dt
->fs_vfstype
)) == 0) {
657 const char *disk
= get_device_name(dt
->fs_spec
);
658 print_wmsg(arg
, dt
->fs_freq
< 0 || !dt
->fs_passno
,
659 disk
? disk
: dt
->fs_spec
,
660 dt
->fs_freq
< 0 ? -dt
->fs_freq
: dt
->fs_freq
,
667 /* print in /etc/dumpdates order if not in /etc/fstab */
668 if (ddatev
!= NULL
) {
669 struct dumpdates
*dtwalk
= NULL
;
674 ITITERATE(i
, dtwalk
) {
675 if (strncmp(lastname
, dtwalk
->dd_name
,
676 sizeof(dtwalk
->dd_name
)) == 0 ||
677 dtwalk
->dd_fstab
!= NULL
)
679 lastname
= dtwalk
->dd_name
;
680 print_wmsg(arg
, 0, dtwalk
->dd_name
,
681 dtwalk
->dd_level
, NULL
, dtwalk
->dd_ddate
);
687 datesort(const void *a1
, const void *a2
)
689 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
690 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
693 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
695 return (d2
->dd_ddate
- d1
->dd_ddate
);