10Sstevel@tonic-gate /*
25299Sws195443  * CDDL HEADER START
35299Sws195443  *
45299Sws195443  * The contents of this file are subject to the terms of the
55299Sws195443  * Common Development and Distribution License (the "License").
65299Sws195443  * You may not use this file except in compliance with the License.
75299Sws195443  *
85299Sws195443  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95299Sws195443  * or http://www.opensolaris.org/os/licensing.
105299Sws195443  * See the License for the specific language governing permissions
115299Sws195443  * and limitations under the License.
125299Sws195443  *
135299Sws195443  * When distributing Covered Code, include this CDDL HEADER in each
145299Sws195443  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155299Sws195443  * If applicable, add the following below this CDDL HEADER, with the
165299Sws195443  * fields enclosed by brackets "[]" replaced with your own identifying
175299Sws195443  * information: Portions Copyright [yyyy] [name of copyright owner]
185299Sws195443  *
195299Sws195443  * CDDL HEADER END
205299Sws195443  */
215299Sws195443 
225299Sws195443 /*
23*6284Svk154806  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
320Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
330Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "dump.h"
390Sstevel@tonic-gate #include <rmt.h>
400Sstevel@tonic-gate #include <sys/mtio.h>
410Sstevel@tonic-gate #include <limits.h>
420Sstevel@tonic-gate #include <priv_utils.h>
430Sstevel@tonic-gate #include "roll_log.h"
44*6284Svk154806 #include <unistd.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate int	notify = 0;		/* notify operator flag */
470Sstevel@tonic-gate int	blockswritten = 0;	/* number of blocks written on current tape */
480Sstevel@tonic-gate uint_t	tapeno = 0;		/* current tape number */
490Sstevel@tonic-gate daddr32_t filenum = 0;		/* current file number on tape */
500Sstevel@tonic-gate int	density = 0;		/* density in bytes/0.1" */
510Sstevel@tonic-gate int	tenthsperirg;		/* inter-record-gap in 0.1"'s */
520Sstevel@tonic-gate uint_t	ntrec = 0;		/* # tape blocks in each tape record */
530Sstevel@tonic-gate uint_t	saved_ntrec = 0;	/* saved value of ntrec */
540Sstevel@tonic-gate uint_t	forceflag = 0;		/* forced to change tp_bsize */
550Sstevel@tonic-gate int	cartridge = 0;		/* assume non-cartridge tape */
560Sstevel@tonic-gate uint_t	tracks;			/* # tracks on a cartridge tape */
570Sstevel@tonic-gate int	diskette = 0;		/* assume not dumping to a diskette */
580Sstevel@tonic-gate int	printsize = 0;		/* just print estimated size and exit */
590Sstevel@tonic-gate int	mapfd = -1;		/* if >= 0, file descriptor for mmap */
600Sstevel@tonic-gate int32_t	tp_bsize = TP_BSIZE_MIN; /* tape block record size (frag size) */
610Sstevel@tonic-gate #ifdef DEBUG
620Sstevel@tonic-gate int	xflag;			/* debugging switch */
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate 
650Sstevel@tonic-gate char	*myname;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * This should be struct fs, but there are trailing bits on disk
690Sstevel@tonic-gate  * that we also need to read in as part of it.  It's an array of
700Sstevel@tonic-gate  * longs instead of char to force proper alignment.
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate static long sblock_buf[SBSIZE/sizeof (long)];
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #ifdef __STDC__
750Sstevel@tonic-gate static char *mb(u_offset_t);
760Sstevel@tonic-gate static void nextstate(int);
770Sstevel@tonic-gate #else
780Sstevel@tonic-gate static char *mb();
790Sstevel@tonic-gate static void nextstate();
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate 
820Sstevel@tonic-gate extern	jmp_buf checkpoint_buf;	/* context for return from checkpoint */
830Sstevel@tonic-gate #define	FUDGE_FACTOR	0x2000000
840Sstevel@tonic-gate 
851053Smaheshvs int
861053Smaheshvs main(int argc, char *argv[])
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	char		*arg;
890Sstevel@tonic-gate 	int		bflag = 0, i, error = 0, saverr;
900Sstevel@tonic-gate 	double		fetapes = 0.0;
910Sstevel@tonic-gate 	struct	mnttab	*dt;
920Sstevel@tonic-gate 	char		msgbuf[3000], *msgp;
930Sstevel@tonic-gate 	char		kbsbuf[BUFSIZ];
940Sstevel@tonic-gate 	u_offset_t	esize_shift = 0;
950Sstevel@tonic-gate 	int32_t	new_mult = 0;
960Sstevel@tonic-gate 	time32_t	snapdate;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	host = NULL;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (myname = strrchr(argv[0], '/'))
1010Sstevel@tonic-gate 		myname++;
1020Sstevel@tonic-gate 	else
1030Sstevel@tonic-gate 		myname = argv[0];
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if (strcmp("hsmdump", myname) == 0) {
1060Sstevel@tonic-gate 		msg(gettext("hsmdump emulation is no longer supported.\n"));
1070Sstevel@tonic-gate 		Exit(X_ABORT);
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	tape = DEFTAPE;
1110Sstevel@tonic-gate 	autoload_period = 12;
1120Sstevel@tonic-gate 	autoload_tries = 12;	/* traditional default of ~2.5 minutes */
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1150Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1160Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1170Sstevel@tonic-gate #endif  /* TEXT_DOMAIN */
1180Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	/*
1210Sstevel@tonic-gate 	 * If someone strips the set-uid bit, dump will still work for local
1220Sstevel@tonic-gate 	 * tapes.  Fail when we try to access a remote tape.
1230Sstevel@tonic-gate 	 */
1240Sstevel@tonic-gate 	(void) __init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (sysinfo(SI_HOSTNAME, spcl.c_host, sizeof (spcl.c_host)) < 0) {
1270Sstevel@tonic-gate 		saverr = errno;
1280Sstevel@tonic-gate 		msg(gettext("Could not get host name: %s\n"),
1290Sstevel@tonic-gate 		    strerror(saverr));
1300Sstevel@tonic-gate 		bzero(spcl.c_host, sizeof (spcl.c_host));
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	dumppid = getpid();
1340Sstevel@tonic-gate 	tsize = 0;	/* no default size, detect EOT dynamically */
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	disk = NULL;
1370Sstevel@tonic-gate 	dname = NULL;
1380Sstevel@tonic-gate 	disk_dynamic = 0;
1390Sstevel@tonic-gate 	increm = NINCREM;
1400Sstevel@tonic-gate 	incno = '9';
1410Sstevel@tonic-gate 	uflag = 0;
1420Sstevel@tonic-gate 	arg = "u";
1430Sstevel@tonic-gate 	tlabel = "none";
1440Sstevel@tonic-gate 	if (argc > 1) {
1450Sstevel@tonic-gate 		argv++;
1460Sstevel@tonic-gate 		argc--;
1470Sstevel@tonic-gate 		arg = *argv;
1480Sstevel@tonic-gate 		if (*arg == '-')
1490Sstevel@tonic-gate 			arg++;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 	while (*arg)
1520Sstevel@tonic-gate 	switch (*arg++) {		/* BE CAUTIOUS OF FALLTHROUGHS */
1530Sstevel@tonic-gate 	case 'M':
1540Sstevel@tonic-gate 		/*
1550Sstevel@tonic-gate 		 * This undocumented option causes each process to
1560Sstevel@tonic-gate 		 * mkdir debug_chdir/getpid(), and chdir to it.  This is
1570Sstevel@tonic-gate 		 * to ease the collection of profiling information and
1580Sstevel@tonic-gate 		 * core dumps.
1590Sstevel@tonic-gate 		 */
1600Sstevel@tonic-gate 		if (argc > 1) {
1610Sstevel@tonic-gate 			argv++;
1620Sstevel@tonic-gate 			argc--;
1630Sstevel@tonic-gate 			debug_chdir = *argv;
1640Sstevel@tonic-gate 			msg(gettext(
1650Sstevel@tonic-gate 			    "Each process shall try to chdir to %s/<pid>\n"),
1660Sstevel@tonic-gate 			    debug_chdir);
1670Sstevel@tonic-gate 			child_chdir();
1680Sstevel@tonic-gate 		} else {
1690Sstevel@tonic-gate 			msg(gettext("Missing move-to-dir (M) name\n"));
1700Sstevel@tonic-gate 			dumpabort();
1710Sstevel@tonic-gate 			/*NOTREACHED*/
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 		break;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	case 'w':
1760Sstevel@tonic-gate 		lastdump('w');		/* tell us only what has to be done */
1770Sstevel@tonic-gate 		exit(0);
1780Sstevel@tonic-gate 		break;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	case 'W':			/* what to do */
1810Sstevel@tonic-gate 		lastdump('W');		/* tell state of what has been done */
1820Sstevel@tonic-gate 		exit(0);		/* do nothing else */
1830Sstevel@tonic-gate 		break;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	case 'T':
1860Sstevel@tonic-gate 		if (argc > 1) {
1870Sstevel@tonic-gate 			int count;
1880Sstevel@tonic-gate 			int multiplier;
1890Sstevel@tonic-gate 			char units;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 			argv++;
1920Sstevel@tonic-gate 			argc--;
1930Sstevel@tonic-gate 			count = atoi(*argv);
1940Sstevel@tonic-gate 			if (count < 1) {
1950Sstevel@tonic-gate 				msg(gettext(
1960Sstevel@tonic-gate 				    "Unreasonable autoload timeout period\n"));
1970Sstevel@tonic-gate 				dumpabort();
1980Sstevel@tonic-gate 				/*NOTREACHED*/
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 			units = *(*argv + strlen(*argv) - 1);
2010Sstevel@tonic-gate 			switch (units) {
2020Sstevel@tonic-gate 			case 's':
2030Sstevel@tonic-gate 				multiplier = 1;
2040Sstevel@tonic-gate 				break;
2050Sstevel@tonic-gate 			case 'h':
2060Sstevel@tonic-gate 				multiplier = 3600;
2070Sstevel@tonic-gate 				break;
2080Sstevel@tonic-gate 			case '0': case '1': case '2': case '3': case '4':
2090Sstevel@tonic-gate 			case '5': case '6': case '7': case '8': case '9':
2100Sstevel@tonic-gate 			case 'm':
2110Sstevel@tonic-gate 				multiplier = 60;
2120Sstevel@tonic-gate 				break;
2130Sstevel@tonic-gate 			default:
2140Sstevel@tonic-gate 				msg(gettext(
2150Sstevel@tonic-gate 				    "Unknown timeout units indicator `%c'\n"),
2160Sstevel@tonic-gate 				    units);
2170Sstevel@tonic-gate 				dumpabort();
2180Sstevel@tonic-gate 				/*NOTREACHED*/
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 			autoload_tries = 1 +
2210Sstevel@tonic-gate 			    ((count * multiplier) / autoload_period);
2220Sstevel@tonic-gate 		} else {
2230Sstevel@tonic-gate 			msg(gettext("Missing autoload timeout period\n"));
2240Sstevel@tonic-gate 			dumpabort();
2250Sstevel@tonic-gate 			/*NOTREACHED*/
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 		break;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	case 'f':			/* output file */
2300Sstevel@tonic-gate 		if (argc > 1) {
2310Sstevel@tonic-gate 			argv++;
2320Sstevel@tonic-gate 			argc--;
2330Sstevel@tonic-gate 			tape = *argv;
2340Sstevel@tonic-gate 			if (*tape == '\0') {
2350Sstevel@tonic-gate 				msg(gettext("Bad output device name\n"));
2360Sstevel@tonic-gate 				dumpabort();
2370Sstevel@tonic-gate 				/*NOTREACHED*/
2380Sstevel@tonic-gate 			}
2390Sstevel@tonic-gate 		} else {
2400Sstevel@tonic-gate 			msg(gettext("Missing output device name\n"));
2410Sstevel@tonic-gate 			dumpabort();
2420Sstevel@tonic-gate 			/*NOTREACHED*/
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		if (strcmp(tape, "-") == 0 && verify) {
2450Sstevel@tonic-gate 			msg(gettext(
2460Sstevel@tonic-gate 			"Cannot verify when dumping to standard out.\n"));
2470Sstevel@tonic-gate 			dumpabort();
2480Sstevel@tonic-gate 			/*NOTREACHED*/
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		break;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	case 'd':			/* density, in bits per inch */
2530Sstevel@tonic-gate 		if (argc > 1) {
2540Sstevel@tonic-gate 			argv++;
2550Sstevel@tonic-gate 			argc--;
2560Sstevel@tonic-gate 			density = atoi(*argv) / 10;
2570Sstevel@tonic-gate 			if (density <= 0) {
2580Sstevel@tonic-gate 				msg(gettext(
2590Sstevel@tonic-gate 				    "Density must be a positive integer\n"));
2600Sstevel@tonic-gate 				dumpabort();
2610Sstevel@tonic-gate 				/*NOTREACHED*/
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 		} else {
2640Sstevel@tonic-gate 			msg(gettext("Missing density\n"));
2650Sstevel@tonic-gate 			dumpabort();
2660Sstevel@tonic-gate 			/*NOTREACHED*/
2670Sstevel@tonic-gate 		}
2680Sstevel@tonic-gate 		break;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	case 's':			/* tape size, feet */
2710Sstevel@tonic-gate 		if (argc > 1) {
2720Sstevel@tonic-gate 			argv++;
2730Sstevel@tonic-gate 			argc--;
2740Sstevel@tonic-gate 			tsize = atol(*argv);
2750Sstevel@tonic-gate 			if ((*argv[0] == '-') || (tsize == 0)) {
2760Sstevel@tonic-gate 				msg(gettext(
2770Sstevel@tonic-gate 			    "Tape size must be a positive integer\n"));
2780Sstevel@tonic-gate 				dumpabort();
2790Sstevel@tonic-gate 				/*NOTREACHED*/
2800Sstevel@tonic-gate 			}
2810Sstevel@tonic-gate 		} else {
2820Sstevel@tonic-gate 			msg(gettext("Missing tape size\n"));
2830Sstevel@tonic-gate 			dumpabort();
2840Sstevel@tonic-gate 			/*NOTREACHED*/
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 		break;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	case 't':			/* tracks */
2890Sstevel@tonic-gate 		if (argc > 1) {
2900Sstevel@tonic-gate 			argv++;
2910Sstevel@tonic-gate 			argc--;
2920Sstevel@tonic-gate 			tracks = atoi(*argv);
2930Sstevel@tonic-gate 		} else {
2940Sstevel@tonic-gate 			msg(gettext("Missing track count\n"));
2950Sstevel@tonic-gate 			dumpabort();
2960Sstevel@tonic-gate 			/*NOTREACHED*/
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 		break;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	case 'b':			/* blocks per tape write */
3010Sstevel@tonic-gate 		if (argc > 1) {
3020Sstevel@tonic-gate 			argv++;
3030Sstevel@tonic-gate 			argc--;
3040Sstevel@tonic-gate 			bflag++;
3050Sstevel@tonic-gate 			/*
3060Sstevel@tonic-gate 			 * We save the ntrec in case we need to change
3070Sstevel@tonic-gate 			 * tp_bsize later, we will have to recalculate
3080Sstevel@tonic-gate 			 * it.
3090Sstevel@tonic-gate 			 */
3100Sstevel@tonic-gate 			saved_ntrec = ntrec = atoi(*argv);
3110Sstevel@tonic-gate 			if (ntrec == 0 || (ntrec&1) || ntrec > (MAXNTREC*2)) {
3120Sstevel@tonic-gate 				msg(gettext(
3130Sstevel@tonic-gate 		    "Block size must be a positive, even integer <= %d\n"),
3140Sstevel@tonic-gate 				    MAXNTREC*2);
3150Sstevel@tonic-gate 				dumpabort();
3160Sstevel@tonic-gate 				/*NOTREACHED*/
3170Sstevel@tonic-gate 			}
3180Sstevel@tonic-gate 			ntrec /= (tp_bsize/DEV_BSIZE);
3190Sstevel@tonic-gate 		} else {
3200Sstevel@tonic-gate 			msg(gettext("Missing blocking factor\n"));
3210Sstevel@tonic-gate 			dumpabort();
3220Sstevel@tonic-gate 			/*NOTREACHED*/
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		break;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	case 'c':			/* Tape is cart. not 9-track */
3270Sstevel@tonic-gate 	case 'C':			/* 'C' to be consistent with 'D' */
3280Sstevel@tonic-gate 		cartridge++;
3290Sstevel@tonic-gate 		break;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	case '0':			/* dump level */
3320Sstevel@tonic-gate 	case '1':
3330Sstevel@tonic-gate 	case '2':
3340Sstevel@tonic-gate 	case '3':
3350Sstevel@tonic-gate 	case '4':
3360Sstevel@tonic-gate 	case '5':
3370Sstevel@tonic-gate 	case '6':
3380Sstevel@tonic-gate 	case '7':
3390Sstevel@tonic-gate 	case '8':
3400Sstevel@tonic-gate 	case '9':
3410Sstevel@tonic-gate 		incno = arg[-1];
3420Sstevel@tonic-gate 		break;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	case 'u':			/* update /etc/dumpdates */
3450Sstevel@tonic-gate 		uflag++;
3460Sstevel@tonic-gate 		break;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	case 'n':			/* notify operators */
3490Sstevel@tonic-gate 		notify++;
3500Sstevel@tonic-gate 		break;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	case 'a':			/* create archive file */
3530Sstevel@tonic-gate 		archive = 1;
3540Sstevel@tonic-gate 		if (argc > 1) {
3550Sstevel@tonic-gate 			argv++;
3560Sstevel@tonic-gate 			argc--;
3570Sstevel@tonic-gate 			if (**argv == '\0') {
3580Sstevel@tonic-gate 				msg(gettext("Bad archive file name\n"));
3590Sstevel@tonic-gate 				dumpabort();
3600Sstevel@tonic-gate 				/*NOTREACHED*/
3610Sstevel@tonic-gate 			}
3620Sstevel@tonic-gate 			archivefile = strdup(*argv);
3630Sstevel@tonic-gate 			if (archivefile == NULL) {
3640Sstevel@tonic-gate 				saverr = errno;
3650Sstevel@tonic-gate 				msg(gettext("Cannot allocate memory: %s\n"),
3660Sstevel@tonic-gate 				    strerror(saverr));
3670Sstevel@tonic-gate 				dumpabort();
3680Sstevel@tonic-gate 				/*NOTREACHED*/
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 		} else {
3710Sstevel@tonic-gate 			msg(gettext("Missing archive file name\n"));
3720Sstevel@tonic-gate 			dumpabort();
3730Sstevel@tonic-gate 			/*NOTREACHED*/
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 		break;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	case 'v':
3780Sstevel@tonic-gate 		verify++;
3790Sstevel@tonic-gate 		doingverify++;
3800Sstevel@tonic-gate 		if (strcmp(tape, "-") == 0) {
3810Sstevel@tonic-gate 			msg(gettext(
3820Sstevel@tonic-gate 			"Cannot verify when dumping to standard out.\n"));
3830Sstevel@tonic-gate 			dumpabort();
3840Sstevel@tonic-gate 			/*NOTREACHED*/
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		break;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	case 'D':
3890Sstevel@tonic-gate 		diskette++;
3900Sstevel@tonic-gate 		break;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	case 'N':
3930Sstevel@tonic-gate 		if (argc > 1) {
3940Sstevel@tonic-gate 			argv++;
3950Sstevel@tonic-gate 			argc--;
3960Sstevel@tonic-gate 			if (**argv == '\0') {
3970Sstevel@tonic-gate 				msg(gettext("Missing name for dumpdates "
3980Sstevel@tonic-gate 				    "entry.\n"));
3990Sstevel@tonic-gate 				dumpabort();
4000Sstevel@tonic-gate 				/*NOTREACHED*/
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 			dname = *argv;
4030Sstevel@tonic-gate 			if (strlen(dname) > MAXNAMLEN + 2) {
4040Sstevel@tonic-gate 				msg(gettext("Dumpdates entry name too "
4050Sstevel@tonic-gate 				    "long.\n"));
4060Sstevel@tonic-gate 				dumpabort();
4070Sstevel@tonic-gate 				/*NOTREACHED*/
4080Sstevel@tonic-gate 			}
4090Sstevel@tonic-gate 			for (i = 0; i < strlen(dname); i++) {
4100Sstevel@tonic-gate 				if (isspace(*(dname+i))) {
4110Sstevel@tonic-gate 					msg(gettext("Dumpdates entry name may "
4120Sstevel@tonic-gate 					    "not contain white space.\n"));
4130Sstevel@tonic-gate 					dumpabort();
4140Sstevel@tonic-gate 					/*NOTREACHED*/
4150Sstevel@tonic-gate 				}
4160Sstevel@tonic-gate 			}
4170Sstevel@tonic-gate 		} else {
4180Sstevel@tonic-gate 			msg(gettext("Missing name for dumpdates entry.\n"));
4190Sstevel@tonic-gate 			dumpabort();
4200Sstevel@tonic-gate 			/*NOTREACHED*/
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 		break;
4230Sstevel@tonic-gate 	case 'L':
4240Sstevel@tonic-gate 		if (argc > 1) {
4250Sstevel@tonic-gate 			argv++;
4260Sstevel@tonic-gate 			argc--;
4270Sstevel@tonic-gate 			if (**argv == '\0') {
4280Sstevel@tonic-gate 				msg(gettext("Missing tape label name\n"));
4290Sstevel@tonic-gate 				dumpabort();
4300Sstevel@tonic-gate 				/*NOTREACHED*/
4310Sstevel@tonic-gate 			}
4320Sstevel@tonic-gate 			tlabel = *argv;
4330Sstevel@tonic-gate 			if (strlen(tlabel) > (sizeof (spcl.c_label) - 1)) {
4340Sstevel@tonic-gate 				tlabel[sizeof (spcl.c_label) - 1] = '\0';
4350Sstevel@tonic-gate 				msg(gettext(
4360Sstevel@tonic-gate 		    "Truncating label to maximum supported length: `%s'\n"),
4370Sstevel@tonic-gate 				    tlabel);
4380Sstevel@tonic-gate 			}
4390Sstevel@tonic-gate 		} else {
4400Sstevel@tonic-gate 			msg(gettext("Missing tape label name\n"));
4410Sstevel@tonic-gate 			dumpabort();
4420Sstevel@tonic-gate 			/*NOTREACHED*/
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 		break;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	case 'l':
4470Sstevel@tonic-gate 		autoload++;
4480Sstevel@tonic-gate 		break;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	case 'o':
4510Sstevel@tonic-gate 		offline++;
4520Sstevel@tonic-gate 		break;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	case 'S':
4550Sstevel@tonic-gate 		printsize++;
4560Sstevel@tonic-gate 		break;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate #ifdef DEBUG
4590Sstevel@tonic-gate 	case 'z':
4600Sstevel@tonic-gate 		xflag++;
4610Sstevel@tonic-gate 		break;
4620Sstevel@tonic-gate #endif
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	default:
4650Sstevel@tonic-gate 		msg(gettext("Bad option `%c'\n"), arg[-1]);
4660Sstevel@tonic-gate 		dumpabort();
4670Sstevel@tonic-gate 		/*NOTREACHED*/
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 	if (argc > 1) {
4700Sstevel@tonic-gate 		argv++;
4710Sstevel@tonic-gate 		argc--;
4720Sstevel@tonic-gate 		if (**argv == '\0') {
4730Sstevel@tonic-gate 			msg(gettext("Bad disk name\n"));
4740Sstevel@tonic-gate 			dumpabort();
4750Sstevel@tonic-gate 			/*NOTREACHED*/
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 		disk = *argv;
4780Sstevel@tonic-gate 		disk_dynamic = 0;
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 	if (disk == NULL) {
4810Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4820Sstevel@tonic-gate 	"Usage: %s [0123456789fustdWwnNDCcbavloS [argument]] filesystem\n"),
4830Sstevel@tonic-gate 		    myname);
4840Sstevel@tonic-gate 		Exit(X_ABORT);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 	if (!filenum)
4870Sstevel@tonic-gate 		filenum = 1;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (signal(SIGINT, interrupt) == SIG_IGN)
4900Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if (strcmp(tape, "-") == 0) {
4930Sstevel@tonic-gate 		pipeout++;
4940Sstevel@tonic-gate 		tape = gettext("standard output");
4950Sstevel@tonic-gate 		dumpdev = sdumpdev = strdup(tape);
4960Sstevel@tonic-gate 		if (dumpdev == NULL) {
4970Sstevel@tonic-gate 			saverr = errno;
4980Sstevel@tonic-gate 			msg(gettext("Cannot allocate memory: %s\n"),
4990Sstevel@tonic-gate 			    strerror(saverr));
5000Sstevel@tonic-gate 			dumpabort();
5010Sstevel@tonic-gate 			/*NOTREACHED*/
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 		/*CONSTANTCONDITION*/
5040Sstevel@tonic-gate 		assert(sizeof (spcl.c_label) > 5);
5050Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, "none");
5060Sstevel@tonic-gate 	} else if (*tape == '+') {
5070Sstevel@tonic-gate 		nextdevice();
5080Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, tlabel);
5090Sstevel@tonic-gate 	} else {
5100Sstevel@tonic-gate 		/* if not already set, set diskette to default */
5110Sstevel@tonic-gate 		if (diskette && strcmp(tape, DEFTAPE) == 0)
5120Sstevel@tonic-gate 			tape = DISKETTE;
5130Sstevel@tonic-gate 		nextdevice();
5140Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, tlabel);
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 	if (cartridge && diskette) {
5170Sstevel@tonic-gate 		error = 1;
5180Sstevel@tonic-gate 		msg(gettext("Cannot select both cartridge and diskette\n"));
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	if (density && diskette) {
5210Sstevel@tonic-gate 		error = 1;
5220Sstevel@tonic-gate 		msg(gettext("Cannot select density of diskette\n"));
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 	if (tracks && diskette) {
5250Sstevel@tonic-gate 		error = 1;
5260Sstevel@tonic-gate 		msg(gettext("Cannot select number of tracks of diskette\n"));
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	if (error) {
5290Sstevel@tonic-gate 		dumpabort();
5300Sstevel@tonic-gate 		/*NOTREACHED*/
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/*
5340Sstevel@tonic-gate 	 * Determine how to default tape size and density
5350Sstevel@tonic-gate 	 *
5360Sstevel@tonic-gate 	 *		density				tape size
5370Sstevel@tonic-gate 	 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
5380Sstevel@tonic-gate 	 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
5390Sstevel@tonic-gate 	 *
5400Sstevel@tonic-gate 	 * Most Sun-2's came with 4 track (20MB) cartridge tape drives,
5410Sstevel@tonic-gate 	 * while most other machines (Sun-3's and non-Sun's) come with
5420Sstevel@tonic-gate 	 * 9 track (45MB) cartridge tape drives.  Some Sun-2's came with
5430Sstevel@tonic-gate 	 * 9 track drives, but there is no way for the software to detect
5440Sstevel@tonic-gate 	 * which drive type is installed.  Sigh...  We make the gross
5450Sstevel@tonic-gate 	 * assumption that #ifdef mc68010 will test for a Sun-2.
5460Sstevel@tonic-gate 	 *
5470Sstevel@tonic-gate 	 * cartridge	8000 bpi (100 bytes/.1")	425 * tracks ft.
5480Sstevel@tonic-gate 	 */
5490Sstevel@tonic-gate 	if (density == 0)
5500Sstevel@tonic-gate 		density = cartridge ? 100 : 625;
5510Sstevel@tonic-gate 	if (tracks == 0)
5520Sstevel@tonic-gate 		tracks = 9;
5530Sstevel@tonic-gate 	if (!bflag) {
5540Sstevel@tonic-gate 		if (cartridge)
5550Sstevel@tonic-gate 			ntrec = CARTRIDGETREC;
5560Sstevel@tonic-gate 		else if (diskette)
5570Sstevel@tonic-gate 			ntrec = NTREC;
5580Sstevel@tonic-gate 		else if (density >= 625)
5590Sstevel@tonic-gate 			ntrec = HIGHDENSITYTREC;
5600Sstevel@tonic-gate 		else
5610Sstevel@tonic-gate 			ntrec = NTREC;
5620Sstevel@tonic-gate 		/*
5630Sstevel@tonic-gate 		 * save ntrec in case we have to change tp_bsize later.
5640Sstevel@tonic-gate 		 */
5650Sstevel@tonic-gate 		saved_ntrec = (ntrec * (tp_bsize/DEV_BSIZE));
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 	if (!diskette) {
5680Sstevel@tonic-gate 		tsize *= 12L*10L;
5690Sstevel@tonic-gate 		if (cartridge)
5700Sstevel@tonic-gate 			tsize *= tracks;
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 	rmtinit(msg, Exit);
5730Sstevel@tonic-gate 	if (host) {
5740Sstevel@tonic-gate 		char	*cp = strchr(host, '@');
5750Sstevel@tonic-gate 		if (cp == (char *)0)
5760Sstevel@tonic-gate 			cp = host;
5770Sstevel@tonic-gate 		else
5780Sstevel@tonic-gate 			cp++;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 		if (rmthost(host, ntrec) == 0) {
5810Sstevel@tonic-gate 			msg(gettext("Cannot connect to tape host `%s'\n"), cp);
5820Sstevel@tonic-gate 			dumpabort();
5830Sstevel@tonic-gate 			/*NOTREACHED*/
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	if (signal(SIGHUP, sigAbort) == SIG_IGN)
5870Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_IGN);
5880Sstevel@tonic-gate 	if (signal(SIGTRAP, sigAbort) == SIG_IGN)
5890Sstevel@tonic-gate 		(void) signal(SIGTRAP, SIG_IGN);
5900Sstevel@tonic-gate 	if (signal(SIGFPE, sigAbort) == SIG_IGN)
5910Sstevel@tonic-gate 		(void) signal(SIGFPE, SIG_IGN);
5920Sstevel@tonic-gate 	if (signal(SIGBUS, sigAbort) == SIG_IGN)
5930Sstevel@tonic-gate 		(void) signal(SIGBUS, SIG_IGN);
5940Sstevel@tonic-gate 	if (signal(SIGSEGV, sigAbort) == SIG_IGN)
5950Sstevel@tonic-gate 		(void) signal(SIGSEGV, SIG_IGN);
5960Sstevel@tonic-gate 	if (signal(SIGTERM, sigAbort) == SIG_IGN)
5970Sstevel@tonic-gate 		(void) signal(SIGTERM, SIG_IGN);
5980Sstevel@tonic-gate 	if (signal(SIGUSR1, sigAbort) == SIG_IGN)
5990Sstevel@tonic-gate 		(void) signal(SIGUSR1, SIG_IGN);
6000Sstevel@tonic-gate 	if (signal(SIGPIPE, sigAbort) == SIG_IGN)
6010Sstevel@tonic-gate 		(void) signal(SIGPIPE, SIG_IGN);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	mnttabread();		/* /etc/fstab, /etc/mtab snarfed */
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/*
6060Sstevel@tonic-gate 	 *	disk can be either the full special file name,
6070Sstevel@tonic-gate 	 *	the suffix of the special file name,
6080Sstevel@tonic-gate 	 *	the special name missing the leading '/',
6090Sstevel@tonic-gate 	 *	the file system name with or without the leading '/'.
6100Sstevel@tonic-gate 	 *	NB:  we attempt to avoid dumping the block device
6110Sstevel@tonic-gate 	 *	(using rawname) because specfs and the vm system
6120Sstevel@tonic-gate 	 *	are not necessarily in sync.
6130Sstevel@tonic-gate 	 */
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/*
616*6284Svk154806 	 * Attempt to roll the log if its root user before doing the dump.
617*6284Svk154806 	 * There's nothing the user can do if we are unable to roll the log,
618*6284Svk154806 	 * so we'll silently ignore failures.
6190Sstevel@tonic-gate 	 */
620*6284Svk154806 	if (getuid() == 0 && rl_roll_log(disk) != RL_SUCCESS &&
621*6284Svk154806 	    disk[0] != '/') {
6220Sstevel@tonic-gate 		/* Try it again with leading '/'. */
6230Sstevel@tonic-gate 		char	*slashed;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		slashed = (char *)malloc(strlen(disk) + 2);
6260Sstevel@tonic-gate 		if (slashed != (char *)NULL) {
6270Sstevel@tonic-gate 			(void) sprintf(slashed, "%c%s", '/', disk);
6280Sstevel@tonic-gate 			(void) rl_roll_log(slashed);
6290Sstevel@tonic-gate 			free(slashed);
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 	dt = mnttabsearch(disk, 0);
6330Sstevel@tonic-gate 	if (dt != 0) {
6340Sstevel@tonic-gate 		filesystem = dt->mnt_mountp;
6350Sstevel@tonic-gate 		if (disk_dynamic) {
6360Sstevel@tonic-gate 			/* LINTED: disk is not NULL */
6370Sstevel@tonic-gate 			free(disk);
6380Sstevel@tonic-gate 		}
6390Sstevel@tonic-gate 		disk = rawname(dt->mnt_special);
6400Sstevel@tonic-gate 		disk_dynamic = (disk != dt->mnt_special);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		(void) strncpy(spcl.c_dev, dt->mnt_special,
6430Sstevel@tonic-gate 		    sizeof (spcl.c_dev));
6440Sstevel@tonic-gate 		spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0';
6450Sstevel@tonic-gate 		(void) strncpy(spcl.c_filesys, dt->mnt_mountp,
6460Sstevel@tonic-gate 		    sizeof (spcl.c_filesys));
6470Sstevel@tonic-gate 		spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
6480Sstevel@tonic-gate 	} else {
6490Sstevel@tonic-gate 		(void) strncpy(spcl.c_dev, disk, sizeof (spcl.c_dev));
6500Sstevel@tonic-gate 		spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0';
6510Sstevel@tonic-gate #ifdef PARTIAL
6520Sstevel@tonic-gate 		/* check for partial filesystem dump */
6530Sstevel@tonic-gate 		partial_check();
6540Sstevel@tonic-gate 		dt = mnttabsearch(disk, 1);
6550Sstevel@tonic-gate 		if (dt != 0) {
6560Sstevel@tonic-gate 			filesystem = dt->mnt_mountp;
6570Sstevel@tonic-gate 			if (disk_dynamic)
6580Sstevel@tonic-gate 				free(disk);
6590Sstevel@tonic-gate 			disk = rawname(dt->mnt_special);
6600Sstevel@tonic-gate 			disk_dynamic = (disk != dt->mnt_special);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 			(void) strncpy(spcl.c_filesys,
6630Sstevel@tonic-gate 			    "a partial file system", sizeof (spcl.c_filesys));
6640Sstevel@tonic-gate 			spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 		else
6670Sstevel@tonic-gate #endif /* PARTIAL */
6680Sstevel@tonic-gate 		{
6690Sstevel@tonic-gate 			char *old_disk = disk;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 			(void) strncpy(spcl.c_filesys,
6720Sstevel@tonic-gate 			    "an unlisted file system",
6730Sstevel@tonic-gate 			    sizeof (spcl.c_filesys));
6740Sstevel@tonic-gate 			spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 			disk = rawname(old_disk);
6770Sstevel@tonic-gate 			if (disk != old_disk) {
6780Sstevel@tonic-gate 				if (disk_dynamic)
6790Sstevel@tonic-gate 					free(old_disk);
6800Sstevel@tonic-gate 				disk_dynamic = 1;
6810Sstevel@tonic-gate 			}
6820Sstevel@tonic-gate 			/*
6830Sstevel@tonic-gate 			 * If disk == old_disk, then disk_dynamic's state
6840Sstevel@tonic-gate 			 * does not change.
6850Sstevel@tonic-gate 			 */
6860Sstevel@tonic-gate 		}
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	fi = open64(disk, O_RDONLY);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	if (fi < 0) {
6920Sstevel@tonic-gate 		saverr = errno;
6930Sstevel@tonic-gate 		msg(gettext("Cannot open dump device `%s': %s\n"),
6945299Sws195443 		    disk, strerror(saverr));
6950Sstevel@tonic-gate 		Exit(X_ABORT);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if (sscanf(&incno, "%1d", &spcl.c_level) != 1) {
6990Sstevel@tonic-gate 		msg(gettext("Bad dump level `%c' specified\n"), incno);
7000Sstevel@tonic-gate 		dumpabort();
7010Sstevel@tonic-gate 		/*NOTREACHED*/
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 	getitime();		/* /etc/dumpdates snarfed */
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	sblock = (struct fs *)&sblock_buf;
7060Sstevel@tonic-gate 	sync();
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE);
7090Sstevel@tonic-gate 	if ((sblock->fs_magic != FS_MAGIC) &&
7100Sstevel@tonic-gate 	    (sblock->fs_magic != MTB_UFS_MAGIC)) {
7110Sstevel@tonic-gate 		msg(gettext(
7120Sstevel@tonic-gate 	    "Warning - super-block on device `%s' is corrupt - run fsck\n"),
7130Sstevel@tonic-gate 		    disk);
7140Sstevel@tonic-gate 		dumpabort();
7150Sstevel@tonic-gate 		/*NOTREACHED*/
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
718757Svsakar 	if (sblock->fs_magic == FS_MAGIC &&
719757Svsakar 	    (sblock->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
720757Svsakar 	    sblock->fs_version != UFS_VERSION_MIN)) {
721757Svsakar 		msg(gettext("Unrecognized UFS version: %d\n"),
722757Svsakar 		    sblock->fs_version);
723757Svsakar 		dumpabort();
724757Svsakar 		/*NOTREACHED*/
725757Svsakar 	}
726757Svsakar 
7270Sstevel@tonic-gate 	if (sblock->fs_magic == MTB_UFS_MAGIC &&
7280Sstevel@tonic-gate 	    (sblock->fs_version < MTB_UFS_VERSION_MIN ||
7290Sstevel@tonic-gate 	    sblock->fs_version > MTB_UFS_VERSION_1)) {
7300Sstevel@tonic-gate 		msg(gettext("Unrecognized UFS version: %d\n"),
7310Sstevel@tonic-gate 		    sblock->fs_version);
7320Sstevel@tonic-gate 		dumpabort();
7330Sstevel@tonic-gate 		/*NOTREACHED*/
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Try to set up for using mmap(2).  It only works on the block
7380Sstevel@tonic-gate 	 * device, but if we can use it, things go somewhat faster.  If
7390Sstevel@tonic-gate 	 * we can't open it, we'll silently fall back to the old method
7400Sstevel@tonic-gate 	 * (read/memcpy). We also only try this if it's been cleanly
7410Sstevel@tonic-gate 	 * unmounted. Dumping a live filesystem this way runs into
7420Sstevel@tonic-gate 	 * buffer consistency problems. Of course, we don't support
7430Sstevel@tonic-gate 	 * running dump on a mounted filesystem, but some people do it
7440Sstevel@tonic-gate 	 * anyway.
7450Sstevel@tonic-gate 	 */
7460Sstevel@tonic-gate 	if (sblock->fs_clean == FSCLEAN) {
7470Sstevel@tonic-gate 		char *block = unrawname(disk);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 		if (block != NULL) {
7500Sstevel@tonic-gate 			mapfd = open(block, O_RDONLY, 0);
7510Sstevel@tonic-gate 			free(block);
7520Sstevel@tonic-gate 		}
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate restart:
7560Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE);
7570Sstevel@tonic-gate 	if ((sblock->fs_magic != FS_MAGIC) &&
7580Sstevel@tonic-gate 	    (sblock->fs_magic != MTB_UFS_MAGIC)) {	/* paranoia */
7590Sstevel@tonic-gate 		msg(gettext("bad super-block magic number, run fsck\n"));
7600Sstevel@tonic-gate 		dumpabort();
7610Sstevel@tonic-gate 		/*NOTREACHED*/
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 
764757Svsakar 	if (sblock->fs_magic == FS_MAGIC &&
765757Svsakar 	    (sblock->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
766757Svsakar 	    sblock->fs_version != UFS_VERSION_MIN)) {
767757Svsakar 		msg(gettext("Unrecognized UFS version: %d\n"),
768757Svsakar 		    sblock->fs_version);
769757Svsakar 		dumpabort();
770757Svsakar 		/*NOTREACHED*/
771757Svsakar 	}
772757Svsakar 
7730Sstevel@tonic-gate 	if (sblock->fs_magic == MTB_UFS_MAGIC &&
7740Sstevel@tonic-gate 	    (sblock->fs_version < MTB_UFS_VERSION_MIN ||
7750Sstevel@tonic-gate 	    sblock->fs_version > MTB_UFS_VERSION_1)) {
7760Sstevel@tonic-gate 		msg(gettext("Unrecognized UFS version: %d\n"),
7770Sstevel@tonic-gate 		    sblock->fs_version);
7780Sstevel@tonic-gate 		dumpabort();
7790Sstevel@tonic-gate 		/*NOTREACHED*/
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	if (!doingactive)
7830Sstevel@tonic-gate 		allocino();
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/* XXX should sanity-check the super block before trusting/using it */
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/* LINTED XXX time truncated - tolerate until tape format changes */
7880Sstevel@tonic-gate 	spcl.c_date = (time32_t)time((time_t *)NULL);
7890Sstevel@tonic-gate 	bcopy(&(spcl.c_shadow), c_shadow_save, sizeof (c_shadow_save));
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	snapdate = is_fssnap_dump(disk);
7920Sstevel@tonic-gate 	if (snapdate)
7930Sstevel@tonic-gate 		spcl.c_date = snapdate;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (!printsize) {
7960Sstevel@tonic-gate 		msg(gettext("Date of this level %c dump: %s\n"),
7970Sstevel@tonic-gate 		    incno, prdate(spcl.c_date));
7980Sstevel@tonic-gate 		msg(gettext("Date of last level %c dump: %s\n"),
7995299Sws195443 		    (uchar_t)lastincno, prdate(spcl.c_ddate));
8000Sstevel@tonic-gate 		msg(gettext("Dumping %s "), disk);
8010Sstevel@tonic-gate 		if (filesystem != 0)
8020Sstevel@tonic-gate 			msgtail("(%.*s:%s) ",
8030Sstevel@tonic-gate 			    /* LINTED unsigned -> signed cast ok */
8040Sstevel@tonic-gate 			    (int)sizeof (spcl.c_host), spcl.c_host, filesystem);
8050Sstevel@tonic-gate 		msgtail(gettext("to %s.\n"), sdumpdev);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	esize = f_esize = o_esize = 0;
8090Sstevel@tonic-gate 	msiz = roundup(d_howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
8105299Sws195443 	    TP_BSIZE_MAX);
8110Sstevel@tonic-gate 	if (!doingactive) {
8120Sstevel@tonic-gate 		clrmap = (uchar_t *)xcalloc(msiz, sizeof (*clrmap));
8130Sstevel@tonic-gate 		filmap = (uchar_t *)xcalloc(msiz, sizeof (*filmap));
8140Sstevel@tonic-gate 		dirmap = (uchar_t *)xcalloc(msiz, sizeof (*dirmap));
8150Sstevel@tonic-gate 		nodmap = (uchar_t *)xcalloc(msiz, sizeof (*nodmap));
8160Sstevel@tonic-gate 		shamap = (uchar_t *)xcalloc(msiz, sizeof (*shamap));
8170Sstevel@tonic-gate 		activemap = (uchar_t *)xcalloc(msiz, sizeof (*activemap));
8180Sstevel@tonic-gate 	} else {
8190Sstevel@tonic-gate 		if (clrmap == NULL || filmap == NULL || dirmap == NULL ||
8200Sstevel@tonic-gate 		    nodmap == NULL || shamap == NULL || activemap == NULL) {
8210Sstevel@tonic-gate 			msg(gettext(
8220Sstevel@tonic-gate 	    "Internal error: NULL map pointer while re-dumping active files"));
8230Sstevel@tonic-gate 			dumpabort();
8240Sstevel@tonic-gate 			/*NOTREACHED*/
8250Sstevel@tonic-gate 		}
8260Sstevel@tonic-gate 		bzero(clrmap, msiz);
8270Sstevel@tonic-gate 		bzero(filmap, msiz);
8280Sstevel@tonic-gate 		bzero(dirmap, msiz);
8290Sstevel@tonic-gate 		bzero(nodmap, msiz);
8300Sstevel@tonic-gate 		bzero(shamap, msiz);
8310Sstevel@tonic-gate 		/* retain active map */
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	dumpstate = DS_INIT;
8350Sstevel@tonic-gate 	dumptoarchive = 1;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/*
8380Sstevel@tonic-gate 	 * Read cylinder group inode-used bitmaps to avoid reading clear inodes.
8390Sstevel@tonic-gate 	 */
8400Sstevel@tonic-gate 	{
8410Sstevel@tonic-gate 		uchar_t *clrp = clrmap;
8420Sstevel@tonic-gate 		struct cg *cgp =
8430Sstevel@tonic-gate 		    (struct cg *)xcalloc((uint_t)sblock->fs_cgsize, 1);
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		for (i = 0; i < sblock->fs_ncg; i++) {
8460Sstevel@tonic-gate 			bread(fsbtodb(sblock, cgtod(sblock, i)),
8470Sstevel@tonic-gate 			    (uchar_t *)cgp, sblock->fs_cgsize);
8480Sstevel@tonic-gate 			bcopy(cg_inosused(cgp), clrp,
8490Sstevel@tonic-gate 			    (int)sblock->fs_ipg / NBBY);
8500Sstevel@tonic-gate 			clrp += sblock->fs_ipg / NBBY;
8510Sstevel@tonic-gate 		}
8520Sstevel@tonic-gate 		free((char *)cgp);
8530Sstevel@tonic-gate 		/* XXX right-shift clrmap one bit.  why? */
8540Sstevel@tonic-gate 		for (i = 0; clrp > clrmap; i <<= NBBY) {
8550Sstevel@tonic-gate 			i |= *--clrp & ((1<<NBBY) - 1);
8560Sstevel@tonic-gate 			*clrp = i >> 1;
8570Sstevel@tonic-gate 		}
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	if (!printsize) {
8610Sstevel@tonic-gate 		msgp = gettext("Mapping (Pass I) [regular files]\n");
8620Sstevel@tonic-gate 		msg(msgp);
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	ino = 0;
8660Sstevel@tonic-gate #ifdef PARTIAL
8670Sstevel@tonic-gate 	if (partial_mark(argc, argv)) {
8680Sstevel@tonic-gate #endif /* PARTIAL */
8690Sstevel@tonic-gate 		if (!doingactive)
8700Sstevel@tonic-gate 			pass(mark, clrmap);	/* mark updates 'x'_esize */
8710Sstevel@tonic-gate 		else
8720Sstevel@tonic-gate 			pass(active_mark, clrmap);	/* updates 'x'_esize */
8730Sstevel@tonic-gate #ifdef PARTIAL
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate #endif /* PARTIAL */
8760Sstevel@tonic-gate 	do {
8770Sstevel@tonic-gate 		if (!printsize) {
8780Sstevel@tonic-gate 			msgp = gettext("Mapping (Pass II) [directories]\n");
8790Sstevel@tonic-gate 			msg(msgp);
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 		nadded = 0;
8820Sstevel@tonic-gate 		ino = 0;
8830Sstevel@tonic-gate 		pass(add, dirmap);
8840Sstevel@tonic-gate 	} while (nadded);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	ino = 0; /* adjust estimated size for shadow inodes */
8870Sstevel@tonic-gate 	pass(markshad, nodmap);
8880Sstevel@tonic-gate 	ino = 0;
8890Sstevel@tonic-gate 	pass(estshad, shamap);
8900Sstevel@tonic-gate 	freeshad();
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	bmapest(clrmap);
8930Sstevel@tonic-gate 	bmapest(nodmap);
8940Sstevel@tonic-gate 	esize = o_esize + f_esize;
8950Sstevel@tonic-gate 	if (diskette) {
8960Sstevel@tonic-gate 		/* estimate number of floppies */
8970Sstevel@tonic-gate 		if (tsize != 0)
8980Sstevel@tonic-gate 			fetapes = (double)(esize + ntrec) / (double)tsize;
8990Sstevel@tonic-gate 	} else if (cartridge) {
9000Sstevel@tonic-gate 		/*
9010Sstevel@tonic-gate 		 * Estimate number of tapes, assuming streaming stops at
9020Sstevel@tonic-gate 		 * the end of each block written, and not in mid-block.
9030Sstevel@tonic-gate 		 * Assume no erroneous blocks; this can be compensated for
9040Sstevel@tonic-gate 		 * with an artificially low tape size.
9050Sstevel@tonic-gate 		 */
9060Sstevel@tonic-gate 		tenthsperirg = 16;	/* actually 15.48, says Archive */
9070Sstevel@tonic-gate 		if (tsize != 0)
9080Sstevel@tonic-gate 			fetapes = ((double)esize /* blocks */
9090Sstevel@tonic-gate 			    * (tp_bsize		/* bytes/block */
9100Sstevel@tonic-gate 			    * (1.0/density))	/* 0.1" / byte */
9110Sstevel@tonic-gate 			    +
9120Sstevel@tonic-gate 			    (double)esize	/* blocks */
9130Sstevel@tonic-gate 			    * (1.0/ntrec)	/* streaming-stops per block */
9140Sstevel@tonic-gate 			    * tenthsperirg)	/* 0.1" / streaming-stop */
9150Sstevel@tonic-gate 			    * (1.0 / tsize);	/* tape / 0.1" */
9160Sstevel@tonic-gate 	} else {
9170Sstevel@tonic-gate 		/* Estimate number of tapes, for old fashioned 9-track tape */
9180Sstevel@tonic-gate #ifdef sun
9190Sstevel@tonic-gate 		/* sun has long irg's */
9200Sstevel@tonic-gate 		tenthsperirg = (density == 625) ? 6 : 12;
9210Sstevel@tonic-gate #else
9220Sstevel@tonic-gate 		tenthsperirg = (density == 625) ? 5 : 8;
9230Sstevel@tonic-gate #endif
9240Sstevel@tonic-gate 		if (tsize != 0)
9250Sstevel@tonic-gate 			fetapes = ((double)esize /* blocks */
9260Sstevel@tonic-gate 			    * (tp_bsize		/* bytes / block */
9270Sstevel@tonic-gate 			    * (1.0/density))	/* 0.1" / byte */
9280Sstevel@tonic-gate 			    +
9290Sstevel@tonic-gate 			    (double)esize	/* blocks */
9300Sstevel@tonic-gate 			    * (1.0/ntrec)	/* IRG's / block */
9310Sstevel@tonic-gate 			    * tenthsperirg)	/* 0.1" / IRG */
9320Sstevel@tonic-gate 			    * (1.0 / tsize);	/* tape / 0.1" */
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	etapes = fetapes;	/* truncating assignment */
9360Sstevel@tonic-gate 	etapes++;
9370Sstevel@tonic-gate 	/* count the nodemap on each additional tape */
9380Sstevel@tonic-gate 	for (i = 1; i < etapes; i++)
9390Sstevel@tonic-gate 		bmapest(nodmap);
9400Sstevel@tonic-gate 	/*
9410Sstevel@tonic-gate 	 * If the above bmapest is called, it changes o_esize and f_esize.
9420Sstevel@tonic-gate 	 * So we will recalculate esize here anyway to make sure.
9430Sstevel@tonic-gate 	 * Also, add tape headers and trailer records.
9440Sstevel@tonic-gate 	 */
9450Sstevel@tonic-gate 	esize = o_esize + f_esize + etapes + ntrec;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	/*
9480Sstevel@tonic-gate 	 * If the estimated number of tp_bsize tape blocks is greater than
9490Sstevel@tonic-gate 	 * INT_MAX we have to adjust tp_bsize and ntrec to handle
9500Sstevel@tonic-gate 	 * the larger dump.  esize is an estimate, so we 'fudge'
9510Sstevel@tonic-gate 	 * INT_MAX a little.  If tp_bsize is adjusted, it will be adjusted
9520Sstevel@tonic-gate 	 * to the size needed for this dump (2048, 4096, 8192, ...)
9530Sstevel@tonic-gate 	 */
9540Sstevel@tonic-gate 	if (esize > (INT_MAX - FUDGE_FACTOR)) { /* esize is too big */
9550Sstevel@tonic-gate 		forceflag++;
9560Sstevel@tonic-gate 		esize_shift =
9570Sstevel@tonic-gate 		    ((esize + (INT_MAX - FUDGE_FACTOR) - 1)/
9580Sstevel@tonic-gate 		    ((u_offset_t)(INT_MAX - FUDGE_FACTOR))) - 1;
9590Sstevel@tonic-gate 		if ((esize_shift > ESIZE_SHIFT_MAX) || (ntrec == 0)) {
9600Sstevel@tonic-gate 			msgp = gettext(
9610Sstevel@tonic-gate 	"Block factor %d ('b' flag) is too small for this size dump.");
9620Sstevel@tonic-gate 			msg(msgp, saved_ntrec);
9630Sstevel@tonic-gate 			dumpabort();
9640Sstevel@tonic-gate 			/*NOTREACHED*/
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 		/*
9670Sstevel@tonic-gate 		 * recalculate esize from:
9680Sstevel@tonic-gate 		 * o_esize - header tape records
9690Sstevel@tonic-gate 		 * (f_esize + (num_mult -1)) >> esize_shift - new non-header
9700Sstevel@tonic-gate 		 *	tape records for files/maps
9710Sstevel@tonic-gate 		 * etapes - TS_TAPE records
9720Sstevel@tonic-gate 		 * ntrec - TS_END records
9730Sstevel@tonic-gate 		 *
9740Sstevel@tonic-gate 		 * ntrec is adjusted so a tape record is still 'b' flag
9750Sstevel@tonic-gate 		 * number of DEV_BSIZE (512) in size
9760Sstevel@tonic-gate 		 */
9770Sstevel@tonic-gate 		new_mult = (tp_bsize << esize_shift)/tp_bsize;
9780Sstevel@tonic-gate 		tp_bsize = (tp_bsize << esize_shift);
9790Sstevel@tonic-gate 		esize = o_esize + ((f_esize +
9800Sstevel@tonic-gate 		    (new_mult - 1)) >> esize_shift) + etapes + ntrec;
9810Sstevel@tonic-gate 		ntrec = (saved_ntrec/(tp_bsize/DEV_BSIZE));
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 	if (forceflag != 0) {
9840Sstevel@tonic-gate 		msgp = gettext(
9850Sstevel@tonic-gate 		    "Forcing larger tape block size (%d).\n");
9860Sstevel@tonic-gate 		msg(msgp, tp_bsize);
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 	alloctape();			/* allocate tape buffers */
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	assert((tp_bsize / DEV_BSIZE != 0) && (tp_bsize % DEV_BSIZE == 0));
9910Sstevel@tonic-gate 	/*
9920Sstevel@tonic-gate 	 * If all we wanted was the size estimate,
9930Sstevel@tonic-gate 	 * just print it out and exit.
9940Sstevel@tonic-gate 	 */
9950Sstevel@tonic-gate 	if (printsize) {
9960Sstevel@tonic-gate 		(void) printf("%llu\n", esize * tp_bsize);
9970Sstevel@tonic-gate 		Exit(0);
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	if (tsize != 0) {
10010Sstevel@tonic-gate 		if (diskette)
10020Sstevel@tonic-gate 			msgp = gettext(
10030Sstevel@tonic-gate 			    "Estimated %lld blocks (%s) on %3.2f diskettes.\n");
10040Sstevel@tonic-gate 		else
10050Sstevel@tonic-gate 			msgp = gettext(
10060Sstevel@tonic-gate 			    "Estimated %lld blocks (%s) on %3.2f tapes.\n");
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 		msg(msgp,
10090Sstevel@tonic-gate 		    (esize*(tp_bsize/DEV_BSIZE)), mb(esize), fetapes);
10100Sstevel@tonic-gate 	} else {
10110Sstevel@tonic-gate 		msgp = gettext("Estimated %lld blocks (%s).\n");
10120Sstevel@tonic-gate 		msg(msgp, (esize*(tp_bsize/DEV_BSIZE)), mb(esize));
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	dumpstate = DS_CLRI;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	otape(1);			/* bitmap is the first to tape write */
10180Sstevel@tonic-gate 	*telapsed = 0;
10190Sstevel@tonic-gate 	(void) time(tstart_writing);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	/* filmap indicates all non-directory inodes */
10220Sstevel@tonic-gate 	{
10230Sstevel@tonic-gate 		uchar_t *np, *fp, *dp;
10240Sstevel@tonic-gate 		np = nodmap;
10250Sstevel@tonic-gate 		dp = dirmap;
10260Sstevel@tonic-gate 		fp = filmap;
10270Sstevel@tonic-gate 		for (i = 0; i < msiz; i++)
10280Sstevel@tonic-gate 			*fp++ = *np++ ^ *dp++;
10290Sstevel@tonic-gate 	}
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	while (dumpstate != DS_DONE) {
10320Sstevel@tonic-gate 		/*
10330Sstevel@tonic-gate 		 * When we receive EOT notification from
10340Sstevel@tonic-gate 		 * the writer, the signal handler calls
10350Sstevel@tonic-gate 		 * rollforward and then jumps here.
10360Sstevel@tonic-gate 		 */
10370Sstevel@tonic-gate 		(void) setjmp(checkpoint_buf);
10380Sstevel@tonic-gate 		switch (dumpstate) {
10390Sstevel@tonic-gate 		case DS_INIT:
10400Sstevel@tonic-gate 			/*
10410Sstevel@tonic-gate 			 * We get here if a tape error occurred
10420Sstevel@tonic-gate 			 * after releasing the name lock but before
10430Sstevel@tonic-gate 			 * the volume containing the last of the
10440Sstevel@tonic-gate 			 * dir info was completed.  We have to start
10450Sstevel@tonic-gate 			 * all over in this case.
10460Sstevel@tonic-gate 			 */
10470Sstevel@tonic-gate 			{
10480Sstevel@tonic-gate 				char *rmsg = gettext(
10490Sstevel@tonic-gate 		"Warning - output error occurred after releasing name lock\n\
10500Sstevel@tonic-gate \tThe dump will restart\n");
10510Sstevel@tonic-gate 				msg(rmsg);
10520Sstevel@tonic-gate 				goto restart;
10530Sstevel@tonic-gate 			}
10540Sstevel@tonic-gate 			/* NOTREACHED */
10550Sstevel@tonic-gate 		case DS_START:
10560Sstevel@tonic-gate 		case DS_CLRI:
10570Sstevel@tonic-gate 			ino = UFSROOTINO;
10580Sstevel@tonic-gate 			dumptoarchive = 1;
10590Sstevel@tonic-gate 			bitmap(clrmap, TS_CLRI);
10600Sstevel@tonic-gate 			nextstate(DS_BITS);
10610Sstevel@tonic-gate 			/* FALLTHROUGH */
10620Sstevel@tonic-gate 		case DS_BITS:
10630Sstevel@tonic-gate 			ino = UFSROOTINO;
10640Sstevel@tonic-gate 			dumptoarchive = 1;
10650Sstevel@tonic-gate 			if (BIT(UFSROOTINO, nodmap))	/* empty dump check */
10660Sstevel@tonic-gate 				bitmap(nodmap, TS_BITS);
10670Sstevel@tonic-gate 			nextstate(DS_DIRS);
10680Sstevel@tonic-gate 			if (!doingverify) {
10690Sstevel@tonic-gate 				msgp = gettext(
10705299Sws195443 				    "Dumping (Pass III) [directories]\n");
10710Sstevel@tonic-gate 				msg(msgp);
10720Sstevel@tonic-gate 			}
10730Sstevel@tonic-gate 			/* FALLTHROUGH */
10740Sstevel@tonic-gate 		case DS_DIRS:
10750Sstevel@tonic-gate 			dumptoarchive = 1;
10760Sstevel@tonic-gate 			pass(dirdump, dirmap);
10770Sstevel@tonic-gate 			nextstate(DS_FILES);
10780Sstevel@tonic-gate 			if (!doingverify) {
10790Sstevel@tonic-gate 				msgp = gettext(
10805299Sws195443 				    "Dumping (Pass IV) [regular files]\n");
10810Sstevel@tonic-gate 				msg(msgp);
10820Sstevel@tonic-gate 			}
10830Sstevel@tonic-gate 			/* FALLTHROUGH */
10840Sstevel@tonic-gate 		case DS_FILES:
10850Sstevel@tonic-gate 			dumptoarchive = 0;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 			pass(lf_dump, filmap);
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 			flushcmds();
10900Sstevel@tonic-gate 			dumpstate = DS_END;	/* don't reset ino */
10910Sstevel@tonic-gate 			/* FALLTHROUGH */
10920Sstevel@tonic-gate 		case DS_END:
10930Sstevel@tonic-gate 			dumptoarchive = 1;
10940Sstevel@tonic-gate 			spcl.c_type = TS_END;
10950Sstevel@tonic-gate 			for (i = 0; i < ntrec; i++) {
10960Sstevel@tonic-gate 				spclrec();
10970Sstevel@tonic-gate 			}
10980Sstevel@tonic-gate 			flusht();
10990Sstevel@tonic-gate 			break;
11000Sstevel@tonic-gate 		case DS_DONE:
11010Sstevel@tonic-gate 			break;
11020Sstevel@tonic-gate 		default:
11030Sstevel@tonic-gate 			msg(gettext("Internal state error\n"));
11040Sstevel@tonic-gate 			dumpabort();
11050Sstevel@tonic-gate 			/*NOTREACHED*/
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	if ((! doingactive) && (! active))
11100Sstevel@tonic-gate 		trewind();
11110Sstevel@tonic-gate 	if (verify && !doingverify) {
11120Sstevel@tonic-gate 		msgp = gettext("Finished writing last dump volume\n");
11130Sstevel@tonic-gate 		msg(msgp);
11140Sstevel@tonic-gate 		Exit(X_VERIFY);
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 	if (spcl.c_volume > 1)
11170Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
11180Sstevel@tonic-gate 		    gettext("%lld blocks (%s) on %ld volumes"),
11190Sstevel@tonic-gate 		    ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)),
11200Sstevel@tonic-gate 		    mb((u_offset_t)(unsigned)(spcl.c_tapea)),
11210Sstevel@tonic-gate 		    spcl.c_volume);
11220Sstevel@tonic-gate 	else
11230Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
11240Sstevel@tonic-gate 		    gettext("%lld blocks (%s) on 1 volume"),
11250Sstevel@tonic-gate 		    ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)),
11260Sstevel@tonic-gate 		    mb((u_offset_t)(unsigned)(spcl.c_tapea)));
11270Sstevel@tonic-gate 	if (timeclock((time_t)0) != (time_t)0) {
11280Sstevel@tonic-gate 		(void) snprintf(kbsbuf, sizeof (kbsbuf),
11290Sstevel@tonic-gate 		    gettext(" at %ld KB/sec"),
11300Sstevel@tonic-gate 		    (long)(((float)spcl.c_tapea / (float)timeclock((time_t)0))
11315299Sws195443 		    * 1000.0));
11320Sstevel@tonic-gate 		(void) strcat(msgbuf, kbsbuf);
11330Sstevel@tonic-gate 	}
11340Sstevel@tonic-gate 	(void) strcat(msgbuf, "\n");
11350Sstevel@tonic-gate 	msg(msgbuf);
11360Sstevel@tonic-gate 	(void) timeclock((time_t)-1);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	if (archive)
11390Sstevel@tonic-gate 		msg(gettext("Archiving dump to `%s'\n"), archivefile);
11400Sstevel@tonic-gate 	if (active && !verify) {
11410Sstevel@tonic-gate 		nextstate(DS_INIT);
11420Sstevel@tonic-gate 		activepass();
11430Sstevel@tonic-gate 		goto restart;
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 	msgp = gettext("DUMP IS DONE\n");
11460Sstevel@tonic-gate 	msg(msgp);
11470Sstevel@tonic-gate 	broadcast(msgp);
11480Sstevel@tonic-gate 	if (! doingactive)
11490Sstevel@tonic-gate 		putitime();
11500Sstevel@tonic-gate 	Exit(X_FINOK);
11511053Smaheshvs 
11521053Smaheshvs 	/*NOTREACHED*/
11530Sstevel@tonic-gate 	return (0);
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate void
11571053Smaheshvs sigAbort(int sig)
11580Sstevel@tonic-gate {
11590Sstevel@tonic-gate 	char	*sigtype;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	switch (sig) {
11620Sstevel@tonic-gate 	case SIGHUP:
11630Sstevel@tonic-gate 		sigtype = "SIGHUP";
11640Sstevel@tonic-gate 		break;
11650Sstevel@tonic-gate 	case SIGTRAP:
11660Sstevel@tonic-gate 		sigtype = "SIGTRAP";
11670Sstevel@tonic-gate 		break;
11680Sstevel@tonic-gate 	case SIGFPE:
11690Sstevel@tonic-gate 		sigtype = "SIGFPE";
11700Sstevel@tonic-gate 		break;
11710Sstevel@tonic-gate 	case SIGBUS:
11720Sstevel@tonic-gate 		msg(gettext("%s  ABORTING!\n"), "SIGBUS()");
11730Sstevel@tonic-gate 		(void) signal(SIGUSR2, SIG_DFL);
11740Sstevel@tonic-gate 		abort();
11750Sstevel@tonic-gate 		/*NOTREACHED*/
11760Sstevel@tonic-gate 	case SIGSEGV:
11770Sstevel@tonic-gate 		msg(gettext("%s  ABORTING!\n"), "SIGSEGV()");
11780Sstevel@tonic-gate 		(void) signal(SIGUSR2, SIG_DFL);
11790Sstevel@tonic-gate 		abort();
11800Sstevel@tonic-gate 		/*NOTREACHED*/
11810Sstevel@tonic-gate 	case SIGALRM:
11820Sstevel@tonic-gate 		sigtype = "SIGALRM";
11830Sstevel@tonic-gate 		break;
11840Sstevel@tonic-gate 	case SIGTERM:
11850Sstevel@tonic-gate 		sigtype = "SIGTERM";
11860Sstevel@tonic-gate 		break;
11870Sstevel@tonic-gate 	case SIGPIPE:
11880Sstevel@tonic-gate 		msg(gettext("Broken pipe\n"));
11890Sstevel@tonic-gate 		dumpabort();
11900Sstevel@tonic-gate 		/*NOTREACHED*/
11910Sstevel@tonic-gate 	default:
11920Sstevel@tonic-gate 		sigtype = "SIGNAL";
11930Sstevel@tonic-gate 		break;
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate 	msg(gettext("%s()  try rewriting\n"), sigtype);
11960Sstevel@tonic-gate 	if (pipeout) {
11970Sstevel@tonic-gate 		msg(gettext("Unknown signal, Cannot recover\n"));
11980Sstevel@tonic-gate 		dumpabort();
11990Sstevel@tonic-gate 		/*NOTREACHED*/
12000Sstevel@tonic-gate 	}
12010Sstevel@tonic-gate 	msg(gettext("Rewriting attempted as response to unknown signal.\n"));
12020Sstevel@tonic-gate 	(void) fflush(stderr);
12030Sstevel@tonic-gate 	(void) fflush(stdout);
12040Sstevel@tonic-gate 	close_rewind();
12050Sstevel@tonic-gate 	Exit(X_REWRITE);
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate /* Note that returned value is malloc'd if != cp && != NULL */
12090Sstevel@tonic-gate char *
12101053Smaheshvs rawname(char *cp)
12110Sstevel@tonic-gate {
12120Sstevel@tonic-gate 	struct stat64 st;
12130Sstevel@tonic-gate 	char *dp;
12140Sstevel@tonic-gate 	extern char *getfullrawname();
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	if (stat64(cp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFBLK)
12170Sstevel@tonic-gate 		return (cp);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	dp = getfullrawname(cp);
12200Sstevel@tonic-gate 	if (dp == 0)
12210Sstevel@tonic-gate 		return (0);
12220Sstevel@tonic-gate 	if (*dp == '\0') {
12230Sstevel@tonic-gate 		free(dp);
12240Sstevel@tonic-gate 		return (0);
12250Sstevel@tonic-gate 	}
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	if (stat64(dp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFCHR) {
12280Sstevel@tonic-gate 		free(dp);
12290Sstevel@tonic-gate 		return (cp);
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	return (dp);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate static char *
12361053Smaheshvs mb(u_offset_t blks)
12370Sstevel@tonic-gate {
12380Sstevel@tonic-gate 	static char buf[16];
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	if (blks < 1024)
12410Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%lldKB", blks);
12420Sstevel@tonic-gate 	else
12430Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%.2fMB",
12440Sstevel@tonic-gate 		    ((double)(blks*tp_bsize)) / (double)(1024*1024));
12450Sstevel@tonic-gate 	return (buf);
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate #ifdef signal
12491053Smaheshvs void (*nsignal(int sig, void (*act)(int)))(int)
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate 	struct sigaction sa, osa;
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	sa.sa_handler = act;
12540Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
12550Sstevel@tonic-gate 	sa.sa_flags = SA_RESTART;
12560Sstevel@tonic-gate 	if (sigaction(sig, &sa, &osa) < 0)
12570Sstevel@tonic-gate 		return ((void (*)(int))-1);
12580Sstevel@tonic-gate 	return (osa.sa_handler);
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate #endif
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate static void
12631053Smaheshvs nextstate(int state)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
12660Sstevel@tonic-gate 	dumpstate = state;
12670Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
12680Sstevel@tonic-gate 	ino = 0;
12690Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
12700Sstevel@tonic-gate 	pos = 0;
12710Sstevel@tonic-gate 	leftover = 0;
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate /*
12750Sstevel@tonic-gate  * timeclock() function, for keeping track of how much time we've spent
12760Sstevel@tonic-gate  * writing to the tape device.  it always returns the amount of time
12770Sstevel@tonic-gate  * already spent, in milliseconds.  if you pass it a positive, then that's
12780Sstevel@tonic-gate  * telling it that we're writing, so the time counts.  if you pass it a
12790Sstevel@tonic-gate  * zero, then that's telling it we're not writing; perhaps we're waiting
12800Sstevel@tonic-gate  * for user input.
12810Sstevel@tonic-gate  *
12820Sstevel@tonic-gate  * a state of -1 resets everything.
12830Sstevel@tonic-gate  */
12840Sstevel@tonic-gate time32_t
12851053Smaheshvs timeclock(time32_t state)
12860Sstevel@tonic-gate {
12870Sstevel@tonic-gate 	static int *currentState = NULL;
12880Sstevel@tonic-gate 	static struct timeval *clockstart;
12890Sstevel@tonic-gate 	static time32_t *emilli;
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	struct timeval current[1];
12920Sstevel@tonic-gate 	int fd, saverr;
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate #ifdef DEBUG
12950Sstevel@tonic-gate 	fprintf(stderr, "pid=%d timeclock ", getpid());
12960Sstevel@tonic-gate 	if (state == (time32_t)-1)
12970Sstevel@tonic-gate 		fprintf(stderr, "cleared\n");
12980Sstevel@tonic-gate 	else if (state > 0)
12990Sstevel@tonic-gate 		fprintf(stderr, "ticking\n");
13000Sstevel@tonic-gate 	else
13010Sstevel@tonic-gate 		fprintf(stderr, "paused\n");
13020Sstevel@tonic-gate #endif /* DEBUG */
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	/* if we haven't setup the shared memory, init */
13050Sstevel@tonic-gate 	if (currentState == (int *)NULL) {
13060Sstevel@tonic-gate 		if ((fd = open("/dev/zero", O_RDWR)) < 0) {
13070Sstevel@tonic-gate 			saverr = errno;
13080Sstevel@tonic-gate 			msg(gettext("Cannot open `%s': %s\n"),
13095299Sws195443 			    "/dev/zero", strerror(saverr));
13100Sstevel@tonic-gate 			dumpabort();
13110Sstevel@tonic-gate 			/*NOTREACHED*/
13120Sstevel@tonic-gate 		}
13130Sstevel@tonic-gate 		/*LINTED [mmap always returns an aligned value]*/
13140Sstevel@tonic-gate 		currentState = (int *)mmap((char *)0, getpagesize(),
13155299Sws195443 		    PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0);
13160Sstevel@tonic-gate 		if (currentState == (int *)-1) {
13170Sstevel@tonic-gate 			saverr = errno;
13180Sstevel@tonic-gate 			msg(gettext(
13195299Sws195443 			    "Cannot memory map monitor variables: %s\n"),
13205299Sws195443 			    strerror(saverr));
13210Sstevel@tonic-gate 			dumpabort();
13220Sstevel@tonic-gate 			/*NOTREACHED*/
13230Sstevel@tonic-gate 		}
13240Sstevel@tonic-gate 		(void) close(fd);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 		/* LINTED currentState is sufficiently aligned */
13270Sstevel@tonic-gate 		clockstart = (struct timeval *)(currentState + 1);
13280Sstevel@tonic-gate 		emilli = (time32_t *)(clockstart + 1);
13290Sstevel@tonic-gate 		/* Note everything is initialized to zero via /dev/zero */
13300Sstevel@tonic-gate 	}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	if (state == (time32_t)-1) {
13330Sstevel@tonic-gate 		bzero(clockstart, sizeof (*clockstart));
13340Sstevel@tonic-gate 		*currentState = 0;
13350Sstevel@tonic-gate 		*emilli = (time32_t)0;
13360Sstevel@tonic-gate 		return (0);
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	(void) gettimeofday(current, NULL);
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	if (*currentState != 0) {
13420Sstevel@tonic-gate 		current->tv_usec += 1000000;
13430Sstevel@tonic-gate 		current->tv_sec--;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 		/* LINTED: result will fit in a time32_t */
13460Sstevel@tonic-gate 		*emilli += (current->tv_sec - clockstart->tv_sec) * 1000;
13470Sstevel@tonic-gate 		/* LINTED: result will fit in a time32_t */
13480Sstevel@tonic-gate 		*emilli += (current->tv_usec - clockstart->tv_usec) / 1000;
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	if (state != 0)
13520Sstevel@tonic-gate 		bcopy(current, clockstart, sizeof (current));
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	*currentState = state;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	return (*emilli);
13570Sstevel@tonic-gate }
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate static int
13600Sstevel@tonic-gate statcmp(const struct stat64 *left, const struct stat64 *right)
13610Sstevel@tonic-gate {
13620Sstevel@tonic-gate 	int result = 1;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	if ((left->st_dev == right->st_dev) &&
13650Sstevel@tonic-gate 	    (left->st_ino == right->st_ino) &&
13660Sstevel@tonic-gate 	    (left->st_mode == right->st_mode) &&
13670Sstevel@tonic-gate 	    (left->st_nlink == right->st_nlink) &&
13680Sstevel@tonic-gate 	    (left->st_uid == right->st_uid) &&
13690Sstevel@tonic-gate 	    (left->st_gid == right->st_gid) &&
13700Sstevel@tonic-gate 	    (left->st_rdev == right->st_rdev) &&
13710Sstevel@tonic-gate 	    (left->st_ctim.tv_sec == right->st_ctim.tv_sec) &&
13720Sstevel@tonic-gate 	    (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) &&
13730Sstevel@tonic-gate 	    (left->st_mtim.tv_sec == right->st_mtim.tv_sec) &&
13745299Sws195443 	    (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec)) {
13755299Sws195443 		/*
13765299Sws195443 		 * Unlike in the ufsrestore version
13775299Sws195443 		 * st_blocks and st_blksiz are not
13785299Sws195443 		 * compared. The reason for this is
13795299Sws195443 		 * problems with zfs dump files. Zfs
13805299Sws195443 		 * changes it's statistics in those
13815299Sws195443 		 * fields.
13825299Sws195443 		 */
13830Sstevel@tonic-gate 		result = 0;
13840Sstevel@tonic-gate 	}
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	return (result);
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate /*
13900Sstevel@tonic-gate  * Safely open a file or device.
13910Sstevel@tonic-gate  */
13920Sstevel@tonic-gate static int
13930Sstevel@tonic-gate safe_open_common(const char *filename, int mode, int perms, int device)
13940Sstevel@tonic-gate {
13950Sstevel@tonic-gate 	int fd;
13960Sstevel@tonic-gate 	int working_mode;
13970Sstevel@tonic-gate 	int saverr;
13980Sstevel@tonic-gate 	char *errtext;
13990Sstevel@tonic-gate 	struct stat64 pre_stat, pre_lstat;
14000Sstevel@tonic-gate 	struct stat64 post_stat, post_lstat;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	/*
14030Sstevel@tonic-gate 	 * Don't want to be spoofed into trashing something we
14040Sstevel@tonic-gate 	 * shouldn't, thus the following rigamarole.  If it doesn't
14050Sstevel@tonic-gate 	 * exist, we create it and proceed.  Otherwise, require that
14060Sstevel@tonic-gate 	 * what's there be a real file with no extraneous links and
14070Sstevel@tonic-gate 	 * owned by whoever ran us.
14080Sstevel@tonic-gate 	 *
14090Sstevel@tonic-gate 	 * The silliness with using both lstat() and fstat() is to avoid
14100Sstevel@tonic-gate 	 * race-condition games with someone replacing the file with a
14110Sstevel@tonic-gate 	 * symlink after we've opened it.  If there was an flstat(),
14120Sstevel@tonic-gate 	 * we wouldn't need the fstat().
14130Sstevel@tonic-gate 	 *
14140Sstevel@tonic-gate 	 * The initial open with the hard-coded flags is ok even if we
14150Sstevel@tonic-gate 	 * are intending to open only for reading.  If it succeeds,
14160Sstevel@tonic-gate 	 * then the file did not exist, and we'll synthesize an appropriate
14170Sstevel@tonic-gate 	 * complaint below.  Otherwise, it does exist, so we won't be
14180Sstevel@tonic-gate 	 * truncating it with the open.
14190Sstevel@tonic-gate 	 */
14200Sstevel@tonic-gate 	if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE,
14210Sstevel@tonic-gate 	    perms)) < 0) {
14220Sstevel@tonic-gate 		if (errno == EEXIST) {
14230Sstevel@tonic-gate 			if (lstat64(filename, &pre_lstat) < 0) {
14240Sstevel@tonic-gate 				return (-1);
14250Sstevel@tonic-gate 			}
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 			if (stat64(filename, &pre_stat) < 0) {
14280Sstevel@tonic-gate 				return (-1);
14290Sstevel@tonic-gate 			}
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 			working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY);
14320Sstevel@tonic-gate 			working_mode |= O_LARGEFILE;
14330Sstevel@tonic-gate 			if ((fd = open(filename, working_mode)) < 0) {
14340Sstevel@tonic-gate 				if (errno == ENOENT) {
14350Sstevel@tonic-gate 					errtext = gettext(
14360Sstevel@tonic-gate "Unexpected condition detected: %s used to exist, but doesn't any longer\n");
14370Sstevel@tonic-gate 					msg(errtext, filename);
14380Sstevel@tonic-gate 					syslog(LOG_WARNING, errtext, filename);
14390Sstevel@tonic-gate 					errno = ENOENT;
14400Sstevel@tonic-gate 				}
14410Sstevel@tonic-gate 				return (-1);
14420Sstevel@tonic-gate 			}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 			if (lstat64(filename, &post_lstat) < 0) {
14450Sstevel@tonic-gate 				saverr = errno;
14460Sstevel@tonic-gate 				(void) close(fd);
14470Sstevel@tonic-gate 				errno = saverr;
14480Sstevel@tonic-gate 				return (-1);
14490Sstevel@tonic-gate 			}
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 			if (fstat64(fd, &post_stat) < 0) {
14520Sstevel@tonic-gate 				saverr = errno;
14530Sstevel@tonic-gate 				(void) close(fd);
14540Sstevel@tonic-gate 				errno = saverr;
14550Sstevel@tonic-gate 				return (-1);
14560Sstevel@tonic-gate 			}
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 			/*
14590Sstevel@tonic-gate 			 * Can't just use memcmp(3C), because the access
14600Sstevel@tonic-gate 			 * time is updated by open(2).
14610Sstevel@tonic-gate 			 */
14620Sstevel@tonic-gate 			if (statcmp(&pre_lstat, &post_lstat) != 0) {
14635299Sws195443 				errtext = gettext("Unexpected change detected: "
14645299Sws195443 				    "%s's lstat(2) information changed\n");
14650Sstevel@tonic-gate 				msg(errtext, filename);
14660Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
14670Sstevel@tonic-gate 				errno = EPERM;
14680Sstevel@tonic-gate 				return (-1);
14690Sstevel@tonic-gate 			}
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 			if (statcmp(&pre_stat, &post_stat) != 0) {
14725299Sws195443 				errtext = gettext("Unexpected change detected: "
14735299Sws195443 				    "%s's stat(2) information changed\n"),
14745299Sws195443 				    msg(errtext, filename);
14750Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
14760Sstevel@tonic-gate 				errno = EPERM;
14770Sstevel@tonic-gate 				return (-1);
14780Sstevel@tonic-gate 			}
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 			/*
14810Sstevel@tonic-gate 			 * If inode, device, or type are wrong, bail out.
14820Sstevel@tonic-gate 			 * Note using post_stat instead of post_lstat for the
14830Sstevel@tonic-gate 			 * S_ISCHR() test.  This is to allow the /dev ->
14840Sstevel@tonic-gate 			 * /devices bit to work, as long as the final target
14850Sstevel@tonic-gate 			 * is a character device (i.e., raw disk or tape).
14860Sstevel@tonic-gate 			 */
14870Sstevel@tonic-gate 			if (device && !(S_ISCHR(post_stat.st_mode)) &&
14880Sstevel@tonic-gate 			    !(S_ISFIFO(post_stat.st_mode)) &&
14890Sstevel@tonic-gate 			    !(S_ISREG(post_lstat.st_mode))) {
14905299Sws195443 				errtext = gettext("Unexpected condition "
14915299Sws195443 				    "detected: %s is not a supported device\n"),
14925299Sws195443 				    msg(errtext, filename);
14930Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
14940Sstevel@tonic-gate 				(void) close(fd);
14950Sstevel@tonic-gate 				errno = EPERM;
14960Sstevel@tonic-gate 				return (-1);
14970Sstevel@tonic-gate 			} else if (!device &&
14980Sstevel@tonic-gate 			    (!S_ISREG(post_lstat.st_mode) ||
14990Sstevel@tonic-gate 			    (post_stat.st_ino != post_lstat.st_ino) ||
15000Sstevel@tonic-gate 			    (post_stat.st_dev != post_lstat.st_dev))) {
15015299Sws195443 				errtext = gettext("Unexpected condition "
15025299Sws195443 				    "detected: %s is not a regular file\n"),
15035299Sws195443 				    msg(errtext, filename);
15040Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
15050Sstevel@tonic-gate 				(void) close(fd);
15060Sstevel@tonic-gate 				errno = EPERM;
15070Sstevel@tonic-gate 				return (-1);
15080Sstevel@tonic-gate 			}
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 			/*
15110Sstevel@tonic-gate 			 * Bad link count implies someone's linked our
15120Sstevel@tonic-gate 			 * target to something else, which we probably
15130Sstevel@tonic-gate 			 * shouldn't step on.
15140Sstevel@tonic-gate 			 */
15150Sstevel@tonic-gate 			if (post_lstat.st_nlink != 1) {
15165299Sws195443 				errtext = gettext("Unexpected condition "
15175299Sws195443 				    "detected: %s must have exactly one "
15185299Sws195443 				    "link\n"), msg(errtext, filename);
15190Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
15200Sstevel@tonic-gate 				(void) close(fd);
15210Sstevel@tonic-gate 				errno = EPERM;
15220Sstevel@tonic-gate 				return (-1);
15230Sstevel@tonic-gate 			}
15240Sstevel@tonic-gate 			/*
15250Sstevel@tonic-gate 			 * Root might make a file, but non-root might
15260Sstevel@tonic-gate 			 * need to open it.  If the permissions let us
15270Sstevel@tonic-gate 			 * get this far, then let it through.
15280Sstevel@tonic-gate 			 */
15290Sstevel@tonic-gate 			if (post_lstat.st_uid != getuid() &&
15300Sstevel@tonic-gate 			    post_lstat.st_uid != 0) {
15315299Sws195443 				errtext = gettext("Unsupported "
15325299Sws195443 				    "condition detected: %s "
15335299Sws195443 				    "must be owned by uid %ld or 0\n"),
15345299Sws195443 				    msg(errtext, filename, (long)getuid());
15350Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename,
15360Sstevel@tonic-gate 				    (long)getuid());
15370Sstevel@tonic-gate 				(void) close(fd);
15380Sstevel@tonic-gate 				errno = EPERM;
15390Sstevel@tonic-gate 				return (-1);
15400Sstevel@tonic-gate 			}
15410Sstevel@tonic-gate 			if (mode & O_TRUNC) {
15420Sstevel@tonic-gate 				if (ftruncate(fd, (off_t)0) < 0) {
15430Sstevel@tonic-gate 					msg("ftruncate(%s): %s\n",
15440Sstevel@tonic-gate 					    filename, strerror(errno));
15450Sstevel@tonic-gate 					(void) close(fd);
15460Sstevel@tonic-gate 					return (-1);
15470Sstevel@tonic-gate 				}
15480Sstevel@tonic-gate 			}
15490Sstevel@tonic-gate 		} else {
15500Sstevel@tonic-gate 			/*
15510Sstevel@tonic-gate 			 * Didn't exist, but couldn't open it.
15520Sstevel@tonic-gate 			 */
15530Sstevel@tonic-gate 			return (-1);
15540Sstevel@tonic-gate 		}
15550Sstevel@tonic-gate 	} else {
15560Sstevel@tonic-gate 		/*
15570Sstevel@tonic-gate 		 * If truncating open succeeded for a read-only open,
15580Sstevel@tonic-gate 		 * bail out, as we really shouldn't have succeeded.
15590Sstevel@tonic-gate 		 */
15600Sstevel@tonic-gate 		if (mode & O_RDONLY) {
15610Sstevel@tonic-gate 			/* Undo the O_CREAT */
15620Sstevel@tonic-gate 			(void) unlink(filename);
15630Sstevel@tonic-gate 			msg("open(%s): %s\n",
15640Sstevel@tonic-gate 			    filename, strerror(ENOENT));
15650Sstevel@tonic-gate 			(void) close(fd);
15660Sstevel@tonic-gate 			errno = ENOENT;
15670Sstevel@tonic-gate 			return (-1);
15680Sstevel@tonic-gate 		}
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	return (fd);
15720Sstevel@tonic-gate }
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate /*
15750Sstevel@tonic-gate  * Safely open a file.
15760Sstevel@tonic-gate  */
15770Sstevel@tonic-gate int
15780Sstevel@tonic-gate safe_file_open(const char *filename, int mode, int perms)
15790Sstevel@tonic-gate {
15800Sstevel@tonic-gate 	return (safe_open_common(filename, mode, perms, 0));
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate  * Safely open a device.
15850Sstevel@tonic-gate  */
15860Sstevel@tonic-gate int
15870Sstevel@tonic-gate safe_device_open(const char *filename, int mode, int perms)
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate 	return (safe_open_common(filename, mode, perms, 1));
15900Sstevel@tonic-gate }
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate /*
15930Sstevel@tonic-gate  * STDIO version of safe_open
15940Sstevel@tonic-gate  */
15950Sstevel@tonic-gate FILE *
15960Sstevel@tonic-gate safe_fopen(const char *filename, const char *smode, int perms)
15970Sstevel@tonic-gate {
15980Sstevel@tonic-gate 	int fd;
15990Sstevel@tonic-gate 	int bmode;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	/*
16020Sstevel@tonic-gate 	 * accepts only modes  "r", "r+", and "w"
16030Sstevel@tonic-gate 	 */
16040Sstevel@tonic-gate 	if (smode[0] == 'r') {
16050Sstevel@tonic-gate 		if (smode[1] == '\0') {
16060Sstevel@tonic-gate 			bmode = O_RDONLY;
16070Sstevel@tonic-gate 		} else if ((smode[1] == '+') && (smode[2] == '\0')) {
16080Sstevel@tonic-gate 			bmode = O_RDWR;
16090Sstevel@tonic-gate 		}
16100Sstevel@tonic-gate 	} else if ((smode[0] == 'w') && (smode[1] == '\0')) {
16110Sstevel@tonic-gate 		bmode = O_WRONLY;
16120Sstevel@tonic-gate 	} else {
16130Sstevel@tonic-gate 		msg(gettext("internal error: safe_fopen: invalid mode `%s'\n"),
16140Sstevel@tonic-gate 		    smode);
16150Sstevel@tonic-gate 		return (NULL);
16160Sstevel@tonic-gate 	}
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	fd = safe_file_open(filename, bmode, perms);
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	/*
16210Sstevel@tonic-gate 	 * caller is expected to report error.
16220Sstevel@tonic-gate 	 */
16230Sstevel@tonic-gate 	if (fd >= 0)
16245299Sws195443 		return (fdopen(fd, smode));
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	return ((FILE *)NULL);
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate void
16300Sstevel@tonic-gate child_chdir(void)
16310Sstevel@tonic-gate {
16320Sstevel@tonic-gate 	char name[MAXPATHLEN];
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	if (debug_chdir != NULL) {
16350Sstevel@tonic-gate 		snprintf(name, sizeof (name), "%s/%ld",
16360Sstevel@tonic-gate 		    debug_chdir, (long)getpid());
16370Sstevel@tonic-gate 		if (mkdir(name, 0755) < 0)
16380Sstevel@tonic-gate 			msg("mkdir(%s): %s", name, strerror(errno));
16390Sstevel@tonic-gate 		if (chdir(name) < 0)
16400Sstevel@tonic-gate 			msg("chdir(%s): %s", name, strerror(errno));
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate }
1643