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$";
361558Srgrimes#endif /* not lint */
371558Srgrimes
381558Srgrimes#include <sys/param.h>
3971787Sphk#include <sys/queue.h>
401558Srgrimes#include <sys/wait.h>
411558Srgrimes
4298542Smckusick#include <ufs/ufs/dinode.h>
4398542Smckusick
441558Srgrimes#include <errno.h>
451558Srgrimes#include <fstab.h>
461558Srgrimes#include <grp.h>
47103949Smike#include <limits.h>
481558Srgrimes#include <stdio.h>
491558Srgrimes#include <stdlib.h>
501558Srgrimes#include <string.h>
511558Srgrimes#include <stdarg.h>
5290742Siedowse#include <signal.h>
53217769Smckusick#include <time.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;
193161000Srse	char *tdone_str;
19490743Siedowse	int deltat, hours, mins;
1951558Srgrimes
196130753Siedowse	(void)time(&tnow);
197129556Simp	if (blockswritten > tapesize) {
198129556Simp		setproctitle("%s: 99.99%% done, finished soon", disk);
199130753Siedowse		if (tnow >= tschedule) {
200130753Siedowse			tschedule = tnow + 300;
201130753Siedowse			msg("99.99%% done, finished soon\n");
202130753Siedowse		}
203129556Simp	} else {
204129556Simp		deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
205129556Simp		    (double)(tnow - tstart_writing) / blockswritten * tapesize;
206129556Simp		tdone = tnow + deltat;
207129556Simp		percent = (blockswritten * 100.0) / tapesize;
208129556Simp		hours = deltat / 3600;
209129556Simp		mins = (deltat % 3600) / 60;
21090743Siedowse
211161000Srse		tdone_str = ctime(&tdone);
212161025Smarck		tdone_str[strlen(tdone_str) - 1] = '\0';
213129556Simp		setproctitle(
214161017Smarck		    "%s: pass %d: %3.2f%% done, finished in %d:%02d at %s",
215161017Smarck		    disk, passno, percent, hours, mins, tdone_str);
216129556Simp		if (tnow >= tschedule) {
217129556Simp			tschedule = tnow + 300;
218129556Simp			if (blockswritten < 500)
219129556Simp				return;
220161017Smarck			msg("%3.2f%% done, finished in %d:%02d at %s\n", percent,
221161000Srse			    hours, mins, tdone_str);
222129556Simp		}
2231558Srgrimes	}
2241558Srgrimes}
2251558Srgrimes
22690742Siedowse/*
22790742Siedowse * Schedule a printout of the estimate in the next call to timeest().
22890742Siedowse */
2291558Srgrimesvoid
23092837Simpinfosch(int signal __unused)
23190742Siedowse{
23290742Siedowse	tschedule = 0;
23390742Siedowse}
23490742Siedowse
23590742Siedowsevoid
2361558Srgrimesmsg(const char *fmt, ...)
2371558Srgrimes{
2381558Srgrimes	va_list ap;
2391558Srgrimes
2401558Srgrimes	(void) fprintf(stderr,"  DUMP: ");
2411558Srgrimes#ifdef TDEBUG
2421558Srgrimes	(void) fprintf(stderr, "pid=%d ", getpid());
2431558Srgrimes#endif
2441558Srgrimes	va_start(ap, fmt);
245120323Sps	(void) vfprintf(stderr, fmt, ap);
246141965Sobrien	va_end(ap);
2471558Srgrimes	(void) fflush(stdout);
2481558Srgrimes	(void) fflush(stderr);
249139422Sobrien	va_start(ap, fmt);
250139422Sobrien	(void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
251139422Sobrien	va_end(ap);
2521558Srgrimes}
2531558Srgrimes
2541558Srgrimesvoid
2551558Srgrimesmsgtail(const char *fmt, ...)
2561558Srgrimes{
2571558Srgrimes	va_list ap;
258141965Sobrien
2591558Srgrimes	va_start(ap, fmt);
2601558Srgrimes	(void) vfprintf(stderr, fmt, ap);
2611558Srgrimes	va_end(ap);
2621558Srgrimes}
2631558Srgrimes
2641558Srgrimesvoid
2651558Srgrimesquit(const char *fmt, ...)
2661558Srgrimes{
2671558Srgrimes	va_list ap;
2681558Srgrimes
2691558Srgrimes	(void) fprintf(stderr,"  DUMP: ");
2701558Srgrimes#ifdef TDEBUG
2711558Srgrimes	(void) fprintf(stderr, "pid=%d ", getpid());
2721558Srgrimes#endif
2731558Srgrimes	va_start(ap, fmt);
2741558Srgrimes	(void) vfprintf(stderr, fmt, ap);
2751558Srgrimes	va_end(ap);
2761558Srgrimes	(void) fflush(stdout);
2771558Srgrimes	(void) fflush(stderr);
2781558Srgrimes	dumpabort(0);
2791558Srgrimes}
2801558Srgrimes
2811558Srgrimes/*
2821558Srgrimes *	Tell the operator what has to be done;
2831558Srgrimes *	we don't actually do it
2841558Srgrimes */
2851558Srgrimes
2861558Srgrimesstruct fstab *
28792837Simpallocfsent(const struct fstab *fs)
2881558Srgrimes{
28986473Siedowse	struct fstab *new;
2901558Srgrimes
2911558Srgrimes	new = (struct fstab *)malloc(sizeof (*fs));
2921558Srgrimes	if (new == NULL ||
2931558Srgrimes	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
2941558Srgrimes	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
2951558Srgrimes	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
2961558Srgrimes		quit("%s\n", strerror(errno));
2971558Srgrimes	new->fs_passno = fs->fs_passno;
2981558Srgrimes	new->fs_freq = fs->fs_freq;
2991558Srgrimes	return (new);
3001558Srgrimes}
3011558Srgrimes
3021558Srgrimesstruct	pfstab {
30371787Sphk	SLIST_ENTRY(pfstab) pf_list;
3041558Srgrimes	struct	fstab *pf_fstab;
3051558Srgrimes};
3061558Srgrimes
30771787Sphkstatic	SLIST_HEAD(, pfstab) table;
3081558Srgrimes
3091558Srgrimesvoid
310113214Smdodddump_getfstab(void)
3111558Srgrimes{
31286473Siedowse	struct fstab *fs;
31386473Siedowse	struct pfstab *pf;
3141558Srgrimes
3151558Srgrimes	if (setfsent() == 0) {
3161558Srgrimes		msg("Can't open %s for dump table information: %s\n",
3171558Srgrimes		    _PATH_FSTAB, strerror(errno));
3181558Srgrimes		return;
3191558Srgrimes	}
3201558Srgrimes	while ((fs = getfsent()) != NULL) {
321189267Scy		if ((strcmp(fs->fs_type, FSTAB_RW) &&
3221558Srgrimes		    strcmp(fs->fs_type, FSTAB_RO) &&
323189267Scy		    strcmp(fs->fs_type, FSTAB_RQ)) ||
324189267Scy		    strcmp(fs->fs_vfstype, "ufs"))
3251558Srgrimes			continue;
3261558Srgrimes		fs = allocfsent(fs);
3271558Srgrimes		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
3281558Srgrimes			quit("%s\n", strerror(errno));
3291558Srgrimes		pf->pf_fstab = fs;
33071787Sphk		SLIST_INSERT_HEAD(&table, pf, pf_list);
3311558Srgrimes	}
3321558Srgrimes	(void) endfsent();
3331558Srgrimes}
3341558Srgrimes
3351558Srgrimes/*
3361558Srgrimes * Search in the fstab for a file name.
3371558Srgrimes * This file name can be either the special or the path file name.
3381558Srgrimes *
3391558Srgrimes * The file name can omit the leading '/'.
3401558Srgrimes */
3411558Srgrimesstruct fstab *
34292837Simpfstabsearch(const char *key)
3431558Srgrimes{
34486473Siedowse	struct pfstab *pf;
34586473Siedowse	struct fstab *fs;
3461558Srgrimes	char *rn;
3471558Srgrimes
34871787Sphk	SLIST_FOREACH(pf, &table, pf_list) {
3491558Srgrimes		fs = pf->pf_fstab;
3501558Srgrimes		if (strcmp(fs->fs_file, key) == 0 ||
3511558Srgrimes		    strcmp(fs->fs_spec, key) == 0)
3521558Srgrimes			return (fs);
3531558Srgrimes		rn = rawname(fs->fs_spec);
3541558Srgrimes		if (rn != NULL && strcmp(rn, key) == 0)
3551558Srgrimes			return (fs);
3561558Srgrimes		if (key[0] != '/') {
3571558Srgrimes			if (*fs->fs_spec == '/' &&
3581558Srgrimes			    strcmp(fs->fs_spec + 1, key) == 0)
3591558Srgrimes				return (fs);
3601558Srgrimes			if (*fs->fs_file == '/' &&
3611558Srgrimes			    strcmp(fs->fs_file + 1, key) == 0)
3621558Srgrimes				return (fs);
3631558Srgrimes		}
3641558Srgrimes	}
3651558Srgrimes	return (NULL);
3661558Srgrimes}
3671558Srgrimes
3681558Srgrimes/*
3691558Srgrimes *	Tell the operator what to do
3701558Srgrimes */
3711558Srgrimesvoid
37292837Simplastdump(int arg)	/* w ==> just what to do; W ==> most recent dumps */
3731558Srgrimes{
37486473Siedowse	int i;
37586473Siedowse	struct fstab *dt;
37686473Siedowse	struct dumpdates *dtwalk;
3771558Srgrimes	char *lastname, *date;
3781558Srgrimes	int dumpme;
37948447Sjkh	time_t tnow;
38048447Sjkh	struct tm *tlast;
3811558Srgrimes
3821558Srgrimes	(void) time(&tnow);
383113214Smdodd	dump_getfstab();	/* /etc/fstab input */
3841558Srgrimes	initdumptimes();	/* /etc/dumpdates input */
3851558Srgrimes	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
3861558Srgrimes
3871558Srgrimes	if (arg == 'w')
388102231Strhodes		(void) printf("Dump these file systems:\n");
3891558Srgrimes	else
390102231Strhodes		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
3911558Srgrimes	lastname = "??";
3921558Srgrimes	ITITERATE(i, dtwalk) {
3931558Srgrimes		if (strncmp(lastname, dtwalk->dd_name,
3941558Srgrimes		    sizeof(dtwalk->dd_name)) == 0)
3951558Srgrimes			continue;
39648447Sjkh		date = (char *)ctime(&dtwalk->dd_ddate);
3971558Srgrimes		date[16] = '\0';	/* blast away seconds and year */
3981558Srgrimes		lastname = dtwalk->dd_name;
3991558Srgrimes		dt = fstabsearch(dtwalk->dd_name);
40048447Sjkh		dumpme = (dt != NULL && dt->fs_freq != 0);
40148447Sjkh		if (dumpme) {
40248447Sjkh		    tlast = localtime(&dtwalk->dd_ddate);
40348447Sjkh		    dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
40448447Sjkh				     - (tlast->tm_min * 60) - tlast->tm_sec
40548447Sjkh				     + (dt->fs_freq * 86400));
40648447Sjkh		};
4071558Srgrimes		if (arg != 'w' || dumpme)
4081558Srgrimes			(void) printf(
409193022Sbrian			    "%c %8s\t(%6s) Last dump: Level %d, Date %s\n",
4101558Srgrimes			    dumpme && (arg != 'w') ? '>' : ' ',
4111558Srgrimes			    dtwalk->dd_name,
4121558Srgrimes			    dt ? dt->fs_file : "",
4131558Srgrimes			    dtwalk->dd_level,
4141558Srgrimes			    date);
4151558Srgrimes	}
4161558Srgrimes}
4171558Srgrimes
4181558Srgrimesint
41992837Simpdatesort(const void *a1, const void *a2)
4201558Srgrimes{
4211558Srgrimes	struct dumpdates *d1 = *(struct dumpdates **)a1;
4221558Srgrimes	struct dumpdates *d2 = *(struct dumpdates **)a2;
4231558Srgrimes	int diff;
4241558Srgrimes
4251558Srgrimes	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
4261558Srgrimes	if (diff == 0)
4271558Srgrimes		return (d2->dd_ddate - d1->dd_ddate);
4281558Srgrimes	return (diff);
4291558Srgrimes}
430