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
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.
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
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 $
33 #include <sys/param.h>
34 #include <sys/queue.h>
48 #include <vfs/ufs/dinode.h>
51 #include "pathnames.h"
53 static void alarmcatch(int);
54 static int datesort(const void *, const void *);
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.
64 * Every 2 minutes we reprint the message, alerting others
65 * that dump needs attention.
68 static const char *attnmessage; /* attention message */
71 query(const char *question)
77 if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
78 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
79 attnmessage = question;
85 if (fgets(replybuffer, 63, mytty) == NULL) {
87 if (++errcount > 30) /* XXX ugly */
88 quit("excessive operator query failures\n");
89 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
91 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
94 fprintf(stderr, " DUMP: \"Yes\" or \"No\"?\n");
95 fprintf(stderr, " DUMP: %s: (\"yes\" or \"no\") ",
101 * Turn off the alarm, and reset the signal to trap out..
104 if (signal(SIGALRM, sig) == SIG_IGN)
105 signal(SIGALRM, SIG_IGN);
110 char lastmsg[BUFSIZ];
113 * Alert the console operator, and enable the alarm clock to
114 * sleep for 2 minutes in case nobody comes to satisfy dump
117 alarmcatch(int signo __unused)
121 fprintf(stderr, " DUMP: %s: (\"yes\" or \"no\") ",
128 broadcast(""); /* just print last msg */
130 fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", attnmessage);
132 signal(SIGALRM, alarmcatch);
138 * Here if an inquisitive operator interrupts the dump program
141 interrupt(int signo __unused)
143 msg("Interrupt received.\n");
144 if (query("Do you want to abort dump?"))
149 * We now use wall(1) to do the actual broadcasting.
152 broadcast(const char *message)
155 char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
160 snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
161 if ((fp = popen(buf, "w")) == NULL)
164 fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
174 * Print out an estimate of the amount of time left to do the dump
177 time_t tschedule = 0;
184 int deltat, hours, mins;
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;
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)
199 msg("%3.2f%% done, finished in %d:%02d\n", percent, hours,
205 * Schedule a printout of the estimate in the next call to timeest().
208 infosch(int signo __unused)
214 msg(const char *fmt, ...)
218 fprintf(stderr," DUMP: ");
220 fprintf(stderr, "pid=%d ", getpid());
223 vfprintf(stderr, fmt, ap);
228 vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
233 msgtail(const char *fmt, ...)
238 vfprintf(stderr, fmt, ap);
243 quit(const char *fmt, ...)
247 fprintf(stderr," DUMP: ");
249 fprintf(stderr, "pid=%d ", getpid());
252 vfprintf(stderr, fmt, ap);
260 * Tell the operator what has to be done;
261 * we don't actually do it
264 static struct fstab *
265 allocfsent(struct fstab *fs)
269 new = (struct fstab *)malloc(sizeof (*fs));
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;
281 SLIST_ENTRY(pfstab) pf_list;
282 struct fstab *pf_fstab;
285 static SLIST_HEAD(, pfstab) table;
293 if (setfsent() == 0) {
294 msg("Can't open %s for dump table information: %s\n",
295 _PATH_FSTAB, strerror(errno));
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))
304 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
305 quit("%s\n", strerror(errno));
307 SLIST_INSERT_HEAD(&table, pf, pf_list);
313 * Search in the fstab for a file name.
314 * This file name can be either the special or the path file name.
316 * The file name can omit the leading '/'.
319 fstabsearch(const char *key)
325 SLIST_FOREACH(pf, &table, pf_list) {
327 if (strcmp(fs->fs_file, key) == 0 ||
328 strcmp(fs->fs_spec, key) == 0)
330 rn = rawname(fs->fs_spec);
331 if (rn != NULL && strcmp(rn, key) == 0)
334 if (*fs->fs_spec == '/' &&
335 strcmp(fs->fs_spec + 1, key) == 0)
337 if (*fs->fs_file == '/' &&
338 strcmp(fs->fs_file + 1, key) == 0)
346 * Tell the operator what to do
348 * char arg: w ==> just what to do; W ==> most recent dumps
355 struct dumpdates *dtwalk;
356 const char *lastname;
363 dump_getfstab(); /* /etc/fstab input */
364 initdumptimes(); /* /etc/dumpdates input */
365 qsort(ddatev, nddates, sizeof(struct dumpdates *), datesort);
368 printf("Dump these file systems:\n");
370 printf("Last dump(s) done (Dump '>' file systems):\n");
372 ITITERATE(i, dtwalk) {
373 if (strncmp(lastname, dtwalk->dd_name,
374 sizeof(dtwalk->dd_name)) == 0)
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);
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));
387 if (arg != 'w' || dumpme)
389 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
390 dumpme && (arg != 'w') ? '>' : ' ',
392 dt ? dt->fs_file : "",
399 datesort(const void *a1, const void *a2)
401 const struct dumpdates *d1 = *(const struct dumpdates * const *)a1;
402 const struct dumpdates *d2 = *(const struct dumpdates * const *)a2;
405 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
407 return (d2->dd_ddate - d1->dd_ddate);