]>
Commit | Line | Data |
---|---|---|
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 |
42 | static const char rcsid[] = | |
f3f35300 | 43 | "$Id: optr.c,v 1.13 2000/11/10 11:54:52 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" | |
51b01afe | 70 | #include "bylabel.h" |
1227625a | 71 | |
ddd2ef55 | 72 | static void alarmcatch __P((int)); |
1227625a | 73 | int datesort __P((const void *, const void *)); |
ddd2ef55 | 74 | static void sendmes __P((const char *, const char *)); |
1227625a | 75 | |
51b01afe SP |
76 | /* List of filesystem types that we can dump (same ext2 on-disk format) */ |
77 | static char *fstypes[] = { "ext2", "ext3", "InterMezzo", NULL }; | |
78 | ||
1227625a SP |
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; | |
ddd2ef55 | 91 | static const char *attnmessage; /* attention message */ |
1227625a SP |
92 | |
93 | int | |
ddd2ef55 | 94 | query(const char *question) |
1227625a SP |
95 | { |
96 | char replybuffer[64]; | |
97 | int back, errcount; | |
98 | FILE *mytty; | |
ddd2ef55 SP |
99 | time_t firstprompt, when_answered; |
100 | ||
8d4197bb SP |
101 | #ifdef __linux__ |
102 | (void)time4(&(firstprompt)); | |
103 | #else | |
104 | (void)time((time_t *)&(firstprompt)); | |
105 | #endif | |
1227625a SP |
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; | |
ddd2ef55 | 111 | alarmcatch(0); |
1227625a SP |
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); | |
8d4197bb SP |
138 | #ifdef __linux__ |
139 | (void)time4(&(when_answered)); | |
140 | #else | |
141 | (void)time((time_t *)&(when_answered)); | |
142 | #endif | |
ddd2ef55 SP |
143 | /* |
144 | * Adjust the base for time estimates to ignore time we spent waiting | |
145 | * for operator input. | |
146 | */ | |
8d4197bb | 147 | if ((tstart_writing != 0) && (when_answered != (time_t)-1) && (firstprompt != (time_t)-1)) |
ddd2ef55 | 148 | tstart_writing += (when_answered - firstprompt); |
1227625a SP |
149 | return(back); |
150 | } | |
151 | ||
ddd2ef55 | 152 | char lastmsg[BUFSIZ]; |
1227625a SP |
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 | */ | |
ddd2ef55 SP |
158 | static void |
159 | alarmcatch(int signo) | |
1227625a | 160 | { |
ddd2ef55 | 161 | int save_errno = errno; |
1227625a SP |
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; | |
ddd2ef55 | 180 | errno = save_errno; |
1227625a SP |
181 | } |
182 | ||
183 | /* | |
184 | * Here if an inquisitive operator interrupts the dump program | |
185 | */ | |
186 | void | |
ddd2ef55 | 187 | interrupt(int signo) |
1227625a SP |
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. | |
b45f51d6 | 203 | */ |
1227625a | 204 | void |
ddd2ef55 | 205 | set_operators(void) |
1227625a SP |
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 | |
ddd2ef55 | 225 | broadcast(const char *message) |
1227625a SP |
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 | |
ddd2ef55 | 280 | sendmes(const char *tty, const char *message) |
1227625a | 281 | { |
b45f51d6 | 282 | char t[MAXPATHLEN], buf[BUFSIZ]; |
ddd2ef55 | 283 | register const char *cp; |
1227625a SP |
284 | int lmsg = 1; |
285 | FILE *f_tty; | |
286 | ||
287 | (void) strcpy(t, _PATH_DEV); | |
b45f51d6 | 288 | (void) strncat(t, tty, sizeof t - strlen(_PATH_DEV) - 1); |
1227625a SP |
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; | |
ddd2ef55 | 301 | if (!(cp && *cp != '\0')) |
1227625a SP |
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 | |
ddd2ef55 | 322 | timeest(void) |
1227625a SP |
323 | { |
324 | time_t tnow, deltat; | |
325 | ||
8d4197bb SP |
326 | #ifdef __linux__ |
327 | (void) time4(&tnow); | |
328 | #else | |
1227625a | 329 | (void) time((time_t *) &tnow); |
8d4197bb | 330 | #endif |
1227625a SP |
331 | if (tnow >= tschedule) { |
332 | tschedule = tnow + 300; | |
333 | if (blockswritten < 500) | |
b45f51d6 | 334 | return; |
8eb05297 SP |
335 | if (blockswritten > tapesize) |
336 | tapesize = blockswritten; | |
1227625a SP |
337 | deltat = tstart_writing - tnow + |
338 | (1.0 * (tnow - tstart_writing)) | |
339 | / blockswritten * tapesize; | |
340 | msg("%3.2f%% done, finished in %d:%02d\n", | |
341 | (blockswritten * 100.0) / tapesize, | |
342 | deltat / 3600, (deltat % 3600) / 60); | |
343 | } | |
344 | } | |
345 | ||
346 | void | |
ddd2ef55 | 347 | #ifdef __STDC__ |
1227625a SP |
348 | msg(const char *fmt, ...) |
349 | #else | |
350 | msg(fmt, va_alist) | |
351 | char *fmt; | |
352 | va_dcl | |
353 | #endif | |
354 | { | |
355 | va_list ap; | |
356 | ||
357 | (void) fprintf(stderr," DUMP: "); | |
358 | #ifdef TDEBUG | |
359 | (void) fprintf(stderr, "pid=%d ", getpid()); | |
360 | #endif | |
ddd2ef55 | 361 | #ifdef __STDC__ |
1227625a SP |
362 | va_start(ap, fmt); |
363 | #else | |
364 | va_start(ap); | |
365 | #endif | |
366 | (void) vfprintf(stderr, fmt, ap); | |
3072400e | 367 | va_end(ap); |
1227625a SP |
368 | (void) fflush(stdout); |
369 | (void) fflush(stderr); | |
3072400e SP |
370 | #ifdef __STDC__ |
371 | va_start(ap, fmt); | |
372 | #else | |
373 | va_start(ap); | |
374 | #endif | |
ddd2ef55 | 375 | (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap); |
1227625a SP |
376 | va_end(ap); |
377 | } | |
378 | ||
379 | void | |
ddd2ef55 | 380 | #ifdef __STDC__ |
1227625a SP |
381 | msgtail(const char *fmt, ...) |
382 | #else | |
383 | msgtail(fmt, va_alist) | |
384 | char *fmt; | |
385 | va_dcl | |
386 | #endif | |
387 | { | |
388 | va_list ap; | |
ddd2ef55 | 389 | #ifdef __STDC__ |
1227625a SP |
390 | va_start(ap, fmt); |
391 | #else | |
392 | va_start(ap); | |
393 | #endif | |
394 | (void) vfprintf(stderr, fmt, ap); | |
395 | va_end(ap); | |
396 | } | |
397 | ||
398 | void | |
ddd2ef55 | 399 | #ifdef __STDC__ |
1227625a SP |
400 | quit(const char *fmt, ...) |
401 | #else | |
402 | quit(fmt, va_alist) | |
403 | char *fmt; | |
404 | va_dcl | |
405 | #endif | |
406 | { | |
407 | va_list ap; | |
408 | ||
409 | (void) fprintf(stderr," DUMP: "); | |
410 | #ifdef TDEBUG | |
411 | (void) fprintf(stderr, "pid=%d ", getpid()); | |
412 | #endif | |
ddd2ef55 | 413 | #ifdef __STDC__ |
1227625a SP |
414 | va_start(ap, fmt); |
415 | #else | |
416 | va_start(ap); | |
417 | #endif | |
418 | (void) vfprintf(stderr, fmt, ap); | |
419 | va_end(ap); | |
420 | (void) fflush(stdout); | |
421 | (void) fflush(stderr); | |
422 | dumpabort(0); | |
423 | } | |
424 | ||
425 | /* | |
426 | * Tell the operator what has to be done; | |
427 | * we don't actually do it | |
428 | */ | |
429 | ||
ddd2ef55 SP |
430 | static struct fstab * |
431 | allocfsent(struct fstab *fs) | |
1227625a SP |
432 | { |
433 | register struct fstab *new; | |
434 | ||
435 | new = (struct fstab *)malloc(sizeof (*fs)); | |
a2c9bd28 SP |
436 | if (new == NULL) |
437 | quit("%s\n", strerror(errno)); | |
438 | if (strlen(fs->fs_file) > 1 && fs->fs_file[strlen(fs->fs_file) - 1] == '/') | |
439 | fs->fs_file[strlen(fs->fs_file) - 1] = '\0'; | |
440 | if ((new->fs_file = strdup(fs->fs_file)) == NULL || | |
1227625a | 441 | (new->fs_type = strdup(fs->fs_type)) == NULL || |
51b01afe | 442 | (new->fs_vfstype = strdup(fs->fs_vfstype)) == NULL || |
1227625a SP |
443 | (new->fs_spec = strdup(fs->fs_spec)) == NULL) |
444 | quit("%s\n", strerror(errno)); | |
445 | new->fs_passno = fs->fs_passno; | |
446 | new->fs_freq = fs->fs_freq; | |
447 | return (new); | |
448 | } | |
449 | ||
450 | struct pfstab { | |
451 | struct pfstab *pf_next; | |
51b01afe | 452 | struct dumpdates *pf_dd; |
1227625a SP |
453 | struct fstab *pf_fstab; |
454 | }; | |
455 | ||
456 | static struct pfstab *table; | |
457 | ||
458 | void | |
ddd2ef55 | 459 | getfstab(void) |
1227625a | 460 | { |
51b01afe SP |
461 | struct fstab *fs; |
462 | struct pfstab *pf; | |
463 | struct pfstab *pfold = NULL; | |
1227625a SP |
464 | |
465 | if (setfsent() == 0) { | |
466 | msg("Can't open %s for dump table information: %s\n", | |
467 | _PATH_FSTAB, strerror(errno)); | |
468 | return; | |
469 | } | |
470 | while ((fs = getfsent()) != NULL) { | |
471 | if (strcmp(fs->fs_type, FSTAB_RW) && | |
472 | strcmp(fs->fs_type, FSTAB_RO) && | |
473 | strcmp(fs->fs_type, FSTAB_RQ)) | |
474 | continue; | |
475 | fs = allocfsent(fs); | |
51b01afe | 476 | fs->fs_passno = 0; |
1227625a SP |
477 | if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) |
478 | quit("%s\n", strerror(errno)); | |
479 | pf->pf_fstab = fs; | |
51b01afe SP |
480 | pf->pf_next = NULL; |
481 | ||
482 | /* keep table in /etc/fstab order for use with -w and -W */ | |
483 | if (pfold) { | |
484 | pfold->pf_next = pf; | |
485 | pfold = pf; | |
486 | } else | |
487 | pfold = table = pf; | |
488 | ||
1227625a SP |
489 | } |
490 | (void) endfsent(); | |
491 | } | |
492 | ||
493 | /* | |
494 | * Search in the fstab for a file name. | |
495 | * This file name can be either the special or the path file name. | |
496 | * | |
497 | * The entries in the fstab are the BLOCK special names, not the | |
498 | * character special names. | |
499 | * The caller of fstabsearch assures that the character device | |
500 | * is dumped (that is much faster) | |
501 | * | |
502 | * The file name can omit the leading '/'. | |
503 | */ | |
504 | struct fstab * | |
ddd2ef55 | 505 | fstabsearch(const char *key) |
1227625a SP |
506 | { |
507 | register struct pfstab *pf; | |
508 | register struct fstab *fs; | |
509 | char *rn; | |
510 | ||
511 | for (pf = table; pf != NULL; pf = pf->pf_next) { | |
512 | fs = pf->pf_fstab; | |
513 | if (strcmp(fs->fs_file, key) == 0 || | |
514 | strcmp(fs->fs_spec, key) == 0) | |
515 | return (fs); | |
516 | rn = rawname(fs->fs_spec); | |
517 | if (rn != NULL && strcmp(rn, key) == 0) | |
518 | return (fs); | |
519 | if (key[0] != '/') { | |
520 | if (*fs->fs_spec == '/' && | |
521 | strcmp(fs->fs_spec + 1, key) == 0) | |
522 | return (fs); | |
523 | if (*fs->fs_file == '/' && | |
524 | strcmp(fs->fs_file + 1, key) == 0) | |
525 | return (fs); | |
526 | } | |
527 | } | |
528 | return (NULL); | |
529 | } | |
530 | ||
531 | #ifdef __linux__ | |
532 | struct fstab * | |
ddd2ef55 | 533 | fstabsearchdir(const char *key, char *directory) |
1227625a SP |
534 | { |
535 | register struct pfstab *pf; | |
536 | register struct fstab *fs; | |
537 | register struct fstab *found_fs = NULL; | |
538 | unsigned int size = 0; | |
8d4197bb SP |
539 | struct stat buf; |
540 | ||
541 | if (stat(key, &buf) == 0 && S_ISBLK(buf.st_mode)) | |
542 | return NULL; | |
1227625a SP |
543 | |
544 | for (pf = table; pf != NULL; pf = pf->pf_next) { | |
545 | fs = pf->pf_fstab; | |
546 | if (strlen(fs->fs_file) > size && | |
4f94452b | 547 | strlen(key) > strlen(fs->fs_file) + 1 && |
1227625a SP |
548 | strncmp(fs->fs_file, key, strlen(fs->fs_file)) == 0 && |
549 | (key[strlen(fs->fs_file)] == '/' || | |
550 | fs->fs_file[strlen(fs->fs_file) - 1] == '/')) { | |
551 | found_fs = fs; | |
552 | size = strlen(fs->fs_file); | |
553 | } | |
554 | } | |
555 | if (found_fs != NULL) { | |
556 | /* | |
557 | * Ok, we have found a fstab entry which matches the argument | |
558 | * We have to split the argument name into: | |
559 | * - a device name (from the fstab entry) | |
560 | * - a directory name on this device | |
561 | */ | |
562 | strcpy(directory, key + size); | |
563 | } | |
564 | return(found_fs); | |
565 | } | |
566 | #endif | |
567 | ||
51b01afe SP |
568 | static void |
569 | print_wmsg(char arg, int dumpme, const char *dev, int level, | |
570 | const char *mtpt, time_t ddate) | |
571 | { | |
572 | char *date; | |
573 | ||
574 | if (ddate) | |
575 | date = (char *)ctime(&ddate); | |
576 | //date[16] = '\0'; /* blast away seconds and year */ | |
577 | ||
578 | if (!dumpme && arg == 'w') | |
579 | return; | |
580 | ||
581 | (void) printf("%c %8s\t(%6s) Last dump: ", | |
582 | dumpme && (arg != 'w') ? '>' : ' ', | |
583 | dev, | |
584 | mtpt ? mtpt : ""); | |
585 | ||
586 | if (ddate) | |
f3f35300 | 587 | printf("Level %c, Date %s\n", |
51b01afe SP |
588 | level, date); |
589 | else | |
f3f35300 | 590 | printf("never\n"); |
51b01afe SP |
591 | } |
592 | ||
1227625a SP |
593 | /* |
594 | * Tell the operator what to do | |
595 | */ | |
596 | void | |
ddd2ef55 | 597 | lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */ |
1227625a | 598 | { |
51b01afe | 599 | struct pfstab *pf; |
1227625a SP |
600 | time_t tnow; |
601 | ||
602 | (void) time(&tnow); | |
603 | getfstab(); /* /etc/fstab input */ | |
8d4197bb | 604 | initdumptimes(0); /* dumpdates input */ |
51b01afe SP |
605 | if (ddatev == NULL && table == NULL) { |
606 | (void) printf("No %s or %s file found\n", | |
607 | _PATH_FSTAB, _PATH_DUMPDATES); | |
608 | return; | |
609 | } | |
610 | ||
611 | if (arg == 'w') | |
612 | (void) printf("Dump these file systems:\n"); | |
613 | else | |
614 | (void) printf("Last dump(s) done (Dump '>' file systems):\n"); | |
615 | ||
8d4197bb | 616 | if (ddatev != NULL) { |
51b01afe SP |
617 | struct dumpdates *dtwalk = NULL; |
618 | int i; | |
619 | char *lastname; | |
620 | ||
8d4197bb SP |
621 | qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); |
622 | ||
51b01afe SP |
623 | lastname = "??"; |
624 | ITITERATE(i, dtwalk) { | |
625 | struct fstab *dt; | |
626 | if (strncmp(lastname, dtwalk->dd_name, | |
627 | sizeof(dtwalk->dd_name)) == 0) | |
628 | continue; | |
629 | lastname = dtwalk->dd_name; | |
630 | if ((dt = dtwalk->dd_fstab) != NULL && | |
631 | dt->fs_freq != 0) { | |
632 | /* Overload fs_freq as dump level and | |
633 | * fs_passno as date, because we can't | |
634 | * change struct fstab format. | |
635 | * A negative fs_freq means this | |
636 | * filesystem needs to be dumped. | |
637 | */ | |
638 | dt->fs_passno = dtwalk->dd_ddate; | |
639 | if (dtwalk->dd_ddate < | |
640 | tnow - (dt->fs_freq * 86400)) | |
641 | dt->fs_freq = -dtwalk->dd_level; | |
642 | else | |
643 | dt->fs_freq = dtwalk->dd_level; | |
644 | ||
645 | } | |
646 | } | |
647 | } | |
648 | ||
649 | /* print in /etc/fstab order only those filesystem types we can dump */ | |
650 | for (pf = table; pf != NULL; pf = pf->pf_next) { | |
651 | struct fstab *dt = pf->pf_fstab; | |
652 | char **type; | |
653 | ||
654 | for (type = fstypes; *type != NULL; type++) { | |
655 | if (strncmp(dt->fs_vfstype, *type, | |
656 | sizeof(dt->fs_vfstype)) == 0) { | |
657 | const char *disk = get_device_name(dt->fs_spec); | |
658 | print_wmsg(arg, dt->fs_freq < 0 || !dt->fs_passno, | |
659 | disk ? disk : dt->fs_spec, | |
660 | dt->fs_freq < 0 ? -dt->fs_freq : dt->fs_freq, | |
661 | dt->fs_file, | |
662 | dt->fs_passno); | |
663 | } | |
664 | } | |
665 | } | |
666 | ||
667 | /* print in /etc/dumpdates order if not in /etc/fstab */ | |
668 | if (ddatev != NULL) { | |
669 | struct dumpdates *dtwalk = NULL; | |
670 | char *lastname; | |
671 | int i; | |
672 | ||
8d4197bb SP |
673 | lastname = "??"; |
674 | ITITERATE(i, dtwalk) { | |
675 | if (strncmp(lastname, dtwalk->dd_name, | |
51b01afe SP |
676 | sizeof(dtwalk->dd_name)) == 0 || |
677 | dtwalk->dd_fstab != NULL) | |
8d4197bb | 678 | continue; |
8d4197bb | 679 | lastname = dtwalk->dd_name; |
51b01afe SP |
680 | print_wmsg(arg, 0, dtwalk->dd_name, |
681 | dtwalk->dd_level, NULL, dtwalk->dd_ddate); | |
8d4197bb | 682 | } |
1227625a SP |
683 | } |
684 | } | |
685 | ||
686 | int | |
ddd2ef55 | 687 | datesort(const void *a1, const void *a2) |
1227625a SP |
688 | { |
689 | struct dumpdates *d1 = *(struct dumpdates **)a1; | |
690 | struct dumpdates *d2 = *(struct dumpdates **)a2; | |
691 | int diff; | |
692 | ||
693 | diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); | |
694 | if (diff == 0) | |
695 | return (d2->dd_ddate - d1->dd_ddate); | |
696 | return (diff); | |
697 | } |