]>
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.11 2000/08/19 23:15:38 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"
71 static void alarmcatch
__P((int));
72 int datesort
__P((const void *, const void *));
73 static void sendmes
__P((const char *, const char *));
76 * Query the operator; This previously-fascist piece of code
77 * no longer requires an exact response.
78 * It is intended to protect dump aborting by inquisitive
79 * people banging on the console terminal to see what is
80 * happening which might cause dump to croak, destroying
81 * a large number of hours of work.
83 * Every 2 minutes we reprint the message, alerting others
84 * that dump needs attention.
87 static const char *attnmessage
; /* attention message */
90 query(const char *question
)
95 time_t firstprompt
, when_answered
;
98 (void)time4(&(firstprompt
));
100 (void)time((time_t *)&(firstprompt
));
103 if ((mytty
= fopen(_PATH_TTY
, "r")) == NULL
)
104 quit("fopen on %s fails: %s\n", _PATH_TTY
, strerror(errno
));
105 attnmessage
= question
;
111 if (fgets(replybuffer
, 63, mytty
) == NULL
) {
113 if (++errcount
> 30) /* XXX ugly */
114 quit("excessive operator query failures\n");
115 } else if (replybuffer
[0] == 'y' || replybuffer
[0] == 'Y') {
117 } else if (replybuffer
[0] == 'n' || replybuffer
[0] == 'N') {
120 (void) fprintf(stderr
,
121 " DUMP: \"Yes\" or \"No\"?\n");
122 (void) fprintf(stderr
,
123 " DUMP: %s: (\"yes\" or \"no\") ", question
);
128 * Turn off the alarm, and reset the signal to trap out..
131 if (signal(SIGALRM
, sig
) == SIG_IGN
)
132 signal(SIGALRM
, SIG_IGN
);
133 (void) fclose(mytty
);
135 (void)time4(&(when_answered
));
137 (void)time((time_t *)&(when_answered
));
140 * Adjust the base for time estimates to ignore time we spent waiting
141 * for operator input.
143 if ((tstart_writing
!= 0) && (when_answered
!= (time_t)-1) && (firstprompt
!= (time_t)-1))
144 tstart_writing
+= (when_answered
- firstprompt
);
148 char lastmsg
[BUFSIZ
];
151 * Alert the console operator, and enable the alarm clock to
152 * sleep for 2 minutes in case nobody comes to satisfy dump
155 alarmcatch(int signo
)
157 int save_errno
= errno
;
160 (void) fprintf(stderr
,
161 " DUMP: %s: (\"yes\" or \"no\") ",
168 broadcast(""); /* just print last msg */
170 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
173 signal(SIGALRM
, alarmcatch
);
180 * Here if an inquisitive operator interrupts the dump program
185 msg("Interrupt received.\n");
186 if (query("Do you want to abort dump?"))
191 * The following variables and routines manage alerting
192 * operators to the status of dump.
193 * This works much like wall(1) does.
198 * Get the names from the group entry "operator" to notify.
203 if (!notify
) /*not going to notify*/
205 gp
= getgrnam(OPGRENT
);
208 msg("No group entry for %s.\n", OPGRENT
);
214 struct tm
*localclock
;
217 * We fork a child to do the actual broadcasting, so
218 * that the process control groups are not messed up
221 broadcast(const char *message
)
229 if (!notify
|| gp
== NULL
)
232 switch (pid
= fork()) {
238 while (wait(&s
) != pid
)
243 clock
= time((time_t *)0);
244 localclock
= localtime(&clock
);
246 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
247 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
251 while (!feof(f_utmp
)) {
252 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
254 if (utmp
.ut_name
[0] == 0)
256 for (np
= gp
->gr_mem
; *np
; np
++) {
257 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
260 * Do not send messages to operators on dialups
262 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
265 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
267 sendmes(utmp
.ut_line
, message
);
270 (void) fclose(f_utmp
);
271 Exit(0); /* the wait in this same routine will catch this */
276 sendmes(const char *tty
, const char *message
)
278 char t
[MAXPATHLEN
], buf
[BUFSIZ
];
279 register const char *cp
;
283 (void) strcpy(t
, _PATH_DEV
);
284 (void) strncat(t
, tty
, sizeof t
- strlen(_PATH_DEV
) - 1);
286 if ((f_tty
= fopen(t
, "w")) != NULL
) {
288 (void) fprintf(f_tty
,
290 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
291 DUMP: NEEDS ATTENTION: ",
292 localclock
->tm_hour
, localclock
->tm_min
);
293 for (cp
= lastmsg
; ; cp
++) {
297 if (!(cp
&& *cp
!= '\0'))
304 (void) putc('\r', f_tty
);
305 (void) putc(*cp
, f_tty
);
307 (void) fclose(f_tty
);
312 * print out an estimate of the amount of time left to do the dump
315 time_t tschedule
= 0;
325 (void) time((time_t *) &tnow
);
327 if (tnow
>= tschedule
) {
328 tschedule
= tnow
+ 300;
329 if (blockswritten
< 500)
331 if (blockswritten
> tapesize
)
332 tapesize
= blockswritten
;
333 deltat
= tstart_writing
- tnow
+
334 (1.0 * (tnow
- tstart_writing
))
335 / blockswritten
* tapesize
;
336 msg("%3.2f%% done, finished in %d:%02d\n",
337 (blockswritten
* 100.0) / tapesize
,
338 deltat
/ 3600, (deltat
% 3600) / 60);
344 msg(const char *fmt
, ...)
353 (void) fprintf(stderr
," DUMP: ");
355 (void) fprintf(stderr
, "pid=%d ", getpid());
362 (void) vfprintf(stderr
, fmt
, ap
);
364 (void) fflush(stdout
);
365 (void) fflush(stderr
);
371 (void) vsnprintf(lastmsg
, sizeof(lastmsg
), fmt
, ap
);
377 msgtail(const char *fmt
, ...)
379 msgtail(fmt
, va_alist
)
390 (void) vfprintf(stderr
, fmt
, ap
);
396 quit(const char *fmt
, ...)
405 (void) fprintf(stderr
," DUMP: ");
407 (void) fprintf(stderr
, "pid=%d ", getpid());
414 (void) vfprintf(stderr
, fmt
, ap
);
416 (void) fflush(stdout
);
417 (void) fflush(stderr
);
422 * Tell the operator what has to be done;
423 * we don't actually do it
426 static struct fstab
*
427 allocfsent(struct fstab
*fs
)
429 register struct fstab
*new;
431 new = (struct fstab
*)malloc(sizeof (*fs
));
433 quit("%s\n", strerror(errno
));
434 if (strlen(fs
->fs_file
) > 1 && fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')
435 fs
->fs_file
[strlen(fs
->fs_file
) - 1] = '\0';
436 if ((new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
437 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
438 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
439 quit("%s\n", strerror(errno
));
440 new->fs_passno
= fs
->fs_passno
;
441 new->fs_freq
= fs
->fs_freq
;
446 struct pfstab
*pf_next
;
447 struct fstab
*pf_fstab
;
450 static struct pfstab
*table
;
455 register struct fstab
*fs
;
456 register struct pfstab
*pf
;
458 if (setfsent() == 0) {
459 msg("Can't open %s for dump table information: %s\n",
460 _PATH_FSTAB
, strerror(errno
));
463 while ((fs
= getfsent()) != NULL
) {
464 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
465 strcmp(fs
->fs_type
, FSTAB_RO
) &&
466 strcmp(fs
->fs_type
, FSTAB_RQ
))
469 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
470 quit("%s\n", strerror(errno
));
479 * Search in the fstab for a file name.
480 * This file name can be either the special or the path file name.
482 * The entries in the fstab are the BLOCK special names, not the
483 * character special names.
484 * The caller of fstabsearch assures that the character device
485 * is dumped (that is much faster)
487 * The file name can omit the leading '/'.
490 fstabsearch(const char *key
)
492 register struct pfstab
*pf
;
493 register struct fstab
*fs
;
496 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
498 if (strcmp(fs
->fs_file
, key
) == 0 ||
499 strcmp(fs
->fs_spec
, key
) == 0)
501 rn
= rawname(fs
->fs_spec
);
502 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
505 if (*fs
->fs_spec
== '/' &&
506 strcmp(fs
->fs_spec
+ 1, key
) == 0)
508 if (*fs
->fs_file
== '/' &&
509 strcmp(fs
->fs_file
+ 1, key
) == 0)
518 fstabsearchdir(const char *key
, char *directory
)
520 register struct pfstab
*pf
;
521 register struct fstab
*fs
;
522 register struct fstab
*found_fs
= NULL
;
523 unsigned int size
= 0;
526 if (stat(key
, &buf
) == 0 && S_ISBLK(buf
.st_mode
))
529 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
531 if (strlen(fs
->fs_file
) > size
&&
532 strlen(key
) > strlen(fs
->fs_file
) + 1 &&
533 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
534 (key
[strlen(fs
->fs_file
)] == '/' ||
535 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
537 size
= strlen(fs
->fs_file
);
540 if (found_fs
!= NULL
) {
542 * Ok, we have found a fstab entry which matches the argument
543 * We have to split the argument name into:
544 * - a device name (from the fstab entry)
545 * - a directory name on this device
547 strcpy(directory
, key
+ size
);
554 * Tell the operator what to do
557 lastdump(char arg
) /* w ==> just what to do; W ==> most recent dumps */
560 register struct fstab
*dt
;
561 register struct dumpdates
*dtwalk
=NULL
;
562 char *lastname
, *date
;
567 getfstab(); /* /etc/fstab input */
568 initdumptimes(0); /* dumpdates input */
569 if (ddatev
!= NULL
) {
570 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
573 (void) printf("Dump these file systems:\n");
575 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
577 ITITERATE(i
, dtwalk
) {
578 if (strncmp(lastname
, dtwalk
->dd_name
,
579 sizeof(dtwalk
->dd_name
)) == 0)
581 date
= (char *)ctime(&dtwalk
->dd_ddate
);
582 date
[16] = '\0'; /* blast away seconds and year */
583 lastname
= dtwalk
->dd_name
;
584 dt
= fstabsearch(dtwalk
->dd_name
);
585 dumpme
= (dt
!= NULL
&&
587 dtwalk
->dd_ddate
< tnow
- (dt
->fs_freq
* 86400));
588 if (arg
!= 'w' || dumpme
)
590 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
591 dumpme
&& (arg
!= 'w') ? '>' : ' ',
593 dt
? dt
->fs_file
: "",
601 datesort(const void *a1
, const void *a2
)
603 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
604 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
607 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
609 return (d2
->dd_ddate
- d1
->dd_ddate
);