xref: /csrg-svn/sbin/dump/main.c (revision 29900)
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*29900Smckusick static char sccsid[] = "@(#)main.c	5.5 (Berkeley) 10/21/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 	}
16628860Smckusick 	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);
194*29900Smckusick 	if (dt != 0) {
1951423Sroot 		disk = rawname(dt->fs_spec);
196*29900Smckusick 		strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
197*29900Smckusick 		strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
198*29900Smckusick 	} else {
199*29900Smckusick 		strncpy(spcl.c_dev, disk, NAMELEN);
200*29900Smckusick 		strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN);
201*29900Smckusick 	}
202*29900Smckusick 	strcpy(spcl.c_label, "none");
203*29900Smckusick 	gethostname(spcl.c_host, NAMELEN);
204*29900Smckusick 	spcl.c_level = incno - '0';
205*29900Smckusick 	spcl.c_type = TS_TAPE;
2061423Sroot 	getitime();		/* /etc/dumpdates snarfed */
2071423Sroot 
2081423Sroot 	msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
20912948Smckusick  	msg("Date of last level %c dump: %s\n",
21012948Smckusick 		lastincno, prdate(spcl.c_ddate));
2111423Sroot 	msg("Dumping %s ", disk);
2121423Sroot 	if (dt != 0)
2131423Sroot 		msgtail("(%s) ", dt->fs_file);
2148506Smckusick #ifdef RDUMP
2158506Smckusick 	msgtail("to %s on host %s\n", tape, host);
2168506Smckusick #else
2178371Smckusick 	msgtail("to %s\n", tape);
2186882Ssam #endif
2191423Sroot 
2201423Sroot 	fi = open(disk, 0);
2211423Sroot 	if (fi < 0) {
2221423Sroot 		msg("Cannot open %s\n", disk);
2231423Sroot 		Exit(X_ABORT);
2241423Sroot 	}
2251423Sroot 	esize = 0;
2265328Smckusic 	sblock = (struct fs *)buf;
2275328Smckusic 	sync();
2285342Smckusic 	bread(SBLOCK, sblock, SBSIZE);
2295328Smckusic 	if (sblock->fs_magic != FS_MAGIC) {
2305328Smckusic 		msg("bad sblock magic number\n");
2315328Smckusic 		dumpabort();
2325328Smckusic 	}
2335328Smckusic 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
2345328Smckusic 		TP_BSIZE);
2355328Smckusic 	clrmap = (char *)calloc(msiz, sizeof(char));
2365328Smckusic 	dirmap = (char *)calloc(msiz, sizeof(char));
2375328Smckusic 	nodmap = (char *)calloc(msiz, sizeof(char));
2381423Sroot 
23925797Smckusick 	anydskipped = 0;
2401423Sroot 	msg("mapping (Pass I) [regular files]\n");
2415328Smckusic 	pass(mark, (char *)NULL);		/* mark updates esize */
2421423Sroot 
24325797Smckusick 	if (anydskipped) {
24425797Smckusick 		do {
24525797Smckusick 			msg("mapping (Pass II) [directories]\n");
24625797Smckusick 			nadded = 0;
24725797Smckusick 			pass(add, dirmap);
24825797Smckusick 		} while(nadded);
24925797Smckusick 	} else				/* keep the operators happy */
2501423Sroot 		msg("mapping (Pass II) [directories]\n");
2511423Sroot 
2521423Sroot 	bmapest(clrmap);
2531423Sroot 	bmapest(nodmap);
2541423Sroot 
25510910Ssam 	if (cartridge) {
25610910Ssam 		/* Estimate number of tapes, assuming streaming stops at
25710910Ssam 		   the end of each block written, and not in mid-block.
25810910Ssam 		   Assume no erroneous blocks; this can be compensated for
25910910Ssam 		   with an artificially low tape size. */
26010910Ssam 		fetapes =
2615328Smckusic 		(	  esize		/* blocks */
26210910Ssam 			* TP_BSIZE	/* bytes/block */
26310910Ssam 			* (1.0/density)	/* 0.1" / byte */
26410910Ssam 		  +
26510910Ssam 			  esize		/* blocks */
26610910Ssam 			* (1.0/ntrec)	/* streaming-stops per block */
26710910Ssam 			* 15.48		/* 0.1" / streaming-stop */
26810910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
26910910Ssam 	} else {
27010910Ssam 		/* Estimate number of tapes, for old fashioned 9-track tape */
27110910Ssam 		int tenthsperirg = (density == 625) ? 3 : 7;
27210910Ssam 		fetapes =
27310910Ssam 		(	  esize		/* blocks */
2745328Smckusic 			* TP_BSIZE	/* bytes / block */
2755328Smckusic 			* (1.0/density)	/* 0.1" / byte */
2761423Sroot 		  +
2775328Smckusic 			  esize		/* blocks */
27810910Ssam 			* (1.0/ntrec)	/* IRG's / block */
27910910Ssam 			* tenthsperirg	/* 0.1" / IRG */
28010910Ssam 		) * (1.0 / tsize );	/* tape / 0.1" */
28110910Ssam 	}
2821423Sroot 	etapes = fetapes;		/* truncating assignment */
2831423Sroot 	etapes++;
2845328Smckusic 	/* count the nodemap on each additional tape */
2855328Smckusic 	for (i = 1; i < etapes; i++)
2865328Smckusic 		bmapest(nodmap);
2875328Smckusic 	esize += i + 10;	/* headers + 10 trailer blocks */
2881423Sroot 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
2891423Sroot 
29010910Ssam 	alloctape();			/* Allocate tape buffer */
29110910Ssam 
2921423Sroot 	otape();			/* bitmap is the first to tape write */
2931423Sroot 	time(&(tstart_writing));
2941423Sroot 	bitmap(clrmap, TS_CLRI);
2951423Sroot 
2961423Sroot 	msg("dumping (Pass III) [directories]\n");
29717234Smckusick  	pass(dirdump, dirmap);
2981423Sroot 
2991423Sroot 	msg("dumping (Pass IV) [regular files]\n");
3001423Sroot 	pass(dump, nodmap);
3011423Sroot 
3021423Sroot 	spcl.c_type = TS_END;
3036882Ssam #ifndef RDUMP
30410910Ssam 	for(i=0; i<ntrec; i++)
3051423Sroot 		spclrec();
3066882Ssam #endif
3071423Sroot 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
3081423Sroot 	msg("DUMP IS DONE\n");
3091423Sroot 
3101423Sroot 	putitime();
3116882Ssam #ifndef RDUMP
31212331Smckusick 	if (!pipeout) {
31312331Smckusick 		close(to);
31412331Smckusick 		rewind();
31512331Smckusick 	}
3166882Ssam #else
3176882Ssam 	tflush(1);
31812331Smckusick 	rewind();
3196882Ssam #endif
3201423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
3211423Sroot 	Exit(X_FINOK);
3221423Sroot }
3231423Sroot 
3241423Sroot int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
3251423Sroot int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
3261423Sroot int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
3271423Sroot int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
3281423Sroot int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
3291423Sroot int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
3301423Sroot int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
3311423Sroot 
3321423Sroot sigAbort()
3331423Sroot {
33412331Smckusick 	if (pipeout) {
33512331Smckusick 		msg("Unknown signal, cannot recover\n");
33612331Smckusick 		dumpabort();
33712331Smckusick 	}
3381423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
3391423Sroot 	fflush(stderr);
3401423Sroot 	fflush(stdout);
3411423Sroot 	close_rewind();
3421423Sroot 	exit(X_REWRITE);
3431423Sroot }
3441423Sroot 
3451423Sroot char *rawname(cp)
3461423Sroot 	char *cp;
3471423Sroot {
3481423Sroot 	static char rawbuf[32];
3494608Smckusic 	char *rindex();
3501423Sroot 	char *dp = rindex(cp, '/');
3511423Sroot 
3521423Sroot 	if (dp == 0)
3531423Sroot 		return (0);
3541423Sroot 	*dp = 0;
3551423Sroot 	strcpy(rawbuf, cp);
3561423Sroot 	*dp = '/';
3571423Sroot 	strcat(rawbuf, "/r");
3581423Sroot 	strcat(rawbuf, dp+1);
3591423Sroot 	return (rawbuf);
3601423Sroot }
361