GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/dump/main.c Lines: 0 352 0.0 %
Date: 2016-12-06 Branches: 0 246 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: main.c,v 1.58 2016/06/07 01:29:38 tedu Exp $	*/
2
/*	$NetBSD: main.c,v 1.14 1997/06/05 11:13:24 lukem Exp $	*/
3
4
/*-
5
 * Copyright (c) 1980, 1991, 1993, 1994
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 DEV_BSIZE roundup */
34
#include <sys/mount.h>
35
#include <sys/stat.h>
36
#include <sys/time.h>
37
#include <sys/ioctl.h>
38
#include <sys/disklabel.h>
39
#include <sys/dkio.h>
40
#include <ufs/ffs/fs.h>
41
#include <ufs/ufs/dinode.h>
42
43
#include <protocols/dumprestore.h>
44
45
#include <ctype.h>
46
#include <err.h>
47
#include <errno.h>
48
#include <fcntl.h>
49
#include <fstab.h>
50
#include <paths.h>
51
#include <signal.h>
52
#include <stdio.h>
53
#include <stdlib.h>
54
#include <string.h>
55
#include <time.h>
56
#include <unistd.h>
57
#include <limits.h>
58
#include <util.h>
59
60
#include "dump.h"
61
#include "pathnames.h"
62
63
int	notify = 0;	/* notify operator flag */
64
int64_t	blockswritten = 0;	/* number of blocks written on current tape */
65
int	tapeno = 0;	/* current tape number */
66
int	density = 0;	/* density in bytes/0.1" */
67
int	ntrec = NTREC;	/* # tape blocks in each tape record */
68
int	cartridge = 0;	/* Assume non-cartridge tape */
69
int64_t	blocksperfile;	/* output blocks per file */
70
char	*host = NULL;	/* remote host (if any) */
71
int	maxbsize = 64*1024;	/* XXX MAXBSIZE from sys/param.h */
72
73
struct disklabel lab;
74
75
/*
76
 * Possible superblock locations ordered from most to least likely.
77
 */
