xref: /csrg-svn/sbin/mount/mount.c (revision 39322)
121156Sdist /*
2*39322Smckusick  * Copyright (c) 1980, 1989 The Regents of the University of California.
3*39322Smckusick  * All rights reserved.
4*39322Smckusick  *
5*39322Smckusick  * Redistribution and use in source and binary forms are permitted
6*39322Smckusick  * provided that the above copyright notice and this paragraph are
7*39322Smckusick  * duplicated in all such forms and that any documentation,
8*39322Smckusick  * advertising materials, and other materials related to such
9*39322Smckusick  * distribution and use acknowledge that the software was developed
10*39322Smckusick  * by the University of California, Berkeley.  The name of the
11*39322Smckusick  * University may not be used to endorse or promote products derived
12*39322Smckusick  * from this software without specific prior written permission.
13*39322Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*39322Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*39322Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621156Sdist  */
1721156Sdist 
1810811Ssam #ifndef lint
1921156Sdist char copyright[] =
20*39322Smckusick "@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\
2121156Sdist  All rights reserved.\n";
22*39322Smckusick #endif /* not lint */
231057Sbill 
2421156Sdist #ifndef lint
25*39322Smckusick static char sccsid[] = "@(#)mount.c	5.15 (Berkeley) 10/17/89";
26*39322Smckusick #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*39322Smckusick int fake, verbose, mnttype;
60*39322Smckusick 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;
9538632Smckusick 	int all, ch, rval, sfake, 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;
104*39322Smckusick 	mntname = "ufs";
10538445Smckusick 	while ((ch = getopt(argc, argv, "afrwvt: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;
11635339Sbostic 		case 'v':
11735339Sbostic 			verbose = 1;
11835339Sbostic 			break;
11935339Sbostic 		case 'w':
12035339Sbostic 			type = FSTAB_RW;
12135339Sbostic 			break;
12239131Smckusick 		case 'o':
12339131Smckusick 			options = optarg;
12439131Smckusick 			break;
12538445Smckusick 		case 't':
126*39322Smckusick 			mnttype = getmnttype(optarg);
127*39322Smckusick 			break;
12835339Sbostic 		case '?':
12935339Sbostic 		default:
13035339Sbostic 			usage();
13139131Smckusick 			/* NOTREACHED */
1325073Sroot 		}
13335339Sbostic 	argc -= optind;
13435339Sbostic 	argv += optind;
13535339Sbostic 
13635339Sbostic 	/* NOSTRICT */
13735339Sbostic 
1384460Sroot 	if (all) {
13935369Sbostic 		rval = 0;
14035372Sbostic 		for (sfake = fake; fs = getfsent(); fake = sfake) {
14135372Sbostic 			if (BADTYPE(fs->fs_type))
14235372Sbostic 				continue;
14335372Sbostic 			/* `/' is special, it's always mounted */
14435372Sbostic 			if (!strcmp(fs->fs_file, "/"))
14535372Sbostic 				fake = 1;
14639133Smckusick 			if ((mnttype = getmnttype(fs->fs_vfstype)) == 0) {
14739133Smckusick 				fprintf(stderr,
14839133Smckusick 				    "%s %s type of file system is unknown.\n",
14939133Smckusick 				    "mount:", fs->fs_vfstype);
15039133Smckusick 				continue;
15139133Smckusick 			}
15235372Sbostic 			rval |= mountfs(fs->fs_spec, fs->fs_file,
15339131Smckusick 			    type ? type : fs->fs_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 		}
18439133Smckusick 		if ((mnttype = getmnttype(fs->fs_vfstype)) == 0) {
18539133Smckusick 			fprintf(stderr,
18639133Smckusick 			    "mount: %s type of file system is unknown.\n",
18739133Smckusick 			    fs->fs_vfstype);
18839133Smckusick 			exit(1);
18939133Smckusick 		}
19035369Sbostic 		exit(mountfs(fs->fs_spec, fs->fs_file,
19139131Smckusick 		    type ? type : fs->fs_type, options, fs->fs_mntops));
19212808Ssam 	}
1931057Sbill 
19435339Sbostic 	if (argc != 2)
19535339Sbostic 		usage();
19612808Ssam 
19739131Smckusick 	exit(mountfs(argv[0], argv[1], type ? type : "rw", options, NULL));
19812808Ssam }
19912808Ssam 
20039131Smckusick mountfs(spec, name, type, options, mntopts)
20139131Smckusick 	char *spec, *name, *type, *options, *mntopts;
20212808Ssam {
20335339Sbostic 	extern int errno;
20435339Sbostic 	register int cnt;
20539131Smckusick 	int flags, argc, status, i;
20638070Smckusick 	struct ufs_args args;
20739131Smckusick 	char *argp, *argv[50];
208*39322Smckusick 	char execname[MAXPATHLEN + 1], flagval[12];
2091057Sbill 
21039316Smckusick 	flags = 0;
21139316Smckusick 	if (!strcmp(type, FSTAB_RO))
21239316Smckusick 		flags |= M_RDONLY;
21339316Smckusick 	if (options)
21439316Smckusick 		getstdopts(options, &flags);
21539316Smckusick 	if (mntopts)
21639316Smckusick 		getstdopts(mntopts, &flags);
217*39322Smckusick 	argc = 1;
21839316Smckusick 	switch (mnttype) {
21939316Smckusick 	case MOUNT_UFS:
22039316Smckusick 		if (options)
22139316Smckusick 			getufsopts(options, &flags);
22239316Smckusick 		if (mntopts)
22339316Smckusick 			getufsopts(mntopts, &flags);
22439316Smckusick 		args.fspec = spec;
22539316Smckusick 		argp = (caddr_t)&args;
22639316Smckusick 		break;
22738632Smckusick 
22838632Smckusick #ifdef NFS
22939316Smckusick 	case MOUNT_NFS:
23039316Smckusick 		if (options)
23139316Smckusick 			getnfsopts(options, &nfsargs, &opflags,
23239316Smckusick 				&retrycnt);
23339316Smckusick 		if (mntopts)
23439316Smckusick 			getnfsopts(mntopts, &nfsargs, &opflags,
23539316Smckusick 				&retrycnt);
23639316Smckusick 		if (argp = getnfsargs(spec, name, type))
23739316Smckusick 			break;
23839316Smckusick 		return (1);
23938632Smckusick #endif /* NFS */
24038632Smckusick 
24139131Smckusick #ifdef MFS
24239316Smckusick 	case MOUNT_MFS:
243*39322Smckusick 		mntname = "memfs";
24439316Smckusick 		if (options)
24539316Smckusick 			argc += getmfsopts(options, &argv[argc]);
24639316Smckusick 		if (mntopts)
24739316Smckusick 			argc += getmfsopts(mntopts, &argv[argc]);
248*39322Smckusick 		/* fall through to */
249*39322Smckusick #endif /* MFS */
250*39322Smckusick 
251*39322Smckusick 	default:
252*39322Smckusick 		argv[0] = mntname;
253*39322Smckusick 		if (flags) {
254*39322Smckusick 			argv[argc++] = "-F";
255*39322Smckusick 			sprintf(flagval, "%d", flags);
256*39322Smckusick 			argv[argc++] = flagval;
257*39322Smckusick 		}
25839316Smckusick 		argv[argc++] = spec;
25939316Smckusick 		argv[argc++] = name;
260*39322Smckusick 		sprintf(execname, "%s/%s", _PATH_EXECDIR, mntname);
26139316Smckusick 		if (verbose) {
262*39322Smckusick 			printf("exec: %s", execname);
263*39322Smckusick 			for (i = 1; i < argc; i++)
26439316Smckusick 				printf(" %s", argv[i]);
26539316Smckusick 			printf("\n");
26639316Smckusick 		}
26739316Smckusick 		if (fake)
26839316Smckusick 			break;
26939316Smckusick 		if (i = vfork()) {
27039316Smckusick 			if (i == -1) {
271*39322Smckusick 				perror("mount: vfork starting file system");
27239316Smckusick 				return (1);
27339131Smckusick 			}
27439316Smckusick 			if (waitpid(i, &status, 0) != -1 &&
27539316Smckusick 			    WIFEXITED(status) &&
27639316Smckusick 			    WEXITSTATUS(status) != 0)
27739316Smckusick 				return (WEXITSTATUS(status));
278*39322Smckusick 			spec = mntname;
27939316Smckusick 			goto out;
28039316Smckusick 		}
281*39322Smckusick 		execve(execname, argv, envp);
282*39322Smckusick 		perror(execname);
28339316Smckusick 		exit (1);
28439316Smckusick 		/* NOTREACHED */
28538632Smckusick 
28639316Smckusick 	}
28739316Smckusick 	if (!fake && mount(mnttype, name, flags, argp)) {
28839316Smckusick 		if (opflags & ISBGRND)
28939316Smckusick 			exit(1);
29039316Smckusick 		fprintf(stderr, "%s on %s: ", spec, name);
29139316Smckusick 		switch (errno) {
29239316Smckusick 		case EMFILE:
29339316Smckusick 			fprintf(stderr, "Mount table full\n");
29439316Smckusick 			break;
29539316Smckusick 		case EINVAL:
29639316Smckusick 			fprintf(stderr, "Bogus super block\n");
29739316Smckusick 			break;
29839316Smckusick 		default:
29939316Smckusick 			perror((char *)NULL);
30039316Smckusick 			break;
3014460Sroot 		}
30239316Smckusick 		return(1);
3034460Sroot 	}
30435339Sbostic 
30539131Smckusick out:
30612808Ssam 	if (verbose)
30739316Smckusick 		prmount(spec, name, flags);
30835339Sbostic 
30938445Smckusick 	if (opflags & ISBGRND)
31038445Smckusick 		exit();
31138445Smckusick 	else
31238445Smckusick 		return(0);
3131057Sbill }
31435339Sbostic 
31535339Sbostic static
31639316Smckusick prmount(spec, name, flags)
31739316Smckusick 	char *spec, *name;
31839316Smckusick 	long flags;
31935339Sbostic {
32038632Smckusick 	register char *root;
32138632Smckusick 
32238445Smckusick 	if (opflags & ISBGRND)
32338445Smckusick 		return;
32438632Smckusick 	/*
32538632Smckusick 	 * trim trailing /'s and find last component of name
32638632Smckusick 	 */
32738632Smckusick 	for (root = index(spec, '\0'); *--root == '/';)
32838632Smckusick 		/* void */;
32938632Smckusick 	*++root = '\0';
33038632Smckusick 	if (root = rindex(spec, '/'))
33138632Smckusick 		spec = root + 1;
33238632Smckusick 	printf("%s on %s", spec, name);
33339316Smckusick 	if (flags & M_RDONLY)
33439316Smckusick 		printf(" (read-only)");
33539316Smckusick 	if (flags & M_NOEXEC)
33639316Smckusick 		printf(" (noexec)");
33739316Smckusick 	if (flags & M_NOSUID)
33839316Smckusick 		printf(" (nosuid)");
33939316Smckusick 	if (flags & M_NODEV)
34039316Smckusick 		printf(" (nodev)");
34139316Smckusick 	if (flags & M_SYNCHRONOUS)
34239316Smckusick 		printf(" (synchronous)");
34335339Sbostic 	printf("\n");
34435339Sbostic }
34535339Sbostic 
34639133Smckusick getmnttype(fstype)
34739133Smckusick 	char *fstype;
34839133Smckusick {
34939133Smckusick 
350*39322Smckusick 	mntname = fstype;
35139133Smckusick 	if (!strcmp(fstype, "ufs"))
35239133Smckusick 		return (MOUNT_UFS);
35339133Smckusick 	if (!strcmp(fstype, "nfs"))
35439133Smckusick 		return (MOUNT_NFS);
35539133Smckusick 	if (!strcmp(fstype, "mfs"))
35639133Smckusick 		return (MOUNT_MFS);
35739133Smckusick 	return (0);
35839133Smckusick }
35939133Smckusick 
36038632Smckusick usage()
36135339Sbostic {
36238632Smckusick 	fprintf(stderr, "usage: mount [-afrw]\nor mount [-frw] special | node\nor mount [-frw] special node\n");
36335339Sbostic 	exit(1);
36435339Sbostic }
36535339Sbostic 
36639316Smckusick getstdopts(options, flagp)
36739316Smckusick 	char *options;
36839316Smckusick 	long *flagp;
36939316Smckusick {
37039316Smckusick 	register char *opt;
37139316Smckusick 	int negative;
37239316Smckusick 	char *optbuf[BUFSIZ], *strtok();
37339316Smckusick 
37439316Smckusick 	strcpy(optbuf, options);
37539316Smckusick 	for (opt = strtok(optbuf, ","); opt; opt = strtok(NULL, ",")) {
37639316Smckusick 		if (opt[0] == 'n' && opt[1] == 'o') {
37739316Smckusick 			negative++;
37839316Smckusick 			opt += 2;
37939316Smckusick 		} else {
38039316Smckusick 			negative = 0;
38139316Smckusick 		}
38239316Smckusick 		if (!strcasecmp(opt, "exec")) {
38339316Smckusick 			if (negative)
38439316Smckusick 				*flagp |= M_NOEXEC;
38539316Smckusick 			continue;
38639316Smckusick 		}
38739316Smckusick 		if (!strcasecmp(opt, "suid")) {
38839316Smckusick 			if (negative)
38939316Smckusick 				*flagp |= M_NOSUID;
39039316Smckusick 			continue;
39139316Smckusick 		}
39239316Smckusick 		if (!strcasecmp(opt, "dev")) {
39339316Smckusick 			if (negative)
39439316Smckusick 				*flagp |= M_NODEV;
39539316Smckusick 			continue;
39639316Smckusick 		}
39739316Smckusick 		if (!strcasecmp(opt, "synchronous")) {
39839316Smckusick 			if (!negative)
39939316Smckusick 				*flagp |= M_SYNCHRONOUS;
40039316Smckusick 			continue;
40139316Smckusick 		}
40239316Smckusick 	}
40339316Smckusick }
40439316Smckusick 
40539131Smckusick getufsopts(options, flagp)
40639131Smckusick 	char *options;
40739131Smckusick 	long *flagp;
40839131Smckusick {
40939131Smckusick 
41039131Smckusick 	return;
41139131Smckusick }
41239131Smckusick 
41339131Smckusick #ifdef MFS
41439131Smckusick getmfsopts(options, argv)
41539131Smckusick 	char *options;
41639131Smckusick 	char **argv;
41739131Smckusick {
41839131Smckusick 	register int argc = 0;
41939131Smckusick 	register char *opt;
42039131Smckusick 	char *strtok();
42139131Smckusick 
42239131Smckusick 	for (opt = strtok(options, ","); opt; opt = strtok(NULL, ",")) {
42339131Smckusick 		if (opt[0] != '-')
42439131Smckusick 			continue;
42539131Smckusick 		argv[argc++] = opt;
42639131Smckusick 		if (opt[2] == '\0' || opt[2] != '=')
42739131Smckusick 			continue;
42839131Smckusick 		opt[2] = '\0';
42939131Smckusick 		argv[argc++] = &opt[3];
43039131Smckusick 	}
43139131Smckusick 	return (argc);
43239131Smckusick }
43339131Smckusick #endif /* MFS */
43439131Smckusick 
43538632Smckusick #ifdef NFS
43639131Smckusick /*
43739131Smckusick  * Handle the getoption arg.
43839131Smckusick  * Essentially update "opflags", "retrycnt" and "nfsargs"
43939131Smckusick  */
44039131Smckusick getnfsopts(optarg, nfsargsp, opflagsp, retrycntp)
44139131Smckusick 	char *optarg;
44239131Smckusick 	struct nfs_args *nfsargsp;
44339131Smckusick 	int *opflagsp;
44439131Smckusick 	int *retrycntp;
44539131Smckusick {
44639131Smckusick 	register char *cp, *nextcp;
44739131Smckusick 	int num;
44839131Smckusick 	char *nump;
44939131Smckusick 
45039131Smckusick 	cp = optarg;
45139131Smckusick 	while (cp != NULL && *cp != '\0') {
45239131Smckusick 		if ((nextcp = index(cp, ',')) != NULL)
45339131Smckusick 			*nextcp++ = '\0';
45439131Smckusick 		if ((nump = index(cp, '=')) != NULL) {
45539131Smckusick 			*nump++ = '\0';
45639131Smckusick 			num = atoi(nump);
45739131Smckusick 		} else
45839131Smckusick 			num = -1;
45939131Smckusick 		/*
46039131Smckusick 		 * Just test for a string match and do it
46139131Smckusick 		 */
46239131Smckusick 		if (!strcmp(cp, "bg")) {
46339131Smckusick 			*opflagsp |= BGRND;
46439131Smckusick 		} else if (!strcmp(cp, "soft")) {
46539131Smckusick 			nfsargsp->flags |= NFSMNT_SOFT;
46639131Smckusick 		} else if (!strcmp(cp, "intr")) {
46739131Smckusick 			nfsargsp->flags |= NFSMNT_INT;
46839131Smckusick 		} else if (!strcmp(cp, "retry") && num > 0) {
46939131Smckusick 			*retrycntp = num;
47039131Smckusick 		} else if (!strcmp(cp, "rsize") && num > 0) {
47139131Smckusick 			nfsargsp->rsize = num;
47239131Smckusick 			nfsargsp->flags |= NFSMNT_RSIZE;
47339131Smckusick 		} else if (!strcmp(cp, "wsize") && num > 0) {
47439131Smckusick 			nfsargsp->wsize = num;
47539131Smckusick 			nfsargsp->flags |= NFSMNT_WSIZE;
47639131Smckusick 		} else if (!strcmp(cp, "timeo") && num > 0) {
47739131Smckusick 			nfsargsp->timeo = num;
47839131Smckusick 			nfsargsp->flags |= NFSMNT_TIMEO;
47939131Smckusick 		} else if (!strcmp(cp, "retrans") && num > 0) {
48039131Smckusick 			nfsargsp->retrans = num;
48139131Smckusick 			nfsargsp->flags |= NFSMNT_RETRANS;
48239131Smckusick 		}
48339131Smckusick 		cp = nextcp;
48439131Smckusick 	}
48539131Smckusick }
48639131Smckusick 
48738632Smckusick char *
48838632Smckusick getnfsargs(spec)
48938632Smckusick 	char *spec;
49035339Sbostic {
49138632Smckusick 	register CLIENT *clp;
49238632Smckusick 	struct hostent *hp;
49338632Smckusick 	struct sockaddr_in saddr;
49438632Smckusick 	struct timeval pertry, try;
49538632Smckusick 	enum clnt_stat clnt_stat;
49638632Smckusick 	int so = RPC_ANYSOCK;
49738632Smckusick 	char *hostp, *delimp;
49838632Smckusick 	u_short tport;
49938632Smckusick 	struct nfhret nfhret;
50038632Smckusick 	char nam[MNAMELEN + 1];
50138632Smckusick 
50238632Smckusick 	strncpy(nam, spec, MNAMELEN);
50338632Smckusick 	nam[MNAMELEN] = '\0';
50438632Smckusick 	if ((delimp = index(spec, '@')) != NULL) {
50538632Smckusick 		hostp = delimp + 1;
50638632Smckusick 	} else if ((delimp = index(spec, ':')) != NULL) {
50738632Smckusick 		hostp = spec;
50838632Smckusick 		spec = delimp + 1;
50938632Smckusick 	} else {
51038632Smckusick 		fprintf(stderr,
51138632Smckusick 		    "No <host>:<dirpath> or <dirpath>@<host> spec\n");
51238632Smckusick 		return (0);
51338632Smckusick 	}
51438632Smckusick 	*delimp = '\0';
51538632Smckusick 	if ((hp = gethostbyname(hostp)) == NULL) {
51638632Smckusick 		fprintf(stderr, "Can't get net id for host\n");
51738632Smckusick 		return (0);
51838632Smckusick 	}
51938632Smckusick 	bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
52038632Smckusick 	nfhret.stat = EACCES;	/* Mark not yet successful */
52138632Smckusick 	while (retrycnt > 0) {
52238632Smckusick 		saddr.sin_family = AF_INET;
52338632Smckusick 		saddr.sin_port = htons(PMAPPORT);
52438632Smckusick 		if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
52538632Smckusick 		    NFS_VER2, IPPROTO_UDP)) == 0) {
52638632Smckusick 			if ((opflags & ISBGRND) == 0)
52738632Smckusick 				clnt_pcreateerror("NFS Portmap");
52838632Smckusick 		} else {
52938632Smckusick 			saddr.sin_port = 0;
53038632Smckusick 			pertry.tv_sec = 10;
53138632Smckusick 			pertry.tv_usec = 0;
53238632Smckusick 			if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
53338632Smckusick 			    RPCMNT_VER1, pertry, &so)) == NULL) {
53438632Smckusick 				if ((opflags & ISBGRND) == 0)
53538632Smckusick 					clnt_pcreateerror("Cannot MNT PRC");
53638632Smckusick 			} else {
53738632Smckusick 				clp->cl_auth = authunix_create_default();
53838632Smckusick 				try.tv_sec = 10;
53938632Smckusick 				try.tv_usec = 0;
54038632Smckusick 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
54138632Smckusick 				    xdr_dir, spec, xdr_fh, &nfhret, try);
54238632Smckusick 				if (clnt_stat != RPC_SUCCESS) {
54338632Smckusick 					if ((opflags & ISBGRND) == 0)
54438632Smckusick 						clnt_perror(clp, "Bad MNT RPC");
54538632Smckusick 				} else {
54638632Smckusick 					auth_destroy(clp->cl_auth);
54738632Smckusick 					clnt_destroy(clp);
54838632Smckusick 					retrycnt = 0;
54938632Smckusick 				}
55038632Smckusick 			}
55138632Smckusick 		}
55238632Smckusick 		if (--retrycnt > 0) {
55338632Smckusick 			if (opflags & BGRND) {
55438632Smckusick 				opflags &= ~BGRND;
55538632Smckusick 				if (fork())
55638632Smckusick 					return (0);
55738632Smckusick 				else
55838632Smckusick 					opflags |= ISBGRND;
55938632Smckusick 			}
56038632Smckusick 			sleep(10);
56138632Smckusick 		}
56238632Smckusick 	}
56338632Smckusick 	if (nfhret.stat) {
56438632Smckusick 		if (opflags & ISBGRND)
56538632Smckusick 			exit(1);
56638632Smckusick 		fprintf(stderr, "Can't access %s, errno=%d\n", spec,
56738632Smckusick 		    nfhret.stat);
56838632Smckusick 		return (0);
56938632Smckusick 	}
57038632Smckusick 	saddr.sin_port = htons(tport);
57138632Smckusick 	nfsargs.addr = &saddr;
57238632Smckusick 	nfsargs.fh = &nfhret.nfh;
57338632Smckusick 	nfsargs.hostname = nam;
57438632Smckusick 	return ((caddr_t)&nfsargs);
57535339Sbostic }
57638445Smckusick 
57738445Smckusick /*
57838445Smckusick  * xdr routines for mount rpc's
57938445Smckusick  */
58038445Smckusick xdr_dir(xdrsp, dirp)
58138445Smckusick 	XDR *xdrsp;
58238445Smckusick 	char *dirp;
58338445Smckusick {
58438445Smckusick 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
58538445Smckusick }
58638445Smckusick 
58738445Smckusick xdr_fh(xdrsp, np)
58838445Smckusick 	XDR *xdrsp;
58938445Smckusick 	struct nfhret *np;
59038445Smckusick {
59138445Smckusick 	if (!xdr_u_long(xdrsp, &(np->stat)))
59238445Smckusick 		return (0);
59338445Smckusick 	if (np->stat)
59438445Smckusick 		return (1);
59538445Smckusick 	return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
59638445Smckusick }
59738632Smckusick #endif /* NFS */
598