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