78
static int sblock_try[] = SBLOCKSEARCH;
79
80
static long long numarg(char *, long long, long long);
81
static void obsolete(int *, char **[]);
82
static void usage(void);
83
84
int
85
main(int argc, char *argv[])
86
{
87
	ino_t ino;
88
	int dirty;
89
	union dinode *dp;
90
	struct	fstab *dt;
91
	char *map;
92
	int ch, mode;
93
	struct tm then;
94
	struct statfs fsbuf;
95
	int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
96
	ino_t maxino;
97
	time_t t;
98
	int dirlist;
99
	char *toplevel, *str, *mount_point = NULL, *realpath;
100
	int just_estimate = 0;
101
	u_int64_t zero_uid = 0;
102
103
	spcl.c_date = (int64_t)time(NULL);
104
105
	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
106
	if ((tape = getenv("TAPE")) == NULL)
107
		tape = _PATH_DEFTAPE;
108
	dumpdates = _PATH_DUMPDATES;
109
	temp = _PATH_DTMP;
110
	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
111
		quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
112
	level = '0';
113
114
	if (argc < 2)
115
		usage();
116
117
	obsolete(&argc, &argv);
118
	while ((ch = getopt(argc, argv, "0123456789aB:b:cd:f:h:ns:ST:uWw")) != -1)
119
		switch (ch) {
120
		/* dump level */
121
		case '0': case '1': case '2': case '3': case '4':
122
		case '5': case '6': case '7': case '8': case '9':
123
			level = ch;
124
			break;
125
126
		case 'B':		/* blocks per output file */
127
			blocksperfile = numarg("blocks per file", 1, 0);
128
			break;
129
130
		case 'b':		/* blocks per tape write */
131
			ntrec = numarg("blocks per write", 1, 1000);
132
			if (ntrec > maxbsize/1024) {
133
				msg("Please choose a blocksize <= %dKB\n",
134
				    maxbsize/1024);
135
				exit(X_STARTUP);
136
			}
137
			bflag = 1;
138
			break;
139
140
		case 'c':		/* Tape is cart. not 9-track */
141
			cartridge = 1;
142
			break;
143
144
		case 'd':		/* density, in bits per inch */
145
			density = numarg("density", 10, 327670) / 10;
146
			if (density >= 625 && !bflag)
147
				ntrec = HIGHDENSITYTREC;
148
			break;
149
150
		case 'f':		/* output file */
151
			tape = optarg;
152
			break;
153
154
		case 'h':
155
			honorlevel = numarg("honor level", 0, 10);
156
			break;
157
158
		case 'n':		/* notify operators */
159
			notify = 1;
160
			break;
161
162
		case 's':		/* tape size, feet */
163
			tsize = numarg("tape size", 1, 0) * 12 * 10;
164
			break;
165
166
		case 'S':		/* estimate blocks and # of tapes */
167
			just_estimate = 1;
168
			break;
169
170
		case 'T':		/* time of last dump */
171
			str = strptime(optarg, "%a %b %e %H:%M:%S %Y", &then);
172
			then.tm_isdst = -1;
173
			if (str == NULL || (*str != '\n' && *str != '\0'))
174
				spcl.c_ddate = -1;
175
			else
176
				spcl.c_ddate = (int64_t)mktime(&then);
177
			if (spcl.c_ddate < 0) {
178
				(void)fprintf(stderr, "bad time \"%s\"\n",
179
				    optarg);
180
				exit(X_STARTUP);
181
			}
182
			Tflag = 1;
183
			lastlevel = '?';
184
			break;
185
186
		case 'u':		/* update /etc/dumpdates */
187
			uflag = 1;
188
			break;
189
190
		case 'W':		/* what to do */
191
		case 'w':
192
			lastdump(ch);
193
			exit(X_FINOK);	/* do nothing else */
194
			break;
195
196
		case 'a':		/* `auto-size', Write to EOM. */
197
			unlimited = 1;
198
			break;
199
200
		default:
201
			usage();
202
		}
203
	argc -= optind;
204
	argv += optind;
205
206
	if (argc < 1) {
207
		(void)fprintf(stderr, "Must specify disk or filesystem\n");
208
		exit(X_STARTUP);
209
	}
210
211
	/*
212
	 *	determine if disk is a subdirectory, and setup appropriately
213
	 */
214
	dirlist = 0;
215
	toplevel = NULL;
216
	for (i = 0; i < argc; i++) {
217
		struct stat sb;
218
219
		/* Convert potential duid into a device name */
220
		if ((diskfd = opendev(argv[i], O_RDONLY | O_NOFOLLOW, 0,
221
		    &realpath)) >= 0) {
222
			argv[i] = strdup(realpath);
223
			if (argv[i] == NULL) {
224
				msg("Cannot malloc realpath\n");
225
				exit(X_STARTUP);
226
			}
227
			(void)close(diskfd);
228
		}
229
		if (lstat(argv[i], &sb) == -1) {
230
			msg("Cannot lstat %s: %s\n", argv[i], strerror(errno));
231
			exit(X_STARTUP);
232
		}
233
		if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
234
			break;
235
		if (statfs(argv[i], &fsbuf) == -1) {
236
			msg("Cannot statfs %s: %s\n", argv[i], strerror(errno));
237
			exit(X_STARTUP);
238
		}
239
		if (strcmp(argv[i], fsbuf.f_mntonname) == 0) {
240
			if (dirlist != 0) {
241
				msg("Can't dump a mountpoint and a filelist\n");
242
				exit(X_STARTUP);
243
			}
244
			break;		/* exit if sole mountpoint */
245
		}
246
		if (!disk) {
247
			if ((toplevel = strdup(fsbuf.f_mntonname)) == NULL) {
248
				msg("Cannot malloc diskname\n");
249
				exit(X_STARTUP);
250
			}
251
			disk = toplevel;
252
			if (uflag) {
253
				msg("Ignoring u flag for subdir dump\n");
254
				uflag = 0;
255
			}
256
			if (level > '0') {
257
				msg("Subdir dump is done at level 0\n");
258
				level = '0';
259
			}
260
			msg("Dumping sub files/directories from %s\n", disk);
261
		} else {
262
			if (strcmp(disk, fsbuf.f_mntonname) != 0) {
263
				msg("%s is not on %s\n", argv[i], disk);
264
				exit(X_STARTUP);
265
			}
266
		}
267
		msg("Dumping file/directory %s\n", argv[i]);
268
		dirlist++;
269
	}
270
	if (dirlist == 0) {
271
		disk = *argv++;
272
		if (argc != 1) {
273
			(void)fputs("Excess arguments to dump:", stderr);
274
			while (--argc) {
275
				(void)putc(' ', stderr);
276
				(void)fputs(*argv++, stderr);
277
			}
278
			(void)putc('\n', stderr);
279
			exit(X_STARTUP);
280
		}
281
	}
282
	if (Tflag && uflag) {
283
	        (void)fprintf(stderr,
284
		    "You cannot use the T and u flags together.\n");
285
		exit(X_STARTUP);
286
	}
287
	if (strcmp(tape, "-") == 0) {
288
		pipeout++;
289
		tape = "standard output";
290
	}
291
292
	if (blocksperfile)
293
		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
294
	else if (!unlimited) {
295
		/*
296
		 * Determine how to default tape size and density
297
		 *
298
		 *         	density				tape size
299
		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
300
		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
301
		 * cartridge	8000 bpi (800 bytes/.1")	1700 ft.
302
		 *						(450*4 - slop)
303
		 */
304
		if (density == 0)
305
			density = cartridge ? 100 : 160;
306
		if (tsize == 0)
307
			tsize = cartridge ? 1700L*120L : 2300L*120L;
308
	}
309
310
	if (strchr(tape, ':')) {
311
		host = tape;
312
		tape = strchr(host, ':');
313
		*tape++ = '\0';
314
#ifdef RDUMP
315
		if (rmthost(host) == 0)
316
			exit(X_STARTUP);
317
#else
318
		(void)fprintf(stderr, "remote dump not enabled\n");
319
		exit(X_STARTUP);
320
#endif
321
	}
322
323
	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
324
		signal(SIGHUP, sig);
325
	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
326
		signal(SIGTRAP, sig);
327
	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
328
		signal(SIGFPE, sig);
329
	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
330
		signal(SIGBUS, sig);
331
	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
332
		signal(SIGSEGV, sig);
333
	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
334
		signal(SIGTERM, sig);
335
	if (signal(SIGINT, interrupt) == SIG_IGN)
336
		signal(SIGINT, SIG_IGN);
337
338
	getfstab();		/* /etc/fstab snarfed */
339
340
	/*
341
	 *	disk can be either the full special file name,
342
	 *	the suffix of the special file name,
343
	 *	the special name missing the leading '/',
344
	 *	the file system name with or without the leading '/'.
345
	 */
346
	if (!statfs(disk, &fsbuf) && !strcmp(fsbuf.f_mntonname, disk)) {
347
		/* mounted disk? */
348
		disk = rawname(fsbuf.f_mntfromname);
349
		if (!disk) {
350
			(void)fprintf(stderr, "cannot get raw name for %s\n",
351
			    fsbuf.f_mntfromname);
352
			exit(X_STARTUP);
353
		}
354
		mount_point = fsbuf.f_mntonname;
355
		(void)strlcpy(spcl.c_dev, fsbuf.f_mntfromname,
356
		    sizeof(spcl.c_dev));
357
		if (dirlist != 0) {
358
			(void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
359
			    "a subset of %s", mount_point);
360
		} else {
361
			(void)strlcpy(spcl.c_filesys, mount_point,
362
			    sizeof(spcl.c_filesys));
363
		}
364
	} else if ((dt = fstabsearch(disk)) != NULL) {
365
		/* in fstab? */
366
		if (strchr(dt->fs_spec, '/')) {
367
			/* fs_spec is a /dev/something */
368
			disk = rawname(dt->fs_spec);
369
		} else {
370
			/* fs_spec is a DUID */
371
			disk = rawname(disk);
372
		}
373
		mount_point = dt->fs_file;
374
		(void)strlcpy(spcl.c_dev, dt->fs_spec, sizeof(spcl.c_dev));
375
		if (dirlist != 0) {
376
			(void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
377
			    "a subset of %s", mount_point);
378
		} else {
379
			(void)strlcpy(spcl.c_filesys, mount_point,
380
			    sizeof(spcl.c_filesys));
381
		}
382
	} else {
383
		/* must be a device */
384
		(void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev));
385
		(void)strlcpy(spcl.c_filesys, "an unlisted file system",
386
		    sizeof(spcl.c_filesys));
387
	}
