Home | History | Annotate | Line # | Download | only in dump
main.c revision 1.9
      1 /*	$NetBSD: main.c,v 1.9 1997/02/27 06:17:23 mikel 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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) 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.4 (Berkeley) 4/15/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: main.c,v 1.9 1997/02/27 06:17:23 mikel Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 #ifdef sunos
     53 #include <sys/vnode.h>
     54 
     55 #include <ufs/inode.h>
     56 #include <ufs/fs.h>
     57 #else
     58 #include <ufs/ffs/fs.h>
     59 #include <ufs/ufs/dinode.h>
     60 #endif
     61 
     62 #include <protocols/dumprestore.h>
     63 
     64 #include <ctype.h>
     65 #include <err.h>
     66 #include <errno.h>
     67 #include <fcntl.h>
     68 #include <fstab.h>
     69 #include <signal.h>
     70 #include <stdio.h>
     71 #include <stdlib.h>
     72 #include <string.h>
     73 #include <unistd.h>
     74 
     75 #include "dump.h"
     76 #include "pathnames.h"
     77 
     78 #ifndef SBOFF
     79 #define SBOFF (SBLOCK * DEV_BSIZE)
     80 #endif
     81 
     82 int	notify = 0;	/* notify operator flag */
     83 int	blockswritten = 0;	/* number of blocks written on current tape */
     84 int	tapeno = 0;	/* current tape number */
     85 int	density = 0;	/* density in bytes/0.1" */
     86 int	ntrec = NTREC;	/* # tape blocks in each tape record */
     87 int	cartridge = 0;	/* Assume non-cartridge tape */
     88 long	dev_bsize = 1;	/* recalculated below */
     89 long	blocksperfile;	/* output blocks per file */
     90 char	*host = NULL;	/* remote host (if any) */
     91 uid_t	uid;		/* real uid */
     92 uid_t	euid;		/* effective uid */
     93 
     94 static long numarg __P((char *, long, long));
     95 static void obsolete __P((int *, char **[]));
     96 static void usage __P((void));
     97 
     98 int
     99 main(argc, argv)
    100 	int argc;
    101 	char *argv[];
    102 {
    103 	register ino_t ino;
    104 	register int dirty;
    105 	register struct dinode *dp;
    106 	register struct	fstab *dt;
    107 	register char *map;
    108 	register int ch;
    109 	int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
    110 	ino_t maxino;
    111 
    112 	uid = getuid();
    113 	euid = geteuid();
    114 	(void) seteuid(uid);
    115 
    116 	spcl.c_date = 0;
    117 	(void)time((time_t *)&spcl.c_date);
    118 
    119 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
    120 	if ((tape = getenv("TAPE")) == NULL)
    121 		tape = _PATH_DEFTAPE;
    122 	dumpdates = _PATH_DUMPDATES;
    123 	temp = _PATH_DTMP;
    124 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
    125 		quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
    126 	level = '0';
    127 
    128 	if (argc < 2)
    129 		usage();
    130 
    131 	obsolete(&argc, &argv);
    132 	while ((ch = getopt(argc, argv, "0123456789B:b:cd:f:h:ns:T:uWw")) != -1)
    133 		switch (ch) {
    134 		/* dump level */
    135 		case '0': case '1': case '2': case '3': case '4':
    136 		case '5': case '6': case '7': case '8': case '9':
    137 			level = ch;
    138 			break;
    139 
    140 		case 'B':		/* blocks per output file */
    141 			blocksperfile = numarg("blocks per file", 1L, 0L);
    142 			break;
    143 
    144 		case 'b':		/* blocks per tape write */
    145 			ntrec = numarg("blocks per write", 1L, 1000L);
    146 			bflag = 1;
    147 			break;
    148 
    149 		case 'c':		/* Tape is cart. not 9-track */
    150 			cartridge = 1;
    151 			break;
    152 
    153 		case 'd':		/* density, in bits per inch */
    154 			density = numarg("density", 10L, 327670L) / 10;
    155 			if (density >= 625 && !bflag)
    156 				ntrec = HIGHDENSITYTREC;
    157 			break;
    158 
    159 		case 'f':		/* output file */
    160 			tape = optarg;
    161 			break;
    162 
    163 		case 'h':
    164 			honorlevel = numarg("honor level", 0L, 10L);
    165 			break;
    166 
    167 		case 'n':		/* notify operators */
    168 			notify = 1;
    169 			break;
    170 
    171 		case 's':		/* tape size, feet */
    172 			tsize = numarg("tape size", 1L, 0L) * 12 * 10;
    173 			break;
    174 
    175 		case 'T':		/* time of last dump */
    176 			spcl.c_ddate = unctime(optarg);
    177 			if (spcl.c_ddate < 0) {
    178 				(void)fprintf(stderr, "bad time \"%s\"\n",
    179 				    optarg);
    180 				exit(X_ABORT);
    181 			}
    182 			Tflag = 1;
    183 			lastlevel = '?';
    184 			break;
    185 
    186 		case 'u':		/* update /etc/dumpdates */
    187 			uflag = 1;
    188 			break;
    189 
    190 		case 'W':		/* what to do */
    191 		case 'w':
    192 			lastdump(ch);
    193 			exit(0);	/* do nothing else */
    194 
    195 		default:
    196 			usage();
    197 		}
    198 	argc -= optind;
    199 	argv += optind;
    200 
    201 	if (argc < 1) {
    202 		(void)fprintf(stderr, "Must specify disk or filesystem\n");
    203 		exit(X_ABORT);
    204 	}
    205 	disk = *argv++;
    206 	argc--;
    207 	if (argc >= 1) {
    208 		(void)fprintf(stderr, "Unknown arguments to dump:");
    209 		while (argc--)
    210 			(void)fprintf(stderr, " %s", *argv++);
    211 		(void)fprintf(stderr, "\n");
    212 		exit(X_ABORT);
    213 	}
    214 	if (Tflag && uflag) {
    215 	        (void)fprintf(stderr,
    216 		    "You cannot use the T and u flags together.\n");
    217 		exit(X_ABORT);
    218 	}
    219 	if (strcmp(tape, "-") == 0) {
    220 		pipeout++;
    221 		tape = "standard output";
    222 	}
    223 
    224 	if (blocksperfile)
    225 		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
    226 	else {
    227 		/*
    228 		 * Determine how to default tape size and density
    229 		 *
    230 		 *         	density				tape size
    231 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
    232 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
    233 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
    234 		 *						(450*4 - slop)
    235 		 */
    236 		if (density == 0)
    237 			density = cartridge ? 100 : 160;
    238 		if (tsize == 0)
    239 			tsize = cartridge ? 1700L*120L : 2300L*120L;
    240 	}
    241 
    242 	if (strchr(tape, ':')) {
    243 		host = tape;
    244 		tape = strchr(host, ':');
    245 		*tape++ = '\0';
    246 #ifdef RDUMP
    247 		if (rmthost(host) == 0)
    248 			exit(X_ABORT);
    249 #else
    250 		(void)fprintf(stderr, "remote dump not enabled\n");
    251 		exit(X_ABORT);
    252 #endif
    253 	}
    254 	(void) setuid(uid); /* rmthost() is the only reason to be setuid */
    255 
    256 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    257 		signal(SIGHUP, sig);
    258 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
    259 		signal(SIGTRAP, sig);
    260 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
    261 		signal(SIGFPE, sig);
    262 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
    263 		signal(SIGBUS, sig);
    264 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
    265 		signal(SIGSEGV, sig);
    266 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    267 		signal(SIGTERM, sig);
    268 	if (signal(SIGINT, interrupt) == SIG_IGN)
    269 		signal(SIGINT, SIG_IGN);
    270 
    271 	set_operators();	/* /etc/group snarfed */
    272 	getfstab();		/* /etc/fstab snarfed */
    273 	/*
    274 	 *	disk can be either the full special file name,
    275 	 *	the suffix of the special file name,
    276 	 *	the special name missing the leading '/',
    277 	 *	the file system name with or without the leading '/'.
    278 	 */
    279 	dt = fstabsearch(disk);
    280 	if (dt != NULL) {
    281 		disk = rawname(dt->fs_spec);
    282 		(void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
    283 		(void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
    284 	} else {
    285 		(void)strncpy(spcl.c_dev, disk, NAMELEN);
    286 		(void)strncpy(spcl.c_filesys, "an unlisted file system",
    287 		    NAMELEN);
    288 	}
    289 	(void)strcpy(spcl.c_label, "none");
    290 	(void)gethostname(spcl.c_host, NAMELEN);
    291 	spcl.c_level = level - '0';
    292 	spcl.c_type = TS_TAPE;
    293 	if (!Tflag)
    294 	        getdumptime();		/* /etc/dumpdates snarfed */
    295 
    296 	msg("Date of this level %c dump: %s", level,
    297 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
    298  	msg("Date of last level %c dump: %s", lastlevel,
    299 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
    300 	msg("Dumping %s ", disk);
    301 	if (dt != NULL)
    302 		msgtail("(%s) ", dt->fs_file);
    303 	if (host)
    304 		msgtail("to %s on host %s\n", tape, host);
    305 	else
    306 		msgtail("to %s\n", tape);
    307 
    308 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
    309 		msg("Cannot open %s\n", disk);
    310 		exit(X_ABORT);
    311 	}
    312 	sync();
    313 	sblock = (struct fs *)sblock_buf;
    314 	bread(SBOFF, (char *) sblock, SBSIZE);
    315 	if (sblock->fs_magic != FS_MAGIC)
    316 		quit("bad sblock magic number\n");
    317 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    318 	dev_bshift = ffs(dev_bsize) - 1;
    319 	if (dev_bsize != (1 << dev_bshift))
    320 		quit("dev_bsize (%d) is not a power of 2", dev_bsize);
    321 	tp_bshift = ffs(TP_BSIZE) - 1;
    322 	if (TP_BSIZE != (1 << tp_bshift))
    323 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
    324 #ifdef FS_44INODEFMT
    325 	if (sblock->fs_inodefmt >= FS_44INODEFMT)
    326 		spcl.c_flags |= DR_NEWINODEFMT;
    327 #endif
    328 	maxino = sblock->fs_ipg * sblock->fs_ncg;
    329 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
    330 	usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
    331 	dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
    332 	dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
    333 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    334 
    335 	nonodump = spcl.c_level < honorlevel;
    336 
    337 	msg("mapping (Pass I) [regular files]\n");
    338 	anydirskipped = mapfiles(maxino, &tapesize);
    339 
    340 	msg("mapping (Pass II) [directories]\n");
    341 	while (anydirskipped) {
    342 		anydirskipped = mapdirs(maxino, &tapesize);
    343 	}
    344 
    345 	if (pipeout) {
    346 		tapesize += 10;	/* 10 trailer blocks */
    347 		msg("estimated %ld tape blocks.\n", tapesize);
    348 	} else {
    349 		double fetapes;
    350 
    351 		if (blocksperfile)
    352 			fetapes = (double) tapesize / blocksperfile;
    353 		else if (cartridge) {
    354 			/* Estimate number of tapes, assuming streaming stops at
    355 			   the end of each block written, and not in mid-block.
    356 			   Assume no erroneous blocks; this can be compensated
    357 			   for with an artificially low tape size. */
    358 			fetapes =
    359 			(	  tapesize	/* blocks */
    360 				* TP_BSIZE	/* bytes/block */
    361 				* (1.0/density)	/* 0.1" / byte */
    362 			  +
    363 				  tapesize	/* blocks */
    364 				* (1.0/ntrec)	/* streaming-stops per block */
    365 				* 15.48		/* 0.1" / streaming-stop */
    366 			) * (1.0 / tsize );	/* tape / 0.1" */
    367 		} else {
    368 			/* Estimate number of tapes, for old fashioned 9-track
    369 			   tape */
    370 			int tenthsperirg = (density == 625) ? 3 : 7;
    371 			fetapes =
    372 			(	  tapesize	/* blocks */
    373 				* TP_BSIZE	/* bytes / block */
    374 				* (1.0/density)	/* 0.1" / byte */
    375 			  +
    376 				  tapesize	/* blocks */
    377 				* (1.0/ntrec)	/* IRG's / block */
    378 				* tenthsperirg	/* 0.1" / IRG */
    379 			) * (1.0 / tsize );	/* tape / 0.1" */
    380 		}
    381 		etapes = fetapes;		/* truncating assignment */
    382 		etapes++;
    383 		/* count the dumped inodes map on each additional tape */
    384 		tapesize += (etapes - 1) *
    385 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    386 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
    387 		msg("estimated %ld tape blocks on %3.2f tape(s).\n",
    388 		    tapesize, fetapes);
    389 	}
    390 
    391 	/*
    392 	 * Allocate tape buffer.
    393 	 */
    394 	if (!alloctape())
    395 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
    396 
    397 	startnewtape(1);
    398 	(void)time((time_t *)&(tstart_writing));
    399 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
    400 
    401 	msg("dumping (Pass III) [directories]\n");
    402 	dirty = 0;		/* XXX just to get gcc to shut up */
    403 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
    404 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    405 			dirty = *map++;
    406 		else
    407 			dirty >>= 1;
    408 		if ((dirty & 1) == 0)
    409 			continue;
    410 		/*
    411 		 * Skip directory inodes deleted and maybe reallocated
    412 		 */
    413 		dp = getino(ino);
    414 		if ((dp->di_mode & IFMT) != IFDIR)
    415 			continue;
    416 		(void)dumpino(dp, ino);
    417 	}
    418 
    419 	msg("dumping (Pass IV) [regular files]\n");
    420 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
    421 		int mode;
    422 
    423 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    424 			dirty = *map++;
    425 		else
    426 			dirty >>= 1;
    427 		if ((dirty & 1) == 0)
    428 			continue;
    429 		/*
    430 		 * Skip inodes deleted and reallocated as directories.
    431 		 */
    432 		dp = getino(ino);
    433 		mode = dp->di_mode & IFMT;
    434 		if (mode == IFDIR)
    435 			continue;
    436 		(void)dumpino(dp, ino);
    437 	}
    438 
    439 	spcl.c_type = TS_END;
    440 	for (i = 0; i < ntrec; i++)
    441 		writeheader(maxino - 1);
    442 	if (pipeout)
    443 		msg("DUMP: %ld tape blocks\n",spcl.c_tapea);
    444 	else
    445 		msg("DUMP: %ld tape blocks on %d volumes(s)\n",
    446 		    spcl.c_tapea, spcl.c_volume);
    447 	putdumptime();
    448 	trewind();
    449 	broadcast("DUMP IS DONE!\7\7\n");
    450 	msg("DUMP IS DONE\n");
    451 	Exit(X_FINOK);
    452 	/* NOTREACHED */
    453 }
    454 
    455 static void
    456 usage()
    457 {
    458 
    459 	(void)fprintf(stderr, "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density] [-f file]\n            [-h level] [-s feet] [-T date] filesystem\n");
    460 	(void)fprintf(stderr, "       dump [-W | -w]\n");
    461 	exit(1);
    462 }
    463 
    464 /*
    465  * Pick up a numeric argument.  It must be nonnegative and in the given
    466  * range (except that a vmax of 0 means unlimited).
    467  */
    468 static long
    469 numarg(meaning, vmin, vmax)
    470 	char *meaning;
    471 	long vmin, vmax;
    472 {
    473 	char *p;
    474 	long val;
    475 
    476 	val = strtol(optarg, &p, 10);
    477 	if (*p)
    478 		errx(1, "illegal %s -- %s", meaning, optarg);
    479 	if (val < vmin || (vmax && val > vmax))
    480 		errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
    481 	return (val);
    482 }
    483 
    484 void
    485 sig(signo)
    486 	int signo;
    487 {
    488 	switch(signo) {
    489 	case SIGALRM:
    490 	case SIGBUS:
    491 	case SIGFPE:
    492 	case SIGHUP:
    493 	case SIGTERM:
    494 	case SIGTRAP:
    495 		if (pipeout)
    496 			quit("Signal on pipe: cannot recover\n");
    497 		msg("Rewriting attempted as response to unknown signal.\n");
    498 		(void)fflush(stderr);
    499 		(void)fflush(stdout);
    500 		close_rewind();
    501 		exit(X_REWRITE);
    502 		/* NOTREACHED */
    503 	case SIGSEGV:
    504 		msg("SIGSEGV: ABORTING!\n");
    505 		(void)signal(SIGSEGV, SIG_DFL);
    506 		(void)kill(0, SIGSEGV);
    507 		/* NOTREACHED */
    508 	}
    509 }
    510 
    511 char *
    512 rawname(cp)
    513 	char *cp;
    514 {
    515 	static char rawbuf[MAXPATHLEN];
    516 	char *dp = strrchr(cp, '/');
    517 
    518 	if (dp == NULL)
    519 		return (NULL);
    520 	*dp = '\0';
    521 	(void)strcpy(rawbuf, cp);
    522 	*dp = '/';
    523 	(void)strcat(rawbuf, "/r");
    524 	(void)strcat(rawbuf, dp + 1);
    525 	return (rawbuf);
    526 }
    527 
    528 /*
    529  * obsolete --
    530  *	Change set of key letters and ordered arguments into something
    531  *	getopt(3) will like.
    532  */
    533 static void
    534 obsolete(argcp, argvp)
    535 	int *argcp;
    536 	char **argvp[];
    537 {
    538 	int argc, flags;
    539 	char *ap, **argv, *flagsp, **nargv, *p;
    540 
    541 	/* Setup. */
    542 	argv = *argvp;
    543 	argc = *argcp;
    544 
    545 	/* Return if no arguments or first argument has leading dash. */
    546 	ap = argv[1];
    547 	if (argc == 1 || *ap == '-')
    548 		return;
    549 
    550 	/* Allocate space for new arguments. */
    551 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
    552 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
    553 		err(1, NULL);
    554 
    555 	*nargv++ = *argv;
    556 	argv += 2;
    557 
    558 	for (flags = 0; *ap; ++ap) {
    559 		switch (*ap) {
    560 		case 'B':
    561 		case 'b':
    562 		case 'd':
    563 		case 'f':
    564 		case 'h':
    565 		case 's':
    566 		case 'T':
    567 			if (*argv == NULL) {
    568 				warnx("option requires an argument -- %c", *ap);
    569 				usage();
    570 			}
    571 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
    572 				err(1, NULL);
    573 			nargv[0][0] = '-';
    574 			nargv[0][1] = *ap;
    575 			(void)strcpy(&nargv[0][2], *argv);
    576 			++argv;
    577 			++nargv;
    578 			break;
    579 		default:
    580 			if (!flags) {
    581 				*p++ = '-';
    582 				flags = 1;
    583 			}
    584 			*p++ = *ap;
    585 			break;
    586 		}
    587 	}
    588 
    589 	/* Terminate flags. */
    590 	if (flags) {
    591 		*p = '\0';
    592 		*nargv++ = flagsp;
    593 	}
    594 
    595 	/* Copy remaining arguments. */
    596 	while (*nargv++ = *argv++);
    597 
    598 	/* Update argument count. */
    599 	*argcp = nargv - *argvp - 1;
    600 }
    601