optr.c revision 102231
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 102231 2002-08-21 18:11:48Z trhodes $";
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>
521558Srgrimes#include <stdio.h>
531558Srgrimes#include <stdlib.h>
541558Srgrimes#include <string.h>
551558Srgrimes#include <stdarg.h>
5690742Siedowse#include <signal.h>
571558Srgrimes#include <unistd.h>
581558Srgrimes
591558Srgrimes#include "dump.h"
601558Srgrimes#include "pathnames.h"
611558Srgrimes
6292837Simpvoid	alarmcatch(int);
6392837Simpint	datesort(const void *, const void *);
641558Srgrimes
651558Srgrimes/*
661558Srgrimes *	Query the operator; This previously-fascist piece of code
671558Srgrimes *	no longer requires an exact response.
681558Srgrimes *	It is intended to protect dump aborting by inquisitive
691558Srgrimes *	people banging on the console terminal to see what is
701558Srgrimes *	happening which might cause dump to croak, destroying
711558Srgrimes *	a large number of hours of work.
721558Srgrimes *
731558Srgrimes *	Every 2 minutes we reprint the message, alerting others
741558Srgrimes *	that dump needs attention.
751558Srgrimes */
761558Srgrimesstatic	int timeout;
7792837Simpstatic	const char *attnmessage;		/* attention message */
781558Srgrimes
791558Srgrimesint
8092837Simpquery(const 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;
9092837Simp	alarmcatch(0);
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
12792837Simpalarmcatch(int sig __unused)
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
15392837Simpinterrupt(int signo __unused)
1541558Srgrimes{
1551558Srgrimes	msg("Interrupt received.\n");
1561558Srgrimes	if (query("Do you want to abort dump?"))
1571558Srgrimes		dumpabort(0);
1581558Srgrimes}
1591558Srgrimes
1601558Srgrimes/*
16183083Sru *	We now use wall(1) to do the actual broadcasting.
1621558Srgrimes */
1631558Srgrimesvoid
16492837Simpbroadcast(const char *message)
1651558Srgrimes{
16683083Sru	FILE *fp;
16783083Sru	char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
1681558Srgrimes
16983083Sru	if (!notify)
1701558Srgrimes		return;
1711558Srgrimes
17283083Sru	snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
17383083Sru	if ((fp = popen(buf, "w")) == NULL)
1741558Srgrimes		return;
1751558Srgrimes
17683083Sru	(void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
17783083Sru	if (lastmsg[0])
17883083Sru		(void) fputs(lastmsg, fp);
17983083Sru	if (message[0])
18083083Sru		(void) fputs(message, fp);
1811558Srgrimes
18283083Sru	(void) pclose(fp);
1831558Srgrimes}
1841558Srgrimes
1851558Srgrimes/*
18683083Sru *	Print out an estimate of the amount of time left to do the dump
1871558Srgrimes */
1881558Srgrimes
1891558Srgrimestime_t	tschedule = 0;
1901558Srgrimes
1911558Srgrimesvoid
19292837Simptimeest(void)
1931558Srgrimes{
19490743Siedowse	double percent;
19585622Sdillon	time_t	tnow;
19690743Siedowse	int deltat, hours, mins;
1971558Srgrimes
19890742Siedowse	(void) time(&tnow);
19990743Siedowse	deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
20090743Siedowse	    (double)(tnow - tstart_writing) / blockswritten * tapesize;
20190743Siedowse	percent = (blockswritten * 100.0) / tapesize;
20290743Siedowse	hours = deltat / 3600;
20390743Siedowse	mins = (deltat % 3600) / 60;
20490743Siedowse
20590743Siedowse	setproctitle("%s: pass %d: %3.2f%% done, finished in %d:%02d",
20690743Siedowse	    disk, passno, percent, hours, mins);
2071558Srgrimes	if (tnow >= tschedule) {
2081558Srgrimes		tschedule = tnow + 300;
2091558Srgrimes		if (blockswritten < 500)
2108871Srgrimes			return;
21190743Siedowse		msg("%3.2f%% done, finished in %d:%02d\n", percent, hours,
21290743Siedowse		    mins);
2131558Srgrimes	}
2141558Srgrimes}
2151558Srgrimes
21690742Siedowse/*
21790742Siedowse * Schedule a printout of the estimate in the next call to timeest().
21890742Siedowse */
2191558Srgrimesvoid
22092837Simpinfosch(int signal __unused)
22190742Siedowse{
22290742Siedowse	tschedule = 0;
22390742Siedowse}
22490742Siedowse
22590742Siedowsevoid
2261558Srgrimesmsg(const char *fmt, ...)
2271558Srgrimes{
2281558Srgrimes	va_list ap;
2291558Srgrimes
2301558Srgrimes	(void) fprintf(stderr,"  DUMP: ");
2311558Srgrimes#ifdef TDEBUG
2321558Srgrimes	(void) fprintf(stderr, "pid=%d ", getpid());
2331558Srgrimes#endif
2341558Srgrimes	va_start(ap, fmt);
2351558Srgrimes	(void) vfprintf(stderr, fmt, ap);
2361558Srgrimes	(void) fflush(stdout);
2371558Srgrimes	(void) fflush(stderr);
23853084Simp	(void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
2391558Srgrimes	va_end(ap);
2401558Srgrimes}
2411558Srgrimes
2421558Srgrimesvoid
2431558Srgrimesmsgtail(const char *fmt, ...)
2441558Srgrimes{
2451558Srgrimes	va_list ap;
2461558Srgrimes	va_start(ap, fmt);
2471558Srgrimes	(void) vfprintf(stderr, fmt, ap);
2481558Srgrimes	va_end(ap);
2491558Srgrimes}
2501558Srgrimes
2511558Srgrimesvoid
2521558Srgrimesquit(const char *fmt, ...)
2531558Srgrimes{
2541558Srgrimes	va_list ap;
2551558Srgrimes
2561558Srgrimes	(void) fprintf(stderr,"  DUMP: ");
2571558Srgrimes#ifdef TDEBUG
2581558Srgrimes	(void) fprintf(stderr, "pid=%d ", getpid());
2591558Srgrimes#endif
2601558Srgrimes	va_start(ap, fmt);
2611558Srgrimes	(void) vfprintf(stderr, fmt, ap);
2621558Srgrimes	va_end(ap);
2631558Srgrimes	(void) fflush(stdout);
2641558Srgrimes	(void) fflush(stderr);
2651558Srgrimes	dumpabort(0);
2661558Srgrimes}
2671558Srgrimes
2681558Srgrimes/*
2691558Srgrimes *	Tell the operator what has to be done;
2701558Srgrimes *	we don't actually do it
2711558Srgrimes */
2721558Srgrimes
2731558Srgrimesstruct fstab *
27492837Simpallocfsent(const struct fstab *fs)
2751558Srgrimes{
27686473Siedowse	struct fstab *new;
2771558Srgrimes
2781558Srgrimes	new = (struct fstab *)malloc(sizeof (*fs));
2791558Srgrimes	if (new == NULL ||
2801558Srgrimes	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
2811558Srgrimes	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
2821558Srgrimes	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
2831558Srgrimes		quit("%s\n", strerror(errno));
2841558Srgrimes	new->fs_passno = fs->fs_passno;
2851558Srgrimes	new->fs_freq = fs->fs_freq;
2861558Srgrimes	return (new);
2871558Srgrimes}
2881558Srgrimes
2891558Srgrimesstruct	pfstab {
29071787Sphk	SLIST_ENTRY(pfstab) pf_list;
2911558Srgrimes	struct	fstab *pf_fstab;
2921558Srgrimes};
2931558Srgrimes
29471787Sphkstatic	SLIST_HEAD(, pfstab) table;
2951558Srgrimes
2961558Srgrimesvoid
29792837Simpgetfstab(void)
2981558Srgrimes{
29986473Siedowse	struct fstab *fs;
30086473Siedowse	struct pfstab *pf;
3011558Srgrimes
3021558Srgrimes	if (setfsent() == 0) {
3031558Srgrimes		msg("Can't open %s for dump table information: %s\n",
3041558Srgrimes		    _PATH_FSTAB, strerror(errno));
3051558Srgrimes		return;
3061558Srgrimes	}
3071558Srgrimes	while ((fs = getfsent()) != NULL) {
3081558Srgrimes		if (strcmp(fs->fs_type, FSTAB_RW) &&
3091558Srgrimes		    strcmp(fs->fs_type, FSTAB_RO) &&
3101558Srgrimes		    strcmp(fs->fs_type, FSTAB_RQ))
3111558Srgrimes			continue;
3121558Srgrimes		fs = allocfsent(fs);
3131558Srgrimes		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
3141558Srgrimes			quit("%s\n", strerror(errno));
3151558Srgrimes		pf->pf_fstab = fs;
31671787Sphk		SLIST_INSERT_HEAD(&table, pf, pf_list);
3171558Srgrimes	}
3181558Srgrimes	(void) endfsent();
3191558Srgrimes}
3201558Srgrimes
3211558Srgrimes/*
3221558Srgrimes * Search in the fstab for a file name.
3231558Srgrimes * This file name can be either the special or the path file name.
3241558Srgrimes *
3251558Srgrimes * The file name can omit the leading '/'.
3261558Srgrimes */
3271558Srgrimesstruct fstab *
32892837Simpfstabsearch(const char *key)
3291558Srgrimes{
33086473Siedowse	struct pfstab *pf;
33186473Siedowse	struct fstab *fs;
3321558Srgrimes	char *rn;
3331558Srgrimes
33471787Sphk	SLIST_FOREACH(pf, &table, pf_list) {
3351558Srgrimes		fs = pf->pf_fstab;
3361558Srgrimes		if (strcmp(fs->fs_file, key) == 0 ||
3371558Srgrimes		    strcmp(fs->fs_spec, key) == 0)
3381558Srgrimes			return (fs);
3391558Srgrimes		rn = rawname(fs->fs_spec);
3401558Srgrimes		if (rn != NULL && strcmp(rn, key) == 0)
3411558Srgrimes			return (fs);
3421558Srgrimes		if (key[0] != '/') {
3431558Srgrimes			if (*fs->fs_spec == '/' &&
3441558Srgrimes			    strcmp(fs->fs_spec + 1, key) == 0)
3451558Srgrimes				return (fs);
3461558Srgrimes			if (*fs->fs_file == '/' &&
3471558Srgrimes			    strcmp(fs->fs_file + 1, key) == 0)
3481558Srgrimes				return (fs);
3491558Srgrimes		}
3501558Srgrimes	}
3511558Srgrimes	return (NULL);
3521558Srgrimes}
3531558Srgrimes
3541558Srgrimes/*
3551558Srgrimes *	Tell the operator what to do
3561558Srgrimes */
3571558Srgrimesvoid
35892837Simplastdump(int arg)	/* w ==> just what to do; W ==> most recent dumps */
3591558Srgrimes{
36086473Siedowse	int i;
36186473Siedowse	struct fstab *dt;
36286473Siedowse	struct dumpdates *dtwalk;
3631558Srgrimes	char *lastname, *date;
3641558Srgrimes	int dumpme;
36548447Sjkh	time_t tnow;
36648447Sjkh	struct tm *tlast;
3671558Srgrimes
3681558Srgrimes	(void) time(&tnow);
3691558Srgrimes	getfstab();		/* /etc/fstab input */
3701558Srgrimes	initdumptimes();	/* /etc/dumpdates input */
3711558Srgrimes	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
3721558Srgrimes
3731558Srgrimes	if (arg == 'w')
374102231Strhodes		(void) printf("Dump these file systems:\n");
3751558Srgrimes	else
376102231Strhodes		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
3771558Srgrimes	lastname = "??";
3781558Srgrimes	ITITERATE(i, dtwalk) {
3791558Srgrimes		if (strncmp(lastname, dtwalk->dd_name,
3801558Srgrimes		    sizeof(dtwalk->dd_name)) == 0)
3811558Srgrimes			continue;
38248447Sjkh		date = (char *)ctime(&dtwalk->dd_ddate);
3831558Srgrimes		date[16] = '\0';	/* blast away seconds and year */
3841558Srgrimes		lastname = dtwalk->dd_name;
3851558Srgrimes		dt = fstabsearch(dtwalk->dd_name);
38648447Sjkh		dumpme = (dt != NULL && dt->fs_freq != 0);
38748447Sjkh		if (dumpme) {
38848447Sjkh		    tlast = localtime(&dtwalk->dd_ddate);
38948447Sjkh		    dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
39048447Sjkh				     - (tlast->tm_min * 60) - tlast->tm_sec
39148447Sjkh				     + (dt->fs_freq * 86400));
39248447Sjkh		};
3931558Srgrimes		if (arg != 'w' || dumpme)
3941558Srgrimes			(void) printf(
3951558Srgrimes			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
3961558Srgrimes			    dumpme && (arg != 'w') ? '>' : ' ',
3971558Srgrimes			    dtwalk->dd_name,
3981558Srgrimes			    dt ? dt->fs_file : "",
3991558Srgrimes			    dtwalk->dd_level,
4001558Srgrimes			    date);
4011558Srgrimes	}
4021558Srgrimes}
4031558Srgrimes
4041558Srgrimesint
40592837Simpdatesort(const void *a1, const void *a2)
4061558Srgrimes{
4071558Srgrimes	struct dumpdates *d1 = *(struct dumpdates **)a1;
4081558Srgrimes	struct dumpdates *d2 = *(struct dumpdates **)a2;
4091558Srgrimes	int diff;
4101558Srgrimes
4111558Srgrimes	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
4121558Srgrimes	if (diff == 0)
4131558Srgrimes		return (d2->dd_ddate - d1->dd_ddate);
4141558Srgrimes	return (diff);
4151558Srgrimes}
416