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