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