optr.c revision 130753
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 * 4. Neither the name of the University nor the names of its contributors 141558Srgrimes * may be used to endorse or promote products derived from this software 151558Srgrimes * without specific prior written permission. 161558Srgrimes * 171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271558Srgrimes * SUCH DAMAGE. 281558Srgrimes */ 291558Srgrimes 301558Srgrimes#ifndef lint 3136997Scharnier#if 0 321558Srgrimesstatic char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94"; 3336997Scharnier#endif 3436997Scharnierstatic const char rcsid[] = 3550476Speter "$FreeBSD: head/sbin/dump/optr.c 130753 2004-06-19 22:41:18Z iedowse $"; 361558Srgrimes#endif /* not lint */ 371558Srgrimes 381558Srgrimes#include <sys/param.h> 3971787Sphk#include <sys/queue.h> 401558Srgrimes#include <sys/wait.h> 411558Srgrimes#include <sys/time.h> 421558Srgrimes 4398542Smckusick#include <ufs/ufs/dinode.h> 4498542Smckusick 451558Srgrimes#include <errno.h> 461558Srgrimes#include <fstab.h> 471558Srgrimes#include <grp.h> 48103949Smike#include <limits.h> 491558Srgrimes#include <stdio.h> 501558Srgrimes#include <stdlib.h> 511558Srgrimes#include <string.h> 521558Srgrimes#include <stdarg.h> 5390742Siedowse#include <signal.h> 541558Srgrimes#include <unistd.h> 551558Srgrimes 561558Srgrimes#include "dump.h" 571558Srgrimes#include "pathnames.h" 581558Srgrimes 5992837Simpvoid alarmcatch(int); 6092837Simpint datesort(const void *, const void *); 611558Srgrimes 621558Srgrimes/* 631558Srgrimes * Query the operator; This previously-fascist piece of code 641558Srgrimes * no longer requires an exact response. 651558Srgrimes * It is intended to protect dump aborting by inquisitive 661558Srgrimes * people banging on the console terminal to see what is 671558Srgrimes * happening which might cause dump to croak, destroying 681558Srgrimes * a large number of hours of work. 691558Srgrimes * 701558Srgrimes * Every 2 minutes we reprint the message, alerting others 711558Srgrimes * that dump needs attention. 721558Srgrimes */ 731558Srgrimesstatic int timeout; 7492837Simpstatic const char *attnmessage; /* attention message */ 751558Srgrimes 761558Srgrimesint 7792837Simpquery(const char *question) 781558Srgrimes{ 791558Srgrimes char replybuffer[64]; 801558Srgrimes int back, errcount; 811558Srgrimes FILE *mytty; 821558Srgrimes 831558Srgrimes if ((mytty = fopen(_PATH_TTY, "r")) == NULL) 841558Srgrimes quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); 851558Srgrimes attnmessage = question; 861558Srgrimes timeout = 0; 8792837Simp alarmcatch(0); 881558Srgrimes back = -1; 891558Srgrimes errcount = 0; 901558Srgrimes do { 911558Srgrimes if (fgets(replybuffer, 63, mytty) == NULL) { 921558Srgrimes clearerr(mytty); 931558Srgrimes if (++errcount > 30) /* XXX ugly */ 941558Srgrimes quit("excessive operator query failures\n"); 951558Srgrimes } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { 961558Srgrimes back = 1; 971558Srgrimes } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { 981558Srgrimes back = 0; 991558Srgrimes } else { 1001558Srgrimes (void) fprintf(stderr, 1011558Srgrimes " DUMP: \"Yes\" or \"No\"?\n"); 1021558Srgrimes (void) fprintf(stderr, 1031558Srgrimes " DUMP: %s: (\"yes\" or \"no\") ", question); 1041558Srgrimes } 1051558Srgrimes } while (back < 0); 1061558Srgrimes 1071558Srgrimes /* 1081558Srgrimes * Turn off the alarm, and reset the signal to trap out.. 1091558Srgrimes */ 1101558Srgrimes (void) alarm(0); 1111558Srgrimes if (signal(SIGALRM, sig) == SIG_IGN) 1121558Srgrimes signal(SIGALRM, SIG_IGN); 1131558Srgrimes (void) fclose(mytty); 1141558Srgrimes return(back); 1151558Srgrimes} 1161558Srgrimes 11783083Sruchar lastmsg[BUFSIZ]; 1181558Srgrimes 1191558Srgrimes/* 1201558Srgrimes * Alert the console operator, and enable the alarm clock to 1211558Srgrimes * sleep for 2 minutes in case nobody comes to satisfy dump 1221558Srgrimes */ 1231558Srgrimesvoid 12492837Simpalarmcatch(int sig __unused) 1251558Srgrimes{ 1261558Srgrimes if (notify == 0) { 1271558Srgrimes if (timeout == 0) 1281558Srgrimes (void) fprintf(stderr, 1291558Srgrimes " DUMP: %s: (\"yes\" or \"no\") ", 1301558Srgrimes attnmessage); 1311558Srgrimes else 13271750Sphk msgtail("\a\a"); 1331558Srgrimes } else { 1341558Srgrimes if (timeout) { 1351558Srgrimes msgtail("\n"); 1361558Srgrimes broadcast(""); /* just print last msg */ 1371558Srgrimes } 1381558Srgrimes (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 1391558Srgrimes attnmessage); 1401558Srgrimes } 1411558Srgrimes signal(SIGALRM, alarmcatch); 1421558Srgrimes (void) alarm(120); 1431558Srgrimes timeout = 1; 1441558Srgrimes} 1451558Srgrimes 1461558Srgrimes/* 1471558Srgrimes * Here if an inquisitive operator interrupts the dump program 1481558Srgrimes */ 1491558Srgrimesvoid 15092837Simpinterrupt(int signo __unused) 1511558Srgrimes{ 1521558Srgrimes msg("Interrupt received.\n"); 1531558Srgrimes if (query("Do you want to abort dump?")) 1541558Srgrimes dumpabort(0); 1551558Srgrimes} 1561558Srgrimes 1571558Srgrimes/* 15883083Sru * We now use wall(1) to do the actual broadcasting. 1591558Srgrimes */ 1601558Srgrimesvoid 16192837Simpbroadcast(const char *message) 1621558Srgrimes{ 16383083Sru FILE *fp; 16483083Sru char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3]; 1651558Srgrimes 16683083Sru if (!notify) 1671558Srgrimes return; 1681558Srgrimes 16983083Sru snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT); 17083083Sru if ((fp = popen(buf, "w")) == NULL) 1711558Srgrimes return; 1721558Srgrimes 17383083Sru (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp); 17483083Sru if (lastmsg[0]) 17583083Sru (void) fputs(lastmsg, fp); 17683083Sru if (message[0]) 17783083Sru (void) fputs(message, fp); 1781558Srgrimes 17983083Sru (void) pclose(fp); 1801558Srgrimes} 1811558Srgrimes 1821558Srgrimes/* 18383083Sru * Print out an estimate of the amount of time left to do the dump 1841558Srgrimes */ 1851558Srgrimes 1861558Srgrimestime_t tschedule = 0; 1871558Srgrimes 1881558Srgrimesvoid 18992837Simptimeest(void) 1901558Srgrimes{ 19190743Siedowse double percent; 192129556Simp time_t tnow, tdone; 19390743Siedowse int deltat, hours, mins; 1941558Srgrimes 195130753Siedowse (void)time(&tnow); 196129556Simp if (blockswritten > tapesize) { 197129556Simp setproctitle("%s: 99.99%% done, finished soon", disk); 198130753Siedowse if (tnow >= tschedule) { 199130753Siedowse tschedule = tnow + 300; 200130753Siedowse msg("99.99%% done, finished soon\n"); 201130753Siedowse } 202129556Simp } else { 203129556Simp deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow + 204129556Simp (double)(tnow - tstart_writing) / blockswritten * tapesize; 205129556Simp tdone = tnow + deltat; 206129556Simp percent = (blockswritten * 100.0) / tapesize; 207129556Simp hours = deltat / 3600; 208129556Simp mins = (deltat % 3600) / 60; 20990743Siedowse 210129556Simp setproctitle( 211129556Simp "%s: pass %d: %3.2f%% done, finished in %d:%02d at %s", 212129556Simp disk, passno, percent, hours, mins, ctime(&tdone)); 213129556Simp if (tnow >= tschedule) { 214129556Simp tschedule = tnow + 300; 215129556Simp if (blockswritten < 500) 216129556Simp return; 217129556Simp msg("%3.2f%% done, finished in %d:%02d at %s", percent, 218129556Simp hours, mins, ctime(&tdone)); 219129556Simp } 2201558Srgrimes } 2211558Srgrimes} 2221558Srgrimes 22390742Siedowse/* 22490742Siedowse * Schedule a printout of the estimate in the next call to timeest(). 22590742Siedowse */ 2261558Srgrimesvoid 22792837Simpinfosch(int signal __unused) 22890742Siedowse{ 22990742Siedowse tschedule = 0; 23090742Siedowse} 23190742Siedowse 23290742Siedowsevoid 2331558Srgrimesmsg(const char *fmt, ...) 2341558Srgrimes{ 2351558Srgrimes va_list ap; 236120323Sps va_list ap2; 2371558Srgrimes 2381558Srgrimes (void) fprintf(stderr," DUMP: "); 2391558Srgrimes#ifdef TDEBUG 2401558Srgrimes (void) fprintf(stderr, "pid=%d ", getpid()); 2411558Srgrimes#endif 2421558Srgrimes va_start(ap, fmt); 243120323Sps va_copy(ap2, ap); 244120323Sps (void) vfprintf(stderr, fmt, ap); 2451558Srgrimes (void) fflush(stdout); 2461558Srgrimes (void) fflush(stderr); 247120323Sps (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap2); 2481558Srgrimes va_end(ap); 249120323Sps va_end(ap2); 2501558Srgrimes} 2511558Srgrimes 2521558Srgrimesvoid 2531558Srgrimesmsgtail(const char *fmt, ...) 2541558Srgrimes{ 2551558Srgrimes va_list ap; 2561558Srgrimes va_start(ap, fmt); 2571558Srgrimes (void) vfprintf(stderr, fmt, ap); 2581558Srgrimes va_end(ap); 2591558Srgrimes} 2601558Srgrimes 2611558Srgrimesvoid 2621558Srgrimesquit(const char *fmt, ...) 2631558Srgrimes{ 2641558Srgrimes va_list ap; 2651558Srgrimes 2661558Srgrimes (void) fprintf(stderr," DUMP: "); 2671558Srgrimes#ifdef TDEBUG 2681558Srgrimes (void) fprintf(stderr, "pid=%d ", getpid()); 2691558Srgrimes#endif 2701558Srgrimes va_start(ap, fmt); 2711558Srgrimes (void) vfprintf(stderr, fmt, ap); 2721558Srgrimes va_end(ap); 2731558Srgrimes (void) fflush(stdout); 2741558Srgrimes (void) fflush(stderr); 2751558Srgrimes dumpabort(0); 2761558Srgrimes} 2771558Srgrimes 2781558Srgrimes/* 2791558Srgrimes * Tell the operator what has to be done; 2801558Srgrimes * we don't actually do it 2811558Srgrimes */ 2821558Srgrimes 2831558Srgrimesstruct fstab * 28492837Simpallocfsent(const struct fstab *fs) 2851558Srgrimes{ 28686473Siedowse struct fstab *new; 2871558Srgrimes 2881558Srgrimes new = (struct fstab *)malloc(sizeof (*fs)); 2891558Srgrimes if (new == NULL || 2901558Srgrimes (new->fs_file = strdup(fs->fs_file)) == NULL || 2911558Srgrimes (new->fs_type = strdup(fs->fs_type)) == NULL || 2921558Srgrimes (new->fs_spec = strdup(fs->fs_spec)) == NULL) 2931558Srgrimes quit("%s\n", strerror(errno)); 2941558Srgrimes new->fs_passno = fs->fs_passno; 2951558Srgrimes new->fs_freq = fs->fs_freq; 2961558Srgrimes return (new); 2971558Srgrimes} 2981558Srgrimes 2991558Srgrimesstruct pfstab { 30071787Sphk SLIST_ENTRY(pfstab) pf_list; 3011558Srgrimes struct fstab *pf_fstab; 3021558Srgrimes}; 3031558Srgrimes 30471787Sphkstatic SLIST_HEAD(, pfstab) table; 3051558Srgrimes 3061558Srgrimesvoid 307113214Smdodddump_getfstab(void) 3081558Srgrimes{ 30986473Siedowse struct fstab *fs; 31086473Siedowse struct pfstab *pf; 3111558Srgrimes 3121558Srgrimes if (setfsent() == 0) { 3131558Srgrimes msg("Can't open %s for dump table information: %s\n", 3141558Srgrimes _PATH_FSTAB, strerror(errno)); 3151558Srgrimes return; 3161558Srgrimes } 3171558Srgrimes while ((fs = getfsent()) != NULL) { 3181558Srgrimes if (strcmp(fs->fs_type, FSTAB_RW) && 3191558Srgrimes strcmp(fs->fs_type, FSTAB_RO) && 3201558Srgrimes strcmp(fs->fs_type, FSTAB_RQ)) 3211558Srgrimes continue; 3221558Srgrimes fs = allocfsent(fs); 3231558Srgrimes if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 3241558Srgrimes quit("%s\n", strerror(errno)); 3251558Srgrimes pf->pf_fstab = fs; 32671787Sphk SLIST_INSERT_HEAD(&table, pf, pf_list); 3271558Srgrimes } 3281558Srgrimes (void) endfsent(); 3291558Srgrimes} 3301558Srgrimes 3311558Srgrimes/* 3321558Srgrimes * Search in the fstab for a file name. 3331558Srgrimes * This file name can be either the special or the path file name. 3341558Srgrimes * 3351558Srgrimes * The file name can omit the leading '/'. 3361558Srgrimes */ 3371558Srgrimesstruct fstab * 33892837Simpfstabsearch(const char *key) 3391558Srgrimes{ 34086473Siedowse struct pfstab *pf; 34186473Siedowse struct fstab *fs; 3421558Srgrimes char *rn; 3431558Srgrimes 34471787Sphk SLIST_FOREACH(pf, &table, pf_list) { 3451558Srgrimes fs = pf->pf_fstab; 3461558Srgrimes if (strcmp(fs->fs_file, key) == 0 || 3471558Srgrimes strcmp(fs->fs_spec, key) == 0) 3481558Srgrimes return (fs); 3491558Srgrimes rn = rawname(fs->fs_spec); 3501558Srgrimes if (rn != NULL && strcmp(rn, key) == 0) 3511558Srgrimes return (fs); 3521558Srgrimes if (key[0] != '/') { 3531558Srgrimes if (*fs->fs_spec == '/' && 3541558Srgrimes strcmp(fs->fs_spec + 1, key) == 0) 3551558Srgrimes return (fs); 3561558Srgrimes if (*fs->fs_file == '/' && 3571558Srgrimes strcmp(fs->fs_file + 1, key) == 0) 3581558Srgrimes return (fs); 3591558Srgrimes } 3601558Srgrimes } 3611558Srgrimes return (NULL); 3621558Srgrimes} 3631558Srgrimes 3641558Srgrimes/* 3651558Srgrimes * Tell the operator what to do 3661558Srgrimes */ 3671558Srgrimesvoid 36892837Simplastdump(int arg) /* w ==> just what to do; W ==> most recent dumps */ 3691558Srgrimes{ 37086473Siedowse int i; 37186473Siedowse struct fstab *dt; 37286473Siedowse struct dumpdates *dtwalk; 3731558Srgrimes char *lastname, *date; 3741558Srgrimes int dumpme; 37548447Sjkh time_t tnow; 37648447Sjkh struct tm *tlast; 3771558Srgrimes 3781558Srgrimes (void) time(&tnow); 379113214Smdodd dump_getfstab(); /* /etc/fstab input */ 3801558Srgrimes initdumptimes(); /* /etc/dumpdates input */ 3811558Srgrimes qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 3821558Srgrimes 3831558Srgrimes if (arg == 'w') 384102231Strhodes (void) printf("Dump these file systems:\n"); 3851558Srgrimes else 386102231Strhodes (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 3871558Srgrimes lastname = "??"; 3881558Srgrimes ITITERATE(i, dtwalk) { 3891558Srgrimes if (strncmp(lastname, dtwalk->dd_name, 3901558Srgrimes sizeof(dtwalk->dd_name)) == 0) 3911558Srgrimes continue; 39248447Sjkh date = (char *)ctime(&dtwalk->dd_ddate); 3931558Srgrimes date[16] = '\0'; /* blast away seconds and year */ 3941558Srgrimes lastname = dtwalk->dd_name; 3951558Srgrimes dt = fstabsearch(dtwalk->dd_name); 39648447Sjkh dumpme = (dt != NULL && dt->fs_freq != 0); 39748447Sjkh if (dumpme) { 39848447Sjkh tlast = localtime(&dtwalk->dd_ddate); 39948447Sjkh dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600) 40048447Sjkh - (tlast->tm_min * 60) - tlast->tm_sec 40148447Sjkh + (dt->fs_freq * 86400)); 40248447Sjkh }; 4031558Srgrimes if (arg != 'w' || dumpme) 4041558Srgrimes (void) printf( 4051558Srgrimes "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 4061558Srgrimes dumpme && (arg != 'w') ? '>' : ' ', 4071558Srgrimes dtwalk->dd_name, 4081558Srgrimes dt ? dt->fs_file : "", 4091558Srgrimes dtwalk->dd_level, 4101558Srgrimes date); 4111558Srgrimes } 4121558Srgrimes} 4131558Srgrimes 4141558Srgrimesint 41592837Simpdatesort(const void *a1, const void *a2) 4161558Srgrimes{ 4171558Srgrimes struct dumpdates *d1 = *(struct dumpdates **)a1; 4181558Srgrimes struct dumpdates *d2 = *(struct dumpdates **)a2; 4191558Srgrimes int diff; 4201558Srgrimes 4211558Srgrimes diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 4221558Srgrimes if (diff == 0) 4231558Srgrimes return (d2->dd_ddate - d1->dd_ddate); 4241558Srgrimes return (diff); 4251558Srgrimes} 426