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