GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/dump/optr.c Lines: 29 169 17.2 %
Date: 2017-11-07 Branches: 14 110 12.7 %

Line Branch Exec Source
1
/*	$OpenBSD: optr.c,v 1.39 2015/10/12 15:12:44 deraadt Exp $	*/
2
/*	$NetBSD: optr.c,v 1.11 1997/05/27 08:34:36 mrg Exp $	*/
3
4
/*-
5
 * Copyright (c) 1980, 1988, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include <sys/param.h>	/* MAXBSIZE */
34
#include <sys/wait.h>
35
#include <sys/time.h>
36
37
#include <ufs/ufs/dinode.h>
38
39
#include <errno.h>
40
#include <fstab.h>
41
#include <grp.h>
42
#include <signal.h>
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include <string.h>
46
#include <stdarg.h>
47
#include <unistd.h>
48
#include <limits.h>
49
#include <utmp.h>
50
51
#include "dump.h"
52
#include "pathnames.h"
53
54
void	alarmcatch(int);
55
int	datesort(const void *, const void *);
56
57
/*
58
 *	Query the operator; This previously-fascist piece of code
59
 *	no longer requires an exact response.
60
 *	It is intended to protect dump aborting by inquisitive
61
 *	people banging on the console terminal to see what is
62
 *	happening which might cause dump to croak, destroying
63
 *	a large number of hours of work.
64
 *
65
 *	Every 2 minutes we reprint the message, alerting others
66
 *	that dump needs attention.
67
 */
68
static	int timeout;
69
static	char *attnmessage;		/* attention message */
70
71
struct fstab *allocfsent(struct fstab *fs);
72
73
int
74
query(char *question)
75
{
76
	char	replybuffer[64];
77
	int	back, errcount;
78
	FILE	*mytty;
79
	time_t	firstprompt, when_answered;
80
81
	(void) time(&firstprompt);
82
83
	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
84
		quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
85
	attnmessage = question;
86
	timeout = 0;
87
	alarmcatch(0);
88
	back = -1;
89
	errcount = 0;
90
	do {
91
		if (fgets(replybuffer, sizeof(replybuffer), mytty) == NULL) {
92
			clearerr(mytty);
93
			if (++errcount > 30)	/* XXX	ugly */
94
				quit("excessive operator query failures\n");
95
		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
96
			back = 1;
97
		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
98
			back = 0;
99
		} else {
100
			(void) fprintf(stderr,
101
			    "  DUMP: \"Yes\" or \"No\"?\n");
102
			(void) fprintf(stderr,
103
			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
104
		}
105
	} while (back < 0);
106
107
	/*
108
	 *	Turn off the alarm, and reset the signal to trap out..
109
	 */
110
	(void) alarm(0);
111
	if (signal(SIGALRM, sig) == SIG_IGN)
112
		signal(SIGALRM, SIG_IGN);
113
	(void) fclose(mytty);
114
	(void) time(&when_answered);
115
	/*
116
	 * Adjust the base for time estimates to ignore time we spent waiting
117
	 * for operator input.
118
	 */
119
	if (when_answered - firstprompt > 0)
120
		tstart_writing += (when_answered - firstprompt);
121
	return (back);
122
}
123
124
char lastmsg[BUFSIZ];
125
126
/*
127
 *	Alert the console operator, and enable the alarm clock to
128
 *	sleep for 2 minutes in case nobody comes to satisfy dump
129
 * XXX not safe
130
 */
131
/* ARGSUSED */
132
void
133
alarmcatch(int signo)
134
{
135
	int save_errno = errno;
136
137
	if (notify == 0) {
138
		if (timeout == 0)
139
			(void) fprintf(stderr,
140
			    "  DUMP: %s: (\"yes\" or \"no\") ",
141
			    attnmessage);
142
		else
143
			msgtail("\7\7");
144
	} else {
145
		if (timeout) {
146
			msgtail("\n");
147
			broadcast("");		/* just print last msg */
148
		}
149
		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
150
		    attnmessage);
151
	}
152
	signal(SIGALRM, alarmcatch);
153
	(void) alarm(120);
154
	timeout = 1;
155
	errno = save_errno;
156
}
157
158
/*
159
 *	Here if an inquisitive operator interrupts the dump program
160
 */
