]> git.wh0rd.org Git - dump.git/blob - dump/optr.c
ab38a3d9217e7046597fd3385cf7452d25b1b1e5
[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@cybercable.fr>, 1999 
6  *
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 #if 0
44 static char sccsid[] = "@(#)optr.c      8.2 (Berkeley) 1/6/94";
45 #endif
46 static const char rcsid[] =
47         "$Id: optr.c,v 1.4 1999/10/11 13:08:08 stelian Exp $";
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/wait.h>
52 #include <sys/time.h>
53
54 #include <errno.h>
55 #include <fstab.h>
56 #include <grp.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <stdarg.h>
61 #include <unistd.h>
62 #include <utmp.h>
63 #include <sys/stat.h>
64
65 #ifdef __linux__
66 #include <linux/ext2_fs.h>
67 #include <ext2fs/ext2fs.h>
68 #include <bsdcompat.h>
69 #include <signal.h>
70 #endif
71
72 #include "dump.h"
73 #include "pathnames.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 /*
80  *      Query the operator; This previously-fascist piece of code
81  *      no longer requires an exact response.
82  *      It is intended to protect dump aborting by inquisitive
83  *      people banging on the console terminal to see what is
84  *      happening which might cause dump to croak, destroying
85  *      a large number of hours of work.
86  *
87  *      Every 2 minutes we reprint the message, alerting others
88  *      that dump needs attention.
89  */
90 static  int timeout;
91 static  const char *attnmessage;                /* attention message */
92
93 int
94 query(const char *question)
95 {
96         char    replybuffer[64];
97         int     back, errcount;
98         FILE    *mytty;
99         time_t  firstprompt, when_answered;
100
101 #ifdef __linux__
102         (void)time4(&(firstprompt));
103 #else
104         (void)time((time_t *)&(firstprompt));
105 #endif
106
107         if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
108                 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
109         attnmessage = question;
110         timeout = 0;
111         alarmcatch(0);
112         back = -1;
113         errcount = 0;
114         do {
115                 if (fgets(replybuffer, 63, mytty) == NULL) {
116                         clearerr(mytty);
117                         if (++errcount > 30)    /* XXX  ugly */
118                                 quit("excessive operator query failures\n");
119                 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
120                         back = 1;
121                 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
122                         back = 0;
123                 } else {
124                         (void) fprintf(stderr,
125                             "  DUMP: \"Yes\" or \"No\"?\n");
126                         (void) fprintf(stderr,
127                             "  DUMP: %s: (\"yes\" or \"no\") ", question);
128                 }
129         } while (back < 0);
130
131         /*
132          *      Turn off the alarm, and reset the signal to trap out..
133          */
134         (void) alarm(0);
135         if (signal(SIGALRM, sig) == SIG_IGN)
136                 signal(SIGALRM, SIG_IGN);
137         (void) fclose(mytty);
138 #ifdef __linux__
139         (void)time4(&(when_answered));
140 #else
141         (void)time((time_t *)&(when_answered));
142 #endif
143         /*
144          * Adjust the base for time estimates to ignore time we spent waiting
145          * for operator input.
146          */
147         if ((tstart_writing != 0) && (when_answered != (time_t)-1) && (firstprompt != (time_t)-1))
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((time_t *)0);
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, deltat;
325
326 #ifdef __linux__
327         (void) time4(&tnow);
328 #else
329         (void) time((time_t *) &tnow);
330 #endif
331         if (tnow >= tschedule) {
332                 tschedule = tnow + 300;
333                 if (blockswritten < 500)
334                         return;
335                 deltat = tstart_writing - tnow +
336                         (1.0 * (tnow - tstart_writing))
337                         / blockswritten * tapesize;
338                 msg("%3.2f%% done, finished in %d:%02d\n",
339                         (blockswritten * 100.0) / tapesize,
340                         deltat / 3600, (deltat % 3600) / 60);
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         (void) fflush(stdout);
366         (void) fflush(stderr);
367         (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
368         va_end(ap);
369 }
370
371 void
372 #ifdef __STDC__
373 msgtail(const char *fmt, ...)
374 #else
375 msgtail(fmt, va_alist)
376         char *fmt;
377         va_dcl
378 #endif
379 {
380         va_list ap;
381 #ifdef __STDC__
382         va_start(ap, fmt);
383 #else
384         va_start(ap);
385 #endif
386         (void) vfprintf(stderr, fmt, ap);
387         va_end(ap);
388 }
389
390 void
391 #ifdef __STDC__
392 quit(const char *fmt, ...)
393 #else
394 quit(fmt, va_alist)
395         char *fmt;
396         va_dcl
397 #endif
398 {
399         va_list ap;
400
401         (void) fprintf(stderr,"  DUMP: ");
402 #ifdef TDEBUG
403         (void) fprintf(stderr, "pid=%d ", getpid());
404 #endif
405 #ifdef __STDC__
406         va_start(ap, fmt);
407 #else
408         va_start(ap);
409 #endif
410         (void) vfprintf(stderr, fmt, ap);
411         va_end(ap);
412         (void) fflush(stdout);
413         (void) fflush(stderr);
414         dumpabort(0);
415 }
416
417 /*
418  *      Tell the operator what has to be done;
419  *      we don't actually do it
420  */
421
422 static struct fstab *
423 allocfsent(struct fstab *fs)
424 {
425         register struct fstab *new;
426
427         new = (struct fstab *)malloc(sizeof (*fs));
428         if (new == NULL ||
429             (new->fs_file = strdup(fs->fs_file)) == NULL ||
430             (new->fs_type = strdup(fs->fs_type)) == NULL ||
431             (new->fs_spec = strdup(fs->fs_spec)) == NULL)
432                 quit("%s\n", strerror(errno));
433         new->fs_passno = fs->fs_passno;
434         new->fs_freq = fs->fs_freq;
435         return (new);
436 }
437
438 struct  pfstab {
439         struct  pfstab *pf_next;
440         struct  fstab *pf_fstab;
441 };
442
443 static  struct pfstab *table;
444
445 void
446 getfstab(void)
447 {
448         register struct fstab *fs;
449         register struct pfstab *pf;
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                 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
463                         quit("%s\n", strerror(errno));
464                 pf->pf_fstab = fs;
465                 pf->pf_next = table;
466                 table = pf;
467         }
468         (void) endfsent();
469 }
470
471 /*
472  * Search in the fstab for a file name.
473  * This file name can be either the special or the path file name.
474  *
475  * The entries in the fstab are the BLOCK special names, not the
476  * character special names.
477  * The caller of fstabsearch assures that the character device
478  * is dumped (that is much faster)
479  *
480  * The file name can omit the leading '/'.
481  */
482 struct fstab *
483 fstabsearch(const char *key)
484 {
485         register struct pfstab *pf;
486         register struct fstab *fs;
487         char *rn;
488
489         for (pf = table; pf != NULL; pf = pf->pf_next) {
490                 fs = pf->pf_fstab;
491                 if (strcmp(fs->fs_file, key) == 0 ||
492                     strcmp(fs->fs_spec, key) == 0)
493                         return (fs);
494                 rn = rawname(fs->fs_spec);
495                 if (rn != NULL && strcmp(rn, key) == 0)
496                         return (fs);
497                 if (key[0] != '/') {
498                         if (*fs->fs_spec == '/' &&
499                             strcmp(fs->fs_spec + 1, key) == 0)
500                                 return (fs);
501                         if (*fs->fs_file == '/' &&
502                             strcmp(fs->fs_file + 1, key) == 0)
503                                 return (fs);
504                 }
505         }
506         return (NULL);
507 }
508
509 #ifdef  __linux__
510 struct fstab *
511 fstabsearchdir(const char *key, char *directory)
512 {
513         register struct pfstab *pf;
514         register struct fstab *fs;
515         register struct fstab *found_fs = NULL;
516         unsigned int size = 0;
517         struct stat buf;
518
519         if (stat(key, &buf) == 0 && S_ISBLK(buf.st_mode))
520                 return NULL;
521
522         for (pf = table; pf != NULL; pf = pf->pf_next) {
523                 fs = pf->pf_fstab;
524                 if (strlen(fs->fs_file) > size &&
525                     strlen(key) > strlen(fs->fs_file) + 2 &&
526                     strncmp(fs->fs_file, key, strlen(fs->fs_file)) == 0 &&
527                     (key[strlen(fs->fs_file)] == '/' ||
528                      fs->fs_file[strlen(fs->fs_file) - 1] == '/')) {
529                         found_fs = fs;
530                         size = strlen(fs->fs_file);
531                 }
532         }
533         if (found_fs != NULL) {
534                 /*
535                  * Ok, we have found a fstab entry which matches the argument
536                  * We have to split the argument name into:
537                  * - a device name (from the fstab entry)
538                  * - a directory name on this device
539                  */
540                 strcpy(directory, key + size);
541         }
542         return(found_fs);
543 }
544 #endif
545
546 /*
547  *      Tell the operator what to do
548  */
549 void
550 lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */
551 {
552         register int i;
553         register struct fstab *dt;
554         register struct dumpdates *dtwalk=NULL;
555         char *lastname, *date;
556         int dumpme;
557         time_t tnow;
558
559         (void) time(&tnow);
560         getfstab();             /* /etc/fstab input */
561         initdumptimes(0);       /* dumpdates input */
562         if (ddatev != NULL) {
563                 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
564
565                 if (arg == 'w')
566                         (void) printf("Dump these file systems:\n");
567                 else
568                         (void) printf("Last dump(s) done (Dump '>' file systems):\n");
569                 lastname = "??";
570                 ITITERATE(i, dtwalk) {
571                         if (strncmp(lastname, dtwalk->dd_name,
572                                 sizeof(dtwalk->dd_name)) == 0)
573                                 continue;
574                         date = (char *)ctime(&dtwalk->dd_ddate);
575                         date[16] = '\0';        /* blast away seconds and year */
576                         lastname = dtwalk->dd_name;
577                         dt = fstabsearch(dtwalk->dd_name);
578                         dumpme = (dt != NULL &&
579                                 dt->fs_freq != 0 &&
580                                 dtwalk->dd_ddate < tnow - (dt->fs_freq * 86400));
581                         if (arg != 'w' || dumpme)
582                                 (void) printf(
583                                         "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
584                                         dumpme && (arg != 'w') ? '>' : ' ',
585                                         dtwalk->dd_name,
586                                         dt ? dt->fs_file : "",
587                                         dtwalk->dd_level,
588                                         date);
589                 }
590         }
591 }
592
593 int
594 datesort(const void *a1, const void *a2)
595 {
596         struct dumpdates *d1 = *(struct dumpdates **)a1;
597         struct dumpdates *d2 = *(struct dumpdates **)a2;
598         int diff;
599
600         diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
601         if (diff == 0)
602                 return (d2->dd_ddate - d1->dd_ddate);
603         return (diff);
604 }