]>
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.3 1999/10/11 12:59:19 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 static void alarmcatch
__P((int));
75 int datesort
__P((const void *, const void *));
76 static void sendmes
__P((const char *, const 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 const char *attnmessage
; /* attention message */
93 query(const char *question
)
98 time_t firstprompt
, when_answered
;
100 firstprompt
= time(NULL
);
102 if ((mytty
= fopen(_PATH_TTY
, "r")) == NULL
)
103 quit("fopen on %s fails: %s\n", _PATH_TTY
, strerror(errno
));
104 attnmessage
= question
;
110 if (fgets(replybuffer
, 63, mytty
) == NULL
) {
112 if (++errcount
> 30) /* XXX ugly */
113 quit("excessive operator query failures\n");
114 } else if (replybuffer
[0] == 'y' || replybuffer
[0] == 'Y') {
116 } else if (replybuffer
[0] == 'n' || replybuffer
[0] == 'N') {
119 (void) fprintf(stderr
,
120 " DUMP: \"Yes\" or \"No\"?\n");
121 (void) fprintf(stderr
,
122 " DUMP: %s: (\"yes\" or \"no\") ", question
);
127 * Turn off the alarm, and reset the signal to trap out..
130 if (signal(SIGALRM
, sig
) == SIG_IGN
)
131 signal(SIGALRM
, SIG_IGN
);
132 (void) fclose(mytty
);
133 when_answered
= time(NULL
);
135 * Adjust the base for time estimates to ignore time we spent waiting
136 * for operator input.
138 if (tstart_writing
!= 0)
139 tstart_writing
+= (when_answered
- firstprompt
);
143 char lastmsg
[BUFSIZ
];
146 * Alert the console operator, and enable the alarm clock to
147 * sleep for 2 minutes in case nobody comes to satisfy dump
150 alarmcatch(int signo
)
152 int save_errno
= errno
;
155 (void) fprintf(stderr
,
156 " DUMP: %s: (\"yes\" or \"no\") ",
163 broadcast(""); /* just print last msg */
165 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
168 signal(SIGALRM
, alarmcatch
);
175 * Here if an inquisitive operator interrupts the dump program
180 msg("Interrupt received.\n");
181 if (query("Do you want to abort dump?"))
186 * The following variables and routines manage alerting
187 * operators to the status of dump.
188 * This works much like wall(1) does.
193 * Get the names from the group entry "operator" to notify.
198 if (!notify
) /*not going to notify*/
200 gp
= getgrnam(OPGRENT
);
203 msg("No group entry for %s.\n", OPGRENT
);
209 struct tm
*localclock
;
212 * We fork a child to do the actual broadcasting, so
213 * that the process control groups are not messed up
216 broadcast(const char *message
)
224 if (!notify
|| gp
== NULL
)
227 switch (pid
= fork()) {
233 while (wait(&s
) != pid
)
238 clock
= time((time_t *)0);
239 localclock
= localtime(&clock
);
241 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
242 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
246 while (!feof(f_utmp
)) {
247 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
249 if (utmp
.ut_name
[0] == 0)
251 for (np
= gp
->gr_mem
; *np
; np
++) {
252 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
255 * Do not send messages to operators on dialups
257 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
260 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
262 sendmes(utmp
.ut_line
, message
);
265 (void) fclose(f_utmp
);
266 Exit(0); /* the wait in this same routine will catch this */
271 sendmes(const char *tty
, const char *message
)
273 char t
[MAXPATHLEN
], buf
[BUFSIZ
];
274 register const char *cp
;
278 (void) strcpy(t
, _PATH_DEV
);
279 (void) strncat(t
, tty
, sizeof t
- strlen(_PATH_DEV
) - 1);
281 if ((f_tty
= fopen(t
, "w")) != NULL
) {
283 (void) fprintf(f_tty
,
285 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
286 DUMP: NEEDS ATTENTION: ",
287 localclock
->tm_hour
, localclock
->tm_min
);
288 for (cp
= lastmsg
; ; cp
++) {
292 if (!(cp
&& *cp
!= '\0'))
299 (void) putc('\r', f_tty
);
300 (void) putc(*cp
, f_tty
);
302 (void) fclose(f_tty
);
307 * print out an estimate of the amount of time left to do the dump
310 time_t tschedule
= 0;
317 (void) time((time_t *) &tnow
);
318 if (tnow
>= tschedule
) {
319 tschedule
= tnow
+ 300;
320 if (blockswritten
< 500)
322 deltat
= tstart_writing
- tnow
+
323 (1.0 * (tnow
- tstart_writing
))
324 / blockswritten
* tapesize
;
325 msg("%3.2f%% done, finished in %d:%02d\n",
326 (blockswritten
* 100.0) / tapesize
,
327 deltat
/ 3600, (deltat
% 3600) / 60);
333 msg(const char *fmt
, ...)
342 (void) fprintf(stderr
," DUMP: ");
344 (void) fprintf(stderr
, "pid=%d ", getpid());
351 (void) vfprintf(stderr
, fmt
, ap
);
352 (void) fflush(stdout
);
353 (void) fflush(stderr
);
354 (void) vsnprintf(lastmsg
, sizeof(lastmsg
), fmt
, ap
);
360 msgtail(const char *fmt
, ...)
362 msgtail(fmt
, va_alist
)
373 (void) vfprintf(stderr
, fmt
, ap
);
379 quit(const char *fmt
, ...)
388 (void) fprintf(stderr
," DUMP: ");
390 (void) fprintf(stderr
, "pid=%d ", getpid());
397 (void) vfprintf(stderr
, fmt
, ap
);
399 (void) fflush(stdout
);
400 (void) fflush(stderr
);
405 * Tell the operator what has to be done;
406 * we don't actually do it
409 static struct fstab
*
410 allocfsent(struct fstab
*fs
)
412 register struct fstab
*new;
414 new = (struct fstab
*)malloc(sizeof (*fs
));
416 (new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
417 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
418 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
419 quit("%s\n", strerror(errno
));
420 new->fs_passno
= fs
->fs_passno
;
421 new->fs_freq
= fs
->fs_freq
;
426 struct pfstab
*pf_next
;
427 struct fstab
*pf_fstab
;
430 static struct pfstab
*table
;
435 register struct fstab
*fs
;
436 register struct pfstab
*pf
;
438 if (setfsent() == 0) {
439 msg("Can't open %s for dump table information: %s\n",
440 _PATH_FSTAB
, strerror(errno
));
443 while ((fs
= getfsent()) != NULL
) {
444 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
445 strcmp(fs
->fs_type
, FSTAB_RO
) &&
446 strcmp(fs
->fs_type
, FSTAB_RQ
))
449 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
450 quit("%s\n", strerror(errno
));
459 * Search in the fstab for a file name.
460 * This file name can be either the special or the path file name.
462 * The entries in the fstab are the BLOCK special names, not the
463 * character special names.
464 * The caller of fstabsearch assures that the character device
465 * is dumped (that is much faster)
467 * The file name can omit the leading '/'.
470 fstabsearch(const char *key
)
472 register struct pfstab
*pf
;
473 register struct fstab
*fs
;
476 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
478 if (strcmp(fs
->fs_file
, key
) == 0 ||
479 strcmp(fs
->fs_spec
, key
) == 0)
481 rn
= rawname(fs
->fs_spec
);
482 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
485 if (*fs
->fs_spec
== '/' &&
486 strcmp(fs
->fs_spec
+ 1, key
) == 0)
488 if (*fs
->fs_file
== '/' &&
489 strcmp(fs
->fs_file
+ 1, key
) == 0)
498 fstabsearchdir(const char *key
, char *directory
)
500 register struct pfstab
*pf
;
501 register struct fstab
*fs
;
502 register struct fstab
*found_fs
= NULL
;
503 unsigned int size
= 0;
505 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
507 if (strlen(fs
->fs_file
) > size
&&
508 strlen(key
) > strlen(fs
->fs_file
) + 2 &&
509 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
510 (key
[strlen(fs
->fs_file
)] == '/' ||
511 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
513 size
= strlen(fs
->fs_file
);
516 if (found_fs
!= NULL
) {
518 * Ok, we have found a fstab entry which matches the argument
519 * We have to split the argument name into:
520 * - a device name (from the fstab entry)
521 * - a directory name on this device
523 strcpy(directory
, key
+ size
);
530 * Tell the operator what to do
533 lastdump(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
* 86400));
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
: "",
575 datesort(const void *a1
, const void *a2
)
577 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
578 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
581 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
583 return (d2
->dd_ddate
- d1
->dd_ddate
);