xref: /csrg-svn/sbin/mount/mount.c (revision 39521)
121156Sdist /*
239322Smckusick  * Copyright (c) 1980, 1989 The Regents of the University of California.
339322Smckusick  * All rights reserved.
439322Smckusick  *
539322Smckusick  * Redistribution and use in source and binary forms are permitted
639322Smckusick  * provided that the above copyright notice and this paragraph are
739322Smckusick  * duplicated in all such forms and that any documentation,
839322Smckusick  * advertising materials, and other materials related to such
939322Smckusick  * distribution and use acknowledge that the software was developed
1039322Smckusick  * by the University of California, Berkeley.  The name of the
1139322Smckusick  * University may not be used to endorse or promote products derived
1239322Smckusick  * from this software without specific prior written permission.
1339322Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1439322Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1539322Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621156Sdist  */
1721156Sdist 
1810811Ssam #ifndef lint
1921156Sdist char copyright[] =
2039322Smckusick "@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\
2121156Sdist  All rights reserved.\n";
2239322Smckusick #endif /* not lint */
231057Sbill 
2421156Sdist #ifndef lint
25*39521Smckusick static char sccsid[] = "@(#)mount.c	5.19 (Berkeley) 11/13/89";
2639322Smckusick #endif /* not lint */
2721156Sdist 
2839131Smckusick #include "pathnames.h"
2912808Ssam #include <sys/param.h>
3035339Sbostic #include <sys/file.h>
3138445Smckusick #include <sys/time.h>
3239133Smckusick #include <sys/wait.h>
3310811Ssam #include <fstab.h>
3416669Ssam #include <errno.h>
3535339Sbostic #include <stdio.h>
3638445Smckusick #include <strings.h>
3738445Smckusick #include <sys/dir.h>
3838445Smckusick #include <sys/uio.h>
3938445Smckusick #include <sys/namei.h>
4038445Smckusick #include <sys/mount.h>
4138445Smckusick #ifdef NFS
4238445Smckusick #include <sys/socket.h>
4338445Smckusick #include <sys/socketvar.h>
4438445Smckusick #include <netdb.h>
4538445Smckusick #include <rpc/rpc.h>
4638445Smckusick #include <rpc/pmap_clnt.h>
4738445Smckusick #include <rpc/pmap_prot.h>
4838445Smckusick #include <nfs/rpcv2.h>
4938445Smckusick #include <nfs/nfsv2.h>
5038445Smckusick #include <nfs/nfs.h>
5138445Smckusick #endif
521057Sbill 
5335339Sbostic #define	BADTYPE(type) \
5435339Sbostic 	(strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \
5535339Sbostic 	    strcmp(type, FSTAB_RQ))
5635339Sbostic #define	SETTYPE(type) \
5735339Sbostic 	(!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ))
581057Sbill 
5939333Smckusick int fake, verbose, updateflg, mnttype;
6039322Smckusick char *mntname, **envp;
6139131Smckusick 
6238445Smckusick #ifdef NFS
6338445Smckusick int xdr_dir(), xdr_fh();
6438632Smckusick char *getnfsargs();
6538445Smckusick struct nfs_args nfsargs = {
6638445Smckusick 	(struct sockaddr_in *)0,
6738445Smckusick 	(nfsv2fh_t *)0,
6838445Smckusick 	0,
6938445Smckusick 	NFS_WSIZE,
7038445Smckusick 	NFS_RSIZE,
7138445Smckusick 	NFS_TIMEO,
7238445Smckusick 	NFS_RETRANS,
7338445Smckusick 	(char *)0,
7438445Smckusick };
755073Sroot 
7638445Smckusick struct nfhret {
7738445Smckusick 	u_long	stat;
7838445Smckusick 	nfsv2fh_t nfh;
7938445Smckusick };
8038445Smckusick int retrycnt = 10000;
8138445Smckusick #define	BGRND	1
8238445Smckusick #define	ISBGRND	2
8338445Smckusick int opflags = 0;
8438445Smckusick #endif
8538445Smckusick 
8639131Smckusick main(argc, argv, arge)
875073Sroot 	int argc;
885073Sroot 	char **argv;
8939131Smckusick 	char **arge;
901057Sbill {
9135339Sbostic 	extern char *optarg;
9235339Sbostic 	extern int optind;
9335339Sbostic 	register struct fstab *fs;
9435339Sbostic 	register int cnt;
9539333Smckusick 	int all, ch, rval, flags, i;
9638632Smckusick 	long mntsize;
9739465Smckusick 	struct statfs *mntbuf, *getmntpt();
9839131Smckusick 	char *type, *options = NULL;
991057Sbill 
10039131Smckusick 	envp = arge;
10135339Sbostic 	all = 0;
10235339Sbostic 	type = NULL;
10338445Smckusick 	mnttype = MOUNT_UFS;
10439322Smckusick 	mntname = "ufs";
10539333Smckusick 	while ((ch = getopt(argc, argv, "afrwuvt:o:")) != EOF)
10635339Sbostic 		switch((char)ch) {
10735339Sbostic 		case 'a':
10835339Sbostic 			all = 1;
10935339Sbostic 			break;
11035339Sbostic 		case 'f':
11135339Sbostic 			fake = 1;
11235339Sbostic 			break;
11335339Sbostic 		case 'r':
11412808Ssam 			type = FSTAB_RO;
11535339Sbostic 			break;
11639333Smckusick 		case 'u':
11739333Smckusick 			updateflg = M_UPDATE;
11839333Smckusick 			break;
11935339Sbostic 		case 'v':
12035339Sbostic 			verbose = 1;
12135339Sbostic 			break;
12235339Sbostic 		case 'w':
12335339Sbostic 			type = FSTAB_RW;
12435339Sbostic 			break;
12539131Smckusick 		case 'o':
12639131Smckusick 			options = optarg;
12739131Smckusick 			break;
12838445Smckusick 		case 't':
12939322Smckusick 			mnttype = getmnttype(optarg);
13039322Smckusick 			break;
13135339Sbostic 		case '?':
13235339Sbostic 		default:
13335339Sbostic 			usage();
13439131Smckusick 			/* NOTREACHED */
1355073Sroot 		}
13635339Sbostic 	argc -= optind;
13735339Sbostic 	argv += optind;
13835339Sbostic 
13935339Sbostic 	/* NOSTRICT */
14035339Sbostic 
1414460Sroot 	if (all) {
14235369Sbostic 		rval = 0;
14339333Smckusick 		while (fs = getfsent()) {
14435372Sbostic 			if (BADTYPE(fs->fs_type))
14535372Sbostic 				continue;
14635372Sbostic 			/* `/' is special, it's always mounted */
14735372Sbostic 			if (!strcmp(fs->fs_file, "/"))
14839333Smckusick 				flags = M_UPDATE;
14939333Smckusick 			else
15039333Smckusick 				flags = updateflg;
15139333Smckusick 			mnttype = getmnttype(fs->fs_vfstype);
15239333Smckusick 			rval |= mountfs(fs->fs_spec, fs->fs_file, flags,
15339333Smckusick 			    type, options, fs->fs_mntops);
15435372Sbostic 		}
15535341Sbostic 		exit(rval);
15635339Sbostic 	}
1575073Sroot 
15835339Sbostic 	if (argc == 0) {
15935339Sbostic 		if (verbose || fake || type)
16035339Sbostic 			usage();
16139319Smckusick 		if ((mntsize = getmntinfo(&mntbuf)) == 0) {
16239319Smckusick 			fprintf(stderr,
16339319Smckusick 				"mount: cannot get mount information\n");
16438632Smckusick 			exit(1);
16538632Smckusick 		}
16639316Smckusick 		for (i = 0; i < mntsize; i++)
16738632Smckusick 			prmount(mntbuf[i].f_mntfromname, mntbuf[i].f_mntonname,
16839316Smckusick 				mntbuf[i].f_flags);
1694460Sroot 		exit(0);
1701057Sbill 	}
17112808Ssam 
17239465Smckusick 	if (argc == 1 && updateflg) {
17339465Smckusick 		if ((mntbuf = getmntpt(*argv)) == NULL) {
17439465Smckusick 			fprintf(stderr,
17539465Smckusick 			    "mount: unknown special file or file system %s.\n",
17639465Smckusick 			    *argv);
17739465Smckusick 			exit(1);
17839465Smckusick 		}
17939465Smckusick 		mnttype = mntbuf->f_type;
18039465Smckusick 		exit(mountfs(mntbuf->f_mntfromname, mntbuf->f_mntonname,
18139465Smckusick 		    updateflg, type, options, NULL));
18239465Smckusick 	}
18339465Smckusick 
18435339Sbostic 	if (argc == 1) {
18535339Sbostic 		if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
18635339Sbostic 			fprintf(stderr,
18735339Sbostic 			    "mount: unknown special file or file system %s.\n",
18835339Sbostic 			    *argv);
18935339Sbostic 			exit(1);
19035339Sbostic 		}
19135339Sbostic 		if (BADTYPE(fs->fs_type)) {
19235339Sbostic 			fprintf(stderr,
19335339Sbostic 			    "mount: %s has unknown file system type.\n", *argv);
19435339Sbostic 			exit(1);
19535339Sbostic 		}
19639333Smckusick 		mnttype = getmnttype(fs->fs_vfstype);
19739333Smckusick 		exit(mountfs(fs->fs_spec, fs->fs_file, updateflg,
19839333Smckusick 		    type, options, fs->fs_mntops));
19912808Ssam 	}
2001057Sbill 
20135339Sbostic 	if (argc != 2)
20235339Sbostic 		usage();
20312808Ssam 
20439333Smckusick 	exit(mountfs(argv[0], argv[1], updateflg, type, options, NULL));
20512808Ssam }
20612808Ssam 
20739333Smckusick mountfs(spec, name, flags, type, options, mntopts)
20839131Smckusick 	char *spec, *name, *type, *options, *mntopts;
20939333Smckusick 	int flags;
21012808Ssam {
21135339Sbostic 	extern int errno;
21235339Sbostic 	register int cnt;
21339333Smckusick 	int argc, status, i;
21438070Smckusick 	struct ufs_args args;
21539131Smckusick 	char *argp, *argv[50];
21639322Smckusick 	char execname[MAXPATHLEN + 1], flagval[12];
2171057Sbill 
21839333Smckusick 	if (mntopts)
21939333Smckusick 		getstdopts(mntopts, &flags);
22039316Smckusick 	if (options)
22139316Smckusick 		getstdopts(options, &flags);
22239333Smckusick 	if (type)
22339333Smckusick 		getstdopts(type, &flags);
22439316Smckusick 	switch (mnttype) {
22539316Smckusick 	case MOUNT_UFS:
22639333Smckusick 		if (mntopts)
22739333Smckusick 			getufsopts(mntopts, &flags);
22839316Smckusick 		if (options)
22939316Smckusick 			getufsopts(options, &flags);
23039316Smckusick 		args.fspec = spec;
23139316Smckusick 		argp = (caddr_t)&args;
23239316Smckusick 		break;
23338632Smckusick 
23438632Smckusick #ifdef NFS
23539316Smckusick 	case MOUNT_NFS:
23639333Smckusick 		if (mntopts)
23739333Smckusick 			getnfsopts(mntopts, &nfsargs, &opflags, &retrycnt);
23839316Smckusick 		if (options)
23939333Smckusick 			getnfsopts(options, &nfsargs, &opflags, &retrycnt);
24039316Smckusick 		if (argp = getnfsargs(spec, name, type))
24139316Smckusick 			break;
24239316Smckusick 		return (1);
24338632Smckusick #endif /* NFS */
24438632Smckusick 
24539316Smckusick 	case MOUNT_MFS:
24639322Smckusick 	default:
24739322Smckusick 		argv[0] = mntname;
24839329Smckusick 		argc = 1;
24939322Smckusick 		if (flags) {
25039322Smckusick 			argv[argc++] = "-F";
25139322Smckusick 			sprintf(flagval, "%d", flags);
25239322Smckusick 			argv[argc++] = flagval;
25339322Smckusick 		}
25439333Smckusick 		if (mntopts)
25539333Smckusick 			argc += getexecopts(mntopts, &argv[argc]);
25639329Smckusick 		if (options)
25739329Smckusick 			argc += getexecopts(options, &argv[argc]);
25839316Smckusick 		argv[argc++] = spec;
25939316Smckusick 		argv[argc++] = name;
260*39521Smckusick 		argv[argc++] = NULL;
26139322Smckusick 		sprintf(execname, "%s/%s", _PATH_EXECDIR, mntname);
26239316Smckusick 		if (verbose) {
26339322Smckusick 			printf("exec: %s", execname);
26439322Smckusick 			for (i = 1; i < argc; i++)
26539316Smckusick 				printf(" %s", argv[i]);
26639316Smckusick 			printf("\n");
26739316Smckusick 		}
26839316Smckusick 		if (fake)
26939316Smckusick 			break;
27039316Smckusick 		if (i = vfork()) {
27139316Smckusick 			if (i == -1) {
27239322Smckusick 				perror("mount: vfork starting file system");
27339316Smckusick 				return (1);
27439131Smckusick 			}
27539316Smckusick 			if (waitpid(i, &status, 0) != -1 &&
27639316Smckusick 			    WIFEXITED(status) &&
27739316Smckusick 			    WEXITSTATUS(status) != 0)
27839316Smckusick 				return (WEXITSTATUS(status));
27939322Smckusick 			spec = mntname;
28039316Smckusick 			goto out;
28139316Smckusick 		}
28239322Smckusick 		execve(execname, argv, envp);
28339322Smckusick 		perror(execname);
28439316Smckusick 		exit (1);
28539316Smckusick 		/* NOTREACHED */
28638632Smckusick 
28739316Smckusick 	}
28839316Smckusick 	if (!fake && mount(mnttype, name, flags, argp)) {
28939316Smckusick 		if (opflags & ISBGRND)
29039316Smckusick 			exit(1);
29139316Smckusick 		fprintf(stderr, "%s on %s: ", spec, name);
29239316Smckusick 		switch (errno) {
29339316Smckusick 		case EMFILE:
29439316Smckusick 			fprintf(stderr, "Mount table full\n");
29539316Smckusick 			break;
29639316Smckusick 		case EINVAL:
29739333Smckusick 			if (flags & M_UPDATE)
29839333Smckusick 				fprintf(stderr, "Specified device does %s\n",
29939333Smckusick 					"not match mounted device");
30039333Smckusick 			else
30139333Smckusick 				fprintf(stderr, "Bogus super block\n");
30239316Smckusick 			break;
30339333Smckusick 		case EOPNOTSUPP:
30439333Smckusick 			fprintf(stderr, "Operation not supported\n");
30539333Smckusick 			break;
30639316Smckusick 		default:
30739316Smckusick 			perror((char *)NULL);
30839316Smckusick 			break;
3094460Sroot 		}
31039316Smckusick 		return(1);
3114460Sroot 	}
31235339Sbostic 
31339131Smckusick out:
31412808Ssam 	if (verbose)
31539316Smckusick 		prmount(spec, name, flags);
31635339Sbostic 
31738445Smckusick 	if (opflags & ISBGRND)
31838445Smckusick 		exit();
31938445Smckusick 	else
32038445Smckusick 		return(0);
3211057Sbill }
32235339Sbostic 
32335339Sbostic static
32439316Smckusick prmount(spec, name, flags)
32539316Smckusick 	char *spec, *name;
32639316Smckusick 	long flags;
32735339Sbostic {
32838632Smckusick 	register char *root;
32938632Smckusick 
33038445Smckusick 	if (opflags & ISBGRND)
33138445Smckusick 		return;
33238632Smckusick 	/*
33338632Smckusick 	 * trim trailing /'s and find last component of name
33438632Smckusick 	 */
33538632Smckusick 	for (root = index(spec, '\0'); *--root == '/';)
33638632Smckusick 		/* void */;
33738632Smckusick 	*++root = '\0';
33838632Smckusick 	if (root = rindex(spec, '/'))
33938632Smckusick 		spec = root + 1;
34038632Smckusick 	printf("%s on %s", spec, name);
34139316Smckusick 	if (flags & M_RDONLY)
34239316Smckusick 		printf(" (read-only)");
34339316Smckusick 	if (flags & M_NOEXEC)
34439316Smckusick 		printf(" (noexec)");
34539316Smckusick 	if (flags & M_NOSUID)
34639316Smckusick 		printf(" (nosuid)");
34739316Smckusick 	if (flags & M_NODEV)
34839316Smckusick 		printf(" (nodev)");
34939316Smckusick 	if (flags & M_SYNCHRONOUS)
35039316Smckusick 		printf(" (synchronous)");
35139333Smckusick 	if (flags & M_UPDATE)
35239333Smckusick 		printf(" (update only)");
35335339Sbostic 	printf("\n");
35435339Sbostic }
35535339Sbostic 
35639133Smckusick getmnttype(fstype)
35739133Smckusick 	char *fstype;
35839133Smckusick {
35939133Smckusick 
36039322Smckusick 	mntname = fstype;
36139133Smckusick 	if (!strcmp(fstype, "ufs"))
36239133Smckusick 		return (MOUNT_UFS);
36339133Smckusick 	if (!strcmp(fstype, "nfs"))
36439133Smckusick 		return (MOUNT_NFS);
36539133Smckusick 	if (!strcmp(fstype, "mfs"))
36639133Smckusick 		return (MOUNT_MFS);
36739133Smckusick 	return (0);
36839133Smckusick }
36939133Smckusick 
37038632Smckusick usage()
37135339Sbostic {
37239333Smckusick 	fprintf(stderr, "usage: mount [-afurw]\nor mount [-furw] special | node\nor mount [-furw] special node\n");
37335339Sbostic 	exit(1);
37435339Sbostic }
37535339Sbostic 
37639316Smckusick getstdopts(options, flagp)
37739316Smckusick 	char *options;
37839316Smckusick 	long *flagp;
37939316Smckusick {
38039316Smckusick 	register char *opt;
38139316Smckusick 	int negative;
38239316Smckusick 	char *optbuf[BUFSIZ], *strtok();
38339316Smckusick 
38439316Smckusick 	strcpy(optbuf, options);
38539316Smckusick 	for (opt = strtok(optbuf, ","); opt; opt = strtok(NULL, ",")) {
38639316Smckusick 		if (opt[0] == 'n' && opt[1] == 'o') {
38739316Smckusick 			negative++;
38839316Smckusick 			opt += 2;
38939316Smckusick 		} else {
39039316Smckusick 			negative = 0;
39139316Smckusick 		}
39239333Smckusick 		if (!negative && !strcasecmp(opt, FSTAB_RO)) {
39339333Smckusick 			*flagp |= M_RDONLY;
39439333Smckusick 			continue;
39539333Smckusick 		}
39639333Smckusick 		if (!negative && !strcasecmp(opt, FSTAB_RW)) {
39739333Smckusick 			*flagp &= ~M_RDONLY;
39839333Smckusick 			continue;
39939333Smckusick 		}
40039316Smckusick 		if (!strcasecmp(opt, "exec")) {
40139316Smckusick 			if (negative)
40239316Smckusick 				*flagp |= M_NOEXEC;
40339333Smckusick 			else
40439333Smckusick 				*flagp &= ~M_NOEXEC;
40539316Smckusick 			continue;
40639316Smckusick 		}
40739316Smckusick 		if (!strcasecmp(opt, "suid")) {
40839316Smckusick 			if (negative)
40939316Smckusick 				*flagp |= M_NOSUID;
41039333Smckusick 			else
41139333Smckusick 				*flagp &= ~M_NOSUID;
41239316Smckusick 			continue;
41339316Smckusick 		}
41439316Smckusick 		if (!strcasecmp(opt, "dev")) {
41539316Smckusick 			if (negative)
41639316Smckusick 				*flagp |= M_NODEV;
41739333Smckusick 			else
41839333Smckusick 				*flagp &= ~M_NODEV;
41939316Smckusick 			continue;
42039316Smckusick 		}
42139316Smckusick 		if (!strcasecmp(opt, "synchronous")) {
42239316Smckusick 			if (!negative)
42339316Smckusick 				*flagp |= M_SYNCHRONOUS;
42439333Smckusick 			else
42539333Smckusick 				*flagp &= ~M_SYNCHRONOUS;
42639316Smckusick 			continue;
42739316Smckusick 		}
42839316Smckusick 	}
42939316Smckusick }
43039316Smckusick 
43139131Smckusick getufsopts(options, flagp)
43239131Smckusick 	char *options;
43339131Smckusick 	long *flagp;
43439131Smckusick {
43539131Smckusick 
43639131Smckusick 	return;
43739131Smckusick }
43839131Smckusick 
43939329Smckusick getexecopts(options, argv)
44039131Smckusick 	char *options;
44139131Smckusick 	char **argv;
44239131Smckusick {
44339131Smckusick 	register int argc = 0;
44439131Smckusick 	register char *opt;
44539131Smckusick 	char *strtok();
44639131Smckusick 
44739131Smckusick 	for (opt = strtok(options, ","); opt; opt = strtok(NULL, ",")) {
44839131Smckusick 		if (opt[0] != '-')
44939131Smckusick 			continue;
45039131Smckusick 		argv[argc++] = opt;
45139131Smckusick 		if (opt[2] == '\0' || opt[2] != '=')
45239131Smckusick 			continue;
45339131Smckusick 		opt[2] = '\0';
45439131Smckusick 		argv[argc++] = &opt[3];
45539131Smckusick 	}
45639131Smckusick 	return (argc);
45739131Smckusick }
45839131Smckusick 
45939465Smckusick struct statfs *
46039465Smckusick getmntpt(name)
46139465Smckusick 	char *name;
46239465Smckusick {
46339465Smckusick 	long mntsize;
46439465Smckusick 	register long i;
46539465Smckusick 	struct statfs *mntbuf;
46639465Smckusick 
46739465Smckusick 	mntsize = getmntinfo(&mntbuf);
46839465Smckusick 	for (i = 0; i < mntsize; i++) {
46939465Smckusick 		if (!strcmp(mntbuf[i].f_mntfromname, name) ||
47039465Smckusick 		    !strcmp(mntbuf[i].f_mntonname, name))
47139465Smckusick 			return (&mntbuf[i]);
47239465Smckusick 	}
47339465Smckusick 	return ((struct statfs *)0);
47439465Smckusick }
47539465Smckusick 
47638632Smckusick #ifdef NFS
47739131Smckusick /*
47839131Smckusick  * Handle the getoption arg.
47939131Smckusick  * Essentially update "opflags", "retrycnt" and "nfsargs"
48039131Smckusick  */
48139131Smckusick getnfsopts(optarg, nfsargsp, opflagsp, retrycntp)
48239131Smckusick 	char *optarg;
48339131Smckusick 	struct nfs_args *nfsargsp;
48439131Smckusick 	int *opflagsp;
48539131Smckusick 	int *retrycntp;
48639131Smckusick {
48739131Smckusick 	register char *cp, *nextcp;
48839131Smckusick 	int num;
48939131Smckusick 	char *nump;
49039131Smckusick 
49139131Smckusick 	cp = optarg;
49239131Smckusick 	while (cp != NULL && *cp != '\0') {
49339131Smckusick 		if ((nextcp = index(cp, ',')) != NULL)
49439131Smckusick 			*nextcp++ = '\0';
49539131Smckusick 		if ((nump = index(cp, '=')) != NULL) {
49639131Smckusick 			*nump++ = '\0';
49739131Smckusick 			num = atoi(nump);
49839131Smckusick 		} else
49939131Smckusick 			num = -1;
50039131Smckusick 		/*
50139131Smckusick 		 * Just test for a string match and do it
50239131Smckusick 		 */
50339131Smckusick 		if (!strcmp(cp, "bg")) {
50439131Smckusick 			*opflagsp |= BGRND;
50539131Smckusick 		} else if (!strcmp(cp, "soft")) {
50639131Smckusick 			nfsargsp->flags |= NFSMNT_SOFT;
50739131Smckusick 		} else if (!strcmp(cp, "intr")) {
50839131Smckusick 			nfsargsp->flags |= NFSMNT_INT;
50939131Smckusick 		} else if (!strcmp(cp, "retry") && num > 0) {
51039131Smckusick 			*retrycntp = num;
51139131Smckusick 		} else if (!strcmp(cp, "rsize") && num > 0) {
51239131Smckusick 			nfsargsp->rsize = num;
51339131Smckusick 			nfsargsp->flags |= NFSMNT_RSIZE;
51439131Smckusick 		} else if (!strcmp(cp, "wsize") && num > 0) {
51539131Smckusick 			nfsargsp->wsize = num;
51639131Smckusick 			nfsargsp->flags |= NFSMNT_WSIZE;
51739131Smckusick 		} else if (!strcmp(cp, "timeo") && num > 0) {
51839131Smckusick 			nfsargsp->timeo = num;
51939131Smckusick 			nfsargsp->flags |= NFSMNT_TIMEO;
52039131Smckusick 		} else if (!strcmp(cp, "retrans") && num > 0) {
52139131Smckusick 			nfsargsp->retrans = num;
52239131Smckusick 			nfsargsp->flags |= NFSMNT_RETRANS;
52339131Smckusick 		}
52439131Smckusick 		cp = nextcp;
52539131Smckusick 	}
52639131Smckusick }
52739131Smckusick 
52838632Smckusick char *
52938632Smckusick getnfsargs(spec)
53038632Smckusick 	char *spec;
53135339Sbostic {
53238632Smckusick 	register CLIENT *clp;
53338632Smckusick 	struct hostent *hp;
53438632Smckusick 	struct sockaddr_in saddr;
53538632Smckusick 	struct timeval pertry, try;
53638632Smckusick 	enum clnt_stat clnt_stat;
53738632Smckusick 	int so = RPC_ANYSOCK;
53838632Smckusick 	char *hostp, *delimp;
53938632Smckusick 	u_short tport;
54038632Smckusick 	struct nfhret nfhret;
54138632Smckusick 	char nam[MNAMELEN + 1];
54238632Smckusick 
54338632Smckusick 	strncpy(nam, spec, MNAMELEN);
54438632Smckusick 	nam[MNAMELEN] = '\0';
54538632Smckusick 	if ((delimp = index(spec, '@')) != NULL) {
54638632Smckusick 		hostp = delimp + 1;
54738632Smckusick 	} else if ((delimp = index(spec, ':')) != NULL) {
54838632Smckusick 		hostp = spec;
54938632Smckusick 		spec = delimp + 1;
55038632Smckusick 	} else {
55138632Smckusick 		fprintf(stderr,
55238632Smckusick 		    "No <host>:<dirpath> or <dirpath>@<host> spec\n");
55338632Smckusick 		return (0);
55438632Smckusick 	}
55538632Smckusick 	*delimp = '\0';
55638632Smckusick 	if ((hp = gethostbyname(hostp)) == NULL) {
55738632Smckusick 		fprintf(stderr, "Can't get net id for host\n");
55838632Smckusick 		return (0);
55938632Smckusick 	}
56038632Smckusick 	bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
56138632Smckusick 	nfhret.stat = EACCES;	/* Mark not yet successful */
56238632Smckusick 	while (retrycnt > 0) {
56338632Smckusick 		saddr.sin_family = AF_INET;
56438632Smckusick 		saddr.sin_port = htons(PMAPPORT);
56538632Smckusick 		if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
56638632Smckusick 		    NFS_VER2, IPPROTO_UDP)) == 0) {
56738632Smckusick 			if ((opflags & ISBGRND) == 0)
56838632Smckusick 				clnt_pcreateerror("NFS Portmap");
56938632Smckusick 		} else {
57038632Smckusick 			saddr.sin_port = 0;
57138632Smckusick 			pertry.tv_sec = 10;
57238632Smckusick 			pertry.tv_usec = 0;
57338632Smckusick 			if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
57438632Smckusick 			    RPCMNT_VER1, pertry, &so)) == NULL) {
57538632Smckusick 				if ((opflags & ISBGRND) == 0)
57638632Smckusick 					clnt_pcreateerror("Cannot MNT PRC");
57738632Smckusick 			} else {
57838632Smckusick 				clp->cl_auth = authunix_create_default();
57938632Smckusick 				try.tv_sec = 10;
58038632Smckusick 				try.tv_usec = 0;
58138632Smckusick 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
58238632Smckusick 				    xdr_dir, spec, xdr_fh, &nfhret, try);
58338632Smckusick 				if (clnt_stat != RPC_SUCCESS) {
58438632Smckusick 					if ((opflags & ISBGRND) == 0)
58538632Smckusick 						clnt_perror(clp, "Bad MNT RPC");
58638632Smckusick 				} else {
58738632Smckusick 					auth_destroy(clp->cl_auth);
58838632Smckusick 					clnt_destroy(clp);
58938632Smckusick 					retrycnt = 0;
59038632Smckusick 				}
59138632Smckusick 			}
59238632Smckusick 		}
59338632Smckusick 		if (--retrycnt > 0) {
59438632Smckusick 			if (opflags & BGRND) {
59538632Smckusick 				opflags &= ~BGRND;
59638632Smckusick 				if (fork())
59738632Smckusick 					return (0);
59838632Smckusick 				else
59938632Smckusick 					opflags |= ISBGRND;
60038632Smckusick 			}
60138632Smckusick 			sleep(10);
60238632Smckusick 		}
60338632Smckusick 	}
60438632Smckusick 	if (nfhret.stat) {
60538632Smckusick 		if (opflags & ISBGRND)
60638632Smckusick 			exit(1);
607*39521Smckusick 		fprintf(stderr, "Can't access %s: ", spec);
608*39521Smckusick 		errno = nfhret.stat;
609*39521Smckusick 		perror(NULL);
61038632Smckusick 		return (0);
61138632Smckusick 	}
61238632Smckusick 	saddr.sin_port = htons(tport);
61338632Smckusick 	nfsargs.addr = &saddr;
61438632Smckusick 	nfsargs.fh = &nfhret.nfh;
61538632Smckusick 	nfsargs.hostname = nam;
61638632Smckusick 	return ((caddr_t)&nfsargs);
61735339Sbostic }
61838445Smckusick 
61938445Smckusick /*
62038445Smckusick  * xdr routines for mount rpc's
62138445Smckusick  */
62238445Smckusick xdr_dir(xdrsp, dirp)
62338445Smckusick 	XDR *xdrsp;
62438445Smckusick 	char *dirp;
62538445Smckusick {
62638445Smckusick 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
62738445Smckusick }
62838445Smckusick 
62938445Smckusick xdr_fh(xdrsp, np)
63038445Smckusick 	XDR *xdrsp;
63138445Smckusick 	struct nfhret *np;
63238445Smckusick {
63338445Smckusick 	if (!xdr_u_long(xdrsp, &(np->stat)))
63438445Smckusick 		return (0);
63538445Smckusick 	if (np->stat)
63638445Smckusick 		return (1);
63738445Smckusick 	return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
63838445Smckusick }
63938632Smckusick #endif /* NFS */
640