optr.c revision 92837
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 92837 2002-03-20 22:49:40Z imp $"; 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 471558Srgrimes#include <errno.h> 481558Srgrimes#include <fstab.h> 491558Srgrimes#include <grp.h> 501558Srgrimes#include <stdio.h> 511558Srgrimes#include <stdlib.h> 521558Srgrimes#include <string.h> 531558Srgrimes#include <stdarg.h> 5490742Siedowse#include <signal.h> 551558Srgrimes#include <unistd.h> 561558Srgrimes#include <utmp.h> 571558Srgrimes 581558Srgrimes#include "dump.h" 591558Srgrimes#include "pathnames.h" 601558Srgrimes 6192837Simpvoid alarmcatch(int); 6292837Simpint datesort(const void *, const void *); 631558Srgrimes 641558Srgrimes/* 651558Srgrimes * Query the operator; This previously-fascist piece of code 661558Srgrimes * no longer requires an exact response. 671558Srgrimes * It is intended to protect dump aborting by inquisitive 681558Srgrimes * people banging on the console terminal to see what is 691558Srgrimes * happening which might cause dump to croak, destroying 701558Srgrimes * a large number of hours of work. 711558Srgrimes * 721558Srgrimes * Every 2 minutes we reprint the message, alerting others 731558Srgrimes * that dump needs attention. 741558Srgrimes */ 751558Srgrimesstatic int timeout; 7692837Simpstatic const char *attnmessage; /* attention message */ 771558Srgrimes 781558Srgrimesint 7992837Simpquery(const char *question) 801558Srgrimes{ 811558Srgrimes char replybuffer[64]; 821558Srgrimes int back, errcount; 831558Srgrimes FILE *mytty; 841558Srgrimes 851558Srgrimes if ((mytty = fopen(_PATH_TTY, "r")) == NULL) 861558Srgrimes quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); 871558Srgrimes attnmessage = question; 881558Srgrimes timeout = 0; 8992837Simp alarmcatch(0); 901558Srgrimes back = -1; 911558Srgrimes errcount = 0; 921558Srgrimes do { 931558Srgrimes if (fgets(replybuffer, 63, mytty) == NULL) { 941558Srgrimes clearerr(mytty); 951558Srgrimes if (++errcount > 30) /* XXX ugly */ 961558Srgrimes quit("excessive operator query failures\n"); 971558Srgrimes } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { 981558Srgrimes back = 1; 991558Srgrimes } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { 1001558Srgrimes back = 0; 1011558Srgrimes } else { 1021558Srgrimes (void) fprintf(stderr, 1031558Srgrimes " DUMP: \"Yes\" or \"No\"?\n"); 1041558Srgrimes (void) fprintf(stderr, 1051558Srgrimes " DUMP: %s: (\"yes\" or \"no\") ", question); 1061558Srgrimes } 1071558Srgrimes } while (back < 0); 1081558Srgrimes 1091558Srgrimes /* 1101558Srgrimes * Turn off the alarm, and reset the signal to trap out.. 1111558Srgrimes */ 1121558Srgrimes (void) alarm(0); 1131558Srgrimes if (signal(SIGALRM, sig) == SIG_IGN) 1141558Srgrimes signal(SIGALRM, SIG_IGN); 1151558Srgrimes (void) fclose(mytty); 1161558Srgrimes return(back); 1171558Srgrimes} 1181558Srgrimes 11983083Sruchar lastmsg[BUFSIZ]; 1201558Srgrimes 1211558Srgrimes/* 1221558Srgrimes * Alert the console operator, and enable the alarm clock to 1231558Srgrimes * sleep for 2 minutes in case nobody comes to satisfy dump 1241558Srgrimes */ 1251558Srgrimesvoid 12692837Simpalarmcatch(int sig __unused) 1271558Srgrimes{ 1281558Srgrimes if (notify == 0) { 1291558Srgrimes if (timeout == 0) 1301558Srgrimes (void) fprintf(stderr, 1311558Srgrimes " DUMP: %s: (\"yes\" or \"no\") ", 1321558Srgrimes attnmessage); 1331558Srgrimes else 13471750Sphk msgtail("\a\a"); 1351558Srgrimes } else { 1361558Srgrimes if (timeout) { 1371558Srgrimes msgtail("\n"); 1381558Srgrimes broadcast(""); /* just print last msg */ 1391558Srgrimes } 1401558Srgrimes (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 1411558Srgrimes attnmessage); 1421558Srgrimes } 1431558Srgrimes signal(SIGALRM, alarmcatch); 1441558Srgrimes (void) alarm(120); 1451558Srgrimes timeout = 1; 1461558Srgrimes} 1471558Srgrimes 1481558Srgrimes/* 1491558Srgrimes * Here if an inquisitive operator interrupts the dump program 1501558Srgrimes */ 1511558Srgrimesvoid 15292837Simpinterrupt(int signo __unused) 1531558Srgrimes{ 1541558Srgrimes msg("Interrupt received.\n"); 1551558Srgrimes if (query("Do you want to abort dump?")) 1561558Srgrimes dumpabort(0); 1571558Srgrimes} 1581558Srgrimes 1591558Srgrimes/* 16083083Sru * We now use wall(1) to do the actual broadcasting. 1611558Srgrimes */ 1621558Srgrimesvoid 16392837Simpbroadcast(const char *message) 1641558Srgrimes{ 16583083Sru FILE *fp; 16683083Sru char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3]; 1671558Srgrimes 16883083Sru if (!notify) 1691558Srgrimes return; 1701558Srgrimes 17183083Sru snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT); 17283083Sru if ((fp = popen(buf, "w")) == NULL) 1731558Srgrimes return; 1741558Srgrimes 17583083Sru (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp); 17683083Sru if (lastmsg[0]) 17783083Sru (void) fputs(lastmsg, fp); 17883083Sru if (message[0]) 17983083Sru (void) fputs(message, fp); 1801558Srgrimes 18183083Sru (void) pclose(fp); 1821558Srgrimes} 1831558Srgrimes 1841558Srgrimes/* 18583083Sru * Print out an estimate of the amount of time left to do the dump 1861558Srgrimes */ 1871558Srgrimes 1881558Srgrimestime_t tschedule = 0; 1891558Srgrimes 1901558Srgrimesvoid 19192837Simptimeest(void) 1921558Srgrimes{ 19390743Siedowse double percent; 19485622Sdillon time_t tnow; 19590743Siedowse int deltat, hours, mins; 1961558Srgrimes 19790742Siedowse (void) time(&tnow); 19890743Siedowse deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow + 19990743Siedowse (double)(tnow - tstart_writing) / blockswritten * tapesize; 20090743Siedowse percent = (blockswritten * 100.0) / tapesize; 20190743Siedowse hours = deltat / 3600; 20290743Siedowse mins = (deltat % 3600) / 60; 20390743Siedowse 20490743Siedowse setproctitle("%s: pass %d: %3.2f%% done, finished in %d:%02d", 20590743Siedowse disk, passno, percent, hours, mins); 2061558Srgrimes if (tnow >= tschedule) { 2071558Srgrimes tschedule = tnow + 300; 2081558Srgrimes if (blockswritten < 500) 2098871Srgrimes return; 21090743Siedowse msg("%3.2f%% done, finished in %d:%02d\n", percent, hours, 21190743Siedowse mins); 2121558Srgrimes } 2131558Srgrimes} 2141558Srgrimes 21590742Siedowse/* 21690742Siedowse * Schedule a printout of the estimate in the next call to timeest(). 21790742Siedowse */ 2181558Srgrimesvoid 21992837Simpinfosch(int signal __unused) 22090742Siedowse{ 22190742Siedowse tschedule = 0; 22290742Siedowse} 22390742Siedowse 22490742Siedowsevoid 2251558Srgrimesmsg(const char *fmt, ...) 2261558Srgrimes{ 2271558Srgrimes va_list ap; 2281558Srgrimes 2291558Srgrimes (void) fprintf(stderr," DUMP: "); 2301558Srgrimes#ifdef TDEBUG 2311558Srgrimes (void) fprintf(stderr, "pid=%d ", getpid()); 2321558Srgrimes#endif 2331558Srgrimes va_start(ap, fmt); 2341558Srgrimes (void) vfprintf(stderr, fmt, ap); 2351558Srgrimes (void) fflush(stdout); 2361558Srgrimes (void) fflush(stderr); 23753084Simp (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap); 2381558Srgrimes va_end(ap); 2391558Srgrimes} 2401558Srgrimes 2411558Srgrimesvoid 2421558Srgrimesmsgtail(const char *fmt, ...) 2431558Srgrimes{ 2441558Srgrimes va_list ap; 2451558Srgrimes va_start(ap, fmt); 2461558Srgrimes (void) vfprintf(stderr, fmt, ap); 2471558Srgrimes va_end(ap); 2481558Srgrimes} 2491558Srgrimes 2501558Srgrimesvoid 2511558Srgrimesquit(const char *fmt, ...) 2521558Srgrimes{ 2531558Srgrimes va_list ap; 2541558Srgrimes 2551558Srgrimes (void) fprintf(stderr," DUMP: "); 2561558Srgrimes#ifdef TDEBUG 2571558Srgrimes (void) fprintf(stderr, "pid=%d ", getpid()); 2581558Srgrimes#endif 2591558Srgrimes va_start(ap, fmt); 2601558Srgrimes (void) vfprintf(stderr, fmt, ap); 2611558Srgrimes va_end(ap); 2621558Srgrimes (void) fflush(stdout); 2631558Srgrimes (void) fflush(stderr); 2641558Srgrimes dumpabort(0); 2651558Srgrimes} 2661558Srgrimes 2671558Srgrimes/* 2681558Srgrimes * Tell the operator what has to be done; 2691558Srgrimes * we don't actually do it 2701558Srgrimes */ 2711558Srgrimes 2721558Srgrimesstruct fstab * 27392837Simpallocfsent(const struct fstab *fs) 2741558Srgrimes{ 27586473Siedowse struct fstab *new; 2761558Srgrimes 2771558Srgrimes new = (struct fstab *)malloc(sizeof (*fs)); 2781558Srgrimes if (new == NULL || 2791558Srgrimes (new->fs_file = strdup(fs->fs_file)) == NULL || 2801558Srgrimes (new->fs_type = strdup(fs->fs_type)) == NULL || 2811558Srgrimes (new->fs_spec = strdup(fs->fs_spec)) == NULL) 2821558Srgrimes quit("%s\n", strerror(errno)); 2831558Srgrimes new->fs_passno = fs->fs_passno; 2841558Srgrimes new->fs_freq = fs->fs_freq; 2851558Srgrimes return (new); 2861558Srgrimes} 2871558Srgrimes 2881558Srgrimesstruct pfstab { 28971787Sphk SLIST_ENTRY(pfstab) pf_list; 2901558Srgrimes struct fstab *pf_fstab; 2911558Srgrimes}; 2921558Srgrimes 29371787Sphkstatic SLIST_HEAD(, pfstab) table; 2941558Srgrimes 2951558Srgrimesvoid 29692837Simpgetfstab(void) 2971558Srgrimes{ 29886473Siedowse struct fstab *fs; 29986473Siedowse struct pfstab *pf; 3001558Srgrimes 3011558Srgrimes if (setfsent() == 0) { 3021558Srgrimes msg("Can't open %s for dump table information: %s\n", 3031558Srgrimes _PATH_FSTAB, strerror(errno)); 3041558Srgrimes return; 3051558Srgrimes } 3061558Srgrimes while ((fs = getfsent()) != NULL) { 3071558Srgrimes if (strcmp(fs->fs_type, FSTAB_RW) && 3081558Srgrimes strcmp(fs->fs_type, FSTAB_RO) && 3091558Srgrimes strcmp(fs->fs_type, FSTAB_RQ)) 3101558Srgrimes continue; 3111558Srgrimes fs = allocfsent(fs); 3121558Srgrimes if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 3131558Srgrimes quit("%s\n", strerror(errno)); 3141558Srgrimes pf->pf_fstab = fs; 31571787Sphk SLIST_INSERT_HEAD(&table, pf, pf_list); 3161558Srgrimes } 3171558Srgrimes (void) endfsent(); 3181558Srgrimes} 3191558Srgrimes 3201558Srgrimes/* 3211558Srgrimes * Search in the fstab for a file name. 3221558Srgrimes * This file name can be either the special or the path file name. 3231558Srgrimes * 3241558Srgrimes * The file name can omit the leading '/'. 3251558Srgrimes */ 3261558Srgrimesstruct fstab * 32792837Simpfstabsearch(const char *key) 3281558Srgrimes{ 32986473Siedowse struct pfstab *pf; 33086473Siedowse struct fstab *fs; 3311558Srgrimes char *rn; 3321558Srgrimes 33371787Sphk SLIST_FOREACH(pf, &table, pf_list) { 3341558Srgrimes fs = pf->pf_fstab; 3351558Srgrimes if (strcmp(fs->fs_file, key) == 0 || 3361558Srgrimes strcmp(fs->fs_spec, key) == 0) 3371558Srgrimes return (fs); 3381558Srgrimes rn = rawname(fs->fs_spec); 3391558Srgrimes if (rn != NULL && strcmp(rn, key) == 0) 3401558Srgrimes return (fs); 3411558Srgrimes if (key[0] != '/') { 3421558Srgrimes if (*fs->fs_spec == '/' && 3431558Srgrimes strcmp(fs->fs_spec + 1, key) == 0) 3441558Srgrimes return (fs); 3451558Srgrimes if (*fs->fs_file == '/' && 3461558Srgrimes strcmp(fs->fs_file + 1, key) == 0) 3471558Srgrimes return (fs); 3481558Srgrimes } 3491558Srgrimes } 3501558Srgrimes return (NULL); 3511558Srgrimes} 3521558Srgrimes 3531558Srgrimes/* 3541558Srgrimes * Tell the operator what to do 3551558Srgrimes */ 3561558Srgrimesvoid 35792837Simplastdump(int arg) /* w ==> just what to do; W ==> most recent dumps */ 3581558Srgrimes{ 35986473Siedowse int i; 36086473Siedowse struct fstab *dt; 36186473Siedowse struct dumpdates *dtwalk; 3621558Srgrimes char *lastname, *date; 3631558Srgrimes int dumpme; 36448447Sjkh time_t tnow; 36548447Sjkh struct tm *tlast; 3661558Srgrimes 3671558Srgrimes (void) time(&tnow); 3681558Srgrimes getfstab(); /* /etc/fstab input */ 3691558Srgrimes initdumptimes(); /* /etc/dumpdates input */ 3701558Srgrimes qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 3711558Srgrimes 3721558Srgrimes if (arg == 'w') 3731558Srgrimes (void) printf("Dump these file systems:\n"); 3741558Srgrimes else 3751558Srgrimes (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 3761558Srgrimes lastname = "??"; 3771558Srgrimes ITITERATE(i, dtwalk) { 3781558Srgrimes if (strncmp(lastname, dtwalk->dd_name, 3791558Srgrimes sizeof(dtwalk->dd_name)) == 0) 3801558Srgrimes continue; 38148447Sjkh date = (char *)ctime(&dtwalk->dd_ddate); 3821558Srgrimes date[16] = '\0'; /* blast away seconds and year */ 3831558Srgrimes lastname = dtwalk->dd_name; 3841558Srgrimes dt = fstabsearch(dtwalk->dd_name); 38548447Sjkh dumpme = (dt != NULL && dt->fs_freq != 0); 38648447Sjkh if (dumpme) { 38748447Sjkh tlast = localtime(&dtwalk->dd_ddate); 38848447Sjkh dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600) 38948447Sjkh - (tlast->tm_min * 60) - tlast->tm_sec 39048447Sjkh + (dt->fs_freq * 86400)); 39148447Sjkh }; 3921558Srgrimes if (arg != 'w' || dumpme) 3931558Srgrimes (void) printf( 3941558Srgrimes "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 3951558Srgrimes dumpme && (arg != 'w') ? '>' : ' ', 3961558Srgrimes dtwalk->dd_name, 3971558Srgrimes dt ? dt->fs_file : "", 3981558Srgrimes dtwalk->dd_level, 3991558Srgrimes date); 4001558Srgrimes } 4011558Srgrimes} 4021558Srgrimes 4031558Srgrimesint 40492837Simpdatesort(const void *a1, const void *a2) 4051558Srgrimes{ 4061558Srgrimes struct dumpdates *d1 = *(struct dumpdates **)a1; 4071558Srgrimes struct dumpdates *d2 = *(struct dumpdates **)a2; 4081558Srgrimes int diff; 4091558Srgrimes 4101558Srgrimes diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 4111558Srgrimes if (diff == 0) 4121558Srgrimes return (d2->dd_ddate - d1->dd_ddate); 4131558Srgrimes return (diff); 4141558Srgrimes} 415