]>
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, 1995, 1996
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 char sccsid
[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94";
45 #include <sys/param.h>
69 #include <linux/ext2_fs.h>
70 #include <ext2fs/ext2fs.h>
71 #include <bsdcompat.h>
75 #include "pathnames.h"
77 void alarmcatch
__P((/* int, int */));
78 int datesort
__P((const void *, const void *));
79 static void sendmes
__P((char *, char *));
82 * Query the operator; This previously-fascist piece of code
83 * no longer requires an exact response.
84 * It is intended to protect dump aborting by inquisitive
85 * people banging on the console terminal to see what is
86 * happening which might cause dump to croak, destroying
87 * a large number of hours of work.
89 * Every 2 minutes we reprint the message, alerting others
90 * that dump needs attention.
93 static char *attnmessage
; /* attention message */
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
);
140 * Alert the console operator, and enable the alarm clock to
141 * sleep for 2 minutes in case nobody comes to satisfy dump
148 (void) fprintf(stderr
,
149 " DUMP: %s: (\"yes\" or \"no\") ",
156 broadcast(""); /* just print last msg */
158 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
161 signal(SIGALRM
, alarmcatch
);
167 * Here if an inquisitive operator interrupts the dump program
173 msg("Interrupt received.\n");
174 if (query("Do you want to abort dump?"))
179 * The following variables and routines manage alerting
180 * operators to the status of dump.
181 * This works much like wall(1) does.
186 * Get the names from the group entry "operator" to notify.
191 if (!notify
) /*not going to notify*/
193 gp
= getgrnam(OPGRENT
);
196 msg("No group entry for %s.\n", OPGRENT
);
202 struct tm
*localclock
;
205 * We fork a child to do the actual broadcasting, so
206 * that the process control groups are not messed up
218 if (!notify
|| gp
== NULL
)
221 switch (pid
= fork()) {
227 while (wait(&s
) != pid
)
232 clock
= time((time_t *)0);
233 localclock
= localtime(&clock
);
235 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
236 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
240 while (!feof(f_utmp
)) {
241 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
243 if (utmp
.ut_name
[0] == 0)
245 for (np
= gp
->gr_mem
; *np
; np
++) {
246 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
249 * Do not send messages to operators on dialups
251 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
254 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
256 sendmes(utmp
.ut_line
, message
);
259 (void) fclose(f_utmp
);
260 Exit(0); /* the wait in this same routine will catch this */
265 sendmes(tty
, message
)
268 char t
[50], buf
[BUFSIZ
];
273 (void) strcpy(t
, _PATH_DEV
);
274 (void) strcat(t
, tty
);
276 if ((f_tty
= fopen(t
, "w")) != NULL
) {
278 (void) fprintf(f_tty
,
280 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
281 DUMP: NEEDS ATTENTION: ",
282 localclock
->tm_hour
, localclock
->tm_min
);
283 for (cp
= lastmsg
; ; cp
++) {
294 (void) putc('\r', f_tty
);
295 (void) putc(*cp
, f_tty
);
297 (void) fclose(f_tty
);
302 * print out an estimate of the amount of time left to do the dump
305 time_t tschedule
= 0;
312 (void) time((time_t *) &tnow
);
313 if (tnow
>= tschedule
) {
314 tschedule
= tnow
+ 300;
315 if (blockswritten
< 500)
317 deltat
= tstart_writing
- tnow
+
318 (1.0 * (tnow
- tstart_writing
))
319 / blockswritten
* tapesize
;
320 msg("%3.2f%% done, finished in %d:%02d\n",
321 (blockswritten
* 100.0) / tapesize
,
322 deltat
/ 3600, (deltat
% 3600) / 60);
328 msg(const char *fmt
, ...)
337 (void) fprintf(stderr
," DUMP: ");
339 (void) fprintf(stderr
, "pid=%d ", getpid());
346 (void) vfprintf(stderr
, fmt
, ap
);
347 (void) fflush(stdout
);
348 (void) fflush(stderr
);
349 (void) vsprintf(lastmsg
, fmt
, ap
);
355 msgtail(const char *fmt
, ...)
357 msgtail(fmt
, va_alist
)
368 (void) vfprintf(stderr
, fmt
, ap
);
374 quit(const char *fmt
, ...)
383 (void) fprintf(stderr
," DUMP: ");
385 (void) fprintf(stderr
, "pid=%d ", getpid());
392 (void) vfprintf(stderr
, fmt
, ap
);
394 (void) fflush(stdout
);
395 (void) fflush(stderr
);
400 * Tell the operator what has to be done;
401 * we don't actually do it
406 register struct fstab
*fs
;
408 register struct fstab
*new;
410 new = (struct fstab
*)malloc(sizeof (*fs
));
412 (new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
413 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
414 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
415 quit("%s\n", strerror(errno
));
416 new->fs_passno
= fs
->fs_passno
;
417 new->fs_freq
= fs
->fs_freq
;
422 struct pfstab
*pf_next
;
423 struct fstab
*pf_fstab
;
426 static struct pfstab
*table
;
431 register struct fstab
*fs
;
432 register struct pfstab
*pf
;
434 if (setfsent() == 0) {
435 msg("Can't open %s for dump table information: %s\n",
436 _PATH_FSTAB
, strerror(errno
));
439 while ((fs
= getfsent()) != NULL
) {
440 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
441 strcmp(fs
->fs_type
, FSTAB_RO
) &&
442 strcmp(fs
->fs_type
, FSTAB_RQ
))
445 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
446 quit("%s\n", strerror(errno
));
455 * Search in the fstab for a file name.
456 * This file name can be either the special or the path file name.
458 * The entries in the fstab are the BLOCK special names, not the
459 * character special names.
460 * The caller of fstabsearch assures that the character device
461 * is dumped (that is much faster)
463 * The file name can omit the leading '/'.
469 register struct pfstab
*pf
;
470 register struct fstab
*fs
;
473 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
475 if (strcmp(fs
->fs_file
, key
) == 0 ||
476 strcmp(fs
->fs_spec
, key
) == 0)
478 rn
= rawname(fs
->fs_spec
);
479 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
482 if (*fs
->fs_spec
== '/' &&
483 strcmp(fs
->fs_spec
+ 1, key
) == 0)
485 if (*fs
->fs_file
== '/' &&
486 strcmp(fs
->fs_file
+ 1, key
) == 0)
495 fstabsearchdir(key
, directory
)
499 register struct pfstab
*pf
;
500 register struct fstab
*fs
;
501 register struct fstab
*found_fs
= NULL
;
502 unsigned int size
= 0;
504 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
506 if (strlen(fs
->fs_file
) > size
&&
507 strlen(key
) > strlen(fs
->fs_file
) + 2 &&
508 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
509 (key
[strlen(fs
->fs_file
)] == '/' ||
510 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
512 size
= strlen(fs
->fs_file
);
515 if (found_fs
!= NULL
) {
517 * Ok, we have found a fstab entry which matches the argument
518 * We have to split the argument name into:
519 * - a device name (from the fstab entry)
520 * - a directory name on this device
522 strcpy(directory
, key
+ size
);
529 * Tell the operator what to do
533 char arg
; /* w ==> just what to do; W ==> most recent dumps */
536 register struct fstab
*dt
;
537 register struct dumpdates
*dtwalk
;
538 char *lastname
, *date
;
543 getfstab(); /* /etc/fstab input */
544 initdumptimes(); /* /etc/dumpdates input */
545 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
548 (void) printf("Dump these file systems:\n");
550 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
552 ITITERATE(i
, dtwalk
) {
553 if (strncmp(lastname
, dtwalk
->dd_name
,
554 sizeof(dtwalk
->dd_name
)) == 0)
556 date
= (char *)ctime(&dtwalk
->dd_ddate
);
557 date
[16] = '\0'; /* blast away seconds and year */
558 lastname
= dtwalk
->dd_name
;
559 dt
= fstabsearch(dtwalk
->dd_name
);
560 dumpme
= (dt
!= NULL
&&
562 dtwalk
->dd_ddate
< tnow
- (dt
->fs_freq
* SECSPERDAY
));
563 if (arg
!= 'w' || dumpme
)
565 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
566 dumpme
&& (arg
!= 'w') ? '>' : ' ',
568 dt
? dt
->fs_file
: "",
578 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
579 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
582 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
584 return (d2
->dd_ddate
- d1
->dd_ddate
);