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