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