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