388
	(void)strlcpy(spcl.c_label, "none", sizeof(spcl.c_label));
389
	(void)gethostname(spcl.c_host, sizeof(spcl.c_host));
390
	spcl.c_level = level - '0';
391
	spcl.c_type = TS_TAPE;
392
393
	if ((diskfd = open(disk, O_RDONLY)) < 0) {
394
		msg("Cannot open %s\n", disk);
395
		exit(X_STARTUP);
396
	}
397
	if (ioctl(diskfd, DIOCGDINFO, (char *)&lab) < 0)
398
		err(1, "ioctl (DIOCGDINFO)");
399
400
	if (memcmp(lab.d_uid, &zero_uid, sizeof(lab.d_uid)) != 0) {
401
		if (asprintf(&duid,
402
		    "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c",
403
		    lab.d_uid[0], lab.d_uid[1], lab.d_uid[2], lab.d_uid[3],
404
		    lab.d_uid[4], lab.d_uid[5], lab.d_uid[6], lab.d_uid[7],
405
		    disk[strlen(disk)-1]) == -1) {
406
			msg("Cannot malloc duid\n");
407
			exit(X_STARTUP);
408
		}
409
	}
410
	if (!Tflag)
411
	        getdumptime();		/* /etc/dumpdates snarfed */
