xref: /csrg-svn/sbin/dump/main.c (revision 22037)
1*22037Sdist /*
2*22037Sdist  * Copyright (c) 1980 Regents of the University of California.
3*22037Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22037Sdist  * specifies the terms and conditions for redistribution.
5*22037Sdist  */
6*22037Sdist 
7*22037Sdist #ifndef lint
8*22037Sdist static char sccsid[] = "@(#)main.c	5.1 (Berkeley) 06/05/85";
9*22037Sdist #endif not lint
10*22037Sdist 
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
221423Sroot 
231423Sroot main(argc, argv)
241423Sroot 	int	argc;
251423Sroot 	char	*argv[];
261423Sroot {
271423Sroot 	char		*arg;
2818494Smckusick 	int		bflag = 0, i;
291423Sroot 	float		fetapes;
301423Sroot 	register	struct	fstab	*dt;
311423Sroot 
321423Sroot 	time(&(spcl.c_date));
331423Sroot 
3410910Ssam 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
351423Sroot 	tape = TAPE;
361423Sroot 	disk = DISK;
371423Sroot 	increm = NINCREM;
3812590Smckusick 	temp = TEMP;
395328Smckusic 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) {
405328Smckusic 		msg("TP_BSIZE must be a multiple of DEV_BSIZE\n");
415328Smckusic 		dumpabort();
425328Smckusic 	}
431423Sroot 	incno = '9';
441423Sroot 	uflag = 0;
451423Sroot 	arg = "u";
461423Sroot 	if(argc > 1) {
471423Sroot 		argv++;
481423Sroot 		argc--;
491423Sroot 		arg = *argv;
501423Sroot 		if (*arg == '-')
511423Sroot 			argc++;
521423Sroot 	}
531423Sroot 	while(*arg)
541423Sroot 	switch (*arg++) {
551463Sroot 	case 'w':
561463Sroot 		lastdump('w');		/* tell us only what has to be done */
571463Sroot 		exit(0);
581463Sroot 		break;
591423Sroot 	case 'W':			/* what to do */
601463Sroot 		lastdump('W');		/* tell us the current state of what has been done */
611423Sroot 		exit(0);		/* do nothing else */
621423Sroot 		break;
631423Sroot 
641423Sroot 	case 'f':			/* output file */
651423Sroot 		if(argc > 1) {
661423Sroot 			argv++;
671423Sroot 			argc--;
681423Sroot 			tape = *argv;
691423Sroot 		}
701423Sroot 		break;
711423Sroot 
721423Sroot 	case 'd':			/* density, in bits per inch */
731423Sroot 		if (argc > 1) {
741423Sroot 			argv++;
751423Sroot 			argc--;
761423Sroot 			density = atoi(*argv) / 10;
7718494Smckusick 			if (density >= 625 && !bflag)
7818494Smckusick 				ntrec = HIGHDENSITYTREC;
791423Sroot 		}
801423Sroot 		break;
811423Sroot 
821423Sroot 	case 's':			/* tape size, feet */
831423Sroot 		if(argc > 1) {
841423Sroot 			argv++;
851423Sroot 			argc--;
861423Sroot 			tsize = atol(*argv);
871423Sroot 			tsize *= 12L*10L;
881423Sroot 		}
891423Sroot 		break;
901423Sroot 
9110910Ssam 	case 'b':			/* blocks per tape write */
9210910Ssam 		if(argc > 1) {
9310910Ssam 			argv++;
9410910Ssam 			argc--;
9518494Smckusick 			bflag++;
9610910Ssam 			ntrec = atol(*argv);
9710910Ssam 		}
9810910Ssam 		break;
9910910Ssam 
10010910Ssam 	case 'c':			/* Tape is cart. not 9-track */
10110910Ssam 		cartridge++;
10210910Ssam 		break;
10310910Ssam 
1041423Sroot 	case '0':			/* dump level */
1051423Sroot 	case '1':
1061423Sroot 	case '2':
1071423Sroot 	case '3':
1081423Sroot 	case '4':
1091423Sroot 	case '5':
1101423Sroot 	case '6':
1111423Sroot 	case '7':
1121423Sroot 	case '8':
1131423Sroot 	case '9':
1141423Sroot 		incno = arg[-1];
1151423Sroot 		break;
1161423Sroot 
1171423Sroot 	case 'u':			/* update /etc/dumpdates */
1181423Sroot 		uflag++;
1191423Sroot 		break;
1201423Sroot 
1211423Sroot 	case 'n':			/* notify operators */
1221423Sroot 		notify++;
1231423Sroot 		break;
1241423Sroot 
1251423Sroot 	default:
12612331Smckusick 		fprintf(stderr, "bad key '%c%'\n", arg[-1]);
1271423Sroot 		Exit(X_ABORT);
1281423Sroot 	}
1291423Sroot 	if(argc > 1) {
1301423Sroot 		argv++;
1311423Sroot 		argc--;
1321423Sroot 		disk = *argv;
1331423Sroot 	}
13412331Smckusick 	if (strcmp(tape, "-") == 0) {
13512331Smckusick 		pipeout++;
13612331Smckusick 		tape = "standard output";
13712331Smckusick 	}
13810910Ssam 
13910910Ssam 	/*
14010910Ssam 	 * Determine how to default tape size and density
14110910Ssam 	 *
14210910Ssam 	 *         	density				tape size
14310910Ssam 	 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
14410910Ssam 	 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
14516251Smckusick  	 * cartridge	8000 bpi (100 bytes/.1")	1700 ft. (450*4 - slop)
14610910Ssam 	 */
14710910Ssam 	if (density == 0)
14810910Ssam 		density = cartridge ? 100 : 160;
14910910Ssam 	if (tsize == 0)
15016251Smckusick  		tsize = cartridge ? 1700L*120L : 2300L*120L;
15110910Ssam 
1526886Ssam #ifdef RDUMP
1536886Ssam 	{ char *index();
1546886Ssam 	  host = tape;
1556886Ssam 	  tape = index(host, ':');
1566886Ssam 	  if (tape == 0) {
1576886Ssam 		msg("need keyletter ``f'' and device ``host:tape''");
1586886Ssam 		exit(1);
1596886Ssam 	  }
1606886Ssam 	  *tape++ = 0;
1616886Ssam 	  if (rmthost(host) == 0)
1626886Ssam 		exit(X_ABORT);
1636886Ssam 	}
1646886Ssam #endif
1651423Sroot 	if (signal(SIGHUP, sighup) == SIG_IGN)
1661423Sroot 		signal(SIGHUP, SIG_IGN);
1671423Sroot 	if (signal(SIGTRAP, sigtrap) == SIG_IGN)
1681423Sroot 		signal(SIGTRAP, SIG_IGN);
1691423Sroot 	if (signal(SIGFPE, sigfpe) == SIG_IGN)
1701423Sroot 		signal(SIGFPE, SIG_IGN);
1711423Sroot 	if (signal(SIGBUS, sigbus) == SIG_IGN)
1721423Sroot 		signal(SIGBUS, SIG_IGN);
1731423Sroot 	if (signal(SIGSEGV, sigsegv) == SIG_IGN)
1741423Sroot 		signal(SIGSEGV, SIG_IGN);
1751423Sroot 	if (signal(SIGTERM, sigterm) == SIG_IGN)
1761423Sroot 		signal(SIGTERM, SIG_IGN);
1771423Sroot 
1781423Sroot 
1791423Sroot 	if (signal(SIGINT, interrupt) == SIG_IGN)
1801423Sroot 		signal(SIGINT, SIG_IGN);
1811423Sroot 
1821423Sroot 	set_operators();	/* /etc/group snarfed */
1831423Sroot 	getfstab();		/* /etc/fstab snarfed */
1841423Sroot 	/*
1851423Sroot 	 *	disk can be either the full special file name,
1861423Sroot 	 *	the suffix of the special file name,
1871423Sroot 	 *	the special name missing the leading '/',
1881423Sroot 	 *	the file system name with or without the leading '/'.
1891423Sroot 	 */
1901423Sroot 	dt = fstabsearch(disk);
1911423Sroot 	if (dt != 0)
1921423Sroot 		disk = rawname(dt->fs_spec);
1931423Sroot 	getitime();		/* /etc/dumpdates snarfed */
1941423Sroot 
1951423Sroot 	msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
19612948Smckusick  	msg("Date of last level %c dump: %s\n",
19712948Smckusick 		lastincno, prdate(spcl.c_ddate));
1981423Sroot 	msg("Dumping %s ", disk);
1991423Sroot 	if (dt != 0)
2001423Sroot 		msgtail("(%s) ", dt->fs_file);
2018506Smckusick #ifdef RDUMP
2028506Smckusick 	msgtail("to %s on host %s\n", tape, host);
2038506Smckusick #else
2048371Smckusick 	msgtail("to %s\n", tape);
2056882Ssam #endif
2061423Sroot 
2071423Sroot 	fi = open(disk, 0);
2081423Sroot 	if (fi < 0) {
2091423Sroot 		msg("Cannot open %s\n", disk);
2101423Sroot 		Exit(X_ABORT);
2111423Sroot 	}
2121423Sroot 	esize = 0;
2135328Smckusic 	sblock = (struct fs *)buf;
2145328Smckusic 	sync();
2155342Smckusic 	bread(SBLOCK, sblock, SBSIZE);
2165328Smckusic 	if (sblock->fs_magic != FS_MAGIC) {
2175328Smckusic 		msg("bad sblock magic number\n");
2185328Smckusic 		dumpabort();
2195328Smckusic 	}
2205328Smckusic 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
2215328Smckusic 		TP_BSIZE);
2225328Smckusic 	clrmap = (char *)calloc(msiz, sizeof(char));
2235328Smckusic 	dirmap = (char *)calloc(msiz, sizeof(char));
2245328Smckusic 	nodmap = (char *)calloc(msiz, sizeof(char));
2251423Sroot 
2261423Sroot 	msg("mapping (Pass I) [regular files]\n");
2275328Smckusic 	pass(mark, (char *)NULL);		/* mark updates esize */
2281423Sroot 
2291423Sroot 	do {
2301423Sroot 		msg("mapping (Pass II) [directories]\n");
2311423Sroot 		nadded = 0;
2321423Sroot 		pass(add, dirmap);
2331423Sroot 	} while(nadded);
2341423Sroot 
2351423Sroot 	bmapest(clrmap);
2361423Sroot 	bmapest(nodmap);
2371423Sroot 
23810910Ssam 	if (cartridge) {
23910910Ssam 		/* Estimate number of tapes, assuming streaming stops at
24010910Ssam 		   the end of each block written, and not in mid-block.
24110910Ssam 		   Assume no erroneous blocks; this can be compensated for
24210910Ssam 		   with an artificially low tape size. */
24310910Ssam 		fetapes =
2445328Smckusic 		(	  esize		/* blocks */
24510910Ssam 			* TP_BSIZE	/* bytes/block */
24610910Ssam 			* (1.0/density)	/* 0.1" / byte */
24710910Ssam 		  +
24810910Ssam 			  esize		/* blocks */
24910910Ssam 			* (1.0/ntrec)	/* streaming-stops per block */
25010910Ssam 			* 15.48		/* 0.1" / streaming-stop */
25110910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
25210910Ssam 	} else {
25310910Ssam 		/* Estimate number of tapes, for old fashioned 9-track tape */
25410910Ssam 		int tenthsperirg = (density == 625) ? 3 : 7;
25510910Ssam 		fetapes =
25610910Ssam 		(	  esize		/* blocks */
2575328Smckusic 			* TP_BSIZE	/* bytes / block */
2585328Smckusic 			* (1.0/density)	/* 0.1" / byte */
2591423Sroot 		  +
2605328Smckusic 			  esize		/* blocks */
26110910Ssam 			* (1.0/ntrec)	/* IRG's / block */
26210910Ssam 			* tenthsperirg	/* 0.1" / IRG */
26310910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
26410910Ssam 	}
2651423Sroot 	etapes = fetapes;		/* truncating assignment */
2661423Sroot 	etapes++;
2675328Smckusic 	/* count the nodemap on each additional tape */
2685328Smckusic 	for (i = 1; i < etapes; i++)
2695328Smckusic 		bmapest(nodmap);
2705328Smckusic 	esize += i + 10;	/* headers + 10 trailer blocks */
2711423Sroot 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
2721423Sroot 
27310910Ssam 	alloctape();			/* Allocate tape buffer */
27410910Ssam 
2751423Sroot 	otape();			/* bitmap is the first to tape write */
2761423Sroot 	time(&(tstart_writing));
2771423Sroot 	bitmap(clrmap, TS_CLRI);
2781423Sroot 
2791423Sroot 	msg("dumping (Pass III) [directories]\n");
28017234Smckusick  	pass(dirdump, dirmap);
2811423Sroot 
2821423Sroot 	msg("dumping (Pass IV) [regular files]\n");
2831423Sroot 	pass(dump, nodmap);
2841423Sroot 
2851423Sroot 	spcl.c_type = TS_END;
2866882Ssam #ifndef RDUMP
28710910Ssam 	for(i=0; i<ntrec; i++)
2881423Sroot 		spclrec();
2896882Ssam #endif
2901423Sroot 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
2911423Sroot 	msg("DUMP IS DONE\n");
2921423Sroot 
2931423Sroot 	putitime();
2946882Ssam #ifndef RDUMP
29512331Smckusick 	if (!pipeout) {
29612331Smckusick 		close(to);
29712331Smckusick 		rewind();
29812331Smckusick 	}
2996882Ssam #else
3006882Ssam 	tflush(1);
30112331Smckusick 	rewind();
3026882Ssam #endif
3031423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
3041423Sroot 	Exit(X_FINOK);
3051423Sroot }
3061423Sroot 
3071423Sroot int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
3081423Sroot int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
3091423Sroot int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
3101423Sroot int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
3111423Sroot int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
3121423Sroot int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
3131423Sroot int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
3141423Sroot 
3151423Sroot sigAbort()
3161423Sroot {
31712331Smckusick 	if (pipeout) {
31812331Smckusick 		msg("Unknown signal, cannot recover\n");
31912331Smckusick 		dumpabort();
32012331Smckusick 	}
3211423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
3221423Sroot 	fflush(stderr);
3231423Sroot 	fflush(stdout);
3241423Sroot 	close_rewind();
3251423Sroot 	exit(X_REWRITE);
3261423Sroot }
3271423Sroot 
3281423Sroot char *rawname(cp)
3291423Sroot 	char *cp;
3301423Sroot {
3311423Sroot 	static char rawbuf[32];
3324608Smckusic 	char *rindex();
3331423Sroot 	char *dp = rindex(cp, '/');
3341423Sroot 
3351423Sroot 	if (dp == 0)
3361423Sroot 		return (0);
3371423Sroot 	*dp = 0;
3381423Sroot 	strcpy(rawbuf, cp);
3391423Sroot 	*dp = '/';
3401423Sroot 	strcat(rawbuf, "/r");
3411423Sroot 	strcat(rawbuf, dp+1);
3421423Sroot 	return (rawbuf);
3431423Sroot }
344