xref: /freebsd/sbin/dump/main.c (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/mount.h>
35 #include <sys/disklabel.h>
36 
37 #include <ufs/ufs/extattr.h>
38 #include <ufs/ufs/quota.h>
39 #include <ufs/ufs/dinode.h>
40 #include <ufs/ufs/ufsmount.h>
41 #include <ufs/ffs/fs.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 <libufs.h>
51 #include <limits.h>
52 #include <signal.h>
53 #include <stdint.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <timeconv.h>
59 #include <unistd.h>
60 
61 #include "dump.h"
62 #include "pathnames.h"
63 
64 int	mapsize;	/* size of the state maps */
65 char	*usedinomap;	/* map of allocated inodes */
66 char	*dumpdirmap;	/* map of directories to be dumped */
67 char	*dumpinomap;	/* map of files to be dumped */
68 char	*disk;		/* name of the disk file */
69 char	*tape;		/* name of the tape file */
70 char	*popenout;	/* popen(3) per-"tape" command */
71 int	level;		/* dump level of this dump */
72 int	uflag;		/* update flag */
73 int	diskfd;		/* disk file descriptor */
74 int	pipeout;	/* true => output to standard output */
75 int	density = 0;	/* density in bytes/0.1" " <- this is for hilit19 */
76 long	tapesize;	/* estimated tape size, blocks */
77 long	tsize;		/* tape size in 0.1" units */
78 int	etapes;		/* estimated number of tapes */
79 int	nonodump;	/* if set, do not honor UF_NODUMP user flags */
80 int	unlimited;	/* if set, write to end of medium */
81 int	cachesize = 0;	/* block cache size (in bytes), defaults to 0 */
82 int	rsync_friendly;	/* be friendly with rsync */
83 int	notify = 0;	/* notify operator flag */
84 int	blockswritten = 0; /* number of blocks written on current tape */
85 int	tapeno = 0;	/* current tape number */
86 int	ntrec = NTREC;	/* # tape blocks in each tape record */
87 long	blocksperfile;	/* number of blocks per output file */
88 int	cartridge = 0;	/* Assume non-cartridge tape */
89 char	*host = NULL;	/* remote host (if any) */
90 time_t	tstart_writing;	/* when started writing the first tape block */
91 time_t	tend_writing;	/* after writing the last tape block */
92 int	passno;		/* current dump pass number */
93 struct	fs *sblock;	/* the file system super block */
94 long	dev_bsize = 1;	/* recalculated below */
95 int	dev_bshift;	/* log2(dev_bsize) */
96 int	tp_bshift;	/* log2(TP_BSIZE) */
97 int	snapdump = 0;	/* dumping live filesystem, so use snapshot */
98 
99 static char *getmntpt(char *, int *);
100 static long numarg(const char *, long, long);
101 static void obsolete(int *, char **[]);
102 static void usage(void) __dead2;
103 
104 int
105 main(int argc, char *argv[])
106 {
107 	struct stat sb;
108 	ino_t ino;
109 	int dirty;
110 	union dinode *dp;
111 	struct fstab *dt;
112 	char *map, *mntpt;
113 	int ch, mode, mntflags;
114 	int i, ret, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
115 	int just_estimate = 0;
116 	ino_t maxino;
117 	char *tmsg;
118 
119 	spcl.c_date = _time_to_time64(time(NULL));
120 
121 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
122 	dumpdates = _PATH_DUMPDATES;
123 	popenout = NULL;
124 	tape = NULL;
125 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
126 		quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
127 	level = 0;
128 	rsync_friendly = 0;
129 
130 	if (argc < 2)
131 		usage();
132 
133 	obsolete(&argc, &argv);
134 	while ((ch = getopt(argc, argv,
135 	    "0123456789aB:b:C:cD:d:f:h:LnP:RrSs:T:uWw")) != -1)
136 		switch (ch) {
137 		/* dump level */
138 		case '0': case '1': case '2': case '3': case '4':
139 		case '5': case '6': case '7': case '8': case '9':
140 			level = 10 * level + ch - '0';
141 			break;
142 
143 		case 'a':		/* `auto-size', Write to EOM. */
144 			unlimited = 1;
145 			break;
146 
147 		case 'B':		/* blocks per output file */
148 			blocksperfile = numarg("number of blocks per file",
149 			    1L, 0L);
150 			break;
151 
152 		case 'b':		/* blocks per tape write */
153 			ntrec = numarg("number of blocks per write",
154 			    1L, 1000L);
155 			break;
156 
157 		case 'C':
158 			cachesize = numarg("cachesize", 0, 0) * 1024 * 1024;
159 			break;
160 
161 		case 'c':		/* Tape is cart. not 9-track */
162 			cartridge = 1;
163 			break;
164 
165 		case 'D':
166 			dumpdates = optarg;
167 			break;
168 
169 		case 'd':		/* density, in bits per inch */
170 			density = numarg("density", 10L, 327670L) / 10;
171 			if (density >= 625 && !bflag)
172 				ntrec = HIGHDENSITYTREC;
173 			break;
174 
175 		case 'f':		/* output file */
176 			if (popenout != NULL)
177 				errx(X_STARTUP, "You cannot use the P and f "
178 				    "flags together.\n");
179 			tape = optarg;
180 			break;
181 
182 		case 'h':
183 			honorlevel = numarg("honor level", 0L, 10L);
184 			break;
185 
186 		case 'L':
187 			snapdump = 1;
188 			break;
189 
190 		case 'n':		/* notify operators */
191 			notify = 1;
192 			break;
193 
194 		case 'P':
195 			if (tape != NULL)
196 				errx(X_STARTUP, "You cannot use the P and f "
197 				    "flags together.\n");
198 			popenout = optarg;
199 			break;
200 
201 		case 'r': /* store slightly less data to be friendly to rsync */
202 			if (rsync_friendly < 1)
203 				rsync_friendly = 1;
204 			break;
205 
206 		case 'R': /* store even less data to be friendlier to rsync */
207 			if (rsync_friendly < 2)
208 				rsync_friendly = 2;
209 			break;
210 
211 		case 'S':               /* exit after estimating # of tapes */
212 			just_estimate = 1;
213 			break;
214 
215 		case 's':		/* tape size, feet */
216 			tsize = numarg("tape size", 1L, 0L) * 12 * 10;
217 			break;
218 
219 		case 'T':		/* time of last dump */
220 			spcl.c_ddate = unctime(optarg);
221 			if (spcl.c_ddate < 0) {
222 				(void)fprintf(stderr, "bad time \"%s\"\n",
223 				    optarg);
224 				exit(X_STARTUP);
225 			}
226 			Tflag = 1;
227 			lastlevel = -1;
228 			break;
229 
230 		case 'u':		/* update /etc/dumpdates */
231 			uflag = 1;
232 			break;
233 
234 		case 'W':		/* what to do */
235 		case 'w':
236 			lastdump(ch);
237 			exit(X_FINOK);	/* do nothing else */
238 
239 		default:
240 			usage();
241 		}
242 	argc -= optind;
243 	argv += optind;
244 
245 	if (argc < 1) {
246 		(void)fprintf(stderr, "Must specify disk or file system\n");
247 		exit(X_STARTUP);
248 	}
249 	disk = *argv++;
250 	argc--;
251 	if (argc >= 1) {
252 		(void)fprintf(stderr, "Unknown arguments to dump:");
253 		while (argc--)
254 			(void)fprintf(stderr, " %s", *argv++);
255 		(void)fprintf(stderr, "\n");
256 		exit(X_STARTUP);
257 	}
258 	if (rsync_friendly && (level > 0)) {
259 		(void)fprintf(stderr, "%s %s\n", "rsync friendly options",
260 		    "can be used only with level 0 dumps.");
261 		exit(X_STARTUP);
262 	}
263 	if (Tflag && uflag) {
264 	        (void)fprintf(stderr,
265 		    "You cannot use the T and u flags together.\n");
266 		exit(X_STARTUP);
267 	}
268 	if (popenout) {
269 		tape = "child pipeline process";
270 	} else if (tape == NULL && (tape = getenv("TAPE")) == NULL)
271 		tape = _PATH_DEFTAPE;
272 	if (strcmp(tape, "-") == 0) {
273 		pipeout++;
274 		tape = "standard output";
275 	}
276 
277 	if (blocksperfile)
278 		blocksperfile = rounddown(blocksperfile, ntrec);
279 	else if (!unlimited) {
280 		/*
281 		 * Determine how to default tape size and density
282 		 *
283 		 *         	density				tape size
284 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
285 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
286 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
287 		 *						(450*4 - slop)
288 		 * hilit19 hits again: "
289 		 */
290 		if (density == 0)
291 			density = cartridge ? 100 : 160;
292 		if (tsize == 0)
293 			tsize = cartridge ? 1700L*120L : 2300L*120L;
294 	}
295 
296 	if (strchr(tape, ':')) {
297 		host = tape;
298 		tape = strchr(host, ':');
299 		*tape++ = '\0';
300 #ifdef RDUMP
301 		if (strchr(tape, '\n')) {
302 		    (void)fprintf(stderr, "invalid characters in tape\n");
303 		    exit(X_STARTUP);
304 		}
305 		if (rmthost(host) == 0)
306 			exit(X_STARTUP);
307 #else
308 		(void)fprintf(stderr, "remote dump not enabled\n");
309 		exit(X_STARTUP);
310 #endif
311 	}
312 	(void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
313 
314 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
315 		signal(SIGHUP, sig);
316 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
317 		signal(SIGTRAP, sig);
318 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
319 		signal(SIGFPE, sig);
320 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
321 		signal(SIGBUS, sig);
322 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
323 		signal(SIGSEGV, sig);
324 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
325 		signal(SIGTERM, sig);
326 	if (signal(SIGINT, interrupt) == SIG_IGN)
327 		signal(SIGINT, SIG_IGN);
328 
329 	dump_getfstab();	/* /etc/fstab snarfed */
330 	/*
331 	 *	disk can be either the full special file name,
332 	 *	the suffix of the special file name,
333 	 *	the special name missing the leading '/',
334 	 *	the file system name with or without the leading '/'.
335 	 */
336 	dt = fstabsearch(disk);
337 	if (dt != NULL) {
338 		disk = rawname(dt->fs_spec);
339  		if (disk == NULL)
340  			errx(X_STARTUP, "%s: unknown file system", dt->fs_spec);
341 		(void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
342 		(void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
343 	} else {
344 		(void)strncpy(spcl.c_dev, disk, NAMELEN);
345 		(void)strncpy(spcl.c_filesys, "an unlisted file system",
346 		    NAMELEN);
347 	}
348 	spcl.c_dev[NAMELEN-1]='\0';
349 	spcl.c_filesys[NAMELEN-1]='\0';
350 
351 	if ((mntpt = getmntpt(disk, &mntflags)) != NULL) {
352 		if (mntflags & MNT_RDONLY) {
353 			if (snapdump != 0) {
354 				msg("WARNING: %s\n",
355 				    "-L ignored for read-only filesystem.");
356 				snapdump = 0;
357 			}
358 		} else if (snapdump == 0) {
359 			msg("WARNING: %s\n",
360 			    "should use -L when dumping live read-write "
361 			    "filesystems!");
362 		} else {
363 			char snapname[BUFSIZ], snapcmd[BUFSIZ];
364 
365 			snprintf(snapname, sizeof snapname, "%s/.snap", mntpt);
366 			if ((stat(snapname, &sb) < 0) || !S_ISDIR(sb.st_mode)) {
367 				msg("WARNING: %s %s\n",
368 				    "-L requested but snapshot location",
369 				    snapname);
370 				msg("         %s: %s\n",
371 				    "is not a directory",
372 				    "dump downgraded, -L ignored");
373 				snapdump = 0;
374 			} else {
375 				snprintf(snapname, sizeof snapname,
376 				    "%s/.snap/dump_snapshot", mntpt);
377 				snprintf(snapcmd, sizeof snapcmd, "%s %s %s",
378 				    _PATH_MKSNAP_FFS, mntpt, snapname);
379 				unlink(snapname);
380 				if (system(snapcmd) != 0)
381 					errx(X_STARTUP, "Cannot create %s: %s\n",
382 					    snapname, strerror(errno));
383 				if ((diskfd = open(snapname, O_RDONLY)) < 0) {
384 					unlink(snapname);
385 					errx(X_STARTUP, "Cannot open %s: %s\n",
386 					    snapname, strerror(errno));
387 				}
388 				unlink(snapname);
389 				if (fstat(diskfd, &sb) != 0)
390 					err(X_STARTUP, "%s: stat", snapname);
391 				spcl.c_date = _time_to_time64(sb.st_mtime);
392 			}
393 		}
394 	} else if (snapdump != 0) {
395 		msg("WARNING: Cannot use -L on an unmounted filesystem.\n");
396 		snapdump = 0;
397 	}
398 	if (snapdump == 0) {
399 		if ((diskfd = open(disk, O_RDONLY)) < 0)
400 			err(X_STARTUP, "Cannot open %s", disk);
401 		if (fstat(diskfd, &sb) != 0)
402 			err(X_STARTUP, "%s: stat", disk);
403 		if (S_ISDIR(sb.st_mode))
404 			errx(X_STARTUP, "%s: unknown file system", disk);
405 	}
406 
407 	(void)strcpy(spcl.c_label, "none");
408 	(void)gethostname(spcl.c_host, NAMELEN);
409 	spcl.c_level = level;
410 	spcl.c_type = TS_TAPE;
411 	if (rsync_friendly) {
412 		/* don't store real dump times */
413 		spcl.c_date = 0;
414 		spcl.c_ddate = 0;
415 	}
416 	if (spcl.c_date == 0) {
417 		tmsg = "the epoch\n";
418 	} else {
419 		time_t t = _time64_to_time(spcl.c_date);
420 		tmsg = ctime(&t);
421 	}
422 	msg("Date of this level %d dump: %s", level, tmsg);
423 
424 	if (!Tflag && (!rsync_friendly))
425 	        getdumptime();		/* /etc/dumpdates snarfed */
426 	if (spcl.c_ddate == 0) {
427 		tmsg = "the epoch\n";
428 	} else {
429 		time_t t = _time64_to_time(spcl.c_ddate);
430 		tmsg = ctime(&t);
431 	}
432 	if (lastlevel < 0)
433 		msg("Date of last (level unknown) dump: %s", tmsg);
434 	else
435 		msg("Date of last level %d dump: %s", lastlevel, tmsg);
436 
437 	msg("Dumping %s%s ", snapdump ? "snapshot of ": "", disk);
438 	if (dt != NULL)
439 		msgtail("(%s) ", dt->fs_file);
440 	if (host)
441 		msgtail("to %s on host %s\n", tape, host);
442 	else
443 		msgtail("to %s\n", tape);
444 
445 	sync();
446 	if ((ret = sbget(diskfd, &sblock, UFS_STDSB, UFS_NOCSUM)) != 0) {
447 		switch (ret) {
448 		case ENOENT:
449 			warn("Cannot find file system superblock");
450 			return (1);
451 		default:
452 			warn("Unable to read file system superblock");
453 			return (1);
454 		}
455 	}
456 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
457 	dev_bshift = ffs(dev_bsize) - 1;
458 	if (dev_bsize != (1 << dev_bshift))
459 		quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
460 	tp_bshift = ffs(TP_BSIZE) - 1;
461 	if (TP_BSIZE != (1 << tp_bshift))
462 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
463 	maxino = sblock->fs_ipg * sblock->fs_ncg;
464 	mapsize = roundup(howmany(maxino, CHAR_BIT), TP_BSIZE);
465 	usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
466 	dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
467 	dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
468 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
469 
470 	nonodump = spcl.c_level < honorlevel;
471 
472 	passno = 1;
473 	setproctitle("%s: pass 1: regular files", disk);
474 	msg("mapping (Pass I) [regular files]\n");
475 	anydirskipped = mapfiles(maxino, &tapesize);
476 
477 	passno = 2;
478 	setproctitle("%s: pass 2: directories", disk);
479 	msg("mapping (Pass II) [directories]\n");
480 	while (anydirskipped) {
481 		anydirskipped = mapdirs(maxino, &tapesize);
482 	}
483 
484 	if (pipeout || unlimited) {
485 		tapesize += 10;	/* 10 trailer blocks */
486 		msg("estimated %ld tape blocks.\n", tapesize);
487 	} else {
488 		double fetapes;
489 
490 		if (blocksperfile)
491 			fetapes = (double) tapesize / blocksperfile;
492 		else if (cartridge) {
493 			/* Estimate number of tapes, assuming streaming stops at
494 			   the end of each block written, and not in mid-block.
495 			   Assume no erroneous blocks; this can be compensated
496 			   for with an artificially low tape size. */
497 			fetapes =
498 			(	  (double) tapesize	/* blocks */
499 				* TP_BSIZE	/* bytes/block */
500 				* (1.0/density)	/* 0.1" / byte " */
501 			  +
502 				  (double) tapesize	/* blocks */
503 				* (1.0/ntrec)	/* streaming-stops per block */
504 				* 15.48		/* 0.1" / streaming-stop " */
505 			) * (1.0 / tsize );	/* tape / 0.1" " */
506 		} else {
507 			/* Estimate number of tapes, for old fashioned 9-track
508 			   tape */
509 			int tenthsperirg = (density == 625) ? 3 : 7;
510 			fetapes =
511 			(	  (double) tapesize	/* blocks */
512 				* TP_BSIZE	/* bytes / block */
513 				* (1.0/density)	/* 0.1" / byte " */
514 			  +
515 				  (double) tapesize	/* blocks */
516 				* (1.0/ntrec)	/* IRG's / block */
517 				* tenthsperirg	/* 0.1" / IRG " */
518 			) * (1.0 / tsize );	/* tape / 0.1" " */
519 		}
520 		etapes = fetapes;		/* truncating assignment */
521 		etapes++;
522 		/* count the dumped inodes map on each additional tape */
523 		tapesize += (etapes - 1) *
524 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
525 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
526 		msg("estimated %ld tape blocks on %3.2f tape(s).\n",
527 		    tapesize, fetapes);
528 	}
529 
530         /*
531          * If the user only wants an estimate of the number of
532          * tapes, exit now.
533          */
534         if (just_estimate)
535                 exit(0);
536 
537 	/*
538 	 * Allocate tape buffer.
539 	 */
540 	if (!alloctape())
541 		quit(
542 	"can't allocate tape buffers - try a smaller blocking factor.\n");
543 
544 	startnewtape(1);
545 	(void)time((time_t *)&(tstart_writing));
546 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
547 
548 	passno = 3;
549 	setproctitle("%s: pass 3: directories", disk);
550 	msg("dumping (Pass III) [directories]\n");
551 	dirty = 0;		/* XXX just to get gcc to shut up */
552 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
553 		if (((ino - 1) % CHAR_BIT) == 0)	/* map is offset by 1 */
554 			dirty = *map++;
555 		else
556 			dirty >>= 1;
557 		if ((dirty & 1) == 0)
558 			continue;
559 		/*
560 		 * Skip directory inodes deleted and maybe reallocated
561 		 */
562 		dp = getino(ino, &mode);
563 		if (mode != IFDIR)
564 			continue;
565 		(void)dumpino(dp, ino);
566 	}
567 
568 	passno = 4;
569 	setproctitle("%s: pass 4: regular files", disk);
570 	msg("dumping (Pass IV) [regular files]\n");
571 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
572 		if (((ino - 1) % CHAR_BIT) == 0)	/* map is offset by 1 */
573 			dirty = *map++;
574 		else
575 			dirty >>= 1;
576 		if ((dirty & 1) == 0)
577 			continue;
578 		/*
579 		 * Skip inodes deleted and reallocated as directories.
580 		 */
581 		dp = getino(ino, &mode);
582 		if (mode == IFDIR)
583 			continue;
584 		(void)dumpino(dp, ino);
585 	}
586 
587 	(void)time((time_t *)&(tend_writing));
588 	spcl.c_type = TS_END;
589 	for (i = 0; i < ntrec; i++)
590 		writeheader(maxino - 1);
591 	if (pipeout)
592 		msg("DUMP: %jd tape blocks\n", (intmax_t)spcl.c_tapea);
593 	else
594 		msg("DUMP: %jd tape blocks on %d volume%s\n",
595 		    (intmax_t)spcl.c_tapea, spcl.c_volume,
596 		    (spcl.c_volume == 1) ? "" : "s");
597 
598 	/* report dump performance, avoid division through zero */
599 	if (tend_writing - tstart_writing == 0)
600 		msg("finished in less than a second\n");
601 	else
602 		msg("finished in %jd seconds, throughput %jd KBytes/sec\n",
603 		    (intmax_t)tend_writing - tstart_writing,
604 		    (intmax_t)(spcl.c_tapea /
605 		    (tend_writing - tstart_writing)));
606 
607 	putdumptime();
608 	trewind();
609 	broadcast("DUMP IS DONE!\a\a\n");
610 	msg("DUMP IS DONE\n");
611 	Exit(X_FINOK);
612 	/* NOTREACHED */
613 }
614 
615 static void
616 usage(void)
617 {
618 	fprintf(stderr,
619 		"usage: dump [-0123456789acLnSu] [-B records] [-b blocksize] [-C cachesize]\n"
620 		"            [-D dumpdates] [-d density] [-f file | -P pipecommand] [-h level]\n"
621 		"            [-s feet] [-T date] filesystem\n"
622 		"       dump -W | -w\n");
623 	exit(X_STARTUP);
624 }
625 
626 /*
627  * Check to see if a disk is currently mounted.
628  */
629 static char *
630 getmntpt(char *name, int *mntflagsp)
631 {
632 	long mntsize, i;
633 	struct statfs *mntbuf;
634 
635 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
636 	for (i = 0; i < mntsize; i++) {
637 		if (!strcmp(mntbuf[i].f_mntfromname, name)) {
638 			*mntflagsp = mntbuf[i].f_flags;
639 			return (mntbuf[i].f_mntonname);
640 		}
641 	}
642 	return (0);
643 }
644 
645 /*
646  * Pick up a numeric argument.  It must be nonnegative and in the given
647  * range (except that a vmax of 0 means unlimited).
648  */
649 static long
650 numarg(const char *meaning, long vmin, long vmax)
651 {
652 	char *p;
653 	long val;
654 
655 	val = strtol(optarg, &p, 10);
656 	if (*p)
657 		errx(1, "illegal %s -- %s", meaning, optarg);
658 	if (val < vmin || (vmax && val > vmax))
659 		errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
660 	return (val);
661 }
662 
663 void
664 sig(int signo)
665 {
666 	switch(signo) {
667 	case SIGALRM:
668 	case SIGBUS:
669 	case SIGFPE:
670 	case SIGHUP:
671 	case SIGTERM:
672 	case SIGTRAP:
673 		if (pipeout)
674 			quit("Signal on pipe: cannot recover\n");
675 		msg("Rewriting attempted as response to unknown signal.\n");
676 		(void)fflush(stderr);
677 		(void)fflush(stdout);
678 		close_rewind();
679 		exit(X_REWRITE);
680 		/* NOTREACHED */
681 	case SIGSEGV:
682 		msg("SIGSEGV: ABORTING!\n");
683 		(void)signal(SIGSEGV, SIG_DFL);
684 		(void)kill(0, SIGSEGV);
685 		/* NOTREACHED */
686 	}
687 }
688 
689 char *
690 rawname(char *cp)
691 {
692 	struct stat sb;
693 
694 	/*
695 	 * Ensure that the device passed in is a raw device.
696 	 */
697 	if (stat(cp, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFCHR)
698 		return (cp);
699 
700 	/*
701 	 * Since there's only one device type now, we can't construct any
702 	 * better name, so we have to return NULL.
703 	 */
704 	return (NULL);
705 }
706 
707 /*
708  * obsolete --
709  *	Change set of key letters and ordered arguments into something
710  *	getopt(3) will like.
711  */
712 static void
713 obsolete(int *argcp, char **argvp[])
714 {
715 	int argc, flags;
716 	char *ap, **argv, *flagsp, **nargv, *p;
717 
718 	/* Setup. */
719 	argv = *argvp;
720 	argc = *argcp;
721 
722 	/*
723 	 * Return if no arguments or first argument has leading
724 	 * dash or slash.
725 	 */
726 	ap = argv[1];
727 	if (argc == 1 || *ap == '-' || *ap == '/')
728 		return;
729 
730 	/* Allocate space for new arguments. */
731 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
732 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
733 		err(1, NULL);
734 
735 	*nargv++ = *argv;
736 	argv += 2;
737 
738 	for (flags = 0; *ap; ++ap) {
739 		switch (*ap) {
740 		case 'B':
741 		case 'b':
742 		case 'd':
743 		case 'f':
744 		case 'D':
745 		case 'C':
746 		case 'h':
747 		case 's':
748 		case 'T':
749 			if (*argv == NULL) {
750 				warnx("option requires an argument -- %c", *ap);
751 				usage();
752 			}
753 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
754 				err(1, NULL);
755 			nargv[0][0] = '-';
756 			nargv[0][1] = *ap;
757 			(void)strcpy(&nargv[0][2], *argv);
758 			++argv;
759 			++nargv;
760 			break;
761 		default:
762 			if (!flags) {
763 				*p++ = '-';
764 				flags = 1;
765 			}
766 			*p++ = *ap;
767 			break;
768 		}
769 	}
770 
771 	/* Terminate flags. */
772 	if (flags) {
773 		*p = '\0';
774 		*nargv++ = flagsp;
775 	} else
776 		free(flagsp);
777 
778 	/* Copy remaining arguments. */
779 	while ((*nargv++ = *argv++));
780 
781 	/* Update argument count. */
782 	*argcp = nargv - *argvp - 1;
783 }
784