xref: /csrg-svn/sbin/dump/main.c (revision 30560)
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*30560Smckusick static char sccsid[] = "@(#)main.c	5.6 (Berkeley) 02/23/87";
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 */
19*30560Smckusick long	dev_bsize = 1;	/* recalculated below */
206882Ssam #ifdef RDUMP
216882Ssam char	*host;
226882Ssam #endif
2325797Smckusick int	anydskipped;	/* set true in mark() if any directories are skipped */
2425797Smckusick 			/* this lets us avoid map pass 2 in some cases */
251423Sroot 
261423Sroot main(argc, argv)
271423Sroot 	int	argc;
281423Sroot 	char	*argv[];
291423Sroot {
301423Sroot 	char		*arg;
3118494Smckusick 	int		bflag = 0, i;
321423Sroot 	float		fetapes;
331423Sroot 	register	struct	fstab	*dt;
341423Sroot 
351423Sroot 	time(&(spcl.c_date));
361423Sroot 
3710910Ssam 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
381423Sroot 	tape = TAPE;
391423Sroot 	disk = DISK;
401423Sroot 	increm = NINCREM;
4112590Smckusick 	temp = TEMP;
425328Smckusic 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) {
435328Smckusic 		msg("TP_BSIZE must be a multiple of DEV_BSIZE\n");
445328Smckusic 		dumpabort();
455328Smckusic 	}
461423Sroot 	incno = '9';
471423Sroot 	uflag = 0;
481423Sroot 	arg = "u";
491423Sroot 	if(argc > 1) {
501423Sroot 		argv++;
511423Sroot 		argc--;
521423Sroot 		arg = *argv;
531423Sroot 		if (*arg == '-')
541423Sroot 			argc++;
551423Sroot 	}
561423Sroot 	while(*arg)
571423Sroot 	switch (*arg++) {
581463Sroot 	case 'w':
591463Sroot 		lastdump('w');		/* tell us only what has to be done */
601463Sroot 		exit(0);
611463Sroot 		break;
621423Sroot 	case 'W':			/* what to do */
631463Sroot 		lastdump('W');		/* tell us the current state of what has been done */
641423Sroot 		exit(0);		/* do nothing else */
651423Sroot 		break;
661423Sroot 
671423Sroot 	case 'f':			/* output file */
681423Sroot 		if(argc > 1) {
691423Sroot 			argv++;
701423Sroot 			argc--;
711423Sroot 			tape = *argv;
721423Sroot 		}
731423Sroot 		break;
741423Sroot 
751423Sroot 	case 'd':			/* density, in bits per inch */
761423Sroot 		if (argc > 1) {
771423Sroot 			argv++;
781423Sroot 			argc--;
791423Sroot 			density = atoi(*argv) / 10;
8018494Smckusick 			if (density >= 625 && !bflag)
8118494Smckusick 				ntrec = HIGHDENSITYTREC;
821423Sroot 		}
831423Sroot 		break;
841423Sroot 
851423Sroot 	case 's':			/* tape size, feet */
861423Sroot 		if(argc > 1) {
871423Sroot 			argv++;
881423Sroot 			argc--;
891423Sroot 			tsize = atol(*argv);
901423Sroot 			tsize *= 12L*10L;
911423Sroot 		}
921423Sroot 		break;
931423Sroot 
9410910Ssam 	case 'b':			/* blocks per tape write */
9510910Ssam 		if(argc > 1) {
9610910Ssam 			argv++;
9710910Ssam 			argc--;
9818494Smckusick 			bflag++;
9910910Ssam 			ntrec = atol(*argv);
10010910Ssam 		}
10110910Ssam 		break;
10210910Ssam 
10310910Ssam 	case 'c':			/* Tape is cart. not 9-track */
10410910Ssam 		cartridge++;
10510910Ssam 		break;
10610910Ssam 
1071423Sroot 	case '0':			/* dump level */
1081423Sroot 	case '1':
1091423Sroot 	case '2':
1101423Sroot 	case '3':
1111423Sroot 	case '4':
1121423Sroot 	case '5':
1131423Sroot 	case '6':
1141423Sroot 	case '7':
1151423Sroot 	case '8':
1161423Sroot 	case '9':
1171423Sroot 		incno = arg[-1];
1181423Sroot 		break;
1191423Sroot 
1201423Sroot 	case 'u':			/* update /etc/dumpdates */
1211423Sroot 		uflag++;
1221423Sroot 		break;
1231423Sroot 
1241423Sroot 	case 'n':			/* notify operators */
1251423Sroot 		notify++;
1261423Sroot 		break;
1271423Sroot 
1281423Sroot 	default:
12912331Smckusick 		fprintf(stderr, "bad key '%c%'\n", arg[-1]);
1301423Sroot 		Exit(X_ABORT);
1311423Sroot 	}
1321423Sroot 	if(argc > 1) {
1331423Sroot 		argv++;
1341423Sroot 		argc--;
1351423Sroot 		disk = *argv;
1361423Sroot 	}
13712331Smckusick 	if (strcmp(tape, "-") == 0) {
13812331Smckusick 		pipeout++;
13912331Smckusick 		tape = "standard output";
14012331Smckusick 	}
14110910Ssam 
14210910Ssam 	/*
14310910Ssam 	 * Determine how to default tape size and density
14410910Ssam 	 *
14510910Ssam 	 *         	density				tape size
14610910Ssam 	 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
14710910Ssam 	 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
14816251Smckusick  	 * cartridge	8000 bpi (100 bytes/.1")	1700 ft. (450*4 - slop)
14910910Ssam 	 */
15010910Ssam 	if (density == 0)
15110910Ssam 		density = cartridge ? 100 : 160;
15210910Ssam 	if (tsize == 0)
15316251Smckusick  		tsize = cartridge ? 1700L*120L : 2300L*120L;
15410910Ssam 
1556886Ssam #ifdef RDUMP
1566886Ssam 	{ char *index();
1576886Ssam 	  host = tape;
1586886Ssam 	  tape = index(host, ':');
1596886Ssam 	  if (tape == 0) {
16028642Smckusick 		msg("need keyletter ``f'' and device ``host:tape''\n");
1616886Ssam 		exit(1);
1626886Ssam 	  }
1636886Ssam 	  *tape++ = 0;
1646886Ssam 	  if (rmthost(host) == 0)
1656886Ssam 		exit(X_ABORT);
1666886Ssam 	}
16728860Smckusick 	setuid(getuid());	/* rmthost() is the only reason to be setuid */
1686886Ssam #endif
1691423Sroot 	if (signal(SIGHUP, sighup) == SIG_IGN)
1701423Sroot 		signal(SIGHUP, SIG_IGN);
1711423Sroot 	if (signal(SIGTRAP, sigtrap) == SIG_IGN)
1721423Sroot 		signal(SIGTRAP, SIG_IGN);
1731423Sroot 	if (signal(SIGFPE, sigfpe) == SIG_IGN)
1741423Sroot 		signal(SIGFPE, SIG_IGN);
1751423Sroot 	if (signal(SIGBUS, sigbus) == SIG_IGN)
1761423Sroot 		signal(SIGBUS, SIG_IGN);
1771423Sroot 	if (signal(SIGSEGV, sigsegv) == SIG_IGN)
1781423Sroot 		signal(SIGSEGV, SIG_IGN);
1791423Sroot 	if (signal(SIGTERM, sigterm) == SIG_IGN)
1801423Sroot 		signal(SIGTERM, SIG_IGN);
1811423Sroot 
1821423Sroot 
1831423Sroot 	if (signal(SIGINT, interrupt) == SIG_IGN)
1841423Sroot 		signal(SIGINT, SIG_IGN);
1851423Sroot 
1861423Sroot 	set_operators();	/* /etc/group snarfed */
1871423Sroot 	getfstab();		/* /etc/fstab snarfed */
1881423Sroot 	/*
1891423Sroot 	 *	disk can be either the full special file name,
1901423Sroot 	 *	the suffix of the special file name,
1911423Sroot 	 *	the special name missing the leading '/',
1921423Sroot 	 *	the file system name with or without the leading '/'.
1931423Sroot 	 */
1941423Sroot 	dt = fstabsearch(disk);
19529900Smckusick 	if (dt != 0) {
1961423Sroot 		disk = rawname(dt->fs_spec);
19729900Smckusick 		strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
19829900Smckusick 		strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
19929900Smckusick 	} else {
20029900Smckusick 		strncpy(spcl.c_dev, disk, NAMELEN);
20129900Smckusick 		strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN);
20229900Smckusick 	}
20329900Smckusick 	strcpy(spcl.c_label, "none");
20429900Smckusick 	gethostname(spcl.c_host, NAMELEN);
20529900Smckusick 	spcl.c_level = incno - '0';
20629900Smckusick 	spcl.c_type = TS_TAPE;
2071423Sroot 	getitime();		/* /etc/dumpdates snarfed */
2081423Sroot 
2091423Sroot 	msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
21012948Smckusick  	msg("Date of last level %c dump: %s\n",
21112948Smckusick 		lastincno, prdate(spcl.c_ddate));
2121423Sroot 	msg("Dumping %s ", disk);
2131423Sroot 	if (dt != 0)
2141423Sroot 		msgtail("(%s) ", dt->fs_file);
2158506Smckusick #ifdef RDUMP
2168506Smckusick 	msgtail("to %s on host %s\n", tape, host);
2178506Smckusick #else
2188371Smckusick 	msgtail("to %s\n", tape);
2196882Ssam #endif
2201423Sroot 
2211423Sroot 	fi = open(disk, 0);
2221423Sroot 	if (fi < 0) {
2231423Sroot 		msg("Cannot open %s\n", disk);
2241423Sroot 		Exit(X_ABORT);
2251423Sroot 	}
2261423Sroot 	esize = 0;
2275328Smckusic 	sblock = (struct fs *)buf;
2285328Smckusic 	sync();
229*30560Smckusick 	bread(SBOFF, sblock, SBSIZE);
2305328Smckusic 	if (sblock->fs_magic != FS_MAGIC) {
2315328Smckusic 		msg("bad sblock magic number\n");
2325328Smckusic 		dumpabort();
2335328Smckusic 	}
234*30560Smckusick 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
2355328Smckusic 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
2365328Smckusic 		TP_BSIZE);
2375328Smckusic 	clrmap = (char *)calloc(msiz, sizeof(char));
2385328Smckusic 	dirmap = (char *)calloc(msiz, sizeof(char));
2395328Smckusic 	nodmap = (char *)calloc(msiz, sizeof(char));
2401423Sroot 
24125797Smckusick 	anydskipped = 0;
2421423Sroot 	msg("mapping (Pass I) [regular files]\n");
2435328Smckusic 	pass(mark, (char *)NULL);		/* mark updates esize */
2441423Sroot 
24525797Smckusick 	if (anydskipped) {
24625797Smckusick 		do {
24725797Smckusick 			msg("mapping (Pass II) [directories]\n");
24825797Smckusick 			nadded = 0;
24925797Smckusick 			pass(add, dirmap);
25025797Smckusick 		} while(nadded);
25125797Smckusick 	} else				/* keep the operators happy */
2521423Sroot 		msg("mapping (Pass II) [directories]\n");
2531423Sroot 
2541423Sroot 	bmapest(clrmap);
2551423Sroot 	bmapest(nodmap);
2561423Sroot 
25710910Ssam 	if (cartridge) {
25810910Ssam 		/* Estimate number of tapes, assuming streaming stops at
25910910Ssam 		   the end of each block written, and not in mid-block.
26010910Ssam 		   Assume no erroneous blocks; this can be compensated for
26110910Ssam 		   with an artificially low tape size. */
26210910Ssam 		fetapes =
2635328Smckusic 		(	  esize		/* blocks */
26410910Ssam 			* TP_BSIZE	/* bytes/block */
26510910Ssam 			* (1.0/density)	/* 0.1" / byte */
26610910Ssam 		  +
26710910Ssam 			  esize		/* blocks */
26810910Ssam 			* (1.0/ntrec)	/* streaming-stops per block */
26910910Ssam 			* 15.48		/* 0.1" / streaming-stop */
27010910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
27110910Ssam 	} else {
27210910Ssam 		/* Estimate number of tapes, for old fashioned 9-track tape */
27310910Ssam 		int tenthsperirg = (density == 625) ? 3 : 7;
27410910Ssam 		fetapes =
27510910Ssam 		(	  esize		/* blocks */
2765328Smckusic 			* TP_BSIZE	/* bytes / block */
2775328Smckusic 			* (1.0/density)	/* 0.1" / byte */
2781423Sroot 		  +
2795328Smckusic 			  esize		/* blocks */
28010910Ssam 			* (1.0/ntrec)	/* IRG's / block */
28110910Ssam 			* tenthsperirg	/* 0.1" / IRG */
28210910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
28310910Ssam 	}
2841423Sroot 	etapes = fetapes;		/* truncating assignment */
2851423Sroot 	etapes++;
2865328Smckusic 	/* count the nodemap on each additional tape */
2875328Smckusic 	for (i = 1; i < etapes; i++)
2885328Smckusic 		bmapest(nodmap);
2895328Smckusic 	esize += i + 10;	/* headers + 10 trailer blocks */
2901423Sroot 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
2911423Sroot 
29210910Ssam 	alloctape();			/* Allocate tape buffer */
29310910Ssam 
2941423Sroot 	otape();			/* bitmap is the first to tape write */
2951423Sroot 	time(&(tstart_writing));
2961423Sroot 	bitmap(clrmap, TS_CLRI);
2971423Sroot 
2981423Sroot 	msg("dumping (Pass III) [directories]\n");
29917234Smckusick  	pass(dirdump, dirmap);
3001423Sroot 
3011423Sroot 	msg("dumping (Pass IV) [regular files]\n");
3021423Sroot 	pass(dump, nodmap);
3031423Sroot 
3041423Sroot 	spcl.c_type = TS_END;
3056882Ssam #ifndef RDUMP
30610910Ssam 	for(i=0; i<ntrec; i++)
3071423Sroot 		spclrec();
3086882Ssam #endif
3091423Sroot 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
3101423Sroot 	msg("DUMP IS DONE\n");
3111423Sroot 
3121423Sroot 	putitime();
3136882Ssam #ifndef RDUMP
31412331Smckusick 	if (!pipeout) {
31512331Smckusick 		close(to);
31612331Smckusick 		rewind();
31712331Smckusick 	}
3186882Ssam #else
3196882Ssam 	tflush(1);
32012331Smckusick 	rewind();
3216882Ssam #endif
3221423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
3231423Sroot 	Exit(X_FINOK);
3241423Sroot }
3251423Sroot 
3261423Sroot int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
3271423Sroot int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
3281423Sroot int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
3291423Sroot int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
3301423Sroot int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
3311423Sroot int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
3321423Sroot int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
3331423Sroot 
3341423Sroot sigAbort()
3351423Sroot {
33612331Smckusick 	if (pipeout) {
33712331Smckusick 		msg("Unknown signal, cannot recover\n");
33812331Smckusick 		dumpabort();
33912331Smckusick 	}
3401423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
3411423Sroot 	fflush(stderr);
3421423Sroot 	fflush(stdout);
3431423Sroot 	close_rewind();
3441423Sroot 	exit(X_REWRITE);
3451423Sroot }
3461423Sroot 
3471423Sroot char *rawname(cp)
3481423Sroot 	char *cp;
3491423Sroot {
3501423Sroot 	static char rawbuf[32];
3514608Smckusic 	char *rindex();
3521423Sroot 	char *dp = rindex(cp, '/');
3531423Sroot 
3541423Sroot 	if (dp == 0)
3551423Sroot 		return (0);
3561423Sroot 	*dp = 0;
3571423Sroot 	strcpy(rawbuf, cp);
3581423Sroot 	*dp = '/';
3591423Sroot 	strcat(rawbuf, "/r");
3601423Sroot 	strcat(rawbuf, dp+1);
3611423Sroot 	return (rawbuf);
3621423Sroot }
363