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