xref: /csrg-svn/sbin/dump/main.c (revision 8371)
1*8371Smckusick static	char *sccsid = "@(#)main.c	1.9 (Berkeley) 10/07/82";
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 */
71423Sroot int	density = 160;	/* density in 0.1" units */
86882Ssam #ifdef RDUMP
96882Ssam char	*host;
106882Ssam #endif
111423Sroot 
121423Sroot main(argc, argv)
131423Sroot 	int	argc;
141423Sroot 	char	*argv[];
151423Sroot {
161423Sroot 	char		*arg;
175328Smckusic 	int		i;
181423Sroot 	float		fetapes;
191423Sroot 	register	struct	fstab	*dt;
201423Sroot 
211423Sroot 	time(&(spcl.c_date));
221423Sroot 
231423Sroot 	tsize = 2300L*12L*10L;
241423Sroot 	tape = TAPE;
251423Sroot 	disk = DISK;
261423Sroot 	increm = NINCREM;
275328Smckusic 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) {
285328Smckusic 		msg("TP_BSIZE must be a multiple of DEV_BSIZE\n");
295328Smckusic 		dumpabort();
305328Smckusic 	}
311423Sroot 	incno = '9';
321423Sroot 	uflag = 0;
331423Sroot 	arg = "u";
341423Sroot 	if(argc > 1) {
351423Sroot 		argv++;
361423Sroot 		argc--;
371423Sroot 		arg = *argv;
381423Sroot 		if (*arg == '-')
391423Sroot 			argc++;
401423Sroot 	}
411423Sroot 	while(*arg)
421423Sroot 	switch (*arg++) {
431463Sroot 	case 'w':
441463Sroot 		lastdump('w');		/* tell us only what has to be done */
451463Sroot 		exit(0);
461463Sroot 		break;
471423Sroot 	case 'W':			/* what to do */
481463Sroot 		lastdump('W');		/* tell us the current state of what has been done */
491423Sroot 		exit(0);		/* do nothing else */
501423Sroot 		break;
511423Sroot 
521423Sroot 	case 'J':			/* update old to new */
531423Sroot 		o_nconvert();
541423Sroot 		exit(0);		/* do nothing else */
551423Sroot 		break;
561423Sroot 
571423Sroot 	case 'f':			/* output file */
581423Sroot 		if(argc > 1) {
591423Sroot 			argv++;
601423Sroot 			argc--;
611423Sroot 			tape = *argv;
621423Sroot 		}
631423Sroot 		break;
641423Sroot 
651423Sroot 	case 'd':			/* density, in bits per inch */
661423Sroot 		if (argc > 1) {
671423Sroot 			argv++;
681423Sroot 			argc--;
691423Sroot 			density = atoi(*argv) / 10;
701423Sroot 		}
711423Sroot 		break;
721423Sroot 
731423Sroot 	case 's':			/* tape size, feet */
741423Sroot 		if(argc > 1) {
751423Sroot 			argv++;
761423Sroot 			argc--;
771423Sroot 			tsize = atol(*argv);
781423Sroot 			tsize *= 12L*10L;
791423Sroot 		}
801423Sroot 		break;
811423Sroot 
821423Sroot 	case '0':			/* dump level */
831423Sroot 	case '1':
841423Sroot 	case '2':
851423Sroot 	case '3':
861423Sroot 	case '4':
871423Sroot 	case '5':
881423Sroot 	case '6':
891423Sroot 	case '7':
901423Sroot 	case '8':
911423Sroot 	case '9':
921423Sroot 		incno = arg[-1];
931423Sroot 		break;
941423Sroot 
951423Sroot 	case 'u':			/* update /etc/dumpdates */
961423Sroot 		uflag++;
971423Sroot 		break;
981423Sroot 
991423Sroot 	case 'n':			/* notify operators */
1001423Sroot 		notify++;
1011423Sroot 		break;
1021423Sroot 
1031423Sroot 	default:
1041423Sroot 		printf("bad key '%c%'\n", arg[-1]);
1051423Sroot 		Exit(X_ABORT);
1061423Sroot 	}
1071423Sroot 	if(argc > 1) {
1081423Sroot 		argv++;
1091423Sroot 		argc--;
1101423Sroot 		disk = *argv;
1111423Sroot 	}
1126886Ssam #ifdef RDUMP
1136886Ssam 	{ char *index();
1146886Ssam 	  host = tape;
1156886Ssam 	  tape = index(host, ':');
1166886Ssam 	  if (tape == 0) {
1176886Ssam 		msg("need keyletter ``f'' and device ``host:tape''");
1186886Ssam 		exit(1);
1196886Ssam 	  }
1206886Ssam 	  *tape++ = 0;
1216886Ssam 	  if (rmthost(host) == 0)
1226886Ssam 		exit(X_ABORT);
1236886Ssam 	}
1246886Ssam #endif
1251423Sroot 	if (signal(SIGHUP, sighup) == SIG_IGN)
1261423Sroot 		signal(SIGHUP, SIG_IGN);
1271423Sroot 	if (signal(SIGTRAP, sigtrap) == SIG_IGN)
1281423Sroot 		signal(SIGTRAP, SIG_IGN);
1291423Sroot 	if (signal(SIGFPE, sigfpe) == SIG_IGN)
1301423Sroot 		signal(SIGFPE, SIG_IGN);
1311423Sroot 	if (signal(SIGBUS, sigbus) == SIG_IGN)
1321423Sroot 		signal(SIGBUS, SIG_IGN);
1331423Sroot 	if (signal(SIGSEGV, sigsegv) == SIG_IGN)
1341423Sroot 		signal(SIGSEGV, SIG_IGN);
1351423Sroot 	if (signal(SIGTERM, sigterm) == SIG_IGN)
1361423Sroot 		signal(SIGTERM, SIG_IGN);
1371423Sroot 
1381423Sroot 
1391423Sroot 	if (signal(SIGINT, interrupt) == SIG_IGN)
1401423Sroot 		signal(SIGINT, SIG_IGN);
1411423Sroot 
1421423Sroot 	set_operators();	/* /etc/group snarfed */
1431423Sroot 	getfstab();		/* /etc/fstab snarfed */
1441423Sroot 	/*
1451423Sroot 	 *	disk can be either the full special file name,
1461423Sroot 	 *	the suffix of the special file name,
1471423Sroot 	 *	the special name missing the leading '/',
1481423Sroot 	 *	the file system name with or without the leading '/'.
1491423Sroot 	 */
1501423Sroot 	dt = fstabsearch(disk);
1511423Sroot 	if (dt != 0)
1521423Sroot 		disk = rawname(dt->fs_spec);
1531423Sroot 	getitime();		/* /etc/dumpdates snarfed */
1541423Sroot 
1551423Sroot 	msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
1561423Sroot 	msg("Date of last level %c dump: %s\n", incno, prdate(spcl.c_ddate));
1571423Sroot 	msg("Dumping %s ", disk);
1581423Sroot 	if (dt != 0)
1591423Sroot 		msgtail("(%s) ", dt->fs_file);
160*8371Smckusick 	msgtail("to %s\n", tape);
1616882Ssam #ifdef RDUMP
1626882Ssam 	msgtail(" on host %s\n", host);
1636882Ssam #endif
1641423Sroot 
1651423Sroot 	fi = open(disk, 0);
1661423Sroot 	if (fi < 0) {
1671423Sroot 		msg("Cannot open %s\n", disk);
1681423Sroot 		Exit(X_ABORT);
1691423Sroot 	}
1701423Sroot 	esize = 0;
1715328Smckusic 	sblock = (struct fs *)buf;
1725328Smckusic 	sync();
1735342Smckusic 	bread(SBLOCK, sblock, SBSIZE);
1745328Smckusic 	if (sblock->fs_magic != FS_MAGIC) {
1755328Smckusic 		msg("bad sblock magic number\n");
1765328Smckusic 		dumpabort();
1775328Smckusic 	}
1785328Smckusic 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
1795328Smckusic 		TP_BSIZE);
1805328Smckusic 	clrmap = (char *)calloc(msiz, sizeof(char));
1815328Smckusic 	dirmap = (char *)calloc(msiz, sizeof(char));
1825328Smckusic 	nodmap = (char *)calloc(msiz, sizeof(char));
1831423Sroot 
1841423Sroot 	msg("mapping (Pass I) [regular files]\n");
1855328Smckusic 	pass(mark, (char *)NULL);		/* mark updates esize */
1861423Sroot 
1871423Sroot 	do {
1881423Sroot 		msg("mapping (Pass II) [directories]\n");
1891423Sroot 		nadded = 0;
1901423Sroot 		pass(add, dirmap);
1911423Sroot 	} while(nadded);
1921423Sroot 
1931423Sroot 	bmapest(clrmap);
1941423Sroot 	bmapest(nodmap);
1951423Sroot 
1961423Sroot 	fetapes =
1975328Smckusic 		(	  esize		/* blocks */
1985328Smckusic 			* TP_BSIZE	/* bytes / block */
1995328Smckusic 			* (1.0/density)	/* 0.1" / byte */
2001423Sroot 		  +
2015328Smckusic 			  esize		/* blocks */
2025328Smckusic 			* (1.0/NTREC)	/* IRG's / block */
2035328Smckusic 			* 7		/* 0.1" / IRG */
2041423Sroot 		) * (1.0 / tsize )	/* tape / 0.1" */
2051423Sroot 	;
2061423Sroot 	etapes = fetapes;		/* truncating assignment */
2071423Sroot 	etapes++;
2085328Smckusic 	/* count the nodemap on each additional tape */
2095328Smckusic 	for (i = 1; i < etapes; i++)
2105328Smckusic 		bmapest(nodmap);
2115328Smckusic 	esize += i + 10;	/* headers + 10 trailer blocks */
2121423Sroot 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
2131423Sroot 
2141423Sroot 	otape();			/* bitmap is the first to tape write */
2151423Sroot 	time(&(tstart_writing));
2161423Sroot 	bitmap(clrmap, TS_CLRI);
2171423Sroot 
2181423Sroot 	msg("dumping (Pass III) [directories]\n");
2191423Sroot 	pass(dump, dirmap);
2201423Sroot 
2211423Sroot 	msg("dumping (Pass IV) [regular files]\n");
2221423Sroot 	pass(dump, nodmap);
2231423Sroot 
2241423Sroot 	spcl.c_type = TS_END;
2256882Ssam #ifndef RDUMP
2266882Ssam 	for(i=0; i<NTREC; i++)
2271423Sroot 		spclrec();
2286882Ssam #endif
2291423Sroot 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
2301423Sroot 	msg("DUMP IS DONE\n");
2311423Sroot 
2321423Sroot 	putitime();
2336882Ssam #ifndef RDUMP
2341423Sroot 	close(to);
2356882Ssam #else
2366882Ssam 	tflush(1);
2376882Ssam #endif
2381423Sroot 	rewind();
2391423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
2401423Sroot 	Exit(X_FINOK);
2411423Sroot }
2421423Sroot 
2431423Sroot int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
2441423Sroot int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
2451423Sroot int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
2461423Sroot int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
2471423Sroot int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
2481423Sroot int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
2491423Sroot int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
2501423Sroot 
2511423Sroot sigAbort()
2521423Sroot {
2531423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
2541423Sroot 	fflush(stderr);
2551423Sroot 	fflush(stdout);
2561423Sroot 	close_rewind();
2571423Sroot 	exit(X_REWRITE);
2581423Sroot }
2591423Sroot 
2601423Sroot char *rawname(cp)
2611423Sroot 	char *cp;
2621423Sroot {
2631423Sroot 	static char rawbuf[32];
2644608Smckusic 	char *rindex();
2651423Sroot 	char *dp = rindex(cp, '/');
2661423Sroot 
2671423Sroot 	if (dp == 0)
2681423Sroot 		return (0);
2691423Sroot 	*dp = 0;
2701423Sroot 	strcpy(rawbuf, cp);
2711423Sroot 	*dp = '/';
2721423Sroot 	strcat(rawbuf, "/r");
2731423Sroot 	strcat(rawbuf, dp+1);
2741423Sroot 	return (rawbuf);
2751423Sroot }
276