optr.c revision 90743
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 90743 2002-02-16 21:05:16Z iedowse $"; 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 611558Srgrimesvoid alarmcatch __P((/* int, int */)); 621558Srgrimesint datesort __P((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; 761558Srgrimesstatic char *attnmessage; /* attention message */ 771558Srgrimes 781558Srgrimesint 791558Srgrimesquery(question) 801558Srgrimes char *question; 811558Srgrimes{ 821558Srgrimes char replybuffer[64]; 831558Srgrimes int back, errcount; 841558Srgrimes FILE *mytty; 851558Srgrimes 861558Srgrimes if ((mytty = fopen(_PATH_TTY, "r")) == NULL) 871558Srgrimes quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); 881558Srgrimes attnmessage = question; 891558Srgrimes timeout = 0; 901558Srgrimes alarmcatch(); 911558Srgrimes back = -1; 921558Srgrimes errcount = 0; 931558Srgrimes do { 941558Srgrimes if (fgets(replybuffer, 63, mytty) == NULL) { 951558Srgrimes clearerr(mytty); 961558Srgrimes if (++errcount > 30) /* XXX ugly */ 971558Srgrimes quit("excessive operator query failures\n"); 981558Srgrimes } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { 991558Srgrimes back = 1; 1001558Srgrimes } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { 1011558Srgrimes back = 0; 1021558Srgrimes } else { 1031558Srgrimes (void) fprintf(stderr, 1041558Srgrimes " DUMP: \"Yes\" or \"No\"?\n"); 1051558Srgrimes (void) fprintf(stderr, 1061558Srgrimes " DUMP: %s: (\"yes\" or \"no\") ", question); 1071558Srgrimes } 1081558Srgrimes } while (back < 0); 1091558Srgrimes 1101558Srgrimes /* 1111558Srgrimes * Turn off the alarm, and reset the signal to trap out.. 1121558Srgrimes */ 1131558Srgrimes (void) alarm(0); 1141558Srgrimes if (signal(SIGALRM, sig) == SIG_IGN) 1151558Srgrimes signal(SIGALRM, SIG_IGN); 1161558Srgrimes (void) fclose(mytty); 1171558Srgrimes return(back); 1181558Srgrimes} 1191558Srgrimes 12083083Sruchar lastmsg[BUFSIZ]; 1211558Srgrimes 1221558Srgrimes/* 1231558Srgrimes * Alert the console operator, and enable the alarm clock to 1241558Srgrimes * sleep for 2 minutes in case nobody comes to satisfy dump 1251558Srgrimes */ 1261558Srgrimesvoid 1271558Srgrimesalarmcatch() 1281558Srgrimes{ 1291558Srgrimes if (notify == 0) { 1301558Srgrimes if (timeout == 0) 1311558Srgrimes (void) fprintf(stderr, 1321558Srgrimes " DUMP: %s: (\"yes\" or \"no\") ", 1331558Srgrimes attnmessage); 1341558Srgrimes else 13571750Sphk msgtail("\a\a"); 1361558Srgrimes } else { 1371558Srgrimes if (timeout) { 1381558Srgrimes msgtail("\n"); 1391558Srgrimes broadcast(""); /* just print last msg */ 1401558Srgrimes } 1411558Srgrimes (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 1421558Srgrimes attnmessage); 1431558Srgrimes } 1441558Srgrimes signal(SIGALRM, alarmcatch); 1451558Srgrimes (void) alarm(120); 1461558Srgrimes timeout = 1; 1471558Srgrimes} 1481558Srgrimes 1491558Srgrimes/* 1501558Srgrimes * Here if an inquisitive operator interrupts the dump program 1511558Srgrimes */ 1521558Srgrimesvoid 1531558Srgrimesinterrupt(signo) 1541558Srgrimes int signo; 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 1651558Srgrimesbroadcast(message) 1661558Srgrimes char *message; 1671558Srgrimes{ 16883083Sru FILE *fp; 16983083Sru char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3]; 1701558Srgrimes 17183083Sru if (!notify) 1721558Srgrimes return; 1731558Srgrimes 17483083Sru snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT); 17583083Sru if ((fp = popen(buf, "w")) == NULL) 1761558Srgrimes return; 1771558Srgrimes 17883083Sru (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp); 17983083Sru if (lastmsg[0]) 18083083Sru (void) fputs(lastmsg, fp); 18183083Sru if (message[0]) 18283083Sru (void) fputs(message, fp); 1831558Srgrimes 18483083Sru (void) pclose(fp); 1851558Srgrimes} 1861558Srgrimes 1871558Srgrimes/* 18883083Sru * Print out an estimate of the amount of time left to do the dump 1891558Srgrimes */ 1901558Srgrimes 1911558Srgrimestime_t tschedule = 0; 1921558Srgrimes 1931558Srgrimesvoid 1941558Srgrimestimeest() 1951558Srgrimes{ 19690743Siedowse double percent; 19785622Sdillon time_t tnow; 19890743Siedowse int deltat, hours, mins; 1991558Srgrimes 20090742Siedowse (void) time(&tnow); 20190743Siedowse deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow + 20290743Siedowse (double)(tnow - tstart_writing) / blockswritten * tapesize; 20390743Siedowse percent = (blockswritten * 100.0) / tapesize; 20490743Siedowse hours = deltat / 3600; 20590743Siedowse mins = (deltat % 3600) / 60; 20690743Siedowse 20790743Siedowse setproctitle("%s: pass %d: %3.2f%% done, finished in %d:%02d", 20890743Siedowse disk, passno, percent, hours, mins); 2091558Srgrimes if (tnow >= tschedule) { 2101558Srgrimes tschedule = tnow + 300; 2111558Srgrimes if (blockswritten < 500) 2128871Srgrimes return; 21390743Siedowse msg("%3.2f%% done, finished in %d:%02d\n", percent, hours, 21490743Siedowse mins); 2151558Srgrimes } 2161558Srgrimes} 2171558Srgrimes 21890742Siedowse/* 21990742Siedowse * Schedule a printout of the estimate in the next call to timeest(). 22090742Siedowse */ 2211558Srgrimesvoid 22290742Siedowseinfosch(signal) 22390742Siedowse int signal; 22490742Siedowse{ 22590742Siedowse tschedule = 0; 22690742Siedowse} 22790742Siedowse 22890742Siedowsevoid 2291558Srgrimes#if __STDC__ 2301558Srgrimesmsg(const char *fmt, ...) 2311558Srgrimes#else 2321558Srgrimesmsg(fmt, va_alist) 2331558Srgrimes char *fmt; 2341558Srgrimes va_dcl 2351558Srgrimes#endif 2361558Srgrimes{ 2371558Srgrimes va_list ap; 2381558Srgrimes 2391558Srgrimes (void) fprintf(stderr," DUMP: "); 2401558Srgrimes#ifdef TDEBUG 2411558Srgrimes (void) fprintf(stderr, "pid=%d ", getpid()); 2421558Srgrimes#endif 2431558Srgrimes#if __STDC__ 2441558Srgrimes va_start(ap, fmt); 2451558Srgrimes#else 2461558Srgrimes va_start(ap); 2471558Srgrimes#endif 2481558Srgrimes (void) vfprintf(stderr, fmt, ap); 2491558Srgrimes (void) fflush(stdout); 2501558Srgrimes (void) fflush(stderr); 25153084Simp (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap); 2521558Srgrimes va_end(ap); 2531558Srgrimes} 2541558Srgrimes 2551558Srgrimesvoid 2561558Srgrimes#if __STDC__ 2571558Srgrimesmsgtail(const char *fmt, ...) 2581558Srgrimes#else 2591558Srgrimesmsgtail(fmt, va_alist) 2601558Srgrimes char *fmt; 2611558Srgrimes va_dcl 2621558Srgrimes#endif 2631558Srgrimes{ 2641558Srgrimes va_list ap; 2651558Srgrimes#if __STDC__ 2661558Srgrimes va_start(ap, fmt); 2671558Srgrimes#else 2681558Srgrimes va_start(ap); 2691558Srgrimes#endif 2701558Srgrimes (void) vfprintf(stderr, fmt, ap); 2711558Srgrimes va_end(ap); 2721558Srgrimes} 2731558Srgrimes 2741558Srgrimesvoid 2751558Srgrimes#if __STDC__ 2761558Srgrimesquit(const char *fmt, ...) 2771558Srgrimes#else 2781558Srgrimesquit(fmt, va_alist) 2791558Srgrimes char *fmt; 2801558Srgrimes va_dcl 2811558Srgrimes#endif 2821558Srgrimes{ 2831558Srgrimes va_list ap; 2841558Srgrimes 2851558Srgrimes (void) fprintf(stderr," DUMP: "); 2861558Srgrimes#ifdef TDEBUG 2871558Srgrimes (void) fprintf(stderr, "pid=%d ", getpid()); 2881558Srgrimes#endif 2891558Srgrimes#if __STDC__ 2901558Srgrimes va_start(ap, fmt); 2911558Srgrimes#else 2921558Srgrimes va_start(ap); 2931558Srgrimes#endif 2941558Srgrimes (void) vfprintf(stderr, fmt, ap); 2951558Srgrimes va_end(ap); 2961558Srgrimes (void) fflush(stdout); 2971558Srgrimes (void) fflush(stderr); 2981558Srgrimes dumpabort(0); 2991558Srgrimes} 3001558Srgrimes 3011558Srgrimes/* 3021558Srgrimes * Tell the operator what has to be done; 3031558Srgrimes * we don't actually do it 3041558Srgrimes */ 3051558Srgrimes 3061558Srgrimesstruct fstab * 3071558Srgrimesallocfsent(fs) 30886473Siedowse struct fstab *fs; 3091558Srgrimes{ 31086473Siedowse struct fstab *new; 3111558Srgrimes 3121558Srgrimes new = (struct fstab *)malloc(sizeof (*fs)); 3131558Srgrimes if (new == NULL || 3141558Srgrimes (new->fs_file = strdup(fs->fs_file)) == NULL || 3151558Srgrimes (new->fs_type = strdup(fs->fs_type)) == NULL || 3161558Srgrimes (new->fs_spec = strdup(fs->fs_spec)) == NULL) 3171558Srgrimes quit("%s\n", strerror(errno)); 3181558Srgrimes new->fs_passno = fs->fs_passno; 3191558Srgrimes new->fs_freq = fs->fs_freq; 3201558Srgrimes return (new); 3211558Srgrimes} 3221558Srgrimes 3231558Srgrimesstruct pfstab { 32471787Sphk SLIST_ENTRY(pfstab) pf_list; 3251558Srgrimes struct fstab *pf_fstab; 3261558Srgrimes}; 3271558Srgrimes 32871787Sphkstatic SLIST_HEAD(, pfstab) table; 3291558Srgrimes 3301558Srgrimesvoid 3311558Srgrimesgetfstab() 3321558Srgrimes{ 33386473Siedowse struct fstab *fs; 33486473Siedowse struct pfstab *pf; 3351558Srgrimes 3361558Srgrimes if (setfsent() == 0) { 3371558Srgrimes msg("Can't open %s for dump table information: %s\n", 3381558Srgrimes _PATH_FSTAB, strerror(errno)); 3391558Srgrimes return; 3401558Srgrimes } 3411558Srgrimes while ((fs = getfsent()) != NULL) { 3421558Srgrimes if (strcmp(fs->fs_type, FSTAB_RW) && 3431558Srgrimes strcmp(fs->fs_type, FSTAB_RO) && 3441558Srgrimes strcmp(fs->fs_type, FSTAB_RQ)) 3451558Srgrimes continue; 3461558Srgrimes fs = allocfsent(fs); 3471558Srgrimes if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 3481558Srgrimes quit("%s\n", strerror(errno)); 3491558Srgrimes pf->pf_fstab = fs; 35071787Sphk SLIST_INSERT_HEAD(&table, pf, pf_list); 3511558Srgrimes } 3521558Srgrimes (void) endfsent(); 3531558Srgrimes} 3541558Srgrimes 3551558Srgrimes/* 3561558Srgrimes * Search in the fstab for a file name. 3571558Srgrimes * This file name can be either the special or the path file name. 3581558Srgrimes * 3591558Srgrimes * The file name can omit the leading '/'. 3601558Srgrimes */ 3611558Srgrimesstruct fstab * 3621558Srgrimesfstabsearch(key) 3631558Srgrimes char *key; 3641558Srgrimes{ 36586473Siedowse struct pfstab *pf; 36686473Siedowse struct fstab *fs; 3671558Srgrimes char *rn; 3681558Srgrimes 36971787Sphk SLIST_FOREACH(pf, &table, pf_list) { 3701558Srgrimes fs = pf->pf_fstab; 3711558Srgrimes if (strcmp(fs->fs_file, key) == 0 || 3721558Srgrimes strcmp(fs->fs_spec, key) == 0) 3731558Srgrimes return (fs); 3741558Srgrimes rn = rawname(fs->fs_spec); 3751558Srgrimes if (rn != NULL && strcmp(rn, key) == 0) 3761558Srgrimes return (fs); 3771558Srgrimes if (key[0] != '/') { 3781558Srgrimes if (*fs->fs_spec == '/' && 3791558Srgrimes strcmp(fs->fs_spec + 1, key) == 0) 3801558Srgrimes return (fs); 3811558Srgrimes if (*fs->fs_file == '/' && 3821558Srgrimes strcmp(fs->fs_file + 1, key) == 0) 3831558Srgrimes return (fs); 3841558Srgrimes } 3851558Srgrimes } 3861558Srgrimes return (NULL); 3871558Srgrimes} 3881558Srgrimes 3891558Srgrimes/* 3901558Srgrimes * Tell the operator what to do 3911558Srgrimes */ 3921558Srgrimesvoid 3931558Srgrimeslastdump(arg) 3941558Srgrimes char arg; /* w ==> just what to do; W ==> most recent dumps */ 3951558Srgrimes{ 39686473Siedowse int i; 39786473Siedowse struct fstab *dt; 39886473Siedowse struct dumpdates *dtwalk; 3991558Srgrimes char *lastname, *date; 4001558Srgrimes int dumpme; 40148447Sjkh time_t tnow; 40248447Sjkh struct tm *tlast; 4031558Srgrimes 4041558Srgrimes (void) time(&tnow); 4051558Srgrimes getfstab(); /* /etc/fstab input */ 4061558Srgrimes initdumptimes(); /* /etc/dumpdates input */ 4071558Srgrimes qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 4081558Srgrimes 4091558Srgrimes if (arg == 'w') 4101558Srgrimes (void) printf("Dump these file systems:\n"); 4111558Srgrimes else 4121558Srgrimes (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 4131558Srgrimes lastname = "??"; 4141558Srgrimes ITITERATE(i, dtwalk) { 4151558Srgrimes if (strncmp(lastname, dtwalk->dd_name, 4161558Srgrimes sizeof(dtwalk->dd_name)) == 0) 4171558Srgrimes continue; 41848447Sjkh date = (char *)ctime(&dtwalk->dd_ddate); 4191558Srgrimes date[16] = '\0'; /* blast away seconds and year */ 4201558Srgrimes lastname = dtwalk->dd_name; 4211558Srgrimes dt = fstabsearch(dtwalk->dd_name); 42248447Sjkh dumpme = (dt != NULL && dt->fs_freq != 0); 42348447Sjkh if (dumpme) { 42448447Sjkh tlast = localtime(&dtwalk->dd_ddate); 42548447Sjkh dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600) 42648447Sjkh - (tlast->tm_min * 60) - tlast->tm_sec 42748447Sjkh + (dt->fs_freq * 86400)); 42848447Sjkh }; 4291558Srgrimes if (arg != 'w' || dumpme) 4301558Srgrimes (void) printf( 4311558Srgrimes "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 4321558Srgrimes dumpme && (arg != 'w') ? '>' : ' ', 4331558Srgrimes dtwalk->dd_name, 4341558Srgrimes dt ? dt->fs_file : "", 4351558Srgrimes dtwalk->dd_level, 4361558Srgrimes date); 4371558Srgrimes } 4381558Srgrimes} 4391558Srgrimes 4401558Srgrimesint 4411558Srgrimesdatesort(a1, a2) 4421558Srgrimes const void *a1, *a2; 4431558Srgrimes{ 4441558Srgrimes struct dumpdates *d1 = *(struct dumpdates **)a1; 4451558Srgrimes struct dumpdates *d2 = *(struct dumpdates **)a2; 4461558Srgrimes int diff; 4471558Srgrimes 4481558Srgrimes diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 4491558Srgrimes if (diff == 0) 4501558Srgrimes return (d2->dd_ddate - d1->dd_ddate); 4511558Srgrimes return (diff); 4521558Srgrimes} 453