412
413
	t = (time_t)spcl.c_date;
414
	msg("Date of this level %c dump: %s", level,
415
		t == 0 ? "the epoch\n" : ctime(&t));
416
	t = (time_t)spcl.c_ddate;
417
 	msg("Date of last level %c dump: %s", lastlevel,
418
		t == 0 ? "the epoch\n" : ctime(&t));
419
	msg("Dumping %s ", disk);
420
	if (mount_point != NULL)
421
		msgtail("(%s) ", mount_point);
422
	if (host)
423
		msgtail("to %s on host %s\n", tape, host);
424
	else
425
		msgtail("to %s\n", tape);
426
427
	if (ioctl(diskfd, DIOCGPDINFO, (char *)&lab) < 0)
428
		err(1, "ioctl (DIOCGPDINFO)");
429
	sync();
430
	sblock = (struct fs *)sblock_buf;
431
	for (i = 0; sblock_try[i] != -1; i++) {
432
		ssize_t n = pread(diskfd, sblock, SBLOCKSIZE,
433
		    (off_t)sblock_try[i]);
434
		if (n == SBLOCKSIZE && (sblock->fs_magic == FS_UFS1_MAGIC ||
435
		     (sblock->fs_magic == FS_UFS2_MAGIC &&
436
		      sblock->fs_sblockloc == sblock_try[i])) &&
437
		    sblock->fs_bsize <= MAXBSIZE &&
438
		    sblock->fs_bsize >= sizeof(struct fs))
439
			break;
440
	}
441
	if (sblock_try[i] == -1)
442
		quit("Cannot find filesystem superblock\n");
