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