122037Sdist /* 222037Sdist * Copyright (c) 1980 Regents of the University of California. 322037Sdist * All rights reserved. The Berkeley software License Agreement 422037Sdist * specifies the terms and conditions for redistribution. 522037Sdist */ 622037Sdist 722037Sdist #ifndef lint 8*28642Smckusick static char sccsid[] = "@(#)main.c 5.3 (Berkeley) 05/23/86"; 922037Sdist #endif not lint 1022037Sdist 111423Sroot #include "dump.h" 121423Sroot 131423Sroot int notify = 0; /* notify operator flag */ 141423Sroot int blockswritten = 0; /* number of blocks written on current tape */ 151423Sroot int tapeno = 0; /* current tape number */ 1610910Ssam int density = 0; /* density in bytes/0.1" */ 1710910Ssam int ntrec = NTREC; /* # tape blocks in each tape record */ 1810910Ssam int cartridge = 0; /* Assume non-cartridge tape */ 196882Ssam #ifdef RDUMP 206882Ssam char *host; 216882Ssam #endif 2225797Smckusick int anydskipped; /* set true in mark() if any directories are skipped */ 2325797Smckusick /* this lets us avoid map pass 2 in some cases */ 241423Sroot 251423Sroot main(argc, argv) 261423Sroot int argc; 271423Sroot char *argv[]; 281423Sroot { 291423Sroot char *arg; 3018494Smckusick int bflag = 0, i; 311423Sroot float fetapes; 321423Sroot register struct fstab *dt; 331423Sroot 341423Sroot time(&(spcl.c_date)); 351423Sroot 3610910Ssam tsize = 0; /* Default later, based on 'c' option for cart tapes */ 371423Sroot tape = TAPE; 381423Sroot disk = DISK; 391423Sroot increm = NINCREM; 4012590Smckusick temp = TEMP; 415328Smckusic if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) { 425328Smckusic msg("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 435328Smckusic dumpabort(); 445328Smckusic } 451423Sroot incno = '9'; 461423Sroot uflag = 0; 471423Sroot arg = "u"; 481423Sroot if(argc > 1) { 491423Sroot argv++; 501423Sroot argc--; 511423Sroot arg = *argv; 521423Sroot if (*arg == '-') 531423Sroot argc++; 541423Sroot } 551423Sroot while(*arg) 561423Sroot switch (*arg++) { 571463Sroot case 'w': 581463Sroot lastdump('w'); /* tell us only what has to be done */ 591463Sroot exit(0); 601463Sroot break; 611423Sroot case 'W': /* what to do */ 621463Sroot lastdump('W'); /* tell us the current state of what has been done */ 631423Sroot exit(0); /* do nothing else */ 641423Sroot break; 651423Sroot 661423Sroot case 'f': /* output file */ 671423Sroot if(argc > 1) { 681423Sroot argv++; 691423Sroot argc--; 701423Sroot tape = *argv; 711423Sroot } 721423Sroot break; 731423Sroot 741423Sroot case 'd': /* density, in bits per inch */ 751423Sroot if (argc > 1) { 761423Sroot argv++; 771423Sroot argc--; 781423Sroot density = atoi(*argv) / 10; 7918494Smckusick if (density >= 625 && !bflag) 8018494Smckusick ntrec = HIGHDENSITYTREC; 811423Sroot } 821423Sroot break; 831423Sroot 841423Sroot case 's': /* tape size, feet */ 851423Sroot if(argc > 1) { 861423Sroot argv++; 871423Sroot argc--; 881423Sroot tsize = atol(*argv); 891423Sroot tsize *= 12L*10L; 901423Sroot } 911423Sroot break; 921423Sroot 9310910Ssam case 'b': /* blocks per tape write */ 9410910Ssam if(argc > 1) { 9510910Ssam argv++; 9610910Ssam argc--; 9718494Smckusick bflag++; 9810910Ssam ntrec = atol(*argv); 9910910Ssam } 10010910Ssam break; 10110910Ssam 10210910Ssam case 'c': /* Tape is cart. not 9-track */ 10310910Ssam cartridge++; 10410910Ssam break; 10510910Ssam 1061423Sroot case '0': /* dump level */ 1071423Sroot case '1': 1081423Sroot case '2': 1091423Sroot case '3': 1101423Sroot case '4': 1111423Sroot case '5': 1121423Sroot case '6': 1131423Sroot case '7': 1141423Sroot case '8': 1151423Sroot case '9': 1161423Sroot incno = arg[-1]; 1171423Sroot break; 1181423Sroot 1191423Sroot case 'u': /* update /etc/dumpdates */ 1201423Sroot uflag++; 1211423Sroot break; 1221423Sroot 1231423Sroot case 'n': /* notify operators */ 1241423Sroot notify++; 1251423Sroot break; 1261423Sroot 1271423Sroot default: 12812331Smckusick fprintf(stderr, "bad key '%c%'\n", arg[-1]); 1291423Sroot Exit(X_ABORT); 1301423Sroot } 1311423Sroot if(argc > 1) { 1321423Sroot argv++; 1331423Sroot argc--; 1341423Sroot disk = *argv; 1351423Sroot } 13612331Smckusick if (strcmp(tape, "-") == 0) { 13712331Smckusick pipeout++; 13812331Smckusick tape = "standard output"; 13912331Smckusick } 14010910Ssam 14110910Ssam /* 14210910Ssam * Determine how to default tape size and density 14310910Ssam * 14410910Ssam * density tape size 14510910Ssam * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 14610910Ssam * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 14716251Smckusick * cartridge 8000 bpi (100 bytes/.1") 1700 ft. (450*4 - slop) 14810910Ssam */ 14910910Ssam if (density == 0) 15010910Ssam density = cartridge ? 100 : 160; 15110910Ssam if (tsize == 0) 15216251Smckusick tsize = cartridge ? 1700L*120L : 2300L*120L; 15310910Ssam 1546886Ssam #ifdef RDUMP 1556886Ssam { char *index(); 1566886Ssam host = tape; 1576886Ssam tape = index(host, ':'); 1586886Ssam if (tape == 0) { 159*28642Smckusick msg("need keyletter ``f'' and device ``host:tape''\n"); 1606886Ssam exit(1); 1616886Ssam } 1626886Ssam *tape++ = 0; 1636886Ssam if (rmthost(host) == 0) 1646886Ssam exit(X_ABORT); 1656886Ssam } 1666886Ssam #endif 1671423Sroot if (signal(SIGHUP, sighup) == SIG_IGN) 1681423Sroot signal(SIGHUP, SIG_IGN); 1691423Sroot if (signal(SIGTRAP, sigtrap) == SIG_IGN) 1701423Sroot signal(SIGTRAP, SIG_IGN); 1711423Sroot if (signal(SIGFPE, sigfpe) == SIG_IGN) 1721423Sroot signal(SIGFPE, SIG_IGN); 1731423Sroot if (signal(SIGBUS, sigbus) == SIG_IGN) 1741423Sroot signal(SIGBUS, SIG_IGN); 1751423Sroot if (signal(SIGSEGV, sigsegv) == SIG_IGN) 1761423Sroot signal(SIGSEGV, SIG_IGN); 1771423Sroot if (signal(SIGTERM, sigterm) == SIG_IGN) 1781423Sroot signal(SIGTERM, SIG_IGN); 1791423Sroot 1801423Sroot 1811423Sroot if (signal(SIGINT, interrupt) == SIG_IGN) 1821423Sroot signal(SIGINT, SIG_IGN); 1831423Sroot 1841423Sroot set_operators(); /* /etc/group snarfed */ 1851423Sroot getfstab(); /* /etc/fstab snarfed */ 1861423Sroot /* 1871423Sroot * disk can be either the full special file name, 1881423Sroot * the suffix of the special file name, 1891423Sroot * the special name missing the leading '/', 1901423Sroot * the file system name with or without the leading '/'. 1911423Sroot */ 1921423Sroot dt = fstabsearch(disk); 1931423Sroot if (dt != 0) 1941423Sroot disk = rawname(dt->fs_spec); 1951423Sroot getitime(); /* /etc/dumpdates snarfed */ 1961423Sroot 1971423Sroot msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date)); 19812948Smckusick msg("Date of last level %c dump: %s\n", 19912948Smckusick lastincno, prdate(spcl.c_ddate)); 2001423Sroot msg("Dumping %s ", disk); 2011423Sroot if (dt != 0) 2021423Sroot msgtail("(%s) ", dt->fs_file); 2038506Smckusick #ifdef RDUMP 2048506Smckusick msgtail("to %s on host %s\n", tape, host); 2058506Smckusick #else 2068371Smckusick msgtail("to %s\n", tape); 2076882Ssam #endif 2081423Sroot 2091423Sroot fi = open(disk, 0); 2101423Sroot if (fi < 0) { 2111423Sroot msg("Cannot open %s\n", disk); 2121423Sroot Exit(X_ABORT); 2131423Sroot } 2141423Sroot esize = 0; 2155328Smckusic sblock = (struct fs *)buf; 2165328Smckusic sync(); 2175342Smckusic bread(SBLOCK, sblock, SBSIZE); 2185328Smckusic if (sblock->fs_magic != FS_MAGIC) { 2195328Smckusic msg("bad sblock magic number\n"); 2205328Smckusic dumpabort(); 2215328Smckusic } 2225328Smckusic msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY), 2235328Smckusic TP_BSIZE); 2245328Smckusic clrmap = (char *)calloc(msiz, sizeof(char)); 2255328Smckusic dirmap = (char *)calloc(msiz, sizeof(char)); 2265328Smckusic nodmap = (char *)calloc(msiz, sizeof(char)); 2271423Sroot 22825797Smckusick anydskipped = 0; 2291423Sroot msg("mapping (Pass I) [regular files]\n"); 2305328Smckusic pass(mark, (char *)NULL); /* mark updates esize */ 2311423Sroot 23225797Smckusick if (anydskipped) { 23325797Smckusick do { 23425797Smckusick msg("mapping (Pass II) [directories]\n"); 23525797Smckusick nadded = 0; 23625797Smckusick pass(add, dirmap); 23725797Smckusick } while(nadded); 23825797Smckusick } else /* keep the operators happy */ 2391423Sroot msg("mapping (Pass II) [directories]\n"); 2401423Sroot 2411423Sroot bmapest(clrmap); 2421423Sroot bmapest(nodmap); 2431423Sroot 24410910Ssam if (cartridge) { 24510910Ssam /* Estimate number of tapes, assuming streaming stops at 24610910Ssam the end of each block written, and not in mid-block. 24710910Ssam Assume no erroneous blocks; this can be compensated for 24810910Ssam with an artificially low tape size. */ 24910910Ssam fetapes = 2505328Smckusic ( esize /* blocks */ 25110910Ssam * TP_BSIZE /* bytes/block */ 25210910Ssam * (1.0/density) /* 0.1" / byte */ 25310910Ssam + 25410910Ssam esize /* blocks */ 25510910Ssam * (1.0/ntrec) /* streaming-stops per block */ 25610910Ssam * 15.48 /* 0.1" / streaming-stop */ 25710910Ssam ) * (1.0 / tsize ); /* tape / 0.1" */ 25810910Ssam } else { 25910910Ssam /* Estimate number of tapes, for old fashioned 9-track tape */ 26010910Ssam int tenthsperirg = (density == 625) ? 3 : 7; 26110910Ssam fetapes = 26210910Ssam ( esize /* blocks */ 2635328Smckusic * TP_BSIZE /* bytes / block */ 2645328Smckusic * (1.0/density) /* 0.1" / byte */ 2651423Sroot + 2665328Smckusic esize /* blocks */ 26710910Ssam * (1.0/ntrec) /* IRG's / block */ 26810910Ssam * tenthsperirg /* 0.1" / IRG */ 26910910Ssam ) * (1.0 / tsize ); /* tape / 0.1" */ 27010910Ssam } 2711423Sroot etapes = fetapes; /* truncating assignment */ 2721423Sroot etapes++; 2735328Smckusic /* count the nodemap on each additional tape */ 2745328Smckusic for (i = 1; i < etapes; i++) 2755328Smckusic bmapest(nodmap); 2765328Smckusic esize += i + 10; /* headers + 10 trailer blocks */ 2771423Sroot msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes); 2781423Sroot 27910910Ssam alloctape(); /* Allocate tape buffer */ 28010910Ssam 2811423Sroot otape(); /* bitmap is the first to tape write */ 2821423Sroot time(&(tstart_writing)); 2831423Sroot bitmap(clrmap, TS_CLRI); 2841423Sroot 2851423Sroot msg("dumping (Pass III) [directories]\n"); 28617234Smckusick pass(dirdump, dirmap); 2871423Sroot 2881423Sroot msg("dumping (Pass IV) [regular files]\n"); 2891423Sroot pass(dump, nodmap); 2901423Sroot 2911423Sroot spcl.c_type = TS_END; 2926882Ssam #ifndef RDUMP 29310910Ssam for(i=0; i<ntrec; i++) 2941423Sroot spclrec(); 2956882Ssam #endif 2961423Sroot msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume); 2971423Sroot msg("DUMP IS DONE\n"); 2981423Sroot 2991423Sroot putitime(); 3006882Ssam #ifndef RDUMP 30112331Smckusick if (!pipeout) { 30212331Smckusick close(to); 30312331Smckusick rewind(); 30412331Smckusick } 3056882Ssam #else 3066882Ssam tflush(1); 30712331Smckusick rewind(); 3086882Ssam #endif 3091423Sroot broadcast("DUMP IS DONE!\7\7\n"); 3101423Sroot Exit(X_FINOK); 3111423Sroot } 3121423Sroot 3131423Sroot int sighup(){ msg("SIGHUP() try rewriting\n"); sigAbort();} 3141423Sroot int sigtrap(){ msg("SIGTRAP() try rewriting\n"); sigAbort();} 3151423Sroot int sigfpe(){ msg("SIGFPE() try rewriting\n"); sigAbort();} 3161423Sroot int sigbus(){ msg("SIGBUS() try rewriting\n"); sigAbort();} 3171423Sroot int sigsegv(){ msg("SIGSEGV() ABORTING!\n"); abort();} 3181423Sroot int sigalrm(){ msg("SIGALRM() try rewriting\n"); sigAbort();} 3191423Sroot int sigterm(){ msg("SIGTERM() try rewriting\n"); sigAbort();} 3201423Sroot 3211423Sroot sigAbort() 3221423Sroot { 32312331Smckusick if (pipeout) { 32412331Smckusick msg("Unknown signal, cannot recover\n"); 32512331Smckusick dumpabort(); 32612331Smckusick } 3271423Sroot msg("Rewriting attempted as response to unknown signal.\n"); 3281423Sroot fflush(stderr); 3291423Sroot fflush(stdout); 3301423Sroot close_rewind(); 3311423Sroot exit(X_REWRITE); 3321423Sroot } 3331423Sroot 3341423Sroot char *rawname(cp) 3351423Sroot char *cp; 3361423Sroot { 3371423Sroot static char rawbuf[32]; 3384608Smckusic char *rindex(); 3391423Sroot char *dp = rindex(cp, '/'); 3401423Sroot 3411423Sroot if (dp == 0) 3421423Sroot return (0); 3431423Sroot *dp = 0; 3441423Sroot strcpy(rawbuf, cp); 3451423Sroot *dp = '/'; 3461423Sroot strcat(rawbuf, "/r"); 3471423Sroot strcat(rawbuf, dp+1); 3481423Sroot return (rawbuf); 3491423Sroot } 350