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