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