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