xref: /titanic_53/usr/src/cmd/backup/dump/dumpmain.c (revision 4983fdb5373239f810c1288128badcb36d079b78)
17c478bd9Sstevel@tonic-gate /*
24098eb52Sws195443  * CDDL HEADER START
34098eb52Sws195443  *
44098eb52Sws195443  * The contents of this file are subject to the terms of the
54098eb52Sws195443  * Common Development and Distribution License (the "License").
64098eb52Sws195443  * You may not use this file except in compliance with the License.
74098eb52Sws195443  *
84098eb52Sws195443  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94098eb52Sws195443  * or http://www.opensolaris.org/os/licensing.
104098eb52Sws195443  * See the License for the specific language governing permissions
114098eb52Sws195443  * and limitations under the License.
124098eb52Sws195443  *
134098eb52Sws195443  * When distributing Covered Code, include this CDDL HEADER in each
144098eb52Sws195443  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154098eb52Sws195443  * If applicable, add the following below this CDDL HEADER, with the
164098eb52Sws195443  * fields enclosed by brackets "[]" replaced with your own identifying
174098eb52Sws195443  * information: Portions Copyright [yyyy] [name of copyright owner]
184098eb52Sws195443  *
194098eb52Sws195443  * CDDL HEADER END
204098eb52Sws195443  */
214098eb52Sws195443 
224098eb52Sws195443 /*
23*4983fdb5Svk154806  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
327c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
337c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "dump.h"
397c478bd9Sstevel@tonic-gate #include <rmt.h>
407c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
417c478bd9Sstevel@tonic-gate #include <limits.h>
427c478bd9Sstevel@tonic-gate #include <priv_utils.h>
437c478bd9Sstevel@tonic-gate #include "roll_log.h"
44*4983fdb5Svk154806 #include <unistd.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate int	notify = 0;		/* notify operator flag */
477c478bd9Sstevel@tonic-gate int	blockswritten = 0;	/* number of blocks written on current tape */
487c478bd9Sstevel@tonic-gate uint_t	tapeno = 0;		/* current tape number */
497c478bd9Sstevel@tonic-gate daddr32_t filenum = 0;		/* current file number on tape */
507c478bd9Sstevel@tonic-gate int	density = 0;		/* density in bytes/0.1" */
517c478bd9Sstevel@tonic-gate int	tenthsperirg;		/* inter-record-gap in 0.1"'s */
527c478bd9Sstevel@tonic-gate uint_t	ntrec = 0;		/* # tape blocks in each tape record */
537c478bd9Sstevel@tonic-gate uint_t	saved_ntrec = 0;	/* saved value of ntrec */
547c478bd9Sstevel@tonic-gate uint_t	forceflag = 0;		/* forced to change tp_bsize */
557c478bd9Sstevel@tonic-gate int	cartridge = 0;		/* assume non-cartridge tape */
567c478bd9Sstevel@tonic-gate uint_t	tracks;			/* # tracks on a cartridge tape */
577c478bd9Sstevel@tonic-gate int	diskette = 0;		/* assume not dumping to a diskette */
587c478bd9Sstevel@tonic-gate int	printsize = 0;		/* just print estimated size and exit */
597c478bd9Sstevel@tonic-gate int	mapfd = -1;		/* if >= 0, file descriptor for mmap */
607c478bd9Sstevel@tonic-gate int32_t	tp_bsize = TP_BSIZE_MIN; /* tape block record size (frag size) */
617c478bd9Sstevel@tonic-gate #ifdef DEBUG
627c478bd9Sstevel@tonic-gate int	xflag;			/* debugging switch */
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate char	*myname;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * This should be struct fs, but there are trailing bits on disk
697c478bd9Sstevel@tonic-gate  * that we also need to read in as part of it.  It's an array of
707c478bd9Sstevel@tonic-gate  * longs instead of char to force proper alignment.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate static long sblock_buf[SBSIZE/sizeof (long)];
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef __STDC__
757c478bd9Sstevel@tonic-gate static char *mb(u_offset_t);
767c478bd9Sstevel@tonic-gate static void nextstate(int);
777c478bd9Sstevel@tonic-gate #else
787c478bd9Sstevel@tonic-gate static char *mb();
797c478bd9Sstevel@tonic-gate static void nextstate();
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate extern	jmp_buf checkpoint_buf;	/* context for return from checkpoint */
837c478bd9Sstevel@tonic-gate #define	FUDGE_FACTOR	0x2000000
847c478bd9Sstevel@tonic-gate 
85fe0e7ec4Smaheshvs int
86fe0e7ec4Smaheshvs main(int argc, char *argv[])
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	char		*arg;
897c478bd9Sstevel@tonic-gate 	int		bflag = 0, i, error = 0, saverr;
907c478bd9Sstevel@tonic-gate 	double		fetapes = 0.0;
917c478bd9Sstevel@tonic-gate 	struct	mnttab	*dt;
927c478bd9Sstevel@tonic-gate 	char		msgbuf[3000], *msgp;
937c478bd9Sstevel@tonic-gate 	char		kbsbuf[BUFSIZ];
947c478bd9Sstevel@tonic-gate 	u_offset_t	esize_shift = 0;
957c478bd9Sstevel@tonic-gate 	int32_t	new_mult = 0;
967c478bd9Sstevel@tonic-gate 	time32_t	snapdate;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	host = NULL;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (myname = strrchr(argv[0], '/'))
1017c478bd9Sstevel@tonic-gate 		myname++;
1027c478bd9Sstevel@tonic-gate 	else
1037c478bd9Sstevel@tonic-gate 		myname = argv[0];
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (strcmp("hsmdump", myname) == 0) {
1067c478bd9Sstevel@tonic-gate 		msg(gettext("hsmdump emulation is no longer supported.\n"));
1077c478bd9Sstevel@tonic-gate 		Exit(X_ABORT);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	tape = DEFTAPE;
1117c478bd9Sstevel@tonic-gate 	autoload_period = 12;
1127c478bd9Sstevel@tonic-gate 	autoload_tries = 12;	/* traditional default of ~2.5 minutes */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1157c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1167c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1177c478bd9Sstevel@tonic-gate #endif  /* TEXT_DOMAIN */
1187c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * If someone strips the set-uid bit, dump will still work for local
1227c478bd9Sstevel@tonic-gate 	 * tapes.  Fail when we try to access a remote tape.
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	(void) __init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_HOSTNAME, spcl.c_host, sizeof (spcl.c_host)) < 0) {
1277c478bd9Sstevel@tonic-gate 		saverr = errno;
1287c478bd9Sstevel@tonic-gate 		msg(gettext("Could not get host name: %s\n"),
1297c478bd9Sstevel@tonic-gate 		    strerror(saverr));
1307c478bd9Sstevel@tonic-gate 		bzero(spcl.c_host, sizeof (spcl.c_host));
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	dumppid = getpid();
1347c478bd9Sstevel@tonic-gate 	tsize = 0;	/* no default size, detect EOT dynamically */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	disk = NULL;
1377c478bd9Sstevel@tonic-gate 	dname = NULL;
1387c478bd9Sstevel@tonic-gate 	disk_dynamic = 0;
1397c478bd9Sstevel@tonic-gate 	increm = NINCREM;
1407c478bd9Sstevel@tonic-gate 	incno = '9';
1417c478bd9Sstevel@tonic-gate 	uflag = 0;
1427c478bd9Sstevel@tonic-gate 	arg = "u";
1437c478bd9Sstevel@tonic-gate 	tlabel = "none";
1447c478bd9Sstevel@tonic-gate 	if (argc > 1) {
1457c478bd9Sstevel@tonic-gate 		argv++;
1467c478bd9Sstevel@tonic-gate 		argc--;
1477c478bd9Sstevel@tonic-gate 		arg = *argv;
1487c478bd9Sstevel@tonic-gate 		if (*arg == '-')
1497c478bd9Sstevel@tonic-gate 			arg++;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	while (*arg)
1527c478bd9Sstevel@tonic-gate 	switch (*arg++) {		/* BE CAUTIOUS OF FALLTHROUGHS */
1537c478bd9Sstevel@tonic-gate 	case 'M':
1547c478bd9Sstevel@tonic-gate 		/*
1557c478bd9Sstevel@tonic-gate 		 * This undocumented option causes each process to
1567c478bd9Sstevel@tonic-gate 		 * mkdir debug_chdir/getpid(), and chdir to it.  This is
1577c478bd9Sstevel@tonic-gate 		 * to ease the collection of profiling information and
1587c478bd9Sstevel@tonic-gate 		 * core dumps.
1597c478bd9Sstevel@tonic-gate 		 */
1607c478bd9Sstevel@tonic-gate 		if (argc > 1) {
1617c478bd9Sstevel@tonic-gate 			argv++;
1627c478bd9Sstevel@tonic-gate 			argc--;
1637c478bd9Sstevel@tonic-gate 			debug_chdir = *argv;
1647c478bd9Sstevel@tonic-gate 			msg(gettext(
1657c478bd9Sstevel@tonic-gate 			    "Each process shall try to chdir to %s/<pid>\n"),
1667c478bd9Sstevel@tonic-gate 			    debug_chdir);
1677c478bd9Sstevel@tonic-gate 			child_chdir();
1687c478bd9Sstevel@tonic-gate 		} else {
1697c478bd9Sstevel@tonic-gate 			msg(gettext("Missing move-to-dir (M) name\n"));
1707c478bd9Sstevel@tonic-gate 			dumpabort();
1717c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 		break;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	case 'w':
1767c478bd9Sstevel@tonic-gate 		lastdump('w');		/* tell us only what has to be done */
1777c478bd9Sstevel@tonic-gate 		exit(0);
1787c478bd9Sstevel@tonic-gate 		break;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	case 'W':			/* what to do */
1817c478bd9Sstevel@tonic-gate 		lastdump('W');		/* tell state of what has been done */
1827c478bd9Sstevel@tonic-gate 		exit(0);		/* do nothing else */
1837c478bd9Sstevel@tonic-gate 		break;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	case 'T':
1867c478bd9Sstevel@tonic-gate 		if (argc > 1) {
1877c478bd9Sstevel@tonic-gate 			int count;
1887c478bd9Sstevel@tonic-gate 			int multiplier;
1897c478bd9Sstevel@tonic-gate 			char units;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 			argv++;
1927c478bd9Sstevel@tonic-gate 			argc--;
1937c478bd9Sstevel@tonic-gate 			count = atoi(*argv);
1947c478bd9Sstevel@tonic-gate 			if (count < 1) {
1957c478bd9Sstevel@tonic-gate 				msg(gettext(
1967c478bd9Sstevel@tonic-gate 				    "Unreasonable autoload timeout period\n"));
1977c478bd9Sstevel@tonic-gate 				dumpabort();
1987c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
1997c478bd9Sstevel@tonic-gate 			}
2007c478bd9Sstevel@tonic-gate 			units = *(*argv + strlen(*argv) - 1);
2017c478bd9Sstevel@tonic-gate 			switch (units) {
2027c478bd9Sstevel@tonic-gate 			case 's':
2037c478bd9Sstevel@tonic-gate 				multiplier = 1;
2047c478bd9Sstevel@tonic-gate 				break;
2057c478bd9Sstevel@tonic-gate 			case 'h':
2067c478bd9Sstevel@tonic-gate 				multiplier = 3600;
2077c478bd9Sstevel@tonic-gate 				break;
2087c478bd9Sstevel@tonic-gate 			case '0': case '1': case '2': case '3': case '4':
2097c478bd9Sstevel@tonic-gate 			case '5': case '6': case '7': case '8': case '9':
2107c478bd9Sstevel@tonic-gate 			case 'm':
2117c478bd9Sstevel@tonic-gate 				multiplier = 60;
2127c478bd9Sstevel@tonic-gate 				break;
2137c478bd9Sstevel@tonic-gate 			default:
2147c478bd9Sstevel@tonic-gate 				msg(gettext(
2157c478bd9Sstevel@tonic-gate 				    "Unknown timeout units indicator `%c'\n"),
2167c478bd9Sstevel@tonic-gate 				    units);
2177c478bd9Sstevel@tonic-gate 				dumpabort();
2187c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 			autoload_tries = 1 +
2217c478bd9Sstevel@tonic-gate 			    ((count * multiplier) / autoload_period);
2227c478bd9Sstevel@tonic-gate 		} else {
2237c478bd9Sstevel@tonic-gate 			msg(gettext("Missing autoload timeout period\n"));
2247c478bd9Sstevel@tonic-gate 			dumpabort();
2257c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 		break;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	case 'f':			/* output file */
2307c478bd9Sstevel@tonic-gate 		if (argc > 1) {
2317c478bd9Sstevel@tonic-gate 			argv++;
2327c478bd9Sstevel@tonic-gate 			argc--;
2337c478bd9Sstevel@tonic-gate 			tape = *argv;
2347c478bd9Sstevel@tonic-gate 			if (*tape == '\0') {
2357c478bd9Sstevel@tonic-gate 				msg(gettext("Bad output device name\n"));
2367c478bd9Sstevel@tonic-gate 				dumpabort();
2377c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
2387c478bd9Sstevel@tonic-gate 			}
2397c478bd9Sstevel@tonic-gate 		} else {
2407c478bd9Sstevel@tonic-gate 			msg(gettext("Missing output device name\n"));
2417c478bd9Sstevel@tonic-gate 			dumpabort();
2427c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		if (strcmp(tape, "-") == 0 && verify) {
2457c478bd9Sstevel@tonic-gate 			msg(gettext(
2467c478bd9Sstevel@tonic-gate 			"Cannot verify when dumping to standard out.\n"));
2477c478bd9Sstevel@tonic-gate 			dumpabort();
2487c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 		break;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	case 'd':			/* density, in bits per inch */
2537c478bd9Sstevel@tonic-gate 		if (argc > 1) {
2547c478bd9Sstevel@tonic-gate 			argv++;
2557c478bd9Sstevel@tonic-gate 			argc--;
2567c478bd9Sstevel@tonic-gate 			density = atoi(*argv) / 10;
2577c478bd9Sstevel@tonic-gate 			if (density <= 0) {
2587c478bd9Sstevel@tonic-gate 				msg(gettext(
2597c478bd9Sstevel@tonic-gate 				    "Density must be a positive integer\n"));
2607c478bd9Sstevel@tonic-gate 				dumpabort();
2617c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 		} else {
2647c478bd9Sstevel@tonic-gate 			msg(gettext("Missing density\n"));
2657c478bd9Sstevel@tonic-gate 			dumpabort();
2667c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 		break;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	case 's':			/* tape size, feet */
2717c478bd9Sstevel@tonic-gate 		if (argc > 1) {
2727c478bd9Sstevel@tonic-gate 			argv++;
2737c478bd9Sstevel@tonic-gate 			argc--;
2747c478bd9Sstevel@tonic-gate 			tsize = atol(*argv);
2757c478bd9Sstevel@tonic-gate 			if ((*argv[0] == '-') || (tsize == 0)) {
2767c478bd9Sstevel@tonic-gate 				msg(gettext(
2777c478bd9Sstevel@tonic-gate 			    "Tape size must be a positive integer\n"));
2787c478bd9Sstevel@tonic-gate 				dumpabort();
2797c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
2807c478bd9Sstevel@tonic-gate 			}
2817c478bd9Sstevel@tonic-gate 		} else {
2827c478bd9Sstevel@tonic-gate 			msg(gettext("Missing tape size\n"));
2837c478bd9Sstevel@tonic-gate 			dumpabort();
2847c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		break;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	case 't':			/* tracks */
2897c478bd9Sstevel@tonic-gate 		if (argc > 1) {
2907c478bd9Sstevel@tonic-gate 			argv++;
2917c478bd9Sstevel@tonic-gate 			argc--;
2927c478bd9Sstevel@tonic-gate 			tracks = atoi(*argv);
2937c478bd9Sstevel@tonic-gate 		} else {
2947c478bd9Sstevel@tonic-gate 			msg(gettext("Missing track count\n"));
2957c478bd9Sstevel@tonic-gate 			dumpabort();
2967c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		break;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	case 'b':			/* blocks per tape write */
3017c478bd9Sstevel@tonic-gate 		if (argc > 1) {
3027c478bd9Sstevel@tonic-gate 			argv++;
3037c478bd9Sstevel@tonic-gate 			argc--;
3047c478bd9Sstevel@tonic-gate 			bflag++;
3057c478bd9Sstevel@tonic-gate 			/*
3067c478bd9Sstevel@tonic-gate 			 * We save the ntrec in case we need to change
3077c478bd9Sstevel@tonic-gate 			 * tp_bsize later, we will have to recalculate
3087c478bd9Sstevel@tonic-gate 			 * it.
3097c478bd9Sstevel@tonic-gate 			 */
3107c478bd9Sstevel@tonic-gate 			saved_ntrec = ntrec = atoi(*argv);
3117c478bd9Sstevel@tonic-gate 			if (ntrec == 0 || (ntrec&1) || ntrec > (MAXNTREC*2)) {
3127c478bd9Sstevel@tonic-gate 				msg(gettext(
3137c478bd9Sstevel@tonic-gate 		    "Block size must be a positive, even integer <= %d\n"),
3147c478bd9Sstevel@tonic-gate 				    MAXNTREC*2);
3157c478bd9Sstevel@tonic-gate 				dumpabort();
3167c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 			ntrec /= (tp_bsize/DEV_BSIZE);
3197c478bd9Sstevel@tonic-gate 		} else {
3207c478bd9Sstevel@tonic-gate 			msg(gettext("Missing blocking factor\n"));
3217c478bd9Sstevel@tonic-gate 			dumpabort();
3227c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		break;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	case 'c':			/* Tape is cart. not 9-track */
3277c478bd9Sstevel@tonic-gate 	case 'C':			/* 'C' to be consistent with 'D' */
3287c478bd9Sstevel@tonic-gate 		cartridge++;
3297c478bd9Sstevel@tonic-gate 		break;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	case '0':			/* dump level */
3327c478bd9Sstevel@tonic-gate 	case '1':
3337c478bd9Sstevel@tonic-gate 	case '2':
3347c478bd9Sstevel@tonic-gate 	case '3':
3357c478bd9Sstevel@tonic-gate 	case '4':
3367c478bd9Sstevel@tonic-gate 	case '5':
3377c478bd9Sstevel@tonic-gate 	case '6':
3387c478bd9Sstevel@tonic-gate 	case '7':
3397c478bd9Sstevel@tonic-gate 	case '8':
3407c478bd9Sstevel@tonic-gate 	case '9':
3417c478bd9Sstevel@tonic-gate 		incno = arg[-1];
3427c478bd9Sstevel@tonic-gate 		break;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	case 'u':			/* update /etc/dumpdates */
3457c478bd9Sstevel@tonic-gate 		uflag++;
3467c478bd9Sstevel@tonic-gate 		break;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	case 'n':			/* notify operators */
3497c478bd9Sstevel@tonic-gate 		notify++;
3507c478bd9Sstevel@tonic-gate 		break;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	case 'a':			/* create archive file */
3537c478bd9Sstevel@tonic-gate 		archive = 1;
3547c478bd9Sstevel@tonic-gate 		if (argc > 1) {
3557c478bd9Sstevel@tonic-gate 			argv++;
3567c478bd9Sstevel@tonic-gate 			argc--;
3577c478bd9Sstevel@tonic-gate 			if (**argv == '\0') {
3587c478bd9Sstevel@tonic-gate 				msg(gettext("Bad archive file name\n"));
3597c478bd9Sstevel@tonic-gate 				dumpabort();
3607c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 			archivefile = strdup(*argv);
3637c478bd9Sstevel@tonic-gate 			if (archivefile == NULL) {
3647c478bd9Sstevel@tonic-gate 				saverr = errno;
3657c478bd9Sstevel@tonic-gate 				msg(gettext("Cannot allocate memory: %s\n"),
3667c478bd9Sstevel@tonic-gate 				    strerror(saverr));
3677c478bd9Sstevel@tonic-gate 				dumpabort();
3687c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
3697c478bd9Sstevel@tonic-gate 			}
3707c478bd9Sstevel@tonic-gate 		} else {
3717c478bd9Sstevel@tonic-gate 			msg(gettext("Missing archive file name\n"));
3727c478bd9Sstevel@tonic-gate 			dumpabort();
3737c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 		break;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	case 'v':
3787c478bd9Sstevel@tonic-gate 		verify++;
3797c478bd9Sstevel@tonic-gate 		doingverify++;
3807c478bd9Sstevel@tonic-gate 		if (strcmp(tape, "-") == 0) {
3817c478bd9Sstevel@tonic-gate 			msg(gettext(
3827c478bd9Sstevel@tonic-gate 			"Cannot verify when dumping to standard out.\n"));
3837c478bd9Sstevel@tonic-gate 			dumpabort();
3847c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		break;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	case 'D':
3897c478bd9Sstevel@tonic-gate 		diskette++;
3907c478bd9Sstevel@tonic-gate 		break;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	case 'N':
3937c478bd9Sstevel@tonic-gate 		if (argc > 1) {
3947c478bd9Sstevel@tonic-gate 			argv++;
3957c478bd9Sstevel@tonic-gate 			argc--;
3967c478bd9Sstevel@tonic-gate 			if (**argv == '\0') {
3977c478bd9Sstevel@tonic-gate 				msg(gettext("Missing name for dumpdates "
3987c478bd9Sstevel@tonic-gate 				    "entry.\n"));
3997c478bd9Sstevel@tonic-gate 				dumpabort();
4007c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
4017c478bd9Sstevel@tonic-gate 			}
4027c478bd9Sstevel@tonic-gate 			dname = *argv;
4037c478bd9Sstevel@tonic-gate 			if (strlen(dname) > MAXNAMLEN + 2) {
4047c478bd9Sstevel@tonic-gate 				msg(gettext("Dumpdates entry name too "
4057c478bd9Sstevel@tonic-gate 				    "long.\n"));
4067c478bd9Sstevel@tonic-gate 				dumpabort();
4077c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
4087c478bd9Sstevel@tonic-gate 			}
4097c478bd9Sstevel@tonic-gate 			for (i = 0; i < strlen(dname); i++) {
4107c478bd9Sstevel@tonic-gate 				if (isspace(*(dname+i))) {
4117c478bd9Sstevel@tonic-gate 					msg(gettext("Dumpdates entry name may "
4127c478bd9Sstevel@tonic-gate 					    "not contain white space.\n"));
4137c478bd9Sstevel@tonic-gate 					dumpabort();
4147c478bd9Sstevel@tonic-gate 					/*NOTREACHED*/
4157c478bd9Sstevel@tonic-gate 				}
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 		} else {
4187c478bd9Sstevel@tonic-gate 			msg(gettext("Missing name for dumpdates entry.\n"));
4197c478bd9Sstevel@tonic-gate 			dumpabort();
4207c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 		break;
4237c478bd9Sstevel@tonic-gate 	case 'L':
4247c478bd9Sstevel@tonic-gate 		if (argc > 1) {
4257c478bd9Sstevel@tonic-gate 			argv++;
4267c478bd9Sstevel@tonic-gate 			argc--;
4277c478bd9Sstevel@tonic-gate 			if (**argv == '\0') {
4287c478bd9Sstevel@tonic-gate 				msg(gettext("Missing tape label name\n"));
4297c478bd9Sstevel@tonic-gate 				dumpabort();
4307c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			tlabel = *argv;
4337c478bd9Sstevel@tonic-gate 			if (strlen(tlabel) > (sizeof (spcl.c_label) - 1)) {
4347c478bd9Sstevel@tonic-gate 				tlabel[sizeof (spcl.c_label) - 1] = '\0';
4357c478bd9Sstevel@tonic-gate 				msg(gettext(
4367c478bd9Sstevel@tonic-gate 		    "Truncating label to maximum supported length: `%s'\n"),
4377c478bd9Sstevel@tonic-gate 				    tlabel);
4387c478bd9Sstevel@tonic-gate 			}
4397c478bd9Sstevel@tonic-gate 		} else {
4407c478bd9Sstevel@tonic-gate 			msg(gettext("Missing tape label name\n"));
4417c478bd9Sstevel@tonic-gate 			dumpabort();
4427c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 		break;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	case 'l':
4477c478bd9Sstevel@tonic-gate 		autoload++;
4487c478bd9Sstevel@tonic-gate 		break;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	case 'o':
4517c478bd9Sstevel@tonic-gate 		offline++;
4527c478bd9Sstevel@tonic-gate 		break;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	case 'S':
4557c478bd9Sstevel@tonic-gate 		printsize++;
4567c478bd9Sstevel@tonic-gate 		break;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate #ifdef DEBUG
4597c478bd9Sstevel@tonic-gate 	case 'z':
4607c478bd9Sstevel@tonic-gate 		xflag++;
4617c478bd9Sstevel@tonic-gate 		break;
4627c478bd9Sstevel@tonic-gate #endif
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	default:
4657c478bd9Sstevel@tonic-gate 		msg(gettext("Bad option `%c'\n"), arg[-1]);
4667c478bd9Sstevel@tonic-gate 		dumpabort();
4677c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	if (argc > 1) {
4707c478bd9Sstevel@tonic-gate 		argv++;
4717c478bd9Sstevel@tonic-gate 		argc--;
4727c478bd9Sstevel@tonic-gate 		if (**argv == '\0') {
4737c478bd9Sstevel@tonic-gate 			msg(gettext("Bad disk name\n"));
4747c478bd9Sstevel@tonic-gate 			dumpabort();
4757c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 		disk = *argv;
4787c478bd9Sstevel@tonic-gate 		disk_dynamic = 0;
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 	if (disk == NULL) {
4817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4827c478bd9Sstevel@tonic-gate 	"Usage: %s [0123456789fustdWwnNDCcbavloS [argument]] filesystem\n"),
4837c478bd9Sstevel@tonic-gate 		    myname);
4847c478bd9Sstevel@tonic-gate 		Exit(X_ABORT);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	if (!filenum)
4877c478bd9Sstevel@tonic-gate 		filenum = 1;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, interrupt) == SIG_IGN)
4907c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (strcmp(tape, "-") == 0) {
4937c478bd9Sstevel@tonic-gate 		pipeout++;
4947c478bd9Sstevel@tonic-gate 		tape = gettext("standard output");
4957c478bd9Sstevel@tonic-gate 		dumpdev = sdumpdev = strdup(tape);
4967c478bd9Sstevel@tonic-gate 		if (dumpdev == NULL) {
4977c478bd9Sstevel@tonic-gate 			saverr = errno;
4987c478bd9Sstevel@tonic-gate 			msg(gettext("Cannot allocate memory: %s\n"),
4997c478bd9Sstevel@tonic-gate 			    strerror(saverr));
5007c478bd9Sstevel@tonic-gate 			dumpabort();
5017c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 		/*CONSTANTCONDITION*/
5047c478bd9Sstevel@tonic-gate 		assert(sizeof (spcl.c_label) > 5);
5057c478bd9Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, "none");
5067c478bd9Sstevel@tonic-gate 	} else if (*tape == '+') {
5077c478bd9Sstevel@tonic-gate 		nextdevice();
5087c478bd9Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, tlabel);
5097c478bd9Sstevel@tonic-gate 	} else {
5107c478bd9Sstevel@tonic-gate 		/* if not already set, set diskette to default */
5117c478bd9Sstevel@tonic-gate 		if (diskette && strcmp(tape, DEFTAPE) == 0)
5127c478bd9Sstevel@tonic-gate 			tape = DISKETTE;
5137c478bd9Sstevel@tonic-gate 		nextdevice();
5147c478bd9Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, tlabel);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	if (cartridge && diskette) {
5177c478bd9Sstevel@tonic-gate 		error = 1;
5187c478bd9Sstevel@tonic-gate 		msg(gettext("Cannot select both cartridge and diskette\n"));
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	if (density && diskette) {
5217c478bd9Sstevel@tonic-gate 		error = 1;
5227c478bd9Sstevel@tonic-gate 		msg(gettext("Cannot select density of diskette\n"));
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 	if (tracks && diskette) {
5257c478bd9Sstevel@tonic-gate 		error = 1;
5267c478bd9Sstevel@tonic-gate 		msg(gettext("Cannot select number of tracks of diskette\n"));
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	if (error) {
5297c478bd9Sstevel@tonic-gate 		dumpabort();
5307c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/*
5347c478bd9Sstevel@tonic-gate 	 * Determine how to default tape size and density
5357c478bd9Sstevel@tonic-gate 	 *
5367c478bd9Sstevel@tonic-gate 	 *		density				tape size
5377c478bd9Sstevel@tonic-gate 	 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
5387c478bd9Sstevel@tonic-gate 	 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
5397c478bd9Sstevel@tonic-gate 	 *
5407c478bd9Sstevel@tonic-gate 	 * Most Sun-2's came with 4 track (20MB) cartridge tape drives,
5417c478bd9Sstevel@tonic-gate 	 * while most other machines (Sun-3's and non-Sun's) come with
5427c478bd9Sstevel@tonic-gate 	 * 9 track (45MB) cartridge tape drives.  Some Sun-2's came with
5437c478bd9Sstevel@tonic-gate 	 * 9 track drives, but there is no way for the software to detect
5447c478bd9Sstevel@tonic-gate 	 * which drive type is installed.  Sigh...  We make the gross
5457c478bd9Sstevel@tonic-gate 	 * assumption that #ifdef mc68010 will test for a Sun-2.
5467c478bd9Sstevel@tonic-gate 	 *
5477c478bd9Sstevel@tonic-gate 	 * cartridge	8000 bpi (100 bytes/.1")	425 * tracks ft.
5487c478bd9Sstevel@tonic-gate 	 */
5497c478bd9Sstevel@tonic-gate 	if (density == 0)
5507c478bd9Sstevel@tonic-gate 		density = cartridge ? 100 : 625;
5517c478bd9Sstevel@tonic-gate 	if (tracks == 0)
5527c478bd9Sstevel@tonic-gate 		tracks = 9;
5537c478bd9Sstevel@tonic-gate 	if (!bflag) {
5547c478bd9Sstevel@tonic-gate 		if (cartridge)
5557c478bd9Sstevel@tonic-gate 			ntrec = CARTRIDGETREC;
5567c478bd9Sstevel@tonic-gate 		else if (diskette)
5577c478bd9Sstevel@tonic-gate 			ntrec = NTREC;
5587c478bd9Sstevel@tonic-gate 		else if (density >= 625)
5597c478bd9Sstevel@tonic-gate 			ntrec = HIGHDENSITYTREC;
5607c478bd9Sstevel@tonic-gate 		else
5617c478bd9Sstevel@tonic-gate 			ntrec = NTREC;
5627c478bd9Sstevel@tonic-gate 		/*
5637c478bd9Sstevel@tonic-gate 		 * save ntrec in case we have to change tp_bsize later.
5647c478bd9Sstevel@tonic-gate 		 */
5657c478bd9Sstevel@tonic-gate 		saved_ntrec = (ntrec * (tp_bsize/DEV_BSIZE));
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	if (!diskette) {
5687c478bd9Sstevel@tonic-gate 		tsize *= 12L*10L;
5697c478bd9Sstevel@tonic-gate 		if (cartridge)
5707c478bd9Sstevel@tonic-gate 			tsize *= tracks;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	rmtinit(msg, Exit);
5737c478bd9Sstevel@tonic-gate 	if (host) {
5747c478bd9Sstevel@tonic-gate 		char	*cp = strchr(host, '@');
5757c478bd9Sstevel@tonic-gate 		if (cp == (char *)0)
5767c478bd9Sstevel@tonic-gate 			cp = host;
5777c478bd9Sstevel@tonic-gate 		else
5787c478bd9Sstevel@tonic-gate 			cp++;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		if (rmthost(host, ntrec) == 0) {
5817c478bd9Sstevel@tonic-gate 			msg(gettext("Cannot connect to tape host `%s'\n"), cp);
5827c478bd9Sstevel@tonic-gate 			dumpabort();
5837c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, sigAbort) == SIG_IGN)
5877c478bd9Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_IGN);
5887c478bd9Sstevel@tonic-gate 	if (signal(SIGTRAP, sigAbort) == SIG_IGN)
5897c478bd9Sstevel@tonic-gate 		(void) signal(SIGTRAP, SIG_IGN);
5907c478bd9Sstevel@tonic-gate 	if (signal(SIGFPE, sigAbort) == SIG_IGN)
5917c478bd9Sstevel@tonic-gate 		(void) signal(SIGFPE, SIG_IGN);
5927c478bd9Sstevel@tonic-gate 	if (signal(SIGBUS, sigAbort) == SIG_IGN)
5937c478bd9Sstevel@tonic-gate 		(void) signal(SIGBUS, SIG_IGN);
5947c478bd9Sstevel@tonic-gate 	if (signal(SIGSEGV, sigAbort) == SIG_IGN)
5957c478bd9Sstevel@tonic-gate 		(void) signal(SIGSEGV, SIG_IGN);
5967c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, sigAbort) == SIG_IGN)
5977c478bd9Sstevel@tonic-gate 		(void) signal(SIGTERM, SIG_IGN);
5987c478bd9Sstevel@tonic-gate 	if (signal(SIGUSR1, sigAbort) == SIG_IGN)
5997c478bd9Sstevel@tonic-gate 		(void) signal(SIGUSR1, SIG_IGN);
6007c478bd9Sstevel@tonic-gate 	if (signal(SIGPIPE, sigAbort) == SIG_IGN)
6017c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, SIG_IGN);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	mnttabread();		/* /etc/fstab, /etc/mtab snarfed */
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/*
6067c478bd9Sstevel@tonic-gate 	 *	disk can be either the full special file name,
6077c478bd9Sstevel@tonic-gate 	 *	the suffix of the special file name,
6087c478bd9Sstevel@tonic-gate 	 *	the special name missing the leading '/',
6097c478bd9Sstevel@tonic-gate 	 *	the file system name with or without the leading '/'.
6107c478bd9Sstevel@tonic-gate 	 *	NB:  we attempt to avoid dumping the block device
6117c478bd9Sstevel@tonic-gate 	 *	(using rawname) because specfs and the vm system
6127c478bd9Sstevel@tonic-gate 	 *	are not necessarily in sync.
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	/*
616*4983fdb5Svk154806 	 * Attempt to roll the log if its root user before doing the dump.
617*4983fdb5Svk154806 	 * There's nothing the user can do if we are unable to roll the log,
618*4983fdb5Svk154806 	 * so we'll silently ignore failures.
6197c478bd9Sstevel@tonic-gate 	 */
620*4983fdb5Svk154806 	if (getuid() == 0 && rl_roll_log(disk) != RL_SUCCESS &&
621*4983fdb5Svk154806 	    disk[0] != '/') {
6227c478bd9Sstevel@tonic-gate 		/* Try it again with leading '/'. */
6237c478bd9Sstevel@tonic-gate 		char	*slashed;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		slashed = (char *)malloc(strlen(disk) + 2);
6267c478bd9Sstevel@tonic-gate 		if (slashed != (char *)NULL) {
6277c478bd9Sstevel@tonic-gate 			(void) sprintf(slashed, "%c%s", '/', disk);
6287c478bd9Sstevel@tonic-gate 			(void) rl_roll_log(slashed);
6297c478bd9Sstevel@tonic-gate 			free(slashed);
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	dt = mnttabsearch(disk, 0);
6337c478bd9Sstevel@tonic-gate 	if (dt != 0) {
6347c478bd9Sstevel@tonic-gate 		filesystem = dt->mnt_mountp;
6357c478bd9Sstevel@tonic-gate 		if (disk_dynamic) {
6367c478bd9Sstevel@tonic-gate 			/* LINTED: disk is not NULL */
6377c478bd9Sstevel@tonic-gate 			free(disk);
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 		disk = rawname(dt->mnt_special);
6407c478bd9Sstevel@tonic-gate 		disk_dynamic = (disk != dt->mnt_special);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		(void) strncpy(spcl.c_dev, dt->mnt_special,
6437c478bd9Sstevel@tonic-gate 		    sizeof (spcl.c_dev));
6447c478bd9Sstevel@tonic-gate 		spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0';
6457c478bd9Sstevel@tonic-gate 		(void) strncpy(spcl.c_filesys, dt->mnt_mountp,
6467c478bd9Sstevel@tonic-gate 		    sizeof (spcl.c_filesys));
6477c478bd9Sstevel@tonic-gate 		spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
6487c478bd9Sstevel@tonic-gate 	} else {
6497c478bd9Sstevel@tonic-gate 		(void) strncpy(spcl.c_dev, disk, sizeof (spcl.c_dev));
6507c478bd9Sstevel@tonic-gate 		spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0';
6517c478bd9Sstevel@tonic-gate #ifdef PARTIAL
6527c478bd9Sstevel@tonic-gate 		/* check for partial filesystem dump */
6537c478bd9Sstevel@tonic-gate 		partial_check();
6547c478bd9Sstevel@tonic-gate 		dt = mnttabsearch(disk, 1);
6557c478bd9Sstevel@tonic-gate 		if (dt != 0) {
6567c478bd9Sstevel@tonic-gate 			filesystem = dt->mnt_mountp;
6577c478bd9Sstevel@tonic-gate 			if (disk_dynamic)
6587c478bd9Sstevel@tonic-gate 				free(disk);
6597c478bd9Sstevel@tonic-gate 			disk = rawname(dt->mnt_special);
6607c478bd9Sstevel@tonic-gate 			disk_dynamic = (disk != dt->mnt_special);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 			(void) strncpy(spcl.c_filesys,
6637c478bd9Sstevel@tonic-gate 			    "a partial file system", sizeof (spcl.c_filesys));
6647c478bd9Sstevel@tonic-gate 			spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 		else
6677c478bd9Sstevel@tonic-gate #endif /* PARTIAL */
6687c478bd9Sstevel@tonic-gate 		{
6697c478bd9Sstevel@tonic-gate 			char *old_disk = disk;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 			(void) strncpy(spcl.c_filesys,
6727c478bd9Sstevel@tonic-gate 			    "an unlisted file system",
6737c478bd9Sstevel@tonic-gate 			    sizeof (spcl.c_filesys));
6747c478bd9Sstevel@tonic-gate 			spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			disk = rawname(old_disk);
6777c478bd9Sstevel@tonic-gate 			if (disk != old_disk) {
6787c478bd9Sstevel@tonic-gate 				if (disk_dynamic)
6797c478bd9Sstevel@tonic-gate 					free(old_disk);
6807c478bd9Sstevel@tonic-gate 				disk_dynamic = 1;
6817c478bd9Sstevel@tonic-gate 			}
6827c478bd9Sstevel@tonic-gate 			/*
6837c478bd9Sstevel@tonic-gate 			 * If disk == old_disk, then disk_dynamic's state
6847c478bd9Sstevel@tonic-gate 			 * does not change.
6857c478bd9Sstevel@tonic-gate 			 */
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	fi = open64(disk, O_RDONLY);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	if (fi < 0) {
6927c478bd9Sstevel@tonic-gate 		saverr = errno;
6937c478bd9Sstevel@tonic-gate 		msg(gettext("Cannot open dump device `%s': %s\n"),
6947c478bd9Sstevel@tonic-gate 		    disk, strerror(saverr));
6957c478bd9Sstevel@tonic-gate 		Exit(X_ABORT);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (sscanf(&incno, "%1d", &spcl.c_level) != 1) {
6997c478bd9Sstevel@tonic-gate 		msg(gettext("Bad dump level `%c' specified\n"), incno);
7007c478bd9Sstevel@tonic-gate 		dumpabort();
7017c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 	getitime();		/* /etc/dumpdates snarfed */
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	sblock = (struct fs *)&sblock_buf;
7067c478bd9Sstevel@tonic-gate 	sync();
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE);
7097c478bd9Sstevel@tonic-gate 	if ((sblock->fs_magic != FS_MAGIC) &&
7107c478bd9Sstevel@tonic-gate 	    (sblock->fs_magic != MTB_UFS_MAGIC)) {
7117c478bd9Sstevel@tonic-gate 		msg(gettext(
7127c478bd9Sstevel@tonic-gate 	    "Warning - super-block on device `%s' is corrupt - run fsck\n"),
7137c478bd9Sstevel@tonic-gate 		    disk);
7147c478bd9Sstevel@tonic-gate 		dumpabort();
7157c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7186451fdbcSvsakar 	if (sblock->fs_magic == FS_MAGIC &&
7196451fdbcSvsakar 	    (sblock->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
7206451fdbcSvsakar 	    sblock->fs_version != UFS_VERSION_MIN)) {
7216451fdbcSvsakar 		msg(gettext("Unrecognized UFS version: %d\n"),
7226451fdbcSvsakar 		    sblock->fs_version);
7236451fdbcSvsakar 		dumpabort();
7246451fdbcSvsakar 		/*NOTREACHED*/
7256451fdbcSvsakar 	}
7266451fdbcSvsakar 
7277c478bd9Sstevel@tonic-gate 	if (sblock->fs_magic == MTB_UFS_MAGIC &&
7287c478bd9Sstevel@tonic-gate 	    (sblock->fs_version < MTB_UFS_VERSION_MIN ||
7297c478bd9Sstevel@tonic-gate 	    sblock->fs_version > MTB_UFS_VERSION_1)) {
7307c478bd9Sstevel@tonic-gate 		msg(gettext("Unrecognized UFS version: %d\n"),
7317c478bd9Sstevel@tonic-gate 		    sblock->fs_version);
7327c478bd9Sstevel@tonic-gate 		dumpabort();
7337c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	/*
7377c478bd9Sstevel@tonic-gate 	 * Try to set up for using mmap(2).  It only works on the block
7387c478bd9Sstevel@tonic-gate 	 * device, but if we can use it, things go somewhat faster.  If
7397c478bd9Sstevel@tonic-gate 	 * we can't open it, we'll silently fall back to the old method
7407c478bd9Sstevel@tonic-gate 	 * (read/memcpy). We also only try this if it's been cleanly
7417c478bd9Sstevel@tonic-gate 	 * unmounted. Dumping a live filesystem this way runs into
7427c478bd9Sstevel@tonic-gate 	 * buffer consistency problems. Of course, we don't support
7437c478bd9Sstevel@tonic-gate 	 * running dump on a mounted filesystem, but some people do it
7447c478bd9Sstevel@tonic-gate 	 * anyway.
7457c478bd9Sstevel@tonic-gate 	 */
7467c478bd9Sstevel@tonic-gate 	if (sblock->fs_clean == FSCLEAN) {
7477c478bd9Sstevel@tonic-gate 		char *block = unrawname(disk);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		if (block != NULL) {
7507c478bd9Sstevel@tonic-gate 			mapfd = open(block, O_RDONLY, 0);
7517c478bd9Sstevel@tonic-gate 			free(block);
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate restart:
7567c478bd9Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE);
7577c478bd9Sstevel@tonic-gate 	if ((sblock->fs_magic != FS_MAGIC) &&
7587c478bd9Sstevel@tonic-gate 	    (sblock->fs_magic != MTB_UFS_MAGIC)) {	/* paranoia */
7597c478bd9Sstevel@tonic-gate 		msg(gettext("bad super-block magic number, run fsck\n"));
7607c478bd9Sstevel@tonic-gate 		dumpabort();
7617c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 
7646451fdbcSvsakar 	if (sblock->fs_magic == FS_MAGIC &&
7656451fdbcSvsakar 	    (sblock->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
7666451fdbcSvsakar 	    sblock->fs_version != UFS_VERSION_MIN)) {
7676451fdbcSvsakar 		msg(gettext("Unrecognized UFS version: %d\n"),
7686451fdbcSvsakar 		    sblock->fs_version);
7696451fdbcSvsakar 		dumpabort();
7706451fdbcSvsakar 		/*NOTREACHED*/
7716451fdbcSvsakar 	}
7726451fdbcSvsakar 
7737c478bd9Sstevel@tonic-gate 	if (sblock->fs_magic == MTB_UFS_MAGIC &&
7747c478bd9Sstevel@tonic-gate 	    (sblock->fs_version < MTB_UFS_VERSION_MIN ||
7757c478bd9Sstevel@tonic-gate 	    sblock->fs_version > MTB_UFS_VERSION_1)) {
7767c478bd9Sstevel@tonic-gate 		msg(gettext("Unrecognized UFS version: %d\n"),
7777c478bd9Sstevel@tonic-gate 		    sblock->fs_version);
7787c478bd9Sstevel@tonic-gate 		dumpabort();
7797c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (!doingactive)
7837c478bd9Sstevel@tonic-gate 		allocino();
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	/* XXX should sanity-check the super block before trusting/using it */
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/* LINTED XXX time truncated - tolerate until tape format changes */
7887c478bd9Sstevel@tonic-gate 	spcl.c_date = (time32_t)time((time_t *)NULL);
7897c478bd9Sstevel@tonic-gate 	bcopy(&(spcl.c_shadow), c_shadow_save, sizeof (c_shadow_save));
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	snapdate = is_fssnap_dump(disk);
7927c478bd9Sstevel@tonic-gate 	if (snapdate)
7937c478bd9Sstevel@tonic-gate 		spcl.c_date = snapdate;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (!printsize) {
7967c478bd9Sstevel@tonic-gate 		msg(gettext("Date of this level %c dump: %s\n"),
7977c478bd9Sstevel@tonic-gate 		    incno, prdate(spcl.c_date));
7987c478bd9Sstevel@tonic-gate 		msg(gettext("Date of last level %c dump: %s\n"),
7997c478bd9Sstevel@tonic-gate 		    (uchar_t)lastincno, prdate(spcl.c_ddate));
8007c478bd9Sstevel@tonic-gate 		msg(gettext("Dumping %s "), disk);
8017c478bd9Sstevel@tonic-gate 		if (filesystem != 0)
8027c478bd9Sstevel@tonic-gate 			msgtail("(%.*s:%s) ",
8037c478bd9Sstevel@tonic-gate 			    /* LINTED unsigned -> signed cast ok */
8047c478bd9Sstevel@tonic-gate 			    (int)sizeof (spcl.c_host), spcl.c_host, filesystem);
8057c478bd9Sstevel@tonic-gate 		msgtail(gettext("to %s.\n"), sdumpdev);
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	esize = f_esize = o_esize = 0;
8097c478bd9Sstevel@tonic-gate 	msiz = roundup(d_howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
8107c478bd9Sstevel@tonic-gate 	    TP_BSIZE_MAX);
8117c478bd9Sstevel@tonic-gate 	if (!doingactive) {
8127c478bd9Sstevel@tonic-gate 		clrmap = (uchar_t *)xcalloc(msiz, sizeof (*clrmap));
8137c478bd9Sstevel@tonic-gate 		filmap = (uchar_t *)xcalloc(msiz, sizeof (*filmap));
8147c478bd9Sstevel@tonic-gate 		dirmap = (uchar_t *)xcalloc(msiz, sizeof (*dirmap));
8157c478bd9Sstevel@tonic-gate 		nodmap = (uchar_t *)xcalloc(msiz, sizeof (*nodmap));
8167c478bd9Sstevel@tonic-gate 		shamap = (uchar_t *)xcalloc(msiz, sizeof (*shamap));
8177c478bd9Sstevel@tonic-gate 		activemap = (uchar_t *)xcalloc(msiz, sizeof (*activemap));
8187c478bd9Sstevel@tonic-gate 	} else {
8197c478bd9Sstevel@tonic-gate 		if (clrmap == NULL || filmap == NULL || dirmap == NULL ||
8207c478bd9Sstevel@tonic-gate 		    nodmap == NULL || shamap == NULL || activemap == NULL) {
8217c478bd9Sstevel@tonic-gate 			msg(gettext(
8227c478bd9Sstevel@tonic-gate 	    "Internal error: NULL map pointer while re-dumping active files"));
8237c478bd9Sstevel@tonic-gate 			dumpabort();
8247c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 		bzero(clrmap, msiz);
8277c478bd9Sstevel@tonic-gate 		bzero(filmap, msiz);
8287c478bd9Sstevel@tonic-gate 		bzero(dirmap, msiz);
8297c478bd9Sstevel@tonic-gate 		bzero(nodmap, msiz);
8307c478bd9Sstevel@tonic-gate 		bzero(shamap, msiz);
8317c478bd9Sstevel@tonic-gate 		/* retain active map */
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	dumpstate = DS_INIT;
8357c478bd9Sstevel@tonic-gate 	dumptoarchive = 1;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	/*
8387c478bd9Sstevel@tonic-gate 	 * Read cylinder group inode-used bitmaps to avoid reading clear inodes.
8397c478bd9Sstevel@tonic-gate 	 */
8407c478bd9Sstevel@tonic-gate 	{
8417c478bd9Sstevel@tonic-gate 		uchar_t *clrp = clrmap;
8427c478bd9Sstevel@tonic-gate 		struct cg *cgp =
8437c478bd9Sstevel@tonic-gate 		    (struct cg *)xcalloc((uint_t)sblock->fs_cgsize, 1);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		for (i = 0; i < sblock->fs_ncg; i++) {
8467c478bd9Sstevel@tonic-gate 			bread(fsbtodb(sblock, cgtod(sblock, i)),
8477c478bd9Sstevel@tonic-gate 			    (uchar_t *)cgp, sblock->fs_cgsize);
8487c478bd9Sstevel@tonic-gate 			bcopy(cg_inosused(cgp), clrp,
8497c478bd9Sstevel@tonic-gate 			    (int)sblock->fs_ipg / NBBY);
8507c478bd9Sstevel@tonic-gate 			clrp += sblock->fs_ipg / NBBY;
8517c478bd9Sstevel@tonic-gate 		}
8527c478bd9Sstevel@tonic-gate 		free((char *)cgp);
8537c478bd9Sstevel@tonic-gate 		/* XXX right-shift clrmap one bit.  why? */
8547c478bd9Sstevel@tonic-gate 		for (i = 0; clrp > clrmap; i <<= NBBY) {
8557c478bd9Sstevel@tonic-gate 			i |= *--clrp & ((1<<NBBY) - 1);
8567c478bd9Sstevel@tonic-gate 			*clrp = i >> 1;
8577c478bd9Sstevel@tonic-gate 		}
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (!printsize) {
8617c478bd9Sstevel@tonic-gate 		msgp = gettext("Mapping (Pass I) [regular files]\n");
8627c478bd9Sstevel@tonic-gate 		msg(msgp);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	ino = 0;
8667c478bd9Sstevel@tonic-gate #ifdef PARTIAL
8677c478bd9Sstevel@tonic-gate 	if (partial_mark(argc, argv)) {
8687c478bd9Sstevel@tonic-gate #endif /* PARTIAL */
8697c478bd9Sstevel@tonic-gate 		if (!doingactive)
8707c478bd9Sstevel@tonic-gate 			pass(mark, clrmap);	/* mark updates 'x'_esize */
8717c478bd9Sstevel@tonic-gate 		else
8727c478bd9Sstevel@tonic-gate 			pass(active_mark, clrmap);	/* updates 'x'_esize */
8737c478bd9Sstevel@tonic-gate #ifdef PARTIAL
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate #endif /* PARTIAL */
8767c478bd9Sstevel@tonic-gate 	do {
8777c478bd9Sstevel@tonic-gate 		if (!printsize) {
8787c478bd9Sstevel@tonic-gate 			msgp = gettext("Mapping (Pass II) [directories]\n");
8797c478bd9Sstevel@tonic-gate 			msg(msgp);
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 		nadded = 0;
8827c478bd9Sstevel@tonic-gate 		ino = 0;
8837c478bd9Sstevel@tonic-gate 		pass(add, dirmap);
8847c478bd9Sstevel@tonic-gate 	} while (nadded);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	ino = 0; /* adjust estimated size for shadow inodes */
8877c478bd9Sstevel@tonic-gate 	pass(markshad, nodmap);
8887c478bd9Sstevel@tonic-gate 	ino = 0;
8897c478bd9Sstevel@tonic-gate 	pass(estshad, shamap);
8907c478bd9Sstevel@tonic-gate 	freeshad();
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	bmapest(clrmap);
8937c478bd9Sstevel@tonic-gate 	bmapest(nodmap);
8947c478bd9Sstevel@tonic-gate 	esize = o_esize + f_esize;
8957c478bd9Sstevel@tonic-gate 	if (diskette) {
8967c478bd9Sstevel@tonic-gate 		/* estimate number of floppies */
8977c478bd9Sstevel@tonic-gate 		if (tsize != 0)
8987c478bd9Sstevel@tonic-gate 			fetapes = (double)(esize + ntrec) / (double)tsize;
8997c478bd9Sstevel@tonic-gate 	} else if (cartridge) {
9007c478bd9Sstevel@tonic-gate 		/*
9017c478bd9Sstevel@tonic-gate 		 * Estimate number of tapes, assuming streaming stops at
9027c478bd9Sstevel@tonic-gate 		 * the end of each block written, and not in mid-block.
9037c478bd9Sstevel@tonic-gate 		 * Assume no erroneous blocks; this can be compensated for
9047c478bd9Sstevel@tonic-gate 		 * with an artificially low tape size.
9057c478bd9Sstevel@tonic-gate 		 */
9067c478bd9Sstevel@tonic-gate 		tenthsperirg = 16;	/* actually 15.48, says Archive */
9077c478bd9Sstevel@tonic-gate 		if (tsize != 0)
9087c478bd9Sstevel@tonic-gate 			fetapes = ((double)esize /* blocks */
9097c478bd9Sstevel@tonic-gate 			    * (tp_bsize		/* bytes/block */
9107c478bd9Sstevel@tonic-gate 			    * (1.0/density))	/* 0.1" / byte */
9117c478bd9Sstevel@tonic-gate 			    +
9127c478bd9Sstevel@tonic-gate 			    (double)esize	/* blocks */
9137c478bd9Sstevel@tonic-gate 			    * (1.0/ntrec)	/* streaming-stops per block */
9147c478bd9Sstevel@tonic-gate 			    * tenthsperirg)	/* 0.1" / streaming-stop */
9157c478bd9Sstevel@tonic-gate 			    * (1.0 / tsize);	/* tape / 0.1" */
9167c478bd9Sstevel@tonic-gate 	} else {
9177c478bd9Sstevel@tonic-gate 		/* Estimate number of tapes, for old fashioned 9-track tape */
9187c478bd9Sstevel@tonic-gate #ifdef sun
9197c478bd9Sstevel@tonic-gate 		/* sun has long irg's */
9207c478bd9Sstevel@tonic-gate 		tenthsperirg = (density == 625) ? 6 : 12;
9217c478bd9Sstevel@tonic-gate #else
9227c478bd9Sstevel@tonic-gate 		tenthsperirg = (density == 625) ? 5 : 8;
9237c478bd9Sstevel@tonic-gate #endif
9247c478bd9Sstevel@tonic-gate 		if (tsize != 0)
9257c478bd9Sstevel@tonic-gate 			fetapes = ((double)esize /* blocks */
9267c478bd9Sstevel@tonic-gate 			    * (tp_bsize		/* bytes / block */
9277c478bd9Sstevel@tonic-gate 			    * (1.0/density))	/* 0.1" / byte */
9287c478bd9Sstevel@tonic-gate 			    +
9297c478bd9Sstevel@tonic-gate 			    (double)esize	/* blocks */
9307c478bd9Sstevel@tonic-gate 			    * (1.0/ntrec)	/* IRG's / block */
9317c478bd9Sstevel@tonic-gate 			    * tenthsperirg)	/* 0.1" / IRG */
9327c478bd9Sstevel@tonic-gate 			    * (1.0 / tsize);	/* tape / 0.1" */
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	etapes = fetapes;	/* truncating assignment */
9367c478bd9Sstevel@tonic-gate 	etapes++;
9377c478bd9Sstevel@tonic-gate 	/* count the nodemap on each additional tape */
9387c478bd9Sstevel@tonic-gate 	for (i = 1; i < etapes; i++)
9397c478bd9Sstevel@tonic-gate 		bmapest(nodmap);
9407c478bd9Sstevel@tonic-gate 	/*
9417c478bd9Sstevel@tonic-gate 	 * If the above bmapest is called, it changes o_esize and f_esize.
9427c478bd9Sstevel@tonic-gate 	 * So we will recalculate esize here anyway to make sure.
9437c478bd9Sstevel@tonic-gate 	 * Also, add tape headers and trailer records.
9447c478bd9Sstevel@tonic-gate 	 */
9457c478bd9Sstevel@tonic-gate 	esize = o_esize + f_esize + etapes + ntrec;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * If the estimated number of tp_bsize tape blocks is greater than
9497c478bd9Sstevel@tonic-gate 	 * INT_MAX we have to adjust tp_bsize and ntrec to handle
9507c478bd9Sstevel@tonic-gate 	 * the larger dump.  esize is an estimate, so we 'fudge'
9517c478bd9Sstevel@tonic-gate 	 * INT_MAX a little.  If tp_bsize is adjusted, it will be adjusted
9527c478bd9Sstevel@tonic-gate 	 * to the size needed for this dump (2048, 4096, 8192, ...)
9537c478bd9Sstevel@tonic-gate 	 */
9547c478bd9Sstevel@tonic-gate 	if (esize > (INT_MAX - FUDGE_FACTOR)) { /* esize is too big */
9557c478bd9Sstevel@tonic-gate 		forceflag++;
9567c478bd9Sstevel@tonic-gate 		esize_shift =
9577c478bd9Sstevel@tonic-gate 		    ((esize + (INT_MAX - FUDGE_FACTOR) - 1)/
9587c478bd9Sstevel@tonic-gate 		    ((u_offset_t)(INT_MAX - FUDGE_FACTOR))) - 1;
9597c478bd9Sstevel@tonic-gate 		if ((esize_shift > ESIZE_SHIFT_MAX) || (ntrec == 0)) {
9607c478bd9Sstevel@tonic-gate 			msgp = gettext(
9617c478bd9Sstevel@tonic-gate 	"Block factor %d ('b' flag) is too small for this size dump.");
9627c478bd9Sstevel@tonic-gate 			msg(msgp, saved_ntrec);
9637c478bd9Sstevel@tonic-gate 			dumpabort();
9647c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 		/*
9677c478bd9Sstevel@tonic-gate 		 * recalculate esize from:
9687c478bd9Sstevel@tonic-gate 		 * o_esize - header tape records
9697c478bd9Sstevel@tonic-gate 		 * (f_esize + (num_mult -1)) >> esize_shift - new non-header
9707c478bd9Sstevel@tonic-gate 		 *	tape records for files/maps
9717c478bd9Sstevel@tonic-gate 		 * etapes - TS_TAPE records
9727c478bd9Sstevel@tonic-gate 		 * ntrec - TS_END records
9737c478bd9Sstevel@tonic-gate 		 *
9747c478bd9Sstevel@tonic-gate 		 * ntrec is adjusted so a tape record is still 'b' flag
9757c478bd9Sstevel@tonic-gate 		 * number of DEV_BSIZE (512) in size
9767c478bd9Sstevel@tonic-gate 		 */
9777c478bd9Sstevel@tonic-gate 		new_mult = (tp_bsize << esize_shift)/tp_bsize;
9787c478bd9Sstevel@tonic-gate 		tp_bsize = (tp_bsize << esize_shift);
9797c478bd9Sstevel@tonic-gate 		esize = o_esize + ((f_esize +
9807c478bd9Sstevel@tonic-gate 		    (new_mult - 1)) >> esize_shift) + etapes + ntrec;
9817c478bd9Sstevel@tonic-gate 		ntrec = (saved_ntrec/(tp_bsize/DEV_BSIZE));
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 	if (forceflag != 0) {
9847c478bd9Sstevel@tonic-gate 		msgp = gettext(
9857c478bd9Sstevel@tonic-gate 		    "Forcing larger tape block size (%d).\n");
9867c478bd9Sstevel@tonic-gate 		msg(msgp, tp_bsize);
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate 	alloctape();			/* allocate tape buffers */
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	assert((tp_bsize / DEV_BSIZE != 0) && (tp_bsize % DEV_BSIZE == 0));
9917c478bd9Sstevel@tonic-gate 	/*
9927c478bd9Sstevel@tonic-gate 	 * If all we wanted was the size estimate,
9937c478bd9Sstevel@tonic-gate 	 * just print it out and exit.
9947c478bd9Sstevel@tonic-gate 	 */
9957c478bd9Sstevel@tonic-gate 	if (printsize) {
9967c478bd9Sstevel@tonic-gate 		(void) printf("%llu\n", esize * tp_bsize);
9977c478bd9Sstevel@tonic-gate 		Exit(0);
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if (tsize != 0) {
10017c478bd9Sstevel@tonic-gate 		if (diskette)
10027c478bd9Sstevel@tonic-gate 			msgp = gettext(
10037c478bd9Sstevel@tonic-gate 			    "Estimated %lld blocks (%s) on %3.2f diskettes.\n");
10047c478bd9Sstevel@tonic-gate 		else
10057c478bd9Sstevel@tonic-gate 			msgp = gettext(
10067c478bd9Sstevel@tonic-gate 			    "Estimated %lld blocks (%s) on %3.2f tapes.\n");
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		msg(msgp,
10097c478bd9Sstevel@tonic-gate 		    (esize*(tp_bsize/DEV_BSIZE)), mb(esize), fetapes);
10107c478bd9Sstevel@tonic-gate 	} else {
10117c478bd9Sstevel@tonic-gate 		msgp = gettext("Estimated %lld blocks (%s).\n");
10127c478bd9Sstevel@tonic-gate 		msg(msgp, (esize*(tp_bsize/DEV_BSIZE)), mb(esize));
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	dumpstate = DS_CLRI;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	otape(1);			/* bitmap is the first to tape write */
10187c478bd9Sstevel@tonic-gate 	*telapsed = 0;
10197c478bd9Sstevel@tonic-gate 	(void) time(tstart_writing);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/* filmap indicates all non-directory inodes */
10227c478bd9Sstevel@tonic-gate 	{
10237c478bd9Sstevel@tonic-gate 		uchar_t *np, *fp, *dp;
10247c478bd9Sstevel@tonic-gate 		np = nodmap;
10257c478bd9Sstevel@tonic-gate 		dp = dirmap;
10267c478bd9Sstevel@tonic-gate 		fp = filmap;
10277c478bd9Sstevel@tonic-gate 		for (i = 0; i < msiz; i++)
10287c478bd9Sstevel@tonic-gate 			*fp++ = *np++ ^ *dp++;
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	while (dumpstate != DS_DONE) {
10327c478bd9Sstevel@tonic-gate 		/*
10337c478bd9Sstevel@tonic-gate 		 * When we receive EOT notification from
10347c478bd9Sstevel@tonic-gate 		 * the writer, the signal handler calls
10357c478bd9Sstevel@tonic-gate 		 * rollforward and then jumps here.
10367c478bd9Sstevel@tonic-gate 		 */
10377c478bd9Sstevel@tonic-gate 		(void) setjmp(checkpoint_buf);
10387c478bd9Sstevel@tonic-gate 		switch (dumpstate) {
10397c478bd9Sstevel@tonic-gate 		case DS_INIT:
10407c478bd9Sstevel@tonic-gate 			/*
10417c478bd9Sstevel@tonic-gate 			 * We get here if a tape error occurred
10427c478bd9Sstevel@tonic-gate 			 * after releasing the name lock but before
10437c478bd9Sstevel@tonic-gate 			 * the volume containing the last of the
10447c478bd9Sstevel@tonic-gate 			 * dir info was completed.  We have to start
10457c478bd9Sstevel@tonic-gate 			 * all over in this case.
10467c478bd9Sstevel@tonic-gate 			 */
10477c478bd9Sstevel@tonic-gate 			{
10487c478bd9Sstevel@tonic-gate 				char *rmsg = gettext(
10497c478bd9Sstevel@tonic-gate 		"Warning - output error occurred after releasing name lock\n\
10507c478bd9Sstevel@tonic-gate \tThe dump will restart\n");
10517c478bd9Sstevel@tonic-gate 				msg(rmsg);
10527c478bd9Sstevel@tonic-gate 				goto restart;
10537c478bd9Sstevel@tonic-gate 			}
10547c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
10557c478bd9Sstevel@tonic-gate 		case DS_START:
10567c478bd9Sstevel@tonic-gate 		case DS_CLRI:
10577c478bd9Sstevel@tonic-gate 			ino = UFSROOTINO;
10587c478bd9Sstevel@tonic-gate 			dumptoarchive = 1;
10597c478bd9Sstevel@tonic-gate 			bitmap(clrmap, TS_CLRI);
10607c478bd9Sstevel@tonic-gate 			nextstate(DS_BITS);
10617c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
10627c478bd9Sstevel@tonic-gate 		case DS_BITS:
10637c478bd9Sstevel@tonic-gate 			ino = UFSROOTINO;
10647c478bd9Sstevel@tonic-gate 			dumptoarchive = 1;
10657c478bd9Sstevel@tonic-gate 			if (BIT(UFSROOTINO, nodmap))	/* empty dump check */
10667c478bd9Sstevel@tonic-gate 				bitmap(nodmap, TS_BITS);
10677c478bd9Sstevel@tonic-gate 			nextstate(DS_DIRS);
10687c478bd9Sstevel@tonic-gate 			if (!doingverify) {
10697c478bd9Sstevel@tonic-gate 				msgp = gettext(
10707c478bd9Sstevel@tonic-gate 				    "Dumping (Pass III) [directories]\n");
10717c478bd9Sstevel@tonic-gate 				msg(msgp);
10727c478bd9Sstevel@tonic-gate 			}
10737c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
10747c478bd9Sstevel@tonic-gate 		case DS_DIRS:
10757c478bd9Sstevel@tonic-gate 			dumptoarchive = 1;
10767c478bd9Sstevel@tonic-gate 			pass(dirdump, dirmap);
10777c478bd9Sstevel@tonic-gate 			nextstate(DS_FILES);
10787c478bd9Sstevel@tonic-gate 			if (!doingverify) {
10797c478bd9Sstevel@tonic-gate 				msgp = gettext(
10807c478bd9Sstevel@tonic-gate 				    "Dumping (Pass IV) [regular files]\n");
10817c478bd9Sstevel@tonic-gate 				msg(msgp);
10827c478bd9Sstevel@tonic-gate 			}
10837c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
10847c478bd9Sstevel@tonic-gate 		case DS_FILES:
10857c478bd9Sstevel@tonic-gate 			dumptoarchive = 0;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 			pass(lf_dump, filmap);
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 			flushcmds();
10907c478bd9Sstevel@tonic-gate 			dumpstate = DS_END;	/* don't reset ino */
10917c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
10927c478bd9Sstevel@tonic-gate 		case DS_END:
10937c478bd9Sstevel@tonic-gate 			dumptoarchive = 1;
10947c478bd9Sstevel@tonic-gate 			spcl.c_type = TS_END;
10957c478bd9Sstevel@tonic-gate 			for (i = 0; i < ntrec; i++) {
10967c478bd9Sstevel@tonic-gate 				spclrec();
10977c478bd9Sstevel@tonic-gate 			}
10987c478bd9Sstevel@tonic-gate 			flusht();
10997c478bd9Sstevel@tonic-gate 			break;
11007c478bd9Sstevel@tonic-gate 		case DS_DONE:
11017c478bd9Sstevel@tonic-gate 			break;
11027c478bd9Sstevel@tonic-gate 		default:
11037c478bd9Sstevel@tonic-gate 			msg(gettext("Internal state error\n"));
11047c478bd9Sstevel@tonic-gate 			dumpabort();
11057c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if ((! doingactive) && (! active))
11107c478bd9Sstevel@tonic-gate 		trewind();
11117c478bd9Sstevel@tonic-gate 	if (verify && !doingverify) {
11127c478bd9Sstevel@tonic-gate 		msgp = gettext("Finished writing last dump volume\n");
11137c478bd9Sstevel@tonic-gate 		msg(msgp);
11147c478bd9Sstevel@tonic-gate 		Exit(X_VERIFY);
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 	if (spcl.c_volume > 1)
11177c478bd9Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
11187c478bd9Sstevel@tonic-gate 		    gettext("%lld blocks (%s) on %ld volumes"),
11197c478bd9Sstevel@tonic-gate 		    ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)),
11207c478bd9Sstevel@tonic-gate 		    mb((u_offset_t)(unsigned)(spcl.c_tapea)),
11217c478bd9Sstevel@tonic-gate 		    spcl.c_volume);
11227c478bd9Sstevel@tonic-gate 	else
11237c478bd9Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
11247c478bd9Sstevel@tonic-gate 		    gettext("%lld blocks (%s) on 1 volume"),
11257c478bd9Sstevel@tonic-gate 		    ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)),
11267c478bd9Sstevel@tonic-gate 		    mb((u_offset_t)(unsigned)(spcl.c_tapea)));
11277c478bd9Sstevel@tonic-gate 	if (timeclock((time_t)0) != (time_t)0) {
11287c478bd9Sstevel@tonic-gate 		(void) snprintf(kbsbuf, sizeof (kbsbuf),
11297c478bd9Sstevel@tonic-gate 		    gettext(" at %ld KB/sec"),
11307c478bd9Sstevel@tonic-gate 		    (long)(((float)spcl.c_tapea / (float)timeclock((time_t)0))
11317c478bd9Sstevel@tonic-gate 		    * 1000.0));
11327c478bd9Sstevel@tonic-gate 		(void) strcat(msgbuf, kbsbuf);
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 	(void) strcat(msgbuf, "\n");
11357c478bd9Sstevel@tonic-gate 	msg(msgbuf);
11367c478bd9Sstevel@tonic-gate 	(void) timeclock((time_t)-1);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	if (archive)
11397c478bd9Sstevel@tonic-gate 		msg(gettext("Archiving dump to `%s'\n"), archivefile);
11407c478bd9Sstevel@tonic-gate 	if (active && !verify) {
11417c478bd9Sstevel@tonic-gate 		nextstate(DS_INIT);
11427c478bd9Sstevel@tonic-gate 		activepass();
11437c478bd9Sstevel@tonic-gate 		goto restart;
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 	msgp = gettext("DUMP IS DONE\n");
11467c478bd9Sstevel@tonic-gate 	msg(msgp);
11477c478bd9Sstevel@tonic-gate 	broadcast(msgp);
11487c478bd9Sstevel@tonic-gate 	if (! doingactive)
11497c478bd9Sstevel@tonic-gate 		putitime();
11507c478bd9Sstevel@tonic-gate 	Exit(X_FINOK);
1151fe0e7ec4Smaheshvs 
1152fe0e7ec4Smaheshvs 	/*NOTREACHED*/
11537c478bd9Sstevel@tonic-gate 	return (0);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate void
1157fe0e7ec4Smaheshvs sigAbort(int sig)
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate 	char	*sigtype;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	switch (sig) {
11627c478bd9Sstevel@tonic-gate 	case SIGHUP:
11637c478bd9Sstevel@tonic-gate 		sigtype = "SIGHUP";
11647c478bd9Sstevel@tonic-gate 		break;
11657c478bd9Sstevel@tonic-gate 	case SIGTRAP:
11667c478bd9Sstevel@tonic-gate 		sigtype = "SIGTRAP";
11677c478bd9Sstevel@tonic-gate 		break;
11687c478bd9Sstevel@tonic-gate 	case SIGFPE:
11697c478bd9Sstevel@tonic-gate 		sigtype = "SIGFPE";
11707c478bd9Sstevel@tonic-gate 		break;
11717c478bd9Sstevel@tonic-gate 	case SIGBUS:
11727c478bd9Sstevel@tonic-gate 		msg(gettext("%s  ABORTING!\n"), "SIGBUS()");
11737c478bd9Sstevel@tonic-gate 		(void) signal(SIGUSR2, SIG_DFL);
11747c478bd9Sstevel@tonic-gate 		abort();
11757c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
11767c478bd9Sstevel@tonic-gate 	case SIGSEGV:
11777c478bd9Sstevel@tonic-gate 		msg(gettext("%s  ABORTING!\n"), "SIGSEGV()");
11787c478bd9Sstevel@tonic-gate 		(void) signal(SIGUSR2, SIG_DFL);
11797c478bd9Sstevel@tonic-gate 		abort();
11807c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
11817c478bd9Sstevel@tonic-gate 	case SIGALRM:
11827c478bd9Sstevel@tonic-gate 		sigtype = "SIGALRM";
11837c478bd9Sstevel@tonic-gate 		break;
11847c478bd9Sstevel@tonic-gate 	case SIGTERM:
11857c478bd9Sstevel@tonic-gate 		sigtype = "SIGTERM";
11867c478bd9Sstevel@tonic-gate 		break;
11877c478bd9Sstevel@tonic-gate 	case SIGPIPE:
11887c478bd9Sstevel@tonic-gate 		msg(gettext("Broken pipe\n"));
11897c478bd9Sstevel@tonic-gate 		dumpabort();
11907c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
11917c478bd9Sstevel@tonic-gate 	default:
11927c478bd9Sstevel@tonic-gate 		sigtype = "SIGNAL";
11937c478bd9Sstevel@tonic-gate 		break;
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 	msg(gettext("%s()  try rewriting\n"), sigtype);
11967c478bd9Sstevel@tonic-gate 	if (pipeout) {
11977c478bd9Sstevel@tonic-gate 		msg(gettext("Unknown signal, Cannot recover\n"));
11987c478bd9Sstevel@tonic-gate 		dumpabort();
11997c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 	msg(gettext("Rewriting attempted as response to unknown signal.\n"));
12027c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
12037c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
12047c478bd9Sstevel@tonic-gate 	close_rewind();
12057c478bd9Sstevel@tonic-gate 	Exit(X_REWRITE);
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate /* Note that returned value is malloc'd if != cp && != NULL */
12097c478bd9Sstevel@tonic-gate char *
1210fe0e7ec4Smaheshvs rawname(char *cp)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	struct stat64 st;
12137c478bd9Sstevel@tonic-gate 	char *dp;
12147c478bd9Sstevel@tonic-gate 	extern char *getfullrawname();
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	if (stat64(cp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFBLK)
12177c478bd9Sstevel@tonic-gate 		return (cp);
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	dp = getfullrawname(cp);
12207c478bd9Sstevel@tonic-gate 	if (dp == 0)
12217c478bd9Sstevel@tonic-gate 		return (0);
12227c478bd9Sstevel@tonic-gate 	if (*dp == '\0') {
12237c478bd9Sstevel@tonic-gate 		free(dp);
12247c478bd9Sstevel@tonic-gate 		return (0);
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	if (stat64(dp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFCHR) {
12287c478bd9Sstevel@tonic-gate 		free(dp);
12297c478bd9Sstevel@tonic-gate 		return (cp);
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	return (dp);
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate static char *
1236fe0e7ec4Smaheshvs mb(u_offset_t blks)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate 	static char buf[16];
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	if (blks < 1024)
12417c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%lldKB", blks);
12427c478bd9Sstevel@tonic-gate 	else
12437c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%.2fMB",
12447c478bd9Sstevel@tonic-gate 		    ((double)(blks*tp_bsize)) / (double)(1024*1024));
12457c478bd9Sstevel@tonic-gate 	return (buf);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate #ifdef signal
1249fe0e7ec4Smaheshvs void (*nsignal(int sig, void (*act)(int)))(int)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	struct sigaction sa, osa;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	sa.sa_handler = act;
12547c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
12557c478bd9Sstevel@tonic-gate 	sa.sa_flags = SA_RESTART;
12567c478bd9Sstevel@tonic-gate 	if (sigaction(sig, &sa, &osa) < 0)
12577c478bd9Sstevel@tonic-gate 		return ((void (*)(int))-1);
12587c478bd9Sstevel@tonic-gate 	return (osa.sa_handler);
12597c478bd9Sstevel@tonic-gate }
12607c478bd9Sstevel@tonic-gate #endif
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate static void
1263fe0e7ec4Smaheshvs nextstate(int state)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
12667c478bd9Sstevel@tonic-gate 	dumpstate = state;
12677c478bd9Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
12687c478bd9Sstevel@tonic-gate 	ino = 0;
12697c478bd9Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
12707c478bd9Sstevel@tonic-gate 	pos = 0;
12717c478bd9Sstevel@tonic-gate 	leftover = 0;
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate /*
12757c478bd9Sstevel@tonic-gate  * timeclock() function, for keeping track of how much time we've spent
12767c478bd9Sstevel@tonic-gate  * writing to the tape device.  it always returns the amount of time
12777c478bd9Sstevel@tonic-gate  * already spent, in milliseconds.  if you pass it a positive, then that's
12787c478bd9Sstevel@tonic-gate  * telling it that we're writing, so the time counts.  if you pass it a
12797c478bd9Sstevel@tonic-gate  * zero, then that's telling it we're not writing; perhaps we're waiting
12807c478bd9Sstevel@tonic-gate  * for user input.
12817c478bd9Sstevel@tonic-gate  *
12827c478bd9Sstevel@tonic-gate  * a state of -1 resets everything.
12837c478bd9Sstevel@tonic-gate  */
12847c478bd9Sstevel@tonic-gate time32_t
1285fe0e7ec4Smaheshvs timeclock(time32_t state)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	static int *currentState = NULL;
12887c478bd9Sstevel@tonic-gate 	static struct timeval *clockstart;
12897c478bd9Sstevel@tonic-gate 	static time32_t *emilli;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	struct timeval current[1];
12927c478bd9Sstevel@tonic-gate 	int fd, saverr;
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate #ifdef DEBUG
12957c478bd9Sstevel@tonic-gate 	fprintf(stderr, "pid=%d timeclock ", getpid());
12967c478bd9Sstevel@tonic-gate 	if (state == (time32_t)-1)
12977c478bd9Sstevel@tonic-gate 		fprintf(stderr, "cleared\n");
12987c478bd9Sstevel@tonic-gate 	else if (state > 0)
12997c478bd9Sstevel@tonic-gate 		fprintf(stderr, "ticking\n");
13007c478bd9Sstevel@tonic-gate 	else
13017c478bd9Sstevel@tonic-gate 		fprintf(stderr, "paused\n");
13027c478bd9Sstevel@tonic-gate #endif /* DEBUG */
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	/* if we haven't setup the shared memory, init */
13057c478bd9Sstevel@tonic-gate 	if (currentState == (int *)NULL) {
13067c478bd9Sstevel@tonic-gate 		if ((fd = open("/dev/zero", O_RDWR)) < 0) {
13077c478bd9Sstevel@tonic-gate 			saverr = errno;
13087c478bd9Sstevel@tonic-gate 			msg(gettext("Cannot open `%s': %s\n"),
13097c478bd9Sstevel@tonic-gate 			    "/dev/zero", strerror(saverr));
13107c478bd9Sstevel@tonic-gate 			dumpabort();
13117c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 		/*LINTED [mmap always returns an aligned value]*/
13147c478bd9Sstevel@tonic-gate 		currentState = (int *)mmap((char *)0, getpagesize(),
13157c478bd9Sstevel@tonic-gate 		    PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0);
13167c478bd9Sstevel@tonic-gate 		if (currentState == (int *)-1) {
13177c478bd9Sstevel@tonic-gate 			saverr = errno;
13187c478bd9Sstevel@tonic-gate 			msg(gettext(
13197c478bd9Sstevel@tonic-gate 			    "Cannot memory map monitor variables: %s\n"),
13207c478bd9Sstevel@tonic-gate 			    strerror(saverr));
13217c478bd9Sstevel@tonic-gate 			dumpabort();
13227c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
13237c478bd9Sstevel@tonic-gate 		}
13247c478bd9Sstevel@tonic-gate 		(void) close(fd);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 		/* LINTED currentState is sufficiently aligned */
13277c478bd9Sstevel@tonic-gate 		clockstart = (struct timeval *)(currentState + 1);
13287c478bd9Sstevel@tonic-gate 		emilli = (time32_t *)(clockstart + 1);
13297c478bd9Sstevel@tonic-gate 		/* Note everything is initialized to zero via /dev/zero */
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	if (state == (time32_t)-1) {
13337c478bd9Sstevel@tonic-gate 		bzero(clockstart, sizeof (*clockstart));
13347c478bd9Sstevel@tonic-gate 		*currentState = 0;
13357c478bd9Sstevel@tonic-gate 		*emilli = (time32_t)0;
13367c478bd9Sstevel@tonic-gate 		return (0);
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	(void) gettimeofday(current, NULL);
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	if (*currentState != 0) {
13427c478bd9Sstevel@tonic-gate 		current->tv_usec += 1000000;
13437c478bd9Sstevel@tonic-gate 		current->tv_sec--;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 		/* LINTED: result will fit in a time32_t */
13467c478bd9Sstevel@tonic-gate 		*emilli += (current->tv_sec - clockstart->tv_sec) * 1000;
13477c478bd9Sstevel@tonic-gate 		/* LINTED: result will fit in a time32_t */
13487c478bd9Sstevel@tonic-gate 		*emilli += (current->tv_usec - clockstart->tv_usec) / 1000;
13497c478bd9Sstevel@tonic-gate 	}
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	if (state != 0)
13527c478bd9Sstevel@tonic-gate 		bcopy(current, clockstart, sizeof (current));
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	*currentState = state;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	return (*emilli);
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate static int
13607c478bd9Sstevel@tonic-gate statcmp(const struct stat64 *left, const struct stat64 *right)
13617c478bd9Sstevel@tonic-gate {
13627c478bd9Sstevel@tonic-gate 	int result = 1;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	if ((left->st_dev == right->st_dev) &&
13657c478bd9Sstevel@tonic-gate 	    (left->st_ino == right->st_ino) &&
13667c478bd9Sstevel@tonic-gate 	    (left->st_mode == right->st_mode) &&
13677c478bd9Sstevel@tonic-gate 	    (left->st_nlink == right->st_nlink) &&
13687c478bd9Sstevel@tonic-gate 	    (left->st_uid == right->st_uid) &&
13697c478bd9Sstevel@tonic-gate 	    (left->st_gid == right->st_gid) &&
13707c478bd9Sstevel@tonic-gate 	    (left->st_rdev == right->st_rdev) &&
13717c478bd9Sstevel@tonic-gate 	    (left->st_ctim.tv_sec == right->st_ctim.tv_sec) &&
13727c478bd9Sstevel@tonic-gate 	    (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) &&
13737c478bd9Sstevel@tonic-gate 	    (left->st_mtim.tv_sec == right->st_mtim.tv_sec) &&
13744098eb52Sws195443 	    (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec)) {
13754098eb52Sws195443 		/*
13764098eb52Sws195443 		 * Unlike in the ufsrestore version
13774098eb52Sws195443 		 * st_blocks and st_blksiz are not
13784098eb52Sws195443 		 * compared. The reason for this is
13794098eb52Sws195443 		 * problems with zfs dump files. Zfs
13804098eb52Sws195443 		 * changes it's statistics in those
13814098eb52Sws195443 		 * fields.
13824098eb52Sws195443 		 */
13837c478bd9Sstevel@tonic-gate 		result = 0;
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	return (result);
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate /*
13907c478bd9Sstevel@tonic-gate  * Safely open a file or device.
13917c478bd9Sstevel@tonic-gate  */
13927c478bd9Sstevel@tonic-gate static int
13937c478bd9Sstevel@tonic-gate safe_open_common(const char *filename, int mode, int perms, int device)
13947c478bd9Sstevel@tonic-gate {
13957c478bd9Sstevel@tonic-gate 	int fd;
13967c478bd9Sstevel@tonic-gate 	int working_mode;
13977c478bd9Sstevel@tonic-gate 	int saverr;
13987c478bd9Sstevel@tonic-gate 	char *errtext;
13997c478bd9Sstevel@tonic-gate 	struct stat64 pre_stat, pre_lstat;
14007c478bd9Sstevel@tonic-gate 	struct stat64 post_stat, post_lstat;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	/*
14037c478bd9Sstevel@tonic-gate 	 * Don't want to be spoofed into trashing something we
14047c478bd9Sstevel@tonic-gate 	 * shouldn't, thus the following rigamarole.  If it doesn't
14057c478bd9Sstevel@tonic-gate 	 * exist, we create it and proceed.  Otherwise, require that
14067c478bd9Sstevel@tonic-gate 	 * what's there be a real file with no extraneous links and
14077c478bd9Sstevel@tonic-gate 	 * owned by whoever ran us.
14087c478bd9Sstevel@tonic-gate 	 *
14097c478bd9Sstevel@tonic-gate 	 * The silliness with using both lstat() and fstat() is to avoid
14107c478bd9Sstevel@tonic-gate 	 * race-condition games with someone replacing the file with a
14117c478bd9Sstevel@tonic-gate 	 * symlink after we've opened it.  If there was an flstat(),
14127c478bd9Sstevel@tonic-gate 	 * we wouldn't need the fstat().
14137c478bd9Sstevel@tonic-gate 	 *
14147c478bd9Sstevel@tonic-gate 	 * The initial open with the hard-coded flags is ok even if we
14157c478bd9Sstevel@tonic-gate 	 * are intending to open only for reading.  If it succeeds,
14167c478bd9Sstevel@tonic-gate 	 * then the file did not exist, and we'll synthesize an appropriate
14177c478bd9Sstevel@tonic-gate 	 * complaint below.  Otherwise, it does exist, so we won't be
14187c478bd9Sstevel@tonic-gate 	 * truncating it with the open.
14197c478bd9Sstevel@tonic-gate 	 */
14207c478bd9Sstevel@tonic-gate 	if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE,
14217c478bd9Sstevel@tonic-gate 	    perms)) < 0) {
14227c478bd9Sstevel@tonic-gate 		if (errno == EEXIST) {
14237c478bd9Sstevel@tonic-gate 			if (lstat64(filename, &pre_lstat) < 0) {
14247c478bd9Sstevel@tonic-gate 				return (-1);
14257c478bd9Sstevel@tonic-gate 			}
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 			if (stat64(filename, &pre_stat) < 0) {
14287c478bd9Sstevel@tonic-gate 				return (-1);
14297c478bd9Sstevel@tonic-gate 			}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 			working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY);
14327c478bd9Sstevel@tonic-gate 			working_mode |= O_LARGEFILE;
14337c478bd9Sstevel@tonic-gate 			if ((fd = open(filename, working_mode)) < 0) {
14347c478bd9Sstevel@tonic-gate 				if (errno == ENOENT) {
14357c478bd9Sstevel@tonic-gate 					errtext = gettext(
14367c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s used to exist, but doesn't any longer\n");
14377c478bd9Sstevel@tonic-gate 					msg(errtext, filename);
14387c478bd9Sstevel@tonic-gate 					syslog(LOG_WARNING, errtext, filename);
14397c478bd9Sstevel@tonic-gate 					errno = ENOENT;
14407c478bd9Sstevel@tonic-gate 				}
14417c478bd9Sstevel@tonic-gate 				return (-1);
14427c478bd9Sstevel@tonic-gate 			}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 			if (lstat64(filename, &post_lstat) < 0) {
14457c478bd9Sstevel@tonic-gate 				saverr = errno;
14467c478bd9Sstevel@tonic-gate 				(void) close(fd);
14477c478bd9Sstevel@tonic-gate 				errno = saverr;
14487c478bd9Sstevel@tonic-gate 				return (-1);
14497c478bd9Sstevel@tonic-gate 			}
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 			if (fstat64(fd, &post_stat) < 0) {
14527c478bd9Sstevel@tonic-gate 				saverr = errno;
14537c478bd9Sstevel@tonic-gate 				(void) close(fd);
14547c478bd9Sstevel@tonic-gate 				errno = saverr;
14557c478bd9Sstevel@tonic-gate 				return (-1);
14567c478bd9Sstevel@tonic-gate 			}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 			/*
14597c478bd9Sstevel@tonic-gate 			 * Can't just use memcmp(3C), because the access
14607c478bd9Sstevel@tonic-gate 			 * time is updated by open(2).
14617c478bd9Sstevel@tonic-gate 			 */
14627c478bd9Sstevel@tonic-gate 			if (statcmp(&pre_lstat, &post_lstat) != 0) {
14634098eb52Sws195443 				errtext = gettext("Unexpected change detected: "
14644098eb52Sws195443 				    "%s's lstat(2) information changed\n");
14657c478bd9Sstevel@tonic-gate 				msg(errtext, filename);
14667c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
14677c478bd9Sstevel@tonic-gate 				errno = EPERM;
14687c478bd9Sstevel@tonic-gate 				return (-1);
14697c478bd9Sstevel@tonic-gate 			}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 			if (statcmp(&pre_stat, &post_stat) != 0) {
14724098eb52Sws195443 				errtext = gettext("Unexpected change detected: "
14734098eb52Sws195443 				    "%s's stat(2) information changed\n"),
14747c478bd9Sstevel@tonic-gate 				    msg(errtext, filename);
14757c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
14767c478bd9Sstevel@tonic-gate 				errno = EPERM;
14777c478bd9Sstevel@tonic-gate 				return (-1);
14787c478bd9Sstevel@tonic-gate 			}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 			/*
14817c478bd9Sstevel@tonic-gate 			 * If inode, device, or type are wrong, bail out.
14827c478bd9Sstevel@tonic-gate 			 * Note using post_stat instead of post_lstat for the
14837c478bd9Sstevel@tonic-gate 			 * S_ISCHR() test.  This is to allow the /dev ->
14847c478bd9Sstevel@tonic-gate 			 * /devices bit to work, as long as the final target
14857c478bd9Sstevel@tonic-gate 			 * is a character device (i.e., raw disk or tape).
14867c478bd9Sstevel@tonic-gate 			 */
14877c478bd9Sstevel@tonic-gate 			if (device && !(S_ISCHR(post_stat.st_mode)) &&
14887c478bd9Sstevel@tonic-gate 			    !(S_ISFIFO(post_stat.st_mode)) &&
14897c478bd9Sstevel@tonic-gate 			    !(S_ISREG(post_lstat.st_mode))) {
14904098eb52Sws195443 				errtext = gettext("Unexpected condition "
14914098eb52Sws195443 				    "detected: %s is not a supported device\n"),
14927c478bd9Sstevel@tonic-gate 				    msg(errtext, filename);
14937c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
14947c478bd9Sstevel@tonic-gate 				(void) close(fd);
14957c478bd9Sstevel@tonic-gate 				errno = EPERM;
14967c478bd9Sstevel@tonic-gate 				return (-1);
14977c478bd9Sstevel@tonic-gate 			} else if (!device &&
14987c478bd9Sstevel@tonic-gate 			    (!S_ISREG(post_lstat.st_mode) ||
14997c478bd9Sstevel@tonic-gate 			    (post_stat.st_ino != post_lstat.st_ino) ||
15007c478bd9Sstevel@tonic-gate 			    (post_stat.st_dev != post_lstat.st_dev))) {
15014098eb52Sws195443 				errtext = gettext("Unexpected condition "
15024098eb52Sws195443 				    "detected: %s is not a regular file\n"),
15037c478bd9Sstevel@tonic-gate 				    msg(errtext, filename);
15047c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
15057c478bd9Sstevel@tonic-gate 				(void) close(fd);
15067c478bd9Sstevel@tonic-gate 				errno = EPERM;
15077c478bd9Sstevel@tonic-gate 				return (-1);
15087c478bd9Sstevel@tonic-gate 			}
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 			/*
15117c478bd9Sstevel@tonic-gate 			 * Bad link count implies someone's linked our
15127c478bd9Sstevel@tonic-gate 			 * target to something else, which we probably
15137c478bd9Sstevel@tonic-gate 			 * shouldn't step on.
15147c478bd9Sstevel@tonic-gate 			 */
15157c478bd9Sstevel@tonic-gate 			if (post_lstat.st_nlink != 1) {
15164098eb52Sws195443 				errtext = gettext("Unexpected condition "
15174098eb52Sws195443 				    "detected: %s must have exactly one "
15184098eb52Sws195443 				    "link\n"), msg(errtext, filename);
15197c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
15207c478bd9Sstevel@tonic-gate 				(void) close(fd);
15217c478bd9Sstevel@tonic-gate 				errno = EPERM;
15227c478bd9Sstevel@tonic-gate 				return (-1);
15237c478bd9Sstevel@tonic-gate 			}
15247c478bd9Sstevel@tonic-gate 			/*
15257c478bd9Sstevel@tonic-gate 			 * Root might make a file, but non-root might
15267c478bd9Sstevel@tonic-gate 			 * need to open it.  If the permissions let us
15277c478bd9Sstevel@tonic-gate 			 * get this far, then let it through.
15287c478bd9Sstevel@tonic-gate 			 */
15297c478bd9Sstevel@tonic-gate 			if (post_lstat.st_uid != getuid() &&
15307c478bd9Sstevel@tonic-gate 			    post_lstat.st_uid != 0) {
15314098eb52Sws195443 				errtext = gettext("Unsupported "
15324098eb52Sws195443 				    "condition detected: %s "
15334098eb52Sws195443 				    "must be owned by uid %ld or 0\n"),
15347c478bd9Sstevel@tonic-gate 				    msg(errtext, filename, (long)getuid());
15357c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename,
15367c478bd9Sstevel@tonic-gate 				    (long)getuid());
15377c478bd9Sstevel@tonic-gate 				(void) close(fd);
15387c478bd9Sstevel@tonic-gate 				errno = EPERM;
15397c478bd9Sstevel@tonic-gate 				return (-1);
15407c478bd9Sstevel@tonic-gate 			}
15417c478bd9Sstevel@tonic-gate 			if (mode & O_TRUNC) {
15427c478bd9Sstevel@tonic-gate 				if (ftruncate(fd, (off_t)0) < 0) {
15437c478bd9Sstevel@tonic-gate 					msg("ftruncate(%s): %s\n",
15447c478bd9Sstevel@tonic-gate 					    filename, strerror(errno));
15457c478bd9Sstevel@tonic-gate 					(void) close(fd);
15467c478bd9Sstevel@tonic-gate 					return (-1);
15477c478bd9Sstevel@tonic-gate 				}
15487c478bd9Sstevel@tonic-gate 			}
15497c478bd9Sstevel@tonic-gate 		} else {
15507c478bd9Sstevel@tonic-gate 			/*
15517c478bd9Sstevel@tonic-gate 			 * Didn't exist, but couldn't open it.
15527c478bd9Sstevel@tonic-gate 			 */
15537c478bd9Sstevel@tonic-gate 			return (-1);
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 	} else {
15567c478bd9Sstevel@tonic-gate 		/*
15577c478bd9Sstevel@tonic-gate 		 * If truncating open succeeded for a read-only open,
15587c478bd9Sstevel@tonic-gate 		 * bail out, as we really shouldn't have succeeded.
15597c478bd9Sstevel@tonic-gate 		 */
15607c478bd9Sstevel@tonic-gate 		if (mode & O_RDONLY) {
15617c478bd9Sstevel@tonic-gate 			/* Undo the O_CREAT */
15627c478bd9Sstevel@tonic-gate 			(void) unlink(filename);
15637c478bd9Sstevel@tonic-gate 			msg("open(%s): %s\n",
15647c478bd9Sstevel@tonic-gate 			    filename, strerror(ENOENT));
15657c478bd9Sstevel@tonic-gate 			(void) close(fd);
15667c478bd9Sstevel@tonic-gate 			errno = ENOENT;
15677c478bd9Sstevel@tonic-gate 			return (-1);
15687c478bd9Sstevel@tonic-gate 		}
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	return (fd);
15727c478bd9Sstevel@tonic-gate }
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate /*
15757c478bd9Sstevel@tonic-gate  * Safely open a file.
15767c478bd9Sstevel@tonic-gate  */
15777c478bd9Sstevel@tonic-gate int
15787c478bd9Sstevel@tonic-gate safe_file_open(const char *filename, int mode, int perms)
15797c478bd9Sstevel@tonic-gate {
15807c478bd9Sstevel@tonic-gate 	return (safe_open_common(filename, mode, perms, 0));
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate /*
15847c478bd9Sstevel@tonic-gate  * Safely open a device.
15857c478bd9Sstevel@tonic-gate  */
15867c478bd9Sstevel@tonic-gate int
15877c478bd9Sstevel@tonic-gate safe_device_open(const char *filename, int mode, int perms)
15887c478bd9Sstevel@tonic-gate {
15897c478bd9Sstevel@tonic-gate 	return (safe_open_common(filename, mode, perms, 1));
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate /*
15937c478bd9Sstevel@tonic-gate  * STDIO version of safe_open
15947c478bd9Sstevel@tonic-gate  */
15957c478bd9Sstevel@tonic-gate FILE *
15967c478bd9Sstevel@tonic-gate safe_fopen(const char *filename, const char *smode, int perms)
15977c478bd9Sstevel@tonic-gate {
15987c478bd9Sstevel@tonic-gate 	int fd;
15997c478bd9Sstevel@tonic-gate 	int bmode;
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	/*
16027c478bd9Sstevel@tonic-gate 	 * accepts only modes  "r", "r+", and "w"
16037c478bd9Sstevel@tonic-gate 	 */
16047c478bd9Sstevel@tonic-gate 	if (smode[0] == 'r') {
16057c478bd9Sstevel@tonic-gate 		if (smode[1] == '\0') {
16067c478bd9Sstevel@tonic-gate 			bmode = O_RDONLY;
16077c478bd9Sstevel@tonic-gate 		} else if ((smode[1] == '+') && (smode[2] == '\0')) {
16087c478bd9Sstevel@tonic-gate 			bmode = O_RDWR;
16097c478bd9Sstevel@tonic-gate 		}
16107c478bd9Sstevel@tonic-gate 	} else if ((smode[0] == 'w') && (smode[1] == '\0')) {
16117c478bd9Sstevel@tonic-gate 		bmode = O_WRONLY;
16127c478bd9Sstevel@tonic-gate 	} else {
16137c478bd9Sstevel@tonic-gate 		msg(gettext("internal error: safe_fopen: invalid mode `%s'\n"),
16147c478bd9Sstevel@tonic-gate 		    smode);
16157c478bd9Sstevel@tonic-gate 		return (NULL);
16167c478bd9Sstevel@tonic-gate 	}
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	fd = safe_file_open(filename, bmode, perms);
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
16217c478bd9Sstevel@tonic-gate 	 * caller is expected to report error.
16227c478bd9Sstevel@tonic-gate 	 */
16237c478bd9Sstevel@tonic-gate 	if (fd >= 0)
16247c478bd9Sstevel@tonic-gate 		return (fdopen(fd, smode));
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	return ((FILE *)NULL);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate void
16307c478bd9Sstevel@tonic-gate child_chdir(void)
16317c478bd9Sstevel@tonic-gate {
16327c478bd9Sstevel@tonic-gate 	char name[MAXPATHLEN];
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	if (debug_chdir != NULL) {
16357c478bd9Sstevel@tonic-gate 		snprintf(name, sizeof (name), "%s/%ld",
16367c478bd9Sstevel@tonic-gate 		    debug_chdir, (long)getpid());
16377c478bd9Sstevel@tonic-gate 		if (mkdir(name, 0755) < 0)
16387c478bd9Sstevel@tonic-gate 			msg("mkdir(%s): %s", name, strerror(errno));
16397c478bd9Sstevel@tonic-gate 		if (chdir(name) < 0)
16407c478bd9Sstevel@tonic-gate 			msg("chdir(%s): %s", name, strerror(errno));
16417c478bd9Sstevel@tonic-gate 	}
16427c478bd9Sstevel@tonic-gate }
1643