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