Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.4
      1 /*
      2  * Copyright (c) 1980, 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)mount.c	5.44 (Berkeley) 2/26/91";*/
     42 static char rcsid[] = "$Id: mount.c,v 1.4 1993/08/01 18:26:27 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/file.h>
     47 #include <sys/time.h>
     48 #include <sys/wait.h>
     49 #include <sys/errno.h>
     50 #include <sys/signal.h>
     51 #include <sys/mount.h>
     52 #ifdef NFS
     53 #include <sys/socket.h>
     54 #include <sys/socketvar.h>
     55 #include <netdb.h>
     56 #include <rpc/rpc.h>
     57 #include <rpc/pmap_clnt.h>
     58 #include <rpc/pmap_prot.h>
     59 #include <nfs/rpcv2.h>
     60 #include <nfs/nfsv2.h>
     61 #include <nfs/nfs.h>
     62 #endif
     63 #include <fstab.h>
     64 #include <string.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include "pathnames.h"
     68 
     69 #define DEFAULT_ROOTUID	-2
     70 
     71 #define	BADTYPE(type) \
     72 	(strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \
     73 	    strcmp(type, FSTAB_RQ))
     74 #define	SETTYPE(type) \
     75 	(!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ))
     76 
     77 int fake, verbose, updateflg, mnttype;
     78 char *mntname, **envp;
     79 char **vfslist, **makevfslist();
     80 static void prmount();
     81 
     82 #ifdef NFS
     83 int xdr_dir(), xdr_fh();
     84 char *getnfsargs();
     85 struct nfs_args nfsdefargs = {
     86 	(struct sockaddr *)0,
     87 	SOCK_DGRAM,
     88 	0,
     89 	(nfsv2fh_t *)0,
     90 	0,
     91 	NFS_WSIZE,
     92 	NFS_RSIZE,
     93 	NFS_TIMEO,
     94 	NFS_RETRANS,
     95 	(char *)0,
     96 };
     97 
     98 struct nfhret {
     99 	u_long	stat;
    100 	nfsv2fh_t nfh;
    101 };
    102 #define	DEF_RETRY	10000
    103 int retrycnt;
    104 #define	BGRND	1
    105 #define	ISBGRND	2
    106 int opflags = 0;
    107 #endif
    108 
    109 main(argc, argv, arge)
    110 	int argc;
    111 	char **argv;
    112 	char **arge;
    113 {
    114 	extern char *optarg;
    115 	extern int optind;
    116 	register struct fstab *fs;
    117 	int all, ch, rval, flags, ret, pid, i;
    118 	long mntsize;
    119 	struct statfs *mntbuf, *getmntpt();
    120 	char *type, *options = NULL;
    121 	FILE *pidfile;
    122 
    123 	envp = arge;
    124 	all = 0;
    125 	type = NULL;
    126 	mnttype = MOUNT_UFS;
    127 	mntname = "ufs";
    128 	while ((ch = getopt(argc, argv, "afrwuvt:o:")) != EOF)
    129 		switch((char)ch) {
    130 		case 'a':
    131 			all = 1;
    132 			break;
    133 		case 'f':
    134 			fake = 1;
    135 			break;
    136 		case 'r':
    137 			type = FSTAB_RO;
    138 			break;
    139 		case 'u':
    140 			updateflg = MNT_UPDATE;
    141 			break;
    142 		case 'v':
    143 			verbose = 1;
    144 			break;
    145 		case 'w':
    146 			type = FSTAB_RW;
    147 			break;
    148 		case 'o':
    149 			options = optarg;
    150 			break;
    151 		case 't':
    152 			vfslist = makevfslist(optarg);
    153 			mnttype = getmnttype(optarg);
    154 			break;
    155 		case '?':
    156 		default:
    157 			usage();
    158 			/* NOTREACHED */
    159 		}
    160 	argc -= optind;
    161 	argv += optind;
    162 
    163 	/* NOSTRICT */
    164 
    165 	if (all) {
    166 		rval = 0;
    167 		while (fs = getfsent()) {
    168 			if (BADTYPE(fs->fs_type))
    169 				continue;
    170 			if (badvfsname(fs->fs_vfstype, vfslist))
    171 				continue;
    172 			/* `/' is special, it's always mounted */
    173 			if (!strcmp(fs->fs_file, "/"))
    174 				flags = MNT_UPDATE;
    175 			else
    176 				flags = updateflg;
    177 			mnttype = getmnttype(fs->fs_vfstype);
    178 			rval |= mountfs(fs->fs_spec, fs->fs_file, flags,
    179 			    type, options, fs->fs_mntops);
    180 		}
    181 		exit(rval);
    182 	}
    183 
    184 	if (argc == 0) {
    185 		if (verbose || fake || type)
    186 			usage();
    187 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    188 			(void) fprintf(stderr,
    189 				"mount: cannot get mount information\n");
    190 			exit(1);
    191 		}
    192 		for (i = 0; i < mntsize; i++) {
    193 			if (badvfstype(mntbuf[i].f_type, vfslist))
    194 				continue;
    195 			prmount(mntbuf[i].f_mntfromname, mntbuf[i].f_mntonname,
    196 				mntbuf[i].f_flags);
    197 		}
    198 		exit(0);
    199 	}
    200 
    201 	if (argc == 1 && updateflg) {
    202 		if ((mntbuf = getmntpt(*argv)) == NULL) {
    203 			(void) fprintf(stderr,
    204 			    "mount: unknown special file or file system %s.\n",
    205 			    *argv);
    206 			exit(1);
    207 		}
    208 		mnttype = mntbuf->f_type;
    209 #ifndef LETS_GET_SMALL
    210 		if (!strcmp(mntbuf->f_mntfromname, "root_device")) {
    211 			fs = getfsfile("/");
    212 			strcpy(mntbuf->f_mntfromname, fs->fs_spec);
    213 		}
    214 #endif
    215 		ret = mountfs(mntbuf->f_mntfromname, mntbuf->f_mntonname,
    216 		    updateflg, type, options, (char *)NULL);
    217 	} else if (argc == 1) {
    218 #ifndef	LETS_GET_SMALL
    219 		if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
    220 			(void) fprintf(stderr,
    221 			    "mount: unknown special file or file system %s.\n",
    222 			    *argv);
    223 			exit(1);
    224 		}
    225 		if (BADTYPE(fs->fs_type)) {
    226 			(void) fprintf(stderr,
    227 			    "mount: %s has unknown file system type.\n", *argv);
    228 			exit(1);
    229 		}
    230 		mnttype = getmnttype(fs->fs_vfstype);
    231 		ret = mountfs(fs->fs_spec, fs->fs_file, updateflg,
    232 		    type, options, fs->fs_mntops);
    233 #else
    234 		exit(1);
    235 #endif
    236 	} else if (argc != 2) {
    237 		usage();
    238 		ret = 1;
    239 	} else {
    240 		/*
    241 		 * If -t flag has not been specified, and spec
    242 		 * contains either a ':' or a '@' then assume that
    243 		 * an NFS filesystem is being specified ala Sun.
    244 		 */
    245 		if (vfslist == (char **)0 &&
    246 		    (index(argv[0], ':') || index(argv[0], '@')))
    247 			mnttype = MOUNT_NFS;
    248 		ret = mountfs(argv[0], argv[1], updateflg, type, options,
    249 		    (char *)NULL);
    250 	}
    251 #ifndef LETS_GET_SMALL
    252 	if ((pidfile = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    253 		pid = 0;
    254 		fscanf(pidfile, "%d", &pid);
    255 		fclose(pidfile);
    256 		if (pid > 0)
    257 			kill(pid, SIGHUP);
    258 	}
    259 #endif
    260 	exit (ret);
    261 }
    262 
    263 mountfs(spec, name, flags, type, options, mntopts)
    264 	char *spec, *name, *type, *options, *mntopts;
    265 	int flags;
    266 {
    267 	union wait status;
    268 	pid_t pid;
    269 	int argc, i;
    270 	struct ufs_args args;
    271 #ifdef	NFS
    272 	struct nfs_args nfsargs;
    273 #endif
    274 	char *argp, *argv[50];
    275 	char execname[MAXPATHLEN + 1], flagval[12];
    276 
    277 #ifdef NFS
    278 	nfsargs = nfsdefargs;
    279 #endif
    280 	if (mntopts)
    281 		getstdopts(mntopts, &flags);
    282 	if (options)
    283 		getstdopts(options, &flags);
    284 	if (type)
    285 		getstdopts(type, &flags);
    286 	switch (mnttype) {
    287 	case MOUNT_UFS:
    288 		if (mntopts)
    289 			getufsopts(mntopts, &flags);
    290 		if (options)
    291 			getufsopts(options, &flags);
    292 		args.fspec = spec;
    293 		args.exroot = DEFAULT_ROOTUID;
    294 		if (flags & MNT_RDONLY)
    295 			args.exflags = MNT_EXRDONLY;
    296 		else
    297 			args.exflags = 0;
    298 		argp = (caddr_t)&args;
    299 		break;
    300 
    301 #ifdef NFS
    302 	case MOUNT_NFS:
    303 		retrycnt = DEF_RETRY;
    304 		if (mntopts)
    305 			getnfsopts(mntopts, &nfsargs, &opflags, &retrycnt);
    306 		if (options)
    307 			getnfsopts(options, &nfsargs, &opflags, &retrycnt);
    308 		if (argp = getnfsargs(spec, &nfsargs))
    309 			break;
    310 		return (1);
    311 #endif /* NFS */
    312 
    313 #ifndef	LETS_GET_SMALL
    314 	case MOUNT_MFS:
    315 	default:
    316 		argv[0] = mntname;
    317 		argc = 1;
    318 		if (flags) {
    319 			argv[argc++] = "-F";
    320 			sprintf(flagval, "%d", flags);
    321 			argv[argc++] = flagval;
    322 		}
    323 		if (mntopts)
    324 			argc += getexecopts(mntopts, &argv[argc]);
    325 		if (options)
    326 			argc += getexecopts(options, &argv[argc]);
    327 		argv[argc++] = spec;
    328 		argv[argc++] = name;
    329 		argv[argc++] = NULL;
    330 		sprintf(execname, "%s/mount_%s", _PATH_EXECDIR, mntname);
    331 		if (verbose) {
    332 			(void)printf("exec: %s", execname);
    333 			for (i = 1; i < argc - 1; i++)
    334 				(void)printf(" %s", argv[i]);
    335 			(void)printf("\n");
    336 		}
    337 		if (fake)
    338 			break;
    339 		if (pid = vfork()) {
    340 			if (pid == -1) {
    341 				perror("mount: vfork starting file system");
    342 				return (1);
    343 			}
    344 			if (waitpid(pid, (int *)&status, 0) != -1 &&
    345 			    WIFEXITED(status) &&
    346 			    WEXITSTATUS(status) != 0)
    347 				return (WEXITSTATUS(status));
    348 			spec = mntname;
    349 			goto out;
    350 		}
    351 		execve(execname, argv, envp);
    352 		(void) fprintf(stderr, "mount: cannot exec %s for %s: ",
    353 			execname, name);
    354 		perror((char *)NULL);
    355 		exit (1);
    356 #endif
    357 		/* NOTREACHED */
    358 
    359 	}
    360 	if (!fake && mount(mnttype, name, flags, argp)) {
    361 #ifdef NFS
    362 		if (opflags & ISBGRND)
    363 			exit(1);
    364 #endif
    365 		(void) fprintf(stderr, "%s on %s: ", spec, name);
    366 		switch (errno) {
    367 		case EMFILE:
    368 			(void) fprintf(stderr, "Mount table full\n");
    369 			break;
    370 		case EINVAL:
    371 			if (flags & MNT_UPDATE)
    372 				(void) fprintf(stderr, "Specified device %s\n",
    373 					"does not match mounted device");
    374 			else if (mnttype == MOUNT_UFS)
    375 				(void) fprintf(stderr, "Bogus super block\n");
    376 			else
    377 				perror((char *)NULL);
    378 			break;
    379 		default:
    380 			perror((char *)NULL);
    381 			break;
    382 		}
    383 		return(1);
    384 	}
    385 
    386 out:
    387 	if (verbose)
    388 		prmount(spec, name, flags);
    389 
    390 #ifdef NFS
    391 	if (opflags & ISBGRND)
    392 		exit(1);
    393 #endif
    394 	return(0);
    395 }
    396 
    397 static void
    398 prmount(spec, name, flags)
    399 	char *spec, *name;
    400 	register short flags;
    401 {
    402 	register int first;
    403 
    404 #ifdef NFS
    405 	if (opflags & ISBGRND)
    406 		return;
    407 #endif
    408 	(void)printf("%s on %s", spec, name);
    409 	if (!(flags & MNT_VISFLAGMASK)) {
    410 		(void)printf("\n");
    411 		return;
    412 	}
    413 	first = 0;
    414 #define	PR(msg)	(void)printf("%s%s", !first++ ? " (" : ", ", msg)
    415 	if (flags & MNT_RDONLY)
    416 		PR("read-only");
    417 	if (flags & MNT_NOEXEC)
    418 		PR("noexec");
    419 	if (flags & MNT_NOSUID)
    420 		PR("nosuid");
    421 	if (flags & MNT_NODEV)
    422 		PR("nodev");
    423 	if (flags & MNT_SYNCHRONOUS)
    424 		PR("synchronous");
    425 	if (flags & MNT_QUOTA)
    426 		PR("with quotas");
    427 	if (flags & MNT_LOCAL)
    428 		PR("local");
    429 	if (flags & MNT_EXPORTED)
    430 		if (flags & MNT_EXRDONLY)
    431 			PR("NFS exported read-only");
    432 		else
    433 			PR("NFS exported");
    434 	(void)printf(")\n");
    435 }
    436 
    437 getmnttype(fstype)
    438 	char *fstype;
    439 {
    440 
    441 	mntname = fstype;
    442 	if (!strcmp(fstype, "ufs"))
    443 		return (MOUNT_UFS);
    444 	if (!strcmp(fstype, "nfs"))
    445 		return (MOUNT_NFS);
    446 	if (!strcmp(fstype, "mfs"))
    447 		return (MOUNT_MFS);
    448 	return (0);
    449 }
    450 
    451 usage()
    452 {
    453 
    454 	(void) fprintf(stderr,
    455 		"usage:\n  mount %s %s\n  mount %s\n  mount %s\n",
    456 		"[ -frwu ] [ -t nfs | ufs | external_type ]",
    457 		"[ -o options ] special node",
    458 		"[ -afrwu ] [ -t nfs | ufs | external_type ]",
    459 		"[ -frwu ] special | node");
    460 	exit(1);
    461 }
    462 
    463 getstdopts(options, flagp)
    464 	char *options;
    465 	int *flagp;
    466 {
    467 	register char *opt;
    468 	int negative;
    469 	char optbuf[BUFSIZ];
    470 
    471 	(void)strcpy(optbuf, options);
    472 	for (opt = strtok(optbuf, ","); opt; opt = strtok((char *)NULL, ",")) {
    473 		if (opt[0] == 'n' && opt[1] == 'o') {
    474 			negative++;
    475 			opt += 2;
    476 		} else {
    477 			negative = 0;
    478 		}
    479 		if (!negative && !strcasecmp(opt, FSTAB_RO)) {
    480 			*flagp |= MNT_RDONLY;
    481 			continue;
    482 		}
    483 		if (!negative && !strcasecmp(opt, FSTAB_RW)) {
    484 			*flagp &= ~MNT_RDONLY;
    485 			continue;
    486 		}
    487 		if (!strcasecmp(opt, "exec")) {
    488 			if (negative)
    489 				*flagp |= MNT_NOEXEC;
    490 			else
    491 				*flagp &= ~MNT_NOEXEC;
    492 			continue;
    493 		}
    494 		if (!strcasecmp(opt, "suid")) {
    495 			if (negative)
    496 				*flagp |= MNT_NOSUID;
    497 			else
    498 				*flagp &= ~MNT_NOSUID;
    499 			continue;
    500 		}
    501 		if (!strcasecmp(opt, "dev")) {
    502 			if (negative)
    503 				*flagp |= MNT_NODEV;
    504 			else
    505 				*flagp &= ~MNT_NODEV;
    506 			continue;
    507 		}
    508 		if (!strcasecmp(opt, "synchronous")) {
    509 			if (!negative)
    510 				*flagp |= MNT_SYNCHRONOUS;
    511 			else
    512 				*flagp &= ~MNT_SYNCHRONOUS;
    513 			continue;
    514 		}
    515 	}
    516 }
    517 
    518 /* ARGSUSED */
    519 getufsopts(options, flagp)
    520 	char *options;
    521 	int *flagp;
    522 {
    523 	return;
    524 }
    525 
    526 getexecopts(options, argv)
    527 	char *options;
    528 	char **argv;
    529 {
    530 	register int argc = 0;
    531 	register char *opt;
    532 
    533 	for (opt = strtok(options, ","); opt; opt = strtok((char *)NULL, ",")) {
    534 		if (opt[0] != '-')
    535 			continue;
    536 		argv[argc++] = opt;
    537 		if (opt[2] == '\0' || opt[2] != '=')
    538 			continue;
    539 		opt[2] = '\0';
    540 		argv[argc++] = &opt[3];
    541 	}
    542 	return (argc);
    543 }
    544 
    545 struct statfs *
    546 getmntpt(name)
    547 	char *name;
    548 {
    549 	long mntsize;
    550 	register long i;
    551 	struct statfs *mntbuf;
    552 
    553 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    554 	for (i = 0; i < mntsize; i++) {
    555 		if (!strcmp(mntbuf[i].f_mntfromname, name) ||
    556 		    !strcmp(mntbuf[i].f_mntonname, name))
    557 			return (&mntbuf[i]);
    558 	}
    559 	return ((struct statfs *)0);
    560 }
    561 
    562 static int skipvfs;
    563 
    564 badvfstype(vfstype, vfslist)
    565 	short vfstype;
    566 	char **vfslist;
    567 {
    568 
    569 	if (vfslist == 0)
    570 		return(0);
    571 	while (*vfslist) {
    572 		if (vfstype == getmnttype(*vfslist))
    573 			return(skipvfs);
    574 		vfslist++;
    575 	}
    576 	return (!skipvfs);
    577 }
    578 
    579 badvfsname(vfsname, vfslist)
    580 	char *vfsname;
    581 	char **vfslist;
    582 {
    583 
    584 	if (vfslist == 0)
    585 		return(0);
    586 	while (*vfslist) {
    587 		if (strcmp(vfsname, *vfslist) == 0)
    588 			return(skipvfs);
    589 		vfslist++;
    590 	}
    591 	return (!skipvfs);
    592 }
    593 
    594 char **
    595 makevfslist(fslist)
    596 	char *fslist;
    597 {
    598 	register char **av, *nextcp;
    599 	register int i;
    600 
    601 	if (fslist == NULL)
    602 		return (NULL);
    603 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    604 		fslist += 2;
    605 		skipvfs = 1;
    606 	}
    607 	for (i = 0, nextcp = fslist; *nextcp; nextcp++)
    608 		if (*nextcp == ',')
    609 			i++;
    610 	av = (char **)malloc((size_t)(i+2) * sizeof(char *));
    611 	if (av == NULL)
    612 		return (NULL);
    613 	nextcp = fslist;
    614 	i = 0;
    615 	av[i++] = nextcp;
    616 	while (nextcp = index(nextcp, ',')) {
    617 		*nextcp++ = '\0';
    618 		av[i++] = nextcp;
    619 	}
    620 	av[i++] = 0;
    621 	return (av);
    622 }
    623 
    624 #ifdef NFS
    625 exclusive(a, b)
    626 	char *a, *b;
    627 {
    628 
    629 	(void) fprintf(stderr, "mount: Options %s, %s mutually exclusive\n",
    630 	    a, b);
    631 	exit(1);
    632 }
    633 
    634 /*
    635  * Handle the getoption arg.
    636  * Essentially update "opflags", "retrycnt" and "nfsargs"
    637  */
    638 getnfsopts(optarg, nfsargsp, opflagsp, retrycntp)
    639 	char *optarg;
    640 	register struct nfs_args *nfsargsp;
    641 	int *opflagsp;
    642 	int *retrycntp;
    643 {
    644 	register char *cp, *nextcp;
    645 	int num;
    646 	char *nump;
    647 
    648 	for (cp = optarg; cp != NULL && *cp != '\0'; cp = nextcp) {
    649 		if ((nextcp = index(cp, ',')) != NULL)
    650 			*nextcp++ = '\0';
    651 		if ((nump = index(cp, '=')) != NULL) {
    652 			*nump++ = '\0';
    653 			num = atoi(nump);
    654 		} else
    655 			num = -1;
    656 		/*
    657 		 * Just test for a string match and do it
    658 		 */
    659 		if (!strcmp(cp, "bg")) {
    660 			*opflagsp |= BGRND;
    661 		} else if (!strcmp(cp, "soft")) {
    662 			if (nfsargsp->flags & NFSMNT_SPONGY)
    663 				exclusive("soft, spongy");
    664 			nfsargsp->flags |= NFSMNT_SOFT;
    665 		} else if (!strcmp(cp, "spongy")) {
    666 			if (nfsargsp->flags & NFSMNT_SOFT)
    667 				exclusive("soft, spongy");
    668 			nfsargsp->flags |= NFSMNT_SPONGY;
    669 		} else if (!strcmp(cp, "compress")) {
    670 			nfsargsp->flags |= NFSMNT_COMPRESS;
    671 		} else if (!strcmp(cp, "intr")) {
    672 			nfsargsp->flags |= NFSMNT_INT;
    673 		} else if (!strcmp(cp, "tcp")) {
    674 			nfsargsp->sotype = SOCK_STREAM;
    675 		} else if (!strcmp(cp, "noconn")) {
    676 			nfsargsp->flags |= NFSMNT_NOCONN;
    677 		} else if (!strcmp(cp, "retry") && num > 0) {
    678 			*retrycntp = num;
    679 		} else if (!strcmp(cp, "rsize") && num > 0) {
    680 			nfsargsp->rsize = num;
    681 			nfsargsp->flags |= NFSMNT_RSIZE;
    682 		} else if (!strcmp(cp, "wsize") && num > 0) {
    683 			nfsargsp->wsize = num;
    684 			nfsargsp->flags |= NFSMNT_WSIZE;
    685 		} else if (!strcmp(cp, "timeo") && num > 0) {
    686 			nfsargsp->timeo = num;
    687 			nfsargsp->flags |= NFSMNT_TIMEO;
    688 		} else if (!strcmp(cp, "retrans") && num > 0) {
    689 			nfsargsp->retrans = num;
    690 			nfsargsp->flags |= NFSMNT_RETRANS;
    691 		}
    692 	}
    693 	if (nfsargsp->sotype == SOCK_DGRAM) {
    694 		if (nfsargsp->rsize > NFS_MAXDGRAMDATA)
    695 			nfsargsp->rsize = NFS_MAXDGRAMDATA;
    696 		if (nfsargsp->wsize > NFS_MAXDGRAMDATA)
    697 			nfsargsp->wsize = NFS_MAXDGRAMDATA;
    698 	}
    699 }
    700 
    701 char *
    702 getnfsargs(spec, nfsargsp)
    703 	char *spec;
    704 	struct nfs_args *nfsargsp;
    705 {
    706 	register CLIENT *clp;
    707 	struct hostent *hp;
    708 	static struct sockaddr_in saddr;
    709 	struct timeval pertry, try;
    710 	enum clnt_stat clnt_stat;
    711 	int so = RPC_ANYSOCK;
    712 	char *fsp, *hostp, *delimp;
    713 	u_short tport;
    714 	static struct nfhret nfhret;
    715 	static char nam[MNAMELEN + 1];
    716 	char buf[MAXPATHLEN + 1];
    717 
    718 	strncpy(buf, spec, MAXPATHLEN);
    719 	buf[MAXPATHLEN] = '\0';
    720 	strncpy(nam, spec, MNAMELEN);
    721 	nam[MNAMELEN] = '\0';
    722 	if ((delimp = index(buf, '@')) != NULL) {
    723 		hostp = delimp + 1;
    724 		fsp = buf;
    725 	} else if ((delimp = index(buf, ':')) != NULL) {
    726 		hostp = buf;
    727 		fsp = delimp + 1;
    728 	} else {
    729 		(void) fprintf(stderr,
    730 		    "mount: No <host>:<dirpath> or <dirpath>@<host> spec\n");
    731 		return (0);
    732 	}
    733 	*delimp = '\0';
    734 	if ((hp = gethostbyname(hostp)) == NULL) {
    735 		(void) fprintf(stderr, "mount: Can't get net id for host\n");
    736 		return (0);
    737 	}
    738 	bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
    739 	nfhret.stat = ETIMEDOUT;	/* Mark not yet successful */
    740 	while (retrycnt > 0) {
    741 		saddr.sin_family = AF_INET;
    742 		saddr.sin_port = htons(PMAPPORT);
    743 		if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
    744 		    NFS_VER2, IPPROTO_UDP)) == 0) {
    745 			if ((opflags & ISBGRND) == 0)
    746 				clnt_pcreateerror("NFS Portmap");
    747 		} else {
    748 			saddr.sin_port = 0;
    749 			pertry.tv_sec = 10;
    750 			pertry.tv_usec = 0;
    751 			if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
    752 			    RPCMNT_VER1, pertry, &so)) == NULL) {
    753 				if ((opflags & ISBGRND) == 0)
    754 					clnt_pcreateerror("Cannot MNT PRC");
    755 			} else {
    756 				clp->cl_auth = authunix_create_default();
    757 				try.tv_sec = 10;
    758 				try.tv_usec = 0;
    759 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
    760 				    xdr_dir, fsp, xdr_fh, &nfhret, try);
    761 				if (clnt_stat != RPC_SUCCESS) {
    762 					if ((opflags & ISBGRND) == 0)
    763 						clnt_perror(clp, "Bad MNT RPC");
    764 				} else {
    765 					auth_destroy(clp->cl_auth);
    766 					clnt_destroy(clp);
    767 					retrycnt = 0;
    768 				}
    769 			}
    770 		}
    771 		if (--retrycnt > 0) {
    772 			if (opflags & BGRND) {
    773 				opflags &= ~BGRND;
    774 				if (fork())
    775 					return (0);
    776 				else
    777 					opflags |= ISBGRND;
    778 			}
    779 			sleep(10);
    780 		}
    781 	}
    782 	if (nfhret.stat) {
    783 		if (opflags & ISBGRND)
    784 			exit(1);
    785 		(void) fprintf(stderr, "Mount RPC error on %s: ", spec);
    786 		errno = nfhret.stat;
    787 		perror((char *)NULL);
    788 		return (0);
    789 	}
    790 	saddr.sin_port = htons(tport);
    791 	nfsargsp->addr = (struct sockaddr *) &saddr;
    792 	nfsargsp->fh = &nfhret.nfh;
    793 	nfsargsp->hostname = nam;
    794 	return ((caddr_t)nfsargsp);
    795 }
    796 
    797 /*
    798  * xdr routines for mount rpc's
    799  */
    800 xdr_dir(xdrsp, dirp)
    801 	XDR *xdrsp;
    802 	char *dirp;
    803 {
    804 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    805 }
    806 
    807 xdr_fh(xdrsp, np)
    808 	XDR *xdrsp;
    809 	struct nfhret *np;
    810 {
    811 	if (!xdr_u_long(xdrsp, &(np->stat)))
    812 		return (0);
    813 	if (np->stat)
    814 		return (1);
    815 	return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
    816 }
    817 #endif /* NFS */
    818