161
/* ARGSUSED */
162
void
163
interrupt(int signo)
164
{
165
	msg("Interrupt received.\n");
166
	if (query("Do you want to abort dump?"))
167
		dumpabort(0);
168
}
169
170
/*
171
 *	We now use wall(1) to do the actual broadcasting.
172
 */
173
void
174
broadcast(char *message)
175
{
176
	FILE *fp;
177
	char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
178
179
	if (!notify)
180
		return;
181
182
	(void)snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
183
	if ((fp = popen(buf, "w")) == NULL)
184
		return;
185
186
	(void) fputs("\7\7\7Message from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
187
	if (lastmsg[0])
188
		(void) fputs(lastmsg, fp);
189
	if (message[0])
190
		(void) fputs(message, fp);
191
192
	(void) pclose(fp);
193
}
194
195
/*
196
 *	Print out an estimate of the amount of time left to do the dump
197
 */
198
199
time_t	tschedule = 0;
200
201
void
202
timeest(void)
203
{
204
	time_t	tnow, deltat;
205
206
	(void) time(&tnow);
207
	if (tnow >= tschedule) {
208
		tschedule = tnow + 300;
209
		if (blockswritten < 500)
210
			return;
211
		deltat = tstart_writing - tnow +
212
			(1.0 * (tnow - tstart_writing))
213
			/ blockswritten * tapesize;
214
		msg("%3.2f%% done, finished in %lld:%02lld\n",
215
		    (blockswritten * 100.0) / tapesize,
216
		    (long long)deltat / 3600,
217
		    ((long long)deltat % 3600) / 60);
218
	}
219
}
220
221
void
222
msg(const char *fmt, ...)
223
{
224
	va_list ap;
225
226
	(void) fprintf(stderr,"  DUMP: ");
227
#ifdef TDEBUG
228
	(void) fprintf(stderr, "pid=%d ", getpid());
229
#endif
230
	va_start(ap, fmt);
231
	(void) vfprintf(stderr, fmt, ap);
232
	va_end(ap);
233
	(void) fflush(stdout);
234
	(void) fflush(stderr);
235
	va_start(ap, fmt);
236
	(void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
237
	va_end(ap);
238
}
239
240
void
241
msgtail(const char *fmt, ...)
242
{
243
	va_list ap;
244
245
	va_start(ap, fmt);
246
	(void) vfprintf(stderr, fmt, ap);
247
	va_end(ap);
248
}
249
250
/* XXX signal races */
251
void
252
quit(const char *fmt, ...)
253
{
254
	va_list ap;
255
256
	(void) fprintf(stderr,"  DUMP: ");
257
#ifdef TDEBUG
258
	(void) fprintf(stderr, "pid=%d ", getpid());
259
#endif
260
	va_start(ap, fmt);
261
	(void) vfprintf(stderr, fmt, ap);
262
	va_end(ap);
263
	(void) fflush(stdout);
264
	(void) fflush(stderr);
265
	dumpabort(0);
266
}
267
268
/*
269
 *	Tell the operator what has to be done;
270
 *	we don't actually do it
271
 */
272
273
struct fstab *
274
allocfsent(struct fstab *fs)
275
{
276
	struct fstab *new;
277
278
18
	new = malloc(sizeof(*fs));
279

18
	if (new == NULL ||
280
9
	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
281
9
	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
282
9
	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
283
		quit("%s\n", strerror(errno));
284
9
	new->fs_passno = fs->fs_passno;
285
9
	new->fs_freq = fs->fs_freq;
286
9
	return (new);
287
}
288
289
struct	pfstab {
290
	struct	pfstab *pf_next;
291
	struct	fstab *pf_fstab;
292
};
293
294
static	struct pfstab *table;
295
296
void
297
getfstab(void)
298
{
299
	struct fstab *fs;
300
	struct pfstab *pf;
301
302
2
	if (setfsent() == 0) {
303
		msg("Can't open %s for dump table information: %s\n",
304
		    _PATH_FSTAB, strerror(errno));
305
		return;
306
	}
307
20
	while ((fs = getfsent()) != NULL) {
308

11
		if (strcmp(fs->fs_vfstype, "ffs") &&
309
1
		    strcmp(fs->fs_vfstype, "ufs"))
310
			continue;
311

9
		if (strcmp(fs->fs_type, FSTAB_RW) &&
312
		    strcmp(fs->fs_type, FSTAB_RO) &&
313
		    strcmp(fs->fs_type, FSTAB_RQ))
314
			continue;
315
9
		fs = allocfsent(fs);
316
9
		if ((pf = malloc(sizeof(*pf))) == NULL)
317
			quit("%s\n", strerror(errno));
318
9
		pf->pf_fstab = fs;
319
9
		pf->pf_next = table;
320
9
		table = pf;
321
	}
322
1
	(void) endfsent();
323
2
}
324
325
/*
326
 * Search in the fstab for a file name.
327
 * This file name can be either the special or the path file name.
328
 *
329
 * The entries in the fstab are the BLOCK special names, not the
330
 * character special names.
331
 * The caller of fstabsearch assures that the character device
332
 * is dumped (that is much faster)
333
 *
334
 * The file name can omit the leading '/'.
335
 */
336
struct fstab *
337
fstabsearch(char *key)
338
{
339
	struct pfstab *pf;
340
	struct fstab *fs;
341
	char *rn, *uid;
342
343
	for (pf = table; pf != NULL; pf = pf->pf_next) {
344
		fs = pf->pf_fstab;
345
		if (strcmp(fs->fs_file, key) == 0 ||
346
		    strcmp(fs->fs_spec, key) == 0)
347
			return (fs);
348
		rn = rawname(fs->fs_spec);
349
		if (rn != NULL && strcmp(rn, key) == 0)
350
			return (fs);
351
		uid = getduid(rn != NULL ? rn : fs->fs_spec);
352
		if (uid != NULL && strcmp(uid, key) == 0) {
353
			free(uid);
354
			return (fs);
355
		}
356
		free(uid);
357
		if (key[0] != '/') {
358
			if (*fs->fs_spec == '/' &&
359
			    strcmp(fs->fs_spec + 1, key) == 0)
360
				return (fs);
361
			if (*fs->fs_file == '/' &&
362
			    strcmp(fs->fs_file + 1, key) == 0)
363
				return (fs);
364
		}
365
	}
366
	return (NULL);
367
}
368
369
#define SECSPERDAY	(24 * 60 * 60)
370
371
/*
372
 *	Tell the operator what to do
373
 *	w ==> just what to do; W ==> most recent dumps
374
 */
375
void
376
lastdump(int arg)
377
{
378
	int i;
379
	struct fstab *dt;
380
	struct dumpdates *dtwalk;
381
	char *lastname, *date;
382
	int dumpme;
383
2
	time_t tnow;
384
385
1
	(void) time(&tnow);
386
1
	getfstab();		/* /etc/fstab input */
387
1
	initdumptimes();	/* /etc/dumpdates input */
388
1
	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
389
390
1
	if (arg == 'w')
391
		(void) printf("Dump these file systems:\n");
392
	else
393
1
		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
394
	lastname = "??";
395

2
	ITITERATE(i, dtwalk) {
396
		if (strncmp(lastname, dtwalk->dd_name,
397
		    sizeof(dtwalk->dd_name)) == 0)
398
			continue;
399
		date = (char *)ctime(&dtwalk->dd_ddate);
400
		date[16] = '\0';	/* blast away seconds and year */
401
		lastname = dtwalk->dd_name;
402
		dt = fstabsearch(dtwalk->dd_name);
403
		dumpme = (dt != NULL &&
404
		    dt->fs_freq != 0 &&
405
		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
406
		if (arg != 'w' || dumpme)
407
			(void) printf(
408
			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
409
			    dumpme && (arg != 'w') ? '>' : ' ',
410
			    dtwalk->dd_name,
411
			    dt ? dt->fs_file : "",
412
			    dtwalk->dd_level,
413
			    date);
414
	}
415
1
}
416
417
int
418
datesort(const void *a1, const void *a2)
419
{
420
	struct dumpdates *d1 = *(struct dumpdates **)a1;
421
	struct dumpdates *d2 = *(struct dumpdates **)a2;
422
	int diff;
423
424
	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
425
	if (diff == 0)
426
		return (d2->dd_ddate - d1->dd_ddate);
427
	return (diff);
428
}