443
	tp_bshift = ffs(TP_BSIZE) - 1;
444
	if (TP_BSIZE != (1 << tp_bshift))
445
		quit("TP_BSIZE (%d) is not a power of 2\n", TP_BSIZE);
446
#ifdef FS_44INODEFMT
447
	if (sblock->fs_magic == FS_UFS2_MAGIC ||
448
	    sblock->fs_inodefmt >= FS_44INODEFMT)
449
		spcl.c_flags |= DR_NEWINODEFMT;
450
#endif
451
	maxino = sblock->fs_ipg * sblock->fs_ncg;
452
	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
453
	usedinomap = calloc((unsigned) mapsize, sizeof(char));
454
	dumpdirmap = calloc((unsigned) mapsize, sizeof(char));
455
	dumpinomap = calloc((unsigned) mapsize, sizeof(char));
456
	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
457
458
	nonodump = spcl.c_level < honorlevel;
459
460
	(void)signal(SIGINFO, statussig);
461
462
	msg("mapping (Pass I) [regular files]\n");
463
	anydirskipped = mapfiles(maxino, &tapesize, toplevel,
464
	    (dirlist ? argv : NULL));
465
466
	msg("mapping (Pass II) [directories]\n");
467
	while (anydirskipped) {
468
		anydirskipped = mapdirs(maxino, &tapesize);
469
	}
470
471
	if (pipeout || unlimited) {
472
		tapesize += 10;	/* 10 trailer blocks */
473
		msg("estimated %lld tape blocks.\n", tapesize);
474
	} else {
475
		double fetapes;
476
477
		if (blocksperfile)
478
			fetapes = (double) tapesize / blocksperfile;
479
		else if (cartridge) {
480
			/* Estimate number of tapes, assuming streaming stops at
481
			   the end of each block written, and not in mid-block.
482
			   Assume no erroneous blocks; this can be compensated
483
			   for with an artificially low tape size. */
484
			fetapes =
485
			(	  tapesize	/* blocks */
486
				* TP_BSIZE	/* bytes/block */
487
				* (1.0/density)	/* 0.1" / byte */
488
			  +
489
				  tapesize	/* blocks */
490
				* (1.0/ntrec)	/* streaming-stops per block */
491
				* 15.48		/* 0.1" / streaming-stop */
492
			) * (1.0 / tsize );	/* tape / 0.1" */
493
		} else {
494
			/* Estimate number of tapes, for old fashioned 9-track
495
			   tape */
496
			int tenthsperirg = (density == 625) ? 3 : 7;
497
			fetapes =
498
			(	  tapesize	/* blocks */
499
				* TP_BSIZE	/* bytes / block */
500
				* (1.0/density)	/* 0.1" / byte */
501
			  +
502
				  tapesize	/* blocks */
503
				* (1.0/ntrec)	/* IRG's / block */
504
				* tenthsperirg	/* 0.1" / IRG */
505
			) * (1.0 / tsize );	/* tape / 0.1" */
506
		}
507
		etapes = fetapes;		/* truncating assignment */
508
		etapes++;
509
		/* count the dumped inodes map on each additional tape */
510
		tapesize += (etapes - 1) *
511
			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
512
		tapesize += etapes + 10;	/* headers + 10 trailer blks */
513
		msg("estimated %lld tape blocks on %3.2f tape(s).\n",
514
		    tapesize, fetapes);
515
	}
516
517
	/*
518
	 * Exit if user wants an estimate of blocks and # of tapes only.
519
	 */
520
	if (just_estimate)
521
		exit(X_FINOK);
522
523
	/*
524
	 * Allocate tape buffer.
525
	 */
526
	if (!alloctape())
527
		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
528
529
	startnewtape(1);
530
	(void)time(&tstart_writing);
531
	xferrate = 0;
532
	dumpmap(usedinomap, TS_CLRI, maxino - 1);
533
534
	msg("dumping (Pass III) [directories]\n");
