xref: /csrg-svn/sbin/mount/mount.c (revision 39319)
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*39319Smckusick static char sccsid[] = "@(#)mount.c	5.14 (Berkeley) 10/17/89";
1521156Sdist #endif not lint
1621156Sdist 
1739131Smckusick #include "pathnames.h"
1812808Ssam #include <sys/param.h>
1935339Sbostic #include <sys/file.h>
2038445Smckusick #include <sys/time.h>
2139133Smckusick #include <sys/wait.h>
2210811Ssam #include <fstab.h>
2316669Ssam #include <errno.h>
2435339Sbostic #include <stdio.h>
2538445Smckusick #include <strings.h>
2638445Smckusick #include <sys/dir.h>
2738445Smckusick #include <sys/uio.h>
2838445Smckusick #include <sys/namei.h>
2938445Smckusick #include <sys/mount.h>
3038445Smckusick #ifdef NFS
3138445Smckusick #include <sys/socket.h>
3238445Smckusick #include <sys/socketvar.h>
3338445Smckusick #include <netdb.h>
3438445Smckusick #include <rpc/rpc.h>
3538445Smckusick #include <rpc/pmap_clnt.h>
3638445Smckusick #include <rpc/pmap_prot.h>
3738445Smckusick #include <nfs/rpcv2.h>
3838445Smckusick #include <nfs/nfsv2.h>
3938445Smckusick #include <nfs/nfs.h>
4038445Smckusick #endif
411057Sbill 
4235339Sbostic #define	BADTYPE(type) \
4335339Sbostic 	(strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \
4435339Sbostic 	    strcmp(type, FSTAB_RQ))
4535339Sbostic #define	SETTYPE(type) \
4635339Sbostic 	(!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ))
471057Sbill 
4838445Smckusick static int fake, verbose, mnttype;
4939131Smckusick char **envp;
5039131Smckusick 
5138445Smckusick #ifdef NFS
5238445Smckusick int xdr_dir(), xdr_fh();
5338632Smckusick char *getnfsargs();
5438445Smckusick struct nfs_args nfsargs = {
5538445Smckusick 	(struct sockaddr_in *)0,
5638445Smckusick 	(nfsv2fh_t *)0,
5738445Smckusick 	0,
5838445Smckusick 	NFS_WSIZE,
5938445Smckusick 	NFS_RSIZE,
6038445Smckusick 	NFS_TIMEO,
6138445Smckusick 	NFS_RETRANS,
6238445Smckusick 	(char *)0,
6338445Smckusick };
645073Sroot 
6538445Smckusick struct nfhret {
6638445Smckusick 	u_long	stat;
6738445Smckusick 	nfsv2fh_t nfh;
6838445Smckusick };
6938445Smckusick int retrycnt = 10000;
7038445Smckusick #define	BGRND	1
7138445Smckusick #define	ISBGRND	2
7238445Smckusick int opflags = 0;
7338445Smckusick #endif
7438445Smckusick 
7539131Smckusick main(argc, argv, arge)
765073Sroot 	int argc;
775073Sroot 	char **argv;
7839131Smckusick 	char **arge;
791057Sbill {
8035339Sbostic 	extern char *optarg;
8135339Sbostic 	extern int optind;
8235339Sbostic 	register struct fstab *fs;
8335339Sbostic 	register int cnt;
8438632Smckusick 	int all, ch, rval, sfake, i;
8538632Smckusick 	long mntsize;
86*39319Smckusick 	struct statfs *mntbuf;
8739131Smckusick 	char *type, *options = NULL;
881057Sbill 
8939131Smckusick 	envp = arge;
9035339Sbostic 	all = 0;
9135339Sbostic 	type = NULL;
9238445Smckusick 	mnttype = MOUNT_UFS;
9338445Smckusick 	while ((ch = getopt(argc, argv, "afrwvt:o:")) != EOF)
9435339Sbostic 		switch((char)ch) {
9535339Sbostic 		case 'a':
9635339Sbostic 			all = 1;
9735339Sbostic 			break;
9835339Sbostic 		case 'f':
9935339Sbostic 			fake = 1;
10035339Sbostic 			break;
10135339Sbostic 		case 'r':
10212808Ssam 			type = FSTAB_RO;
10335339Sbostic 			break;
10435339Sbostic 		case 'v':
10535339Sbostic 			verbose = 1;
10635339Sbostic 			break;
10735339Sbostic 		case 'w':
10835339Sbostic 			type = FSTAB_RW;
10935339Sbostic 			break;
11039131Smckusick 		case 'o':
11139131Smckusick 			options = optarg;
11239131Smckusick 			break;
11338445Smckusick 		case 't':
11439133Smckusick 			if (mnttype = getmnttype(optarg))
11539131Smckusick 				break;
11639131Smckusick 			/* fall through */
11735339Sbostic 		case '?':
11835339Sbostic 		default:
11935339Sbostic 			usage();
12039131Smckusick 			/* NOTREACHED */
1215073Sroot 		}
12235339Sbostic 	argc -= optind;
12335339Sbostic 	argv += optind;
12435339Sbostic 
12535339Sbostic 	/* NOSTRICT */
12635339Sbostic 
1274460Sroot 	if (all) {
12835369Sbostic 		rval = 0;
12935372Sbostic 		for (sfake = fake; fs = getfsent(); fake = sfake) {
13035372Sbostic 			if (BADTYPE(fs->fs_type))
13135372Sbostic 				continue;
13235372Sbostic 			/* `/' is special, it's always mounted */
13335372Sbostic 			if (!strcmp(fs->fs_file, "/"))
13435372Sbostic 				fake = 1;
13539133Smckusick 			if ((mnttype = getmnttype(fs->fs_vfstype)) == 0) {
13639133Smckusick 				fprintf(stderr,
13739133Smckusick 				    "%s %s type of file system is unknown.\n",
13839133Smckusick 				    "mount:", fs->fs_vfstype);
13939133Smckusick 				continue;
14039133Smckusick 			}
14135372Sbostic 			rval |= mountfs(fs->fs_spec, fs->fs_file,
14239131Smckusick 			    type ? type : fs->fs_type, options, fs->fs_mntops);
14335372Sbostic 		}
14435341Sbostic 		exit(rval);
14535339Sbostic 	}
1465073Sroot 
14735339Sbostic 	if (argc == 0) {
14835339Sbostic 		if (verbose || fake || type)
14935339Sbostic 			usage();
150*39319Smckusick 		if ((mntsize = getmntinfo(&mntbuf)) == 0) {
151*39319Smckusick 			fprintf(stderr,
152*39319Smckusick 				"mount: cannot get mount information\n");
15338632Smckusick 			exit(1);
15438632Smckusick 		}
15539316Smckusick 		for (i = 0; i < mntsize; i++)
15638632Smckusick 			prmount(mntbuf[i].f_mntfromname, mntbuf[i].f_mntonname,
15739316Smckusick 				mntbuf[i].f_flags);
1584460Sroot 		exit(0);
1591057Sbill 	}
16012808Ssam 
16135339Sbostic 	if (argc == 1) {
16235339Sbostic 		if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
16335339Sbostic 			fprintf(stderr,
16435339Sbostic 			    "mount: unknown special file or file system %s.\n",
16535339Sbostic 			    *argv);
16635339Sbostic 			exit(1);
16735339Sbostic 		}
16835339Sbostic 		if (BADTYPE(fs->fs_type)) {
16935339Sbostic 			fprintf(stderr,
17035339Sbostic 			    "mount: %s has unknown file system type.\n", *argv);
17135339Sbostic 			exit(1);
17235339Sbostic 		}
17339133Smckusick 		if ((mnttype = getmnttype(fs->fs_vfstype)) == 0) {
17439133Smckusick 			fprintf(stderr,
17539133Smckusick 			    "mount: %s type of file system is unknown.\n",
17639133Smckusick 			    fs->fs_vfstype);
17739133Smckusick 			exit(1);
17839133Smckusick 		}
17935369Sbostic 		exit(mountfs(fs->fs_spec, fs->fs_file,
18039131Smckusick 		    type ? type : fs->fs_type, options, fs->fs_mntops));
18112808Ssam 	}
1821057Sbill 
18335339Sbostic 	if (argc != 2)
18435339Sbostic 		usage();
18512808Ssam 
18639131Smckusick 	exit(mountfs(argv[0], argv[1], type ? type : "rw", options, NULL));
18712808Ssam }
18812808Ssam 
18939131Smckusick mountfs(spec, name, type, options, mntopts)
19039131Smckusick 	char *spec, *name, *type, *options, *mntopts;
19112808Ssam {
19235339Sbostic 	extern int errno;
19335339Sbostic 	register int cnt;
19439131Smckusick 	int flags, argc, status, i;
19538070Smckusick 	struct ufs_args args;
19639131Smckusick 	char *argp, *argv[50];
1971057Sbill 
19839316Smckusick 	flags = 0;
19939316Smckusick 	if (!strcmp(type, FSTAB_RO))
20039316Smckusick 		flags |= M_RDONLY;
20139316Smckusick 	if (options)
20239316Smckusick 		getstdopts(options, &flags);
20339316Smckusick 	if (mntopts)
20439316Smckusick 		getstdopts(mntopts, &flags);
20539316Smckusick 	switch (mnttype) {
20639316Smckusick 	case MOUNT_UFS:
20739316Smckusick 		if (options)
20839316Smckusick 			getufsopts(options, &flags);
20939316Smckusick 		if (mntopts)
21039316Smckusick 			getufsopts(mntopts, &flags);
21139316Smckusick 		args.fspec = spec;
21239316Smckusick 		argp = (caddr_t)&args;
21339316Smckusick 		break;
21438632Smckusick 
21538632Smckusick #ifdef NFS
21639316Smckusick 	case MOUNT_NFS:
21739316Smckusick 		if (options)
21839316Smckusick 			getnfsopts(options, &nfsargs, &opflags,
21939316Smckusick 				&retrycnt);
22039316Smckusick 		if (mntopts)
22139316Smckusick 			getnfsopts(mntopts, &nfsargs, &opflags,
22239316Smckusick 				&retrycnt);
22339316Smckusick 		if (argp = getnfsargs(spec, name, type))
22439316Smckusick 			break;
22539316Smckusick 		return (1);
22638632Smckusick #endif /* NFS */
22738632Smckusick 
22839131Smckusick #ifdef MFS
22939316Smckusick 	case MOUNT_MFS:
23039316Smckusick 		argv[0] = "memfs";
23139316Smckusick 		argc = 1;
23239316Smckusick 		if (options)
23339316Smckusick 			argc += getmfsopts(options, &argv[argc]);
23439316Smckusick 		if (mntopts)
23539316Smckusick 			argc += getmfsopts(mntopts, &argv[argc]);
23639316Smckusick 		argv[argc++] = spec;
23739316Smckusick 		argv[argc++] = name;
23839316Smckusick 		if (verbose) {
23939316Smckusick 			printf("exec:");
24039316Smckusick 			for (i = 0; i < argc; i++)
24139316Smckusick 				printf(" %s", argv[i]);
24239316Smckusick 			printf("\n");
24339316Smckusick 		}
24439316Smckusick 		if (fake)
24539316Smckusick 			break;
24639316Smckusick 		if (i = vfork()) {
24739316Smckusick 			if (i == -1) {
24839316Smckusick 				perror("mount: vfork for memfs");
24939316Smckusick 				return (1);
25039131Smckusick 			}
25139316Smckusick 			if (waitpid(i, &status, 0) != -1 &&
25239316Smckusick 			    WIFEXITED(status) &&
25339316Smckusick 			    WEXITSTATUS(status) != 0)
25439316Smckusick 				return (WEXITSTATUS(status));
25539316Smckusick 			spec = "memfs";
25639316Smckusick 			goto out;
25739316Smckusick 		}
25839316Smckusick 		execve(_PATH_MEMFS, argv, envp);
25939316Smckusick 		perror(_PATH_MEMFS);
26039316Smckusick 		exit (1);
26139131Smckusick #endif /* MFS */
26239131Smckusick 
26339316Smckusick 	default:
26439316Smckusick 		if (opflags & ISBGRND)
26538632Smckusick 			exit(1);
26639316Smckusick 		fprintf(stderr, "%d: unknown mount type\n", mnttype);
26739316Smckusick 		exit(1);
26839316Smckusick 		/* NOTREACHED */
26938632Smckusick 
27039316Smckusick 	}
27139316Smckusick 	if (!fake && mount(mnttype, name, flags, argp)) {
27239316Smckusick 		if (opflags & ISBGRND)
27339316Smckusick 			exit(1);
27439316Smckusick 		fprintf(stderr, "%s on %s: ", spec, name);
27539316Smckusick 		switch (errno) {
27639316Smckusick 		case EMFILE:
27739316Smckusick 			fprintf(stderr, "Mount table full\n");
27839316Smckusick 			break;
27939316Smckusick 		case EINVAL:
28039316Smckusick 			fprintf(stderr, "Bogus super block\n");
28139316Smckusick 			break;
28239316Smckusick 		default:
28339316Smckusick 			perror((char *)NULL);
28439316Smckusick 			break;
2854460Sroot 		}
28639316Smckusick 		return(1);
2874460Sroot 	}
28835339Sbostic 
28939131Smckusick out:
29012808Ssam 	if (verbose)
29139316Smckusick 		prmount(spec, name, flags);
29235339Sbostic 
29338445Smckusick 	if (opflags & ISBGRND)
29438445Smckusick 		exit();
29538445Smckusick 	else
29638445Smckusick 		return(0);
2971057Sbill }
29835339Sbostic 
29935339Sbostic static
30039316Smckusick prmount(spec, name, flags)
30139316Smckusick 	char *spec, *name;
30239316Smckusick 	long flags;
30335339Sbostic {
30438632Smckusick 	register char *root;
30538632Smckusick 
30638445Smckusick 	if (opflags & ISBGRND)
30738445Smckusick 		return;
30838632Smckusick 	/*
30938632Smckusick 	 * trim trailing /'s and find last component of name
31038632Smckusick 	 */
31138632Smckusick 	for (root = index(spec, '\0'); *--root == '/';)
31238632Smckusick 		/* void */;
31338632Smckusick 	*++root = '\0';
31438632Smckusick 	if (root = rindex(spec, '/'))
31538632Smckusick 		spec = root + 1;
31638632Smckusick 	printf("%s on %s", spec, name);
31739316Smckusick 	if (flags & M_RDONLY)
31839316Smckusick 		printf(" (read-only)");
31939316Smckusick 	if (flags & M_NOEXEC)
32039316Smckusick 		printf(" (noexec)");
32139316Smckusick 	if (flags & M_NOSUID)
32239316Smckusick 		printf(" (nosuid)");
32339316Smckusick 	if (flags & M_NODEV)
32439316Smckusick 		printf(" (nodev)");
32539316Smckusick 	if (flags & M_SYNCHRONOUS)
32639316Smckusick 		printf(" (synchronous)");
32735339Sbostic 	printf("\n");
32835339Sbostic }
32935339Sbostic 
33039133Smckusick getmnttype(fstype)
33139133Smckusick 	char *fstype;
33239133Smckusick {
33339133Smckusick 
33439133Smckusick 	if (!strcmp(fstype, "ufs"))
33539133Smckusick 		return (MOUNT_UFS);
33639133Smckusick 	if (!strcmp(fstype, "nfs"))
33739133Smckusick 		return (MOUNT_NFS);
33839133Smckusick 	if (!strcmp(fstype, "mfs"))
33939133Smckusick 		return (MOUNT_MFS);
34039133Smckusick 	return (0);
34139133Smckusick }
34239133Smckusick 
34338632Smckusick usage()
34435339Sbostic {
34538632Smckusick 	fprintf(stderr, "usage: mount [-afrw]\nor mount [-frw] special | node\nor mount [-frw] special node\n");
34635339Sbostic 	exit(1);
34735339Sbostic }
34835339Sbostic 
34939316Smckusick getstdopts(options, flagp)
35039316Smckusick 	char *options;
35139316Smckusick 	long *flagp;
35239316Smckusick {
35339316Smckusick 	register char *opt;
35439316Smckusick 	int negative;
35539316Smckusick 	char *optbuf[BUFSIZ], *strtok();
35639316Smckusick 
35739316Smckusick 	strcpy(optbuf, options);
35839316Smckusick 	for (opt = strtok(optbuf, ","); opt; opt = strtok(NULL, ",")) {
35939316Smckusick 		if (opt[0] == 'n' && opt[1] == 'o') {
36039316Smckusick 			negative++;
36139316Smckusick 			opt += 2;
36239316Smckusick 		} else {
36339316Smckusick 			negative = 0;
36439316Smckusick 		}
36539316Smckusick 		if (!strcasecmp(opt, "exec")) {
36639316Smckusick 			if (negative)
36739316Smckusick 				*flagp |= M_NOEXEC;
36839316Smckusick 			continue;
36939316Smckusick 		}
37039316Smckusick 		if (!strcasecmp(opt, "suid")) {
37139316Smckusick 			if (negative)
37239316Smckusick 				*flagp |= M_NOSUID;
37339316Smckusick 			continue;
37439316Smckusick 		}
37539316Smckusick 		if (!strcasecmp(opt, "dev")) {
37639316Smckusick 			if (negative)
37739316Smckusick 				*flagp |= M_NODEV;
37839316Smckusick 			continue;
37939316Smckusick 		}
38039316Smckusick 		if (!strcasecmp(opt, "synchronous")) {
38139316Smckusick 			if (!negative)
38239316Smckusick 				*flagp |= M_SYNCHRONOUS;
38339316Smckusick 			continue;
38439316Smckusick 		}
38539316Smckusick 	}
38639316Smckusick }
38739316Smckusick 
38839131Smckusick getufsopts(options, flagp)
38939131Smckusick 	char *options;
39039131Smckusick 	long *flagp;
39139131Smckusick {
39239131Smckusick 
39339131Smckusick 	return;
39439131Smckusick }
39539131Smckusick 
39639131Smckusick #ifdef MFS
39739131Smckusick getmfsopts(options, argv)
39839131Smckusick 	char *options;
39939131Smckusick 	char **argv;
40039131Smckusick {
40139131Smckusick 	register int argc = 0;
40239131Smckusick 	register char *opt;
40339131Smckusick 	char *strtok();
40439131Smckusick 
40539131Smckusick 	for (opt = strtok(options, ","); opt; opt = strtok(NULL, ",")) {
40639131Smckusick 		if (opt[0] != '-')
40739131Smckusick 			continue;
40839131Smckusick 		argv[argc++] = opt;
40939131Smckusick 		if (opt[2] == '\0' || opt[2] != '=')
41039131Smckusick 			continue;
41139131Smckusick 		opt[2] = '\0';
41239131Smckusick 		argv[argc++] = &opt[3];
41339131Smckusick 	}
41439131Smckusick 	return (argc);
41539131Smckusick }
41639131Smckusick #endif /* MFS */
41739131Smckusick 
41838632Smckusick #ifdef NFS
41939131Smckusick /*
42039131Smckusick  * Handle the getoption arg.
42139131Smckusick  * Essentially update "opflags", "retrycnt" and "nfsargs"
42239131Smckusick  */
42339131Smckusick getnfsopts(optarg, nfsargsp, opflagsp, retrycntp)
42439131Smckusick 	char *optarg;
42539131Smckusick 	struct nfs_args *nfsargsp;
42639131Smckusick 	int *opflagsp;
42739131Smckusick 	int *retrycntp;
42839131Smckusick {
42939131Smckusick 	register char *cp, *nextcp;
43039131Smckusick 	int num;
43139131Smckusick 	char *nump;
43239131Smckusick 
43339131Smckusick 	cp = optarg;
43439131Smckusick 	while (cp != NULL && *cp != '\0') {
43539131Smckusick 		if ((nextcp = index(cp, ',')) != NULL)
43639131Smckusick 			*nextcp++ = '\0';
43739131Smckusick 		if ((nump = index(cp, '=')) != NULL) {
43839131Smckusick 			*nump++ = '\0';
43939131Smckusick 			num = atoi(nump);
44039131Smckusick 		} else
44139131Smckusick 			num = -1;
44239131Smckusick 		/*
44339131Smckusick 		 * Just test for a string match and do it
44439131Smckusick 		 */
44539131Smckusick 		if (!strcmp(cp, "bg")) {
44639131Smckusick 			*opflagsp |= BGRND;
44739131Smckusick 		} else if (!strcmp(cp, "soft")) {
44839131Smckusick 			nfsargsp->flags |= NFSMNT_SOFT;
44939131Smckusick 		} else if (!strcmp(cp, "intr")) {
45039131Smckusick 			nfsargsp->flags |= NFSMNT_INT;
45139131Smckusick 		} else if (!strcmp(cp, "retry") && num > 0) {
45239131Smckusick 			*retrycntp = num;
45339131Smckusick 		} else if (!strcmp(cp, "rsize") && num > 0) {
45439131Smckusick 			nfsargsp->rsize = num;
45539131Smckusick 			nfsargsp->flags |= NFSMNT_RSIZE;
45639131Smckusick 		} else if (!strcmp(cp, "wsize") && num > 0) {
45739131Smckusick 			nfsargsp->wsize = num;
45839131Smckusick 			nfsargsp->flags |= NFSMNT_WSIZE;
45939131Smckusick 		} else if (!strcmp(cp, "timeo") && num > 0) {
46039131Smckusick 			nfsargsp->timeo = num;
46139131Smckusick 			nfsargsp->flags |= NFSMNT_TIMEO;
46239131Smckusick 		} else if (!strcmp(cp, "retrans") && num > 0) {
46339131Smckusick 			nfsargsp->retrans = num;
46439131Smckusick 			nfsargsp->flags |= NFSMNT_RETRANS;
46539131Smckusick 		}
46639131Smckusick 		cp = nextcp;
46739131Smckusick 	}
46839131Smckusick }
46939131Smckusick 
47038632Smckusick char *
47138632Smckusick getnfsargs(spec)
47238632Smckusick 	char *spec;
47335339Sbostic {
47438632Smckusick 	register CLIENT *clp;
47538632Smckusick 	struct hostent *hp;
47638632Smckusick 	struct sockaddr_in saddr;
47738632Smckusick 	struct timeval pertry, try;
47838632Smckusick 	enum clnt_stat clnt_stat;
47938632Smckusick 	int so = RPC_ANYSOCK;
48038632Smckusick 	char *hostp, *delimp;
48138632Smckusick 	u_short tport;
48238632Smckusick 	struct nfhret nfhret;
48338632Smckusick 	char nam[MNAMELEN + 1];
48438632Smckusick 
48538632Smckusick 	strncpy(nam, spec, MNAMELEN);
48638632Smckusick 	nam[MNAMELEN] = '\0';
48738632Smckusick 	if ((delimp = index(spec, '@')) != NULL) {
48838632Smckusick 		hostp = delimp + 1;
48938632Smckusick 	} else if ((delimp = index(spec, ':')) != NULL) {
49038632Smckusick 		hostp = spec;
49138632Smckusick 		spec = delimp + 1;
49238632Smckusick 	} else {
49338632Smckusick 		fprintf(stderr,
49438632Smckusick 		    "No <host>:<dirpath> or <dirpath>@<host> spec\n");
49538632Smckusick 		return (0);
49638632Smckusick 	}
49738632Smckusick 	*delimp = '\0';
49838632Smckusick 	if ((hp = gethostbyname(hostp)) == NULL) {
49938632Smckusick 		fprintf(stderr, "Can't get net id for host\n");
50038632Smckusick 		return (0);
50138632Smckusick 	}
50238632Smckusick 	bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
50338632Smckusick 	nfhret.stat = EACCES;	/* Mark not yet successful */
50438632Smckusick 	while (retrycnt > 0) {
50538632Smckusick 		saddr.sin_family = AF_INET;
50638632Smckusick 		saddr.sin_port = htons(PMAPPORT);
50738632Smckusick 		if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
50838632Smckusick 		    NFS_VER2, IPPROTO_UDP)) == 0) {
50938632Smckusick 			if ((opflags & ISBGRND) == 0)
51038632Smckusick 				clnt_pcreateerror("NFS Portmap");
51138632Smckusick 		} else {
51238632Smckusick 			saddr.sin_port = 0;
51338632Smckusick 			pertry.tv_sec = 10;
51438632Smckusick 			pertry.tv_usec = 0;
51538632Smckusick 			if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
51638632Smckusick 			    RPCMNT_VER1, pertry, &so)) == NULL) {
51738632Smckusick 				if ((opflags & ISBGRND) == 0)
51838632Smckusick 					clnt_pcreateerror("Cannot MNT PRC");
51938632Smckusick 			} else {
52038632Smckusick 				clp->cl_auth = authunix_create_default();
52138632Smckusick 				try.tv_sec = 10;
52238632Smckusick 				try.tv_usec = 0;
52338632Smckusick 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
52438632Smckusick 				    xdr_dir, spec, xdr_fh, &nfhret, try);
52538632Smckusick 				if (clnt_stat != RPC_SUCCESS) {
52638632Smckusick 					if ((opflags & ISBGRND) == 0)
52738632Smckusick 						clnt_perror(clp, "Bad MNT RPC");
52838632Smckusick 				} else {
52938632Smckusick 					auth_destroy(clp->cl_auth);
53038632Smckusick 					clnt_destroy(clp);
53138632Smckusick 					retrycnt = 0;
53238632Smckusick 				}
53338632Smckusick 			}
53438632Smckusick 		}
53538632Smckusick 		if (--retrycnt > 0) {
53638632Smckusick 			if (opflags & BGRND) {
53738632Smckusick 				opflags &= ~BGRND;
53838632Smckusick 				if (fork())
53938632Smckusick 					return (0);
54038632Smckusick 				else
54138632Smckusick 					opflags |= ISBGRND;
54238632Smckusick 			}
54338632Smckusick 			sleep(10);
54438632Smckusick 		}
54538632Smckusick 	}
54638632Smckusick 	if (nfhret.stat) {
54738632Smckusick 		if (opflags & ISBGRND)
54838632Smckusick 			exit(1);
54938632Smckusick 		fprintf(stderr, "Can't access %s, errno=%d\n", spec,
55038632Smckusick 		    nfhret.stat);
55138632Smckusick 		return (0);
55238632Smckusick 	}
55338632Smckusick 	saddr.sin_port = htons(tport);
55438632Smckusick 	nfsargs.addr = &saddr;
55538632Smckusick 	nfsargs.fh = &nfhret.nfh;
55638632Smckusick 	nfsargs.hostname = nam;
55738632Smckusick 	return ((caddr_t)&nfsargs);
55835339Sbostic }
55938445Smckusick 
56038445Smckusick /*
56138445Smckusick  * xdr routines for mount rpc's
56238445Smckusick  */
56338445Smckusick xdr_dir(xdrsp, dirp)
56438445Smckusick 	XDR *xdrsp;
56538445Smckusick 	char *dirp;
56638445Smckusick {
56738445Smckusick 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
56838445Smckusick }
56938445Smckusick 
57038445Smckusick xdr_fh(xdrsp, np)
57138445Smckusick 	XDR *xdrsp;
57238445Smckusick 	struct nfhret *np;
57338445Smckusick {
57438445Smckusick 	if (!xdr_u_long(xdrsp, &(np->stat)))
57538445Smckusick 		return (0);
57638445Smckusick 	if (np->stat)
57738445Smckusick 		return (1);
57838445Smckusick 	return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
57938445Smckusick }
58038632Smckusick #endif /* NFS */
581