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