]> git.wh0rd.org Git - dump.git/blob - dump/optr.c
Check for the e2fsprogs header <ext2fs/ext2_fs.h> instead of the kernel header if...
[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.23 2001/06/18 10:58:28 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                 fs->fs_passno = 0;
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         char *date = NULL;
563         
564         if (ddate) {
565                 char *d;
566                 date = (char *)ctime(&ddate);
567                 d = strchr(date, '\n');
568                 if (d) *d = '\0';
569         }
570
571         if (!dumpme && arg == 'w')
572                 return;
573
574         (void) printf("%c %8s\t(%6s) Last dump: ",
575                       dumpme && (arg != 'w') ? '>' : ' ',
576                       dev,
577                       mtpt ? mtpt : "");
578
579         if (ddate)
580                 printf("Level %c, Date %s\n",
581                       level, date);
582         else
583                 printf("never\n");
584 }
585
586 /*
587  *      Tell the operator what to do
588  */
589 void
590 lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */
591 {
592         struct pfstab *pf;
593         time_t tnow;
594
595         tnow = time(NULL);
596         getfstab();             /* /etc/fstab input */
597         initdumptimes(0);       /* dumpdates input */
598         if (ddatev == NULL && table == NULL) {
599                 (void) printf("No %s or %s file found\n",
600                               _PATH_FSTAB, _PATH_DUMPDATES);
601                 return;
602         }
603
604         if (arg == 'w')
605                 (void) printf("Dump these file systems:\n");
606         else
607                 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
608
609         if (ddatev != NULL) {
610                 struct dumpdates *dtwalk = NULL;
611                 int i;
612                 char *lastname;
613
614                 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
615
616                 lastname = "??";
617                 ITITERATE(i, dtwalk) {
618                         struct fstab *dt;
619                         if (strncmp(lastname, dtwalk->dd_name,
620                                 sizeof(dtwalk->dd_name)) == 0)
621                                 continue;
622                         lastname = dtwalk->dd_name;
623                         if ((dt = dtwalk->dd_fstab) != NULL) {
624                                 /* Overload fs_freq as dump level and
625                                  * fs_passno as date, because we can't
626                                  * change struct fstab format.
627                                  * A negative fs_freq means this
628                                  * filesystem needs to be dumped.
629                                  */
630                                 dt->fs_passno = dtwalk->dd_ddate;
631                                 if (dt->fs_freq > 0 && (dtwalk->dd_ddate <
632                                     tnow - (dt->fs_freq * 86400)))
633                                         dt->fs_freq = -dtwalk->dd_level - 1;
634                                 else
635                                         dt->fs_freq = dtwalk->dd_level + 1;
636
637                         }
638                 }
639         }
640
641         /* print in /etc/fstab order only those filesystem types we can dump */
642         for (pf = table; pf != NULL; pf = pf->pf_next) {
643                 struct fstab *dt = pf->pf_fstab;
644                 char **type;
645
646                 for (type = fstypes; *type != NULL; type++) {
647                         if (strncmp(dt->fs_vfstype, *type,
648                                     sizeof(dt->fs_vfstype)) == 0) {
649                                 const char *disk = get_device_name(dt->fs_spec);
650                                 print_wmsg(arg, dt->fs_freq < 0,
651                                            disk ? disk : dt->fs_spec,
652                                            dt->fs_freq < 0 ? -dt->fs_freq - 1 : dt->fs_freq - 1, 
653                                            dt->fs_file,
654                                            dt->fs_passno);
655                         }
656                 }
657         }
658
659         /* print in /etc/dumpdates order if not in /etc/fstab */
660         if (ddatev != NULL) {
661                 struct dumpdates *dtwalk = NULL;
662                 char *lastname;
663                 int i;
664
665                 lastname = "??";
666                 ITITERATE(i, dtwalk) {
667                         if (strncmp(lastname, dtwalk->dd_name,
668                                 sizeof(dtwalk->dd_name)) == 0 ||
669                             dtwalk->dd_fstab != NULL)
670                                 continue;
671                         lastname = dtwalk->dd_name;
672                         print_wmsg(arg, 0, dtwalk->dd_name,
673                                    dtwalk->dd_level, NULL, dtwalk->dd_ddate);
674                 }
675         }
676 }
677
678 int
679 datesort(const void *a1, const void *a2)
680 {
681         struct dumpdates *d1 = *(struct dumpdates **)a1;
682         struct dumpdates *d2 = *(struct dumpdates **)a2;
683         int diff;
684
685         diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
686         if (diff == 0)
687                 return (d2->dd_ddate - d1->dd_ddate);
688         return (diff);
689 }