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