* Nuke ~771 __P()'s from our tree.
[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.4 2003/11/01 17:15:58 drhodus 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(/* int, int */);
58 int     datesort(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(char *question)
76 {
77         char    replybuffer[64];
78         int     back, errcount;
79         FILE    *mytty;
80
81         if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
82                 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
83         attnmessage = question;
84         timeout = 0;
85         alarmcatch();
86         back = -1;
87         errcount = 0;
88         do {
89                 if (fgets(replybuffer, 63, mytty) == NULL) {
90                         clearerr(mytty);
91                         if (++errcount > 30)    /* XXX  ugly */
92                                 quit("excessive operator query failures\n");
93                 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
94                         back = 1;
95                 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
96                         back = 0;
97                 } else {
98                         (void) fprintf(stderr,
99                             "  DUMP: \"Yes\" or \"No\"?\n");
100                         (void) fprintf(stderr,
101                             "  DUMP: %s: (\"yes\" or \"no\") ", question);
102                 }
103         } while (back < 0);
104
105         /*
106          *      Turn off the alarm, and reset the signal to trap out..
107          */
108         (void) alarm(0);
109         if (signal(SIGALRM, sig) == SIG_IGN)
110                 signal(SIGALRM, SIG_IGN);
111         (void) fclose(mytty);
112         return(back);
113 }
114
115 char lastmsg[BUFSIZ];
116
117 /*
118  *      Alert the console operator, and enable the alarm clock to
119  *      sleep for 2 minutes in case nobody comes to satisfy dump
120  */
121 void
122 alarmcatch()
123 {
124         if (notify == 0) {
125                 if (timeout == 0)
126                         (void) fprintf(stderr,
127                             "  DUMP: %s: (\"yes\" or \"no\") ",
128                             attnmessage);
129                 else
130                         msgtail("\a\a");
131         } else {
132                 if (timeout) {
133                         msgtail("\n");
134                         broadcast("");          /* just print last msg */
135                 }
136                 (void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
137                     attnmessage);
138         }
139         signal(SIGALRM, alarmcatch);
140         (void) alarm(120);
141         timeout = 1;
142 }
143
144 /*
145  *      Here if an inquisitive operator interrupts the dump program
146  */
147 void
148 interrupt(int signo)
149 {
150         msg("Interrupt received.\n");
151         if (query("Do you want to abort dump?"))
152                 dumpabort(0);
153 }
154
155 /*
156  *      We now use wall(1) to do the actual broadcasting.
157  */
158 void
159 broadcast(char *message)
160 {
161         FILE *fp;
162         char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
163
164         if (!notify)
165                 return;
166
167         snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
168         if ((fp = popen(buf, "w")) == NULL)
169                 return;
170
171         (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
172         if (lastmsg[0])
173                 (void) fputs(lastmsg, fp);
174         if (message[0])
175                 (void) fputs(message, fp);
176
177         (void) pclose(fp);
178 }
179
180 /*
181  *      Print out an estimate of the amount of time left to do the dump
182  */
183
184 time_t  tschedule = 0;
185
186 void
187 timeest(void)
188 {
189         double percent;
190         time_t  tnow;
191         int deltat, hours, mins;
192
193         (void) time(&tnow);
194         deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
195             (double)(tnow - tstart_writing) / blockswritten * tapesize;
196         percent = (blockswritten * 100.0) / tapesize;
197         hours = deltat / 3600;
198         mins = (deltat % 3600) / 60;
199
200         setproctitle("%s: pass %d: %3.2f%% done, finished in %d:%02d",
201             disk, passno, percent, hours, mins);
202         if (tnow >= tschedule) {
203                 tschedule = tnow + 300;
204                 if (blockswritten < 500)
205                         return;
206                 msg("%3.2f%% done, finished in %d:%02d\n", percent, hours,
207                     mins);
208         }
209 }
210
211 /*
212  * Schedule a printout of the estimate in the next call to timeest().
213  */
214 void
215 infosch(int signal)
216 {
217         tschedule = 0;
218 }
219
220 void
221 msg(const char *fmt, ...)
222 {
223         va_list ap;
224
225         (void) fprintf(stderr,"  DUMP: ");
226 #ifdef TDEBUG
227         (void) fprintf(stderr, "pid=%d ", getpid());
228 #endif
229         va_start(ap, fmt);
230         (void) vfprintf(stderr, fmt, ap);
231         (void) fflush(stdout);
232         (void) fflush(stderr);
233         (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
234         va_end(ap);
235 }
236
237 void
238 msgtail(const char *fmt, ...)
239 {
240         va_list ap;
241
242         va_start(ap, fmt);
243         (void) vfprintf(stderr, fmt, ap);
244         va_end(ap);
245 }
246
247 void
248 quit(const char *fmt, ...)
249 {
250         va_list ap;
251
252         (void) fprintf(stderr,"  DUMP: ");
253 #ifdef TDEBUG
254         (void) fprintf(stderr, "pid=%d ", getpid());
255 #endif
256         va_start(ap, fmt);
257         (void) vfprintf(stderr, fmt, ap);
258         va_end(ap);
259         (void) fflush(stdout);
260         (void) fflush(stderr);
261         dumpabort(0);
262 }
263
264 /*
265  *      Tell the operator what has to be done;
266  *      we don't actually do it
267  */
268
269 struct fstab *
270 allocfsent(register struct fstab *fs)
271 {
272         register struct fstab *new;
273
274         new = (struct fstab *)malloc(sizeof (*fs));
275         if (new == NULL ||
276             (new->fs_file = strdup(fs->fs_file)) == NULL ||
277             (new->fs_type = strdup(fs->fs_type)) == NULL ||
278             (new->fs_spec = strdup(fs->fs_spec)) == NULL)
279                 quit("%s\n", strerror(errno));
280         new->fs_passno = fs->fs_passno;
281         new->fs_freq = fs->fs_freq;
282         return (new);
283 }
284
285 struct  pfstab {
286         SLIST_ENTRY(pfstab) pf_list;
287         struct  fstab *pf_fstab;
288 };
289
290 static  SLIST_HEAD(, pfstab) table;
291
292 void
293 getfstab(void)
294 {
295         register struct fstab *fs;
296         register struct pfstab *pf;
297
298         if (setfsent() == 0) {
299                 msg("Can't open %s for dump table information: %s\n",
300                     _PATH_FSTAB, strerror(errno));
301                 return;
302         }
303         while ((fs = getfsent()) != NULL) {
304                 if (strcmp(fs->fs_type, FSTAB_RW) &&
305                     strcmp(fs->fs_type, FSTAB_RO) &&
306                     strcmp(fs->fs_type, FSTAB_RQ))
307                         continue;
308                 fs = allocfsent(fs);
309                 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
310                         quit("%s\n", strerror(errno));
311                 pf->pf_fstab = fs;
312                 SLIST_INSERT_HEAD(&table, pf, pf_list);
313         }
314         (void) endfsent();
315 }
316
317 /*
318  * Search in the fstab for a file name.
319  * This file name can be either the special or the path file name.
320  *
321  * The file name can omit the leading '/'.
322  */
323 struct fstab *
324 fstabsearch(char *key)
325 {
326         register struct pfstab *pf;
327         register struct fstab *fs;
328         char *rn;
329
330         SLIST_FOREACH(pf, &table, pf_list) {
331                 fs = pf->pf_fstab;
332                 if (strcmp(fs->fs_file, key) == 0 ||
333                     strcmp(fs->fs_spec, key) == 0)
334                         return (fs);
335                 rn = rawname(fs->fs_spec);
336                 if (rn != NULL && strcmp(rn, key) == 0)
337                         return (fs);
338                 if (key[0] != '/') {
339                         if (*fs->fs_spec == '/' &&
340                             strcmp(fs->fs_spec + 1, key) == 0)
341                                 return (fs);
342                         if (*fs->fs_file == '/' &&
343                             strcmp(fs->fs_file + 1, key) == 0)
344                                 return (fs);
345                 }
346         }
347         return (NULL);
348 }
349
350 /*
351  *      Tell the operator what to do
352  *
353  * char arg: w ==> just what to do; W ==> most recent dumps 
354  */
355 void
356 lastdump(int arg)
357 {
358         register int i;
359         register struct fstab *dt;
360         register struct dumpdates *dtwalk;
361         char *lastname, *date;
362         int dumpme;
363         time_t tnow;
364         struct tm *tlast;
365
366         (void) time(&tnow);
367         getfstab();             /* /etc/fstab input */
368         initdumptimes();        /* /etc/dumpdates input */
369         qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
370
371         if (arg == 'w')
372                 (void) printf("Dump these file systems:\n");
373         else
374                 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
375         lastname = "??";
376         ITITERATE(i, dtwalk) {
377                 if (strncmp(lastname, dtwalk->dd_name,
378                     sizeof(dtwalk->dd_name)) == 0)
379                         continue;
380                 date = (char *)ctime(&dtwalk->dd_ddate);
381                 date[16] = '\0';        /* blast away seconds and year */
382                 lastname = dtwalk->dd_name;
383                 dt = fstabsearch(dtwalk->dd_name);
384                 dumpme = (dt != NULL && dt->fs_freq != 0);
385                 if (dumpme) {
386                     tlast = localtime(&dtwalk->dd_ddate);
387                     dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
388                                      - (tlast->tm_min * 60) - tlast->tm_sec
389                                      + (dt->fs_freq * 86400));
390                 };
391                 if (arg != 'w' || dumpme)
392                         (void) printf(
393                             "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
394                             dumpme && (arg != 'w') ? '>' : ' ',
395                             dtwalk->dd_name,
396                             dt ? dt->fs_file : "",
397                             dtwalk->dd_level,
398                             date);
399         }
400 }
401
402 int
403 datesort(const void *a1, const void *a2)
404 {
405         struct dumpdates *d1 = *(struct dumpdates **)a1;
406         struct dumpdates *d2 = *(struct dumpdates **)a2;
407         int diff;
408
409         diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
410         if (diff == 0)
411                 return (d2->dd_ddate - d1->dd_ddate);
412         return (diff);
413 }