xref: /csrg-svn/sbin/dump/main.c (revision 12590)
1*12590Smckusick static	char *sccsid = "@(#)main.c	1.13 (Berkeley) 05/19/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;
29*12590Smckusick 	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));
1841423Sroot 	msg("Date of last level %c dump: %s\n", incno, prdate(spcl.c_ddate));
1851423Sroot 	msg("Dumping %s ", disk);
1861423Sroot 	if (dt != 0)
1871423Sroot 		msgtail("(%s) ", dt->fs_file);
1888506Smckusick #ifdef RDUMP
1898506Smckusick 	msgtail("to %s on host %s\n", tape, host);
1908506Smckusick #else
1918371Smckusick 	msgtail("to %s\n", tape);
1926882Ssam #endif
1931423Sroot 
1941423Sroot 	fi = open(disk, 0);
1951423Sroot 	if (fi < 0) {
1961423Sroot 		msg("Cannot open %s\n", disk);
1971423Sroot 		Exit(X_ABORT);
1981423Sroot 	}
1991423Sroot 	esize = 0;
2005328Smckusic 	sblock = (struct fs *)buf;
2015328Smckusic 	sync();
2025342Smckusic 	bread(SBLOCK, sblock, SBSIZE);
2035328Smckusic 	if (sblock->fs_magic != FS_MAGIC) {
2045328Smckusic 		msg("bad sblock magic number\n");
2055328Smckusic 		dumpabort();
2065328Smckusic 	}
2075328Smckusic 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
2085328Smckusic 		TP_BSIZE);
2095328Smckusic 	clrmap = (char *)calloc(msiz, sizeof(char));
2105328Smckusic 	dirmap = (char *)calloc(msiz, sizeof(char));
2115328Smckusic 	nodmap = (char *)calloc(msiz, sizeof(char));
2121423Sroot 
2131423Sroot 	msg("mapping (Pass I) [regular files]\n");
2145328Smckusic 	pass(mark, (char *)NULL);		/* mark updates esize */
2151423Sroot 
2161423Sroot 	do {
2171423Sroot 		msg("mapping (Pass II) [directories]\n");
2181423Sroot 		nadded = 0;
2191423Sroot 		pass(add, dirmap);
2201423Sroot 	} while(nadded);
2211423Sroot 
2221423Sroot 	bmapest(clrmap);
2231423Sroot 	bmapest(nodmap);
2241423Sroot 
22510910Ssam 	if (cartridge) {
22610910Ssam 		/* Estimate number of tapes, assuming streaming stops at
22710910Ssam 		   the end of each block written, and not in mid-block.
22810910Ssam 		   Assume no erroneous blocks; this can be compensated for
22910910Ssam 		   with an artificially low tape size. */
23010910Ssam 		fetapes =
2315328Smckusic 		(	  esize		/* blocks */
23210910Ssam 			* TP_BSIZE	/* bytes/block */
23310910Ssam 			* (1.0/density)	/* 0.1" / byte */
23410910Ssam 		  +
23510910Ssam 			  esize		/* blocks */
23610910Ssam 			* (1.0/ntrec)	/* streaming-stops per block */
23710910Ssam 			* 15.48		/* 0.1" / streaming-stop */
23810910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
23910910Ssam 	} else {
24010910Ssam 		/* Estimate number of tapes, for old fashioned 9-track tape */
24110910Ssam 		int tenthsperirg = (density == 625) ? 3 : 7;
24210910Ssam 		fetapes =
24310910Ssam 		(	  esize		/* blocks */
2445328Smckusic 			* TP_BSIZE	/* bytes / block */
2455328Smckusic 			* (1.0/density)	/* 0.1" / byte */
2461423Sroot 		  +
2475328Smckusic 			  esize		/* blocks */
24810910Ssam 			* (1.0/ntrec)	/* IRG's / block */
24910910Ssam 			* tenthsperirg	/* 0.1" / IRG */
2505328Smckusic 			* 7		/* 0.1" / IRG */
25110910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
25210910Ssam 	}
2531423Sroot 	etapes = fetapes;		/* truncating assignment */
2541423Sroot 	etapes++;
2555328Smckusic 	/* count the nodemap on each additional tape */
2565328Smckusic 	for (i = 1; i < etapes; i++)
2575328Smckusic 		bmapest(nodmap);
2585328Smckusic 	esize += i + 10;	/* headers + 10 trailer blocks */
2591423Sroot 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
2601423Sroot 
26110910Ssam 	alloctape();			/* Allocate tape buffer */
26210910Ssam 
2631423Sroot 	otape();			/* bitmap is the first to tape write */
2641423Sroot 	time(&(tstart_writing));
2651423Sroot 	bitmap(clrmap, TS_CLRI);
2661423Sroot 
2671423Sroot 	msg("dumping (Pass III) [directories]\n");
2681423Sroot 	pass(dump, dirmap);
2691423Sroot 
2701423Sroot 	msg("dumping (Pass IV) [regular files]\n");
2711423Sroot 	pass(dump, nodmap);
2721423Sroot 
2731423Sroot 	spcl.c_type = TS_END;
2746882Ssam #ifndef RDUMP
27510910Ssam 	for(i=0; i<ntrec; i++)
2761423Sroot 		spclrec();
2776882Ssam #endif
2781423Sroot 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
2791423Sroot 	msg("DUMP IS DONE\n");
2801423Sroot 
2811423Sroot 	putitime();
2826882Ssam #ifndef RDUMP
28312331Smckusick 	if (!pipeout) {
28412331Smckusick 		close(to);
28512331Smckusick 		rewind();
28612331Smckusick 	}
2876882Ssam #else
2886882Ssam 	tflush(1);
28912331Smckusick 	rewind();
2906882Ssam #endif
2911423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
2921423Sroot 	Exit(X_FINOK);
2931423Sroot }
2941423Sroot 
2951423Sroot int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
2961423Sroot int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
2971423Sroot int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
2981423Sroot int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
2991423Sroot int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
3001423Sroot int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
3011423Sroot int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
3021423Sroot 
3031423Sroot sigAbort()
3041423Sroot {
30512331Smckusick 	if (pipeout) {
30612331Smckusick 		msg("Unknown signal, cannot recover\n");
30712331Smckusick 		dumpabort();
30812331Smckusick 	}
3091423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
3101423Sroot 	fflush(stderr);
3111423Sroot 	fflush(stdout);
3121423Sroot 	close_rewind();
3131423Sroot 	exit(X_REWRITE);
3141423Sroot }
3151423Sroot 
3161423Sroot char *rawname(cp)
3171423Sroot 	char *cp;
3181423Sroot {
3191423Sroot 	static char rawbuf[32];
3204608Smckusic 	char *rindex();
3211423Sroot 	char *dp = rindex(cp, '/');
3221423Sroot 
3231423Sroot 	if (dp == 0)
3241423Sroot 		return (0);
3251423Sroot 	*dp = 0;
3261423Sroot 	strcpy(rawbuf, cp);
3271423Sroot 	*dp = '/';
3281423Sroot 	strcat(rawbuf, "/r");
3291423Sroot 	strcat(rawbuf, dp+1);
3301423Sroot 	return (rawbuf);
3311423Sroot }
332