10Sstevel@tonic-gate /* 2*5299Sws195443 * CDDL HEADER START 3*5299Sws195443 * 4*5299Sws195443 * The contents of this file are subject to the terms of the 5*5299Sws195443 * Common Development and Distribution License (the "License"). 6*5299Sws195443 * You may not use this file except in compliance with the License. 7*5299Sws195443 * 8*5299Sws195443 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*5299Sws195443 * or http://www.opensolaris.org/os/licensing. 10*5299Sws195443 * See the License for the specific language governing permissions 11*5299Sws195443 * and limitations under the License. 12*5299Sws195443 * 13*5299Sws195443 * When distributing Covered Code, include this CDDL HEADER in each 14*5299Sws195443 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*5299Sws195443 * If applicable, add the following below this CDDL HEADER, with the 16*5299Sws195443 * fields enclosed by brackets "[]" replaced with your own identifying 17*5299Sws195443 * information: Portions Copyright [yyyy] [name of copyright owner] 18*5299Sws195443 * 19*5299Sws195443 * CDDL HEADER END 20*5299Sws195443 */ 21*5299Sws195443 22*5299Sws195443 /* 23*5299Sws195443 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 320Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 330Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 340Sstevel@tonic-gate */ 350Sstevel@tonic-gate 360Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include "dump.h" 390Sstevel@tonic-gate #include <rmt.h> 400Sstevel@tonic-gate #include <sys/mtio.h> 410Sstevel@tonic-gate #include <limits.h> 420Sstevel@tonic-gate #include <priv_utils.h> 430Sstevel@tonic-gate #include "roll_log.h" 440Sstevel@tonic-gate 450Sstevel@tonic-gate int notify = 0; /* notify operator flag */ 460Sstevel@tonic-gate int blockswritten = 0; /* number of blocks written on current tape */ 470Sstevel@tonic-gate uint_t tapeno = 0; /* current tape number */ 480Sstevel@tonic-gate daddr32_t filenum = 0; /* current file number on tape */ 490Sstevel@tonic-gate int density = 0; /* density in bytes/0.1" */ 500Sstevel@tonic-gate int tenthsperirg; /* inter-record-gap in 0.1"'s */ 510Sstevel@tonic-gate uint_t ntrec = 0; /* # tape blocks in each tape record */ 520Sstevel@tonic-gate uint_t saved_ntrec = 0; /* saved value of ntrec */ 530Sstevel@tonic-gate uint_t forceflag = 0; /* forced to change tp_bsize */ 540Sstevel@tonic-gate int cartridge = 0; /* assume non-cartridge tape */ 550Sstevel@tonic-gate uint_t tracks; /* # tracks on a cartridge tape */ 560Sstevel@tonic-gate int diskette = 0; /* assume not dumping to a diskette */ 570Sstevel@tonic-gate int printsize = 0; /* just print estimated size and exit */ 580Sstevel@tonic-gate int mapfd = -1; /* if >= 0, file descriptor for mmap */ 590Sstevel@tonic-gate int32_t tp_bsize = TP_BSIZE_MIN; /* tape block record size (frag size) */ 600Sstevel@tonic-gate #ifdef DEBUG 610Sstevel@tonic-gate int xflag; /* debugging switch */ 620Sstevel@tonic-gate #endif 630Sstevel@tonic-gate 640Sstevel@tonic-gate char *myname; 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* 670Sstevel@tonic-gate * This should be struct fs, but there are trailing bits on disk 680Sstevel@tonic-gate * that we also need to read in as part of it. It's an array of 690Sstevel@tonic-gate * longs instead of char to force proper alignment. 700Sstevel@tonic-gate */ 710Sstevel@tonic-gate static long sblock_buf[SBSIZE/sizeof (long)]; 720Sstevel@tonic-gate 730Sstevel@tonic-gate #ifdef __STDC__ 740Sstevel@tonic-gate static char *mb(u_offset_t); 750Sstevel@tonic-gate static void nextstate(int); 760Sstevel@tonic-gate #else 770Sstevel@tonic-gate static char *mb(); 780Sstevel@tonic-gate static void nextstate(); 790Sstevel@tonic-gate #endif 800Sstevel@tonic-gate 810Sstevel@tonic-gate extern jmp_buf checkpoint_buf; /* context for return from checkpoint */ 820Sstevel@tonic-gate #define FUDGE_FACTOR 0x2000000 830Sstevel@tonic-gate 841053Smaheshvs int 851053Smaheshvs main(int argc, char *argv[]) 860Sstevel@tonic-gate { 870Sstevel@tonic-gate char *arg; 880Sstevel@tonic-gate int bflag = 0, i, error = 0, saverr; 890Sstevel@tonic-gate double fetapes = 0.0; 900Sstevel@tonic-gate struct mnttab *dt; 910Sstevel@tonic-gate char msgbuf[3000], *msgp; 920Sstevel@tonic-gate char kbsbuf[BUFSIZ]; 930Sstevel@tonic-gate u_offset_t esize_shift = 0; 940Sstevel@tonic-gate int32_t new_mult = 0; 950Sstevel@tonic-gate time32_t snapdate; 960Sstevel@tonic-gate 970Sstevel@tonic-gate host = NULL; 980Sstevel@tonic-gate 990Sstevel@tonic-gate if (myname = strrchr(argv[0], '/')) 1000Sstevel@tonic-gate myname++; 1010Sstevel@tonic-gate else 1020Sstevel@tonic-gate myname = argv[0]; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate if (strcmp("hsmdump", myname) == 0) { 1050Sstevel@tonic-gate msg(gettext("hsmdump emulation is no longer supported.\n")); 1060Sstevel@tonic-gate Exit(X_ABORT); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate tape = DEFTAPE; 1100Sstevel@tonic-gate autoload_period = 12; 1110Sstevel@tonic-gate autoload_tries = 12; /* traditional default of ~2.5 minutes */ 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1140Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 1150Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 1160Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 1170Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * If someone strips the set-uid bit, dump will still work for local 1210Sstevel@tonic-gate * tapes. Fail when we try to access a remote tape. 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate (void) __init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL); 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate if (sysinfo(SI_HOSTNAME, spcl.c_host, sizeof (spcl.c_host)) < 0) { 1260Sstevel@tonic-gate saverr = errno; 1270Sstevel@tonic-gate msg(gettext("Could not get host name: %s\n"), 1280Sstevel@tonic-gate strerror(saverr)); 1290Sstevel@tonic-gate bzero(spcl.c_host, sizeof (spcl.c_host)); 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate dumppid = getpid(); 1330Sstevel@tonic-gate tsize = 0; /* no default size, detect EOT dynamically */ 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate disk = NULL; 1360Sstevel@tonic-gate dname = NULL; 1370Sstevel@tonic-gate disk_dynamic = 0; 1380Sstevel@tonic-gate increm = NINCREM; 1390Sstevel@tonic-gate incno = '9'; 1400Sstevel@tonic-gate uflag = 0; 1410Sstevel@tonic-gate arg = "u"; 1420Sstevel@tonic-gate tlabel = "none"; 1430Sstevel@tonic-gate if (argc > 1) { 1440Sstevel@tonic-gate argv++; 1450Sstevel@tonic-gate argc--; 1460Sstevel@tonic-gate arg = *argv; 1470Sstevel@tonic-gate if (*arg == '-') 1480Sstevel@tonic-gate arg++; 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate while (*arg) 1510Sstevel@tonic-gate switch (*arg++) { /* BE CAUTIOUS OF FALLTHROUGHS */ 1520Sstevel@tonic-gate case 'M': 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * This undocumented option causes each process to 1550Sstevel@tonic-gate * mkdir debug_chdir/getpid(), and chdir to it. This is 1560Sstevel@tonic-gate * to ease the collection of profiling information and 1570Sstevel@tonic-gate * core dumps. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate if (argc > 1) { 1600Sstevel@tonic-gate argv++; 1610Sstevel@tonic-gate argc--; 1620Sstevel@tonic-gate debug_chdir = *argv; 1630Sstevel@tonic-gate msg(gettext( 1640Sstevel@tonic-gate "Each process shall try to chdir to %s/<pid>\n"), 1650Sstevel@tonic-gate debug_chdir); 1660Sstevel@tonic-gate child_chdir(); 1670Sstevel@tonic-gate } else { 1680Sstevel@tonic-gate msg(gettext("Missing move-to-dir (M) name\n")); 1690Sstevel@tonic-gate dumpabort(); 1700Sstevel@tonic-gate /*NOTREACHED*/ 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate break; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate case 'w': 1750Sstevel@tonic-gate lastdump('w'); /* tell us only what has to be done */ 1760Sstevel@tonic-gate exit(0); 1770Sstevel@tonic-gate break; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate case 'W': /* what to do */ 1800Sstevel@tonic-gate lastdump('W'); /* tell state of what has been done */ 1810Sstevel@tonic-gate exit(0); /* do nothing else */ 1820Sstevel@tonic-gate break; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate case 'T': 1850Sstevel@tonic-gate if (argc > 1) { 1860Sstevel@tonic-gate int count; 1870Sstevel@tonic-gate int multiplier; 1880Sstevel@tonic-gate char units; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate argv++; 1910Sstevel@tonic-gate argc--; 1920Sstevel@tonic-gate count = atoi(*argv); 1930Sstevel@tonic-gate if (count < 1) { 1940Sstevel@tonic-gate msg(gettext( 1950Sstevel@tonic-gate "Unreasonable autoload timeout period\n")); 1960Sstevel@tonic-gate dumpabort(); 1970Sstevel@tonic-gate /*NOTREACHED*/ 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate units = *(*argv + strlen(*argv) - 1); 2000Sstevel@tonic-gate switch (units) { 2010Sstevel@tonic-gate case 's': 2020Sstevel@tonic-gate multiplier = 1; 2030Sstevel@tonic-gate break; 2040Sstevel@tonic-gate case 'h': 2050Sstevel@tonic-gate multiplier = 3600; 2060Sstevel@tonic-gate break; 2070Sstevel@tonic-gate case '0': case '1': case '2': case '3': case '4': 2080Sstevel@tonic-gate case '5': case '6': case '7': case '8': case '9': 2090Sstevel@tonic-gate case 'm': 2100Sstevel@tonic-gate multiplier = 60; 2110Sstevel@tonic-gate break; 2120Sstevel@tonic-gate default: 2130Sstevel@tonic-gate msg(gettext( 2140Sstevel@tonic-gate "Unknown timeout units indicator `%c'\n"), 2150Sstevel@tonic-gate units); 2160Sstevel@tonic-gate dumpabort(); 2170Sstevel@tonic-gate /*NOTREACHED*/ 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate autoload_tries = 1 + 2200Sstevel@tonic-gate ((count * multiplier) / autoload_period); 2210Sstevel@tonic-gate } else { 2220Sstevel@tonic-gate msg(gettext("Missing autoload timeout period\n")); 2230Sstevel@tonic-gate dumpabort(); 2240Sstevel@tonic-gate /*NOTREACHED*/ 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate break; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate case 'f': /* output file */ 2290Sstevel@tonic-gate if (argc > 1) { 2300Sstevel@tonic-gate argv++; 2310Sstevel@tonic-gate argc--; 2320Sstevel@tonic-gate tape = *argv; 2330Sstevel@tonic-gate if (*tape == '\0') { 2340Sstevel@tonic-gate msg(gettext("Bad output device name\n")); 2350Sstevel@tonic-gate dumpabort(); 2360Sstevel@tonic-gate /*NOTREACHED*/ 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate } else { 2390Sstevel@tonic-gate msg(gettext("Missing output device name\n")); 2400Sstevel@tonic-gate dumpabort(); 2410Sstevel@tonic-gate /*NOTREACHED*/ 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate if (strcmp(tape, "-") == 0 && verify) { 2440Sstevel@tonic-gate msg(gettext( 2450Sstevel@tonic-gate "Cannot verify when dumping to standard out.\n")); 2460Sstevel@tonic-gate dumpabort(); 2470Sstevel@tonic-gate /*NOTREACHED*/ 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate break; 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate case 'd': /* density, in bits per inch */ 2520Sstevel@tonic-gate if (argc > 1) { 2530Sstevel@tonic-gate argv++; 2540Sstevel@tonic-gate argc--; 2550Sstevel@tonic-gate density = atoi(*argv) / 10; 2560Sstevel@tonic-gate if (density <= 0) { 2570Sstevel@tonic-gate msg(gettext( 2580Sstevel@tonic-gate "Density must be a positive integer\n")); 2590Sstevel@tonic-gate dumpabort(); 2600Sstevel@tonic-gate /*NOTREACHED*/ 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate } else { 2630Sstevel@tonic-gate msg(gettext("Missing density\n")); 2640Sstevel@tonic-gate dumpabort(); 2650Sstevel@tonic-gate /*NOTREACHED*/ 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate break; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate case 's': /* tape size, feet */ 2700Sstevel@tonic-gate if (argc > 1) { 2710Sstevel@tonic-gate argv++; 2720Sstevel@tonic-gate argc--; 2730Sstevel@tonic-gate tsize = atol(*argv); 2740Sstevel@tonic-gate if ((*argv[0] == '-') || (tsize == 0)) { 2750Sstevel@tonic-gate msg(gettext( 2760Sstevel@tonic-gate "Tape size must be a positive integer\n")); 2770Sstevel@tonic-gate dumpabort(); 2780Sstevel@tonic-gate /*NOTREACHED*/ 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate } else { 2810Sstevel@tonic-gate msg(gettext("Missing tape size\n")); 2820Sstevel@tonic-gate dumpabort(); 2830Sstevel@tonic-gate /*NOTREACHED*/ 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate break; 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate case 't': /* tracks */ 2880Sstevel@tonic-gate if (argc > 1) { 2890Sstevel@tonic-gate argv++; 2900Sstevel@tonic-gate argc--; 2910Sstevel@tonic-gate tracks = atoi(*argv); 2920Sstevel@tonic-gate } else { 2930Sstevel@tonic-gate msg(gettext("Missing track count\n")); 2940Sstevel@tonic-gate dumpabort(); 2950Sstevel@tonic-gate /*NOTREACHED*/ 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate break; 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate case 'b': /* blocks per tape write */ 3000Sstevel@tonic-gate if (argc > 1) { 3010Sstevel@tonic-gate argv++; 3020Sstevel@tonic-gate argc--; 3030Sstevel@tonic-gate bflag++; 3040Sstevel@tonic-gate /* 3050Sstevel@tonic-gate * We save the ntrec in case we need to change 3060Sstevel@tonic-gate * tp_bsize later, we will have to recalculate 3070Sstevel@tonic-gate * it. 3080Sstevel@tonic-gate */ 3090Sstevel@tonic-gate saved_ntrec = ntrec = atoi(*argv); 3100Sstevel@tonic-gate if (ntrec == 0 || (ntrec&1) || ntrec > (MAXNTREC*2)) { 3110Sstevel@tonic-gate msg(gettext( 3120Sstevel@tonic-gate "Block size must be a positive, even integer <= %d\n"), 3130Sstevel@tonic-gate MAXNTREC*2); 3140Sstevel@tonic-gate dumpabort(); 3150Sstevel@tonic-gate /*NOTREACHED*/ 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate ntrec /= (tp_bsize/DEV_BSIZE); 3180Sstevel@tonic-gate } else { 3190Sstevel@tonic-gate msg(gettext("Missing blocking factor\n")); 3200Sstevel@tonic-gate dumpabort(); 3210Sstevel@tonic-gate /*NOTREACHED*/ 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate break; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate case 'c': /* Tape is cart. not 9-track */ 3260Sstevel@tonic-gate case 'C': /* 'C' to be consistent with 'D' */ 3270Sstevel@tonic-gate cartridge++; 3280Sstevel@tonic-gate break; 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate case '0': /* dump level */ 3310Sstevel@tonic-gate case '1': 3320Sstevel@tonic-gate case '2': 3330Sstevel@tonic-gate case '3': 3340Sstevel@tonic-gate case '4': 3350Sstevel@tonic-gate case '5': 3360Sstevel@tonic-gate case '6': 3370Sstevel@tonic-gate case '7': 3380Sstevel@tonic-gate case '8': 3390Sstevel@tonic-gate case '9': 3400Sstevel@tonic-gate incno = arg[-1]; 3410Sstevel@tonic-gate break; 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate case 'u': /* update /etc/dumpdates */ 3440Sstevel@tonic-gate uflag++; 3450Sstevel@tonic-gate break; 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate case 'n': /* notify operators */ 3480Sstevel@tonic-gate notify++; 3490Sstevel@tonic-gate break; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate case 'a': /* create archive file */ 3520Sstevel@tonic-gate archive = 1; 3530Sstevel@tonic-gate if (argc > 1) { 3540Sstevel@tonic-gate argv++; 3550Sstevel@tonic-gate argc--; 3560Sstevel@tonic-gate if (**argv == '\0') { 3570Sstevel@tonic-gate msg(gettext("Bad archive file name\n")); 3580Sstevel@tonic-gate dumpabort(); 3590Sstevel@tonic-gate /*NOTREACHED*/ 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate archivefile = strdup(*argv); 3620Sstevel@tonic-gate if (archivefile == NULL) { 3630Sstevel@tonic-gate saverr = errno; 3640Sstevel@tonic-gate msg(gettext("Cannot allocate memory: %s\n"), 3650Sstevel@tonic-gate strerror(saverr)); 3660Sstevel@tonic-gate dumpabort(); 3670Sstevel@tonic-gate /*NOTREACHED*/ 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate } else { 3700Sstevel@tonic-gate msg(gettext("Missing archive file name\n")); 3710Sstevel@tonic-gate dumpabort(); 3720Sstevel@tonic-gate /*NOTREACHED*/ 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate break; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate case 'v': 3770Sstevel@tonic-gate verify++; 3780Sstevel@tonic-gate doingverify++; 3790Sstevel@tonic-gate if (strcmp(tape, "-") == 0) { 3800Sstevel@tonic-gate msg(gettext( 3810Sstevel@tonic-gate "Cannot verify when dumping to standard out.\n")); 3820Sstevel@tonic-gate dumpabort(); 3830Sstevel@tonic-gate /*NOTREACHED*/ 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate break; 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate case 'D': 3880Sstevel@tonic-gate diskette++; 3890Sstevel@tonic-gate break; 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate case 'N': 3920Sstevel@tonic-gate if (argc > 1) { 3930Sstevel@tonic-gate argv++; 3940Sstevel@tonic-gate argc--; 3950Sstevel@tonic-gate if (**argv == '\0') { 3960Sstevel@tonic-gate msg(gettext("Missing name for dumpdates " 3970Sstevel@tonic-gate "entry.\n")); 3980Sstevel@tonic-gate dumpabort(); 3990Sstevel@tonic-gate /*NOTREACHED*/ 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate dname = *argv; 4020Sstevel@tonic-gate if (strlen(dname) > MAXNAMLEN + 2) { 4030Sstevel@tonic-gate msg(gettext("Dumpdates entry name too " 4040Sstevel@tonic-gate "long.\n")); 4050Sstevel@tonic-gate dumpabort(); 4060Sstevel@tonic-gate /*NOTREACHED*/ 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate for (i = 0; i < strlen(dname); i++) { 4090Sstevel@tonic-gate if (isspace(*(dname+i))) { 4100Sstevel@tonic-gate msg(gettext("Dumpdates entry name may " 4110Sstevel@tonic-gate "not contain white space.\n")); 4120Sstevel@tonic-gate dumpabort(); 4130Sstevel@tonic-gate /*NOTREACHED*/ 4140Sstevel@tonic-gate } 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate } else { 4170Sstevel@tonic-gate msg(gettext("Missing name for dumpdates entry.\n")); 4180Sstevel@tonic-gate dumpabort(); 4190Sstevel@tonic-gate /*NOTREACHED*/ 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate break; 4220Sstevel@tonic-gate case 'L': 4230Sstevel@tonic-gate if (argc > 1) { 4240Sstevel@tonic-gate argv++; 4250Sstevel@tonic-gate argc--; 4260Sstevel@tonic-gate if (**argv == '\0') { 4270Sstevel@tonic-gate msg(gettext("Missing tape label name\n")); 4280Sstevel@tonic-gate dumpabort(); 4290Sstevel@tonic-gate /*NOTREACHED*/ 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate tlabel = *argv; 4320Sstevel@tonic-gate if (strlen(tlabel) > (sizeof (spcl.c_label) - 1)) { 4330Sstevel@tonic-gate tlabel[sizeof (spcl.c_label) - 1] = '\0'; 4340Sstevel@tonic-gate msg(gettext( 4350Sstevel@tonic-gate "Truncating label to maximum supported length: `%s'\n"), 4360Sstevel@tonic-gate tlabel); 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate } else { 4390Sstevel@tonic-gate msg(gettext("Missing tape label name\n")); 4400Sstevel@tonic-gate dumpabort(); 4410Sstevel@tonic-gate /*NOTREACHED*/ 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate break; 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate case 'l': 4460Sstevel@tonic-gate autoload++; 4470Sstevel@tonic-gate break; 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate case 'o': 4500Sstevel@tonic-gate offline++; 4510Sstevel@tonic-gate break; 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate case 'S': 4540Sstevel@tonic-gate printsize++; 4550Sstevel@tonic-gate break; 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate #ifdef DEBUG 4580Sstevel@tonic-gate case 'z': 4590Sstevel@tonic-gate xflag++; 4600Sstevel@tonic-gate break; 4610Sstevel@tonic-gate #endif 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate default: 4640Sstevel@tonic-gate msg(gettext("Bad option `%c'\n"), arg[-1]); 4650Sstevel@tonic-gate dumpabort(); 4660Sstevel@tonic-gate /*NOTREACHED*/ 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate if (argc > 1) { 4690Sstevel@tonic-gate argv++; 4700Sstevel@tonic-gate argc--; 4710Sstevel@tonic-gate if (**argv == '\0') { 4720Sstevel@tonic-gate msg(gettext("Bad disk name\n")); 4730Sstevel@tonic-gate dumpabort(); 4740Sstevel@tonic-gate /*NOTREACHED*/ 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate disk = *argv; 4770Sstevel@tonic-gate disk_dynamic = 0; 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate if (disk == NULL) { 4800Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4810Sstevel@tonic-gate "Usage: %s [0123456789fustdWwnNDCcbavloS [argument]] filesystem\n"), 4820Sstevel@tonic-gate myname); 4830Sstevel@tonic-gate Exit(X_ABORT); 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate if (!filenum) 4860Sstevel@tonic-gate filenum = 1; 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate if (signal(SIGINT, interrupt) == SIG_IGN) 4890Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate if (strcmp(tape, "-") == 0) { 4920Sstevel@tonic-gate pipeout++; 4930Sstevel@tonic-gate tape = gettext("standard output"); 4940Sstevel@tonic-gate dumpdev = sdumpdev = strdup(tape); 4950Sstevel@tonic-gate if (dumpdev == NULL) { 4960Sstevel@tonic-gate saverr = errno; 4970Sstevel@tonic-gate msg(gettext("Cannot allocate memory: %s\n"), 4980Sstevel@tonic-gate strerror(saverr)); 4990Sstevel@tonic-gate dumpabort(); 5000Sstevel@tonic-gate /*NOTREACHED*/ 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate /*CONSTANTCONDITION*/ 5030Sstevel@tonic-gate assert(sizeof (spcl.c_label) > 5); 5040Sstevel@tonic-gate (void) strcpy(spcl.c_label, "none"); 5050Sstevel@tonic-gate } else if (*tape == '+') { 5060Sstevel@tonic-gate nextdevice(); 5070Sstevel@tonic-gate (void) strcpy(spcl.c_label, tlabel); 5080Sstevel@tonic-gate } else { 5090Sstevel@tonic-gate /* if not already set, set diskette to default */ 5100Sstevel@tonic-gate if (diskette && strcmp(tape, DEFTAPE) == 0) 5110Sstevel@tonic-gate tape = DISKETTE; 5120Sstevel@tonic-gate nextdevice(); 5130Sstevel@tonic-gate (void) strcpy(spcl.c_label, tlabel); 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate if (cartridge && diskette) { 5160Sstevel@tonic-gate error = 1; 5170Sstevel@tonic-gate msg(gettext("Cannot select both cartridge and diskette\n")); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate if (density && diskette) { 5200Sstevel@tonic-gate error = 1; 5210Sstevel@tonic-gate msg(gettext("Cannot select density of diskette\n")); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate if (tracks && diskette) { 5240Sstevel@tonic-gate error = 1; 5250Sstevel@tonic-gate msg(gettext("Cannot select number of tracks of diskette\n")); 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate if (error) { 5280Sstevel@tonic-gate dumpabort(); 5290Sstevel@tonic-gate /*NOTREACHED*/ 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* 5330Sstevel@tonic-gate * Determine how to default tape size and density 5340Sstevel@tonic-gate * 5350Sstevel@tonic-gate * density tape size 5360Sstevel@tonic-gate * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 5370Sstevel@tonic-gate * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 5380Sstevel@tonic-gate * 5390Sstevel@tonic-gate * Most Sun-2's came with 4 track (20MB) cartridge tape drives, 5400Sstevel@tonic-gate * while most other machines (Sun-3's and non-Sun's) come with 5410Sstevel@tonic-gate * 9 track (45MB) cartridge tape drives. Some Sun-2's came with 5420Sstevel@tonic-gate * 9 track drives, but there is no way for the software to detect 5430Sstevel@tonic-gate * which drive type is installed. Sigh... We make the gross 5440Sstevel@tonic-gate * assumption that #ifdef mc68010 will test for a Sun-2. 5450Sstevel@tonic-gate * 5460Sstevel@tonic-gate * cartridge 8000 bpi (100 bytes/.1") 425 * tracks ft. 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate if (density == 0) 5490Sstevel@tonic-gate density = cartridge ? 100 : 625; 5500Sstevel@tonic-gate if (tracks == 0) 5510Sstevel@tonic-gate tracks = 9; 5520Sstevel@tonic-gate if (!bflag) { 5530Sstevel@tonic-gate if (cartridge) 5540Sstevel@tonic-gate ntrec = CARTRIDGETREC; 5550Sstevel@tonic-gate else if (diskette) 5560Sstevel@tonic-gate ntrec = NTREC; 5570Sstevel@tonic-gate else if (density >= 625) 5580Sstevel@tonic-gate ntrec = HIGHDENSITYTREC; 5590Sstevel@tonic-gate else 5600Sstevel@tonic-gate ntrec = NTREC; 5610Sstevel@tonic-gate /* 5620Sstevel@tonic-gate * save ntrec in case we have to change tp_bsize later. 5630Sstevel@tonic-gate */ 5640Sstevel@tonic-gate saved_ntrec = (ntrec * (tp_bsize/DEV_BSIZE)); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate if (!diskette) { 5670Sstevel@tonic-gate tsize *= 12L*10L; 5680Sstevel@tonic-gate if (cartridge) 5690Sstevel@tonic-gate tsize *= tracks; 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate rmtinit(msg, Exit); 5720Sstevel@tonic-gate if (host) { 5730Sstevel@tonic-gate char *cp = strchr(host, '@'); 5740Sstevel@tonic-gate if (cp == (char *)0) 5750Sstevel@tonic-gate cp = host; 5760Sstevel@tonic-gate else 5770Sstevel@tonic-gate cp++; 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate if (rmthost(host, ntrec) == 0) { 5800Sstevel@tonic-gate msg(gettext("Cannot connect to tape host `%s'\n"), cp); 5810Sstevel@tonic-gate dumpabort(); 5820Sstevel@tonic-gate /*NOTREACHED*/ 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate if (signal(SIGHUP, sigAbort) == SIG_IGN) 5860Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); 5870Sstevel@tonic-gate if (signal(SIGTRAP, sigAbort) == SIG_IGN) 5880Sstevel@tonic-gate (void) signal(SIGTRAP, SIG_IGN); 5890Sstevel@tonic-gate if (signal(SIGFPE, sigAbort) == SIG_IGN) 5900Sstevel@tonic-gate (void) signal(SIGFPE, SIG_IGN); 5910Sstevel@tonic-gate if (signal(SIGBUS, sigAbort) == SIG_IGN) 5920Sstevel@tonic-gate (void) signal(SIGBUS, SIG_IGN); 5930Sstevel@tonic-gate if (signal(SIGSEGV, sigAbort) == SIG_IGN) 5940Sstevel@tonic-gate (void) signal(SIGSEGV, SIG_IGN); 5950Sstevel@tonic-gate if (signal(SIGTERM, sigAbort) == SIG_IGN) 5960Sstevel@tonic-gate (void) signal(SIGTERM, SIG_IGN); 5970Sstevel@tonic-gate if (signal(SIGUSR1, sigAbort) == SIG_IGN) 5980Sstevel@tonic-gate (void) signal(SIGUSR1, SIG_IGN); 5990Sstevel@tonic-gate if (signal(SIGPIPE, sigAbort) == SIG_IGN) 6000Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_IGN); 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate mnttabread(); /* /etc/fstab, /etc/mtab snarfed */ 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate /* 6050Sstevel@tonic-gate * disk can be either the full special file name, 6060Sstevel@tonic-gate * the suffix of the special file name, 6070Sstevel@tonic-gate * the special name missing the leading '/', 6080Sstevel@tonic-gate * the file system name with or without the leading '/'. 6090Sstevel@tonic-gate * NB: we attempt to avoid dumping the block device 6100Sstevel@tonic-gate * (using rawname) because specfs and the vm system 6110Sstevel@tonic-gate * are not necessarily in sync. 6120Sstevel@tonic-gate */ 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate /* 6150Sstevel@tonic-gate * Attempt to roll the log before doing the dump. There's nothing 6160Sstevel@tonic-gate * the user can do if we are unable to roll the log, so we'll silently 6170Sstevel@tonic-gate * ignore failures. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate if ((rl_roll_log(disk) != RL_SUCCESS) && (disk[0] != '/')) { 6200Sstevel@tonic-gate /* Try it again with leading '/'. */ 6210Sstevel@tonic-gate char *slashed; 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate slashed = (char *)malloc(strlen(disk) + 2); 6240Sstevel@tonic-gate if (slashed != (char *)NULL) { 6250Sstevel@tonic-gate (void) sprintf(slashed, "%c%s", '/', disk); 6260Sstevel@tonic-gate (void) rl_roll_log(slashed); 6270Sstevel@tonic-gate free(slashed); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate dt = mnttabsearch(disk, 0); 6310Sstevel@tonic-gate if (dt != 0) { 6320Sstevel@tonic-gate filesystem = dt->mnt_mountp; 6330Sstevel@tonic-gate if (disk_dynamic) { 6340Sstevel@tonic-gate /* LINTED: disk is not NULL */ 6350Sstevel@tonic-gate free(disk); 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate disk = rawname(dt->mnt_special); 6380Sstevel@tonic-gate disk_dynamic = (disk != dt->mnt_special); 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate (void) strncpy(spcl.c_dev, dt->mnt_special, 6410Sstevel@tonic-gate sizeof (spcl.c_dev)); 6420Sstevel@tonic-gate spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0'; 6430Sstevel@tonic-gate (void) strncpy(spcl.c_filesys, dt->mnt_mountp, 6440Sstevel@tonic-gate sizeof (spcl.c_filesys)); 6450Sstevel@tonic-gate spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0'; 6460Sstevel@tonic-gate } else { 6470Sstevel@tonic-gate (void) strncpy(spcl.c_dev, disk, sizeof (spcl.c_dev)); 6480Sstevel@tonic-gate spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0'; 6490Sstevel@tonic-gate #ifdef PARTIAL 6500Sstevel@tonic-gate /* check for partial filesystem dump */ 6510Sstevel@tonic-gate partial_check(); 6520Sstevel@tonic-gate dt = mnttabsearch(disk, 1); 6530Sstevel@tonic-gate if (dt != 0) { 6540Sstevel@tonic-gate filesystem = dt->mnt_mountp; 6550Sstevel@tonic-gate if (disk_dynamic) 6560Sstevel@tonic-gate free(disk); 6570Sstevel@tonic-gate disk = rawname(dt->mnt_special); 6580Sstevel@tonic-gate disk_dynamic = (disk != dt->mnt_special); 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate (void) strncpy(spcl.c_filesys, 6610Sstevel@tonic-gate "a partial file system", sizeof (spcl.c_filesys)); 6620Sstevel@tonic-gate spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0'; 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate else 6650Sstevel@tonic-gate #endif /* PARTIAL */ 6660Sstevel@tonic-gate { 6670Sstevel@tonic-gate char *old_disk = disk; 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate (void) strncpy(spcl.c_filesys, 6700Sstevel@tonic-gate "an unlisted file system", 6710Sstevel@tonic-gate sizeof (spcl.c_filesys)); 6720Sstevel@tonic-gate spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0'; 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate disk = rawname(old_disk); 6750Sstevel@tonic-gate if (disk != old_disk) { 6760Sstevel@tonic-gate if (disk_dynamic) 6770Sstevel@tonic-gate free(old_disk); 6780Sstevel@tonic-gate disk_dynamic = 1; 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * If disk == old_disk, then disk_dynamic's state 6820Sstevel@tonic-gate * does not change. 6830Sstevel@tonic-gate */ 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate fi = open64(disk, O_RDONLY); 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate if (fi < 0) { 6900Sstevel@tonic-gate saverr = errno; 6910Sstevel@tonic-gate msg(gettext("Cannot open dump device `%s': %s\n"), 692*5299Sws195443 disk, strerror(saverr)); 6930Sstevel@tonic-gate Exit(X_ABORT); 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate if (sscanf(&incno, "%1d", &spcl.c_level) != 1) { 6970Sstevel@tonic-gate msg(gettext("Bad dump level `%c' specified\n"), incno); 6980Sstevel@tonic-gate dumpabort(); 6990Sstevel@tonic-gate /*NOTREACHED*/ 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate getitime(); /* /etc/dumpdates snarfed */ 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate sblock = (struct fs *)&sblock_buf; 7040Sstevel@tonic-gate sync(); 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE); 7070Sstevel@tonic-gate if ((sblock->fs_magic != FS_MAGIC) && 7080Sstevel@tonic-gate (sblock->fs_magic != MTB_UFS_MAGIC)) { 7090Sstevel@tonic-gate msg(gettext( 7100Sstevel@tonic-gate "Warning - super-block on device `%s' is corrupt - run fsck\n"), 7110Sstevel@tonic-gate disk); 7120Sstevel@tonic-gate dumpabort(); 7130Sstevel@tonic-gate /*NOTREACHED*/ 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 716757Svsakar if (sblock->fs_magic == FS_MAGIC && 717757Svsakar (sblock->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 && 718757Svsakar sblock->fs_version != UFS_VERSION_MIN)) { 719757Svsakar msg(gettext("Unrecognized UFS version: %d\n"), 720757Svsakar sblock->fs_version); 721757Svsakar dumpabort(); 722757Svsakar /*NOTREACHED*/ 723757Svsakar } 724757Svsakar 7250Sstevel@tonic-gate if (sblock->fs_magic == MTB_UFS_MAGIC && 7260Sstevel@tonic-gate (sblock->fs_version < MTB_UFS_VERSION_MIN || 7270Sstevel@tonic-gate sblock->fs_version > MTB_UFS_VERSION_1)) { 7280Sstevel@tonic-gate msg(gettext("Unrecognized UFS version: %d\n"), 7290Sstevel@tonic-gate sblock->fs_version); 7300Sstevel@tonic-gate dumpabort(); 7310Sstevel@tonic-gate /*NOTREACHED*/ 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate /* 7350Sstevel@tonic-gate * Try to set up for using mmap(2). It only works on the block 7360Sstevel@tonic-gate * device, but if we can use it, things go somewhat faster. If 7370Sstevel@tonic-gate * we can't open it, we'll silently fall back to the old method 7380Sstevel@tonic-gate * (read/memcpy). We also only try this if it's been cleanly 7390Sstevel@tonic-gate * unmounted. Dumping a live filesystem this way runs into 7400Sstevel@tonic-gate * buffer consistency problems. Of course, we don't support 7410Sstevel@tonic-gate * running dump on a mounted filesystem, but some people do it 7420Sstevel@tonic-gate * anyway. 7430Sstevel@tonic-gate */ 7440Sstevel@tonic-gate if (sblock->fs_clean == FSCLEAN) { 7450Sstevel@tonic-gate char *block = unrawname(disk); 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate if (block != NULL) { 7480Sstevel@tonic-gate mapfd = open(block, O_RDONLY, 0); 7490Sstevel@tonic-gate free(block); 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate restart: 7540Sstevel@tonic-gate bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE); 7550Sstevel@tonic-gate if ((sblock->fs_magic != FS_MAGIC) && 7560Sstevel@tonic-gate (sblock->fs_magic != MTB_UFS_MAGIC)) { /* paranoia */ 7570Sstevel@tonic-gate msg(gettext("bad super-block magic number, run fsck\n")); 7580Sstevel@tonic-gate dumpabort(); 7590Sstevel@tonic-gate /*NOTREACHED*/ 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate 762757Svsakar if (sblock->fs_magic == FS_MAGIC && 763757Svsakar (sblock->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 && 764757Svsakar sblock->fs_version != UFS_VERSION_MIN)) { 765757Svsakar msg(gettext("Unrecognized UFS version: %d\n"), 766757Svsakar sblock->fs_version); 767757Svsakar dumpabort(); 768757Svsakar /*NOTREACHED*/ 769757Svsakar } 770757Svsakar 7710Sstevel@tonic-gate if (sblock->fs_magic == MTB_UFS_MAGIC && 7720Sstevel@tonic-gate (sblock->fs_version < MTB_UFS_VERSION_MIN || 7730Sstevel@tonic-gate sblock->fs_version > MTB_UFS_VERSION_1)) { 7740Sstevel@tonic-gate msg(gettext("Unrecognized UFS version: %d\n"), 7750Sstevel@tonic-gate sblock->fs_version); 7760Sstevel@tonic-gate dumpabort(); 7770Sstevel@tonic-gate /*NOTREACHED*/ 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate if (!doingactive) 7810Sstevel@tonic-gate allocino(); 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate /* XXX should sanity-check the super block before trusting/using it */ 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate /* LINTED XXX time truncated - tolerate until tape format changes */ 7860Sstevel@tonic-gate spcl.c_date = (time32_t)time((time_t *)NULL); 7870Sstevel@tonic-gate bcopy(&(spcl.c_shadow), c_shadow_save, sizeof (c_shadow_save)); 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate snapdate = is_fssnap_dump(disk); 7900Sstevel@tonic-gate if (snapdate) 7910Sstevel@tonic-gate spcl.c_date = snapdate; 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate if (!printsize) { 7940Sstevel@tonic-gate msg(gettext("Date of this level %c dump: %s\n"), 7950Sstevel@tonic-gate incno, prdate(spcl.c_date)); 7960Sstevel@tonic-gate msg(gettext("Date of last level %c dump: %s\n"), 797*5299Sws195443 (uchar_t)lastincno, prdate(spcl.c_ddate)); 7980Sstevel@tonic-gate msg(gettext("Dumping %s "), disk); 7990Sstevel@tonic-gate if (filesystem != 0) 8000Sstevel@tonic-gate msgtail("(%.*s:%s) ", 8010Sstevel@tonic-gate /* LINTED unsigned -> signed cast ok */ 8020Sstevel@tonic-gate (int)sizeof (spcl.c_host), spcl.c_host, filesystem); 8030Sstevel@tonic-gate msgtail(gettext("to %s.\n"), sdumpdev); 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate esize = f_esize = o_esize = 0; 8070Sstevel@tonic-gate msiz = roundup(d_howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY), 808*5299Sws195443 TP_BSIZE_MAX); 8090Sstevel@tonic-gate if (!doingactive) { 8100Sstevel@tonic-gate clrmap = (uchar_t *)xcalloc(msiz, sizeof (*clrmap)); 8110Sstevel@tonic-gate filmap = (uchar_t *)xcalloc(msiz, sizeof (*filmap)); 8120Sstevel@tonic-gate dirmap = (uchar_t *)xcalloc(msiz, sizeof (*dirmap)); 8130Sstevel@tonic-gate nodmap = (uchar_t *)xcalloc(msiz, sizeof (*nodmap)); 8140Sstevel@tonic-gate shamap = (uchar_t *)xcalloc(msiz, sizeof (*shamap)); 8150Sstevel@tonic-gate activemap = (uchar_t *)xcalloc(msiz, sizeof (*activemap)); 8160Sstevel@tonic-gate } else { 8170Sstevel@tonic-gate if (clrmap == NULL || filmap == NULL || dirmap == NULL || 8180Sstevel@tonic-gate nodmap == NULL || shamap == NULL || activemap == NULL) { 8190Sstevel@tonic-gate msg(gettext( 8200Sstevel@tonic-gate "Internal error: NULL map pointer while re-dumping active files")); 8210Sstevel@tonic-gate dumpabort(); 8220Sstevel@tonic-gate /*NOTREACHED*/ 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate bzero(clrmap, msiz); 8250Sstevel@tonic-gate bzero(filmap, msiz); 8260Sstevel@tonic-gate bzero(dirmap, msiz); 8270Sstevel@tonic-gate bzero(nodmap, msiz); 8280Sstevel@tonic-gate bzero(shamap, msiz); 8290Sstevel@tonic-gate /* retain active map */ 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate dumpstate = DS_INIT; 8330Sstevel@tonic-gate dumptoarchive = 1; 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate /* 8360Sstevel@tonic-gate * Read cylinder group inode-used bitmaps to avoid reading clear inodes. 8370Sstevel@tonic-gate */ 8380Sstevel@tonic-gate { 8390Sstevel@tonic-gate uchar_t *clrp = clrmap; 8400Sstevel@tonic-gate struct cg *cgp = 8410Sstevel@tonic-gate (struct cg *)xcalloc((uint_t)sblock->fs_cgsize, 1); 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate for (i = 0; i < sblock->fs_ncg; i++) { 8440Sstevel@tonic-gate bread(fsbtodb(sblock, cgtod(sblock, i)), 8450Sstevel@tonic-gate (uchar_t *)cgp, sblock->fs_cgsize); 8460Sstevel@tonic-gate bcopy(cg_inosused(cgp), clrp, 8470Sstevel@tonic-gate (int)sblock->fs_ipg / NBBY); 8480Sstevel@tonic-gate clrp += sblock->fs_ipg / NBBY; 8490Sstevel@tonic-gate } 8500Sstevel@tonic-gate free((char *)cgp); 8510Sstevel@tonic-gate /* XXX right-shift clrmap one bit. why? */ 8520Sstevel@tonic-gate for (i = 0; clrp > clrmap; i <<= NBBY) { 8530Sstevel@tonic-gate i |= *--clrp & ((1<<NBBY) - 1); 8540Sstevel@tonic-gate *clrp = i >> 1; 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate if (!printsize) { 8590Sstevel@tonic-gate msgp = gettext("Mapping (Pass I) [regular files]\n"); 8600Sstevel@tonic-gate msg(msgp); 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate ino = 0; 8640Sstevel@tonic-gate #ifdef PARTIAL 8650Sstevel@tonic-gate if (partial_mark(argc, argv)) { 8660Sstevel@tonic-gate #endif /* PARTIAL */ 8670Sstevel@tonic-gate if (!doingactive) 8680Sstevel@tonic-gate pass(mark, clrmap); /* mark updates 'x'_esize */ 8690Sstevel@tonic-gate else 8700Sstevel@tonic-gate pass(active_mark, clrmap); /* updates 'x'_esize */ 8710Sstevel@tonic-gate #ifdef PARTIAL 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate #endif /* PARTIAL */ 8740Sstevel@tonic-gate do { 8750Sstevel@tonic-gate if (!printsize) { 8760Sstevel@tonic-gate msgp = gettext("Mapping (Pass II) [directories]\n"); 8770Sstevel@tonic-gate msg(msgp); 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate nadded = 0; 8800Sstevel@tonic-gate ino = 0; 8810Sstevel@tonic-gate pass(add, dirmap); 8820Sstevel@tonic-gate } while (nadded); 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate ino = 0; /* adjust estimated size for shadow inodes */ 8850Sstevel@tonic-gate pass(markshad, nodmap); 8860Sstevel@tonic-gate ino = 0; 8870Sstevel@tonic-gate pass(estshad, shamap); 8880Sstevel@tonic-gate freeshad(); 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate bmapest(clrmap); 8910Sstevel@tonic-gate bmapest(nodmap); 8920Sstevel@tonic-gate esize = o_esize + f_esize; 8930Sstevel@tonic-gate if (diskette) { 8940Sstevel@tonic-gate /* estimate number of floppies */ 8950Sstevel@tonic-gate if (tsize != 0) 8960Sstevel@tonic-gate fetapes = (double)(esize + ntrec) / (double)tsize; 8970Sstevel@tonic-gate } else if (cartridge) { 8980Sstevel@tonic-gate /* 8990Sstevel@tonic-gate * Estimate number of tapes, assuming streaming stops at 9000Sstevel@tonic-gate * the end of each block written, and not in mid-block. 9010Sstevel@tonic-gate * Assume no erroneous blocks; this can be compensated for 9020Sstevel@tonic-gate * with an artificially low tape size. 9030Sstevel@tonic-gate */ 9040Sstevel@tonic-gate tenthsperirg = 16; /* actually 15.48, says Archive */ 9050Sstevel@tonic-gate if (tsize != 0) 9060Sstevel@tonic-gate fetapes = ((double)esize /* blocks */ 9070Sstevel@tonic-gate * (tp_bsize /* bytes/block */ 9080Sstevel@tonic-gate * (1.0/density)) /* 0.1" / byte */ 9090Sstevel@tonic-gate + 9100Sstevel@tonic-gate (double)esize /* blocks */ 9110Sstevel@tonic-gate * (1.0/ntrec) /* streaming-stops per block */ 9120Sstevel@tonic-gate * tenthsperirg) /* 0.1" / streaming-stop */ 9130Sstevel@tonic-gate * (1.0 / tsize); /* tape / 0.1" */ 9140Sstevel@tonic-gate } else { 9150Sstevel@tonic-gate /* Estimate number of tapes, for old fashioned 9-track tape */ 9160Sstevel@tonic-gate #ifdef sun 9170Sstevel@tonic-gate /* sun has long irg's */ 9180Sstevel@tonic-gate tenthsperirg = (density == 625) ? 6 : 12; 9190Sstevel@tonic-gate #else 9200Sstevel@tonic-gate tenthsperirg = (density == 625) ? 5 : 8; 9210Sstevel@tonic-gate #endif 9220Sstevel@tonic-gate if (tsize != 0) 9230Sstevel@tonic-gate fetapes = ((double)esize /* blocks */ 9240Sstevel@tonic-gate * (tp_bsize /* bytes / block */ 9250Sstevel@tonic-gate * (1.0/density)) /* 0.1" / byte */ 9260Sstevel@tonic-gate + 9270Sstevel@tonic-gate (double)esize /* blocks */ 9280Sstevel@tonic-gate * (1.0/ntrec) /* IRG's / block */ 9290Sstevel@tonic-gate * tenthsperirg) /* 0.1" / IRG */ 9300Sstevel@tonic-gate * (1.0 / tsize); /* tape / 0.1" */ 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate etapes = fetapes; /* truncating assignment */ 9340Sstevel@tonic-gate etapes++; 9350Sstevel@tonic-gate /* count the nodemap on each additional tape */ 9360Sstevel@tonic-gate for (i = 1; i < etapes; i++) 9370Sstevel@tonic-gate bmapest(nodmap); 9380Sstevel@tonic-gate /* 9390Sstevel@tonic-gate * If the above bmapest is called, it changes o_esize and f_esize. 9400Sstevel@tonic-gate * So we will recalculate esize here anyway to make sure. 9410Sstevel@tonic-gate * Also, add tape headers and trailer records. 9420Sstevel@tonic-gate */ 9430Sstevel@tonic-gate esize = o_esize + f_esize + etapes + ntrec; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate /* 9460Sstevel@tonic-gate * If the estimated number of tp_bsize tape blocks is greater than 9470Sstevel@tonic-gate * INT_MAX we have to adjust tp_bsize and ntrec to handle 9480Sstevel@tonic-gate * the larger dump. esize is an estimate, so we 'fudge' 9490Sstevel@tonic-gate * INT_MAX a little. If tp_bsize is adjusted, it will be adjusted 9500Sstevel@tonic-gate * to the size needed for this dump (2048, 4096, 8192, ...) 9510Sstevel@tonic-gate */ 9520Sstevel@tonic-gate if (esize > (INT_MAX - FUDGE_FACTOR)) { /* esize is too big */ 9530Sstevel@tonic-gate forceflag++; 9540Sstevel@tonic-gate esize_shift = 9550Sstevel@tonic-gate ((esize + (INT_MAX - FUDGE_FACTOR) - 1)/ 9560Sstevel@tonic-gate ((u_offset_t)(INT_MAX - FUDGE_FACTOR))) - 1; 9570Sstevel@tonic-gate if ((esize_shift > ESIZE_SHIFT_MAX) || (ntrec == 0)) { 9580Sstevel@tonic-gate msgp = gettext( 9590Sstevel@tonic-gate "Block factor %d ('b' flag) is too small for this size dump."); 9600Sstevel@tonic-gate msg(msgp, saved_ntrec); 9610Sstevel@tonic-gate dumpabort(); 9620Sstevel@tonic-gate /*NOTREACHED*/ 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate /* 9650Sstevel@tonic-gate * recalculate esize from: 9660Sstevel@tonic-gate * o_esize - header tape records 9670Sstevel@tonic-gate * (f_esize + (num_mult -1)) >> esize_shift - new non-header 9680Sstevel@tonic-gate * tape records for files/maps 9690Sstevel@tonic-gate * etapes - TS_TAPE records 9700Sstevel@tonic-gate * ntrec - TS_END records 9710Sstevel@tonic-gate * 9720Sstevel@tonic-gate * ntrec is adjusted so a tape record is still 'b' flag 9730Sstevel@tonic-gate * number of DEV_BSIZE (512) in size 9740Sstevel@tonic-gate */ 9750Sstevel@tonic-gate new_mult = (tp_bsize << esize_shift)/tp_bsize; 9760Sstevel@tonic-gate tp_bsize = (tp_bsize << esize_shift); 9770Sstevel@tonic-gate esize = o_esize + ((f_esize + 9780Sstevel@tonic-gate (new_mult - 1)) >> esize_shift) + etapes + ntrec; 9790Sstevel@tonic-gate ntrec = (saved_ntrec/(tp_bsize/DEV_BSIZE)); 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate if (forceflag != 0) { 9820Sstevel@tonic-gate msgp = gettext( 9830Sstevel@tonic-gate "Forcing larger tape block size (%d).\n"); 9840Sstevel@tonic-gate msg(msgp, tp_bsize); 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate alloctape(); /* allocate tape buffers */ 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate assert((tp_bsize / DEV_BSIZE != 0) && (tp_bsize % DEV_BSIZE == 0)); 9890Sstevel@tonic-gate /* 9900Sstevel@tonic-gate * If all we wanted was the size estimate, 9910Sstevel@tonic-gate * just print it out and exit. 9920Sstevel@tonic-gate */ 9930Sstevel@tonic-gate if (printsize) { 9940Sstevel@tonic-gate (void) printf("%llu\n", esize * tp_bsize); 9950Sstevel@tonic-gate Exit(0); 9960Sstevel@tonic-gate } 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate if (tsize != 0) { 9990Sstevel@tonic-gate if (diskette) 10000Sstevel@tonic-gate msgp = gettext( 10010Sstevel@tonic-gate "Estimated %lld blocks (%s) on %3.2f diskettes.\n"); 10020Sstevel@tonic-gate else 10030Sstevel@tonic-gate msgp = gettext( 10040Sstevel@tonic-gate "Estimated %lld blocks (%s) on %3.2f tapes.\n"); 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate msg(msgp, 10070Sstevel@tonic-gate (esize*(tp_bsize/DEV_BSIZE)), mb(esize), fetapes); 10080Sstevel@tonic-gate } else { 10090Sstevel@tonic-gate msgp = gettext("Estimated %lld blocks (%s).\n"); 10100Sstevel@tonic-gate msg(msgp, (esize*(tp_bsize/DEV_BSIZE)), mb(esize)); 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate dumpstate = DS_CLRI; 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate otape(1); /* bitmap is the first to tape write */ 10160Sstevel@tonic-gate *telapsed = 0; 10170Sstevel@tonic-gate (void) time(tstart_writing); 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate /* filmap indicates all non-directory inodes */ 10200Sstevel@tonic-gate { 10210Sstevel@tonic-gate uchar_t *np, *fp, *dp; 10220Sstevel@tonic-gate np = nodmap; 10230Sstevel@tonic-gate dp = dirmap; 10240Sstevel@tonic-gate fp = filmap; 10250Sstevel@tonic-gate for (i = 0; i < msiz; i++) 10260Sstevel@tonic-gate *fp++ = *np++ ^ *dp++; 10270Sstevel@tonic-gate } 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate while (dumpstate != DS_DONE) { 10300Sstevel@tonic-gate /* 10310Sstevel@tonic-gate * When we receive EOT notification from 10320Sstevel@tonic-gate * the writer, the signal handler calls 10330Sstevel@tonic-gate * rollforward and then jumps here. 10340Sstevel@tonic-gate */ 10350Sstevel@tonic-gate (void) setjmp(checkpoint_buf); 10360Sstevel@tonic-gate switch (dumpstate) { 10370Sstevel@tonic-gate case DS_INIT: 10380Sstevel@tonic-gate /* 10390Sstevel@tonic-gate * We get here if a tape error occurred 10400Sstevel@tonic-gate * after releasing the name lock but before 10410Sstevel@tonic-gate * the volume containing the last of the 10420Sstevel@tonic-gate * dir info was completed. We have to start 10430Sstevel@tonic-gate * all over in this case. 10440Sstevel@tonic-gate */ 10450Sstevel@tonic-gate { 10460Sstevel@tonic-gate char *rmsg = gettext( 10470Sstevel@tonic-gate "Warning - output error occurred after releasing name lock\n\ 10480Sstevel@tonic-gate \tThe dump will restart\n"); 10490Sstevel@tonic-gate msg(rmsg); 10500Sstevel@tonic-gate goto restart; 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate /* NOTREACHED */ 10530Sstevel@tonic-gate case DS_START: 10540Sstevel@tonic-gate case DS_CLRI: 10550Sstevel@tonic-gate ino = UFSROOTINO; 10560Sstevel@tonic-gate dumptoarchive = 1; 10570Sstevel@tonic-gate bitmap(clrmap, TS_CLRI); 10580Sstevel@tonic-gate nextstate(DS_BITS); 10590Sstevel@tonic-gate /* FALLTHROUGH */ 10600Sstevel@tonic-gate case DS_BITS: 10610Sstevel@tonic-gate ino = UFSROOTINO; 10620Sstevel@tonic-gate dumptoarchive = 1; 10630Sstevel@tonic-gate if (BIT(UFSROOTINO, nodmap)) /* empty dump check */ 10640Sstevel@tonic-gate bitmap(nodmap, TS_BITS); 10650Sstevel@tonic-gate nextstate(DS_DIRS); 10660Sstevel@tonic-gate if (!doingverify) { 10670Sstevel@tonic-gate msgp = gettext( 1068*5299Sws195443 "Dumping (Pass III) [directories]\n"); 10690Sstevel@tonic-gate msg(msgp); 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate /* FALLTHROUGH */ 10720Sstevel@tonic-gate case DS_DIRS: 10730Sstevel@tonic-gate dumptoarchive = 1; 10740Sstevel@tonic-gate pass(dirdump, dirmap); 10750Sstevel@tonic-gate nextstate(DS_FILES); 10760Sstevel@tonic-gate if (!doingverify) { 10770Sstevel@tonic-gate msgp = gettext( 1078*5299Sws195443 "Dumping (Pass IV) [regular files]\n"); 10790Sstevel@tonic-gate msg(msgp); 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate /* FALLTHROUGH */ 10820Sstevel@tonic-gate case DS_FILES: 10830Sstevel@tonic-gate dumptoarchive = 0; 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate pass(lf_dump, filmap); 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate flushcmds(); 10880Sstevel@tonic-gate dumpstate = DS_END; /* don't reset ino */ 10890Sstevel@tonic-gate /* FALLTHROUGH */ 10900Sstevel@tonic-gate case DS_END: 10910Sstevel@tonic-gate dumptoarchive = 1; 10920Sstevel@tonic-gate spcl.c_type = TS_END; 10930Sstevel@tonic-gate for (i = 0; i < ntrec; i++) { 10940Sstevel@tonic-gate spclrec(); 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate flusht(); 10970Sstevel@tonic-gate break; 10980Sstevel@tonic-gate case DS_DONE: 10990Sstevel@tonic-gate break; 11000Sstevel@tonic-gate default: 11010Sstevel@tonic-gate msg(gettext("Internal state error\n")); 11020Sstevel@tonic-gate dumpabort(); 11030Sstevel@tonic-gate /*NOTREACHED*/ 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate } 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate if ((! doingactive) && (! active)) 11080Sstevel@tonic-gate trewind(); 11090Sstevel@tonic-gate if (verify && !doingverify) { 11100Sstevel@tonic-gate msgp = gettext("Finished writing last dump volume\n"); 11110Sstevel@tonic-gate msg(msgp); 11120Sstevel@tonic-gate Exit(X_VERIFY); 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate if (spcl.c_volume > 1) 11150Sstevel@tonic-gate (void) snprintf(msgbuf, sizeof (msgbuf), 11160Sstevel@tonic-gate gettext("%lld blocks (%s) on %ld volumes"), 11170Sstevel@tonic-gate ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)), 11180Sstevel@tonic-gate mb((u_offset_t)(unsigned)(spcl.c_tapea)), 11190Sstevel@tonic-gate spcl.c_volume); 11200Sstevel@tonic-gate else 11210Sstevel@tonic-gate (void) snprintf(msgbuf, sizeof (msgbuf), 11220Sstevel@tonic-gate gettext("%lld blocks (%s) on 1 volume"), 11230Sstevel@tonic-gate ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)), 11240Sstevel@tonic-gate mb((u_offset_t)(unsigned)(spcl.c_tapea))); 11250Sstevel@tonic-gate if (timeclock((time_t)0) != (time_t)0) { 11260Sstevel@tonic-gate (void) snprintf(kbsbuf, sizeof (kbsbuf), 11270Sstevel@tonic-gate gettext(" at %ld KB/sec"), 11280Sstevel@tonic-gate (long)(((float)spcl.c_tapea / (float)timeclock((time_t)0)) 1129*5299Sws195443 * 1000.0)); 11300Sstevel@tonic-gate (void) strcat(msgbuf, kbsbuf); 11310Sstevel@tonic-gate } 11320Sstevel@tonic-gate (void) strcat(msgbuf, "\n"); 11330Sstevel@tonic-gate msg(msgbuf); 11340Sstevel@tonic-gate (void) timeclock((time_t)-1); 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate if (archive) 11370Sstevel@tonic-gate msg(gettext("Archiving dump to `%s'\n"), archivefile); 11380Sstevel@tonic-gate if (active && !verify) { 11390Sstevel@tonic-gate nextstate(DS_INIT); 11400Sstevel@tonic-gate activepass(); 11410Sstevel@tonic-gate goto restart; 11420Sstevel@tonic-gate } 11430Sstevel@tonic-gate msgp = gettext("DUMP IS DONE\n"); 11440Sstevel@tonic-gate msg(msgp); 11450Sstevel@tonic-gate broadcast(msgp); 11460Sstevel@tonic-gate if (! doingactive) 11470Sstevel@tonic-gate putitime(); 11480Sstevel@tonic-gate Exit(X_FINOK); 11491053Smaheshvs 11501053Smaheshvs /*NOTREACHED*/ 11510Sstevel@tonic-gate return (0); 11520Sstevel@tonic-gate } 11530Sstevel@tonic-gate 11540Sstevel@tonic-gate void 11551053Smaheshvs sigAbort(int sig) 11560Sstevel@tonic-gate { 11570Sstevel@tonic-gate char *sigtype; 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate switch (sig) { 11600Sstevel@tonic-gate case SIGHUP: 11610Sstevel@tonic-gate sigtype = "SIGHUP"; 11620Sstevel@tonic-gate break; 11630Sstevel@tonic-gate case SIGTRAP: 11640Sstevel@tonic-gate sigtype = "SIGTRAP"; 11650Sstevel@tonic-gate break; 11660Sstevel@tonic-gate case SIGFPE: 11670Sstevel@tonic-gate sigtype = "SIGFPE"; 11680Sstevel@tonic-gate break; 11690Sstevel@tonic-gate case SIGBUS: 11700Sstevel@tonic-gate msg(gettext("%s ABORTING!\n"), "SIGBUS()"); 11710Sstevel@tonic-gate (void) signal(SIGUSR2, SIG_DFL); 11720Sstevel@tonic-gate abort(); 11730Sstevel@tonic-gate /*NOTREACHED*/ 11740Sstevel@tonic-gate case SIGSEGV: 11750Sstevel@tonic-gate msg(gettext("%s ABORTING!\n"), "SIGSEGV()"); 11760Sstevel@tonic-gate (void) signal(SIGUSR2, SIG_DFL); 11770Sstevel@tonic-gate abort(); 11780Sstevel@tonic-gate /*NOTREACHED*/ 11790Sstevel@tonic-gate case SIGALRM: 11800Sstevel@tonic-gate sigtype = "SIGALRM"; 11810Sstevel@tonic-gate break; 11820Sstevel@tonic-gate case SIGTERM: 11830Sstevel@tonic-gate sigtype = "SIGTERM"; 11840Sstevel@tonic-gate break; 11850Sstevel@tonic-gate case SIGPIPE: 11860Sstevel@tonic-gate msg(gettext("Broken pipe\n")); 11870Sstevel@tonic-gate dumpabort(); 11880Sstevel@tonic-gate /*NOTREACHED*/ 11890Sstevel@tonic-gate default: 11900Sstevel@tonic-gate sigtype = "SIGNAL"; 11910Sstevel@tonic-gate break; 11920Sstevel@tonic-gate } 11930Sstevel@tonic-gate msg(gettext("%s() try rewriting\n"), sigtype); 11940Sstevel@tonic-gate if (pipeout) { 11950Sstevel@tonic-gate msg(gettext("Unknown signal, Cannot recover\n")); 11960Sstevel@tonic-gate dumpabort(); 11970Sstevel@tonic-gate /*NOTREACHED*/ 11980Sstevel@tonic-gate } 11990Sstevel@tonic-gate msg(gettext("Rewriting attempted as response to unknown signal.\n")); 12000Sstevel@tonic-gate (void) fflush(stderr); 12010Sstevel@tonic-gate (void) fflush(stdout); 12020Sstevel@tonic-gate close_rewind(); 12030Sstevel@tonic-gate Exit(X_REWRITE); 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate /* Note that returned value is malloc'd if != cp && != NULL */ 12070Sstevel@tonic-gate char * 12081053Smaheshvs rawname(char *cp) 12090Sstevel@tonic-gate { 12100Sstevel@tonic-gate struct stat64 st; 12110Sstevel@tonic-gate char *dp; 12120Sstevel@tonic-gate extern char *getfullrawname(); 12130Sstevel@tonic-gate 12140Sstevel@tonic-gate if (stat64(cp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFBLK) 12150Sstevel@tonic-gate return (cp); 12160Sstevel@tonic-gate 12170Sstevel@tonic-gate dp = getfullrawname(cp); 12180Sstevel@tonic-gate if (dp == 0) 12190Sstevel@tonic-gate return (0); 12200Sstevel@tonic-gate if (*dp == '\0') { 12210Sstevel@tonic-gate free(dp); 12220Sstevel@tonic-gate return (0); 12230Sstevel@tonic-gate } 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate if (stat64(dp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFCHR) { 12260Sstevel@tonic-gate free(dp); 12270Sstevel@tonic-gate return (cp); 12280Sstevel@tonic-gate } 12290Sstevel@tonic-gate 12300Sstevel@tonic-gate return (dp); 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate static char * 12341053Smaheshvs mb(u_offset_t blks) 12350Sstevel@tonic-gate { 12360Sstevel@tonic-gate static char buf[16]; 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate if (blks < 1024) 12390Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%lldKB", blks); 12400Sstevel@tonic-gate else 12410Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%.2fMB", 12420Sstevel@tonic-gate ((double)(blks*tp_bsize)) / (double)(1024*1024)); 12430Sstevel@tonic-gate return (buf); 12440Sstevel@tonic-gate } 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate #ifdef signal 12471053Smaheshvs void (*nsignal(int sig, void (*act)(int)))(int) 12480Sstevel@tonic-gate { 12490Sstevel@tonic-gate struct sigaction sa, osa; 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate sa.sa_handler = act; 12520Sstevel@tonic-gate (void) sigemptyset(&sa.sa_mask); 12530Sstevel@tonic-gate sa.sa_flags = SA_RESTART; 12540Sstevel@tonic-gate if (sigaction(sig, &sa, &osa) < 0) 12550Sstevel@tonic-gate return ((void (*)(int))-1); 12560Sstevel@tonic-gate return (osa.sa_handler); 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate #endif 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate static void 12611053Smaheshvs nextstate(int state) 12620Sstevel@tonic-gate { 12630Sstevel@tonic-gate /* LINTED assigned value never used - kept for documentary purposes */ 12640Sstevel@tonic-gate dumpstate = state; 12650Sstevel@tonic-gate /* LINTED assigned value never used - kept for documentary purposes */ 12660Sstevel@tonic-gate ino = 0; 12670Sstevel@tonic-gate /* LINTED assigned value never used - kept for documentary purposes */ 12680Sstevel@tonic-gate pos = 0; 12690Sstevel@tonic-gate leftover = 0; 12700Sstevel@tonic-gate } 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate /* 12730Sstevel@tonic-gate * timeclock() function, for keeping track of how much time we've spent 12740Sstevel@tonic-gate * writing to the tape device. it always returns the amount of time 12750Sstevel@tonic-gate * already spent, in milliseconds. if you pass it a positive, then that's 12760Sstevel@tonic-gate * telling it that we're writing, so the time counts. if you pass it a 12770Sstevel@tonic-gate * zero, then that's telling it we're not writing; perhaps we're waiting 12780Sstevel@tonic-gate * for user input. 12790Sstevel@tonic-gate * 12800Sstevel@tonic-gate * a state of -1 resets everything. 12810Sstevel@tonic-gate */ 12820Sstevel@tonic-gate time32_t 12831053Smaheshvs timeclock(time32_t state) 12840Sstevel@tonic-gate { 12850Sstevel@tonic-gate static int *currentState = NULL; 12860Sstevel@tonic-gate static struct timeval *clockstart; 12870Sstevel@tonic-gate static time32_t *emilli; 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate struct timeval current[1]; 12900Sstevel@tonic-gate int fd, saverr; 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate #ifdef DEBUG 12930Sstevel@tonic-gate fprintf(stderr, "pid=%d timeclock ", getpid()); 12940Sstevel@tonic-gate if (state == (time32_t)-1) 12950Sstevel@tonic-gate fprintf(stderr, "cleared\n"); 12960Sstevel@tonic-gate else if (state > 0) 12970Sstevel@tonic-gate fprintf(stderr, "ticking\n"); 12980Sstevel@tonic-gate else 12990Sstevel@tonic-gate fprintf(stderr, "paused\n"); 13000Sstevel@tonic-gate #endif /* DEBUG */ 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate /* if we haven't setup the shared memory, init */ 13030Sstevel@tonic-gate if (currentState == (int *)NULL) { 13040Sstevel@tonic-gate if ((fd = open("/dev/zero", O_RDWR)) < 0) { 13050Sstevel@tonic-gate saverr = errno; 13060Sstevel@tonic-gate msg(gettext("Cannot open `%s': %s\n"), 1307*5299Sws195443 "/dev/zero", strerror(saverr)); 13080Sstevel@tonic-gate dumpabort(); 13090Sstevel@tonic-gate /*NOTREACHED*/ 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate /*LINTED [mmap always returns an aligned value]*/ 13120Sstevel@tonic-gate currentState = (int *)mmap((char *)0, getpagesize(), 1313*5299Sws195443 PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0); 13140Sstevel@tonic-gate if (currentState == (int *)-1) { 13150Sstevel@tonic-gate saverr = errno; 13160Sstevel@tonic-gate msg(gettext( 1317*5299Sws195443 "Cannot memory map monitor variables: %s\n"), 1318*5299Sws195443 strerror(saverr)); 13190Sstevel@tonic-gate dumpabort(); 13200Sstevel@tonic-gate /*NOTREACHED*/ 13210Sstevel@tonic-gate } 13220Sstevel@tonic-gate (void) close(fd); 13230Sstevel@tonic-gate 13240Sstevel@tonic-gate /* LINTED currentState is sufficiently aligned */ 13250Sstevel@tonic-gate clockstart = (struct timeval *)(currentState + 1); 13260Sstevel@tonic-gate emilli = (time32_t *)(clockstart + 1); 13270Sstevel@tonic-gate /* Note everything is initialized to zero via /dev/zero */ 13280Sstevel@tonic-gate } 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate if (state == (time32_t)-1) { 13310Sstevel@tonic-gate bzero(clockstart, sizeof (*clockstart)); 13320Sstevel@tonic-gate *currentState = 0; 13330Sstevel@tonic-gate *emilli = (time32_t)0; 13340Sstevel@tonic-gate return (0); 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate (void) gettimeofday(current, NULL); 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate if (*currentState != 0) { 13400Sstevel@tonic-gate current->tv_usec += 1000000; 13410Sstevel@tonic-gate current->tv_sec--; 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate /* LINTED: result will fit in a time32_t */ 13440Sstevel@tonic-gate *emilli += (current->tv_sec - clockstart->tv_sec) * 1000; 13450Sstevel@tonic-gate /* LINTED: result will fit in a time32_t */ 13460Sstevel@tonic-gate *emilli += (current->tv_usec - clockstart->tv_usec) / 1000; 13470Sstevel@tonic-gate } 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate if (state != 0) 13500Sstevel@tonic-gate bcopy(current, clockstart, sizeof (current)); 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate *currentState = state; 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate return (*emilli); 13550Sstevel@tonic-gate } 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate static int 13580Sstevel@tonic-gate statcmp(const struct stat64 *left, const struct stat64 *right) 13590Sstevel@tonic-gate { 13600Sstevel@tonic-gate int result = 1; 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate if ((left->st_dev == right->st_dev) && 13630Sstevel@tonic-gate (left->st_ino == right->st_ino) && 13640Sstevel@tonic-gate (left->st_mode == right->st_mode) && 13650Sstevel@tonic-gate (left->st_nlink == right->st_nlink) && 13660Sstevel@tonic-gate (left->st_uid == right->st_uid) && 13670Sstevel@tonic-gate (left->st_gid == right->st_gid) && 13680Sstevel@tonic-gate (left->st_rdev == right->st_rdev) && 13690Sstevel@tonic-gate (left->st_ctim.tv_sec == right->st_ctim.tv_sec) && 13700Sstevel@tonic-gate (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) && 13710Sstevel@tonic-gate (left->st_mtim.tv_sec == right->st_mtim.tv_sec) && 1372*5299Sws195443 (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec)) { 1373*5299Sws195443 /* 1374*5299Sws195443 * Unlike in the ufsrestore version 1375*5299Sws195443 * st_blocks and st_blksiz are not 1376*5299Sws195443 * compared. The reason for this is 1377*5299Sws195443 * problems with zfs dump files. Zfs 1378*5299Sws195443 * changes it's statistics in those 1379*5299Sws195443 * fields. 1380*5299Sws195443 */ 13810Sstevel@tonic-gate result = 0; 13820Sstevel@tonic-gate } 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate return (result); 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate /* 13880Sstevel@tonic-gate * Safely open a file or device. 13890Sstevel@tonic-gate */ 13900Sstevel@tonic-gate static int 13910Sstevel@tonic-gate safe_open_common(const char *filename, int mode, int perms, int device) 13920Sstevel@tonic-gate { 13930Sstevel@tonic-gate int fd; 13940Sstevel@tonic-gate int working_mode; 13950Sstevel@tonic-gate int saverr; 13960Sstevel@tonic-gate char *errtext; 13970Sstevel@tonic-gate struct stat64 pre_stat, pre_lstat; 13980Sstevel@tonic-gate struct stat64 post_stat, post_lstat; 13990Sstevel@tonic-gate 14000Sstevel@tonic-gate /* 14010Sstevel@tonic-gate * Don't want to be spoofed into trashing something we 14020Sstevel@tonic-gate * shouldn't, thus the following rigamarole. If it doesn't 14030Sstevel@tonic-gate * exist, we create it and proceed. Otherwise, require that 14040Sstevel@tonic-gate * what's there be a real file with no extraneous links and 14050Sstevel@tonic-gate * owned by whoever ran us. 14060Sstevel@tonic-gate * 14070Sstevel@tonic-gate * The silliness with using both lstat() and fstat() is to avoid 14080Sstevel@tonic-gate * race-condition games with someone replacing the file with a 14090Sstevel@tonic-gate * symlink after we've opened it. If there was an flstat(), 14100Sstevel@tonic-gate * we wouldn't need the fstat(). 14110Sstevel@tonic-gate * 14120Sstevel@tonic-gate * The initial open with the hard-coded flags is ok even if we 14130Sstevel@tonic-gate * are intending to open only for reading. If it succeeds, 14140Sstevel@tonic-gate * then the file did not exist, and we'll synthesize an appropriate 14150Sstevel@tonic-gate * complaint below. Otherwise, it does exist, so we won't be 14160Sstevel@tonic-gate * truncating it with the open. 14170Sstevel@tonic-gate */ 14180Sstevel@tonic-gate if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE, 14190Sstevel@tonic-gate perms)) < 0) { 14200Sstevel@tonic-gate if (errno == EEXIST) { 14210Sstevel@tonic-gate if (lstat64(filename, &pre_lstat) < 0) { 14220Sstevel@tonic-gate return (-1); 14230Sstevel@tonic-gate } 14240Sstevel@tonic-gate 14250Sstevel@tonic-gate if (stat64(filename, &pre_stat) < 0) { 14260Sstevel@tonic-gate return (-1); 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY); 14300Sstevel@tonic-gate working_mode |= O_LARGEFILE; 14310Sstevel@tonic-gate if ((fd = open(filename, working_mode)) < 0) { 14320Sstevel@tonic-gate if (errno == ENOENT) { 14330Sstevel@tonic-gate errtext = gettext( 14340Sstevel@tonic-gate "Unexpected condition detected: %s used to exist, but doesn't any longer\n"); 14350Sstevel@tonic-gate msg(errtext, filename); 14360Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename); 14370Sstevel@tonic-gate errno = ENOENT; 14380Sstevel@tonic-gate } 14390Sstevel@tonic-gate return (-1); 14400Sstevel@tonic-gate } 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate if (lstat64(filename, &post_lstat) < 0) { 14430Sstevel@tonic-gate saverr = errno; 14440Sstevel@tonic-gate (void) close(fd); 14450Sstevel@tonic-gate errno = saverr; 14460Sstevel@tonic-gate return (-1); 14470Sstevel@tonic-gate } 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate if (fstat64(fd, &post_stat) < 0) { 14500Sstevel@tonic-gate saverr = errno; 14510Sstevel@tonic-gate (void) close(fd); 14520Sstevel@tonic-gate errno = saverr; 14530Sstevel@tonic-gate return (-1); 14540Sstevel@tonic-gate } 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate /* 14570Sstevel@tonic-gate * Can't just use memcmp(3C), because the access 14580Sstevel@tonic-gate * time is updated by open(2). 14590Sstevel@tonic-gate */ 14600Sstevel@tonic-gate if (statcmp(&pre_lstat, &post_lstat) != 0) { 1461*5299Sws195443 errtext = gettext("Unexpected change detected: " 1462*5299Sws195443 "%s's lstat(2) information changed\n"); 14630Sstevel@tonic-gate msg(errtext, filename); 14640Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename); 14650Sstevel@tonic-gate errno = EPERM; 14660Sstevel@tonic-gate return (-1); 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate 14690Sstevel@tonic-gate if (statcmp(&pre_stat, &post_stat) != 0) { 1470*5299Sws195443 errtext = gettext("Unexpected change detected: " 1471*5299Sws195443 "%s's stat(2) information changed\n"), 1472*5299Sws195443 msg(errtext, filename); 14730Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename); 14740Sstevel@tonic-gate errno = EPERM; 14750Sstevel@tonic-gate return (-1); 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate /* 14790Sstevel@tonic-gate * If inode, device, or type are wrong, bail out. 14800Sstevel@tonic-gate * Note using post_stat instead of post_lstat for the 14810Sstevel@tonic-gate * S_ISCHR() test. This is to allow the /dev -> 14820Sstevel@tonic-gate * /devices bit to work, as long as the final target 14830Sstevel@tonic-gate * is a character device (i.e., raw disk or tape). 14840Sstevel@tonic-gate */ 14850Sstevel@tonic-gate if (device && !(S_ISCHR(post_stat.st_mode)) && 14860Sstevel@tonic-gate !(S_ISFIFO(post_stat.st_mode)) && 14870Sstevel@tonic-gate !(S_ISREG(post_lstat.st_mode))) { 1488*5299Sws195443 errtext = gettext("Unexpected condition " 1489*5299Sws195443 "detected: %s is not a supported device\n"), 1490*5299Sws195443 msg(errtext, filename); 14910Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename); 14920Sstevel@tonic-gate (void) close(fd); 14930Sstevel@tonic-gate errno = EPERM; 14940Sstevel@tonic-gate return (-1); 14950Sstevel@tonic-gate } else if (!device && 14960Sstevel@tonic-gate (!S_ISREG(post_lstat.st_mode) || 14970Sstevel@tonic-gate (post_stat.st_ino != post_lstat.st_ino) || 14980Sstevel@tonic-gate (post_stat.st_dev != post_lstat.st_dev))) { 1499*5299Sws195443 errtext = gettext("Unexpected condition " 1500*5299Sws195443 "detected: %s is not a regular file\n"), 1501*5299Sws195443 msg(errtext, filename); 15020Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename); 15030Sstevel@tonic-gate (void) close(fd); 15040Sstevel@tonic-gate errno = EPERM; 15050Sstevel@tonic-gate return (-1); 15060Sstevel@tonic-gate } 15070Sstevel@tonic-gate 15080Sstevel@tonic-gate /* 15090Sstevel@tonic-gate * Bad link count implies someone's linked our 15100Sstevel@tonic-gate * target to something else, which we probably 15110Sstevel@tonic-gate * shouldn't step on. 15120Sstevel@tonic-gate */ 15130Sstevel@tonic-gate if (post_lstat.st_nlink != 1) { 1514*5299Sws195443 errtext = gettext("Unexpected condition " 1515*5299Sws195443 "detected: %s must have exactly one " 1516*5299Sws195443 "link\n"), msg(errtext, filename); 15170Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename); 15180Sstevel@tonic-gate (void) close(fd); 15190Sstevel@tonic-gate errno = EPERM; 15200Sstevel@tonic-gate return (-1); 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate /* 15230Sstevel@tonic-gate * Root might make a file, but non-root might 15240Sstevel@tonic-gate * need to open it. If the permissions let us 15250Sstevel@tonic-gate * get this far, then let it through. 15260Sstevel@tonic-gate */ 15270Sstevel@tonic-gate if (post_lstat.st_uid != getuid() && 15280Sstevel@tonic-gate post_lstat.st_uid != 0) { 1529*5299Sws195443 errtext = gettext("Unsupported " 1530*5299Sws195443 "condition detected: %s " 1531*5299Sws195443 "must be owned by uid %ld or 0\n"), 1532*5299Sws195443 msg(errtext, filename, (long)getuid()); 15330Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename, 15340Sstevel@tonic-gate (long)getuid()); 15350Sstevel@tonic-gate (void) close(fd); 15360Sstevel@tonic-gate errno = EPERM; 15370Sstevel@tonic-gate return (-1); 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate if (mode & O_TRUNC) { 15400Sstevel@tonic-gate if (ftruncate(fd, (off_t)0) < 0) { 15410Sstevel@tonic-gate msg("ftruncate(%s): %s\n", 15420Sstevel@tonic-gate filename, strerror(errno)); 15430Sstevel@tonic-gate (void) close(fd); 15440Sstevel@tonic-gate return (-1); 15450Sstevel@tonic-gate } 15460Sstevel@tonic-gate } 15470Sstevel@tonic-gate } else { 15480Sstevel@tonic-gate /* 15490Sstevel@tonic-gate * Didn't exist, but couldn't open it. 15500Sstevel@tonic-gate */ 15510Sstevel@tonic-gate return (-1); 15520Sstevel@tonic-gate } 15530Sstevel@tonic-gate } else { 15540Sstevel@tonic-gate /* 15550Sstevel@tonic-gate * If truncating open succeeded for a read-only open, 15560Sstevel@tonic-gate * bail out, as we really shouldn't have succeeded. 15570Sstevel@tonic-gate */ 15580Sstevel@tonic-gate if (mode & O_RDONLY) { 15590Sstevel@tonic-gate /* Undo the O_CREAT */ 15600Sstevel@tonic-gate (void) unlink(filename); 15610Sstevel@tonic-gate msg("open(%s): %s\n", 15620Sstevel@tonic-gate filename, strerror(ENOENT)); 15630Sstevel@tonic-gate (void) close(fd); 15640Sstevel@tonic-gate errno = ENOENT; 15650Sstevel@tonic-gate return (-1); 15660Sstevel@tonic-gate } 15670Sstevel@tonic-gate } 15680Sstevel@tonic-gate 15690Sstevel@tonic-gate return (fd); 15700Sstevel@tonic-gate } 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate /* 15730Sstevel@tonic-gate * Safely open a file. 15740Sstevel@tonic-gate */ 15750Sstevel@tonic-gate int 15760Sstevel@tonic-gate safe_file_open(const char *filename, int mode, int perms) 15770Sstevel@tonic-gate { 15780Sstevel@tonic-gate return (safe_open_common(filename, mode, perms, 0)); 15790Sstevel@tonic-gate } 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate /* 15820Sstevel@tonic-gate * Safely open a device. 15830Sstevel@tonic-gate */ 15840Sstevel@tonic-gate int 15850Sstevel@tonic-gate safe_device_open(const char *filename, int mode, int perms) 15860Sstevel@tonic-gate { 15870Sstevel@tonic-gate return (safe_open_common(filename, mode, perms, 1)); 15880Sstevel@tonic-gate } 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate /* 15910Sstevel@tonic-gate * STDIO version of safe_open 15920Sstevel@tonic-gate */ 15930Sstevel@tonic-gate FILE * 15940Sstevel@tonic-gate safe_fopen(const char *filename, const char *smode, int perms) 15950Sstevel@tonic-gate { 15960Sstevel@tonic-gate int fd; 15970Sstevel@tonic-gate int bmode; 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate /* 16000Sstevel@tonic-gate * accepts only modes "r", "r+", and "w" 16010Sstevel@tonic-gate */ 16020Sstevel@tonic-gate if (smode[0] == 'r') { 16030Sstevel@tonic-gate if (smode[1] == '\0') { 16040Sstevel@tonic-gate bmode = O_RDONLY; 16050Sstevel@tonic-gate } else if ((smode[1] == '+') && (smode[2] == '\0')) { 16060Sstevel@tonic-gate bmode = O_RDWR; 16070Sstevel@tonic-gate } 16080Sstevel@tonic-gate } else if ((smode[0] == 'w') && (smode[1] == '\0')) { 16090Sstevel@tonic-gate bmode = O_WRONLY; 16100Sstevel@tonic-gate } else { 16110Sstevel@tonic-gate msg(gettext("internal error: safe_fopen: invalid mode `%s'\n"), 16120Sstevel@tonic-gate smode); 16130Sstevel@tonic-gate return (NULL); 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate fd = safe_file_open(filename, bmode, perms); 16170Sstevel@tonic-gate 16180Sstevel@tonic-gate /* 16190Sstevel@tonic-gate * caller is expected to report error. 16200Sstevel@tonic-gate */ 16210Sstevel@tonic-gate if (fd >= 0) 1622*5299Sws195443 return (fdopen(fd, smode)); 16230Sstevel@tonic-gate 16240Sstevel@tonic-gate return ((FILE *)NULL); 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate void 16280Sstevel@tonic-gate child_chdir(void) 16290Sstevel@tonic-gate { 16300Sstevel@tonic-gate char name[MAXPATHLEN]; 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate if (debug_chdir != NULL) { 16330Sstevel@tonic-gate snprintf(name, sizeof (name), "%s/%ld", 16340Sstevel@tonic-gate debug_chdir, (long)getpid()); 16350Sstevel@tonic-gate if (mkdir(name, 0755) < 0) 16360Sstevel@tonic-gate msg("mkdir(%s): %s", name, strerror(errno)); 16370Sstevel@tonic-gate if (chdir(name) < 0) 16380Sstevel@tonic-gate msg("chdir(%s): %s", name, strerror(errno)); 16390Sstevel@tonic-gate } 16400Sstevel@tonic-gate } 1641