xref: /csrg-svn/sbin/dump/main.c (revision 28860)
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*28860Smckusick static char sccsid[] = "@(#)main.c	5.4 (Berkeley) 05/28/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) {
15928642Smckusick 		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 	}
166*28860Smckusick 	setuid(getuid());	/* rmthost() is the only reason to be setuid */
1676886Ssam #endif
1681423Sroot 	if (signal(SIGHUP, sighup) == SIG_IGN)
1691423Sroot 		signal(SIGHUP, SIG_IGN);
1701423Sroot 	if (signal(SIGTRAP, sigtrap) == SIG_IGN)
1711423Sroot 		signal(SIGTRAP, SIG_IGN);
1721423Sroot 	if (signal(SIGFPE, sigfpe) == SIG_IGN)
1731423Sroot 		signal(SIGFPE, SIG_IGN);
1741423Sroot 	if (signal(SIGBUS, sigbus) == SIG_IGN)
1751423Sroot 		signal(SIGBUS, SIG_IGN);
1761423Sroot 	if (signal(SIGSEGV, sigsegv) == SIG_IGN)
1771423Sroot 		signal(SIGSEGV, SIG_IGN);
1781423Sroot 	if (signal(SIGTERM, sigterm) == SIG_IGN)
1791423Sroot 		signal(SIGTERM, SIG_IGN);
1801423Sroot 
1811423Sroot 
1821423Sroot 	if (signal(SIGINT, interrupt) == SIG_IGN)
1831423Sroot 		signal(SIGINT, SIG_IGN);
1841423Sroot 
1851423Sroot 	set_operators();	/* /etc/group snarfed */
1861423Sroot 	getfstab();		/* /etc/fstab snarfed */
1871423Sroot 	/*
1881423Sroot 	 *	disk can be either the full special file name,
1891423Sroot 	 *	the suffix of the special file name,
1901423Sroot 	 *	the special name missing the leading '/',
1911423Sroot 	 *	the file system name with or without the leading '/'.
1921423Sroot 	 */
1931423Sroot 	dt = fstabsearch(disk);
1941423Sroot 	if (dt != 0)
1951423Sroot 		disk = rawname(dt->fs_spec);
1961423Sroot 	getitime();		/* /etc/dumpdates snarfed */
1971423Sroot 
1981423Sroot 	msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
19912948Smckusick  	msg("Date of last level %c dump: %s\n",
20012948Smckusick 		lastincno, prdate(spcl.c_ddate));
2011423Sroot 	msg("Dumping %s ", disk);
2021423Sroot 	if (dt != 0)
2031423Sroot 		msgtail("(%s) ", dt->fs_file);
2048506Smckusick #ifdef RDUMP
2058506Smckusick 	msgtail("to %s on host %s\n", tape, host);
2068506Smckusick #else
2078371Smckusick 	msgtail("to %s\n", tape);
2086882Ssam #endif
2091423Sroot 
2101423Sroot 	fi = open(disk, 0);
2111423Sroot 	if (fi < 0) {
2121423Sroot 		msg("Cannot open %s\n", disk);
2131423Sroot 		Exit(X_ABORT);
2141423Sroot 	}
2151423Sroot 	esize = 0;
2165328Smckusic 	sblock = (struct fs *)buf;
2175328Smckusic 	sync();
2185342Smckusic 	bread(SBLOCK, sblock, SBSIZE);
2195328Smckusic 	if (sblock->fs_magic != FS_MAGIC) {
2205328Smckusic 		msg("bad sblock magic number\n");
2215328Smckusic 		dumpabort();
2225328Smckusic 	}
2235328Smckusic 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
2245328Smckusic 		TP_BSIZE);
2255328Smckusic 	clrmap = (char *)calloc(msiz, sizeof(char));
2265328Smckusic 	dirmap = (char *)calloc(msiz, sizeof(char));
2275328Smckusic 	nodmap = (char *)calloc(msiz, sizeof(char));
2281423Sroot 
22925797Smckusick 	anydskipped = 0;
2301423Sroot 	msg("mapping (Pass I) [regular files]\n");
2315328Smckusic 	pass(mark, (char *)NULL);		/* mark updates esize */
2321423Sroot 
23325797Smckusick 	if (anydskipped) {
23425797Smckusick 		do {
23525797Smckusick 			msg("mapping (Pass II) [directories]\n");
23625797Smckusick 			nadded = 0;
23725797Smckusick 			pass(add, dirmap);
23825797Smckusick 		} while(nadded);
23925797Smckusick 	} else				/* keep the operators happy */
2401423Sroot 		msg("mapping (Pass II) [directories]\n");
2411423Sroot 
2421423Sroot 	bmapest(clrmap);
2431423Sroot 	bmapest(nodmap);
2441423Sroot 
24510910Ssam 	if (cartridge) {
24610910Ssam 		/* Estimate number of tapes, assuming streaming stops at
24710910Ssam 		   the end of each block written, and not in mid-block.
24810910Ssam 		   Assume no erroneous blocks; this can be compensated for
24910910Ssam 		   with an artificially low tape size. */
25010910Ssam 		fetapes =
2515328Smckusic 		(	  esize		/* blocks */
25210910Ssam 			* TP_BSIZE	/* bytes/block */
25310910Ssam 			* (1.0/density)	/* 0.1" / byte */
25410910Ssam 		  +
25510910Ssam 			  esize		/* blocks */
25610910Ssam 			* (1.0/ntrec)	/* streaming-stops per block */
25710910Ssam 			* 15.48		/* 0.1" / streaming-stop */
25810910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
25910910Ssam 	} else {
26010910Ssam 		/* Estimate number of tapes, for old fashioned 9-track tape */
26110910Ssam 		int tenthsperirg = (density == 625) ? 3 : 7;
26210910Ssam 		fetapes =
26310910Ssam 		(	  esize		/* blocks */
2645328Smckusic 			* TP_BSIZE	/* bytes / block */
2655328Smckusic 			* (1.0/density)	/* 0.1" / byte */
2661423Sroot 		  +
2675328Smckusic 			  esize		/* blocks */
26810910Ssam 			* (1.0/ntrec)	/* IRG's / block */
26910910Ssam 			* tenthsperirg	/* 0.1" / IRG */
27010910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
27110910Ssam 	}
2721423Sroot 	etapes = fetapes;		/* truncating assignment */
2731423Sroot 	etapes++;
2745328Smckusic 	/* count the nodemap on each additional tape */
2755328Smckusic 	for (i = 1; i < etapes; i++)
2765328Smckusic 		bmapest(nodmap);
2775328Smckusic 	esize += i + 10;	/* headers + 10 trailer blocks */
2781423Sroot 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
2791423Sroot 
28010910Ssam 	alloctape();			/* Allocate tape buffer */
28110910Ssam 
2821423Sroot 	otape();			/* bitmap is the first to tape write */
2831423Sroot 	time(&(tstart_writing));
2841423Sroot 	bitmap(clrmap, TS_CLRI);
2851423Sroot 
2861423Sroot 	msg("dumping (Pass III) [directories]\n");
28717234Smckusick  	pass(dirdump, dirmap);
2881423Sroot 
2891423Sroot 	msg("dumping (Pass IV) [regular files]\n");
2901423Sroot 	pass(dump, nodmap);
2911423Sroot 
2921423Sroot 	spcl.c_type = TS_END;
2936882Ssam #ifndef RDUMP
29410910Ssam 	for(i=0; i<ntrec; i++)
2951423Sroot 		spclrec();
2966882Ssam #endif
2971423Sroot 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
2981423Sroot 	msg("DUMP IS DONE\n");
2991423Sroot 
3001423Sroot 	putitime();
3016882Ssam #ifndef RDUMP
30212331Smckusick 	if (!pipeout) {
30312331Smckusick 		close(to);
30412331Smckusick 		rewind();
30512331Smckusick 	}
3066882Ssam #else
3076882Ssam 	tflush(1);
30812331Smckusick 	rewind();
3096882Ssam #endif
3101423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
3111423Sroot 	Exit(X_FINOK);
3121423Sroot }
3131423Sroot 
3141423Sroot int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
3151423Sroot int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
3161423Sroot int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
3171423Sroot int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
3181423Sroot int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
3191423Sroot int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
3201423Sroot int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
3211423Sroot 
3221423Sroot sigAbort()
3231423Sroot {
32412331Smckusick 	if (pipeout) {
32512331Smckusick 		msg("Unknown signal, cannot recover\n");
32612331Smckusick 		dumpabort();
32712331Smckusick 	}
3281423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
3291423Sroot 	fflush(stderr);
3301423Sroot 	fflush(stdout);
3311423Sroot 	close_rewind();
3321423Sroot 	exit(X_REWRITE);
3331423Sroot }
3341423Sroot 
3351423Sroot char *rawname(cp)
3361423Sroot 	char *cp;
3371423Sroot {
3381423Sroot 	static char rawbuf[32];
3394608Smckusic 	char *rindex();
3401423Sroot 	char *dp = rindex(cp, '/');
3411423Sroot 
3421423Sroot 	if (dp == 0)
3431423Sroot 		return (0);
3441423Sroot 	*dp = 0;
3451423Sroot 	strcpy(rawbuf, cp);
3461423Sroot 	*dp = '/';
3471423Sroot 	strcat(rawbuf, "/r");
3481423Sroot 	strcat(rawbuf, dp+1);
3491423Sroot 	return (rawbuf);
3501423Sroot }
351