xref: /csrg-svn/sbin/mount/mount.c (revision 39333)
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*39333Smckusick static char sccsid[] = "@(#)mount.c	5.17 (Berkeley) 10/19/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 
59*39333Smckusick 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;
95*39333Smckusick 	int all, ch, rval, flags, i;
9638632Smckusick 	long mntsize;
9739319Smckusick 	struct statfs *mntbuf;
9839131Smckusick 	char *type, *options = NULL;
991057Sbill 
10039131Smckusick 	envp = arge;
10135339Sbostic 	all = 0;
10235339Sbostic 	type = NULL;
10338445Smckusick 	mnttype = MOUNT_UFS;
10439322Smckusick 	mntname = "ufs";
105*39333Smckusick 	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;
116*39333Smckusick 		case 'u':
117*39333Smckusick 			updateflg = M_UPDATE;
118*39333Smckusick 			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;
143*39333Smckusick 		while (fs = getfsent()) {
14435372Sbostic 			if (BADTYPE(fs->fs_type))
14535372Sbostic 				continue;
14635372Sbostic 			/* `/' is special, it's always mounted */
14735372Sbostic 			if (!strcmp(fs->fs_file, "/"))
148*39333Smckusick 				flags = M_UPDATE;
149*39333Smckusick 			else
150*39333Smckusick 				flags = updateflg;
151*39333Smckusick 			mnttype = getmnttype(fs->fs_vfstype);
152*39333Smckusick 			rval |= mountfs(fs->fs_spec, fs->fs_file, flags,
153*39333Smckusick 			    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 
17235339Sbostic 	if (argc == 1) {
17335339Sbostic 		if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
17435339Sbostic 			fprintf(stderr,
17535339Sbostic 			    "mount: unknown special file or file system %s.\n",
17635339Sbostic 			    *argv);
17735339Sbostic 			exit(1);
17835339Sbostic 		}
17935339Sbostic 		if (BADTYPE(fs->fs_type)) {
18035339Sbostic 			fprintf(stderr,
18135339Sbostic 			    "mount: %s has unknown file system type.\n", *argv);
18235339Sbostic 			exit(1);
18335339Sbostic 		}
184*39333Smckusick 		mnttype = getmnttype(fs->fs_vfstype);
185*39333Smckusick 		exit(mountfs(fs->fs_spec, fs->fs_file, updateflg,
186*39333Smckusick 		    type, options, fs->fs_mntops));
18712808Ssam 	}
1881057Sbill 
18935339Sbostic 	if (argc != 2)
19035339Sbostic 		usage();
19112808Ssam 
192*39333Smckusick 	exit(mountfs(argv[0], argv[1], updateflg, type, options, NULL));
19312808Ssam }
19412808Ssam 
195*39333Smckusick mountfs(spec, name, flags, type, options, mntopts)
19639131Smckusick 	char *spec, *name, *type, *options, *mntopts;
197*39333Smckusick 	int flags;
19812808Ssam {
19935339Sbostic 	extern int errno;
20035339Sbostic 	register int cnt;
201*39333Smckusick 	int argc, status, i;
20238070Smckusick 	struct ufs_args args;
20339131Smckusick 	char *argp, *argv[50];
20439322Smckusick 	char execname[MAXPATHLEN + 1], flagval[12];
2051057Sbill 
206*39333Smckusick 	if (mntopts)
207*39333Smckusick 		getstdopts(mntopts, &flags);
20839316Smckusick 	if (options)
20939316Smckusick 		getstdopts(options, &flags);
210*39333Smckusick 	if (type)
211*39333Smckusick 		getstdopts(type, &flags);
21239316Smckusick 	switch (mnttype) {
21339316Smckusick 	case MOUNT_UFS:
214*39333Smckusick 		if (mntopts)
215*39333Smckusick 			getufsopts(mntopts, &flags);
21639316Smckusick 		if (options)
21739316Smckusick 			getufsopts(options, &flags);
21839316Smckusick 		args.fspec = spec;
21939316Smckusick 		argp = (caddr_t)&args;
22039316Smckusick 		break;
22138632Smckusick 
22238632Smckusick #ifdef NFS
22339316Smckusick 	case MOUNT_NFS:
224*39333Smckusick 		if (mntopts)
225*39333Smckusick 			getnfsopts(mntopts, &nfsargs, &opflags, &retrycnt);
22639316Smckusick 		if (options)
227*39333Smckusick 			getnfsopts(options, &nfsargs, &opflags, &retrycnt);
22839316Smckusick 		if (argp = getnfsargs(spec, name, type))
22939316Smckusick 			break;
23039316Smckusick 		return (1);
23138632Smckusick #endif /* NFS */
23238632Smckusick 
23339316Smckusick 	case MOUNT_MFS:
23439322Smckusick 	default:
23539322Smckusick 		argv[0] = mntname;
23639329Smckusick 		argc = 1;
23739322Smckusick 		if (flags) {
23839322Smckusick 			argv[argc++] = "-F";
23939322Smckusick 			sprintf(flagval, "%d", flags);
24039322Smckusick 			argv[argc++] = flagval;
24139322Smckusick 		}
242*39333Smckusick 		if (mntopts)
243*39333Smckusick 			argc += getexecopts(mntopts, &argv[argc]);
24439329Smckusick 		if (options)
24539329Smckusick 			argc += getexecopts(options, &argv[argc]);
24639316Smckusick 		argv[argc++] = spec;
24739316Smckusick 		argv[argc++] = name;
24839322Smckusick 		sprintf(execname, "%s/%s", _PATH_EXECDIR, mntname);
24939316Smckusick 		if (verbose) {
25039322Smckusick 			printf("exec: %s", execname);
25139322Smckusick 			for (i = 1; i < argc; i++)
25239316Smckusick 				printf(" %s", argv[i]);
25339316Smckusick 			printf("\n");
25439316Smckusick 		}
25539316Smckusick 		if (fake)
25639316Smckusick 			break;
25739316Smckusick 		if (i = vfork()) {
25839316Smckusick 			if (i == -1) {
25939322Smckusick 				perror("mount: vfork starting file system");
26039316Smckusick 				return (1);
26139131Smckusick 			}
26239316Smckusick 			if (waitpid(i, &status, 0) != -1 &&
26339316Smckusick 			    WIFEXITED(status) &&
26439316Smckusick 			    WEXITSTATUS(status) != 0)
26539316Smckusick 				return (WEXITSTATUS(status));
26639322Smckusick 			spec = mntname;
26739316Smckusick 			goto out;
26839316Smckusick 		}
26939322Smckusick 		execve(execname, argv, envp);
27039322Smckusick 		perror(execname);
27139316Smckusick 		exit (1);
27239316Smckusick 		/* NOTREACHED */
27338632Smckusick 
27439316Smckusick 	}
27539316Smckusick 	if (!fake && mount(mnttype, name, flags, argp)) {
27639316Smckusick 		if (opflags & ISBGRND)
27739316Smckusick 			exit(1);
27839316Smckusick 		fprintf(stderr, "%s on %s: ", spec, name);
27939316Smckusick 		switch (errno) {
28039316Smckusick 		case EMFILE:
28139316Smckusick 			fprintf(stderr, "Mount table full\n");
28239316Smckusick 			break;
28339316Smckusick 		case EINVAL:
284*39333Smckusick 			if (flags & M_UPDATE)
285*39333Smckusick 				fprintf(stderr, "Specified device does %s\n",
286*39333Smckusick 					"not match mounted device");
287*39333Smckusick 			else
288*39333Smckusick 				fprintf(stderr, "Bogus super block\n");
28939316Smckusick 			break;
290*39333Smckusick 		case EOPNOTSUPP:
291*39333Smckusick 			fprintf(stderr, "Operation not supported\n");
292*39333Smckusick 			break;
29339316Smckusick 		default:
29439316Smckusick 			perror((char *)NULL);
29539316Smckusick 			break;
2964460Sroot 		}
29739316Smckusick 		return(1);
2984460Sroot 	}
29935339Sbostic 
30039131Smckusick out:
30112808Ssam 	if (verbose)
30239316Smckusick 		prmount(spec, name, flags);
30335339Sbostic 
30438445Smckusick 	if (opflags & ISBGRND)
30538445Smckusick 		exit();
30638445Smckusick 	else
30738445Smckusick 		return(0);
3081057Sbill }
30935339Sbostic 
31035339Sbostic static
31139316Smckusick prmount(spec, name, flags)
31239316Smckusick 	char *spec, *name;
31339316Smckusick 	long flags;
31435339Sbostic {
31538632Smckusick 	register char *root;
31638632Smckusick 
31738445Smckusick 	if (opflags & ISBGRND)
31838445Smckusick 		return;
31938632Smckusick 	/*
32038632Smckusick 	 * trim trailing /'s and find last component of name
32138632Smckusick 	 */
32238632Smckusick 	for (root = index(spec, '\0'); *--root == '/';)
32338632Smckusick 		/* void */;
32438632Smckusick 	*++root = '\0';
32538632Smckusick 	if (root = rindex(spec, '/'))
32638632Smckusick 		spec = root + 1;
32738632Smckusick 	printf("%s on %s", spec, name);
32839316Smckusick 	if (flags & M_RDONLY)
32939316Smckusick 		printf(" (read-only)");
33039316Smckusick 	if (flags & M_NOEXEC)
33139316Smckusick 		printf(" (noexec)");
33239316Smckusick 	if (flags & M_NOSUID)
33339316Smckusick 		printf(" (nosuid)");
33439316Smckusick 	if (flags & M_NODEV)
33539316Smckusick 		printf(" (nodev)");
33639316Smckusick 	if (flags & M_SYNCHRONOUS)
33739316Smckusick 		printf(" (synchronous)");
338*39333Smckusick 	if (flags & M_UPDATE)
339*39333Smckusick 		printf(" (update only)");
34035339Sbostic 	printf("\n");
34135339Sbostic }
34235339Sbostic 
34339133Smckusick getmnttype(fstype)
34439133Smckusick 	char *fstype;
34539133Smckusick {
34639133Smckusick 
34739322Smckusick 	mntname = fstype;
34839133Smckusick 	if (!strcmp(fstype, "ufs"))
34939133Smckusick 		return (MOUNT_UFS);
35039133Smckusick 	if (!strcmp(fstype, "nfs"))
35139133Smckusick 		return (MOUNT_NFS);
35239133Smckusick 	if (!strcmp(fstype, "mfs"))
35339133Smckusick 		return (MOUNT_MFS);
35439133Smckusick 	return (0);
35539133Smckusick }
35639133Smckusick 
35738632Smckusick usage()
35835339Sbostic {
359*39333Smckusick 	fprintf(stderr, "usage: mount [-afurw]\nor mount [-furw] special | node\nor mount [-furw] special node\n");
36035339Sbostic 	exit(1);
36135339Sbostic }
36235339Sbostic 
36339316Smckusick getstdopts(options, flagp)
36439316Smckusick 	char *options;
36539316Smckusick 	long *flagp;
36639316Smckusick {
36739316Smckusick 	register char *opt;
36839316Smckusick 	int negative;
36939316Smckusick 	char *optbuf[BUFSIZ], *strtok();
37039316Smckusick 
37139316Smckusick 	strcpy(optbuf, options);
37239316Smckusick 	for (opt = strtok(optbuf, ","); opt; opt = strtok(NULL, ",")) {
37339316Smckusick 		if (opt[0] == 'n' && opt[1] == 'o') {
37439316Smckusick 			negative++;
37539316Smckusick 			opt += 2;
37639316Smckusick 		} else {
37739316Smckusick 			negative = 0;
37839316Smckusick 		}
379*39333Smckusick 		if (!negative && !strcasecmp(opt, FSTAB_RO)) {
380*39333Smckusick 			*flagp |= M_RDONLY;
381*39333Smckusick 			continue;
382*39333Smckusick 		}
383*39333Smckusick 		if (!negative && !strcasecmp(opt, FSTAB_RW)) {
384*39333Smckusick 			*flagp &= ~M_RDONLY;
385*39333Smckusick 			continue;
386*39333Smckusick 		}
38739316Smckusick 		if (!strcasecmp(opt, "exec")) {
38839316Smckusick 			if (negative)
38939316Smckusick 				*flagp |= M_NOEXEC;
390*39333Smckusick 			else
391*39333Smckusick 				*flagp &= ~M_NOEXEC;
39239316Smckusick 			continue;
39339316Smckusick 		}
39439316Smckusick 		if (!strcasecmp(opt, "suid")) {
39539316Smckusick 			if (negative)
39639316Smckusick 				*flagp |= M_NOSUID;
397*39333Smckusick 			else
398*39333Smckusick 				*flagp &= ~M_NOSUID;
39939316Smckusick 			continue;
40039316Smckusick 		}
40139316Smckusick 		if (!strcasecmp(opt, "dev")) {
40239316Smckusick 			if (negative)
40339316Smckusick 				*flagp |= M_NODEV;
404*39333Smckusick 			else
405*39333Smckusick 				*flagp &= ~M_NODEV;
40639316Smckusick 			continue;
40739316Smckusick 		}
40839316Smckusick 		if (!strcasecmp(opt, "synchronous")) {
40939316Smckusick 			if (!negative)
41039316Smckusick 				*flagp |= M_SYNCHRONOUS;
411*39333Smckusick 			else
412*39333Smckusick 				*flagp &= ~M_SYNCHRONOUS;
41339316Smckusick 			continue;
41439316Smckusick 		}
41539316Smckusick 	}
41639316Smckusick }
41739316Smckusick 
41839131Smckusick getufsopts(options, flagp)
41939131Smckusick 	char *options;
42039131Smckusick 	long *flagp;
42139131Smckusick {
42239131Smckusick 
42339131Smckusick 	return;
42439131Smckusick }
42539131Smckusick 
42639329Smckusick getexecopts(options, argv)
42739131Smckusick 	char *options;
42839131Smckusick 	char **argv;
42939131Smckusick {
43039131Smckusick 	register int argc = 0;
43139131Smckusick 	register char *opt;
43239131Smckusick 	char *strtok();
43339131Smckusick 
43439131Smckusick 	for (opt = strtok(options, ","); opt; opt = strtok(NULL, ",")) {
43539131Smckusick 		if (opt[0] != '-')
43639131Smckusick 			continue;
43739131Smckusick 		argv[argc++] = opt;
43839131Smckusick 		if (opt[2] == '\0' || opt[2] != '=')
43939131Smckusick 			continue;
44039131Smckusick 		opt[2] = '\0';
44139131Smckusick 		argv[argc++] = &opt[3];
44239131Smckusick 	}
44339131Smckusick 	return (argc);
44439131Smckusick }
44539131Smckusick 
44638632Smckusick #ifdef NFS
44739131Smckusick /*
44839131Smckusick  * Handle the getoption arg.
44939131Smckusick  * Essentially update "opflags", "retrycnt" and "nfsargs"
45039131Smckusick  */
45139131Smckusick getnfsopts(optarg, nfsargsp, opflagsp, retrycntp)
45239131Smckusick 	char *optarg;
45339131Smckusick 	struct nfs_args *nfsargsp;
45439131Smckusick 	int *opflagsp;
45539131Smckusick 	int *retrycntp;
45639131Smckusick {
45739131Smckusick 	register char *cp, *nextcp;
45839131Smckusick 	int num;
45939131Smckusick 	char *nump;
46039131Smckusick 
46139131Smckusick 	cp = optarg;
46239131Smckusick 	while (cp != NULL && *cp != '\0') {
46339131Smckusick 		if ((nextcp = index(cp, ',')) != NULL)
46439131Smckusick 			*nextcp++ = '\0';
46539131Smckusick 		if ((nump = index(cp, '=')) != NULL) {
46639131Smckusick 			*nump++ = '\0';
46739131Smckusick 			num = atoi(nump);
46839131Smckusick 		} else
46939131Smckusick 			num = -1;
47039131Smckusick 		/*
47139131Smckusick 		 * Just test for a string match and do it
47239131Smckusick 		 */
47339131Smckusick 		if (!strcmp(cp, "bg")) {
47439131Smckusick 			*opflagsp |= BGRND;
47539131Smckusick 		} else if (!strcmp(cp, "soft")) {
47639131Smckusick 			nfsargsp->flags |= NFSMNT_SOFT;
47739131Smckusick 		} else if (!strcmp(cp, "intr")) {
47839131Smckusick 			nfsargsp->flags |= NFSMNT_INT;
47939131Smckusick 		} else if (!strcmp(cp, "retry") && num > 0) {
48039131Smckusick 			*retrycntp = num;
48139131Smckusick 		} else if (!strcmp(cp, "rsize") && num > 0) {
48239131Smckusick 			nfsargsp->rsize = num;
48339131Smckusick 			nfsargsp->flags |= NFSMNT_RSIZE;
48439131Smckusick 		} else if (!strcmp(cp, "wsize") && num > 0) {
48539131Smckusick 			nfsargsp->wsize = num;
48639131Smckusick 			nfsargsp->flags |= NFSMNT_WSIZE;
48739131Smckusick 		} else if (!strcmp(cp, "timeo") && num > 0) {
48839131Smckusick 			nfsargsp->timeo = num;
48939131Smckusick 			nfsargsp->flags |= NFSMNT_TIMEO;
49039131Smckusick 		} else if (!strcmp(cp, "retrans") && num > 0) {
49139131Smckusick 			nfsargsp->retrans = num;
49239131Smckusick 			nfsargsp->flags |= NFSMNT_RETRANS;
49339131Smckusick 		}
49439131Smckusick 		cp = nextcp;
49539131Smckusick 	}
49639131Smckusick }
49739131Smckusick 
49838632Smckusick char *
49938632Smckusick getnfsargs(spec)
50038632Smckusick 	char *spec;
50135339Sbostic {
50238632Smckusick 	register CLIENT *clp;
50338632Smckusick 	struct hostent *hp;
50438632Smckusick 	struct sockaddr_in saddr;
50538632Smckusick 	struct timeval pertry, try;
50638632Smckusick 	enum clnt_stat clnt_stat;
50738632Smckusick 	int so = RPC_ANYSOCK;
50838632Smckusick 	char *hostp, *delimp;
50938632Smckusick 	u_short tport;
51038632Smckusick 	struct nfhret nfhret;
51138632Smckusick 	char nam[MNAMELEN + 1];
51238632Smckusick 
51338632Smckusick 	strncpy(nam, spec, MNAMELEN);
51438632Smckusick 	nam[MNAMELEN] = '\0';
51538632Smckusick 	if ((delimp = index(spec, '@')) != NULL) {
51638632Smckusick 		hostp = delimp + 1;
51738632Smckusick 	} else if ((delimp = index(spec, ':')) != NULL) {
51838632Smckusick 		hostp = spec;
51938632Smckusick 		spec = delimp + 1;
52038632Smckusick 	} else {
52138632Smckusick 		fprintf(stderr,
52238632Smckusick 		    "No <host>:<dirpath> or <dirpath>@<host> spec\n");
52338632Smckusick 		return (0);
52438632Smckusick 	}
52538632Smckusick 	*delimp = '\0';
52638632Smckusick 	if ((hp = gethostbyname(hostp)) == NULL) {
52738632Smckusick 		fprintf(stderr, "Can't get net id for host\n");
52838632Smckusick 		return (0);
52938632Smckusick 	}
53038632Smckusick 	bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
53138632Smckusick 	nfhret.stat = EACCES;	/* Mark not yet successful */
53238632Smckusick 	while (retrycnt > 0) {
53338632Smckusick 		saddr.sin_family = AF_INET;
53438632Smckusick 		saddr.sin_port = htons(PMAPPORT);
53538632Smckusick 		if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
53638632Smckusick 		    NFS_VER2, IPPROTO_UDP)) == 0) {
53738632Smckusick 			if ((opflags & ISBGRND) == 0)
53838632Smckusick 				clnt_pcreateerror("NFS Portmap");
53938632Smckusick 		} else {
54038632Smckusick 			saddr.sin_port = 0;
54138632Smckusick 			pertry.tv_sec = 10;
54238632Smckusick 			pertry.tv_usec = 0;
54338632Smckusick 			if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
54438632Smckusick 			    RPCMNT_VER1, pertry, &so)) == NULL) {
54538632Smckusick 				if ((opflags & ISBGRND) == 0)
54638632Smckusick 					clnt_pcreateerror("Cannot MNT PRC");
54738632Smckusick 			} else {
54838632Smckusick 				clp->cl_auth = authunix_create_default();
54938632Smckusick 				try.tv_sec = 10;
55038632Smckusick 				try.tv_usec = 0;
55138632Smckusick 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
55238632Smckusick 				    xdr_dir, spec, xdr_fh, &nfhret, try);
55338632Smckusick 				if (clnt_stat != RPC_SUCCESS) {
55438632Smckusick 					if ((opflags & ISBGRND) == 0)
55538632Smckusick 						clnt_perror(clp, "Bad MNT RPC");
55638632Smckusick 				} else {
55738632Smckusick 					auth_destroy(clp->cl_auth);
55838632Smckusick 					clnt_destroy(clp);
55938632Smckusick 					retrycnt = 0;
56038632Smckusick 				}
56138632Smckusick 			}
56238632Smckusick 		}
56338632Smckusick 		if (--retrycnt > 0) {
56438632Smckusick 			if (opflags & BGRND) {
56538632Smckusick 				opflags &= ~BGRND;
56638632Smckusick 				if (fork())
56738632Smckusick 					return (0);
56838632Smckusick 				else
56938632Smckusick 					opflags |= ISBGRND;
57038632Smckusick 			}
57138632Smckusick 			sleep(10);
57238632Smckusick 		}
57338632Smckusick 	}
57438632Smckusick 	if (nfhret.stat) {
57538632Smckusick 		if (opflags & ISBGRND)
57638632Smckusick 			exit(1);
57738632Smckusick 		fprintf(stderr, "Can't access %s, errno=%d\n", spec,
57838632Smckusick 		    nfhret.stat);
57938632Smckusick 		return (0);
58038632Smckusick 	}
58138632Smckusick 	saddr.sin_port = htons(tport);
58238632Smckusick 	nfsargs.addr = &saddr;
58338632Smckusick 	nfsargs.fh = &nfhret.nfh;
58438632Smckusick 	nfsargs.hostname = nam;
58538632Smckusick 	return ((caddr_t)&nfsargs);
58635339Sbostic }
58738445Smckusick 
58838445Smckusick /*
58938445Smckusick  * xdr routines for mount rpc's
59038445Smckusick  */
59138445Smckusick xdr_dir(xdrsp, dirp)
59238445Smckusick 	XDR *xdrsp;
59338445Smckusick 	char *dirp;
59438445Smckusick {
59538445Smckusick 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
59638445Smckusick }
59738445Smckusick 
59838445Smckusick xdr_fh(xdrsp, np)
59938445Smckusick 	XDR *xdrsp;
60038445Smckusick 	struct nfhret *np;
60138445Smckusick {
60238445Smckusick 	if (!xdr_u_long(xdrsp, &(np->stat)))
60338445Smckusick 		return (0);
60438445Smckusick 	if (np->stat)
60538445Smckusick 		return (1);
60638445Smckusick 	return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
60738445Smckusick }
60838632Smckusick #endif /* NFS */
609