can't reset it if not set up
[unix-history] / usr / src / old / dump.4.1 / dumpoptr.c
CommitLineData
76797561
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8static char sccsid[] = "@(#)dumpoptr.c 5.1 (Berkeley) %G%";
9#endif not lint
10
f3869107
BJ
11#include "dump.h"
12
13/*
14 * This is from /usr/include/grp.h
15 * That defined struct group, which conflicts
16 * with the struct group defined in param.h
17 */
18struct Group { /* see getgrent(3) */
19 char *gr_name;
20 char *gr_passwd;
21 int gr_gid;
22 char **gr_mem;
23};
24struct Group *getgrnam();
25/*
26 * Query the operator; This fascist piece of code requires
27 * an exact response.
28 * It is intended to protect dump aborting by inquisitive
29 * people banging on the console terminal to see what is
30 * happening which might cause dump to croak, destroying
31 * a large number of hours of work.
32 *
33 * Every 2 minutes we reprint the message, alerting others
34 * that dump needs attention.
35 */
36int timeout;
37char *attnmessage; /* attemtion message */
38query(question)
39 char *question;
40{
41 char replybuffer[64];
42 int back;
43 FILE *mytty;
44
45 if ( (mytty = fopen("/dev/tty", "r")) == NULL){
46 msg("fopen on /dev/tty fails\n");
47 abort();
48 }
49 attnmessage = question;
50 timeout = 0;
51 alarmcatch();
52 for(;;){
53 if ( fgets(replybuffer, 63, mytty) == NULL){
54 if (ferror(mytty)){
55 clearerr(mytty);
56 continue;
57 }
58 } else if ( (strcmp(replybuffer, "yes\n") == 0) ||
59 (strcmp(replybuffer, "Yes\n") == 0)){
60 back = 1;
61 goto done;
62 } else if ( (strcmp(replybuffer, "no\n") == 0) ||
63 (strcmp(replybuffer, "No\n") == 0)){
64 back = 0;
65 goto done;
66 } else {
67 msg("\"Yes\" or \"No\" ONLY!\n");
68 alarmcatch();
69 }
70 }
71 done:
72 /*
73 * Turn off the alarm, and reset the signal to trap out..
74 */
75 alarm(0);
76 if (signal(SIGALRM, sigalrm) == SIG_IGN)
77 signal(SIGALRM, SIG_IGN);
78 fclose(mytty);
79 return(back);
80}
81/*
82 * Alert the console operator, and enable the alarm clock to
83 * sleep for 2 minutes in case nobody comes to satisfy dump
84 */
85alarmcatch()
86{
87 if (timeout)
88 msgtail("\n");
89 msg("NEEDS ATTENTION: %s: (\"yes\" or \"no\") ",
90 attnmessage);
91 signal(SIGALRM, alarmcatch);
92 alarm(120);
93 timeout = 1;
94}
95/*
96 * Here if an inquisitive operator interrupts the dump program
97 */
98interrupt()
99{
100 msg("Interrupt received. Do >>>YOU<<< know what are you doing?\n");
101 if (query("Do you really want to abort dump?"))
102 dumpabort();
103 signal(SIGINT, interrupt);
104}
105
106/*
107 * The following variables and routines manage alerting
108 * operators to the status of dump.
109 * This works much like wall(1) does.
110 */
111struct Group *gp;
112
113/*
114 * Get the names from the group entry "operator" to notify.
115 */
116set_operators()
117{
118 if (!notify) /*not going to notify*/
119 return;
120 gp = getgrnam(OPGRENT);
121 endgrent();
122 if (gp == (struct Group *)0){
123 msg("No entry in /etc/group for %s.\n",
124 OPGRENT);
125 notify = 0;
126 return;
127 }
128}
129
130struct tm *localtime();
131struct tm *localclock;
132
133/*
134 * We fork a child to do the actual broadcasting, so
135 * that the process control groups are not messed up
136 */
137broadcast(message)
138 char *message;
139{
140 time_t clock;
141 FILE *f_utmp;
142 struct utmp utmp;
143 int nusers;
144 char **np;
145 int pid, s;
146
147 switch (pid = fork()) {
148 case -1:
149 return;
150 case 0:
151 break;
152 default:
153 while (wait(&s) != pid)
154 continue;
155 return;
156 }
157
158 if (!notify || gp == 0)
ed7c701e 159 exit(0);
f3869107
BJ
160 clock = time(0);
161 localclock = localtime(&clock);
162
163 if((f_utmp = fopen("/etc/utmp", "r")) == NULL) {
164 msg("Cannot open /etc/utmp\n");
165 return;
166 }
167
168 nusers = 0;
169 while (!feof(f_utmp)){
170 if (fread(&utmp, sizeof (struct utmp), 1, f_utmp) != 1)
171 break;
172 if (utmp.ut_name[0] == 0)
173 continue;
174 nusers++;
175 for (np = gp->gr_mem; *np; np++){
176 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0)
177 continue;
178 /*
179 * Do not send messages to operators on dialups
180 */
181 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0)
182 continue;
183#ifdef DEBUG
184 msg("Message to %s at %s\n",
185 utmp.ut_name, utmp.ut_line);
186#endif DEBUG
187 sendmes(utmp.ut_line, message);
188 }
189 }
190 fclose(f_utmp);
191 Exit(0); /* the wait in this same routine will catch this */
192 /* NOTREACHED */
193}
194
195sendmes(tty, message)
196char *tty, *message;
197{
198 char t[50], buf[BUFSIZ];
199 register char *cp;
200 register int c, ch;
201 int msize;
202 FILE *f_tty;
203
204 msize = strlen(message);
205 strcpy(t, "/dev/");
206 strcat(t, tty);
207
208 if((f_tty = fopen(t, "w")) != NULL) {
209 setbuf(f_tty, buf);
210 fprintf(f_tty, "\n\a\a\aMessage from the dump program to all operators at %d:%02d ...\r\n\n"
211 ,localclock->tm_hour
212 ,localclock->tm_min);
213 for (cp = message, c = msize; c-- > 0; cp++) {
214 ch = *cp;
215 if (ch == '\n')
216 putc('\r', f_tty);
217 putc(ch, f_tty);
218 }
219 fclose(f_tty);
220 }
221}
222
223/*
224 * print out an estimate of the amount of time left to do the dump
225 */
226
227time_t tschedule = 0;
228
229timeest()
230{
231 time_t tnow, deltat;
232
233 time (&tnow);
234 if (tnow >= tschedule){
235 tschedule = tnow + 300;
236 if (blockswritten < 500)
237 return;
238 deltat = tstart_writing - tnow +
239 (((1.0*(tnow - tstart_writing))/blockswritten) * esize);
240 msg("%3.2f%% done, finished in %d:%02d\n",
241 (blockswritten*100.0)/esize,
242 deltat/3600, (deltat%3600)/60);
243 }
244}
245
246int blocksontape()
247{
248 /*
249 * esize: total number of blocks estimated over all reels
250 * blockswritten: blocks actually written, over all reels
251 * etapes: estimated number of tapes to write
252 *
253 * tsize: blocks can write on this reel
254 * asize: blocks written on this reel
255 * tapeno: number of tapes written so far
256 */
257 if (tapeno == etapes)
258 return(esize - (etapes - 1)*tsize);
259 return(tsize);
260}
261
262 /* VARARGS1 */
263 /* ARGSUSED */
264msg(fmt, a1, a2, a3, a4, a5)
265 char *fmt;
266{
267 fprintf(stderr," DUMP: ");
268#ifdef TDEBUG
269 fprintf(stderr,"pid=%d ", getpid());
270#endif
271 fprintf(stderr, fmt, a1, a2, a3, a4, a5);
272 fflush(stdout);
273 fflush(stderr);
274}
275
276 /* VARARGS1 */
277 /* ARGSUSED */
278msgtail(fmt, a1, a2, a3, a4, a5)
279 char *fmt;
280{
281 fprintf(stderr, fmt, a1, a2, a3, a4, a5);
282}
283/*
284 * Tell the operator what has to be done;
285 * we don't actually do it
286 */
287
b33b7ff3
SL
288struct fstab *
289allocfsent(fs)
290 register struct fstab *fs;
291{
292 register struct fstab *new;
293 register char *cp;
294 char *malloc();
295
296 new = (struct fstab *)malloc(sizeof (*fs));
297 cp = malloc(strlen(fs->fs_file) + 1);
298 strcpy(cp, fs->fs_file);
299 new->fs_file = cp;
300 cp = malloc(strlen(fs->fs_type) + 1);
301 strcpy(cp, fs->fs_type);
302 new->fs_type = cp;
303 cp = malloc(strlen(fs->fs_spec) + 1);
304 strcpy(cp, fs->fs_spec);
305 new->fs_spec = cp;
306 new->fs_passno = fs->fs_passno;
307 new->fs_freq = fs->fs_freq;
308 return (new);
309}
310
311struct pfstab {
312 struct pfstab *pf_next;
313 struct fstab *pf_fstab;
314};
315
316static struct pfstab *table = NULL;
317
f3869107
BJ
318getfstab()
319{
b33b7ff3
SL
320 register struct fstab *fs;
321 register struct pfstab *pf;
f3869107 322
9ba0a26f 323 if (setfsent() == 0) {
f3869107 324 msg("Can't open %s for dump table information.\n", FSTAB);
b33b7ff3
SL
325 return;
326 }
327 while (fs = getfsent()) {
328 if (strcmp(fs->fs_type, FSTAB_RW) &&
329 strcmp(fs->fs_type, FSTAB_RO) &&
330 strcmp(fs->fs_type, FSTAB_RQ))
331 continue;
332 fs = allocfsent(fs);
333 pf = (struct pfstab *)malloc(sizeof (*pf));
334 pf->pf_fstab = fs;
335 pf->pf_next = table;
336 table = pf;
f3869107 337 }
b33b7ff3 338 endfsent();
f3869107
BJ
339}
340
341/*
b33b7ff3
SL
342 * Search in the fstab for a file name.
343 * This file name can be either the special or the path file name.
f3869107 344 *
b33b7ff3
SL
345 * The entries in the fstab are the BLOCK special names, not the
346 * character special names.
347 * The caller of fstabsearch assures that the character device
348 * is dumped (that is much faster)
f3869107 349 *
b33b7ff3 350 * The file name can omit the leading '/'.
f3869107 351 */
b33b7ff3
SL
352struct fstab *
353fstabsearch(key)
354 char *key;
f3869107 355{
b33b7ff3
SL
356 register struct pfstab *pf;
357 register struct fstab *fs;
b33b7ff3 358 char *rawname();
f3869107 359
b33b7ff3
SL
360 if (table == NULL)
361 return ((struct fstab *)0);
b33b7ff3
SL
362 for (pf = table; pf; pf = pf->pf_next) {
363 fs = pf->pf_fstab;
831e2afa 364 if (strcmp(fs->fs_file, key) == 0)
b33b7ff3 365 return (fs);
831e2afa 366 if (strcmp(fs->fs_spec, key) == 0)
b33b7ff3 367 return (fs);
831e2afa 368 if (strcmp(rawname(fs->fs_spec), key) == 0)
b33b7ff3 369 return (fs);
f3869107 370 if (key[0] != '/'){
b33b7ff3 371 if (*fs->fs_spec == '/' &&
831e2afa 372 strcmp(fs->fs_spec + 1, key) == 0)
b33b7ff3
SL
373 return (fs);
374 if (*fs->fs_file == '/' &&
831e2afa 375 strcmp(fs->fs_file + 1, key) == 0)
b33b7ff3 376 return (fs);
f3869107
BJ
377 }
378 }
b33b7ff3 379 return (0);
f3869107
BJ
380}
381
382/*
383 * Tell the operator what to do
384 */
77db894d
BJ
385lastdump(arg)
386 char arg; /* w ==> just what to do; W ==> most recent dumps */
f3869107
BJ
387{
388 char *lastname;
389 char *date;
390 register int i;
391 time_t tnow;
392 register struct fstab *dt;
393 int dumpme;
394 register struct idates *itwalk;
395
396 int idatesort();
397
398 time(&tnow);
399 getfstab(); /* /etc/fstab input */
400 inititimes(); /* /etc/dumpdates input */
401 qsort(idatev, nidates, sizeof(struct idates *), idatesort);
402
77db894d
BJ
403 if (arg == 'w')
404 fprintf(stdout, "Dump these file systems:\n");
405 else
406 fprintf(stdout, "Last dump(s) done (Dump '>' file systems):\n");
f3869107
BJ
407 lastname = "??";
408 ITITERATE(i, itwalk){
409 if (strncmp(lastname, itwalk->id_name, sizeof(itwalk->id_name)) == 0)
410 continue;
411 date = (char *)ctime(&itwalk->id_ddate);
412 date[16] = '\0'; /* blast away seconds and year */
413 lastname = itwalk->id_name;
414 dt = fstabsearch(itwalk->id_name);
415 dumpme = ( (dt != 0)
416 && (dt->fs_freq != 0)
417 && (itwalk->id_ddate < tnow - (dt->fs_freq*DAY)));
77db894d
BJ
418 if ( (arg != 'w') || dumpme)
419 fprintf(stdout,"%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
420 dumpme && (arg != 'w') ? '>' : ' ',
f3869107
BJ
421 itwalk->id_name,
422 dt ? dt->fs_file : 0,
423 itwalk->id_incno,
424 date
425 );
426 }
427}
428
429int idatesort(p1, p2)
430 struct idates **p1, **p2;
431{
432 int diff;
433
434 diff = strncmp((*p1)->id_name, (*p2)->id_name, sizeof((*p1)->id_name));
435 if (diff == 0)
436 return ((*p2)->id_ddate - (*p1)->id_ddate);
437 else
438 return (diff);
439}
440
441int max(a,b)
442{
443 return(a>b?a:b);
444}
445int min(a,b)
446{
447 return(a<b?a:b);
448}