]>
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
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
44 static char sccsid
[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94";
46 static const char rcsid
[] =
47 "$Id: optr.c,v 1.2 1999/10/11 12:53:22 stelian Exp $";
50 #include <sys/param.h>
65 #include <linux/ext2_fs.h>
66 #include <ext2fs/ext2fs.h>
67 #include <bsdcompat.h>
72 #include "pathnames.h"
74 void alarmcatch
__P((/* int, int */));
75 int datesort
__P((const void *, const void *));
76 static void sendmes
__P((char *, char *));
79 * Query the operator; This previously-fascist piece of code
80 * no longer requires an exact response.
81 * It is intended to protect dump aborting by inquisitive
82 * people banging on the console terminal to see what is
83 * happening which might cause dump to croak, destroying
84 * a large number of hours of work.
86 * Every 2 minutes we reprint the message, alerting others
87 * that dump needs attention.
90 static char *attnmessage
; /* attention message */
100 if ((mytty
= fopen(_PATH_TTY
, "r")) == NULL
)
101 quit("fopen on %s fails: %s\n", _PATH_TTY
, strerror(errno
));
102 attnmessage
= question
;
108 if (fgets(replybuffer
, 63, mytty
) == NULL
) {
110 if (++errcount
> 30) /* XXX ugly */
111 quit("excessive operator query failures\n");
112 } else if (replybuffer
[0] == 'y' || replybuffer
[0] == 'Y') {
114 } else if (replybuffer
[0] == 'n' || replybuffer
[0] == 'N') {
117 (void) fprintf(stderr
,
118 " DUMP: \"Yes\" or \"No\"?\n");
119 (void) fprintf(stderr
,
120 " DUMP: %s: (\"yes\" or \"no\") ", question
);
125 * Turn off the alarm, and reset the signal to trap out..
128 if (signal(SIGALRM
, sig
) == SIG_IGN
)
129 signal(SIGALRM
, SIG_IGN
);
130 (void) fclose(mytty
);
137 * Alert the console operator, and enable the alarm clock to
138 * sleep for 2 minutes in case nobody comes to satisfy dump
145 (void) fprintf(stderr
,
146 " DUMP: %s: (\"yes\" or \"no\") ",
153 broadcast(""); /* just print last msg */
155 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
158 signal(SIGALRM
, alarmcatch
);
164 * Here if an inquisitive operator interrupts the dump program
170 msg("Interrupt received.\n");
171 if (query("Do you want to abort dump?"))
176 * The following variables and routines manage alerting
177 * operators to the status of dump.
178 * This works much like wall(1) does.
183 * Get the names from the group entry "operator" to notify.
188 if (!notify
) /*not going to notify*/
190 gp
= getgrnam(OPGRENT
);
193 msg("No group entry for %s.\n", OPGRENT
);
199 struct tm
*localclock
;
202 * We fork a child to do the actual broadcasting, so
203 * that the process control groups are not messed up
215 if (!notify
|| gp
== NULL
)
218 switch (pid
= fork()) {
224 while (wait(&s
) != pid
)
229 clock
= time((time_t *)0);
230 localclock
= localtime(&clock
);
232 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
233 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
237 while (!feof(f_utmp
)) {
238 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
240 if (utmp
.ut_name
[0] == 0)
242 for (np
= gp
->gr_mem
; *np
; np
++) {
243 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
246 * Do not send messages to operators on dialups
248 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
251 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
253 sendmes(utmp
.ut_line
, message
);
256 (void) fclose(f_utmp
);
257 Exit(0); /* the wait in this same routine will catch this */
262 sendmes(tty
, message
)
265 char t
[MAXPATHLEN
], buf
[BUFSIZ
];
270 (void) strcpy(t
, _PATH_DEV
);
271 (void) strncat(t
, tty
, sizeof t
- strlen(_PATH_DEV
) - 1);
273 if ((f_tty
= fopen(t
, "w")) != NULL
) {
275 (void) fprintf(f_tty
,
277 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
278 DUMP: NEEDS ATTENTION: ",
279 localclock
->tm_hour
, localclock
->tm_min
);
280 for (cp
= lastmsg
; ; cp
++) {
291 (void) putc('\r', f_tty
);
292 (void) putc(*cp
, f_tty
);
294 (void) fclose(f_tty
);
299 * print out an estimate of the amount of time left to do the dump
302 time_t tschedule
= 0;
309 (void) time((time_t *) &tnow
);
310 if (tnow
>= tschedule
) {
311 tschedule
= tnow
+ 300;
312 if (blockswritten
< 500)
314 deltat
= tstart_writing
- tnow
+
315 (1.0 * (tnow
- tstart_writing
))
316 / blockswritten
* tapesize
;
317 msg("%3.2f%% done, finished in %d:%02d\n",
318 (blockswritten
* 100.0) / tapesize
,
319 deltat
/ 3600, (deltat
% 3600) / 60);
325 msg(const char *fmt
, ...)
334 (void) fprintf(stderr
," DUMP: ");
336 (void) fprintf(stderr
, "pid=%d ", getpid());
343 (void) vfprintf(stderr
, fmt
, ap
);
344 (void) fflush(stdout
);
345 (void) fflush(stderr
);
346 (void) vsprintf(lastmsg
, fmt
, ap
);
352 msgtail(const char *fmt
, ...)
354 msgtail(fmt
, va_alist
)
365 (void) vfprintf(stderr
, fmt
, ap
);
371 quit(const char *fmt
, ...)
380 (void) fprintf(stderr
," DUMP: ");
382 (void) fprintf(stderr
, "pid=%d ", getpid());
389 (void) vfprintf(stderr
, fmt
, ap
);
391 (void) fflush(stdout
);
392 (void) fflush(stderr
);
397 * Tell the operator what has to be done;
398 * we don't actually do it
403 register struct fstab
*fs
;
405 register struct fstab
*new;
407 new = (struct fstab
*)malloc(sizeof (*fs
));
409 (new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
410 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
411 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
412 quit("%s\n", strerror(errno
));
413 new->fs_passno
= fs
->fs_passno
;
414 new->fs_freq
= fs
->fs_freq
;
419 struct pfstab
*pf_next
;
420 struct fstab
*pf_fstab
;
423 static struct pfstab
*table
;
428 register struct fstab
*fs
;
429 register struct pfstab
*pf
;
431 if (setfsent() == 0) {
432 msg("Can't open %s for dump table information: %s\n",
433 _PATH_FSTAB
, strerror(errno
));
436 while ((fs
= getfsent()) != NULL
) {
437 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
438 strcmp(fs
->fs_type
, FSTAB_RO
) &&
439 strcmp(fs
->fs_type
, FSTAB_RQ
))
442 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
443 quit("%s\n", strerror(errno
));
452 * Search in the fstab for a file name.
453 * This file name can be either the special or the path file name.
455 * The entries in the fstab are the BLOCK special names, not the
456 * character special names.
457 * The caller of fstabsearch assures that the character device
458 * is dumped (that is much faster)
460 * The file name can omit the leading '/'.
466 register struct pfstab
*pf
;
467 register struct fstab
*fs
;
470 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
472 if (strcmp(fs
->fs_file
, key
) == 0 ||
473 strcmp(fs
->fs_spec
, key
) == 0)
475 rn
= rawname(fs
->fs_spec
);
476 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
479 if (*fs
->fs_spec
== '/' &&
480 strcmp(fs
->fs_spec
+ 1, key
) == 0)
482 if (*fs
->fs_file
== '/' &&
483 strcmp(fs
->fs_file
+ 1, key
) == 0)
492 fstabsearchdir(key
, directory
)
496 register struct pfstab
*pf
;
497 register struct fstab
*fs
;
498 register struct fstab
*found_fs
= NULL
;
499 unsigned int size
= 0;
501 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
503 if (strlen(fs
->fs_file
) > size
&&
504 strlen(key
) > strlen(fs
->fs_file
) + 2 &&
505 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
506 (key
[strlen(fs
->fs_file
)] == '/' ||
507 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
509 size
= strlen(fs
->fs_file
);
512 if (found_fs
!= NULL
) {
514 * Ok, we have found a fstab entry which matches the argument
515 * We have to split the argument name into:
516 * - a device name (from the fstab entry)
517 * - a directory name on this device
519 strcpy(directory
, key
+ size
);
526 * Tell the operator what to do
530 char arg
; /* w ==> just what to do; W ==> most recent dumps */
533 register struct fstab
*dt
;
534 register struct dumpdates
*dtwalk
;
535 char *lastname
, *date
;
540 getfstab(); /* /etc/fstab input */
541 initdumptimes(); /* /etc/dumpdates input */
542 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
545 (void) printf("Dump these file systems:\n");
547 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
549 ITITERATE(i
, dtwalk
) {
550 if (strncmp(lastname
, dtwalk
->dd_name
,
551 sizeof(dtwalk
->dd_name
)) == 0)
553 date
= (char *)ctime(&dtwalk
->dd_ddate
);
554 date
[16] = '\0'; /* blast away seconds and year */
555 lastname
= dtwalk
->dd_name
;
556 dt
= fstabsearch(dtwalk
->dd_name
);
557 dumpme
= (dt
!= NULL
&&
559 dtwalk
->dd_ddate
< tnow
- (dt
->fs_freq
* 86400));
560 if (arg
!= 'w' || dumpme
)
562 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
563 dumpme
&& (arg
!= 'w') ? '>' : ' ',
565 dt
? dt
->fs_file
: "",
575 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
576 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
579 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
581 return (d2
->dd_ddate
- d1
->dd_ddate
);