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