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