optr.c revision 103949
11558Srgrimes/*- 21558Srgrimes * Copyright (c) 1980, 1988, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3536997Scharnier#if 0 361558Srgrimesstatic char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94"; 3736997Scharnier#endif 3836997Scharnierstatic const char rcsid[] = 3950476Speter "$FreeBSD: head/sbin/dump/optr.c 103949 2002-09-25 04:06:37Z mike $"; 401558Srgrimes#endif /* not lint */ 411558Srgrimes 421558Srgrimes#include <sys/param.h> 4371787Sphk#include <sys/queue.h> 441558Srgrimes#include <sys/wait.h> 451558Srgrimes#include <sys/time.h> 461558Srgrimes 4798542Smckusick#include <ufs/ufs/dinode.h> 4898542Smckusick 491558Srgrimes#include <errno.h> 501558Srgrimes#include <fstab.h> 511558Srgrimes#include <grp.h> 52103949Smike#include <limits.h> 531558Srgrimes#include <stdio.h> 541558Srgrimes#include <stdlib.h> 551558Srgrimes#include <string.h> 561558Srgrimes#include <stdarg.h> 5790742Siedowse#include <signal.h> 581558Srgrimes#include <unistd.h> 591558Srgrimes 601558Srgrimes#include "dump.h" 611558Srgrimes#include "pathnames.h" 621558Srgrimes 6392837Simpvoid alarmcatch(int); 6492837Simpint datesort(const void *, const void *); 651558Srgrimes 661558Srgrimes/* 671558Srgrimes * Query the operator; This previously-fascist piece of code 681558Srgrimes * no longer requires an exact response. 691558Srgrimes * It is intended to protect dump aborting by inquisitive 701558Srgrimes * people banging on the console terminal to see what is 711558Srgrimes * happening which might cause dump to croak, destroying 721558Srgrimes * a large number of hours of work. 731558Srgrimes * 741558Srgrimes * Every 2 minutes we reprint the message, alerting others 751558Srgrimes * that dump needs attention. 761558Srgrimes */ 771558Srgrimesstatic int timeout; 7892837Simpstatic const char *attnmessage; /* attention message */ 791558Srgrimes 801558Srgrimesint 8192837Simpquery(const char *question) 821558Srgrimes{ 831558Srgrimes char replybuffer[64]; 841558Srgrimes int back, errcount; 851558Srgrimes FILE *mytty; 861558Srgrimes 871558Srgrimes if ((mytty = fopen(_PATH_TTY, "r")) == NULL) 881558Srgrimes quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); 891558Srgrimes attnmessage = question; 901558Srgrimes timeout = 0; 9192837Simp alarmcatch(0); 921558Srgrimes back = -1; 931558Srgrimes errcount = 0; 941558Srgrimes do { 951558Srgrimes if (fgets(replybuffer, 63, mytty) == NULL) { 961558Srgrimes clearerr(mytty); 971558Srgrimes if (++errcount > 30) /* XXX ugly */ 981558Srgrimes quit("excessive operator query failures\n"); 991558Srgrimes } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { 1001558Srgrimes back = 1; 1011558Srgrimes } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { 1021558Srgrimes back = 0; 1031558Srgrimes } else { 1041558Srgrimes (void) fprintf(stderr, 1051558Srgrimes " DUMP: \"Yes\" or \"No\"?\n"); 1061558Srgrimes (void) fprintf(stderr, 1071558Srgrimes " DUMP: %s: (\"yes\" or \"no\") ", question); 1081558Srgrimes } 1091558Srgrimes } while (back < 0); 1101558Srgrimes 1111558Srgrimes /* 1121558Srgrimes * Turn off the alarm, and reset the signal to trap out.. 1131558Srgrimes */ 1141558Srgrimes (void) alarm(0); 1151558Srgrimes if (signal(SIGALRM, sig) == SIG_IGN) 1161558Srgrimes signal(SIGALRM, SIG_IGN); 1171558Srgrimes (void) fclose(mytty); 1181558Srgrimes return(back); 1191558Srgrimes} 1201558Srgrimes 12183083Sruchar lastmsg[BUFSIZ]; 1221558Srgrimes 1231558Srgrimes/* 1241558Srgrimes * Alert the console operator, and enable the alarm clock to 1251558Srgrimes * sleep for 2 minutes in case nobody comes to satisfy dump 1261558Srgrimes */ 1271558Srgrimesvoid 12892837Simpalarmcatch(int sig __unused) 1291558Srgrimes{ 1301558Srgrimes if (notify == 0) { 1311558Srgrimes if (timeout == 0) 1321558Srgrimes (void) fprintf(stderr, 1331558Srgrimes " DUMP: %s: (\"yes\" or \"no\") ", 1341558Srgrimes attnmessage); 1351558Srgrimes else 13671750Sphk msgtail("\a\a"); 1371558Srgrimes } else { 1381558Srgrimes if (timeout) { 1391558Srgrimes msgtail("\n"); 1401558Srgrimes broadcast(""); /* just print last msg */ 1411558Srgrimes } 1421558Srgrimes (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 1431558Srgrimes attnmessage); 1441558Srgrimes } 1451558Srgrimes signal(SIGALRM, alarmcatch); 1461558Srgrimes (void) alarm(120); 1471558Srgrimes timeout = 1; 1481558Srgrimes} 1491558Srgrimes 1501558Srgrimes/* 1511558Srgrimes * Here if an inquisitive operator interrupts the dump program 1521558Srgrimes */ 1531558Srgrimesvoid 15492837Simpinterrupt(int signo __unused) 1551558Srgrimes{ 1561558Srgrimes msg("Interrupt received.\n"); 1571558Srgrimes if (query("Do you want to abort dump?")) 1581558Srgrimes dumpabort(0); 1591558Srgrimes} 1601558Srgrimes 1611558Srgrimes/* 16283083Sru * We now use wall(1) to do the actual broadcasting. 1631558Srgrimes */ 1641558Srgrimesvoid 16592837Simpbroadcast(const char *message) 1661558Srgrimes{ 16783083Sru FILE *fp; 16883083Sru char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3]; 1691558Srgrimes 17083083Sru if (!notify) 1711558Srgrimes return; 1721558Srgrimes 17383083Sru snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT); 17483083Sru if ((fp = popen(buf, "w")) == NULL) 1751558Srgrimes return; 1761558Srgrimes 17783083Sru (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp); 17883083Sru if (lastmsg[0]) 17983083Sru (void) fputs(lastmsg, fp); 18083083Sru if (message[0]) 18183083Sru (void) fputs(message, fp); 1821558Srgrimes 18383083Sru (void) pclose(fp); 1841558Srgrimes} 1851558Srgrimes 1861558Srgrimes/* 18783083Sru * Print out an estimate of the amount of time left to do the dump 1881558Srgrimes */ 1891558Srgrimes 1901558Srgrimestime_t tschedule = 0; 1911558Srgrimes 1921558Srgrimesvoid 19392837Simptimeest(void) 1941558Srgrimes{ 19590743Siedowse double percent; 19685622Sdillon time_t tnow; 19790743Siedowse int deltat, hours, mins; 1981558Srgrimes 19990742Siedowse (void) time(&tnow); 20090743Siedowse deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow + 20190743Siedowse (double)(tnow - tstart_writing) / blockswritten * tapesize; 20290743Siedowse percent = (blockswritten * 100.0) / tapesize; 20390743Siedowse hours = deltat / 3600; 20490743Siedowse mins = (deltat % 3600) / 60; 20590743Siedowse 20690743Siedowse setproctitle("%s: pass %d: %3.2f%% done, finished in %d:%02d", 20790743Siedowse disk, passno, percent, hours, mins); 2081558Srgrimes if (tnow >= tschedule) { 2091558Srgrimes tschedule = tnow + 300; 2101558Srgrimes if (blockswritten < 500) 2118871Srgrimes return; 21290743Siedowse msg("%3.2f%% done, finished in %d:%02d\n", percent, hours, 21390743Siedowse mins); 2141558Srgrimes } 2151558Srgrimes} 2161558Srgrimes 21790742Siedowse/* 21890742Siedowse * Schedule a printout of the estimate in the next call to timeest(). 21990742Siedowse */ 2201558Srgrimesvoid 22192837Simpinfosch(int signal __unused) 22290742Siedowse{ 22390742Siedowse tschedule = 0; 22490742Siedowse} 22590742Siedowse 22690742Siedowsevoid 2271558Srgrimesmsg(const char *fmt, ...) 2281558Srgrimes{ 2291558Srgrimes va_list ap; 2301558Srgrimes 2311558Srgrimes (void) fprintf(stderr," DUMP: "); 2321558Srgrimes#ifdef TDEBUG 2331558Srgrimes (void) fprintf(stderr, "pid=%d ", getpid()); 2341558Srgrimes#endif 2351558Srgrimes va_start(ap, fmt); 2361558Srgrimes (void) vfprintf(stderr, fmt, ap); 2371558Srgrimes (void) fflush(stdout); 2381558Srgrimes (void) fflush(stderr); 23953084Simp (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap); 2401558Srgrimes va_end(ap); 2411558Srgrimes} 2421558Srgrimes 2431558Srgrimesvoid 2441558Srgrimesmsgtail(const char *fmt, ...) 2451558Srgrimes{ 2461558Srgrimes va_list ap; 2471558Srgrimes va_start(ap, fmt); 2481558Srgrimes (void) vfprintf(stderr, fmt, ap); 2491558Srgrimes va_end(ap); 2501558Srgrimes} 2511558Srgrimes 2521558Srgrimesvoid 2531558Srgrimesquit(const char *fmt, ...) 2541558Srgrimes{ 2551558Srgrimes va_list ap; 2561558Srgrimes 2571558Srgrimes (void) fprintf(stderr," DUMP: "); 2581558Srgrimes#ifdef TDEBUG 2591558Srgrimes (void) fprintf(stderr, "pid=%d ", getpid()); 2601558Srgrimes#endif 2611558Srgrimes va_start(ap, fmt); 2621558Srgrimes (void) vfprintf(stderr, fmt, ap); 2631558Srgrimes va_end(ap); 2641558Srgrimes (void) fflush(stdout); 2651558Srgrimes (void) fflush(stderr); 2661558Srgrimes dumpabort(0); 2671558Srgrimes} 2681558Srgrimes 2691558Srgrimes/* 2701558Srgrimes * Tell the operator what has to be done; 2711558Srgrimes * we don't actually do it 2721558Srgrimes */ 2731558Srgrimes 2741558Srgrimesstruct fstab * 27592837Simpallocfsent(const struct fstab *fs) 2761558Srgrimes{ 27786473Siedowse struct fstab *new; 2781558Srgrimes 2791558Srgrimes new = (struct fstab *)malloc(sizeof (*fs)); 2801558Srgrimes if (new == NULL || 2811558Srgrimes (new->fs_file = strdup(fs->fs_file)) == NULL || 2821558Srgrimes (new->fs_type = strdup(fs->fs_type)) == NULL || 2831558Srgrimes (new->fs_spec = strdup(fs->fs_spec)) == NULL) 2841558Srgrimes quit("%s\n", strerror(errno)); 2851558Srgrimes new->fs_passno = fs->fs_passno; 2861558Srgrimes new->fs_freq = fs->fs_freq; 2871558Srgrimes return (new); 2881558Srgrimes} 2891558Srgrimes 2901558Srgrimesstruct pfstab { 29171787Sphk SLIST_ENTRY(pfstab) pf_list; 2921558Srgrimes struct fstab *pf_fstab; 2931558Srgrimes}; 2941558Srgrimes 29571787Sphkstatic SLIST_HEAD(, pfstab) table; 2961558Srgrimes 2971558Srgrimesvoid 29892837Simpgetfstab(void) 2991558Srgrimes{ 30086473Siedowse struct fstab *fs; 30186473Siedowse struct pfstab *pf; 3021558Srgrimes 3031558Srgrimes if (setfsent() == 0) { 3041558Srgrimes msg("Can't open %s for dump table information: %s\n", 3051558Srgrimes _PATH_FSTAB, strerror(errno)); 3061558Srgrimes return; 3071558Srgrimes } 3081558Srgrimes while ((fs = getfsent()) != NULL) { 3091558Srgrimes if (strcmp(fs->fs_type, FSTAB_RW) && 3101558Srgrimes strcmp(fs->fs_type, FSTAB_RO) && 3111558Srgrimes strcmp(fs->fs_type, FSTAB_RQ)) 3121558Srgrimes continue; 3131558Srgrimes fs = allocfsent(fs); 3141558Srgrimes if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 3151558Srgrimes quit("%s\n", strerror(errno)); 3161558Srgrimes pf->pf_fstab = fs; 31771787Sphk SLIST_INSERT_HEAD(&table, pf, pf_list); 3181558Srgrimes } 3191558Srgrimes (void) endfsent(); 3201558Srgrimes} 3211558Srgrimes 3221558Srgrimes/* 3231558Srgrimes * Search in the fstab for a file name. 3241558Srgrimes * This file name can be either the special or the path file name. 3251558Srgrimes * 3261558Srgrimes * The file name can omit the leading '/'. 3271558Srgrimes */ 3281558Srgrimesstruct fstab * 32992837Simpfstabsearch(const char *key) 3301558Srgrimes{ 33186473Siedowse struct pfstab *pf; 33286473Siedowse struct fstab *fs; 3331558Srgrimes char *rn; 3341558Srgrimes 33571787Sphk SLIST_FOREACH(pf, &table, pf_list) { 3361558Srgrimes fs = pf->pf_fstab; 3371558Srgrimes if (strcmp(fs->fs_file, key) == 0 || 3381558Srgrimes strcmp(fs->fs_spec, key) == 0) 3391558Srgrimes return (fs); 3401558Srgrimes rn = rawname(fs->fs_spec); 3411558Srgrimes if (rn != NULL && strcmp(rn, key) == 0) 3421558Srgrimes return (fs); 3431558Srgrimes if (key[0] != '/') { 3441558Srgrimes if (*fs->fs_spec == '/' && 3451558Srgrimes strcmp(fs->fs_spec + 1, key) == 0) 3461558Srgrimes return (fs); 3471558Srgrimes if (*fs->fs_file == '/' && 3481558Srgrimes strcmp(fs->fs_file + 1, key) == 0) 3491558Srgrimes return (fs); 3501558Srgrimes } 3511558Srgrimes } 3521558Srgrimes return (NULL); 3531558Srgrimes} 3541558Srgrimes 3551558Srgrimes/* 3561558Srgrimes * Tell the operator what to do 3571558Srgrimes */ 3581558Srgrimesvoid 35992837Simplastdump(int arg) /* w ==> just what to do; W ==> most recent dumps */ 3601558Srgrimes{ 36186473Siedowse int i; 36286473Siedowse struct fstab *dt; 36386473Siedowse struct dumpdates *dtwalk; 3641558Srgrimes char *lastname, *date; 3651558Srgrimes int dumpme; 36648447Sjkh time_t tnow; 36748447Sjkh struct tm *tlast; 3681558Srgrimes 3691558Srgrimes (void) time(&tnow); 3701558Srgrimes getfstab(); /* /etc/fstab input */ 3711558Srgrimes initdumptimes(); /* /etc/dumpdates input */ 3721558Srgrimes qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 3731558Srgrimes 3741558Srgrimes if (arg == 'w') 375102231Strhodes (void) printf("Dump these file systems:\n"); 3761558Srgrimes else 377102231Strhodes (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 3781558Srgrimes lastname = "??"; 3791558Srgrimes ITITERATE(i, dtwalk) { 3801558Srgrimes if (strncmp(lastname, dtwalk->dd_name, 3811558Srgrimes sizeof(dtwalk->dd_name)) == 0) 3821558Srgrimes continue; 38348447Sjkh date = (char *)ctime(&dtwalk->dd_ddate); 3841558Srgrimes date[16] = '\0'; /* blast away seconds and year */ 3851558Srgrimes lastname = dtwalk->dd_name; 3861558Srgrimes dt = fstabsearch(dtwalk->dd_name); 38748447Sjkh dumpme = (dt != NULL && dt->fs_freq != 0); 38848447Sjkh if (dumpme) { 38948447Sjkh tlast = localtime(&dtwalk->dd_ddate); 39048447Sjkh dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600) 39148447Sjkh - (tlast->tm_min * 60) - tlast->tm_sec 39248447Sjkh + (dt->fs_freq * 86400)); 39348447Sjkh }; 3941558Srgrimes if (arg != 'w' || dumpme) 3951558Srgrimes (void) printf( 3961558Srgrimes "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 3971558Srgrimes dumpme && (arg != 'w') ? '>' : ' ', 3981558Srgrimes dtwalk->dd_name, 3991558Srgrimes dt ? dt->fs_file : "", 4001558Srgrimes dtwalk->dd_level, 4011558Srgrimes date); 4021558Srgrimes } 4031558Srgrimes} 4041558Srgrimes 4051558Srgrimesint 40692837Simpdatesort(const void *a1, const void *a2) 4071558Srgrimes{ 4081558Srgrimes struct dumpdates *d1 = *(struct dumpdates **)a1; 4091558Srgrimes struct dumpdates *d2 = *(struct dumpdates **)a2; 4101558Srgrimes int diff; 4111558Srgrimes 4121558Srgrimes diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 4131558Srgrimes if (diff == 0) 4141558Srgrimes return (d2->dd_ddate - d1->dd_ddate); 4151558Srgrimes return (diff); 4161558Srgrimes} 417