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