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