Oops, fix build before anyone realises. :-)
[dragonfly.git] / sbin / dump / optr.c
1 /*-
2  * Copyright (c) 1980, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)optr.c   8.2 (Berkeley) 1/6/94
34  * $FreeBSD: src/sbin/dump/optr.c,v 1.9.2.5 2002/02/23 22:32:51 iedowse Exp $
35  * $DragonFly: src/sbin/dump/optr.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/wait.h>
41 #include <sys/time.h>
42
43 #include <errno.h>
44 #include <fstab.h>
45 #include <grp.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <stdarg.h>
50 #include <signal.h>
51 #include <unistd.h>
52 #include <utmp.h>
53
54 #include "dump.h"
55 #include "pathnames.h"
56
57 void    alarmcatch __P((/* int, int */));
58 int     datesort __P((const void *, const void *));
59
60 /*
61  *      Query the operator; This previously-fascist piece of code
62  *      no longer requires an exact response.
63  *      It is intended to protect dump aborting by inquisitive
64  *      people banging on the console terminal to see what is
65  *      happening which might cause dump to croak, destroying
66  *      a large number of hours of work.
67  *
68  *      Every 2 minutes we reprint the message, alerting others
69  *      that dump needs attention.
70  */
71 static  int timeout;
72 static  char *attnmessage;              /* attention message */
73
74 int
75 query(question)
76         char    *question;
77 {
78         char    replybuffer[64];
79         int     back, errcount;
80         FILE    *mytty;
81
82         if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
83                 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
84         attnmessage = question;
85         timeout = 0;
86         alarmcatch();
87         back = -1;
88         errcount = 0;
89         do {
90                 if (fgets(replybuffer, 63, mytty) == NULL) {
91                         clearerr(mytty);
92                         if (++errcount > 30)    /* XXX  ugly */
93                                 quit("excessive operator query failures\n");
94                 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
95                         back = 1;
96                 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
97                         back = 0;
98                 } else {
99                         (void) fprintf(stderr,
100                             "  DUMP: \"Yes\" or \"No\"?\n");
101                         (void) fprintf(stderr,
102                             "  DUMP: %s: (\"yes\" or \"no\") ", question);
103                 }
104         } while (back < 0);
105
106         /*
107          *      Turn off the alarm, and reset the signal to trap out..
108          */
109         (void) alarm(0);
110         if (signal(SIGALRM, sig) == SIG_IGN)
111                 signal(SIGALRM, SIG_IGN);
112         (void) fclose(mytty);
113         return(back);
114 }
115
116 char lastmsg[BUFSIZ];
117
118 /*
119  *      Alert the console operator, and enable the alarm clock to
120  *      sleep for 2 minutes in case nobody comes to satisfy dump
121  */
122 void
123 alarmcatch()
124 {
125         if (notify == 0) {
126                 if (timeout == 0)
127                         (void) fprintf(stderr,
128                             "  DUMP: %s: (\"yes\" or \"no\") ",
129                             attnmessage);
130                 else
131                         msgtail("\a\a");
132         } else {
133                 if (timeout) {
134                         msgtail("\n");
135                         broadcast("");          /* just print last msg */
136                 }
137                 (void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
138                     attnmessage);
139         }
140         signal(SIGALRM, alarmcatch);
141         (void) alarm(120);
142         timeout = 1;
143 }
144
145 /*
146  *      Here if an inquisitive operator interrupts the dump program
147  */
148 void
149 interrupt(signo)
150         int signo;
151 {
152         msg("Interrupt received.\n");
153         if (query("Do you want to abort dump?"))
154                 dumpabort(0);
155 }
156
157 /*
158  *      We now use wall(1) to do the actual broadcasting.
159  */
160 void
161 broadcast(message)
162         char    *message;
163 {
164         FILE *fp;
165         char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
166
167         if (!notify)
168                 return;
169
170         snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
171         if ((fp = popen(buf, "w")) == NULL)
172                 return;
173
174         (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
175         if (lastmsg[0])
176                 (void) fputs(lastmsg, fp);
177         if (message[0])
178                 (void) fputs(message, fp);
179
180         (void) pclose(fp);
181 }
182
183 /*
184  *      Print out an estimate of the amount of time left to do the dump
185  */
186
187 time_t  tschedule = 0;
188
189 void
190 timeest()
191 {
192         double percent;
193         time_t  tnow;
194         int deltat, hours, mins;
195
196         (void) time(&tnow);
197         deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
198             (double)(tnow - tstart_writing) / blockswritten * tapesize;
199         percent = (blockswritten * 100.0) / tapesize;
200         hours = deltat / 3600;
201         mins = (deltat % 3600) / 60;
202
203         setproctitle("%s: pass %d: %3.2f%% done, finished in %d:%02d",
204             disk, passno, percent, hours, mins);
205         if (tnow >= tschedule) {
206                 tschedule = tnow + 300;
207                 if (blockswritten < 500)
208                         return;
209                 msg("%3.2f%% done, finished in %d:%02d\n", percent, hours,
210                     mins);
211         }
212 }
213
214 /*
215  * Schedule a printout of the estimate in the next call to timeest().
216  */
217 void
218 infosch(signal)
219         int signal;
220 {
221         tschedule = 0;
222 }
223
224 void
225 #if __STDC__
226 msg(const char *fmt, ...)
227 #else
228 msg(fmt, va_alist)
229         char *fmt;
230         va_dcl
231 #endif
232 {
233         va_list ap;
234
235         (void) fprintf(stderr,"  DUMP: ");
236 #ifdef TDEBUG
237         (void) fprintf(stderr, "pid=%d ", getpid());
238 #endif
239 #if __STDC__
240         va_start(ap, fmt);
241 #else
242         va_start(ap);
243 #endif
244         (void) vfprintf(stderr, fmt, ap);
245         (void) fflush(stdout);
246         (void) fflush(stderr);
247         (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
248         va_end(ap);
249 }
250
251 void
252 #if __STDC__
253 msgtail(const char *fmt, ...)
254 #else
255 msgtail(fmt, va_alist)
256         char *fmt;
257         va_dcl
258 #endif
259 {
260         va_list ap;
261 #if __STDC__
262         va_start(ap, fmt);
263 #else
264         va_start(ap);
265 #endif
266         (void) vfprintf(stderr, fmt, ap);
267         va_end(ap);
268 }
269
270 void
271 #if __STDC__
272 quit(const char *fmt, ...)
273 #else
274 quit(fmt, va_alist)
275         char *fmt;
276         va_dcl
277 #endif
278 {
279         va_list ap;
280
281         (void) fprintf(stderr,"  DUMP: ");
282 #ifdef TDEBUG
283         (void) fprintf(stderr, "pid=%d ", getpid());
284 #endif
285 #if __STDC__
286         va_start(ap, fmt);
287 #else
288         va_start(ap);
289 #endif
290         (void) vfprintf(stderr, fmt, ap);
291         va_end(ap);
292         (void) fflush(stdout);
293         (void) fflush(stderr);
294         dumpabort(0);
295 }
296
297 /*
298  *      Tell the operator what has to be done;
299  *      we don't actually do it
300  */
301
302 struct fstab *
303 allocfsent(fs)
304         register struct fstab *fs;
305 {
306         register struct fstab *new;
307
308         new = (struct fstab *)malloc(sizeof (*fs));
309         if (new == NULL ||
310             (new->fs_file = strdup(fs->fs_file)) == NULL ||
311             (new->fs_type = strdup(fs->fs_type)) == NULL ||
312             (new->fs_spec = strdup(fs->fs_spec)) == NULL)
313                 quit("%s\n", strerror(errno));
314         new->fs_passno = fs->fs_passno;
315         new->fs_freq = fs->fs_freq;
316         return (new);
317 }
318
319 struct  pfstab {
320         SLIST_ENTRY(pfstab) pf_list;
321         struct  fstab *pf_fstab;
322 };
323
324 static  SLIST_HEAD(, pfstab) table;
325
326 void
327 getfstab()
328 {
329         register struct fstab *fs;
330         register struct pfstab *pf;
331
332         if (setfsent() == 0) {
333                 msg("Can't open %s for dump table information: %s\n",
334                     _PATH_FSTAB, strerror(errno));
335                 return;
336         }
337         while ((fs = getfsent()) != NULL) {
338                 if (strcmp(fs->fs_type, FSTAB_RW) &&
339                     strcmp(fs->fs_type, FSTAB_RO) &&
340                     strcmp(fs->fs_type, FSTAB_RQ))
341                         continue;
342                 fs = allocfsent(fs);
343                 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
344                         quit("%s\n", strerror(errno));
345                 pf->pf_fstab = fs;
346                 SLIST_INSERT_HEAD(&table, pf, pf_list);
347         }
348         (void) endfsent();
349 }
350
351 /*
352  * Search in the fstab for a file name.
353  * This file name can be either the special or the path file name.
354  *
355  * The file name can omit the leading '/'.
356  */
357 struct fstab *
358 fstabsearch(key)
359         char *key;
360 {
361         register struct pfstab *pf;
362         register struct fstab *fs;
363         char *rn;
364
365         SLIST_FOREACH(pf, &table, pf_list) {
366                 fs = pf->pf_fstab;
367                 if (strcmp(fs->fs_file, key) == 0 ||
368                     strcmp(fs->fs_spec, key) == 0)
369                         return (fs);
370                 rn = rawname(fs->fs_spec);
371                 if (rn != NULL && strcmp(rn, key) == 0)
372                         return (fs);
373                 if (key[0] != '/') {
374                         if (*fs->fs_spec == '/' &&
375                             strcmp(fs->fs_spec + 1, key) == 0)
376                                 return (fs);
377                         if (*fs->fs_file == '/' &&
378                             strcmp(fs->fs_file + 1, key) == 0)
379                                 return (fs);
380                 }
381         }
382         return (NULL);
383 }
384
385 /*
386  *      Tell the operator what to do
387  */
388 void
389 lastdump(arg)
390         char    arg;    /* w ==> just what to do; W ==> most recent dumps */
391 {
392         register int i;
393         register struct fstab *dt;
394         register struct dumpdates *dtwalk;
395         char *lastname, *date;
396         int dumpme;
397         time_t tnow;
398         struct tm *tlast;
399
400         (void) time(&tnow);
401         getfstab();             /* /etc/fstab input */
402         initdumptimes();        /* /etc/dumpdates input */
403         qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
404
405         if (arg == 'w')
406                 (void) printf("Dump these file systems:\n");
407         else
408                 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
409         lastname = "??";
410         ITITERATE(i, dtwalk) {
411                 if (strncmp(lastname, dtwalk->dd_name,
412                     sizeof(dtwalk->dd_name)) == 0)
413                         continue;
414                 date = (char *)ctime(&dtwalk->dd_ddate);
415                 date[16] = '\0';        /* blast away seconds and year */
416                 lastname = dtwalk->dd_name;
417                 dt = fstabsearch(dtwalk->dd_name);
418                 dumpme = (dt != NULL && dt->fs_freq != 0);
419                 if (dumpme) {
420                     tlast = localtime(&dtwalk->dd_ddate);
421                     dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
422                                      - (tlast->tm_min * 60) - tlast->tm_sec
423                                      + (dt->fs_freq * 86400));
424                 };
425                 if (arg != 'w' || dumpme)
426                         (void) printf(
427                             "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
428                             dumpme && (arg != 'w') ? '>' : ' ',
429                             dtwalk->dd_name,
430                             dt ? dt->fs_file : "",
431                             dtwalk->dd_level,
432                             date);
433         }
434 }
435
436 int
437 datesort(a1, a2)
438         const void *a1, *a2;
439 {
440         struct dumpdates *d1 = *(struct dumpdates **)a1;
441         struct dumpdates *d2 = *(struct dumpdates **)a2;
442         int diff;
443
444         diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
445         if (diff == 0)
446                 return (d2->dd_ddate - d1->dd_ddate);
447         return (diff);
448 }