S5 file system magic number (was (Re: Pixel/80 trashed fs)

Barry Shein bzs at bu-cs.BU.EDU
Tue Dec 30 10:01:36 AEST 1986


Rather than delving into why the SYSV mount command doesn't check for
magic numbers (as Guy Harris said, because not all mountable file
systems will have them) it's easy enough to implement the following
for yourself: (you could probably do the same thing in a shell script
by some sort of "dd | od | awk" kind of thing but this seemed simpler,
the shell script is left as an exercise to the reader :-)

	-Barry Shein, Boston University

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/filsys.h>
#include <fcntl.h>

/*
 * Simple hack to check a SYSV file system for a magic number
 * and then mount it if present. If -f flag is present it skips
 * the check (or you could just use /etc/mount directly.)
 *
 * execs /etc/mount to do the actual mount, except for leading
 * optional -f (which is elided) just passes args as is. Could add
 * additional checks easily.
 * -Barry Shein (BZS at BU-CS.BU.EDU)
 */

#define MOUNT "/etc/mount"
#define MOUNT0 "mount"

main(argc,argv) int argc ; char **argv ;
{
	int fd ;
	struct filsys s ;
	char *prog;
	char *special, *dir, *ronly;
	int force = 0;
	int i;

	prog = *argv;	/* name we were run under, for error msgs */
	/*
	 * check for leading "-f" flag to force the mount
	 */
	if((argc > 1) && (strcmp(argv[1],"-f") == 0)) {
		--argc;
		++argv;
		force = 1;
	}
	/* must be at least "prog special dir" */
	if(argc < 3 ) {
		fprintf(stderr,"Usage: %s [-f] special dir [-r]\n",*prog);
		exit(1) ;
	}
	special = argv[1];
	dir = argv[2];
	ronly = (argc == 4) ? argv[3] : NULL;
	if(force) goto doit;	/* they gave a -f */

	if((fd = open(special,O_RDONLY)) < 0) {
		perror(argv[1]);
		exit(1);
	}
	if(lseek(fd,512L,0) < 0) {
		perror("seek");
		exit(1);
	}
	/* read in the super-block */
	if((i = read(fd,&s,sizeof s)) != sizeof s) {
		if(i < 0)
			perror("read");
		else fprintf(stderr,"short read?\n");
		exit(1);
	}
	if(s.s_magic != FsMAGIC) {
		fprintf(stderr,"%s: bad magic number! use -f to mount anyhow\n",
			special);
		exit(1);
	}
doit:
	/* do the actual mount */
	execl(MOUNT,MOUNT0,special,dir,ronly,0);
	fprintf(stderr,"can't run %s?!\n",MOUNT);
	exit(1);
}



More information about the Comp.unix.questions mailing list