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