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