new way of modifying gdt
[freebsd-src/fkvm-freebsd.git] / sbin / dump / optr.c
blob8fc255ae73c59c63f9f52f7b7fcbdff1e7d3d2eb
1 /*-
2 * Copyright (c) 1980, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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 * 4. 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.
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.
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94";
33 #endif
34 static const char rcsid[] =
35 "$FreeBSD$";
36 #endif /* not lint */
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/wait.h>
41 #include <sys/time.h>
43 #include <ufs/ufs/dinode.h>
45 #include <errno.h>
46 #include <fstab.h>
47 #include <grp.h>
48 #include <limits.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <stdarg.h>
53 #include <signal.h>
54 #include <unistd.h>
56 #include "dump.h"
57 #include "pathnames.h"
59 void alarmcatch(int);
60 int datesort(const void *, const void *);
63 * Query the operator; This previously-fascist piece of code
64 * no longer requires an exact response.
65 * It is intended to protect dump aborting by inquisitive
66 * people banging on the console terminal to see what is
67 * happening which might cause dump to croak, destroying
68 * a large number of hours of work.
70 * Every 2 minutes we reprint the message, alerting others
71 * that dump needs attention.
73 static int timeout;
74 static const char *attnmessage; /* attention message */
76 int
77 query(const char *question)
79 char replybuffer[64];
80 int back, errcount;
81 FILE *mytty;
83 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
84 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
85 attnmessage = question;
86 timeout = 0;
87 alarmcatch(0);
88 back = -1;
89 errcount = 0;
90 do {
91 if (fgets(replybuffer, 63, mytty) == NULL) {
92 clearerr(mytty);
93 if (++errcount > 30) /* XXX ugly */
94 quit("excessive operator query failures\n");
95 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
96 back = 1;
97 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
98 back = 0;
99 } else {
100 (void) fprintf(stderr,
101 " DUMP: \"Yes\" or \"No\"?\n");
102 (void) fprintf(stderr,
103 " DUMP: %s: (\"yes\" or \"no\") ", question);
105 } while (back < 0);
108 * Turn off the alarm, and reset the signal to trap out..
110 (void) alarm(0);
111 if (signal(SIGALRM, sig) == SIG_IGN)
112 signal(SIGALRM, SIG_IGN);
113 (void) fclose(mytty);
114 return(back);
117 char lastmsg[BUFSIZ];
120 * Alert the console operator, and enable the alarm clock to
121 * sleep for 2 minutes in case nobody comes to satisfy dump
123 void
124 alarmcatch(int sig __unused)
126 if (notify == 0) {
127 if (timeout == 0)
128 (void) fprintf(stderr,
129 " DUMP: %s: (\"yes\" or \"no\") ",
130 attnmessage);
131 else
132 msgtail("\a\a");
133 } else {
134 if (timeout) {
135 msgtail("\n");
136 broadcast(""); /* just print last msg */
138 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ",
139 attnmessage);
141 signal(SIGALRM, alarmcatch);
142 (void) alarm(120);
143 timeout = 1;
147 * Here if an inquisitive operator interrupts the dump program
149 void
150 interrupt(int signo __unused)
152 msg("Interrupt received.\n");
153 if (query("Do you want to abort dump?"))
154 dumpabort(0);
158 * We now use wall(1) to do the actual broadcasting.
160 void
161 broadcast(const char *message)
163 FILE *fp;
164 char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
166 if (!notify)
167 return;
169 snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
170 if ((fp = popen(buf, "w")) == NULL)
171 return;
173 (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
174 if (lastmsg[0])
175 (void) fputs(lastmsg, fp);
176 if (message[0])
177 (void) fputs(message, fp);
179 (void) pclose(fp);
183 * Print out an estimate of the amount of time left to do the dump
186 time_t tschedule = 0;
188 void
189 timeest(void)
191 double percent;
192 time_t tnow, tdone;
193 char *tdone_str;
194 int deltat, hours, mins;
196 (void)time(&tnow);
197 if (blockswritten > tapesize) {
198 setproctitle("%s: 99.99%% done, finished soon", disk);
199 if (tnow >= tschedule) {
200 tschedule = tnow + 300;
201 msg("99.99%% done, finished soon\n");
203 } else {
204 deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
205 (double)(tnow - tstart_writing) / blockswritten * tapesize;
206 tdone = tnow + deltat;
207 percent = (blockswritten * 100.0) / tapesize;
208 hours = deltat / 3600;
209 mins = (deltat % 3600) / 60;
211 tdone_str = ctime(&tdone);
212 tdone_str[strlen(tdone_str) - 1] = '\0';
213 setproctitle(
214 "%s: pass %d: %3.2f%% done, finished in %d:%02d at %s",
215 disk, passno, percent, hours, mins, tdone_str);
216 if (tnow >= tschedule) {
217 tschedule = tnow + 300;
218 if (blockswritten < 500)
219 return;
220 msg("%3.2f%% done, finished in %d:%02d at %s\n", percent,
221 hours, mins, tdone_str);
227 * Schedule a printout of the estimate in the next call to timeest().
229 void
230 infosch(int signal __unused)
232 tschedule = 0;
235 void
236 msg(const char *fmt, ...)
238 va_list ap;
240 (void) fprintf(stderr," DUMP: ");
241 #ifdef TDEBUG
242 (void) fprintf(stderr, "pid=%d ", getpid());
243 #endif
244 va_start(ap, fmt);
245 (void) vfprintf(stderr, fmt, ap);
246 va_end(ap);
247 (void) fflush(stdout);
248 (void) fflush(stderr);
249 va_start(ap, fmt);
250 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
251 va_end(ap);
254 void
255 msgtail(const char *fmt, ...)
257 va_list ap;
259 va_start(ap, fmt);
260 (void) vfprintf(stderr, fmt, ap);
261 va_end(ap);
264 void
265 quit(const char *fmt, ...)
267 va_list ap;
269 (void) fprintf(stderr," DUMP: ");
270 #ifdef TDEBUG
271 (void) fprintf(stderr, "pid=%d ", getpid());
272 #endif
273 va_start(ap, fmt);
274 (void) vfprintf(stderr, fmt, ap);
275 va_end(ap);
276 (void) fflush(stdout);
277 (void) fflush(stderr);
278 dumpabort(0);
282 * Tell the operator what has to be done;
283 * we don't actually do it
286 struct fstab *
287 allocfsent(const struct fstab *fs)
289 struct fstab *new;
291 new = (struct fstab *)malloc(sizeof (*fs));
292 if (new == NULL ||
293 (new->fs_file = strdup(fs->fs_file)) == NULL ||
294 (new->fs_type = strdup(fs->fs_type)) == NULL ||
295 (new->fs_spec = strdup(fs->fs_spec)) == NULL)
296 quit("%s\n", strerror(errno));
297 new->fs_passno = fs->fs_passno;
298 new->fs_freq = fs->fs_freq;
299 return (new);
302 struct pfstab {
303 SLIST_ENTRY(pfstab) pf_list;
304 struct fstab *pf_fstab;
307 static SLIST_HEAD(, pfstab) table;
309 void
310 dump_getfstab(void)
312 struct fstab *fs;
313 struct pfstab *pf;
315 if (setfsent() == 0) {
316 msg("Can't open %s for dump table information: %s\n",
317 _PATH_FSTAB, strerror(errno));
318 return;
320 while ((fs = getfsent()) != NULL) {
321 if (strcmp(fs->fs_type, FSTAB_RW) &&
322 strcmp(fs->fs_type, FSTAB_RO) &&
323 strcmp(fs->fs_type, FSTAB_RQ))
324 continue;
325 fs = allocfsent(fs);
326 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
327 quit("%s\n", strerror(errno));
328 pf->pf_fstab = fs;
329 SLIST_INSERT_HEAD(&table, pf, pf_list);
331 (void) endfsent();
335 * Search in the fstab for a file name.
336 * This file name can be either the special or the path file name.
338 * The file name can omit the leading '/'.
340 struct fstab *
341 fstabsearch(const char *key)
343 struct pfstab *pf;
344 struct fstab *fs;
345 char *rn;
347 SLIST_FOREACH(pf, &table, pf_list) {
348 fs = pf->pf_fstab;
349 if (strcmp(fs->fs_file, key) == 0 ||
350 strcmp(fs->fs_spec, key) == 0)
351 return (fs);
352 rn = rawname(fs->fs_spec);
353 if (rn != NULL && strcmp(rn, key) == 0)
354 return (fs);
355 if (key[0] != '/') {
356 if (*fs->fs_spec == '/' &&
357 strcmp(fs->fs_spec + 1, key) == 0)
358 return (fs);
359 if (*fs->fs_file == '/' &&
360 strcmp(fs->fs_file + 1, key) == 0)
361 return (fs);
364 return (NULL);
368 * Tell the operator what to do
370 void
371 lastdump(int arg) /* w ==> just what to do; W ==> most recent dumps */
373 int i;
374 struct fstab *dt;
375 struct dumpdates *dtwalk;
376 char *lastname, *date;
377 int dumpme;
378 time_t tnow;
379 struct tm *tlast;
381 (void) time(&tnow);
382 dump_getfstab(); /* /etc/fstab input */
383 initdumptimes(); /* /etc/dumpdates input */
384 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
386 if (arg == 'w')
387 (void) printf("Dump these file systems:\n");
388 else
389 (void) printf("Last dump(s) done (Dump '>' file systems):\n");
390 lastname = "??";
391 ITITERATE(i, dtwalk) {
392 if (strncmp(lastname, dtwalk->dd_name,
393 sizeof(dtwalk->dd_name)) == 0)
394 continue;
395 date = (char *)ctime(&dtwalk->dd_ddate);
396 date[16] = '\0'; /* blast away seconds and year */
397 lastname = dtwalk->dd_name;
398 dt = fstabsearch(dtwalk->dd_name);
399 dumpme = (dt != NULL && dt->fs_freq != 0);
400 if (dumpme) {
401 tlast = localtime(&dtwalk->dd_ddate);
402 dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
403 - (tlast->tm_min * 60) - tlast->tm_sec
404 + (dt->fs_freq * 86400));
406 if (arg != 'w' || dumpme)
407 (void) printf(
408 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
409 dumpme && (arg != 'w') ? '>' : ' ',
410 dtwalk->dd_name,
411 dt ? dt->fs_file : "",
412 dtwalk->dd_level,
413 date);
418 datesort(const void *a1, const void *a2)
420 struct dumpdates *d1 = *(struct dumpdates **)a1;
421 struct dumpdates *d2 = *(struct dumpdates **)a2;
422 int diff;
424 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
425 if (diff == 0)
426 return (d2->dd_ddate - d1->dd_ddate);
427 return (diff);