Home | History | Annotate | Line # | Download | only in dump
main.c revision 1.45
      1 /*	$NetBSD: main.c,v 1.45 2001/12/23 12:29:56 lukem Exp $	*/
      2 
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/1/95";
     45 #else
     46 __RCSID("$NetBSD: main.c,v 1.45 2001/12/23 12:29:56 lukem Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 #include <sys/stat.h>
     53 #include <sys/mount.h>
     54 
     55 #ifdef sunos
     56 #include <sys/vnode.h>
     57 
     58 #include <ufs/inode.h>
     59 #include <ufs/fs.h>
     60 #else
     61 #include <ufs/ufs/dinode.h>
     62 #include <ufs/ffs/fs.h>
     63 #include <ufs/ffs/ffs_extern.h>
     64 #endif
     65 
     66 #include <protocols/dumprestore.h>
     67 
     68 #include <ctype.h>
     69 #include <err.h>
     70 #include <errno.h>
     71 #include <fcntl.h>
     72 #include <fstab.h>
     73 #include <signal.h>
     74 #include <stdio.h>
     75 #include <stdlib.h>
     76 #include <string.h>
     77 #include <time.h>
     78 #include <unistd.h>
     79 
     80 #include "dump.h"
     81 #include "pathnames.h"
     82 
     83 gid_t	egid;			/* Retain tty privs for notification */
     84 int	timestamp;		/* print message timestamps */
     85 int	notify;			/* notify operator flag */
     86 int	blockswritten;		/* number of blocks written on current tape */
     87 int	tapeno;			/* current tape number */
     88 int	density;		/* density in bytes/0.1" */
     89 int	ntrec = NTREC;		/* # tape blocks in each tape record */
     90 int	cartridge;		/* Assume non-cartridge tape */
     91 long	dev_bsize = 1;		/* recalculated below */
     92 long	blocksperfile;		/* output blocks per file */
     93 char	*host;			/* remote host (if any) */
     94 int	readcache = -1;		/* read cache size (in readblksize blks) */
     95 int	readblksize = 32 * 1024; /* read block size */
     96 char    default_time_string[] = "%T %Z"; /* default timestamp string */
     97 char    *time_string = default_time_string; /* timestamp string */
     98 
     99 int	main(int, char *[]);
    100 static long numarg(char *, long, long);
    101 static void obsolete(int *, char **[]);
    102 static void usage(void);
    103 
    104 int
    105 main(int argc, char *argv[])
    106 {
    107 	ino_t ino;
    108 	int dirty;
    109 	struct dinode *dp;
    110 	struct fstab *dt;
    111 	struct statfs *mntinfo, fsbuf;
    112 	char *map;
    113 	int ch;
    114 	int i, anydirskipped, bflag = 0, Tflag = 0, Fflag = 0, honorlevel = 1;
    115 	ino_t maxino;
    116 	time_t tnow, date;
    117 	int dirc;
    118 	char *mountpoint;
    119 	int just_estimate = 0;
    120 	char labelstr[LBLSIZE];
    121 	char *new_time_format;
    122 
    123 	spcl.c_date = 0;
    124 	(void)time((time_t *)&spcl.c_date);
    125 	tzset(); /* set up timezone for strftime */
    126 	if ((new_time_format = getenv("TIMEFORMAT")) != NULL)
    127 		time_string = new_time_format;
    128 
    129 	/* Save setgid bit for use later */
    130 	egid = getegid();
    131 	setegid(getgid());
    132 
    133 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
    134 	if ((tape = getenv("TAPE")) == NULL)
    135 		tape = _PATH_DEFTAPE;
    136 	dumpdates = _PATH_DUMPDATES;
    137 	temp = _PATH_DTMP;
    138 	strcpy(labelstr, "none");	/* XXX safe strcpy. */
    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 	timestamp = 0;
    143 
    144 	if (argc < 2)
    145 		usage();
    146 
    147 	obsolete(&argc, &argv);
    148 	while ((ch = getopt(argc, argv,
    149 	    "0123456789B:b:cd:eFf:h:k:lL:nr:s:StT: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 = ch;
    155 			break;
    156 
    157 		case 'B':		/* blocks per output file */
    158 			blocksperfile = numarg("blocks per file", 1L, 0L);
    159 			break;
    160 
    161 		case 'b':		/* blocks per tape write */
    162 			ntrec = numarg("blocks per write", 1L, 1000L);
    163 			bflag = 1;
    164 			break;
    165 
    166 		case 'c':		/* Tape is cart. not 9-track */
    167 			cartridge = 1;
    168 			break;
    169 
    170 		case 'd':		/* density, in bits per inch */
    171 			density = numarg("density", 10L, 327670L) / 10;
    172 			if (density >= 625 && !bflag)
    173 				ntrec = HIGHDENSITYTREC;
    174 			break;
    175 
    176 		case 'e':		/* eject full tapes */
    177 			eflag = 1;
    178 			break;
    179 
    180 		case 'F':		/* files-to-dump is an fs image */
    181 			Fflag = 1;
    182 			break;
    183 
    184 		case 'f':		/* output file */
    185 			tape = optarg;
    186 			break;
    187 
    188 		case 'h':
    189 			honorlevel = numarg("honor level", 0L, 10L);
    190 			break;
    191 
    192 		case 'k':
    193 			readblksize = numarg("read block size", 0, 64) * 1024;
    194 			break;
    195 
    196 		case 'l':		/* autoload after eject full tapes */
    197 			eflag = 1;
    198 			lflag = 1;
    199 			break;
    200 
    201 		case 'L':
    202 			/*
    203 			 * Note that although there are LBLSIZE characters,
    204 			 * the last must be '\0', so the limit on strlen()
    205 			 * is really LBLSIZE-1.
    206 			 */
    207 			strncpy(labelstr, optarg, LBLSIZE);
    208 			labelstr[LBLSIZE-1] = '\0';
    209 			if (strlen(optarg) > LBLSIZE-1) {
    210 				msg(
    211 		"WARNING Label `%s' is larger than limit of %d characters.\n",
    212 				    optarg, LBLSIZE-1);
    213 				msg("WARNING: Using truncated label `%s'.\n",
    214 				    labelstr);
    215 			}
    216 			break;
    217 		case 'n':		/* notify operators */
    218 			notify = 1;
    219 			break;
    220 
    221 		case 'r':		/* read cache size */
    222 			readcache = numarg("read cache size", 0, 512);
    223 			break;
    224 
    225 		case 's':		/* tape size, feet */
    226 			tsize = numarg("tape size", 1L, 0L) * 12 * 10;
    227 			break;
    228 
    229 		case 'S':		/* exit after estimating # of tapes */
    230 			just_estimate = 1;
    231 			break;
    232 
    233 		case 't':
    234 			timestamp = 1;
    235 			break;
    236 
    237 		case 'T':		/* time of last dump */
    238 			spcl.c_ddate = unctime(optarg);
    239 			if (spcl.c_ddate < 0) {
    240 				(void)fprintf(stderr, "bad time \"%s\"\n",
    241 				    optarg);
    242 				exit(X_ABORT);
    243 			}
    244 			Tflag = 1;
    245 			lastlevel = '?';
    246 			break;
    247 
    248 		case 'u':		/* update /etc/dumpdates */
    249 			uflag = 1;
    250 			break;
    251 
    252 		case 'W':		/* what to do */
    253 		case 'w':
    254 			lastdump(ch);
    255 			exit(0);	/* do nothing else */
    256 
    257 		default:
    258 			usage();
    259 		}
    260 	argc -= optind;
    261 	argv += optind;
    262 
    263 	if (argc < 1) {
    264 		(void)fprintf(stderr,
    265 		    "Must specify disk or image, or file list\n");
    266 		exit(X_ABORT);
    267 	}
    268 
    269 
    270 	/*
    271 	 *	determine if disk is a subdirectory, and setup appropriately
    272 	 */
    273 	getfstab();		/* /etc/fstab snarfed */
    274 	disk = NULL;
    275 	mountpoint = NULL;
    276 	dirc = 0;
    277 	for (i = 0; i < argc; i++) {
    278 		struct stat sb;
    279 
    280 		if (lstat(argv[i], &sb) == -1)
    281 			quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
    282 		if (Fflag || S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
    283 			disk = argv[i];
    284  multicheck:
    285 			if (dirc != 0)
    286 				quit(
    287 	"Can't dump a disk or image at the same time as a file list\n");
    288 			break;
    289 		}
    290 		if ((dt = fstabsearch(argv[i])) != NULL) {
    291 			disk = dt->fs_spec;
    292 			mountpoint = xstrdup(dt->fs_file);
    293 			goto multicheck;
    294 		}
    295 		if (statfs(argv[i], &fsbuf) == -1)
    296 			quit("Cannot statfs %s: %s\n", argv[i],
    297 			    strerror(errno));
    298 		disk = fsbuf.f_mntfromname;
    299 		if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
    300 			goto multicheck;
    301 		if (mountpoint == NULL) {
    302 			mountpoint = xstrdup(fsbuf.f_mntonname);
    303 			if (uflag) {
    304 				msg("Ignoring u flag for subdir dump\n");
    305 				uflag = 0;
    306 			}
    307 			if (level > '0') {
    308 				msg("Subdir dump is done at level 0\n");
    309 				level = '0';
    310 			}
    311 			msg("Dumping sub files/directories from %s\n",
    312 			    mountpoint);
    313 		} else {
    314 			if (strcmp(mountpoint, fsbuf.f_mntonname) != 0)
    315 				quit("%s is not on %s\n", argv[i], mountpoint);
    316 		}
    317 		msg("Dumping file/directory %s\n", argv[i]);
    318 		dirc++;
    319 	}
    320 	if (mountpoint)
    321 		free(mountpoint);
    322 
    323 	if (dirc == 0) {
    324 		argv++;
    325 		if (argc != 1) {
    326 			(void)fprintf(stderr, "Excess arguments to dump:");
    327 			while (--argc)
    328 				(void)fprintf(stderr, " %s", *argv++);
    329 			(void)fprintf(stderr, "\n");
    330 			exit(X_ABORT);
    331 		}
    332 	}
    333 	if (Tflag && uflag) {
    334 		(void)fprintf(stderr,
    335 		    "You cannot use the T and u flags together.\n");
    336 		exit(X_ABORT);
    337 	}
    338 	if (strcmp(tape, "-") == 0) {
    339 		pipeout++;
    340 		tape = "standard output";
    341 	}
    342 
    343 	if (blocksperfile)
    344 		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
    345 	else {
    346 		/*
    347 		 * Determine how to default tape size and density
    348 		 *
    349 		 *		density				tape size
    350 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
    351 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
    352 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
    353 		 *						(450*4 - slop)
    354 		 */
    355 		if (density == 0)
    356 			density = cartridge ? 100 : 160;
    357 		if (tsize == 0)
    358 			tsize = cartridge ? 1700L*120L : 2300L*120L;
    359 	}
    360 
    361 	if (strchr(tape, ':')) {
    362 		host = tape;
    363 		tape = strchr(host, ':');
    364 		*tape++ = '\0';
    365 #ifdef RDUMP
    366 		if (rmthost(host) == 0)
    367 			exit(X_ABORT);
    368 #else
    369 		(void)fprintf(stderr, "remote dump not enabled\n");
    370 		exit(X_ABORT);
    371 #endif
    372 	}
    373 
    374 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    375 		signal(SIGHUP, sig);
    376 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
    377 		signal(SIGTRAP, sig);
    378 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
    379 		signal(SIGFPE, sig);
    380 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
    381 		signal(SIGBUS, sig);
    382 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
    383 		signal(SIGSEGV, sig);
    384 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    385 		signal(SIGTERM, sig);
    386 	if (signal(SIGINT, interrupt) == SIG_IGN)
    387 		signal(SIGINT, SIG_IGN);
    388 
    389 	set_operators();	/* /etc/group snarfed */
    390 
    391 	/*
    392 	 *	disk can be either the full special file name, or
    393 	 *	the file system name.
    394 	 */
    395 	mountpoint = NULL;
    396 	if ((dt = fstabsearch(disk)) != NULL) {
    397 		disk = rawname(dt->fs_spec);
    398 		mountpoint = dt->fs_file;
    399 		msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
    400 	} else if ((mntinfo = mntinfosearch(disk)) != NULL) {
    401 		disk = rawname(mntinfo->f_mntfromname);
    402 		mountpoint = mntinfo->f_mntonname;
    403 		msg("Found %s on %s in mount table\n", disk, mountpoint);
    404 	}
    405 	if (mountpoint != NULL) {
    406 		if (dirc != 0)
    407 			(void)snprintf(spcl.c_filesys, NAMELEN,
    408 			    "a subset of %s", mountpoint);
    409 		else
    410 			(void)strncpy(spcl.c_filesys, mountpoint, NAMELEN);
    411 	} else if (Fflag) {
    412 		(void)strncpy(spcl.c_filesys, "a file system image", NAMELEN);
    413 	} else {
    414 		(void)strncpy(spcl.c_filesys, "an unlisted file system",
    415 		    NAMELEN);
    416 	}
    417 	(void)strncpy(spcl.c_dev, disk, NAMELEN);
    418 	(void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
    419 	(void)gethostname(spcl.c_host, NAMELEN);
    420 	spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
    421 
    422 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
    423 		msg("Cannot open %s\n", disk);
    424 		exit(X_ABORT);
    425 	}
    426 	sync();
    427 
    428 	needswap = fs_read_sblock(sblock_buf);
    429 
    430 	spcl.c_level = iswap32(level - '0');
    431 	spcl.c_type = iswap32(TS_TAPE);
    432 	spcl.c_date = iswap32(spcl.c_date);
    433 	spcl.c_ddate = iswap32(spcl.c_ddate);
    434 	if (!Tflag)
    435 		getdumptime();		/* /etc/dumpdates snarfed */
    436 
    437 	date = iswap32(spcl.c_date);
    438 	msg("Date of this level %c dump: %s", level,
    439 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
    440 	date = iswap32(spcl.c_ddate);
    441  	msg("Date of last level %c dump: %s", lastlevel,
    442 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
    443 	msg("Dumping ");
    444 	if (dirc != 0)
    445 		msgtail("a subset of ");
    446 	msgtail("%s (%s) ", disk, spcl.c_filesys);
    447 	if (host)
    448 		msgtail("to %s on host %s\n", tape, host);
    449 	else
    450 		msgtail("to %s\n", tape);
    451 	msg("Label: %s\n", labelstr);
    452 
    453 	ufsib = fs_parametrize();
    454 
    455 	dev_bshift = ffs(dev_bsize) - 1;
    456 	if (dev_bsize != (1 << dev_bshift))
    457 		quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
    458 	tp_bshift = ffs(TP_BSIZE) - 1;
    459 	if (TP_BSIZE != (1 << tp_bshift))
    460 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
    461 	maxino = fs_maxino();
    462 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
    463 	usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    464 	dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    465 	dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    466 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    467 
    468 	nonodump = iswap32(spcl.c_level) < honorlevel;
    469 
    470 	initcache(readcache, readblksize);
    471 
    472 	(void)signal(SIGINFO, statussig);
    473 
    474 	msg("mapping (Pass I) [regular files]\n");
    475 	anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
    476 	    (dirc ? argv : NULL));
    477 
    478 	msg("mapping (Pass II) [directories]\n");
    479 	while (anydirskipped) {
    480 		anydirskipped = mapdirs(maxino, &tapesize);
    481 	}
    482 
    483 	if (pipeout) {
    484 		tapesize += 10;	/* 10 trailer blocks */
    485 		msg("estimated %ld tape blocks.\n", tapesize);
    486 	} else {
    487 		double fetapes;
    488 
    489 		if (blocksperfile)
    490 			fetapes = (double) tapesize / blocksperfile;
    491 		else if (cartridge) {
    492 			/* Estimate number of tapes, assuming streaming stops at
    493 			   the end of each block written, and not in mid-block.
    494 			   Assume no erroneous blocks; this can be compensated
    495 			   for with an artificially low tape size. */
    496 			fetapes =
    497 			(	  tapesize	/* blocks */
    498 				* TP_BSIZE	/* bytes/block */
    499 				* (1.0/density)	/* 0.1" / byte */
    500 			  +
    501 				  tapesize	/* blocks */
    502 				* (1.0/ntrec)	/* streaming-stops per block */
    503 				* 15.48		/* 0.1" / streaming-stop */
    504 			) * (1.0 / tsize );	/* tape / 0.1" */
    505 		} else {
    506 			/* Estimate number of tapes, for old fashioned 9-track
    507 			   tape */
    508 			int tenthsperirg = (density == 625) ? 3 : 7;
    509 			fetapes =
    510 			(	  tapesize	/* blocks */
    511 				* TP_BSIZE	/* bytes / block */
    512 				* (1.0/density)	/* 0.1" / byte */
    513 			  +
    514 				  tapesize	/* blocks */
    515 				* (1.0/ntrec)	/* IRG's / block */
    516 				* tenthsperirg	/* 0.1" / IRG */
    517 			) * (1.0 / tsize );	/* tape / 0.1" */
    518 		}
    519 		etapes = fetapes;		/* truncating assignment */
    520 		etapes++;
    521 		/* count the dumped inodes map on each additional tape */
    522 		tapesize += (etapes - 1) *
    523 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    524 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
    525 		msg("estimated %ld tape blocks on %3.2f tape(s).\n",
    526 		    tapesize, fetapes);
    527 	}
    528 	/*
    529 	 * If the user only wants an estimate of the number of
    530 	 * tapes, exit now.
    531 	 */
    532 	if (just_estimate)
    533 		exit(0);
    534 
    535 	/*
    536 	 * Allocate tape buffer.
    537 	 */
    538 	if (!alloctape())
    539 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
    540 
    541 	startnewtape(1);
    542 	(void)time((time_t *)&(tstart_writing));
    543 	xferrate = 0;
    544 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
    545 
    546 	msg("dumping (Pass III) [directories]\n");
    547 	dirty = 0;		/* XXX just to get gcc to shut up */
    548 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
    549 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    550 			dirty = *map++;
    551 		else
    552 			dirty >>= 1;
    553 		if ((dirty & 1) == 0)
    554 			continue;
    555 		/*
    556 		 * Skip directory inodes deleted and maybe reallocated
    557 		 */
    558 		dp = getino(ino);
    559 		if ((dp->di_mode & IFMT) != IFDIR)
    560 			continue;
    561 		(void)dumpino(dp, ino);
    562 	}
    563 
    564 	msg("dumping (Pass IV) [regular files]\n");
    565 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
    566 		int mode;
    567 
    568 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    569 			dirty = *map++;
    570 		else
    571 			dirty >>= 1;
    572 		if ((dirty & 1) == 0)
    573 			continue;
    574 		/*
    575 		 * Skip inodes deleted and reallocated as directories.
    576 		 */
    577 		dp = getino(ino);
    578 		mode = dp->di_mode & IFMT;
    579 		if (mode == IFDIR)
    580 			continue;
    581 		(void)dumpino(dp, ino);
    582 	}
    583 
    584 	spcl.c_type = iswap32(TS_END);
    585 	for (i = 0; i < ntrec; i++)
    586 		writeheader(maxino - 1);
    587 	if (pipeout)
    588 		msg("%d tape blocks\n",iswap32(spcl.c_tapea));
    589 	else
    590 		msg("%d tape blocks on %d volume%s\n",
    591 		    iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
    592 		    (iswap32(spcl.c_volume) == 1) ? "" : "s");
    593 	tnow = do_stats();
    594 	date = iswap32(spcl.c_date);
    595 	msg("Date of this level %c dump: %s", level,
    596 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
    597 	msg("Date this dump completed:  %s", ctime(&tnow));
    598 	msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
    599 	putdumptime();
    600 	trewind(0);
    601 	broadcast("DUMP IS DONE!\7\7\n");
    602 	msg("DUMP IS DONE\n");
    603 	Exit(X_FINOK);
    604 	/* NOTREACHED */
    605 	exit(X_FINOK);		/* XXX: to satisfy gcc */
    606 }
    607 
    608 static void
    609 usage(void)
    610 {
    611 
    612 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
    613 "usage: dump [-0123456789ceFlnStu] [-B records] [-b blocksize] [-d density]",
    614 "            [-f file] [-h level] [-k read block size] [-L label]",
    615 "            [-r read cache size] [-s feet] [-T date] file system",
    616 "       dump [-W | -w]");
    617 	exit(1);
    618 }
    619 
    620 /*
    621  * Pick up a numeric argument.  It must be nonnegative and in the given
    622  * range (except that a vmax of 0 means unlimited).
    623  */
    624 static long
    625 numarg(char *meaning, long vmin, long vmax)
    626 {
    627 	char *p;
    628 	long val;
    629 
    630 	val = strtol(optarg, &p, 10);
    631 	if (*p)
    632 		errx(1, "illegal %s -- %s", meaning, optarg);
    633 	if (val < vmin || (vmax && val > vmax))
    634 		errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
    635 	return (val);
    636 }
    637 
    638 void
    639 sig(int signo)
    640 {
    641 
    642 	switch(signo) {
    643 	case SIGALRM:
    644 	case SIGBUS:
    645 	case SIGFPE:
    646 	case SIGHUP:
    647 	case SIGTERM:
    648 	case SIGTRAP:
    649 		if (pipeout)
    650 			quit("Signal on pipe: cannot recover\n");
    651 		msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
    652 		(void)fflush(stderr);
    653 		(void)fflush(stdout);
    654 		close_rewind();
    655 		exit(X_REWRITE);
    656 		/* NOTREACHED */
    657 	case SIGSEGV:
    658 		msg("SIGSEGV: ABORTING!\n");
    659 		(void)signal(SIGSEGV, SIG_DFL);
    660 		(void)kill(0, SIGSEGV);
    661 		/* NOTREACHED */
    662 	}
    663 }
    664 
    665 char *
    666 rawname(char *cp)
    667 {
    668 	static char rawbuf[MAXPATHLEN];
    669 	char *dp = strrchr(cp, '/');
    670 
    671 	if (dp == NULL)
    672 		return (NULL);
    673 	*dp = '\0';
    674 	(void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
    675 	*dp = '/';
    676 	return (rawbuf);
    677 }
    678 
    679 /*
    680  * obsolete --
    681  *	Change set of key letters and ordered arguments into something
    682  *	getopt(3) will like.
    683  */
    684 static void
    685 obsolete(int *argcp, char **argvp[])
    686 {
    687 	int argc, flags;
    688 	char *ap, **argv, *flagsp, **nargv, *p;
    689 
    690 	/* Setup. */
    691 	argv = *argvp;
    692 	argc = *argcp;
    693 
    694 	/* Return if no arguments or first argument has leading dash. */
    695 	ap = argv[1];
    696 	if (argc == 1 || *ap == '-')
    697 		return;
    698 
    699 	/* Allocate space for new arguments. */
    700 	*argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
    701 	p = flagsp = xmalloc(strlen(ap) + 2);
    702 
    703 	*nargv++ = *argv;
    704 	argv += 2;
    705 
    706 	for (flags = 0; *ap; ++ap) {
    707 		switch (*ap) {
    708 		case 'B':
    709 		case 'b':
    710 		case 'd':
    711 		case 'f':
    712 		case 'h':
    713 		case 's':
    714 		case 'T':
    715 			if (*argv == NULL) {
    716 				warnx("option requires an argument -- %c", *ap);
    717 				usage();
    718 			}
    719 			nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
    720 			nargv[0][0] = '-';
    721 			nargv[0][1] = *ap;
    722 			(void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
    723 			++argv;
    724 			++nargv;
    725 			break;
    726 		default:
    727 			if (!flags) {
    728 				*p++ = '-';
    729 				flags = 1;
    730 			}
    731 			*p++ = *ap;
    732 			break;
    733 		}
    734 	}
    735 
    736 	/* Terminate flags. */
    737 	if (flags) {
    738 		*p = '\0';
    739 		*nargv++ = flagsp;
    740 	}
    741 
    742 	/* Copy remaining arguments. */
    743 	while ((*nargv++ = *argv++) != NULL)
    744 		;
    745 
    746 	/* Update argument count. */
    747 	*argcp = nargv - *argvp - 1;
    748 }
    749 
    750 
    751 void *
    752 xcalloc(size_t number, size_t size)
    753 {
    754 	void *p;
    755 
    756 	p = calloc(number, size);
    757 	if (p == NULL)
    758 		quit("%s\n", strerror(errno));
    759 	return (p);
    760 }
    761 
    762 void *
    763 xmalloc(size_t size)
    764 {
    765 	void *p;
    766 
    767 	p = malloc(size);
    768 	if (p == NULL)
    769 		quit("%s\n", strerror(errno));
    770 	return (p);
    771 }
    772 
    773 char *
    774 xstrdup(const char *str)
    775 {
    776 	char *p;
    777 
    778 	p = strdup(str);
    779 	if (p == NULL)
    780 		quit("%s\n", strerror(errno));
    781 	return (p);
    782 }
    783