535
	dirty = 0;		/* XXX just to get gcc to shut up */
536
	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
537
		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
538
			dirty = *map++;
539
		else
540
			dirty >>= 1;
541
		if ((dirty & 1) == 0)
542
			continue;
543
		/*
544
		 * Skip directory inodes deleted and maybe reallocated
545
		 */
546
		dp = getino(ino, &mode);
547
		if (mode != IFDIR)
548
			continue;
549
		(void)dumpino(dp, ino);
550
	}
551
552
	msg("dumping (Pass IV) [regular files]\n");
553
	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
554
		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
555
			dirty = *map++;
556
		else
557
			dirty >>= 1;
558
		if ((dirty & 1) == 0)
559
			continue;
560
		/*
561
		 * Skip inodes deleted and reallocated as directories.
562
		 */
563
		dp = getino(ino, &mode);
564
		if (mode == IFDIR)
565
			continue;
566
		(void)dumpino(dp, ino);
567
	}
568
569
	spcl.c_type = TS_END;
570
	for (i = 0; i < ntrec; i++)
571
		writeheader(maxino - 1);
572
	if (pipeout)
573
		msg("%lld tape blocks\n", spcl.c_tapea);
574
	else
575
		msg("%lld tape blocks on %d volume%s\n",
576
		    spcl.c_tapea, spcl.c_volume,
577
		    (spcl.c_volume == 1) ? "" : "s");
578
	t = (time_t)spcl.c_date;
579
	msg("Date of this level %c dump: %s", level,
580
	    t == 0 ? "the epoch\n" : ctime(&t));
581
	t = do_stats();
582
	msg("Date this dump completed:  %s", ctime(&t));
583
	msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno);
584
	putdumptime();
585
	trewind();
586
	broadcast("DUMP IS DONE!\7\7\n");
587
	msg("DUMP IS DONE\n");
588
	Exit(X_FINOK);
589
	/* NOTREACHED */
590
}
591
592
static void
593
usage(void)
594
{
595
	extern char *__progname;
596
597
	(void)fprintf(stderr, "usage: %s [-0123456789acnSuWw] [-B records] "
598
		      "[-b blocksize] [-d density]\n"
599
		      "\t[-f file] [-h level] [-s feet] "
600
		      "[-T date] files-to-dump\n",
601
		      __progname);
602
	exit(X_STARTUP);
603
}
604
605
/*
606
 * Pick up a numeric argument.  It must be nonnegative and in the given
607
 * range (except that a vmax of 0 means unlimited).
608
 */
