xref: /titanic_50/usr/src/cmd/backup/dump/dumpmain.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1996-1998, 2000-2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
8*7c478bd9Sstevel@tonic-gate 
9*7c478bd9Sstevel@tonic-gate /*
10*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
11*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*7c478bd9Sstevel@tonic-gate  */
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate #include "dump.h"
18*7c478bd9Sstevel@tonic-gate #include <rmt.h>
19*7c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
20*7c478bd9Sstevel@tonic-gate #include <limits.h>
21*7c478bd9Sstevel@tonic-gate #include <priv_utils.h>
22*7c478bd9Sstevel@tonic-gate #include "roll_log.h"
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate int	notify = 0;		/* notify operator flag */
25*7c478bd9Sstevel@tonic-gate int	blockswritten = 0;	/* number of blocks written on current tape */
26*7c478bd9Sstevel@tonic-gate uint_t	tapeno = 0;		/* current tape number */
27*7c478bd9Sstevel@tonic-gate daddr32_t filenum = 0;		/* current file number on tape */
28*7c478bd9Sstevel@tonic-gate int	density = 0;		/* density in bytes/0.1" */
29*7c478bd9Sstevel@tonic-gate int	tenthsperirg;		/* inter-record-gap in 0.1"'s */
30*7c478bd9Sstevel@tonic-gate uint_t	ntrec = 0;		/* # tape blocks in each tape record */
31*7c478bd9Sstevel@tonic-gate uint_t	saved_ntrec = 0;	/* saved value of ntrec */
32*7c478bd9Sstevel@tonic-gate uint_t	forceflag = 0;		/* forced to change tp_bsize */
33*7c478bd9Sstevel@tonic-gate int	cartridge = 0;		/* assume non-cartridge tape */
34*7c478bd9Sstevel@tonic-gate uint_t	tracks;			/* # tracks on a cartridge tape */
35*7c478bd9Sstevel@tonic-gate int	diskette = 0;		/* assume not dumping to a diskette */
36*7c478bd9Sstevel@tonic-gate int	printsize = 0;		/* just print estimated size and exit */
37*7c478bd9Sstevel@tonic-gate int	mapfd = -1;		/* if >= 0, file descriptor for mmap */
38*7c478bd9Sstevel@tonic-gate int32_t	tp_bsize = TP_BSIZE_MIN; /* tape block record size (frag size) */
39*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
40*7c478bd9Sstevel@tonic-gate int	xflag;			/* debugging switch */
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate char	*myname;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * This should be struct fs, but there are trailing bits on disk
47*7c478bd9Sstevel@tonic-gate  * that we also need to read in as part of it.  It's an array of
48*7c478bd9Sstevel@tonic-gate  * longs instead of char to force proper alignment.
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate static long sblock_buf[SBSIZE/sizeof (long)];
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
53*7c478bd9Sstevel@tonic-gate static char *mb(u_offset_t);
54*7c478bd9Sstevel@tonic-gate static void nextstate(int);
55*7c478bd9Sstevel@tonic-gate #else
56*7c478bd9Sstevel@tonic-gate static char *mb();
57*7c478bd9Sstevel@tonic-gate static void nextstate();
58*7c478bd9Sstevel@tonic-gate #endif
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate extern	jmp_buf checkpoint_buf;	/* context for return from checkpoint */
61*7c478bd9Sstevel@tonic-gate #define	FUDGE_FACTOR	0x2000000
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate main(argc, argv)
64*7c478bd9Sstevel@tonic-gate 	int	argc;
65*7c478bd9Sstevel@tonic-gate 	char	*argv[];
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	char		*arg;
68*7c478bd9Sstevel@tonic-gate 	int		bflag = 0, i, error = 0, saverr;
69*7c478bd9Sstevel@tonic-gate 	double		fetapes = 0.0;
70*7c478bd9Sstevel@tonic-gate 	struct	mnttab	*dt;
71*7c478bd9Sstevel@tonic-gate 	char		msgbuf[3000], *msgp;
72*7c478bd9Sstevel@tonic-gate 	char		kbsbuf[BUFSIZ];
73*7c478bd9Sstevel@tonic-gate 	u_offset_t	esize_shift = 0;
74*7c478bd9Sstevel@tonic-gate 	int32_t	new_mult = 0;
75*7c478bd9Sstevel@tonic-gate 	time32_t	snapdate;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	host = NULL;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (myname = strrchr(argv[0], '/'))
80*7c478bd9Sstevel@tonic-gate 		myname++;
81*7c478bd9Sstevel@tonic-gate 	else
82*7c478bd9Sstevel@tonic-gate 		myname = argv[0];
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (strcmp("hsmdump", myname) == 0) {
85*7c478bd9Sstevel@tonic-gate 		msg(gettext("hsmdump emulation is no longer supported.\n"));
86*7c478bd9Sstevel@tonic-gate 		Exit(X_ABORT);
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	tape = DEFTAPE;
90*7c478bd9Sstevel@tonic-gate 	autoload_period = 12;
91*7c478bd9Sstevel@tonic-gate 	autoload_tries = 12;	/* traditional default of ~2.5 minutes */
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
94*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
95*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
96*7c478bd9Sstevel@tonic-gate #endif  /* TEXT_DOMAIN */
97*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	/*
100*7c478bd9Sstevel@tonic-gate 	 * If someone strips the set-uid bit, dump will still work for local
101*7c478bd9Sstevel@tonic-gate 	 * tapes.  Fail when we try to access a remote tape.
102*7c478bd9Sstevel@tonic-gate 	 */
103*7c478bd9Sstevel@tonic-gate 	(void) __init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_HOSTNAME, spcl.c_host, sizeof (spcl.c_host)) < 0) {
106*7c478bd9Sstevel@tonic-gate 		saverr = errno;
107*7c478bd9Sstevel@tonic-gate 		msg(gettext("Could not get host name: %s\n"),
108*7c478bd9Sstevel@tonic-gate 		    strerror(saverr));
109*7c478bd9Sstevel@tonic-gate 		bzero(spcl.c_host, sizeof (spcl.c_host));
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	dumppid = getpid();
113*7c478bd9Sstevel@tonic-gate 	tsize = 0;	/* no default size, detect EOT dynamically */
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	disk = NULL;
116*7c478bd9Sstevel@tonic-gate 	dname = NULL;
117*7c478bd9Sstevel@tonic-gate 	disk_dynamic = 0;
118*7c478bd9Sstevel@tonic-gate 	increm = NINCREM;
119*7c478bd9Sstevel@tonic-gate 	incno = '9';
120*7c478bd9Sstevel@tonic-gate 	uflag = 0;
121*7c478bd9Sstevel@tonic-gate 	arg = "u";
122*7c478bd9Sstevel@tonic-gate 	tlabel = "none";
123*7c478bd9Sstevel@tonic-gate 	if (argc > 1) {
124*7c478bd9Sstevel@tonic-gate 		argv++;
125*7c478bd9Sstevel@tonic-gate 		argc--;
126*7c478bd9Sstevel@tonic-gate 		arg = *argv;
127*7c478bd9Sstevel@tonic-gate 		if (*arg == '-')
128*7c478bd9Sstevel@tonic-gate 			arg++;
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 	while (*arg)
131*7c478bd9Sstevel@tonic-gate 	switch (*arg++) {		/* BE CAUTIOUS OF FALLTHROUGHS */
132*7c478bd9Sstevel@tonic-gate 	case 'M':
133*7c478bd9Sstevel@tonic-gate 		/*
134*7c478bd9Sstevel@tonic-gate 		 * This undocumented option causes each process to
135*7c478bd9Sstevel@tonic-gate 		 * mkdir debug_chdir/getpid(), and chdir to it.  This is
136*7c478bd9Sstevel@tonic-gate 		 * to ease the collection of profiling information and
137*7c478bd9Sstevel@tonic-gate 		 * core dumps.
138*7c478bd9Sstevel@tonic-gate 		 */
139*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
140*7c478bd9Sstevel@tonic-gate 			argv++;
141*7c478bd9Sstevel@tonic-gate 			argc--;
142*7c478bd9Sstevel@tonic-gate 			debug_chdir = *argv;
143*7c478bd9Sstevel@tonic-gate 			msg(gettext(
144*7c478bd9Sstevel@tonic-gate 			    "Each process shall try to chdir to %s/<pid>\n"),
145*7c478bd9Sstevel@tonic-gate 			    debug_chdir);
146*7c478bd9Sstevel@tonic-gate 			child_chdir();
147*7c478bd9Sstevel@tonic-gate 		} else {
148*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing move-to-dir (M) name\n"));
149*7c478bd9Sstevel@tonic-gate 			dumpabort();
150*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
151*7c478bd9Sstevel@tonic-gate 		}
152*7c478bd9Sstevel@tonic-gate 		break;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	case 'w':
155*7c478bd9Sstevel@tonic-gate 		lastdump('w');		/* tell us only what has to be done */
156*7c478bd9Sstevel@tonic-gate 		exit(0);
157*7c478bd9Sstevel@tonic-gate 		break;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	case 'W':			/* what to do */
160*7c478bd9Sstevel@tonic-gate 		lastdump('W');		/* tell state of what has been done */
161*7c478bd9Sstevel@tonic-gate 		exit(0);		/* do nothing else */
162*7c478bd9Sstevel@tonic-gate 		break;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	case 'T':
165*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
166*7c478bd9Sstevel@tonic-gate 			int count;
167*7c478bd9Sstevel@tonic-gate 			int multiplier;
168*7c478bd9Sstevel@tonic-gate 			char units;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 			argv++;
171*7c478bd9Sstevel@tonic-gate 			argc--;
172*7c478bd9Sstevel@tonic-gate 			count = atoi(*argv);
173*7c478bd9Sstevel@tonic-gate 			if (count < 1) {
174*7c478bd9Sstevel@tonic-gate 				msg(gettext(
175*7c478bd9Sstevel@tonic-gate 				    "Unreasonable autoload timeout period\n"));
176*7c478bd9Sstevel@tonic-gate 				dumpabort();
177*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
178*7c478bd9Sstevel@tonic-gate 			}
179*7c478bd9Sstevel@tonic-gate 			units = *(*argv + strlen(*argv) - 1);
180*7c478bd9Sstevel@tonic-gate 			switch (units) {
181*7c478bd9Sstevel@tonic-gate 			case 's':
182*7c478bd9Sstevel@tonic-gate 				multiplier = 1;
183*7c478bd9Sstevel@tonic-gate 				break;
184*7c478bd9Sstevel@tonic-gate 			case 'h':
185*7c478bd9Sstevel@tonic-gate 				multiplier = 3600;
186*7c478bd9Sstevel@tonic-gate 				break;
187*7c478bd9Sstevel@tonic-gate 			case '0': case '1': case '2': case '3': case '4':
188*7c478bd9Sstevel@tonic-gate 			case '5': case '6': case '7': case '8': case '9':
189*7c478bd9Sstevel@tonic-gate 			case 'm':
190*7c478bd9Sstevel@tonic-gate 				multiplier = 60;
191*7c478bd9Sstevel@tonic-gate 				break;
192*7c478bd9Sstevel@tonic-gate 			default:
193*7c478bd9Sstevel@tonic-gate 				msg(gettext(
194*7c478bd9Sstevel@tonic-gate 				    "Unknown timeout units indicator `%c'\n"),
195*7c478bd9Sstevel@tonic-gate 				    units);
196*7c478bd9Sstevel@tonic-gate 				dumpabort();
197*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
198*7c478bd9Sstevel@tonic-gate 			}
199*7c478bd9Sstevel@tonic-gate 			autoload_tries = 1 +
200*7c478bd9Sstevel@tonic-gate 			    ((count * multiplier) / autoload_period);
201*7c478bd9Sstevel@tonic-gate 		} else {
202*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing autoload timeout period\n"));
203*7c478bd9Sstevel@tonic-gate 			dumpabort();
204*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 		break;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	case 'f':			/* output file */
209*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
210*7c478bd9Sstevel@tonic-gate 			argv++;
211*7c478bd9Sstevel@tonic-gate 			argc--;
212*7c478bd9Sstevel@tonic-gate 			tape = *argv;
213*7c478bd9Sstevel@tonic-gate 			if (*tape == '\0') {
214*7c478bd9Sstevel@tonic-gate 				msg(gettext("Bad output device name\n"));
215*7c478bd9Sstevel@tonic-gate 				dumpabort();
216*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
217*7c478bd9Sstevel@tonic-gate 			}
218*7c478bd9Sstevel@tonic-gate 		} else {
219*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing output device name\n"));
220*7c478bd9Sstevel@tonic-gate 			dumpabort();
221*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 		if (strcmp(tape, "-") == 0 && verify) {
224*7c478bd9Sstevel@tonic-gate 			msg(gettext(
225*7c478bd9Sstevel@tonic-gate 			"Cannot verify when dumping to standard out.\n"));
226*7c478bd9Sstevel@tonic-gate 			dumpabort();
227*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 		break;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	case 'd':			/* density, in bits per inch */
232*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
233*7c478bd9Sstevel@tonic-gate 			argv++;
234*7c478bd9Sstevel@tonic-gate 			argc--;
235*7c478bd9Sstevel@tonic-gate 			density = atoi(*argv) / 10;
236*7c478bd9Sstevel@tonic-gate 			if (density <= 0) {
237*7c478bd9Sstevel@tonic-gate 				msg(gettext(
238*7c478bd9Sstevel@tonic-gate 				    "Density must be a positive integer\n"));
239*7c478bd9Sstevel@tonic-gate 				dumpabort();
240*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
241*7c478bd9Sstevel@tonic-gate 			}
242*7c478bd9Sstevel@tonic-gate 		} else {
243*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing density\n"));
244*7c478bd9Sstevel@tonic-gate 			dumpabort();
245*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
246*7c478bd9Sstevel@tonic-gate 		}
247*7c478bd9Sstevel@tonic-gate 		break;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	case 's':			/* tape size, feet */
250*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
251*7c478bd9Sstevel@tonic-gate 			argv++;
252*7c478bd9Sstevel@tonic-gate 			argc--;
253*7c478bd9Sstevel@tonic-gate 			tsize = atol(*argv);
254*7c478bd9Sstevel@tonic-gate 			if ((*argv[0] == '-') || (tsize == 0)) {
255*7c478bd9Sstevel@tonic-gate 				msg(gettext(
256*7c478bd9Sstevel@tonic-gate 			    "Tape size must be a positive integer\n"));
257*7c478bd9Sstevel@tonic-gate 				dumpabort();
258*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
259*7c478bd9Sstevel@tonic-gate 			}
260*7c478bd9Sstevel@tonic-gate 		} else {
261*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing tape size\n"));
262*7c478bd9Sstevel@tonic-gate 			dumpabort();
263*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 		break;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	case 't':			/* tracks */
268*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
269*7c478bd9Sstevel@tonic-gate 			argv++;
270*7c478bd9Sstevel@tonic-gate 			argc--;
271*7c478bd9Sstevel@tonic-gate 			tracks = atoi(*argv);
272*7c478bd9Sstevel@tonic-gate 		} else {
273*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing track count\n"));
274*7c478bd9Sstevel@tonic-gate 			dumpabort();
275*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
276*7c478bd9Sstevel@tonic-gate 		}
277*7c478bd9Sstevel@tonic-gate 		break;
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	case 'b':			/* blocks per tape write */
280*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
281*7c478bd9Sstevel@tonic-gate 			argv++;
282*7c478bd9Sstevel@tonic-gate 			argc--;
283*7c478bd9Sstevel@tonic-gate 			bflag++;
284*7c478bd9Sstevel@tonic-gate 			/*
285*7c478bd9Sstevel@tonic-gate 			 * We save the ntrec in case we need to change
286*7c478bd9Sstevel@tonic-gate 			 * tp_bsize later, we will have to recalculate
287*7c478bd9Sstevel@tonic-gate 			 * it.
288*7c478bd9Sstevel@tonic-gate 			 */
289*7c478bd9Sstevel@tonic-gate 			saved_ntrec = ntrec = atoi(*argv);
290*7c478bd9Sstevel@tonic-gate 			if (ntrec == 0 || (ntrec&1) || ntrec > (MAXNTREC*2)) {
291*7c478bd9Sstevel@tonic-gate 				msg(gettext(
292*7c478bd9Sstevel@tonic-gate 		    "Block size must be a positive, even integer <= %d\n"),
293*7c478bd9Sstevel@tonic-gate 				    MAXNTREC*2);
294*7c478bd9Sstevel@tonic-gate 				dumpabort();
295*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
296*7c478bd9Sstevel@tonic-gate 			}
297*7c478bd9Sstevel@tonic-gate 			ntrec /= (tp_bsize/DEV_BSIZE);
298*7c478bd9Sstevel@tonic-gate 		} else {
299*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing blocking factor\n"));
300*7c478bd9Sstevel@tonic-gate 			dumpabort();
301*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
302*7c478bd9Sstevel@tonic-gate 		}
303*7c478bd9Sstevel@tonic-gate 		break;
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 	case 'c':			/* Tape is cart. not 9-track */
306*7c478bd9Sstevel@tonic-gate 	case 'C':			/* 'C' to be consistent with 'D' */
307*7c478bd9Sstevel@tonic-gate 		cartridge++;
308*7c478bd9Sstevel@tonic-gate 		break;
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	case '0':			/* dump level */
311*7c478bd9Sstevel@tonic-gate 	case '1':
312*7c478bd9Sstevel@tonic-gate 	case '2':
313*7c478bd9Sstevel@tonic-gate 	case '3':
314*7c478bd9Sstevel@tonic-gate 	case '4':
315*7c478bd9Sstevel@tonic-gate 	case '5':
316*7c478bd9Sstevel@tonic-gate 	case '6':
317*7c478bd9Sstevel@tonic-gate 	case '7':
318*7c478bd9Sstevel@tonic-gate 	case '8':
319*7c478bd9Sstevel@tonic-gate 	case '9':
320*7c478bd9Sstevel@tonic-gate 		incno = arg[-1];
321*7c478bd9Sstevel@tonic-gate 		break;
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	case 'u':			/* update /etc/dumpdates */
324*7c478bd9Sstevel@tonic-gate 		uflag++;
325*7c478bd9Sstevel@tonic-gate 		break;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	case 'n':			/* notify operators */
328*7c478bd9Sstevel@tonic-gate 		notify++;
329*7c478bd9Sstevel@tonic-gate 		break;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	case 'a':			/* create archive file */
332*7c478bd9Sstevel@tonic-gate 		archive = 1;
333*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
334*7c478bd9Sstevel@tonic-gate 			argv++;
335*7c478bd9Sstevel@tonic-gate 			argc--;
336*7c478bd9Sstevel@tonic-gate 			if (**argv == '\0') {
337*7c478bd9Sstevel@tonic-gate 				msg(gettext("Bad archive file name\n"));
338*7c478bd9Sstevel@tonic-gate 				dumpabort();
339*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
340*7c478bd9Sstevel@tonic-gate 			}
341*7c478bd9Sstevel@tonic-gate 			archivefile = strdup(*argv);
342*7c478bd9Sstevel@tonic-gate 			if (archivefile == NULL) {
343*7c478bd9Sstevel@tonic-gate 				saverr = errno;
344*7c478bd9Sstevel@tonic-gate 				msg(gettext("Cannot allocate memory: %s\n"),
345*7c478bd9Sstevel@tonic-gate 				    strerror(saverr));
346*7c478bd9Sstevel@tonic-gate 				dumpabort();
347*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
348*7c478bd9Sstevel@tonic-gate 			}
349*7c478bd9Sstevel@tonic-gate 		} else {
350*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing archive file name\n"));
351*7c478bd9Sstevel@tonic-gate 			dumpabort();
352*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
353*7c478bd9Sstevel@tonic-gate 		}
354*7c478bd9Sstevel@tonic-gate 		break;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	case 'v':
357*7c478bd9Sstevel@tonic-gate 		verify++;
358*7c478bd9Sstevel@tonic-gate 		doingverify++;
359*7c478bd9Sstevel@tonic-gate 		if (strcmp(tape, "-") == 0) {
360*7c478bd9Sstevel@tonic-gate 			msg(gettext(
361*7c478bd9Sstevel@tonic-gate 			"Cannot verify when dumping to standard out.\n"));
362*7c478bd9Sstevel@tonic-gate 			dumpabort();
363*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 		break;
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	case 'D':
368*7c478bd9Sstevel@tonic-gate 		diskette++;
369*7c478bd9Sstevel@tonic-gate 		break;
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	case 'N':
372*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
373*7c478bd9Sstevel@tonic-gate 			argv++;
374*7c478bd9Sstevel@tonic-gate 			argc--;
375*7c478bd9Sstevel@tonic-gate 			if (**argv == '\0') {
376*7c478bd9Sstevel@tonic-gate 				msg(gettext("Missing name for dumpdates "
377*7c478bd9Sstevel@tonic-gate 				    "entry.\n"));
378*7c478bd9Sstevel@tonic-gate 				dumpabort();
379*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
380*7c478bd9Sstevel@tonic-gate 			}
381*7c478bd9Sstevel@tonic-gate 			dname = *argv;
382*7c478bd9Sstevel@tonic-gate 			if (strlen(dname) > MAXNAMLEN + 2) {
383*7c478bd9Sstevel@tonic-gate 				msg(gettext("Dumpdates entry name too "
384*7c478bd9Sstevel@tonic-gate 				    "long.\n"));
385*7c478bd9Sstevel@tonic-gate 				dumpabort();
386*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
387*7c478bd9Sstevel@tonic-gate 			}
388*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < strlen(dname); i++) {
389*7c478bd9Sstevel@tonic-gate 				if (isspace(*(dname+i))) {
390*7c478bd9Sstevel@tonic-gate 					msg(gettext("Dumpdates entry name may "
391*7c478bd9Sstevel@tonic-gate 					    "not contain white space.\n"));
392*7c478bd9Sstevel@tonic-gate 					dumpabort();
393*7c478bd9Sstevel@tonic-gate 					/*NOTREACHED*/
394*7c478bd9Sstevel@tonic-gate 				}
395*7c478bd9Sstevel@tonic-gate 			}
396*7c478bd9Sstevel@tonic-gate 		} else {
397*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing name for dumpdates entry.\n"));
398*7c478bd9Sstevel@tonic-gate 			dumpabort();
399*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
400*7c478bd9Sstevel@tonic-gate 		}
401*7c478bd9Sstevel@tonic-gate 		break;
402*7c478bd9Sstevel@tonic-gate 	case 'L':
403*7c478bd9Sstevel@tonic-gate 		if (argc > 1) {
404*7c478bd9Sstevel@tonic-gate 			argv++;
405*7c478bd9Sstevel@tonic-gate 			argc--;
406*7c478bd9Sstevel@tonic-gate 			if (**argv == '\0') {
407*7c478bd9Sstevel@tonic-gate 				msg(gettext("Missing tape label name\n"));
408*7c478bd9Sstevel@tonic-gate 				dumpabort();
409*7c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
410*7c478bd9Sstevel@tonic-gate 			}
411*7c478bd9Sstevel@tonic-gate 			tlabel = *argv;
412*7c478bd9Sstevel@tonic-gate 			if (strlen(tlabel) > (sizeof (spcl.c_label) - 1)) {
413*7c478bd9Sstevel@tonic-gate 				tlabel[sizeof (spcl.c_label) - 1] = '\0';
414*7c478bd9Sstevel@tonic-gate 				msg(gettext(
415*7c478bd9Sstevel@tonic-gate 		    "Truncating label to maximum supported length: `%s'\n"),
416*7c478bd9Sstevel@tonic-gate 				    tlabel);
417*7c478bd9Sstevel@tonic-gate 			}
418*7c478bd9Sstevel@tonic-gate 		} else {
419*7c478bd9Sstevel@tonic-gate 			msg(gettext("Missing tape label name\n"));
420*7c478bd9Sstevel@tonic-gate 			dumpabort();
421*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
422*7c478bd9Sstevel@tonic-gate 		}
423*7c478bd9Sstevel@tonic-gate 		break;
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	case 'l':
426*7c478bd9Sstevel@tonic-gate 		autoload++;
427*7c478bd9Sstevel@tonic-gate 		break;
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	case 'o':
430*7c478bd9Sstevel@tonic-gate 		offline++;
431*7c478bd9Sstevel@tonic-gate 		break;
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	case 'S':
434*7c478bd9Sstevel@tonic-gate 		printsize++;
435*7c478bd9Sstevel@tonic-gate 		break;
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
438*7c478bd9Sstevel@tonic-gate 	case 'z':
439*7c478bd9Sstevel@tonic-gate 		xflag++;
440*7c478bd9Sstevel@tonic-gate 		break;
441*7c478bd9Sstevel@tonic-gate #endif
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 	default:
444*7c478bd9Sstevel@tonic-gate 		msg(gettext("Bad option `%c'\n"), arg[-1]);
445*7c478bd9Sstevel@tonic-gate 		dumpabort();
446*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
447*7c478bd9Sstevel@tonic-gate 	}
448*7c478bd9Sstevel@tonic-gate 	if (argc > 1) {
449*7c478bd9Sstevel@tonic-gate 		argv++;
450*7c478bd9Sstevel@tonic-gate 		argc--;
451*7c478bd9Sstevel@tonic-gate 		if (**argv == '\0') {
452*7c478bd9Sstevel@tonic-gate 			msg(gettext("Bad disk name\n"));
453*7c478bd9Sstevel@tonic-gate 			dumpabort();
454*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
455*7c478bd9Sstevel@tonic-gate 		}
456*7c478bd9Sstevel@tonic-gate 		disk = *argv;
457*7c478bd9Sstevel@tonic-gate 		disk_dynamic = 0;
458*7c478bd9Sstevel@tonic-gate 	}
459*7c478bd9Sstevel@tonic-gate 	if (disk == NULL) {
460*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
461*7c478bd9Sstevel@tonic-gate 	"Usage: %s [0123456789fustdWwnNDCcbavloS [argument]] filesystem\n"),
462*7c478bd9Sstevel@tonic-gate 		    myname);
463*7c478bd9Sstevel@tonic-gate 		Exit(X_ABORT);
464*7c478bd9Sstevel@tonic-gate 	}
465*7c478bd9Sstevel@tonic-gate 	if (!filenum)
466*7c478bd9Sstevel@tonic-gate 		filenum = 1;
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, interrupt) == SIG_IGN)
469*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 	if (strcmp(tape, "-") == 0) {
472*7c478bd9Sstevel@tonic-gate 		pipeout++;
473*7c478bd9Sstevel@tonic-gate 		tape = gettext("standard output");
474*7c478bd9Sstevel@tonic-gate 		dumpdev = sdumpdev = strdup(tape);
475*7c478bd9Sstevel@tonic-gate 		if (dumpdev == NULL) {
476*7c478bd9Sstevel@tonic-gate 			saverr = errno;
477*7c478bd9Sstevel@tonic-gate 			msg(gettext("Cannot allocate memory: %s\n"),
478*7c478bd9Sstevel@tonic-gate 			    strerror(saverr));
479*7c478bd9Sstevel@tonic-gate 			dumpabort();
480*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
481*7c478bd9Sstevel@tonic-gate 		}
482*7c478bd9Sstevel@tonic-gate 		/*CONSTANTCONDITION*/
483*7c478bd9Sstevel@tonic-gate 		assert(sizeof (spcl.c_label) > 5);
484*7c478bd9Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, "none");
485*7c478bd9Sstevel@tonic-gate 	} else if (*tape == '+') {
486*7c478bd9Sstevel@tonic-gate 		nextdevice();
487*7c478bd9Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, tlabel);
488*7c478bd9Sstevel@tonic-gate 	} else {
489*7c478bd9Sstevel@tonic-gate 		/* if not already set, set diskette to default */
490*7c478bd9Sstevel@tonic-gate 		if (diskette && strcmp(tape, DEFTAPE) == 0)
491*7c478bd9Sstevel@tonic-gate 			tape = DISKETTE;
492*7c478bd9Sstevel@tonic-gate 		nextdevice();
493*7c478bd9Sstevel@tonic-gate 		(void) strcpy(spcl.c_label, tlabel);
494*7c478bd9Sstevel@tonic-gate 	}
495*7c478bd9Sstevel@tonic-gate 	if (cartridge && diskette) {
496*7c478bd9Sstevel@tonic-gate 		error = 1;
497*7c478bd9Sstevel@tonic-gate 		msg(gettext("Cannot select both cartridge and diskette\n"));
498*7c478bd9Sstevel@tonic-gate 	}
499*7c478bd9Sstevel@tonic-gate 	if (density && diskette) {
500*7c478bd9Sstevel@tonic-gate 		error = 1;
501*7c478bd9Sstevel@tonic-gate 		msg(gettext("Cannot select density of diskette\n"));
502*7c478bd9Sstevel@tonic-gate 	}
503*7c478bd9Sstevel@tonic-gate 	if (tracks && diskette) {
504*7c478bd9Sstevel@tonic-gate 		error = 1;
505*7c478bd9Sstevel@tonic-gate 		msg(gettext("Cannot select number of tracks of diskette\n"));
506*7c478bd9Sstevel@tonic-gate 	}
507*7c478bd9Sstevel@tonic-gate 	if (error) {
508*7c478bd9Sstevel@tonic-gate 		dumpabort();
509*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
510*7c478bd9Sstevel@tonic-gate 	}
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 	/*
513*7c478bd9Sstevel@tonic-gate 	 * Determine how to default tape size and density
514*7c478bd9Sstevel@tonic-gate 	 *
515*7c478bd9Sstevel@tonic-gate 	 *		density				tape size
516*7c478bd9Sstevel@tonic-gate 	 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
517*7c478bd9Sstevel@tonic-gate 	 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
518*7c478bd9Sstevel@tonic-gate 	 *
519*7c478bd9Sstevel@tonic-gate 	 * Most Sun-2's came with 4 track (20MB) cartridge tape drives,
520*7c478bd9Sstevel@tonic-gate 	 * while most other machines (Sun-3's and non-Sun's) come with
521*7c478bd9Sstevel@tonic-gate 	 * 9 track (45MB) cartridge tape drives.  Some Sun-2's came with
522*7c478bd9Sstevel@tonic-gate 	 * 9 track drives, but there is no way for the software to detect
523*7c478bd9Sstevel@tonic-gate 	 * which drive type is installed.  Sigh...  We make the gross
524*7c478bd9Sstevel@tonic-gate 	 * assumption that #ifdef mc68010 will test for a Sun-2.
525*7c478bd9Sstevel@tonic-gate 	 *
526*7c478bd9Sstevel@tonic-gate 	 * cartridge	8000 bpi (100 bytes/.1")	425 * tracks ft.
527*7c478bd9Sstevel@tonic-gate 	 */
528*7c478bd9Sstevel@tonic-gate 	if (density == 0)
529*7c478bd9Sstevel@tonic-gate 		density = cartridge ? 100 : 625;
530*7c478bd9Sstevel@tonic-gate 	if (tracks == 0)
531*7c478bd9Sstevel@tonic-gate 		tracks = 9;
532*7c478bd9Sstevel@tonic-gate 	if (!bflag) {
533*7c478bd9Sstevel@tonic-gate 		if (cartridge)
534*7c478bd9Sstevel@tonic-gate 			ntrec = CARTRIDGETREC;
535*7c478bd9Sstevel@tonic-gate 		else if (diskette)
536*7c478bd9Sstevel@tonic-gate 			ntrec = NTREC;
537*7c478bd9Sstevel@tonic-gate 		else if (density >= 625)
538*7c478bd9Sstevel@tonic-gate 			ntrec = HIGHDENSITYTREC;
539*7c478bd9Sstevel@tonic-gate 		else
540*7c478bd9Sstevel@tonic-gate 			ntrec = NTREC;
541*7c478bd9Sstevel@tonic-gate 		/*
542*7c478bd9Sstevel@tonic-gate 		 * save ntrec in case we have to change tp_bsize later.
543*7c478bd9Sstevel@tonic-gate 		 */
544*7c478bd9Sstevel@tonic-gate 		saved_ntrec = (ntrec * (tp_bsize/DEV_BSIZE));
545*7c478bd9Sstevel@tonic-gate 	}
546*7c478bd9Sstevel@tonic-gate 	if (!diskette) {
547*7c478bd9Sstevel@tonic-gate 		tsize *= 12L*10L;
548*7c478bd9Sstevel@tonic-gate 		if (cartridge)
549*7c478bd9Sstevel@tonic-gate 			tsize *= tracks;
550*7c478bd9Sstevel@tonic-gate 	}
551*7c478bd9Sstevel@tonic-gate 	rmtinit(msg, Exit);
552*7c478bd9Sstevel@tonic-gate 	if (host) {
553*7c478bd9Sstevel@tonic-gate 		char	*cp = strchr(host, '@');
554*7c478bd9Sstevel@tonic-gate 		if (cp == (char *)0)
555*7c478bd9Sstevel@tonic-gate 			cp = host;
556*7c478bd9Sstevel@tonic-gate 		else
557*7c478bd9Sstevel@tonic-gate 			cp++;
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 		if (rmthost(host, ntrec) == 0) {
560*7c478bd9Sstevel@tonic-gate 			msg(gettext("Cannot connect to tape host `%s'\n"), cp);
561*7c478bd9Sstevel@tonic-gate 			dumpabort();
562*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
563*7c478bd9Sstevel@tonic-gate 		}
564*7c478bd9Sstevel@tonic-gate 	}
565*7c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, sigAbort) == SIG_IGN)
566*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_IGN);
567*7c478bd9Sstevel@tonic-gate 	if (signal(SIGTRAP, sigAbort) == SIG_IGN)
568*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGTRAP, SIG_IGN);
569*7c478bd9Sstevel@tonic-gate 	if (signal(SIGFPE, sigAbort) == SIG_IGN)
570*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGFPE, SIG_IGN);
571*7c478bd9Sstevel@tonic-gate 	if (signal(SIGBUS, sigAbort) == SIG_IGN)
572*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGBUS, SIG_IGN);
573*7c478bd9Sstevel@tonic-gate 	if (signal(SIGSEGV, sigAbort) == SIG_IGN)
574*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGSEGV, SIG_IGN);
575*7c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, sigAbort) == SIG_IGN)
576*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGTERM, SIG_IGN);
577*7c478bd9Sstevel@tonic-gate 	if (signal(SIGUSR1, sigAbort) == SIG_IGN)
578*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGUSR1, SIG_IGN);
579*7c478bd9Sstevel@tonic-gate 	if (signal(SIGPIPE, sigAbort) == SIG_IGN)
580*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, SIG_IGN);
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 	mnttabread();		/* /etc/fstab, /etc/mtab snarfed */
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	/*
585*7c478bd9Sstevel@tonic-gate 	 *	disk can be either the full special file name,
586*7c478bd9Sstevel@tonic-gate 	 *	the suffix of the special file name,
587*7c478bd9Sstevel@tonic-gate 	 *	the special name missing the leading '/',
588*7c478bd9Sstevel@tonic-gate 	 *	the file system name with or without the leading '/'.
589*7c478bd9Sstevel@tonic-gate 	 *	NB:  we attempt to avoid dumping the block device
590*7c478bd9Sstevel@tonic-gate 	 *	(using rawname) because specfs and the vm system
591*7c478bd9Sstevel@tonic-gate 	 *	are not necessarily in sync.
592*7c478bd9Sstevel@tonic-gate 	 */
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 	/*
595*7c478bd9Sstevel@tonic-gate 	 * Attempt to roll the log before doing the dump.  There's nothing
596*7c478bd9Sstevel@tonic-gate 	 * the user can do if we are unable to roll the log, so we'll silently
597*7c478bd9Sstevel@tonic-gate 	 * ignore failures.
598*7c478bd9Sstevel@tonic-gate 	 */
599*7c478bd9Sstevel@tonic-gate 	if ((rl_roll_log(disk) != RL_SUCCESS) && (disk[0] != '/')) {
600*7c478bd9Sstevel@tonic-gate 		/* Try it again with leading '/'. */
601*7c478bd9Sstevel@tonic-gate 		char	*slashed;
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 		slashed = (char *)malloc(strlen(disk) + 2);
604*7c478bd9Sstevel@tonic-gate 		if (slashed != (char *)NULL) {
605*7c478bd9Sstevel@tonic-gate 			(void) sprintf(slashed, "%c%s", '/', disk);
606*7c478bd9Sstevel@tonic-gate 			(void) rl_roll_log(slashed);
607*7c478bd9Sstevel@tonic-gate 			free(slashed);
608*7c478bd9Sstevel@tonic-gate 		}
609*7c478bd9Sstevel@tonic-gate 	}
610*7c478bd9Sstevel@tonic-gate 	dt = mnttabsearch(disk, 0);
611*7c478bd9Sstevel@tonic-gate 	if (dt != 0) {
612*7c478bd9Sstevel@tonic-gate 		filesystem = dt->mnt_mountp;
613*7c478bd9Sstevel@tonic-gate 		if (disk_dynamic) {
614*7c478bd9Sstevel@tonic-gate 			/* LINTED: disk is not NULL */
615*7c478bd9Sstevel@tonic-gate 			free(disk);
616*7c478bd9Sstevel@tonic-gate 		}
617*7c478bd9Sstevel@tonic-gate 		disk = rawname(dt->mnt_special);
618*7c478bd9Sstevel@tonic-gate 		disk_dynamic = (disk != dt->mnt_special);
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate 		(void) strncpy(spcl.c_dev, dt->mnt_special,
621*7c478bd9Sstevel@tonic-gate 		    sizeof (spcl.c_dev));
622*7c478bd9Sstevel@tonic-gate 		spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0';
623*7c478bd9Sstevel@tonic-gate 		(void) strncpy(spcl.c_filesys, dt->mnt_mountp,
624*7c478bd9Sstevel@tonic-gate 		    sizeof (spcl.c_filesys));
625*7c478bd9Sstevel@tonic-gate 		spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
626*7c478bd9Sstevel@tonic-gate 	} else {
627*7c478bd9Sstevel@tonic-gate 		(void) strncpy(spcl.c_dev, disk, sizeof (spcl.c_dev));
628*7c478bd9Sstevel@tonic-gate 		spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0';
629*7c478bd9Sstevel@tonic-gate #ifdef PARTIAL
630*7c478bd9Sstevel@tonic-gate 		/* check for partial filesystem dump */
631*7c478bd9Sstevel@tonic-gate 		partial_check();
632*7c478bd9Sstevel@tonic-gate 		dt = mnttabsearch(disk, 1);
633*7c478bd9Sstevel@tonic-gate 		if (dt != 0) {
634*7c478bd9Sstevel@tonic-gate 			filesystem = dt->mnt_mountp;
635*7c478bd9Sstevel@tonic-gate 			if (disk_dynamic)
636*7c478bd9Sstevel@tonic-gate 				free(disk);
637*7c478bd9Sstevel@tonic-gate 			disk = rawname(dt->mnt_special);
638*7c478bd9Sstevel@tonic-gate 			disk_dynamic = (disk != dt->mnt_special);
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 			(void) strncpy(spcl.c_filesys,
641*7c478bd9Sstevel@tonic-gate 			    "a partial file system", sizeof (spcl.c_filesys));
642*7c478bd9Sstevel@tonic-gate 			spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
643*7c478bd9Sstevel@tonic-gate 		}
644*7c478bd9Sstevel@tonic-gate 		else
645*7c478bd9Sstevel@tonic-gate #endif /* PARTIAL */
646*7c478bd9Sstevel@tonic-gate 		{
647*7c478bd9Sstevel@tonic-gate 			char *old_disk = disk;
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 			(void) strncpy(spcl.c_filesys,
650*7c478bd9Sstevel@tonic-gate 			    "an unlisted file system",
651*7c478bd9Sstevel@tonic-gate 			    sizeof (spcl.c_filesys));
652*7c478bd9Sstevel@tonic-gate 			spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 			disk = rawname(old_disk);
655*7c478bd9Sstevel@tonic-gate 			if (disk != old_disk) {
656*7c478bd9Sstevel@tonic-gate 				if (disk_dynamic)
657*7c478bd9Sstevel@tonic-gate 					free(old_disk);
658*7c478bd9Sstevel@tonic-gate 				disk_dynamic = 1;
659*7c478bd9Sstevel@tonic-gate 			}
660*7c478bd9Sstevel@tonic-gate 			/*
661*7c478bd9Sstevel@tonic-gate 			 * If disk == old_disk, then disk_dynamic's state
662*7c478bd9Sstevel@tonic-gate 			 * does not change.
663*7c478bd9Sstevel@tonic-gate 			 */
664*7c478bd9Sstevel@tonic-gate 		}
665*7c478bd9Sstevel@tonic-gate 	}
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 	fi = open64(disk, O_RDONLY);
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 	if (fi < 0) {
670*7c478bd9Sstevel@tonic-gate 		saverr = errno;
671*7c478bd9Sstevel@tonic-gate 		msg(gettext("Cannot open dump device `%s': %s\n"),
672*7c478bd9Sstevel@tonic-gate 			disk, strerror(saverr));
673*7c478bd9Sstevel@tonic-gate 		Exit(X_ABORT);
674*7c478bd9Sstevel@tonic-gate 	}
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 	if (sscanf(&incno, "%1d", &spcl.c_level) != 1) {
677*7c478bd9Sstevel@tonic-gate 		msg(gettext("Bad dump level `%c' specified\n"), incno);
678*7c478bd9Sstevel@tonic-gate 		dumpabort();
679*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
680*7c478bd9Sstevel@tonic-gate 	}
681*7c478bd9Sstevel@tonic-gate 	getitime();		/* /etc/dumpdates snarfed */
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 	sblock = (struct fs *)&sblock_buf;
684*7c478bd9Sstevel@tonic-gate 	sync();
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE);
687*7c478bd9Sstevel@tonic-gate 	if ((sblock->fs_magic != FS_MAGIC) &&
688*7c478bd9Sstevel@tonic-gate 	    (sblock->fs_magic != MTB_UFS_MAGIC)) {
689*7c478bd9Sstevel@tonic-gate 		msg(gettext(
690*7c478bd9Sstevel@tonic-gate 	    "Warning - super-block on device `%s' is corrupt - run fsck\n"),
691*7c478bd9Sstevel@tonic-gate 		    disk);
692*7c478bd9Sstevel@tonic-gate 		dumpabort();
693*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
694*7c478bd9Sstevel@tonic-gate 	}
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate 	if (sblock->fs_magic == MTB_UFS_MAGIC &&
697*7c478bd9Sstevel@tonic-gate 	    (sblock->fs_version < MTB_UFS_VERSION_MIN ||
698*7c478bd9Sstevel@tonic-gate 	    sblock->fs_version > MTB_UFS_VERSION_1)) {
699*7c478bd9Sstevel@tonic-gate 		msg(gettext("Unrecognized UFS version: %d\n"),
700*7c478bd9Sstevel@tonic-gate 		    sblock->fs_version);
701*7c478bd9Sstevel@tonic-gate 		dumpabort();
702*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
703*7c478bd9Sstevel@tonic-gate 	}
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 	/*
706*7c478bd9Sstevel@tonic-gate 	 * Try to set up for using mmap(2).  It only works on the block
707*7c478bd9Sstevel@tonic-gate 	 * device, but if we can use it, things go somewhat faster.  If
708*7c478bd9Sstevel@tonic-gate 	 * we can't open it, we'll silently fall back to the old method
709*7c478bd9Sstevel@tonic-gate 	 * (read/memcpy). We also only try this if it's been cleanly
710*7c478bd9Sstevel@tonic-gate 	 * unmounted. Dumping a live filesystem this way runs into
711*7c478bd9Sstevel@tonic-gate 	 * buffer consistency problems. Of course, we don't support
712*7c478bd9Sstevel@tonic-gate 	 * running dump on a mounted filesystem, but some people do it
713*7c478bd9Sstevel@tonic-gate 	 * anyway.
714*7c478bd9Sstevel@tonic-gate 	 */
715*7c478bd9Sstevel@tonic-gate 	if (sblock->fs_clean == FSCLEAN) {
716*7c478bd9Sstevel@tonic-gate 		char *block = unrawname(disk);
717*7c478bd9Sstevel@tonic-gate 
718*7c478bd9Sstevel@tonic-gate 		if (block != NULL) {
719*7c478bd9Sstevel@tonic-gate 			mapfd = open(block, O_RDONLY, 0);
720*7c478bd9Sstevel@tonic-gate 			free(block);
721*7c478bd9Sstevel@tonic-gate 		}
722*7c478bd9Sstevel@tonic-gate 	}
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate restart:
725*7c478bd9Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE);
726*7c478bd9Sstevel@tonic-gate 	if ((sblock->fs_magic != FS_MAGIC) &&
727*7c478bd9Sstevel@tonic-gate 	    (sblock->fs_magic != MTB_UFS_MAGIC)) {	/* paranoia */
728*7c478bd9Sstevel@tonic-gate 		msg(gettext("bad super-block magic number, run fsck\n"));
729*7c478bd9Sstevel@tonic-gate 		dumpabort();
730*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
731*7c478bd9Sstevel@tonic-gate 	}
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate 	if (sblock->fs_magic == MTB_UFS_MAGIC &&
734*7c478bd9Sstevel@tonic-gate 	    (sblock->fs_version < MTB_UFS_VERSION_MIN ||
735*7c478bd9Sstevel@tonic-gate 	    sblock->fs_version > MTB_UFS_VERSION_1)) {
736*7c478bd9Sstevel@tonic-gate 		msg(gettext("Unrecognized UFS version: %d\n"),
737*7c478bd9Sstevel@tonic-gate 		    sblock->fs_version);
738*7c478bd9Sstevel@tonic-gate 		dumpabort();
739*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
740*7c478bd9Sstevel@tonic-gate 	}
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate 	if (!doingactive)
743*7c478bd9Sstevel@tonic-gate 		allocino();
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	/* XXX should sanity-check the super block before trusting/using it */
746*7c478bd9Sstevel@tonic-gate 
747*7c478bd9Sstevel@tonic-gate 	/* LINTED XXX time truncated - tolerate until tape format changes */
748*7c478bd9Sstevel@tonic-gate 	spcl.c_date = (time32_t)time((time_t *)NULL);
749*7c478bd9Sstevel@tonic-gate 	bcopy(&(spcl.c_shadow), c_shadow_save, sizeof (c_shadow_save));
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 	snapdate = is_fssnap_dump(disk);
752*7c478bd9Sstevel@tonic-gate 	if (snapdate)
753*7c478bd9Sstevel@tonic-gate 		spcl.c_date = snapdate;
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate 	if (!printsize) {
756*7c478bd9Sstevel@tonic-gate 		msg(gettext("Date of this level %c dump: %s\n"),
757*7c478bd9Sstevel@tonic-gate 		    incno, prdate(spcl.c_date));
758*7c478bd9Sstevel@tonic-gate 		msg(gettext("Date of last level %c dump: %s\n"),
759*7c478bd9Sstevel@tonic-gate 			(uchar_t)lastincno, prdate(spcl.c_ddate));
760*7c478bd9Sstevel@tonic-gate 		msg(gettext("Dumping %s "), disk);
761*7c478bd9Sstevel@tonic-gate 		if (filesystem != 0)
762*7c478bd9Sstevel@tonic-gate 			msgtail("(%.*s:%s) ",
763*7c478bd9Sstevel@tonic-gate 			    /* LINTED unsigned -> signed cast ok */
764*7c478bd9Sstevel@tonic-gate 			    (int)sizeof (spcl.c_host), spcl.c_host, filesystem);
765*7c478bd9Sstevel@tonic-gate 		msgtail(gettext("to %s.\n"), sdumpdev);
766*7c478bd9Sstevel@tonic-gate 	}
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 	esize = f_esize = o_esize = 0;
769*7c478bd9Sstevel@tonic-gate 	msiz = roundup(d_howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
770*7c478bd9Sstevel@tonic-gate 		TP_BSIZE_MAX);
771*7c478bd9Sstevel@tonic-gate 	if (!doingactive) {
772*7c478bd9Sstevel@tonic-gate 		clrmap = (uchar_t *)xcalloc(msiz, sizeof (*clrmap));
773*7c478bd9Sstevel@tonic-gate 		filmap = (uchar_t *)xcalloc(msiz, sizeof (*filmap));
774*7c478bd9Sstevel@tonic-gate 		dirmap = (uchar_t *)xcalloc(msiz, sizeof (*dirmap));
775*7c478bd9Sstevel@tonic-gate 		nodmap = (uchar_t *)xcalloc(msiz, sizeof (*nodmap));
776*7c478bd9Sstevel@tonic-gate 		shamap = (uchar_t *)xcalloc(msiz, sizeof (*shamap));
777*7c478bd9Sstevel@tonic-gate 		activemap = (uchar_t *)xcalloc(msiz, sizeof (*activemap));
778*7c478bd9Sstevel@tonic-gate 	} else {
779*7c478bd9Sstevel@tonic-gate 		if (clrmap == NULL || filmap == NULL || dirmap == NULL ||
780*7c478bd9Sstevel@tonic-gate 		    nodmap == NULL || shamap == NULL || activemap == NULL) {
781*7c478bd9Sstevel@tonic-gate 			msg(gettext(
782*7c478bd9Sstevel@tonic-gate 	    "Internal error: NULL map pointer while re-dumping active files"));
783*7c478bd9Sstevel@tonic-gate 			dumpabort();
784*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
785*7c478bd9Sstevel@tonic-gate 		}
786*7c478bd9Sstevel@tonic-gate 		bzero(clrmap, msiz);
787*7c478bd9Sstevel@tonic-gate 		bzero(filmap, msiz);
788*7c478bd9Sstevel@tonic-gate 		bzero(dirmap, msiz);
789*7c478bd9Sstevel@tonic-gate 		bzero(nodmap, msiz);
790*7c478bd9Sstevel@tonic-gate 		bzero(shamap, msiz);
791*7c478bd9Sstevel@tonic-gate 		/* retain active map */
792*7c478bd9Sstevel@tonic-gate 	}
793*7c478bd9Sstevel@tonic-gate 
794*7c478bd9Sstevel@tonic-gate 	dumpstate = DS_INIT;
795*7c478bd9Sstevel@tonic-gate 	dumptoarchive = 1;
796*7c478bd9Sstevel@tonic-gate 
797*7c478bd9Sstevel@tonic-gate 	/*
798*7c478bd9Sstevel@tonic-gate 	 * Read cylinder group inode-used bitmaps to avoid reading clear inodes.
799*7c478bd9Sstevel@tonic-gate 	 */
800*7c478bd9Sstevel@tonic-gate 	{
801*7c478bd9Sstevel@tonic-gate 		uchar_t *clrp = clrmap;
802*7c478bd9Sstevel@tonic-gate 		struct cg *cgp =
803*7c478bd9Sstevel@tonic-gate 		    (struct cg *)xcalloc((uint_t)sblock->fs_cgsize, 1);
804*7c478bd9Sstevel@tonic-gate 
805*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < sblock->fs_ncg; i++) {
806*7c478bd9Sstevel@tonic-gate 			bread(fsbtodb(sblock, cgtod(sblock, i)),
807*7c478bd9Sstevel@tonic-gate 			    (uchar_t *)cgp, sblock->fs_cgsize);
808*7c478bd9Sstevel@tonic-gate 			bcopy(cg_inosused(cgp), clrp,
809*7c478bd9Sstevel@tonic-gate 			    (int)sblock->fs_ipg / NBBY);
810*7c478bd9Sstevel@tonic-gate 			clrp += sblock->fs_ipg / NBBY;
811*7c478bd9Sstevel@tonic-gate 		}
812*7c478bd9Sstevel@tonic-gate 		free((char *)cgp);
813*7c478bd9Sstevel@tonic-gate 		/* XXX right-shift clrmap one bit.  why? */
814*7c478bd9Sstevel@tonic-gate 		for (i = 0; clrp > clrmap; i <<= NBBY) {
815*7c478bd9Sstevel@tonic-gate 			i |= *--clrp & ((1<<NBBY) - 1);
816*7c478bd9Sstevel@tonic-gate 			*clrp = i >> 1;
817*7c478bd9Sstevel@tonic-gate 		}
818*7c478bd9Sstevel@tonic-gate 	}
819*7c478bd9Sstevel@tonic-gate 
820*7c478bd9Sstevel@tonic-gate 	if (!printsize) {
821*7c478bd9Sstevel@tonic-gate 		msgp = gettext("Mapping (Pass I) [regular files]\n");
822*7c478bd9Sstevel@tonic-gate 		msg(msgp);
823*7c478bd9Sstevel@tonic-gate 	}
824*7c478bd9Sstevel@tonic-gate 
825*7c478bd9Sstevel@tonic-gate 	ino = 0;
826*7c478bd9Sstevel@tonic-gate #ifdef PARTIAL
827*7c478bd9Sstevel@tonic-gate 	if (partial_mark(argc, argv)) {
828*7c478bd9Sstevel@tonic-gate #endif /* PARTIAL */
829*7c478bd9Sstevel@tonic-gate 		if (!doingactive)
830*7c478bd9Sstevel@tonic-gate 			pass(mark, clrmap);	/* mark updates 'x'_esize */
831*7c478bd9Sstevel@tonic-gate 		else
832*7c478bd9Sstevel@tonic-gate 			pass(active_mark, clrmap);	/* updates 'x'_esize */
833*7c478bd9Sstevel@tonic-gate #ifdef PARTIAL
834*7c478bd9Sstevel@tonic-gate 	}
835*7c478bd9Sstevel@tonic-gate #endif /* PARTIAL */
836*7c478bd9Sstevel@tonic-gate 	do {
837*7c478bd9Sstevel@tonic-gate 		if (!printsize) {
838*7c478bd9Sstevel@tonic-gate 			msgp = gettext("Mapping (Pass II) [directories]\n");
839*7c478bd9Sstevel@tonic-gate 			msg(msgp);
840*7c478bd9Sstevel@tonic-gate 		}
841*7c478bd9Sstevel@tonic-gate 		nadded = 0;
842*7c478bd9Sstevel@tonic-gate 		ino = 0;
843*7c478bd9Sstevel@tonic-gate 		pass(add, dirmap);
844*7c478bd9Sstevel@tonic-gate 	} while (nadded);
845*7c478bd9Sstevel@tonic-gate 
846*7c478bd9Sstevel@tonic-gate 	ino = 0; /* adjust estimated size for shadow inodes */
847*7c478bd9Sstevel@tonic-gate 	pass(markshad, nodmap);
848*7c478bd9Sstevel@tonic-gate 	ino = 0;
849*7c478bd9Sstevel@tonic-gate 	pass(estshad, shamap);
850*7c478bd9Sstevel@tonic-gate 	freeshad();
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate 	bmapest(clrmap);
853*7c478bd9Sstevel@tonic-gate 	bmapest(nodmap);
854*7c478bd9Sstevel@tonic-gate 	esize = o_esize + f_esize;
855*7c478bd9Sstevel@tonic-gate 	if (diskette) {
856*7c478bd9Sstevel@tonic-gate 		/* estimate number of floppies */
857*7c478bd9Sstevel@tonic-gate 		if (tsize != 0)
858*7c478bd9Sstevel@tonic-gate 			fetapes = (double)(esize + ntrec) / (double)tsize;
859*7c478bd9Sstevel@tonic-gate 	} else if (cartridge) {
860*7c478bd9Sstevel@tonic-gate 		/*
861*7c478bd9Sstevel@tonic-gate 		 * Estimate number of tapes, assuming streaming stops at
862*7c478bd9Sstevel@tonic-gate 		 * the end of each block written, and not in mid-block.
863*7c478bd9Sstevel@tonic-gate 		 * Assume no erroneous blocks; this can be compensated for
864*7c478bd9Sstevel@tonic-gate 		 * with an artificially low tape size.
865*7c478bd9Sstevel@tonic-gate 		 */
866*7c478bd9Sstevel@tonic-gate 		tenthsperirg = 16;	/* actually 15.48, says Archive */
867*7c478bd9Sstevel@tonic-gate 		if (tsize != 0)
868*7c478bd9Sstevel@tonic-gate 			fetapes = ((double)esize /* blocks */
869*7c478bd9Sstevel@tonic-gate 			    * (tp_bsize		/* bytes/block */
870*7c478bd9Sstevel@tonic-gate 			    * (1.0/density))	/* 0.1" / byte */
871*7c478bd9Sstevel@tonic-gate 			    +
872*7c478bd9Sstevel@tonic-gate 			    (double)esize	/* blocks */
873*7c478bd9Sstevel@tonic-gate 			    * (1.0/ntrec)	/* streaming-stops per block */
874*7c478bd9Sstevel@tonic-gate 			    * tenthsperirg)	/* 0.1" / streaming-stop */
875*7c478bd9Sstevel@tonic-gate 			    * (1.0 / tsize);	/* tape / 0.1" */
876*7c478bd9Sstevel@tonic-gate 	} else {
877*7c478bd9Sstevel@tonic-gate 		/* Estimate number of tapes, for old fashioned 9-track tape */
878*7c478bd9Sstevel@tonic-gate #ifdef sun
879*7c478bd9Sstevel@tonic-gate 		/* sun has long irg's */
880*7c478bd9Sstevel@tonic-gate 		tenthsperirg = (density == 625) ? 6 : 12;
881*7c478bd9Sstevel@tonic-gate #else
882*7c478bd9Sstevel@tonic-gate 		tenthsperirg = (density == 625) ? 5 : 8;
883*7c478bd9Sstevel@tonic-gate #endif
884*7c478bd9Sstevel@tonic-gate 		if (tsize != 0)
885*7c478bd9Sstevel@tonic-gate 			fetapes = ((double)esize /* blocks */
886*7c478bd9Sstevel@tonic-gate 			    * (tp_bsize		/* bytes / block */
887*7c478bd9Sstevel@tonic-gate 			    * (1.0/density))	/* 0.1" / byte */
888*7c478bd9Sstevel@tonic-gate 			    +
889*7c478bd9Sstevel@tonic-gate 			    (double)esize	/* blocks */
890*7c478bd9Sstevel@tonic-gate 			    * (1.0/ntrec)	/* IRG's / block */
891*7c478bd9Sstevel@tonic-gate 			    * tenthsperirg)	/* 0.1" / IRG */
892*7c478bd9Sstevel@tonic-gate 			    * (1.0 / tsize);	/* tape / 0.1" */
893*7c478bd9Sstevel@tonic-gate 	}
894*7c478bd9Sstevel@tonic-gate 
895*7c478bd9Sstevel@tonic-gate 	etapes = fetapes;	/* truncating assignment */
896*7c478bd9Sstevel@tonic-gate 	etapes++;
897*7c478bd9Sstevel@tonic-gate 	/* count the nodemap on each additional tape */
898*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < etapes; i++)
899*7c478bd9Sstevel@tonic-gate 		bmapest(nodmap);
900*7c478bd9Sstevel@tonic-gate 	/*
901*7c478bd9Sstevel@tonic-gate 	 * If the above bmapest is called, it changes o_esize and f_esize.
902*7c478bd9Sstevel@tonic-gate 	 * So we will recalculate esize here anyway to make sure.
903*7c478bd9Sstevel@tonic-gate 	 * Also, add tape headers and trailer records.
904*7c478bd9Sstevel@tonic-gate 	 */
905*7c478bd9Sstevel@tonic-gate 	esize = o_esize + f_esize + etapes + ntrec;
906*7c478bd9Sstevel@tonic-gate 
907*7c478bd9Sstevel@tonic-gate 	/*
908*7c478bd9Sstevel@tonic-gate 	 * If the estimated number of tp_bsize tape blocks is greater than
909*7c478bd9Sstevel@tonic-gate 	 * INT_MAX we have to adjust tp_bsize and ntrec to handle
910*7c478bd9Sstevel@tonic-gate 	 * the larger dump.  esize is an estimate, so we 'fudge'
911*7c478bd9Sstevel@tonic-gate 	 * INT_MAX a little.  If tp_bsize is adjusted, it will be adjusted
912*7c478bd9Sstevel@tonic-gate 	 * to the size needed for this dump (2048, 4096, 8192, ...)
913*7c478bd9Sstevel@tonic-gate 	 */
914*7c478bd9Sstevel@tonic-gate 	if (esize > (INT_MAX - FUDGE_FACTOR)) { /* esize is too big */
915*7c478bd9Sstevel@tonic-gate 		forceflag++;
916*7c478bd9Sstevel@tonic-gate 		esize_shift =
917*7c478bd9Sstevel@tonic-gate 		    ((esize + (INT_MAX - FUDGE_FACTOR) - 1)/
918*7c478bd9Sstevel@tonic-gate 		    ((u_offset_t)(INT_MAX - FUDGE_FACTOR))) - 1;
919*7c478bd9Sstevel@tonic-gate 		if ((esize_shift > ESIZE_SHIFT_MAX) || (ntrec == 0)) {
920*7c478bd9Sstevel@tonic-gate 			msgp = gettext(
921*7c478bd9Sstevel@tonic-gate 	"Block factor %d ('b' flag) is too small for this size dump.");
922*7c478bd9Sstevel@tonic-gate 			msg(msgp, saved_ntrec);
923*7c478bd9Sstevel@tonic-gate 			dumpabort();
924*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
925*7c478bd9Sstevel@tonic-gate 		}
926*7c478bd9Sstevel@tonic-gate 		/*
927*7c478bd9Sstevel@tonic-gate 		 * recalculate esize from:
928*7c478bd9Sstevel@tonic-gate 		 * o_esize - header tape records
929*7c478bd9Sstevel@tonic-gate 		 * (f_esize + (num_mult -1)) >> esize_shift - new non-header
930*7c478bd9Sstevel@tonic-gate 		 *	tape records for files/maps
931*7c478bd9Sstevel@tonic-gate 		 * etapes - TS_TAPE records
932*7c478bd9Sstevel@tonic-gate 		 * ntrec - TS_END records
933*7c478bd9Sstevel@tonic-gate 		 *
934*7c478bd9Sstevel@tonic-gate 		 * ntrec is adjusted so a tape record is still 'b' flag
935*7c478bd9Sstevel@tonic-gate 		 * number of DEV_BSIZE (512) in size
936*7c478bd9Sstevel@tonic-gate 		 */
937*7c478bd9Sstevel@tonic-gate 		new_mult = (tp_bsize << esize_shift)/tp_bsize;
938*7c478bd9Sstevel@tonic-gate 		tp_bsize = (tp_bsize << esize_shift);
939*7c478bd9Sstevel@tonic-gate 		esize = o_esize + ((f_esize +
940*7c478bd9Sstevel@tonic-gate 		    (new_mult - 1)) >> esize_shift) + etapes + ntrec;
941*7c478bd9Sstevel@tonic-gate 		ntrec = (saved_ntrec/(tp_bsize/DEV_BSIZE));
942*7c478bd9Sstevel@tonic-gate 	}
943*7c478bd9Sstevel@tonic-gate 	if (forceflag != 0) {
944*7c478bd9Sstevel@tonic-gate 		msgp = gettext(
945*7c478bd9Sstevel@tonic-gate 		    "Forcing larger tape block size (%d).\n");
946*7c478bd9Sstevel@tonic-gate 		msg(msgp, tp_bsize);
947*7c478bd9Sstevel@tonic-gate 	}
948*7c478bd9Sstevel@tonic-gate 	alloctape();			/* allocate tape buffers */
949*7c478bd9Sstevel@tonic-gate 
950*7c478bd9Sstevel@tonic-gate 	assert((tp_bsize / DEV_BSIZE != 0) && (tp_bsize % DEV_BSIZE == 0));
951*7c478bd9Sstevel@tonic-gate 	/*
952*7c478bd9Sstevel@tonic-gate 	 * If all we wanted was the size estimate,
953*7c478bd9Sstevel@tonic-gate 	 * just print it out and exit.
954*7c478bd9Sstevel@tonic-gate 	 */
955*7c478bd9Sstevel@tonic-gate 	if (printsize) {
956*7c478bd9Sstevel@tonic-gate 		(void) printf("%llu\n", esize * tp_bsize);
957*7c478bd9Sstevel@tonic-gate 		Exit(0);
958*7c478bd9Sstevel@tonic-gate 	}
959*7c478bd9Sstevel@tonic-gate 
960*7c478bd9Sstevel@tonic-gate 	if (tsize != 0) {
961*7c478bd9Sstevel@tonic-gate 		if (diskette)
962*7c478bd9Sstevel@tonic-gate 			msgp = gettext(
963*7c478bd9Sstevel@tonic-gate 			    "Estimated %lld blocks (%s) on %3.2f diskettes.\n");
964*7c478bd9Sstevel@tonic-gate 		else
965*7c478bd9Sstevel@tonic-gate 			msgp = gettext(
966*7c478bd9Sstevel@tonic-gate 			    "Estimated %lld blocks (%s) on %3.2f tapes.\n");
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 		msg(msgp,
969*7c478bd9Sstevel@tonic-gate 		    (esize*(tp_bsize/DEV_BSIZE)), mb(esize), fetapes);
970*7c478bd9Sstevel@tonic-gate 	} else {
971*7c478bd9Sstevel@tonic-gate 		msgp = gettext("Estimated %lld blocks (%s).\n");
972*7c478bd9Sstevel@tonic-gate 		msg(msgp, (esize*(tp_bsize/DEV_BSIZE)), mb(esize));
973*7c478bd9Sstevel@tonic-gate 	}
974*7c478bd9Sstevel@tonic-gate 
975*7c478bd9Sstevel@tonic-gate 	dumpstate = DS_CLRI;
976*7c478bd9Sstevel@tonic-gate 
977*7c478bd9Sstevel@tonic-gate 	otape(1);			/* bitmap is the first to tape write */
978*7c478bd9Sstevel@tonic-gate 	*telapsed = 0;
979*7c478bd9Sstevel@tonic-gate 	(void) time(tstart_writing);
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate 	/* filmap indicates all non-directory inodes */
982*7c478bd9Sstevel@tonic-gate 	{
983*7c478bd9Sstevel@tonic-gate 		uchar_t *np, *fp, *dp;
984*7c478bd9Sstevel@tonic-gate 		np = nodmap;
985*7c478bd9Sstevel@tonic-gate 		dp = dirmap;
986*7c478bd9Sstevel@tonic-gate 		fp = filmap;
987*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < msiz; i++)
988*7c478bd9Sstevel@tonic-gate 			*fp++ = *np++ ^ *dp++;
989*7c478bd9Sstevel@tonic-gate 	}
990*7c478bd9Sstevel@tonic-gate 
991*7c478bd9Sstevel@tonic-gate 	while (dumpstate != DS_DONE) {
992*7c478bd9Sstevel@tonic-gate 		/*
993*7c478bd9Sstevel@tonic-gate 		 * When we receive EOT notification from
994*7c478bd9Sstevel@tonic-gate 		 * the writer, the signal handler calls
995*7c478bd9Sstevel@tonic-gate 		 * rollforward and then jumps here.
996*7c478bd9Sstevel@tonic-gate 		 */
997*7c478bd9Sstevel@tonic-gate 		(void) setjmp(checkpoint_buf);
998*7c478bd9Sstevel@tonic-gate 		switch (dumpstate) {
999*7c478bd9Sstevel@tonic-gate 		case DS_INIT:
1000*7c478bd9Sstevel@tonic-gate 			/*
1001*7c478bd9Sstevel@tonic-gate 			 * We get here if a tape error occurred
1002*7c478bd9Sstevel@tonic-gate 			 * after releasing the name lock but before
1003*7c478bd9Sstevel@tonic-gate 			 * the volume containing the last of the
1004*7c478bd9Sstevel@tonic-gate 			 * dir info was completed.  We have to start
1005*7c478bd9Sstevel@tonic-gate 			 * all over in this case.
1006*7c478bd9Sstevel@tonic-gate 			 */
1007*7c478bd9Sstevel@tonic-gate 			{
1008*7c478bd9Sstevel@tonic-gate 				char *rmsg = gettext(
1009*7c478bd9Sstevel@tonic-gate 		"Warning - output error occurred after releasing name lock\n\
1010*7c478bd9Sstevel@tonic-gate \tThe dump will restart\n");
1011*7c478bd9Sstevel@tonic-gate 				msg(rmsg);
1012*7c478bd9Sstevel@tonic-gate 				goto restart;
1013*7c478bd9Sstevel@tonic-gate 			}
1014*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
1015*7c478bd9Sstevel@tonic-gate 		case DS_START:
1016*7c478bd9Sstevel@tonic-gate 		case DS_CLRI:
1017*7c478bd9Sstevel@tonic-gate 			ino = UFSROOTINO;
1018*7c478bd9Sstevel@tonic-gate 			dumptoarchive = 1;
1019*7c478bd9Sstevel@tonic-gate 			bitmap(clrmap, TS_CLRI);
1020*7c478bd9Sstevel@tonic-gate 			nextstate(DS_BITS);
1021*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
1022*7c478bd9Sstevel@tonic-gate 		case DS_BITS:
1023*7c478bd9Sstevel@tonic-gate 			ino = UFSROOTINO;
1024*7c478bd9Sstevel@tonic-gate 			dumptoarchive = 1;
1025*7c478bd9Sstevel@tonic-gate 			if (BIT(UFSROOTINO, nodmap))	/* empty dump check */
1026*7c478bd9Sstevel@tonic-gate 				bitmap(nodmap, TS_BITS);
1027*7c478bd9Sstevel@tonic-gate 			nextstate(DS_DIRS);
1028*7c478bd9Sstevel@tonic-gate 			if (!doingverify) {
1029*7c478bd9Sstevel@tonic-gate 				msgp = gettext(
1030*7c478bd9Sstevel@tonic-gate 					"Dumping (Pass III) [directories]\n");
1031*7c478bd9Sstevel@tonic-gate 				msg(msgp);
1032*7c478bd9Sstevel@tonic-gate 			}
1033*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
1034*7c478bd9Sstevel@tonic-gate 		case DS_DIRS:
1035*7c478bd9Sstevel@tonic-gate 			dumptoarchive = 1;
1036*7c478bd9Sstevel@tonic-gate 			pass(dirdump, dirmap);
1037*7c478bd9Sstevel@tonic-gate 			nextstate(DS_FILES);
1038*7c478bd9Sstevel@tonic-gate 			if (!doingverify) {
1039*7c478bd9Sstevel@tonic-gate 				msgp = gettext(
1040*7c478bd9Sstevel@tonic-gate 					"Dumping (Pass IV) [regular files]\n");
1041*7c478bd9Sstevel@tonic-gate 				msg(msgp);
1042*7c478bd9Sstevel@tonic-gate 			}
1043*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
1044*7c478bd9Sstevel@tonic-gate 		case DS_FILES:
1045*7c478bd9Sstevel@tonic-gate 			dumptoarchive = 0;
1046*7c478bd9Sstevel@tonic-gate 
1047*7c478bd9Sstevel@tonic-gate 			pass(lf_dump, filmap);
1048*7c478bd9Sstevel@tonic-gate 
1049*7c478bd9Sstevel@tonic-gate 			flushcmds();
1050*7c478bd9Sstevel@tonic-gate 			dumpstate = DS_END;	/* don't reset ino */
1051*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
1052*7c478bd9Sstevel@tonic-gate 		case DS_END:
1053*7c478bd9Sstevel@tonic-gate 			dumptoarchive = 1;
1054*7c478bd9Sstevel@tonic-gate 			spcl.c_type = TS_END;
1055*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < ntrec; i++) {
1056*7c478bd9Sstevel@tonic-gate 				spclrec();
1057*7c478bd9Sstevel@tonic-gate 			}
1058*7c478bd9Sstevel@tonic-gate 			flusht();
1059*7c478bd9Sstevel@tonic-gate 			break;
1060*7c478bd9Sstevel@tonic-gate 		case DS_DONE:
1061*7c478bd9Sstevel@tonic-gate 			break;
1062*7c478bd9Sstevel@tonic-gate 		default:
1063*7c478bd9Sstevel@tonic-gate 			msg(gettext("Internal state error\n"));
1064*7c478bd9Sstevel@tonic-gate 			dumpabort();
1065*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1066*7c478bd9Sstevel@tonic-gate 		}
1067*7c478bd9Sstevel@tonic-gate 	}
1068*7c478bd9Sstevel@tonic-gate 
1069*7c478bd9Sstevel@tonic-gate 	if ((! doingactive) && (! active))
1070*7c478bd9Sstevel@tonic-gate 		trewind();
1071*7c478bd9Sstevel@tonic-gate 	if (verify && !doingverify) {
1072*7c478bd9Sstevel@tonic-gate 		msgp = gettext("Finished writing last dump volume\n");
1073*7c478bd9Sstevel@tonic-gate 		msg(msgp);
1074*7c478bd9Sstevel@tonic-gate 		Exit(X_VERIFY);
1075*7c478bd9Sstevel@tonic-gate 	}
1076*7c478bd9Sstevel@tonic-gate 	if (spcl.c_volume > 1)
1077*7c478bd9Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
1078*7c478bd9Sstevel@tonic-gate 		    gettext("%lld blocks (%s) on %ld volumes"),
1079*7c478bd9Sstevel@tonic-gate 		    ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)),
1080*7c478bd9Sstevel@tonic-gate 		    mb((u_offset_t)(unsigned)(spcl.c_tapea)),
1081*7c478bd9Sstevel@tonic-gate 		    spcl.c_volume);
1082*7c478bd9Sstevel@tonic-gate 	else
1083*7c478bd9Sstevel@tonic-gate 		(void) snprintf(msgbuf, sizeof (msgbuf),
1084*7c478bd9Sstevel@tonic-gate 		    gettext("%lld blocks (%s) on 1 volume"),
1085*7c478bd9Sstevel@tonic-gate 		    ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)),
1086*7c478bd9Sstevel@tonic-gate 		    mb((u_offset_t)(unsigned)(spcl.c_tapea)));
1087*7c478bd9Sstevel@tonic-gate 	if (timeclock((time_t)0) != (time_t)0) {
1088*7c478bd9Sstevel@tonic-gate 		(void) snprintf(kbsbuf, sizeof (kbsbuf),
1089*7c478bd9Sstevel@tonic-gate 		    gettext(" at %ld KB/sec"),
1090*7c478bd9Sstevel@tonic-gate 		    (long)(((float)spcl.c_tapea / (float)timeclock((time_t)0))
1091*7c478bd9Sstevel@tonic-gate 			* 1000.0));
1092*7c478bd9Sstevel@tonic-gate 		(void) strcat(msgbuf, kbsbuf);
1093*7c478bd9Sstevel@tonic-gate 	}
1094*7c478bd9Sstevel@tonic-gate 	(void) strcat(msgbuf, "\n");
1095*7c478bd9Sstevel@tonic-gate 	msg(msgbuf);
1096*7c478bd9Sstevel@tonic-gate 	(void) timeclock((time_t)-1);
1097*7c478bd9Sstevel@tonic-gate 
1098*7c478bd9Sstevel@tonic-gate 	if (archive)
1099*7c478bd9Sstevel@tonic-gate 		msg(gettext("Archiving dump to `%s'\n"), archivefile);
1100*7c478bd9Sstevel@tonic-gate 	if (active && !verify) {
1101*7c478bd9Sstevel@tonic-gate 		nextstate(DS_INIT);
1102*7c478bd9Sstevel@tonic-gate 		activepass();
1103*7c478bd9Sstevel@tonic-gate 		goto restart;
1104*7c478bd9Sstevel@tonic-gate 	}
1105*7c478bd9Sstevel@tonic-gate 	msgp = gettext("DUMP IS DONE\n");
1106*7c478bd9Sstevel@tonic-gate 	msg(msgp);
1107*7c478bd9Sstevel@tonic-gate 	broadcast(msgp);
1108*7c478bd9Sstevel@tonic-gate 	if (! doingactive)
1109*7c478bd9Sstevel@tonic-gate 		putitime();
1110*7c478bd9Sstevel@tonic-gate 	Exit(X_FINOK);
1111*7c478bd9Sstevel@tonic-gate #ifdef lint
1112*7c478bd9Sstevel@tonic-gate 	return (0);
1113*7c478bd9Sstevel@tonic-gate #endif
1114*7c478bd9Sstevel@tonic-gate }
1115*7c478bd9Sstevel@tonic-gate 
1116*7c478bd9Sstevel@tonic-gate void
1117*7c478bd9Sstevel@tonic-gate sigAbort(sig)
1118*7c478bd9Sstevel@tonic-gate 	int	sig;
1119*7c478bd9Sstevel@tonic-gate {
1120*7c478bd9Sstevel@tonic-gate 	char	*sigtype;
1121*7c478bd9Sstevel@tonic-gate 
1122*7c478bd9Sstevel@tonic-gate 	switch (sig) {
1123*7c478bd9Sstevel@tonic-gate 	case SIGHUP:
1124*7c478bd9Sstevel@tonic-gate 		sigtype = "SIGHUP";
1125*7c478bd9Sstevel@tonic-gate 		break;
1126*7c478bd9Sstevel@tonic-gate 	case SIGTRAP:
1127*7c478bd9Sstevel@tonic-gate 		sigtype = "SIGTRAP";
1128*7c478bd9Sstevel@tonic-gate 		break;
1129*7c478bd9Sstevel@tonic-gate 	case SIGFPE:
1130*7c478bd9Sstevel@tonic-gate 		sigtype = "SIGFPE";
1131*7c478bd9Sstevel@tonic-gate 		break;
1132*7c478bd9Sstevel@tonic-gate 	case SIGBUS:
1133*7c478bd9Sstevel@tonic-gate 		msg(gettext("%s  ABORTING!\n"), "SIGBUS()");
1134*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGUSR2, SIG_DFL);
1135*7c478bd9Sstevel@tonic-gate 		abort();
1136*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1137*7c478bd9Sstevel@tonic-gate 	case SIGSEGV:
1138*7c478bd9Sstevel@tonic-gate 		msg(gettext("%s  ABORTING!\n"), "SIGSEGV()");
1139*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGUSR2, SIG_DFL);
1140*7c478bd9Sstevel@tonic-gate 		abort();
1141*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1142*7c478bd9Sstevel@tonic-gate 	case SIGALRM:
1143*7c478bd9Sstevel@tonic-gate 		sigtype = "SIGALRM";
1144*7c478bd9Sstevel@tonic-gate 		break;
1145*7c478bd9Sstevel@tonic-gate 	case SIGTERM:
1146*7c478bd9Sstevel@tonic-gate 		sigtype = "SIGTERM";
1147*7c478bd9Sstevel@tonic-gate 		break;
1148*7c478bd9Sstevel@tonic-gate 	case SIGPIPE:
1149*7c478bd9Sstevel@tonic-gate 		msg(gettext("Broken pipe\n"));
1150*7c478bd9Sstevel@tonic-gate 		dumpabort();
1151*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1152*7c478bd9Sstevel@tonic-gate 	default:
1153*7c478bd9Sstevel@tonic-gate 		sigtype = "SIGNAL";
1154*7c478bd9Sstevel@tonic-gate 		break;
1155*7c478bd9Sstevel@tonic-gate 	}
1156*7c478bd9Sstevel@tonic-gate 	msg(gettext("%s()  try rewriting\n"), sigtype);
1157*7c478bd9Sstevel@tonic-gate 	if (pipeout) {
1158*7c478bd9Sstevel@tonic-gate 		msg(gettext("Unknown signal, Cannot recover\n"));
1159*7c478bd9Sstevel@tonic-gate 		dumpabort();
1160*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1161*7c478bd9Sstevel@tonic-gate 	}
1162*7c478bd9Sstevel@tonic-gate 	msg(gettext("Rewriting attempted as response to unknown signal.\n"));
1163*7c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
1164*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
1165*7c478bd9Sstevel@tonic-gate 	close_rewind();
1166*7c478bd9Sstevel@tonic-gate 	Exit(X_REWRITE);
1167*7c478bd9Sstevel@tonic-gate }
1168*7c478bd9Sstevel@tonic-gate 
1169*7c478bd9Sstevel@tonic-gate /* Note that returned value is malloc'd if != cp && != NULL */
1170*7c478bd9Sstevel@tonic-gate char *
1171*7c478bd9Sstevel@tonic-gate rawname(cp)
1172*7c478bd9Sstevel@tonic-gate 	char *cp;
1173*7c478bd9Sstevel@tonic-gate {
1174*7c478bd9Sstevel@tonic-gate 	struct stat64 st;
1175*7c478bd9Sstevel@tonic-gate 	char *dp;
1176*7c478bd9Sstevel@tonic-gate 	extern char *getfullrawname();
1177*7c478bd9Sstevel@tonic-gate 
1178*7c478bd9Sstevel@tonic-gate 	if (stat64(cp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFBLK)
1179*7c478bd9Sstevel@tonic-gate 		return (cp);
1180*7c478bd9Sstevel@tonic-gate 
1181*7c478bd9Sstevel@tonic-gate 	dp = getfullrawname(cp);
1182*7c478bd9Sstevel@tonic-gate 	if (dp == 0)
1183*7c478bd9Sstevel@tonic-gate 		return (0);
1184*7c478bd9Sstevel@tonic-gate 	if (*dp == '\0') {
1185*7c478bd9Sstevel@tonic-gate 		free(dp);
1186*7c478bd9Sstevel@tonic-gate 		return (0);
1187*7c478bd9Sstevel@tonic-gate 	}
1188*7c478bd9Sstevel@tonic-gate 
1189*7c478bd9Sstevel@tonic-gate 	if (stat64(dp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFCHR) {
1190*7c478bd9Sstevel@tonic-gate 		free(dp);
1191*7c478bd9Sstevel@tonic-gate 		return (cp);
1192*7c478bd9Sstevel@tonic-gate 	}
1193*7c478bd9Sstevel@tonic-gate 
1194*7c478bd9Sstevel@tonic-gate 	return (dp);
1195*7c478bd9Sstevel@tonic-gate }
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate static char *
1198*7c478bd9Sstevel@tonic-gate mb(blks)
1199*7c478bd9Sstevel@tonic-gate 	u_offset_t blks;
1200*7c478bd9Sstevel@tonic-gate {
1201*7c478bd9Sstevel@tonic-gate 	static char buf[16];
1202*7c478bd9Sstevel@tonic-gate 
1203*7c478bd9Sstevel@tonic-gate 	if (blks < 1024)
1204*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%lldKB", blks);
1205*7c478bd9Sstevel@tonic-gate 	else
1206*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%.2fMB",
1207*7c478bd9Sstevel@tonic-gate 		    ((double)(blks*tp_bsize)) / (double)(1024*1024));
1208*7c478bd9Sstevel@tonic-gate 	return (buf);
1209*7c478bd9Sstevel@tonic-gate }
1210*7c478bd9Sstevel@tonic-gate 
1211*7c478bd9Sstevel@tonic-gate #ifdef signal
1212*7c478bd9Sstevel@tonic-gate void (*nsignal(sig, act))(int)
1213*7c478bd9Sstevel@tonic-gate 	int	sig;
1214*7c478bd9Sstevel@tonic-gate 	void	(*act)(int);
1215*7c478bd9Sstevel@tonic-gate {
1216*7c478bd9Sstevel@tonic-gate 	struct sigaction sa, osa;
1217*7c478bd9Sstevel@tonic-gate 
1218*7c478bd9Sstevel@tonic-gate 	sa.sa_handler = act;
1219*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sa.sa_mask);
1220*7c478bd9Sstevel@tonic-gate 	sa.sa_flags = SA_RESTART;
1221*7c478bd9Sstevel@tonic-gate 	if (sigaction(sig, &sa, &osa) < 0)
1222*7c478bd9Sstevel@tonic-gate 		return ((void (*)(int))-1);
1223*7c478bd9Sstevel@tonic-gate 	return (osa.sa_handler);
1224*7c478bd9Sstevel@tonic-gate }
1225*7c478bd9Sstevel@tonic-gate #endif
1226*7c478bd9Sstevel@tonic-gate 
1227*7c478bd9Sstevel@tonic-gate static void
1228*7c478bd9Sstevel@tonic-gate nextstate(state)
1229*7c478bd9Sstevel@tonic-gate 	int	state;
1230*7c478bd9Sstevel@tonic-gate {
1231*7c478bd9Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
1232*7c478bd9Sstevel@tonic-gate 	dumpstate = state;
1233*7c478bd9Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
1234*7c478bd9Sstevel@tonic-gate 	ino = 0;
1235*7c478bd9Sstevel@tonic-gate 	/* LINTED assigned value never used - kept for documentary purposes */
1236*7c478bd9Sstevel@tonic-gate 	pos = 0;
1237*7c478bd9Sstevel@tonic-gate 	leftover = 0;
1238*7c478bd9Sstevel@tonic-gate }
1239*7c478bd9Sstevel@tonic-gate 
1240*7c478bd9Sstevel@tonic-gate /*
1241*7c478bd9Sstevel@tonic-gate  * timeclock() function, for keeping track of how much time we've spent
1242*7c478bd9Sstevel@tonic-gate  * writing to the tape device.  it always returns the amount of time
1243*7c478bd9Sstevel@tonic-gate  * already spent, in milliseconds.  if you pass it a positive, then that's
1244*7c478bd9Sstevel@tonic-gate  * telling it that we're writing, so the time counts.  if you pass it a
1245*7c478bd9Sstevel@tonic-gate  * zero, then that's telling it we're not writing; perhaps we're waiting
1246*7c478bd9Sstevel@tonic-gate  * for user input.
1247*7c478bd9Sstevel@tonic-gate  *
1248*7c478bd9Sstevel@tonic-gate  * a state of -1 resets everything.
1249*7c478bd9Sstevel@tonic-gate  */
1250*7c478bd9Sstevel@tonic-gate time32_t
1251*7c478bd9Sstevel@tonic-gate timeclock(state)
1252*7c478bd9Sstevel@tonic-gate 	time32_t state;
1253*7c478bd9Sstevel@tonic-gate {
1254*7c478bd9Sstevel@tonic-gate 	static int *currentState = NULL;
1255*7c478bd9Sstevel@tonic-gate 	static struct timeval *clockstart;
1256*7c478bd9Sstevel@tonic-gate 	static time32_t *emilli;
1257*7c478bd9Sstevel@tonic-gate 
1258*7c478bd9Sstevel@tonic-gate 	struct timeval current[1];
1259*7c478bd9Sstevel@tonic-gate 	int fd, saverr;
1260*7c478bd9Sstevel@tonic-gate 
1261*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
1262*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "pid=%d timeclock ", getpid());
1263*7c478bd9Sstevel@tonic-gate 	if (state == (time32_t)-1)
1264*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "cleared\n");
1265*7c478bd9Sstevel@tonic-gate 	else if (state > 0)
1266*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "ticking\n");
1267*7c478bd9Sstevel@tonic-gate 	else
1268*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "paused\n");
1269*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1270*7c478bd9Sstevel@tonic-gate 
1271*7c478bd9Sstevel@tonic-gate 	/* if we haven't setup the shared memory, init */
1272*7c478bd9Sstevel@tonic-gate 	if (currentState == (int *)NULL) {
1273*7c478bd9Sstevel@tonic-gate 		if ((fd = open("/dev/zero", O_RDWR)) < 0) {
1274*7c478bd9Sstevel@tonic-gate 			saverr = errno;
1275*7c478bd9Sstevel@tonic-gate 			msg(gettext("Cannot open `%s': %s\n"),
1276*7c478bd9Sstevel@tonic-gate 				"/dev/zero", strerror(saverr));
1277*7c478bd9Sstevel@tonic-gate 			dumpabort();
1278*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1279*7c478bd9Sstevel@tonic-gate 		}
1280*7c478bd9Sstevel@tonic-gate 		/*LINTED [mmap always returns an aligned value]*/
1281*7c478bd9Sstevel@tonic-gate 		currentState = (int *)mmap((char *)0, getpagesize(),
1282*7c478bd9Sstevel@tonic-gate 			PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0);
1283*7c478bd9Sstevel@tonic-gate 		if (currentState == (int *)-1) {
1284*7c478bd9Sstevel@tonic-gate 			saverr = errno;
1285*7c478bd9Sstevel@tonic-gate 			msg(gettext(
1286*7c478bd9Sstevel@tonic-gate 				"Cannot memory map monitor variables: %s\n"),
1287*7c478bd9Sstevel@tonic-gate 				strerror(saverr));
1288*7c478bd9Sstevel@tonic-gate 			dumpabort();
1289*7c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1290*7c478bd9Sstevel@tonic-gate 		}
1291*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
1292*7c478bd9Sstevel@tonic-gate 
1293*7c478bd9Sstevel@tonic-gate 		/* LINTED currentState is sufficiently aligned */
1294*7c478bd9Sstevel@tonic-gate 		clockstart = (struct timeval *)(currentState + 1);
1295*7c478bd9Sstevel@tonic-gate 		emilli = (time32_t *)(clockstart + 1);
1296*7c478bd9Sstevel@tonic-gate 		/* Note everything is initialized to zero via /dev/zero */
1297*7c478bd9Sstevel@tonic-gate 	}
1298*7c478bd9Sstevel@tonic-gate 
1299*7c478bd9Sstevel@tonic-gate 	if (state == (time32_t)-1) {
1300*7c478bd9Sstevel@tonic-gate 		bzero(clockstart, sizeof (*clockstart));
1301*7c478bd9Sstevel@tonic-gate 		*currentState = 0;
1302*7c478bd9Sstevel@tonic-gate 		*emilli = (time32_t)0;
1303*7c478bd9Sstevel@tonic-gate 		return (0);
1304*7c478bd9Sstevel@tonic-gate 	}
1305*7c478bd9Sstevel@tonic-gate 
1306*7c478bd9Sstevel@tonic-gate 	(void) gettimeofday(current, NULL);
1307*7c478bd9Sstevel@tonic-gate 
1308*7c478bd9Sstevel@tonic-gate 	if (*currentState != 0) {
1309*7c478bd9Sstevel@tonic-gate 		current->tv_usec += 1000000;
1310*7c478bd9Sstevel@tonic-gate 		current->tv_sec--;
1311*7c478bd9Sstevel@tonic-gate 
1312*7c478bd9Sstevel@tonic-gate 		/* LINTED: result will fit in a time32_t */
1313*7c478bd9Sstevel@tonic-gate 		*emilli += (current->tv_sec - clockstart->tv_sec) * 1000;
1314*7c478bd9Sstevel@tonic-gate 		/* LINTED: result will fit in a time32_t */
1315*7c478bd9Sstevel@tonic-gate 		*emilli += (current->tv_usec - clockstart->tv_usec) / 1000;
1316*7c478bd9Sstevel@tonic-gate 	}
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate 	if (state != 0)
1319*7c478bd9Sstevel@tonic-gate 		bcopy(current, clockstart, sizeof (current));
1320*7c478bd9Sstevel@tonic-gate 
1321*7c478bd9Sstevel@tonic-gate 	*currentState = state;
1322*7c478bd9Sstevel@tonic-gate 
1323*7c478bd9Sstevel@tonic-gate 	return (*emilli);
1324*7c478bd9Sstevel@tonic-gate }
1325*7c478bd9Sstevel@tonic-gate 
1326*7c478bd9Sstevel@tonic-gate static int
1327*7c478bd9Sstevel@tonic-gate statcmp(const struct stat64 *left, const struct stat64 *right)
1328*7c478bd9Sstevel@tonic-gate {
1329*7c478bd9Sstevel@tonic-gate 	int result = 1;
1330*7c478bd9Sstevel@tonic-gate 
1331*7c478bd9Sstevel@tonic-gate 	if ((left->st_dev == right->st_dev) &&
1332*7c478bd9Sstevel@tonic-gate 	    (left->st_ino == right->st_ino) &&
1333*7c478bd9Sstevel@tonic-gate 	    (left->st_mode == right->st_mode) &&
1334*7c478bd9Sstevel@tonic-gate 	    (left->st_nlink == right->st_nlink) &&
1335*7c478bd9Sstevel@tonic-gate 	    (left->st_uid == right->st_uid) &&
1336*7c478bd9Sstevel@tonic-gate 	    (left->st_gid == right->st_gid) &&
1337*7c478bd9Sstevel@tonic-gate 	    (left->st_rdev == right->st_rdev) &&
1338*7c478bd9Sstevel@tonic-gate 	    (left->st_ctim.tv_sec == right->st_ctim.tv_sec) &&
1339*7c478bd9Sstevel@tonic-gate 	    (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) &&
1340*7c478bd9Sstevel@tonic-gate 	    (left->st_mtim.tv_sec == right->st_mtim.tv_sec) &&
1341*7c478bd9Sstevel@tonic-gate 	    (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec) &&
1342*7c478bd9Sstevel@tonic-gate 	    (left->st_blksize == right->st_blksize) &&
1343*7c478bd9Sstevel@tonic-gate 	    (left->st_blocks == right->st_blocks)) {
1344*7c478bd9Sstevel@tonic-gate 		result = 0;
1345*7c478bd9Sstevel@tonic-gate 	}
1346*7c478bd9Sstevel@tonic-gate 
1347*7c478bd9Sstevel@tonic-gate 	return (result);
1348*7c478bd9Sstevel@tonic-gate }
1349*7c478bd9Sstevel@tonic-gate 
1350*7c478bd9Sstevel@tonic-gate /*
1351*7c478bd9Sstevel@tonic-gate  * Safely open a file or device.
1352*7c478bd9Sstevel@tonic-gate  */
1353*7c478bd9Sstevel@tonic-gate static int
1354*7c478bd9Sstevel@tonic-gate safe_open_common(const char *filename, int mode, int perms, int device)
1355*7c478bd9Sstevel@tonic-gate {
1356*7c478bd9Sstevel@tonic-gate 	int fd;
1357*7c478bd9Sstevel@tonic-gate 	int working_mode;
1358*7c478bd9Sstevel@tonic-gate 	int saverr;
1359*7c478bd9Sstevel@tonic-gate 	char *errtext;
1360*7c478bd9Sstevel@tonic-gate 	struct stat64 pre_stat, pre_lstat;
1361*7c478bd9Sstevel@tonic-gate 	struct stat64 post_stat, post_lstat;
1362*7c478bd9Sstevel@tonic-gate 
1363*7c478bd9Sstevel@tonic-gate 	/*
1364*7c478bd9Sstevel@tonic-gate 	 * Don't want to be spoofed into trashing something we
1365*7c478bd9Sstevel@tonic-gate 	 * shouldn't, thus the following rigamarole.  If it doesn't
1366*7c478bd9Sstevel@tonic-gate 	 * exist, we create it and proceed.  Otherwise, require that
1367*7c478bd9Sstevel@tonic-gate 	 * what's there be a real file with no extraneous links and
1368*7c478bd9Sstevel@tonic-gate 	 * owned by whoever ran us.
1369*7c478bd9Sstevel@tonic-gate 	 *
1370*7c478bd9Sstevel@tonic-gate 	 * The silliness with using both lstat() and fstat() is to avoid
1371*7c478bd9Sstevel@tonic-gate 	 * race-condition games with someone replacing the file with a
1372*7c478bd9Sstevel@tonic-gate 	 * symlink after we've opened it.  If there was an flstat(),
1373*7c478bd9Sstevel@tonic-gate 	 * we wouldn't need the fstat().
1374*7c478bd9Sstevel@tonic-gate 	 *
1375*7c478bd9Sstevel@tonic-gate 	 * The initial open with the hard-coded flags is ok even if we
1376*7c478bd9Sstevel@tonic-gate 	 * are intending to open only for reading.  If it succeeds,
1377*7c478bd9Sstevel@tonic-gate 	 * then the file did not exist, and we'll synthesize an appropriate
1378*7c478bd9Sstevel@tonic-gate 	 * complaint below.  Otherwise, it does exist, so we won't be
1379*7c478bd9Sstevel@tonic-gate 	 * truncating it with the open.
1380*7c478bd9Sstevel@tonic-gate 	 */
1381*7c478bd9Sstevel@tonic-gate 	if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE,
1382*7c478bd9Sstevel@tonic-gate 	    perms)) < 0) {
1383*7c478bd9Sstevel@tonic-gate 		if (errno == EEXIST) {
1384*7c478bd9Sstevel@tonic-gate 			if (lstat64(filename, &pre_lstat) < 0) {
1385*7c478bd9Sstevel@tonic-gate 				return (-1);
1386*7c478bd9Sstevel@tonic-gate 			}
1387*7c478bd9Sstevel@tonic-gate 
1388*7c478bd9Sstevel@tonic-gate 			if (stat64(filename, &pre_stat) < 0) {
1389*7c478bd9Sstevel@tonic-gate 				return (-1);
1390*7c478bd9Sstevel@tonic-gate 			}
1391*7c478bd9Sstevel@tonic-gate 
1392*7c478bd9Sstevel@tonic-gate 			working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY);
1393*7c478bd9Sstevel@tonic-gate 			working_mode |= O_LARGEFILE;
1394*7c478bd9Sstevel@tonic-gate 			if ((fd = open(filename, working_mode)) < 0) {
1395*7c478bd9Sstevel@tonic-gate 				if (errno == ENOENT) {
1396*7c478bd9Sstevel@tonic-gate 					errtext = gettext(
1397*7c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s used to exist, but doesn't any longer\n");
1398*7c478bd9Sstevel@tonic-gate 					msg(errtext, filename);
1399*7c478bd9Sstevel@tonic-gate 					syslog(LOG_WARNING, errtext, filename);
1400*7c478bd9Sstevel@tonic-gate 					errno = ENOENT;
1401*7c478bd9Sstevel@tonic-gate 				}
1402*7c478bd9Sstevel@tonic-gate 				return (-1);
1403*7c478bd9Sstevel@tonic-gate 			}
1404*7c478bd9Sstevel@tonic-gate 
1405*7c478bd9Sstevel@tonic-gate 			if (lstat64(filename, &post_lstat) < 0) {
1406*7c478bd9Sstevel@tonic-gate 				saverr = errno;
1407*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
1408*7c478bd9Sstevel@tonic-gate 				errno = saverr;
1409*7c478bd9Sstevel@tonic-gate 				return (-1);
1410*7c478bd9Sstevel@tonic-gate 			}
1411*7c478bd9Sstevel@tonic-gate 
1412*7c478bd9Sstevel@tonic-gate 			if (fstat64(fd, &post_stat) < 0) {
1413*7c478bd9Sstevel@tonic-gate 				saverr = errno;
1414*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
1415*7c478bd9Sstevel@tonic-gate 				errno = saverr;
1416*7c478bd9Sstevel@tonic-gate 				return (-1);
1417*7c478bd9Sstevel@tonic-gate 			}
1418*7c478bd9Sstevel@tonic-gate 
1419*7c478bd9Sstevel@tonic-gate 			/*
1420*7c478bd9Sstevel@tonic-gate 			 * Can't just use memcmp(3C), because the access
1421*7c478bd9Sstevel@tonic-gate 			 * time is updated by open(2).
1422*7c478bd9Sstevel@tonic-gate 			 */
1423*7c478bd9Sstevel@tonic-gate 			if (statcmp(&pre_lstat, &post_lstat) != 0) {
1424*7c478bd9Sstevel@tonic-gate 				errtext = gettext(
1425*7c478bd9Sstevel@tonic-gate 	    "Unexpected change detected: %s's lstat(2) information changed\n");
1426*7c478bd9Sstevel@tonic-gate 				msg(errtext, filename);
1427*7c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
1428*7c478bd9Sstevel@tonic-gate 				errno = EPERM;
1429*7c478bd9Sstevel@tonic-gate 				return (-1);
1430*7c478bd9Sstevel@tonic-gate 			}
1431*7c478bd9Sstevel@tonic-gate 
1432*7c478bd9Sstevel@tonic-gate 			if (statcmp(&pre_stat, &post_stat) != 0) {
1433*7c478bd9Sstevel@tonic-gate 				errtext = gettext(
1434*7c478bd9Sstevel@tonic-gate 	    "Unexpected change detected: %s's stat(2) information changed\n"),
1435*7c478bd9Sstevel@tonic-gate 				msg(errtext, filename);
1436*7c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
1437*7c478bd9Sstevel@tonic-gate 				errno = EPERM;
1438*7c478bd9Sstevel@tonic-gate 				return (-1);
1439*7c478bd9Sstevel@tonic-gate 			}
1440*7c478bd9Sstevel@tonic-gate 
1441*7c478bd9Sstevel@tonic-gate 			/*
1442*7c478bd9Sstevel@tonic-gate 			 * If inode, device, or type are wrong, bail out.
1443*7c478bd9Sstevel@tonic-gate 			 * Note using post_stat instead of post_lstat for the
1444*7c478bd9Sstevel@tonic-gate 			 * S_ISCHR() test.  This is to allow the /dev ->
1445*7c478bd9Sstevel@tonic-gate 			 * /devices bit to work, as long as the final target
1446*7c478bd9Sstevel@tonic-gate 			 * is a character device (i.e., raw disk or tape).
1447*7c478bd9Sstevel@tonic-gate 			 */
1448*7c478bd9Sstevel@tonic-gate 			if (device && !(S_ISCHR(post_stat.st_mode)) &&
1449*7c478bd9Sstevel@tonic-gate 			    !(S_ISFIFO(post_stat.st_mode)) &&
1450*7c478bd9Sstevel@tonic-gate 			    !(S_ISREG(post_lstat.st_mode))) {
1451*7c478bd9Sstevel@tonic-gate 				errtext = gettext(
1452*7c478bd9Sstevel@tonic-gate 	    "Unexpected condition detected: %s is not a supported device\n"),
1453*7c478bd9Sstevel@tonic-gate 				msg(errtext, filename);
1454*7c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
1455*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
1456*7c478bd9Sstevel@tonic-gate 				errno = EPERM;
1457*7c478bd9Sstevel@tonic-gate 				return (-1);
1458*7c478bd9Sstevel@tonic-gate 			} else if (!device &&
1459*7c478bd9Sstevel@tonic-gate 			    (!S_ISREG(post_lstat.st_mode) ||
1460*7c478bd9Sstevel@tonic-gate 			    (post_stat.st_ino != post_lstat.st_ino) ||
1461*7c478bd9Sstevel@tonic-gate 			    (post_stat.st_dev != post_lstat.st_dev))) {
1462*7c478bd9Sstevel@tonic-gate 				errtext = gettext(
1463*7c478bd9Sstevel@tonic-gate 	    "Unexpected condition detected: %s is not a regular file\n"),
1464*7c478bd9Sstevel@tonic-gate 				msg(errtext, filename);
1465*7c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
1466*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
1467*7c478bd9Sstevel@tonic-gate 				errno = EPERM;
1468*7c478bd9Sstevel@tonic-gate 				return (-1);
1469*7c478bd9Sstevel@tonic-gate 			}
1470*7c478bd9Sstevel@tonic-gate 
1471*7c478bd9Sstevel@tonic-gate 			/*
1472*7c478bd9Sstevel@tonic-gate 			 * Bad link count implies someone's linked our
1473*7c478bd9Sstevel@tonic-gate 			 * target to something else, which we probably
1474*7c478bd9Sstevel@tonic-gate 			 * shouldn't step on.
1475*7c478bd9Sstevel@tonic-gate 			 */
1476*7c478bd9Sstevel@tonic-gate 			if (post_lstat.st_nlink != 1) {
1477*7c478bd9Sstevel@tonic-gate 				errtext = gettext(
1478*7c478bd9Sstevel@tonic-gate 	    "Unexpected condition detected: %s must have exactly one link\n"),
1479*7c478bd9Sstevel@tonic-gate 				msg(errtext, filename);
1480*7c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
1481*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
1482*7c478bd9Sstevel@tonic-gate 				errno = EPERM;
1483*7c478bd9Sstevel@tonic-gate 				return (-1);
1484*7c478bd9Sstevel@tonic-gate 			}
1485*7c478bd9Sstevel@tonic-gate 			/*
1486*7c478bd9Sstevel@tonic-gate 			 * Root might make a file, but non-root might
1487*7c478bd9Sstevel@tonic-gate 			 * need to open it.  If the permissions let us
1488*7c478bd9Sstevel@tonic-gate 			 * get this far, then let it through.
1489*7c478bd9Sstevel@tonic-gate 			 */
1490*7c478bd9Sstevel@tonic-gate 			if (post_lstat.st_uid != getuid() &&
1491*7c478bd9Sstevel@tonic-gate 			    post_lstat.st_uid != 0) {
1492*7c478bd9Sstevel@tonic-gate 				errtext = gettext(
1493*7c478bd9Sstevel@tonic-gate "Unsupported condition detected: %s must be owned by uid %ld or 0\n"),
1494*7c478bd9Sstevel@tonic-gate 				msg(errtext, filename, (long)getuid());
1495*7c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename,
1496*7c478bd9Sstevel@tonic-gate 				    (long)getuid());
1497*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
1498*7c478bd9Sstevel@tonic-gate 				errno = EPERM;
1499*7c478bd9Sstevel@tonic-gate 				return (-1);
1500*7c478bd9Sstevel@tonic-gate 			}
1501*7c478bd9Sstevel@tonic-gate 			if (mode & O_TRUNC) {
1502*7c478bd9Sstevel@tonic-gate 				if (ftruncate(fd, (off_t)0) < 0) {
1503*7c478bd9Sstevel@tonic-gate 					msg("ftruncate(%s): %s\n",
1504*7c478bd9Sstevel@tonic-gate 					    filename, strerror(errno));
1505*7c478bd9Sstevel@tonic-gate 					(void) close(fd);
1506*7c478bd9Sstevel@tonic-gate 					return (-1);
1507*7c478bd9Sstevel@tonic-gate 				}
1508*7c478bd9Sstevel@tonic-gate 			}
1509*7c478bd9Sstevel@tonic-gate 		} else {
1510*7c478bd9Sstevel@tonic-gate 			/*
1511*7c478bd9Sstevel@tonic-gate 			 * Didn't exist, but couldn't open it.
1512*7c478bd9Sstevel@tonic-gate 			 */
1513*7c478bd9Sstevel@tonic-gate 			return (-1);
1514*7c478bd9Sstevel@tonic-gate 		}
1515*7c478bd9Sstevel@tonic-gate 	} else {
1516*7c478bd9Sstevel@tonic-gate 		/*
1517*7c478bd9Sstevel@tonic-gate 		 * If truncating open succeeded for a read-only open,
1518*7c478bd9Sstevel@tonic-gate 		 * bail out, as we really shouldn't have succeeded.
1519*7c478bd9Sstevel@tonic-gate 		 */
1520*7c478bd9Sstevel@tonic-gate 		if (mode & O_RDONLY) {
1521*7c478bd9Sstevel@tonic-gate 			/* Undo the O_CREAT */
1522*7c478bd9Sstevel@tonic-gate 			(void) unlink(filename);
1523*7c478bd9Sstevel@tonic-gate 			msg("open(%s): %s\n",
1524*7c478bd9Sstevel@tonic-gate 			    filename, strerror(ENOENT));
1525*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
1526*7c478bd9Sstevel@tonic-gate 			errno = ENOENT;
1527*7c478bd9Sstevel@tonic-gate 			return (-1);
1528*7c478bd9Sstevel@tonic-gate 		}
1529*7c478bd9Sstevel@tonic-gate 	}
1530*7c478bd9Sstevel@tonic-gate 
1531*7c478bd9Sstevel@tonic-gate 	return (fd);
1532*7c478bd9Sstevel@tonic-gate }
1533*7c478bd9Sstevel@tonic-gate 
1534*7c478bd9Sstevel@tonic-gate /*
1535*7c478bd9Sstevel@tonic-gate  * Safely open a file.
1536*7c478bd9Sstevel@tonic-gate  */
1537*7c478bd9Sstevel@tonic-gate int
1538*7c478bd9Sstevel@tonic-gate safe_file_open(const char *filename, int mode, int perms)
1539*7c478bd9Sstevel@tonic-gate {
1540*7c478bd9Sstevel@tonic-gate 	return (safe_open_common(filename, mode, perms, 0));
1541*7c478bd9Sstevel@tonic-gate }
1542*7c478bd9Sstevel@tonic-gate 
1543*7c478bd9Sstevel@tonic-gate /*
1544*7c478bd9Sstevel@tonic-gate  * Safely open a device.
1545*7c478bd9Sstevel@tonic-gate  */
1546*7c478bd9Sstevel@tonic-gate int
1547*7c478bd9Sstevel@tonic-gate safe_device_open(const char *filename, int mode, int perms)
1548*7c478bd9Sstevel@tonic-gate {
1549*7c478bd9Sstevel@tonic-gate 	return (safe_open_common(filename, mode, perms, 1));
1550*7c478bd9Sstevel@tonic-gate }
1551*7c478bd9Sstevel@tonic-gate 
1552*7c478bd9Sstevel@tonic-gate /*
1553*7c478bd9Sstevel@tonic-gate  * STDIO version of safe_open
1554*7c478bd9Sstevel@tonic-gate  */
1555*7c478bd9Sstevel@tonic-gate FILE *
1556*7c478bd9Sstevel@tonic-gate safe_fopen(const char *filename, const char *smode, int perms)
1557*7c478bd9Sstevel@tonic-gate {
1558*7c478bd9Sstevel@tonic-gate 	int fd;
1559*7c478bd9Sstevel@tonic-gate 	int bmode;
1560*7c478bd9Sstevel@tonic-gate 
1561*7c478bd9Sstevel@tonic-gate 	/*
1562*7c478bd9Sstevel@tonic-gate 	 * accepts only modes  "r", "r+", and "w"
1563*7c478bd9Sstevel@tonic-gate 	 */
1564*7c478bd9Sstevel@tonic-gate 	if (smode[0] == 'r') {
1565*7c478bd9Sstevel@tonic-gate 		if (smode[1] == '\0') {
1566*7c478bd9Sstevel@tonic-gate 			bmode = O_RDONLY;
1567*7c478bd9Sstevel@tonic-gate 		} else if ((smode[1] == '+') && (smode[2] == '\0')) {
1568*7c478bd9Sstevel@tonic-gate 			bmode = O_RDWR;
1569*7c478bd9Sstevel@tonic-gate 		}
1570*7c478bd9Sstevel@tonic-gate 	} else if ((smode[0] == 'w') && (smode[1] == '\0')) {
1571*7c478bd9Sstevel@tonic-gate 		bmode = O_WRONLY;
1572*7c478bd9Sstevel@tonic-gate 	} else {
1573*7c478bd9Sstevel@tonic-gate 		msg(gettext("internal error: safe_fopen: invalid mode `%s'\n"),
1574*7c478bd9Sstevel@tonic-gate 		    smode);
1575*7c478bd9Sstevel@tonic-gate 		return (NULL);
1576*7c478bd9Sstevel@tonic-gate 	}
1577*7c478bd9Sstevel@tonic-gate 
1578*7c478bd9Sstevel@tonic-gate 	fd = safe_file_open(filename, bmode, perms);
1579*7c478bd9Sstevel@tonic-gate 
1580*7c478bd9Sstevel@tonic-gate 	/*
1581*7c478bd9Sstevel@tonic-gate 	 * caller is expected to report error.
1582*7c478bd9Sstevel@tonic-gate 	 */
1583*7c478bd9Sstevel@tonic-gate 	if (fd >= 0)
1584*7c478bd9Sstevel@tonic-gate 	    return (fdopen(fd, smode));
1585*7c478bd9Sstevel@tonic-gate 
1586*7c478bd9Sstevel@tonic-gate 	return ((FILE *)NULL);
1587*7c478bd9Sstevel@tonic-gate }
1588*7c478bd9Sstevel@tonic-gate 
1589*7c478bd9Sstevel@tonic-gate void
1590*7c478bd9Sstevel@tonic-gate child_chdir(void)
1591*7c478bd9Sstevel@tonic-gate {
1592*7c478bd9Sstevel@tonic-gate 	char name[MAXPATHLEN];
1593*7c478bd9Sstevel@tonic-gate 
1594*7c478bd9Sstevel@tonic-gate 	if (debug_chdir != NULL) {
1595*7c478bd9Sstevel@tonic-gate 		snprintf(name, sizeof (name), "%s/%ld",
1596*7c478bd9Sstevel@tonic-gate 		    debug_chdir, (long)getpid());
1597*7c478bd9Sstevel@tonic-gate 		if (mkdir(name, 0755) < 0)
1598*7c478bd9Sstevel@tonic-gate 			msg("mkdir(%s): %s", name, strerror(errno));
1599*7c478bd9Sstevel@tonic-gate 		if (chdir(name) < 0)
1600*7c478bd9Sstevel@tonic-gate 			msg("chdir(%s): %s", name, strerror(errno));
1601*7c478bd9Sstevel@tonic-gate 	}
1602*7c478bd9Sstevel@tonic-gate }
1603