]>
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
41 * $Id: optr.c,v 1.5 1999/10/11 13:31:11 stelian Exp $
44 #include <sys/param.h>
60 #include <linux/ext2_fs.h>
61 #include <ext2fs/ext2fs.h>
62 #include <bsdcompat.h>
67 #include "pathnames.h"
69 static void alarmcatch
__P((int));
70 int datesort
__P((const void *, const void *));
71 static void sendmes
__P((const char *, const char *));
74 * Query the operator; This previously-fascist piece of code
75 * no longer requires an exact response.
76 * It is intended to protect dump aborting by inquisitive
77 * people banging on the console terminal to see what is
78 * happening which might cause dump to croak, destroying
79 * a large number of hours of work.
81 * Every 2 minutes we reprint the message, alerting others
82 * that dump needs attention.
85 static const char *attnmessage
; /* attention message */
88 query(const char *question
)
93 time_t firstprompt
, when_answered
;
96 (void)time4(&(firstprompt
));
98 (void)time((time_t *)&(firstprompt
));
101 if ((mytty
= fopen(_PATH_TTY
, "r")) == NULL
)
102 quit("fopen on %s fails: %s\n", _PATH_TTY
, strerror(errno
));
103 attnmessage
= question
;
109 if (fgets(replybuffer
, 63, mytty
) == NULL
) {
111 if (++errcount
> 30) /* XXX ugly */
112 quit("excessive operator query failures\n");
113 } else if (replybuffer
[0] == 'y' || replybuffer
[0] == 'Y') {
115 } else if (replybuffer
[0] == 'n' || replybuffer
[0] == 'N') {
118 (void) fprintf(stderr
,
119 " DUMP: \"Yes\" or \"No\"?\n");
120 (void) fprintf(stderr
,
121 " DUMP: %s: (\"yes\" or \"no\") ", question
);
126 * Turn off the alarm, and reset the signal to trap out..
129 if (signal(SIGALRM
, sig
) == SIG_IGN
)
130 signal(SIGALRM
, SIG_IGN
);
131 (void) fclose(mytty
);
133 (void)time4(&(when_answered
));
135 (void)time((time_t *)&(when_answered
));
138 * Adjust the base for time estimates to ignore time we spent waiting
139 * for operator input.
141 if ((tstart_writing
!= 0) && (when_answered
!= (time_t)-1) && (firstprompt
!= (time_t)-1))
142 tstart_writing
+= (when_answered
- firstprompt
);
146 char lastmsg
[BUFSIZ
];
149 * Alert the console operator, and enable the alarm clock to
150 * sleep for 2 minutes in case nobody comes to satisfy dump
153 alarmcatch(int signo
)
155 int save_errno
= errno
;
158 (void) fprintf(stderr
,
159 " DUMP: %s: (\"yes\" or \"no\") ",
166 broadcast(""); /* just print last msg */
168 (void) fprintf(stderr
," DUMP: %s: (\"yes\" or \"no\") ",
171 signal(SIGALRM
, alarmcatch
);
178 * Here if an inquisitive operator interrupts the dump program
183 msg("Interrupt received.\n");
184 if (query("Do you want to abort dump?"))
189 * The following variables and routines manage alerting
190 * operators to the status of dump.
191 * This works much like wall(1) does.
196 * Get the names from the group entry "operator" to notify.
201 if (!notify
) /*not going to notify*/
203 gp
= getgrnam(OPGRENT
);
206 msg("No group entry for %s.\n", OPGRENT
);
212 struct tm
*localclock
;
215 * We fork a child to do the actual broadcasting, so
216 * that the process control groups are not messed up
219 broadcast(const char *message
)
227 if (!notify
|| gp
== NULL
)
230 switch (pid
= fork()) {
236 while (wait(&s
) != pid
)
241 clock
= time((time_t *)0);
242 localclock
= localtime(&clock
);
244 if ((f_utmp
= fopen(_PATH_UTMP
, "r")) == NULL
) {
245 msg("Cannot open %s: %s\n", _PATH_UTMP
, strerror(errno
));
249 while (!feof(f_utmp
)) {
250 if (fread((char *) &utmp
, sizeof (struct utmp
), 1, f_utmp
) != 1)
252 if (utmp
.ut_name
[0] == 0)
254 for (np
= gp
->gr_mem
; *np
; np
++) {
255 if (strncmp(*np
, utmp
.ut_name
, sizeof(utmp
.ut_name
)) != 0)
258 * Do not send messages to operators on dialups
260 if (strncmp(utmp
.ut_line
, DIALUP
, strlen(DIALUP
)) == 0)
263 msg("Message to %s at %s\n", *np
, utmp
.ut_line
);
265 sendmes(utmp
.ut_line
, message
);
268 (void) fclose(f_utmp
);
269 Exit(0); /* the wait in this same routine will catch this */
274 sendmes(const char *tty
, const char *message
)
276 char t
[MAXPATHLEN
], buf
[BUFSIZ
];
277 register const char *cp
;
281 (void) strcpy(t
, _PATH_DEV
);
282 (void) strncat(t
, tty
, sizeof t
- strlen(_PATH_DEV
) - 1);
284 if ((f_tty
= fopen(t
, "w")) != NULL
) {
286 (void) fprintf(f_tty
,
288 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
289 DUMP: NEEDS ATTENTION: ",
290 localclock
->tm_hour
, localclock
->tm_min
);
291 for (cp
= lastmsg
; ; cp
++) {
295 if (!(cp
&& *cp
!= '\0'))
302 (void) putc('\r', f_tty
);
303 (void) putc(*cp
, f_tty
);
305 (void) fclose(f_tty
);
310 * print out an estimate of the amount of time left to do the dump
313 time_t tschedule
= 0;
323 (void) time((time_t *) &tnow
);
325 if (tnow
>= tschedule
) {
326 tschedule
= tnow
+ 300;
327 if (blockswritten
< 500)
329 deltat
= tstart_writing
- tnow
+
330 (1.0 * (tnow
- tstart_writing
))
331 / blockswritten
* tapesize
;
332 msg("%3.2f%% done, finished in %d:%02d\n",
333 (blockswritten
* 100.0) / tapesize
,
334 deltat
/ 3600, (deltat
% 3600) / 60);
340 msg(const char *fmt
, ...)
349 (void) fprintf(stderr
," DUMP: ");
351 (void) fprintf(stderr
, "pid=%d ", getpid());
358 (void) vfprintf(stderr
, fmt
, ap
);
359 (void) fflush(stdout
);
360 (void) fflush(stderr
);
361 (void) vsnprintf(lastmsg
, sizeof(lastmsg
), fmt
, ap
);
367 msgtail(const char *fmt
, ...)
369 msgtail(fmt
, va_alist
)
380 (void) vfprintf(stderr
, fmt
, ap
);
386 quit(const char *fmt
, ...)
395 (void) fprintf(stderr
," DUMP: ");
397 (void) fprintf(stderr
, "pid=%d ", getpid());
404 (void) vfprintf(stderr
, fmt
, ap
);
406 (void) fflush(stdout
);
407 (void) fflush(stderr
);
412 * Tell the operator what has to be done;
413 * we don't actually do it
416 static struct fstab
*
417 allocfsent(struct fstab
*fs
)
419 register struct fstab
*new;
421 new = (struct fstab
*)malloc(sizeof (*fs
));
423 (new->fs_file
= strdup(fs
->fs_file
)) == NULL
||
424 (new->fs_type
= strdup(fs
->fs_type
)) == NULL
||
425 (new->fs_spec
= strdup(fs
->fs_spec
)) == NULL
)
426 quit("%s\n", strerror(errno
));
427 new->fs_passno
= fs
->fs_passno
;
428 new->fs_freq
= fs
->fs_freq
;
433 struct pfstab
*pf_next
;
434 struct fstab
*pf_fstab
;
437 static struct pfstab
*table
;
442 register struct fstab
*fs
;
443 register struct pfstab
*pf
;
445 if (setfsent() == 0) {
446 msg("Can't open %s for dump table information: %s\n",
447 _PATH_FSTAB
, strerror(errno
));
450 while ((fs
= getfsent()) != NULL
) {
451 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
452 strcmp(fs
->fs_type
, FSTAB_RO
) &&
453 strcmp(fs
->fs_type
, FSTAB_RQ
))
456 if ((pf
= (struct pfstab
*)malloc(sizeof (*pf
))) == NULL
)
457 quit("%s\n", strerror(errno
));
466 * Search in the fstab for a file name.
467 * This file name can be either the special or the path file name.
469 * The entries in the fstab are the BLOCK special names, not the
470 * character special names.
471 * The caller of fstabsearch assures that the character device
472 * is dumped (that is much faster)
474 * The file name can omit the leading '/'.
477 fstabsearch(const char *key
)
479 register struct pfstab
*pf
;
480 register struct fstab
*fs
;
483 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
485 if (strcmp(fs
->fs_file
, key
) == 0 ||
486 strcmp(fs
->fs_spec
, key
) == 0)
488 rn
= rawname(fs
->fs_spec
);
489 if (rn
!= NULL
&& strcmp(rn
, key
) == 0)
492 if (*fs
->fs_spec
== '/' &&
493 strcmp(fs
->fs_spec
+ 1, key
) == 0)
495 if (*fs
->fs_file
== '/' &&
496 strcmp(fs
->fs_file
+ 1, key
) == 0)
505 fstabsearchdir(const char *key
, char *directory
)
507 register struct pfstab
*pf
;
508 register struct fstab
*fs
;
509 register struct fstab
*found_fs
= NULL
;
510 unsigned int size
= 0;
513 if (stat(key
, &buf
) == 0 && S_ISBLK(buf
.st_mode
))
516 for (pf
= table
; pf
!= NULL
; pf
= pf
->pf_next
) {
518 if (strlen(fs
->fs_file
) > size
&&
519 strlen(key
) > strlen(fs
->fs_file
) + 2 &&
520 strncmp(fs
->fs_file
, key
, strlen(fs
->fs_file
)) == 0 &&
521 (key
[strlen(fs
->fs_file
)] == '/' ||
522 fs
->fs_file
[strlen(fs
->fs_file
) - 1] == '/')) {
524 size
= strlen(fs
->fs_file
);
527 if (found_fs
!= NULL
) {
529 * Ok, we have found a fstab entry which matches the argument
530 * We have to split the argument name into:
531 * - a device name (from the fstab entry)
532 * - a directory name on this device
534 strcpy(directory
, key
+ size
);
541 * Tell the operator what to do
544 lastdump(char arg
) /* w ==> just what to do; W ==> most recent dumps */
547 register struct fstab
*dt
;
548 register struct dumpdates
*dtwalk
=NULL
;
549 char *lastname
, *date
;
554 getfstab(); /* /etc/fstab input */
555 initdumptimes(0); /* dumpdates input */
556 if (ddatev
!= NULL
) {
557 qsort((char *) ddatev
, nddates
, sizeof(struct dumpdates
*), datesort
);
560 (void) printf("Dump these file systems:\n");
562 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
564 ITITERATE(i
, dtwalk
) {
565 if (strncmp(lastname
, dtwalk
->dd_name
,
566 sizeof(dtwalk
->dd_name
)) == 0)
568 date
= (char *)ctime(&dtwalk
->dd_ddate
);
569 date
[16] = '\0'; /* blast away seconds and year */
570 lastname
= dtwalk
->dd_name
;
571 dt
= fstabsearch(dtwalk
->dd_name
);
572 dumpme
= (dt
!= NULL
&&
574 dtwalk
->dd_ddate
< tnow
- (dt
->fs_freq
* 86400));
575 if (arg
!= 'w' || dumpme
)
577 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
578 dumpme
&& (arg
!= 'w') ? '>' : ' ',
580 dt
? dt
->fs_file
: "",
588 datesort(const void *a1
, const void *a2
)
590 struct dumpdates
*d1
= *(struct dumpdates
**)a1
;
591 struct dumpdates
*d2
= *(struct dumpdates
**)a2
;
594 diff
= strncmp(d1
->dd_name
, d2
->dd_name
, sizeof(d1
->dd_name
));
596 return (d2
->dd_ddate
- d1
->dd_ddate
);