]> git.wh0rd.org - dump.git/blob - dump/optr.c
Added the throughput information in dump's progression messages.
[dump.git] / dump / optr.c
1 /*
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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
7 */
8
9 /*-
10 * Copyright (c) 1980, 1988, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
28 *
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
39 * SUCH DAMAGE.
40 */
41
42 #ifndef lint
43 static const char rcsid[] =
44 "$Id: optr.c,v 1.17 2000/12/05 16:31:36 stelian Exp $";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/wait.h>
49 #include <sys/time.h>
50
51 #include <errno.h>
52 #include <fstab.h>
53 #include <grp.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <stdarg.h>
58 #include <unistd.h>
59 #include <utmp.h>
60 #include <sys/stat.h>
61
62 #ifdef __linux__
63 #include <linux/ext2_fs.h>
64 #include <ext2fs/ext2fs.h>
65 #include <bsdcompat.h>
66 #include <signal.h>
67 #endif
68
69 #include "dump.h"
70 #include "pathnames.h"
71 #include "bylabel.h"
72
73 static void alarmcatch __P((int));
74 int datesort __P((const void *, const void *));
75 static void sendmes __P((const char *, const char *));
76
77 /* List of filesystem types that we can dump (same ext2 on-disk format) */
78 static char *fstypes[] = { "ext2", "ext3", "InterMezzo", NULL };
79
80 /*
81 * Query the operator; This previously-fascist piece of code
82 * no longer requires an exact response.
83 * It is intended to protect dump aborting by inquisitive
84 * people banging on the console terminal to see what is
85 * happening which might cause dump to croak, destroying
86 * a large number of hours of work.
87 *
88 * Every 2 minutes we reprint the message, alerting others
89 * that dump needs attention.
90 */
91 static int timeout;
92 static const char *attnmessage; /* attention message */
93
94 int
95 query(const char *question)
96 {
97 char replybuffer[64];
98 int back, errcount;
99 FILE *mytty;
100 time_t firstprompt, when_answered;
101
102 #ifdef __linux__
103 (void)time4(&(firstprompt));
104 #else
105 (void)time((time_t *)&(firstprompt));
106 #endif
107
108 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
109 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
110 attnmessage = question;
111 timeout = 0;
112 alarmcatch(0);
113 back = -1;
114 errcount = 0;
115 do {
116 if (fgets(replybuffer, 63, mytty) == NULL) {
117 clearerr(mytty);
118 if (++errcount > 30) /* XXX ugly */
119 quit("excessive operator query failures\n");
120 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
121 back = 1;
122 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
123 back = 0;
124 } else {
125 (void) fprintf(stderr,
126 " DUMP: \"Yes\" or \"No\"?\n");
127 (void) fprintf(stderr,
128 " DUMP: %s: (\"yes\" or \"no\") ", question);
129 }
130 } while (back < 0);
131
132 /*
133 * Turn off the alarm, and reset the signal to trap out..
134 */
135 (void) alarm(0);
136 if (signal(SIGALRM, sig) == SIG_IGN)
137 signal(SIGALRM, SIG_IGN);
138 (void) fclose(mytty);
139 #ifdef __linux__
140 (void)time4(&(when_answered));
141 #else
142 (void)time((time_t *)&(when_answered));
143 #endif
144 /*
145 * Adjust the base for time estimates to ignore time we spent waiting
146 * for operator input.
147 */
148 if ((tstart_writing != 0) && (when_answered != (time_t)-1) && (firstprompt != (time_t)-1))
149 tstart_writing += (when_answered - firstprompt);
150 return(back);
151 }
152
153 char lastmsg[BUFSIZ];
154
155 /*
156 * Alert the console operator, and enable the alarm clock to
157 * sleep for 2 minutes in case nobody comes to satisfy dump
158 */
159 static void
160 alarmcatch(int signo)
161 {
162 int save_errno = errno;
163 if (notify == 0) {
164 if (timeout == 0)
165 (void) fprintf(stderr,
166 " DUMP: %s: (\"yes\" or \"no\") ",
167 attnmessage);
168 else
169 msgtail("\7\7");
170 } else {
171 if (timeout) {
172 msgtail("\n");
173 broadcast(""); /* just print last msg */
174 }
175 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
176 attnmessage);
177 }
178 signal(SIGALRM, alarmcatch);
179 (void) alarm(120);
180 timeout = 1;
181 errno = save_errno;
182 }
183
184 /*
185 * Here if an inquisitive operator interrupts the dump program
186 */
187 void
188 interrupt(int signo)
189 {
190 msg("Interrupt received.\n");
191 if (query("Do you want to abort dump?"))
192 dumpabort(0);
193 }
194
195 /*
196 * The following variables and routines manage alerting
197 * operators to the status of dump.
198 * This works much like wall(1) does.
199 */
200 struct group *gp;
201
202 /*
203 * Get the names from the group entry "operator" to notify.
204 */
205 void
206 set_operators(void)
207 {
208 if (!notify) /*not going to notify*/
209 return;
210 gp = getgrnam(OPGRENT);
211 (void) endgrent();
212 if (gp == NULL) {
213 msg("No group entry for %s.\n", OPGRENT);
214 notify = 0;
215 return;
216 }
217 }
218
219 struct tm *localclock;
220
221 /*
222 * We fork a child to do the actual broadcasting, so
223 * that the process control groups are not messed up
224 */
225 void
226 broadcast(const char *message)
227 {
228 time_t clock;
229 FILE *f_utmp;
230 struct utmp utmp;
231 char **np;
232 int pid, s;
233
234 if (!notify || gp == NULL)
235 return;
236
237 switch (pid = fork()) {
238 case -1:
239 return;
240 case 0:
241 break;
242 default:
243 while (wait(&s) != pid)
244 continue;
245 return;
246 }
247
248 clock = time((time_t *)0);
249 localclock = localtime(&clock);
250
251 if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) {
252 msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno));
253 return;
254 }
255
256 while (!feof(f_utmp)) {
257 if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
258 break;
259 if (utmp.ut_name[0] == 0)
260 continue;
261 for (np = gp->gr_mem; *np; np++) {
262 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
263 continue;
264 /*
265 * Do not send messages to operators on dialups
266 */
267 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
268 continue;
269 #ifdef DEBUG
270 msg("Message to %s at %s\n", *np, utmp.ut_line);
271 #endif
272 sendmes(utmp.ut_line, message);
273 }
274 }
275 (void) fclose(f_utmp);
276 Exit(0); /* the wait in this same routine will catch this */
277 /* NOTREACHED */
278 }
279
280 static void
281 sendmes(const char *tty, const char *message)
282 {
283 char t[MAXPATHLEN], buf[BUFSIZ];
284 register const char *cp;
285 int lmsg = 1;
286 FILE *f_tty;
287
288 (void) strcpy(t, _PATH_DEV);
289 (void) strncat(t, tty, sizeof t - strlen(_PATH_DEV) - 1);
290
291 if ((f_tty = fopen(t, "w")) != NULL) {
292 setbuf(f_tty, buf);
293 (void) fprintf(f_tty,
294 "\n\
295 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\
296 DUMP: NEEDS ATTENTION: ",
297 localclock->tm_hour, localclock->tm_min);
298 for (cp = lastmsg; ; cp++) {
299 if (*cp == '\0') {
300 if (lmsg) {
301 cp = message;
302 if (!(cp && *cp != '\0'))
303 break;
304 lmsg = 0;
305 } else
306 break;
307 }
308 if (*cp == '\n')
309 (void) putc('\r', f_tty);
310 (void) putc(*cp, f_tty);
311 }
312 (void) fclose(f_tty);
313 }
314 }
315
316 /*
317 * print out an estimate of the amount of time left to do the dump
318 */
319
320 time_t tschedule = 0;
321
322 void
323 timeest(void)
324 {
325 time_t tnow;
326
327 #ifdef __linux__
328 (void) time4(&tnow);
329 #else
330 (void) time((time_t *) &tnow);
331 #endif
332 if (tnow >= tschedule) {
333 char *buf = mktimeest(tnow);
334 tschedule = tnow + 300;
335 if (buf) {
336 fprintf(stderr, " DUMP: ");
337 fwrite(buf, strlen(buf), 1, stderr);
338 fflush(stderr);
339 }
340 }
341 }
342
343 void
344 #ifdef __STDC__
345 msg(const char *fmt, ...)
346 #else
347 msg(fmt, va_alist)
348 char *fmt;
349 va_dcl
350 #endif
351 {
352 va_list ap;
353
354 (void) fprintf(stderr," DUMP: ");
355 #ifdef TDEBUG
356 (void) fprintf(stderr, "pid=%d ", getpid());
357 #endif
358 #ifdef __STDC__
359 va_start(ap, fmt);
360 #else
361 va_start(ap);
362 #endif
363 (void) vfprintf(stderr, fmt, ap);
364 va_end(ap);
365 (void) fflush(stdout);
366 (void) fflush(stderr);
367 #ifdef __STDC__
368 va_start(ap, fmt);
369 #else
370 va_start(ap);
371 #endif
372 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
373 va_end(ap);
374 }
375
376 void
377 #ifdef __STDC__
378 msgtail(const char *fmt, ...)
379 #else
380 msgtail(fmt, va_alist)
381 char *fmt;
382 va_dcl
383 #endif
384 {
385 va_list ap;
386 #ifdef __STDC__
387 va_start(ap, fmt);
388 #else
389 va_start(ap);
390 #endif
391 (void) vfprintf(stderr, fmt, ap);
392 va_end(ap);
393 }
394
395 void
396 #ifdef __STDC__
397 quit(const char *fmt, ...)
398 #else
399 quit(fmt, va_alist)
400 char *fmt;
401 va_dcl
402 #endif
403 {
404 va_list ap;
405
406 (void) fprintf(stderr," DUMP: ");
407 #ifdef TDEBUG
408 (void) fprintf(stderr, "pid=%d ", getpid());
409 #endif
410 #ifdef __STDC__
411 va_start(ap, fmt);
412 #else
413 va_start(ap);
414 #endif
415 (void) vfprintf(stderr, fmt, ap);
416 va_end(ap);
417 (void) fflush(stdout);
418 (void) fflush(stderr);
419 dumpabort(0);
420 }
421
422 /*
423 * Tell the operator what has to be done;
424 * we don't actually do it
425 */
426
427 static struct fstab *
428 allocfsent(struct fstab *fs)
429 {
430 register struct fstab *new;
431
432 new = (struct fstab *)malloc(sizeof (*fs));
433 if (new == NULL)
434 quit("%s\n", strerror(errno));
435 if (strlen(fs->fs_file) > 1 && fs->fs_file[strlen(fs->fs_file) - 1] == '/')
436 fs->fs_file[strlen(fs->fs_file) - 1] = '\0';
437 if ((new->fs_file = strdup(fs->fs_file)) == NULL ||
438 (new->fs_type = strdup(fs->fs_type)) == NULL ||
439 (new->fs_vfstype = strdup(fs->fs_vfstype)) == NULL ||
440 (new->fs_spec = strdup(fs->fs_spec)) == NULL)
441 quit("%s\n", strerror(errno));
442 new->fs_passno = fs->fs_passno;
443 new->fs_freq = fs->fs_freq;
444 return (new);
445 }
446
447 struct pfstab {
448 struct pfstab *pf_next;
449 struct dumpdates *pf_dd;
450 struct fstab *pf_fstab;
451 };
452
453 static struct pfstab *table;
454
455 void
456 getfstab(void)
457 {
458 struct fstab *fs;
459 struct pfstab *pf;
460 struct pfstab *pfold = NULL;
461
462 if (setfsent() == 0) {
463 msg("Can't open %s for dump table information: %s\n",
464 _PATH_FSTAB, strerror(errno));
465 return;
466 }
467 while ((fs = getfsent()) != NULL) {
468 if (strcmp(fs->fs_type, FSTAB_RW) &&
469 strcmp(fs->fs_type, FSTAB_RO) &&
470 strcmp(fs->fs_type, FSTAB_RQ))
471 continue;
472 fs = allocfsent(fs);
473 fs->fs_passno = 0;
474 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
475 quit("%s\n", strerror(errno));
476 pf->pf_fstab = fs;
477 pf->pf_next = NULL;
478
479 /* keep table in /etc/fstab order for use with -w and -W */
480 if (pfold) {
481 pfold->pf_next = pf;
482 pfold = pf;
483 } else
484 pfold = table = pf;
485
486 }
487 (void) endfsent();
488 }
489
490 /*
491 * Search in the fstab for a file name.
492 * This file name can be either the special or the path file name.
493 *
494 * The entries in the fstab are the BLOCK special names, not the
495 * character special names.
496 * The caller of fstabsearch assures that the character device
497 * is dumped (that is much faster)
498 *
499 * The file name can omit the leading '/'.
500 */
501 struct fstab *
502 fstabsearch(const char *key)
503 {
504 register struct pfstab *pf;
505 register struct fstab *fs;
506 char *rn;
507
508 for (pf = table; pf != NULL; pf = pf->pf_next) {
509 fs = pf->pf_fstab;
510 if (strcmp(fs->fs_file, key) == 0 ||
511 strcmp(fs->fs_spec, key) == 0)
512 return (fs);
513 rn = rawname(fs->fs_spec);
514 if (rn != NULL && strcmp(rn, key) == 0)
515 return (fs);
516 if (key[0] != '/') {
517 if (*fs->fs_spec == '/' &&
518 strcmp(fs->fs_spec + 1, key) == 0)
519 return (fs);
520 if (*fs->fs_file == '/' &&
521 strcmp(fs->fs_file + 1, key) == 0)
522 return (fs);
523 }
524 }
525 return (NULL);
526 }
527
528 #ifdef __linux__
529 struct fstab *
530 fstabsearchdir(const char *key, char *directory)
531 {
532 register struct pfstab *pf;
533 register struct fstab *fs;
534 register struct fstab *found_fs = NULL;
535 unsigned int size = 0;
536 struct stat buf;
537
538 if (stat(key, &buf) == 0 && S_ISBLK(buf.st_mode))
539 return NULL;
540
541 for (pf = table; pf != NULL; pf = pf->pf_next) {
542 fs = pf->pf_fstab;
543 if (strlen(fs->fs_file) > size &&
544 strlen(key) > strlen(fs->fs_file) + 1 &&
545 strncmp(fs->fs_file, key, strlen(fs->fs_file)) == 0 &&
546 (key[strlen(fs->fs_file)] == '/' ||
547 fs->fs_file[strlen(fs->fs_file) - 1] == '/')) {
548 found_fs = fs;
549 size = strlen(fs->fs_file);
550 }
551 }
552 if (found_fs != NULL) {
553 /*
554 * Ok, we have found a fstab entry which matches the argument
555 * We have to split the argument name into:
556 * - a device name (from the fstab entry)
557 * - a directory name on this device
558 */
559 strcpy(directory, key + size);
560 }
561 return(found_fs);
562 }
563 #endif
564
565 static void
566 print_wmsg(char arg, int dumpme, const char *dev, int level,
567 const char *mtpt, time_t ddate)
568 {
569 char *date;
570
571 if (ddate) {
572 char *d;
573 date = (char *)ctime(&ddate);
574 d = strchr(date, '\n');
575 if (d) *d = '\0';
576 }
577
578 if (!dumpme && arg == 'w')
579 return;
580
581 (void) printf("%c %8s\t(%6s) Last dump: ",
582 dumpme && (arg != 'w') ? '>' : ' ',
583 dev,
584 mtpt ? mtpt : "");
585
586 if (ddate)
587 printf("Level %c, Date %s\n",
588 level, date);
589 else
590 printf("never\n");
591 }
592
593 /*
594 * Tell the operator what to do
595 */
596 void
597 lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */
598 {
599 struct pfstab *pf;
600 time_t tnow;
601
602 (void) time(&tnow);
603 getfstab(); /* /etc/fstab input */
604 initdumptimes(0); /* dumpdates input */
605 if (ddatev == NULL && table == NULL) {
606 (void) printf("No %s or %s file found\n",
607 _PATH_FSTAB, _PATH_DUMPDATES);
608 return;
609 }
610
611 if (arg == 'w')
612 (void) printf("Dump these file systems:\n");
613 else
614 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
615
616 if (ddatev != NULL) {
617 struct dumpdates *dtwalk = NULL;
618 int i;
619 char *lastname;
620
621 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
622
623 lastname = "??";
624 ITITERATE(i, dtwalk) {
625 struct fstab *dt;
626 if (strncmp(lastname, dtwalk->dd_name,
627 sizeof(dtwalk->dd_name)) == 0)
628 continue;
629 lastname = dtwalk->dd_name;
630 if ((dt = dtwalk->dd_fstab) != NULL) {
631 /* Overload fs_freq as dump level and
632 * fs_passno as date, because we can't
633 * change struct fstab format.
634 * A negative fs_freq means this
635 * filesystem needs to be dumped.
636 */
637 dt->fs_passno = dtwalk->dd_ddate;
638 if (dt->fs_freq > 0 && (dtwalk->dd_ddate <
639 tnow - (dt->fs_freq * 86400)))
640 dt->fs_freq = -dtwalk->dd_level - 1;
641 else
642 dt->fs_freq = dtwalk->dd_level + 1;
643
644 }
645 }
646 }
647
648 /* print in /etc/fstab order only those filesystem types we can dump */
649 for (pf = table; pf != NULL; pf = pf->pf_next) {
650 struct fstab *dt = pf->pf_fstab;
651 char **type;
652
653 for (type = fstypes; *type != NULL; type++) {
654 if (strncmp(dt->fs_vfstype, *type,
655 sizeof(dt->fs_vfstype)) == 0) {
656 const char *disk = get_device_name(dt->fs_spec);
657 print_wmsg(arg, dt->fs_freq < 0,
658 disk ? disk : dt->fs_spec,
659 dt->fs_freq < 0 ? -dt->fs_freq - 1 : dt->fs_freq - 1,
660 dt->fs_file,
661 dt->fs_passno);
662 }
663 }
664 }
665
666 /* print in /etc/dumpdates order if not in /etc/fstab */
667 if (ddatev != NULL) {
668 struct dumpdates *dtwalk = NULL;
669 char *lastname;
670 int i;
671
672 lastname = "??";
673 ITITERATE(i, dtwalk) {
674 if (strncmp(lastname, dtwalk->dd_name,
675 sizeof(dtwalk->dd_name)) == 0 ||
676 dtwalk->dd_fstab != NULL)
677 continue;
678 lastname = dtwalk->dd_name;
679 print_wmsg(arg, 0, dtwalk->dd_name,
680 dtwalk->dd_level, NULL, dtwalk->dd_ddate);
681 }
682 }
683 }
684
685 int
686 datesort(const void *a1, const void *a2)
687 {
688 struct dumpdates *d1 = *(struct dumpdates **)a1;
689 struct dumpdates *d2 = *(struct dumpdates **)a2;
690 int diff;
691
692 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
693 if (diff == 0)
694 return (d2->dd_ddate - d1->dd_ddate);
695 return (diff);
696 }