1*12948Smckusick static char *sccsid = "@(#)main.c 1.14 (Berkeley) 06/09/83"; 21423Sroot #include "dump.h" 31423Sroot 41423Sroot int notify = 0; /* notify operator flag */ 51423Sroot int blockswritten = 0; /* number of blocks written on current tape */ 61423Sroot int tapeno = 0; /* current tape number */ 710910Ssam int density = 0; /* density in bytes/0.1" */ 810910Ssam int ntrec = NTREC; /* # tape blocks in each tape record */ 910910Ssam int cartridge = 0; /* Assume non-cartridge tape */ 106882Ssam #ifdef RDUMP 116882Ssam char *host; 126882Ssam #endif 131423Sroot 141423Sroot main(argc, argv) 151423Sroot int argc; 161423Sroot char *argv[]; 171423Sroot { 181423Sroot char *arg; 195328Smckusic int i; 201423Sroot float fetapes; 211423Sroot register struct fstab *dt; 221423Sroot 231423Sroot time(&(spcl.c_date)); 241423Sroot 2510910Ssam tsize = 0; /* Default later, based on 'c' option for cart tapes */ 261423Sroot tape = TAPE; 271423Sroot disk = DISK; 281423Sroot increm = NINCREM; 2912590Smckusick temp = TEMP; 305328Smckusic if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) { 315328Smckusic msg("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 325328Smckusic dumpabort(); 335328Smckusic } 341423Sroot incno = '9'; 351423Sroot uflag = 0; 361423Sroot arg = "u"; 371423Sroot if(argc > 1) { 381423Sroot argv++; 391423Sroot argc--; 401423Sroot arg = *argv; 411423Sroot if (*arg == '-') 421423Sroot argc++; 431423Sroot } 441423Sroot while(*arg) 451423Sroot switch (*arg++) { 461463Sroot case 'w': 471463Sroot lastdump('w'); /* tell us only what has to be done */ 481463Sroot exit(0); 491463Sroot break; 501423Sroot case 'W': /* what to do */ 511463Sroot lastdump('W'); /* tell us the current state of what has been done */ 521423Sroot exit(0); /* do nothing else */ 531423Sroot break; 541423Sroot 551423Sroot case 'f': /* output file */ 561423Sroot if(argc > 1) { 571423Sroot argv++; 581423Sroot argc--; 591423Sroot tape = *argv; 601423Sroot } 611423Sroot break; 621423Sroot 631423Sroot case 'd': /* density, in bits per inch */ 641423Sroot if (argc > 1) { 651423Sroot argv++; 661423Sroot argc--; 671423Sroot density = atoi(*argv) / 10; 681423Sroot } 691423Sroot break; 701423Sroot 711423Sroot case 's': /* tape size, feet */ 721423Sroot if(argc > 1) { 731423Sroot argv++; 741423Sroot argc--; 751423Sroot tsize = atol(*argv); 761423Sroot tsize *= 12L*10L; 771423Sroot } 781423Sroot break; 791423Sroot 8010910Ssam case 'b': /* blocks per tape write */ 8110910Ssam if(argc > 1) { 8210910Ssam argv++; 8310910Ssam argc--; 8410910Ssam ntrec = atol(*argv); 8510910Ssam } 8610910Ssam break; 8710910Ssam 8810910Ssam case 'c': /* Tape is cart. not 9-track */ 8910910Ssam cartridge++; 9010910Ssam break; 9110910Ssam 921423Sroot case '0': /* dump level */ 931423Sroot case '1': 941423Sroot case '2': 951423Sroot case '3': 961423Sroot case '4': 971423Sroot case '5': 981423Sroot case '6': 991423Sroot case '7': 1001423Sroot case '8': 1011423Sroot case '9': 1021423Sroot incno = arg[-1]; 1031423Sroot break; 1041423Sroot 1051423Sroot case 'u': /* update /etc/dumpdates */ 1061423Sroot uflag++; 1071423Sroot break; 1081423Sroot 1091423Sroot case 'n': /* notify operators */ 1101423Sroot notify++; 1111423Sroot break; 1121423Sroot 1131423Sroot default: 11412331Smckusick fprintf(stderr, "bad key '%c%'\n", arg[-1]); 1151423Sroot Exit(X_ABORT); 1161423Sroot } 1171423Sroot if(argc > 1) { 1181423Sroot argv++; 1191423Sroot argc--; 1201423Sroot disk = *argv; 1211423Sroot } 12212331Smckusick if (strcmp(tape, "-") == 0) { 12312331Smckusick pipeout++; 12412331Smckusick tape = "standard output"; 12512331Smckusick } 12610910Ssam 12710910Ssam /* 12810910Ssam * Determine how to default tape size and density 12910910Ssam * 13010910Ssam * density tape size 13110910Ssam * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 13210910Ssam * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 13310910Ssam * cartridge 8000 bpi (100 bytes/.1") 4000 ft. (450*9 - slop) 13410910Ssam */ 13510910Ssam if (density == 0) 13610910Ssam density = cartridge ? 100 : 160; 13710910Ssam if (tsize == 0) 13810910Ssam tsize = cartridge ? 4000L*120L : 2300L*120L; 13910910Ssam 1406886Ssam #ifdef RDUMP 1416886Ssam { char *index(); 1426886Ssam host = tape; 1436886Ssam tape = index(host, ':'); 1446886Ssam if (tape == 0) { 1456886Ssam msg("need keyletter ``f'' and device ``host:tape''"); 1466886Ssam exit(1); 1476886Ssam } 1486886Ssam *tape++ = 0; 1496886Ssam if (rmthost(host) == 0) 1506886Ssam exit(X_ABORT); 1516886Ssam } 1526886Ssam #endif 1531423Sroot if (signal(SIGHUP, sighup) == SIG_IGN) 1541423Sroot signal(SIGHUP, SIG_IGN); 1551423Sroot if (signal(SIGTRAP, sigtrap) == SIG_IGN) 1561423Sroot signal(SIGTRAP, SIG_IGN); 1571423Sroot if (signal(SIGFPE, sigfpe) == SIG_IGN) 1581423Sroot signal(SIGFPE, SIG_IGN); 1591423Sroot if (signal(SIGBUS, sigbus) == SIG_IGN) 1601423Sroot signal(SIGBUS, SIG_IGN); 1611423Sroot if (signal(SIGSEGV, sigsegv) == SIG_IGN) 1621423Sroot signal(SIGSEGV, SIG_IGN); 1631423Sroot if (signal(SIGTERM, sigterm) == SIG_IGN) 1641423Sroot signal(SIGTERM, SIG_IGN); 1651423Sroot 1661423Sroot 1671423Sroot if (signal(SIGINT, interrupt) == SIG_IGN) 1681423Sroot signal(SIGINT, SIG_IGN); 1691423Sroot 1701423Sroot set_operators(); /* /etc/group snarfed */ 1711423Sroot getfstab(); /* /etc/fstab snarfed */ 1721423Sroot /* 1731423Sroot * disk can be either the full special file name, 1741423Sroot * the suffix of the special file name, 1751423Sroot * the special name missing the leading '/', 1761423Sroot * the file system name with or without the leading '/'. 1771423Sroot */ 1781423Sroot dt = fstabsearch(disk); 1791423Sroot if (dt != 0) 1801423Sroot disk = rawname(dt->fs_spec); 1811423Sroot getitime(); /* /etc/dumpdates snarfed */ 1821423Sroot 1831423Sroot msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date)); 184*12948Smckusick msg("Date of last level %c dump: %s\n", 185*12948Smckusick lastincno, prdate(spcl.c_ddate)); 1861423Sroot msg("Dumping %s ", disk); 1871423Sroot if (dt != 0) 1881423Sroot msgtail("(%s) ", dt->fs_file); 1898506Smckusick #ifdef RDUMP 1908506Smckusick msgtail("to %s on host %s\n", tape, host); 1918506Smckusick #else 1928371Smckusick msgtail("to %s\n", tape); 1936882Ssam #endif 1941423Sroot 1951423Sroot fi = open(disk, 0); 1961423Sroot if (fi < 0) { 1971423Sroot msg("Cannot open %s\n", disk); 1981423Sroot Exit(X_ABORT); 1991423Sroot } 2001423Sroot esize = 0; 2015328Smckusic sblock = (struct fs *)buf; 2025328Smckusic sync(); 2035342Smckusic bread(SBLOCK, sblock, SBSIZE); 2045328Smckusic if (sblock->fs_magic != FS_MAGIC) { 2055328Smckusic msg("bad sblock magic number\n"); 2065328Smckusic dumpabort(); 2075328Smckusic } 2085328Smckusic msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY), 2095328Smckusic TP_BSIZE); 2105328Smckusic clrmap = (char *)calloc(msiz, sizeof(char)); 2115328Smckusic dirmap = (char *)calloc(msiz, sizeof(char)); 2125328Smckusic nodmap = (char *)calloc(msiz, sizeof(char)); 2131423Sroot 2141423Sroot msg("mapping (Pass I) [regular files]\n"); 2155328Smckusic pass(mark, (char *)NULL); /* mark updates esize */ 2161423Sroot 2171423Sroot do { 2181423Sroot msg("mapping (Pass II) [directories]\n"); 2191423Sroot nadded = 0; 2201423Sroot pass(add, dirmap); 2211423Sroot } while(nadded); 2221423Sroot 2231423Sroot bmapest(clrmap); 2241423Sroot bmapest(nodmap); 2251423Sroot 22610910Ssam if (cartridge) { 22710910Ssam /* Estimate number of tapes, assuming streaming stops at 22810910Ssam the end of each block written, and not in mid-block. 22910910Ssam Assume no erroneous blocks; this can be compensated for 23010910Ssam with an artificially low tape size. */ 23110910Ssam fetapes = 2325328Smckusic ( esize /* blocks */ 23310910Ssam * TP_BSIZE /* bytes/block */ 23410910Ssam * (1.0/density) /* 0.1" / byte */ 23510910Ssam + 23610910Ssam esize /* blocks */ 23710910Ssam * (1.0/ntrec) /* streaming-stops per block */ 23810910Ssam * 15.48 /* 0.1" / streaming-stop */ 23910910Ssam ) * (1.0 / tsize ); /* tape / 0.1" */ 24010910Ssam } else { 24110910Ssam /* Estimate number of tapes, for old fashioned 9-track tape */ 24210910Ssam int tenthsperirg = (density == 625) ? 3 : 7; 24310910Ssam fetapes = 24410910Ssam ( esize /* blocks */ 2455328Smckusic * TP_BSIZE /* bytes / block */ 2465328Smckusic * (1.0/density) /* 0.1" / byte */ 2471423Sroot + 2485328Smckusic esize /* blocks */ 24910910Ssam * (1.0/ntrec) /* IRG's / block */ 25010910Ssam * tenthsperirg /* 0.1" / IRG */ 2515328Smckusic * 7 /* 0.1" / IRG */ 25210910Ssam ) * (1.0 / tsize ); /* tape / 0.1" */ 25310910Ssam } 2541423Sroot etapes = fetapes; /* truncating assignment */ 2551423Sroot etapes++; 2565328Smckusic /* count the nodemap on each additional tape */ 2575328Smckusic for (i = 1; i < etapes; i++) 2585328Smckusic bmapest(nodmap); 2595328Smckusic esize += i + 10; /* headers + 10 trailer blocks */ 2601423Sroot msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes); 2611423Sroot 26210910Ssam alloctape(); /* Allocate tape buffer */ 26310910Ssam 2641423Sroot otape(); /* bitmap is the first to tape write */ 2651423Sroot time(&(tstart_writing)); 2661423Sroot bitmap(clrmap, TS_CLRI); 2671423Sroot 2681423Sroot msg("dumping (Pass III) [directories]\n"); 2691423Sroot pass(dump, dirmap); 2701423Sroot 2711423Sroot msg("dumping (Pass IV) [regular files]\n"); 2721423Sroot pass(dump, nodmap); 2731423Sroot 2741423Sroot spcl.c_type = TS_END; 2756882Ssam #ifndef RDUMP 27610910Ssam for(i=0; i<ntrec; i++) 2771423Sroot spclrec(); 2786882Ssam #endif 2791423Sroot msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume); 2801423Sroot msg("DUMP IS DONE\n"); 2811423Sroot 2821423Sroot putitime(); 2836882Ssam #ifndef RDUMP 28412331Smckusick if (!pipeout) { 28512331Smckusick close(to); 28612331Smckusick rewind(); 28712331Smckusick } 2886882Ssam #else 2896882Ssam tflush(1); 29012331Smckusick rewind(); 2916882Ssam #endif 2921423Sroot broadcast("DUMP IS DONE!\7\7\n"); 2931423Sroot Exit(X_FINOK); 2941423Sroot } 2951423Sroot 2961423Sroot int sighup(){ msg("SIGHUP() try rewriting\n"); sigAbort();} 2971423Sroot int sigtrap(){ msg("SIGTRAP() try rewriting\n"); sigAbort();} 2981423Sroot int sigfpe(){ msg("SIGFPE() try rewriting\n"); sigAbort();} 2991423Sroot int sigbus(){ msg("SIGBUS() try rewriting\n"); sigAbort();} 3001423Sroot int sigsegv(){ msg("SIGSEGV() ABORTING!\n"); abort();} 3011423Sroot int sigalrm(){ msg("SIGALRM() try rewriting\n"); sigAbort();} 3021423Sroot int sigterm(){ msg("SIGTERM() try rewriting\n"); sigAbort();} 3031423Sroot 3041423Sroot sigAbort() 3051423Sroot { 30612331Smckusick if (pipeout) { 30712331Smckusick msg("Unknown signal, cannot recover\n"); 30812331Smckusick dumpabort(); 30912331Smckusick } 3101423Sroot msg("Rewriting attempted as response to unknown signal.\n"); 3111423Sroot fflush(stderr); 3121423Sroot fflush(stdout); 3131423Sroot close_rewind(); 3141423Sroot exit(X_REWRITE); 3151423Sroot } 3161423Sroot 3171423Sroot char *rawname(cp) 3181423Sroot char *cp; 3191423Sroot { 3201423Sroot static char rawbuf[32]; 3214608Smckusic char *rindex(); 3221423Sroot char *dp = rindex(cp, '/'); 3231423Sroot 3241423Sroot if (dp == 0) 3251423Sroot return (0); 3261423Sroot *dp = 0; 3271423Sroot strcpy(rawbuf, cp); 3281423Sroot *dp = '/'; 3291423Sroot strcat(rawbuf, "/r"); 3301423Sroot strcat(rawbuf, dp+1); 3311423Sroot return (rawbuf); 3321423Sroot } 333