609
static long long
610
numarg(char *meaning, long long vmin, long long vmax)
611
{
612
	long long val;
613
	const char *errstr;
614
615
	if (vmax == 0)
616
		vmax = LLONG_MAX;
617
	val = strtonum(optarg, vmin, vmax, &errstr);
618
	if (errstr)
619
		errx(X_STARTUP, "%s is %s [%lld - %lld]",
620
		    meaning, errstr, vmin, vmax);
621
622
	return (val);
623
}
624
625
void
626
sig(int signo)
627
{
628
	switch(signo) {
629
	case SIGALRM:
630
	case SIGBUS:
631
	case SIGFPE:
632
	case SIGHUP:
633
	case SIGTERM:
634
	case SIGTRAP:
635
		/* XXX signal race */
636
		if (pipeout)
637
			quit("Signal on pipe: cannot recover\n");
638
		msg("Rewriting attempted as response to unknown signal.\n");
639
		(void)fflush(stderr);
640
		(void)fflush(stdout);
641
		close_rewind();
642
		exit(X_REWRITE);
643
		/* NOTREACHED */
644
	case SIGSEGV:
645
#define SIGSEGV_MSG "SIGSEGV: ABORTING!\n"
646
		write(STDERR_FILENO, SIGSEGV_MSG, strlen(SIGSEGV_MSG));
647
		(void)signal(SIGSEGV, SIG_DFL);
648
		(void)kill(0, SIGSEGV);
649
		/* NOTREACHED */
650
	}
651
}
652
653
char *
654
rawname(char *cp)
655
{
656
	static char rawbuf[PATH_MAX];
657
	char *dp = strrchr(cp, '/');
658
	char *prefix;
659
660
	if (dp == NULL)
661
		return (NULL);
662
	prefix = dp[1] == 'r' ? "" : "r";
663
	*dp = '\0';
664
	(void)snprintf(rawbuf, sizeof(rawbuf), "%s/%s%s", cp, prefix, dp + 1);
665
	*dp = '/';
666
	return (rawbuf);
667
}
668
669
char *
670
getduid(char *path)
671
{
672
	int fd;
673
	struct disklabel lab;
674
	u_int64_t zero_uid = 0;
675
	char *duid;
676
677
	if ((fd = opendev(path, O_RDONLY | O_NOFOLLOW, 0, NULL)) >= 0) {
678
		if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
679
			close(fd);
680
			warn("ioctl(DIOCGDINFO)");
681
			return (NULL);
682
		}
683
		close(fd);
684
685
		if (memcmp(lab.d_uid, &zero_uid, sizeof(lab.d_uid)) != 0) {
686
			if (asprintf(&duid,
687
			    "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c",
688
			    lab.d_uid[0], lab.d_uid[1], lab.d_uid[2],
689
			    lab.d_uid[3], lab.d_uid[4], lab.d_uid[5],
690
			    lab.d_uid[6], lab.d_uid[7],
691
			    path[strlen(path)-1]) == -1) {
692
				warn("Cannot malloc duid");
693
				return (NULL);
694
			}
695
			return (duid);
696
		}
697
	}
698
699
	return (NULL);
700
}
701
702
/*
703
 * obsolete --
704
 *	Change set of key letters and ordered arguments into something
705
 *	getopt(3) will like.
706
 */
707
static void
708
obsolete(int *argcp, char **argvp[])
709
{
710
	int argc, flags;
711
	char *ap, **argv, *flagsp, **nargv, *p;
712
	size_t len;
713
714
	/* Setup. */
715
	argv = *argvp;
716
	argc = *argcp;
717
718
	/* Return if no arguments or first argument has leading dash. */
719
	ap = argv[1];
720
	if (argc == 1 || *ap == '-')
721
		return;
722
723
	/* Allocate space for new arguments. */
724
	if ((*argvp = nargv = calloc(argc + 1, sizeof(char *))) == NULL ||
725
	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
726
		err(1, NULL);
727
728
	*nargv++ = *argv;
729
	argv += 2;
730
731
	for (flags = 0; *ap; ++ap) {
732
		switch (*ap) {
733
		case 'B':
734
		case 'b':
735
		case 'd':
736
		case 'f':
737
		case 'h':
738
		case 's':
739
		case 'T':
740
			if (*argv == NULL) {
741
				warnx("option requires an argument -- %c", *ap);
742
				usage();
743
			}
744
			len = 2 + strlen(*argv) + 1;
745
			if ((nargv[0] = malloc(len)) == NULL)
746
				err(1, NULL);
747
			nargv[0][0] = '-';
748
			nargv[0][1] = *ap;
749
			(void)strlcpy(&nargv[0][2], *argv, len - 2);
750
			++argv;
751
			++nargv;
752
			break;
753
		default:
754
			if (!flags) {
755
				*p++ = '-';
756
				flags = 1;
757
			}
758
			*p++ = *ap;
759
			break;
760
		}
761
	}
762
763
	/* Terminate flags, or toss the buffer we did not use. */
764
	if (flags) {
765
		*p = '\0';
766
		*nargv++ = flagsp;
767
	} else
768
		free(flagsp);
769
770
	/* Copy remaining arguments. */
771
	while ((*nargv++ = *argv++))
772
		continue;
773
774
	/* Update argument count. */
775
	*argcp = nargv - *argvp - 1;
776
}