xref: /csrg-svn/sbin/dump/main.c (revision 18494)
1*18494Smckusick static	char *sccsid = "@(#)main.c	1.18 (Berkeley) 03/24/85";
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;
19*18494Smckusick 	int		bflag = 0, 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;
68*18494Smckusick 			if (density >= 625 && !bflag)
69*18494Smckusick 				ntrec = HIGHDENSITYTREC;
701423Sroot 		}
711423Sroot 		break;
721423Sroot 
731423Sroot 	case 's':			/* tape size, feet */
741423Sroot 		if(argc > 1) {
751423Sroot 			argv++;
761423Sroot 			argc--;
771423Sroot 			tsize = atol(*argv);
781423Sroot 			tsize *= 12L*10L;
791423Sroot 		}
801423Sroot 		break;
811423Sroot 
8210910Ssam 	case 'b':			/* blocks per tape write */
8310910Ssam 		if(argc > 1) {
8410910Ssam 			argv++;
8510910Ssam 			argc--;
86*18494Smckusick 			bflag++;
8710910Ssam 			ntrec = atol(*argv);
8810910Ssam 		}
8910910Ssam 		break;
9010910Ssam 
9110910Ssam 	case 'c':			/* Tape is cart. not 9-track */
9210910Ssam 		cartridge++;
9310910Ssam 		break;
9410910Ssam 
951423Sroot 	case '0':			/* dump level */
961423Sroot 	case '1':
971423Sroot 	case '2':
981423Sroot 	case '3':
991423Sroot 	case '4':
1001423Sroot 	case '5':
1011423Sroot 	case '6':
1021423Sroot 	case '7':
1031423Sroot 	case '8':
1041423Sroot 	case '9':
1051423Sroot 		incno = arg[-1];
1061423Sroot 		break;
1071423Sroot 
1081423Sroot 	case 'u':			/* update /etc/dumpdates */
1091423Sroot 		uflag++;
1101423Sroot 		break;
1111423Sroot 
1121423Sroot 	case 'n':			/* notify operators */
1131423Sroot 		notify++;
1141423Sroot 		break;
1151423Sroot 
1161423Sroot 	default:
11712331Smckusick 		fprintf(stderr, "bad key '%c%'\n", arg[-1]);
1181423Sroot 		Exit(X_ABORT);
1191423Sroot 	}
1201423Sroot 	if(argc > 1) {
1211423Sroot 		argv++;
1221423Sroot 		argc--;
1231423Sroot 		disk = *argv;
1241423Sroot 	}
12512331Smckusick 	if (strcmp(tape, "-") == 0) {
12612331Smckusick 		pipeout++;
12712331Smckusick 		tape = "standard output";
12812331Smckusick 	}
12910910Ssam 
13010910Ssam 	/*
13110910Ssam 	 * Determine how to default tape size and density
13210910Ssam 	 *
13310910Ssam 	 *         	density				tape size
13410910Ssam 	 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
13510910Ssam 	 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
13616251Smckusick  	 * cartridge	8000 bpi (100 bytes/.1")	1700 ft. (450*4 - slop)
13710910Ssam 	 */
13810910Ssam 	if (density == 0)
13910910Ssam 		density = cartridge ? 100 : 160;
14010910Ssam 	if (tsize == 0)
14116251Smckusick  		tsize = cartridge ? 1700L*120L : 2300L*120L;
14210910Ssam 
1436886Ssam #ifdef RDUMP
1446886Ssam 	{ char *index();
1456886Ssam 	  host = tape;
1466886Ssam 	  tape = index(host, ':');
1476886Ssam 	  if (tape == 0) {
1486886Ssam 		msg("need keyletter ``f'' and device ``host:tape''");
1496886Ssam 		exit(1);
1506886Ssam 	  }
1516886Ssam 	  *tape++ = 0;
1526886Ssam 	  if (rmthost(host) == 0)
1536886Ssam 		exit(X_ABORT);
1546886Ssam 	}
1556886Ssam #endif
1561423Sroot 	if (signal(SIGHUP, sighup) == SIG_IGN)
1571423Sroot 		signal(SIGHUP, SIG_IGN);
1581423Sroot 	if (signal(SIGTRAP, sigtrap) == SIG_IGN)
1591423Sroot 		signal(SIGTRAP, SIG_IGN);
1601423Sroot 	if (signal(SIGFPE, sigfpe) == SIG_IGN)
1611423Sroot 		signal(SIGFPE, SIG_IGN);
1621423Sroot 	if (signal(SIGBUS, sigbus) == SIG_IGN)
1631423Sroot 		signal(SIGBUS, SIG_IGN);
1641423Sroot 	if (signal(SIGSEGV, sigsegv) == SIG_IGN)
1651423Sroot 		signal(SIGSEGV, SIG_IGN);
1661423Sroot 	if (signal(SIGTERM, sigterm) == SIG_IGN)
1671423Sroot 		signal(SIGTERM, SIG_IGN);
1681423Sroot 
1691423Sroot 
1701423Sroot 	if (signal(SIGINT, interrupt) == SIG_IGN)
1711423Sroot 		signal(SIGINT, SIG_IGN);
1721423Sroot 
1731423Sroot 	set_operators();	/* /etc/group snarfed */
1741423Sroot 	getfstab();		/* /etc/fstab snarfed */
1751423Sroot 	/*
1761423Sroot 	 *	disk can be either the full special file name,
1771423Sroot 	 *	the suffix of the special file name,
1781423Sroot 	 *	the special name missing the leading '/',
1791423Sroot 	 *	the file system name with or without the leading '/'.
1801423Sroot 	 */
1811423Sroot 	dt = fstabsearch(disk);
1821423Sroot 	if (dt != 0)
1831423Sroot 		disk = rawname(dt->fs_spec);
1841423Sroot 	getitime();		/* /etc/dumpdates snarfed */
1851423Sroot 
1861423Sroot 	msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
18712948Smckusick  	msg("Date of last level %c dump: %s\n",
18812948Smckusick 		lastincno, prdate(spcl.c_ddate));
1891423Sroot 	msg("Dumping %s ", disk);
1901423Sroot 	if (dt != 0)
1911423Sroot 		msgtail("(%s) ", dt->fs_file);
1928506Smckusick #ifdef RDUMP
1938506Smckusick 	msgtail("to %s on host %s\n", tape, host);
1948506Smckusick #else
1958371Smckusick 	msgtail("to %s\n", tape);
1966882Ssam #endif
1971423Sroot 
1981423Sroot 	fi = open(disk, 0);
1991423Sroot 	if (fi < 0) {
2001423Sroot 		msg("Cannot open %s\n", disk);
2011423Sroot 		Exit(X_ABORT);
2021423Sroot 	}
2031423Sroot 	esize = 0;
2045328Smckusic 	sblock = (struct fs *)buf;
2055328Smckusic 	sync();
2065342Smckusic 	bread(SBLOCK, sblock, SBSIZE);
2075328Smckusic 	if (sblock->fs_magic != FS_MAGIC) {
2085328Smckusic 		msg("bad sblock magic number\n");
2095328Smckusic 		dumpabort();
2105328Smckusic 	}
2115328Smckusic 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
2125328Smckusic 		TP_BSIZE);
2135328Smckusic 	clrmap = (char *)calloc(msiz, sizeof(char));
2145328Smckusic 	dirmap = (char *)calloc(msiz, sizeof(char));
2155328Smckusic 	nodmap = (char *)calloc(msiz, sizeof(char));
2161423Sroot 
2171423Sroot 	msg("mapping (Pass I) [regular files]\n");
2185328Smckusic 	pass(mark, (char *)NULL);		/* mark updates esize */
2191423Sroot 
2201423Sroot 	do {
2211423Sroot 		msg("mapping (Pass II) [directories]\n");
2221423Sroot 		nadded = 0;
2231423Sroot 		pass(add, dirmap);
2241423Sroot 	} while(nadded);
2251423Sroot 
2261423Sroot 	bmapest(clrmap);
2271423Sroot 	bmapest(nodmap);
2281423Sroot 
22910910Ssam 	if (cartridge) {
23010910Ssam 		/* Estimate number of tapes, assuming streaming stops at
23110910Ssam 		   the end of each block written, and not in mid-block.
23210910Ssam 		   Assume no erroneous blocks; this can be compensated for
23310910Ssam 		   with an artificially low tape size. */
23410910Ssam 		fetapes =
2355328Smckusic 		(	  esize		/* blocks */
23610910Ssam 			* TP_BSIZE	/* bytes/block */
23710910Ssam 			* (1.0/density)	/* 0.1" / byte */
23810910Ssam 		  +
23910910Ssam 			  esize		/* blocks */
24010910Ssam 			* (1.0/ntrec)	/* streaming-stops per block */
24110910Ssam 			* 15.48		/* 0.1" / streaming-stop */
24210910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
24310910Ssam 	} else {
24410910Ssam 		/* Estimate number of tapes, for old fashioned 9-track tape */
24510910Ssam 		int tenthsperirg = (density == 625) ? 3 : 7;
24610910Ssam 		fetapes =
24710910Ssam 		(	  esize		/* blocks */
2485328Smckusic 			* TP_BSIZE	/* bytes / block */
2495328Smckusic 			* (1.0/density)	/* 0.1" / byte */
2501423Sroot 		  +
2515328Smckusic 			  esize		/* blocks */
25210910Ssam 			* (1.0/ntrec)	/* IRG's / block */
25310910Ssam 			* tenthsperirg	/* 0.1" / IRG */
25410910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
25510910Ssam 	}
2561423Sroot 	etapes = fetapes;		/* truncating assignment */
2571423Sroot 	etapes++;
2585328Smckusic 	/* count the nodemap on each additional tape */
2595328Smckusic 	for (i = 1; i < etapes; i++)
2605328Smckusic 		bmapest(nodmap);
2615328Smckusic 	esize += i + 10;	/* headers + 10 trailer blocks */
2621423Sroot 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
2631423Sroot 
26410910Ssam 	alloctape();			/* Allocate tape buffer */
26510910Ssam 
2661423Sroot 	otape();			/* bitmap is the first to tape write */
2671423Sroot 	time(&(tstart_writing));
2681423Sroot 	bitmap(clrmap, TS_CLRI);
2691423Sroot 
2701423Sroot 	msg("dumping (Pass III) [directories]\n");
27117234Smckusick  	pass(dirdump, dirmap);
2721423Sroot 
2731423Sroot 	msg("dumping (Pass IV) [regular files]\n");
2741423Sroot 	pass(dump, nodmap);
2751423Sroot 
2761423Sroot 	spcl.c_type = TS_END;
2776882Ssam #ifndef RDUMP
27810910Ssam 	for(i=0; i<ntrec; i++)
2791423Sroot 		spclrec();
2806882Ssam #endif
2811423Sroot 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
2821423Sroot 	msg("DUMP IS DONE\n");
2831423Sroot 
2841423Sroot 	putitime();
2856882Ssam #ifndef RDUMP
28612331Smckusick 	if (!pipeout) {
28712331Smckusick 		close(to);
28812331Smckusick 		rewind();
28912331Smckusick 	}
2906882Ssam #else
2916882Ssam 	tflush(1);
29212331Smckusick 	rewind();
2936882Ssam #endif
2941423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
2951423Sroot 	Exit(X_FINOK);
2961423Sroot }
2971423Sroot 
2981423Sroot int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
2991423Sroot int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
3001423Sroot int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
3011423Sroot int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
3021423Sroot int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
3031423Sroot int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
3041423Sroot int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
3051423Sroot 
3061423Sroot sigAbort()
3071423Sroot {
30812331Smckusick 	if (pipeout) {
30912331Smckusick 		msg("Unknown signal, cannot recover\n");
31012331Smckusick 		dumpabort();
31112331Smckusick 	}
3121423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
3131423Sroot 	fflush(stderr);
3141423Sroot 	fflush(stdout);
3151423Sroot 	close_rewind();
3161423Sroot 	exit(X_REWRITE);
3171423Sroot }
3181423Sroot 
3191423Sroot char *rawname(cp)
3201423Sroot 	char *cp;
3211423Sroot {
3221423Sroot 	static char rawbuf[32];
3234608Smckusic 	char *rindex();
3241423Sroot 	char *dp = rindex(cp, '/');
3251423Sroot 
3261423Sroot 	if (dp == 0)
3271423Sroot 		return (0);
3281423Sroot 	*dp = 0;
3291423Sroot 	strcpy(rawbuf, cp);
3301423Sroot 	*dp = '/';
3311423Sroot 	strcat(rawbuf, "/r");
3321423Sroot 	strcat(rawbuf, dp+1);
3331423Sroot 	return (rawbuf);
3341423Sroot }
335