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