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