xref: /csrg-svn/sbin/mount/mount.c (revision 38070)
121156Sdist /*
221156Sdist  * Copyright (c) 1980 Regents of the University of California.
321156Sdist  * All rights reserved.  The Berkeley software License Agreement
421156Sdist  * specifies the terms and conditions for redistribution.
521156Sdist  */
621156Sdist 
710811Ssam #ifndef lint
821156Sdist char copyright[] =
921156Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1021156Sdist  All rights reserved.\n";
1121156Sdist #endif not lint
121057Sbill 
1321156Sdist #ifndef lint
14*38070Smckusick static char sccsid[] = "@(#)mount.c	5.7 (Berkeley) 05/19/89";
1521156Sdist #endif not lint
1621156Sdist 
1712808Ssam #include <sys/param.h>
1835339Sbostic #include <sys/file.h>
1910811Ssam #include <fstab.h>
2012808Ssam #include <mtab.h>
2116669Ssam #include <errno.h>
2235339Sbostic #include <stdio.h>
23*38070Smckusick #include </usr/src/local/mkmsys/macklem/mkmsys/h/mount.h>
241057Sbill 
2535339Sbostic #define	BADTYPE(type) \
2635339Sbostic 	(strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \
2735339Sbostic 	    strcmp(type, FSTAB_RQ))
2835339Sbostic #define	SETTYPE(type) \
2935339Sbostic 	(!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ))
301057Sbill 
3135339Sbostic #define	MTAB	"/etc/mtab"
321057Sbill 
3335339Sbostic static struct mtab mtab[NMOUNT];
3435369Sbostic static int fake, verbose;
355073Sroot 
361057Sbill main(argc, argv)
375073Sroot 	int argc;
385073Sroot 	char **argv;
391057Sbill {
4035339Sbostic 	extern char *optarg;
4135339Sbostic 	extern int optind;
421057Sbill 	register struct mtab *mp;
4335339Sbostic 	register struct fstab *fs;
4435339Sbostic 	register int cnt;
4535372Sbostic 	int all, ch, fd, rval, sfake;
4635339Sbostic 	char *type;
471057Sbill 
4835339Sbostic 	all = 0;
4935339Sbostic 	type = NULL;
5035339Sbostic 	while ((ch = getopt(argc, argv, "afrwv")) != EOF)
5135339Sbostic 		switch((char)ch) {
5235339Sbostic 		case 'a':
5335339Sbostic 			all = 1;
5435339Sbostic 			break;
5535339Sbostic 		case 'f':
5635339Sbostic 			fake = 1;
5735339Sbostic 			break;
5835339Sbostic 		case 'r':
5912808Ssam 			type = FSTAB_RO;
6035339Sbostic 			break;
6135339Sbostic 		case 'v':
6235339Sbostic 			verbose = 1;
6335339Sbostic 			break;
6435339Sbostic 		case 'w':
6535339Sbostic 			type = FSTAB_RW;
6635339Sbostic 			break;
6735339Sbostic 		case '?':
6835339Sbostic 		default:
6935339Sbostic 			usage();
705073Sroot 		}
7135339Sbostic 	argc -= optind;
7235339Sbostic 	argv += optind;
7335339Sbostic 
7435339Sbostic 	/* NOSTRICT */
7535339Sbostic 	if ((fd = open(MTAB, O_RDONLY, 0)) >= 0) {
7635339Sbostic 		if (read(fd, (char *)mtab, NMOUNT * sizeof(struct mtab)) < 0)
7735339Sbostic 			mtaberr();
7835339Sbostic 		(void)close(fd);
791057Sbill 	}
8035339Sbostic 
814460Sroot 	if (all) {
8235369Sbostic 		rval = 0;
8335372Sbostic 		for (sfake = fake; fs = getfsent(); fake = sfake) {
8435372Sbostic 			if (BADTYPE(fs->fs_type))
8535372Sbostic 				continue;
8635372Sbostic 			/* `/' is special, it's always mounted */
8735372Sbostic 			if (!strcmp(fs->fs_file, "/"))
8835372Sbostic 				fake = 1;
8935372Sbostic 			rval |= mountfs(fs->fs_spec, fs->fs_file,
9035372Sbostic 			    type ? type : fs->fs_type);
9135372Sbostic 		}
9235341Sbostic 		exit(rval);
9335339Sbostic 	}
945073Sroot 
9535339Sbostic 	if (argc == 0) {
9635339Sbostic 		if (verbose || fake || type)
9735339Sbostic 			usage();
9835339Sbostic 		for (mp = mtab, cnt = NMOUNT; cnt--; ++mp)
9935339Sbostic 			if (*mp->m_path)
10035339Sbostic 				prmtab(mp);
1014460Sroot 		exit(0);
1021057Sbill 	}
10312808Ssam 
10435339Sbostic 	if (all)
10535339Sbostic 		usage();
10635339Sbostic 
10735339Sbostic 	if (argc == 1) {
10835339Sbostic 		if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
10935339Sbostic 			fprintf(stderr,
11035339Sbostic 			    "mount: unknown special file or file system %s.\n",
11135339Sbostic 			    *argv);
11235339Sbostic 			exit(1);
11335339Sbostic 		}
11435339Sbostic 		if (BADTYPE(fs->fs_type)) {
11535339Sbostic 			fprintf(stderr,
11635339Sbostic 			    "mount: %s has unknown file system type.\n", *argv);
11735339Sbostic 			exit(1);
11835339Sbostic 		}
11935369Sbostic 		exit(mountfs(fs->fs_spec, fs->fs_file,
12035369Sbostic 		    type ? type : fs->fs_type));
12112808Ssam 	}
1221057Sbill 
12335339Sbostic 	if (argc != 2)
12435339Sbostic 		usage();
12512808Ssam 
12635369Sbostic 	exit(mountfs(argv[0], argv[1], type ? type : "rw"));
12712808Ssam }
12812808Ssam 
12935339Sbostic static
13012808Ssam mountfs(spec, name, type)
13112808Ssam 	char *spec, *name, *type;
13212808Ssam {
13335339Sbostic 	extern int errno;
13435339Sbostic 	register struct mtab *mp, *space;
13535339Sbostic 	register int cnt;
13635339Sbostic 	register char *p;
137*38070Smckusick 	int fd, flags;
138*38070Smckusick 	struct ufs_args args;
13935339Sbostic 	char *index(), *rindex(), *strcpy();
1401057Sbill 
14112808Ssam 	if (!fake) {
142*38070Smckusick 		flags = 0;
143*38070Smckusick 		if (!strcmp(type, FSTAB_RO))
144*38070Smckusick 			flags |= M_RDONLY;
145*38070Smckusick 		args.fspec = spec;
146*38070Smckusick 		if (mount(MOUNT_UFS, name, flags, &args)) {
14735339Sbostic 			fprintf(stderr, "%s on %s: ", spec, name);
14816669Ssam 			switch (errno) {
14916669Ssam 			case EMFILE:
15035339Sbostic 				fprintf(stderr, "Mount table full\n");
15116669Ssam 				break;
15216669Ssam 			case EINVAL:
15335339Sbostic 				fprintf(stderr, "Bogus super block\n");
15416669Ssam 				break;
15516669Ssam 			default:
15635339Sbostic 				perror((char *)NULL);
15735339Sbostic 				break;
15816669Ssam 			}
15935369Sbostic 			return(1);
1604460Sroot 		}
16135339Sbostic 
16212808Ssam 		/* we don't do quotas.... */
16312808Ssam 		if (strcmp(type, FSTAB_RQ) == 0)
16412808Ssam 			type = FSTAB_RW;
1654460Sroot 	}
16635339Sbostic 
16735339Sbostic 	/* trim trailing /'s and find last component of name */
16835339Sbostic 	for (p = index(spec, '\0'); *--p == '/';);
16935339Sbostic 	*++p = '\0';
17035339Sbostic 	if (p = rindex(spec, '/')) {
17135339Sbostic 		*p = '\0';
17235339Sbostic 		spec = p + 1;
17312808Ssam 	}
17435339Sbostic 
17535339Sbostic 	for (mp = mtab, cnt = NMOUNT, space = NULL; cnt--; ++mp) {
17635339Sbostic 		if (!strcmp(mp->m_dname, spec))
17735339Sbostic 			break;
17835339Sbostic 		if (!space && !*mp->m_path)
17935339Sbostic 			space = mp;
18035339Sbostic 	}
18135339Sbostic 	if (cnt == -1) {
18235339Sbostic 		if (!space) {
18335339Sbostic 			fprintf(stderr, "mount: no more room in %s.\n", MTAB);
18435339Sbostic 			exit(1);
18535339Sbostic 		}
18635339Sbostic 		mp = space;
18735339Sbostic 	}
18835339Sbostic 
18935339Sbostic #define	DNMAX	(sizeof(mtab[0].m_dname) - 1)
19035339Sbostic #define	PNMAX	(sizeof(mtab[0].m_path) - 1)
19135339Sbostic 
19235339Sbostic 	(void)strncpy(mp->m_dname, spec, DNMAX);
19312808Ssam 	mp->m_dname[DNMAX] = '\0';
19435339Sbostic 	(void)strncpy(mp->m_path, name, PNMAX);
19512808Ssam 	mp->m_path[PNMAX] = '\0';
19635339Sbostic 	(void)strcpy(mp->m_type, type);
19735339Sbostic 
19812808Ssam 	if (verbose)
19912808Ssam 		prmtab(mp);
20035339Sbostic 
20135339Sbostic 	for (mp = mtab + NMOUNT - 1; mp > mtab && !*mp->m_path; --mp);
20235339Sbostic 	if ((fd = open(MTAB, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0)
20335339Sbostic 		mtaberr();
20435339Sbostic 	cnt = (mp - mtab + 1) * sizeof(struct mtab);
20535339Sbostic 	/* NOSTRICT */
20635339Sbostic 	if (write(fd, (char *)mtab, cnt) != cnt)
20735339Sbostic 		mtaberr();
20835339Sbostic 	(void)close(fd);
20935369Sbostic 	return(0);
2101057Sbill }
21135339Sbostic 
21235339Sbostic static
21335339Sbostic prmtab(mp)
21435339Sbostic 	register struct mtab *mp;
21535339Sbostic {
21635339Sbostic 	printf("%s on %s", mp->m_dname, mp->m_path);
21735339Sbostic 	if (!strcmp(mp->m_type, FSTAB_RO))
21835339Sbostic 		printf("\t(read-only)");
21935339Sbostic 	else if (!strcmp(mp->m_type, FSTAB_RQ))
22035339Sbostic 		printf("\t(with quotas)");
22135339Sbostic 	printf("\n");
22235339Sbostic }
22335339Sbostic 
22435339Sbostic static
22535339Sbostic mtaberr()
22635339Sbostic {
22735339Sbostic 	fprintf(stderr, "mount: %s: ", MTAB);
22835339Sbostic 	perror((char *)NULL);
22935339Sbostic 	exit(1);
23035339Sbostic }
23135339Sbostic 
23235339Sbostic static
23335339Sbostic usage()
23435339Sbostic {
23535369Sbostic 	fprintf(stderr, "usage: mount [-afrw]\nor mount [-frw] special | node\nor mount [-frw] special node\n");
23635339Sbostic 	exit(1);
23735339Sbostic }
238