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