mount.c revision 1.8 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.8 1994/01/05 08:32:18 cgd 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 (fs->fs_mntops && getnoauto(fs->fs_mntops))
169 continue;
170 if (BADTYPE(fs->fs_type))
171 continue;
172 if (badvfsname(fs->fs_vfstype, vfslist))
173 continue;
174 /* `/' is special, it's always mounted */
175 if (!strcmp(fs->fs_file, "/"))
176 flags = MNT_UPDATE;
177 else
178 flags = updateflg;
179 mnttype = getmnttype(fs->fs_vfstype);
180 rval |= mountfs(fs->fs_spec, fs->fs_file, flags,
181 type, options, fs->fs_mntops);
182 }
183 exit(rval);
184 }
185
186 if (argc == 0) {
187 if (verbose || fake || type)
188 usage();
189 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
190 (void) fprintf(stderr,
191 "mount: cannot get mount information\n");
192 exit(1);
193 }
194 for (i = 0; i < mntsize; i++) {
195 if (badvfstype(mntbuf[i].f_type, vfslist))
196 continue;
197 prmount(mntbuf[i].f_mntfromname, mntbuf[i].f_mntonname,
198 mntbuf[i].f_flags);
199 }
200 exit(0);
201 }
202
203 if (argc == 1 && updateflg) {
204 if ((mntbuf = getmntpt(*argv)) == NULL) {
205 (void) fprintf(stderr,
206 "mount: unknown special file or file system %s.\n",
207 *argv);
208 exit(1);
209 }
210 mnttype = mntbuf->f_type;
211 #ifndef LETS_GET_SMALL
212 if (!strcmp(mntbuf->f_mntfromname, "root_device")) {
213 fs = getfsfile("/");
214 strcpy(mntbuf->f_mntfromname, fs->fs_spec);
215 }
216 #endif
217 ret = mountfs(mntbuf->f_mntfromname, mntbuf->f_mntonname,
218 updateflg, type, options, (char *)NULL);
219 } else if (argc == 1) {
220 #ifndef LETS_GET_SMALL
221 if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
222 (void) fprintf(stderr,
223 "mount: unknown special file or file system %s.\n",
224 *argv);
225 exit(1);
226 }
227 if (BADTYPE(fs->fs_type)) {
228 (void) fprintf(stderr,
229 "mount: %s has unknown file system type.\n", *argv);
230 exit(1);
231 }
232 mnttype = getmnttype(fs->fs_vfstype);
233 ret = mountfs(fs->fs_spec, fs->fs_file, updateflg,
234 type, options, fs->fs_mntops);
235 #else
236 exit(1);
237 #endif
238 } else if (argc != 2) {
239 usage();
240 ret = 1;
241 } else {
242 /*
243 * If -t flag has not been specified, and spec
244 * contains either a ':' or a '@' then assume that
245 * an NFS filesystem is being specified ala Sun.
246 */
247 if (vfslist == (char **)0 &&
248 (index(argv[0], ':') || index(argv[0], '@')))
249 mnttype = MOUNT_NFS;
250 ret = mountfs(argv[0], argv[1], updateflg, type, options,
251 (char *)NULL);
252 }
253 #ifndef LETS_GET_SMALL
254 if ((pidfile = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
255 pid = 0;
256 fscanf(pidfile, "%d", &pid);
257 fclose(pidfile);
258 if (pid > 0)
259 kill(pid, SIGHUP);
260 }
261 #endif
262 exit (ret);
263 }
264
265 mountfs(spec, name, flags, type, options, mntopts)
266 char *spec, *name, *type, *options, *mntopts;
267 int flags;
268 {
269 union wait status;
270 pid_t pid;
271 int argc, i;
272 struct ufs_args args;
273 #ifdef NFS
274 struct nfs_args nfsargs;
275 #endif
276 char *argp, *argv[50];
277 char execname[MAXPATHLEN + 1], flagval[12];
278
279 #ifdef NFS
280 nfsargs = nfsdefargs;
281 #endif
282 if (mntopts)
283 getstdopts(mntopts, &flags);
284 if (options)
285 getstdopts(options, &flags);
286 if (type)
287 getstdopts(type, &flags);
288 switch (mnttype) {
289 case MOUNT_UFS:
290 if (mntopts)
291 getufsopts(mntopts, &flags);
292 if (options)
293 getufsopts(options, &flags);
294 args.fspec = spec;
295 argp = (caddr_t)&args;
296 break;
297
298 #ifdef NFS
299 case MOUNT_NFS:
300 retrycnt = DEF_RETRY;
301 if (mntopts)
302 getnfsopts(mntopts, &nfsargs, &opflags, &retrycnt);
303 if (options)
304 getnfsopts(options, &nfsargs, &opflags, &retrycnt);
305 if (argp = getnfsargs(spec, &nfsargs))
306 break;
307 return (1);
308 #endif /* NFS */
309
310 #ifndef LETS_GET_SMALL
311 case MOUNT_MFS:
312 default:
313 argv[0] = mntname;
314 argc = 1;
315 if (flags) {
316 argv[argc++] = "-F";
317 sprintf(flagval, "%d", flags);
318 argv[argc++] = flagval;
319 }
320 if (mntopts)
321 argc += getexecopts(mntopts, &argv[argc]);
322 if (options)
323 argc += getexecopts(options, &argv[argc]);
324 argv[argc++] = spec;
325 argv[argc++] = name;
326 argv[argc++] = NULL;
327 sprintf(execname, "%s/mount_%s", _PATH_EXECDIR, mntname);
328 if (verbose) {
329 (void)printf("exec: %s", execname);
330 for (i = 1; i < argc - 1; i++)
331 (void)printf(" %s", argv[i]);
332 (void)printf("\n");
333 }
334 if (fake)
335 break;
336 if (pid = vfork()) {
337 if (pid == -1) {
338 perror("mount: vfork starting file system");
339 return (1);
340 }
341 if (waitpid(pid, (int *)&status, 0) != -1 &&
342 WIFEXITED(status) &&
343 WEXITSTATUS(status) != 0)
344 return (WEXITSTATUS(status));
345 spec = mntname;
346 goto out;
347 }
348 execve(execname, argv, envp);
349 (void) fprintf(stderr, "mount: cannot exec %s for %s: ",
350 execname, name);
351 perror((char *)NULL);
352 exit (1);
353 #endif
354 /* NOTREACHED */
355
356 }
357 if (!fake && mount(mnttype, name, flags, argp)) {
358 #ifdef NFS
359 if (opflags & ISBGRND)
360 exit(1);
361 #endif
362 (void) fprintf(stderr, "%s on %s: ", spec, name);
363 switch (errno) {
364 case EMFILE:
365 (void) fprintf(stderr, "Mount table full\n");
366 break;
367 case EINVAL:
368 if (flags & MNT_UPDATE)
369 (void) fprintf(stderr, "Specified device %s\n",
370 "does not match mounted device");
371 else if (mnttype == MOUNT_UFS)
372 (void) fprintf(stderr, "Bogus super block\n");
373 else
374 perror((char *)NULL);
375 break;
376 default:
377 perror((char *)NULL);
378 break;
379 }
380 return(1);
381 }
382
383 out:
384 if (verbose)
385 prmount(spec, name, flags);
386
387 #ifdef NFS
388 if (opflags & ISBGRND)
389 exit(1);
390 #endif
391 return(0);
392 }
393
394 static void
395 prmount(spec, name, flags)
396 char *spec, *name;
397 register short flags;
398 {
399 register int first;
400
401 #ifdef NFS
402 if (opflags & ISBGRND)
403 return;
404 #endif
405 (void)printf("%s on %s", spec, name);
406 if (!(flags & MNT_VISFLAGMASK)) {
407 (void)printf("\n");
408 return;
409 }
410 first = 0;
411 #define PR(msg) (void)printf("%s%s", !first++ ? " (" : ", ", msg)
412 if (flags & MNT_RDONLY)
413 PR("read-only");
414 if (flags & MNT_NOEXEC)
415 PR("noexec");
416 if (flags & MNT_NOSUID)
417 PR("nosuid");
418 if (flags & MNT_NODEV)
419 PR("nodev");
420 if (flags & MNT_SYNCHRONOUS)
421 PR("synchronous");
422 if (flags & MNT_QUOTA)
423 PR("with quotas");
424 if (flags & MNT_LOCAL)
425 PR("local");
426 if (flags & MNT_EXPORTED)
427 if (flags & MNT_EXRDONLY)
428 PR("NFS exported read-only");
429 else
430 PR("NFS exported");
431 if (flags & MNT_UNION)
432 PR("union");
433 (void)printf(")\n");
434 }
435
436 getmnttype(fstype)
437 char *fstype;
438 {
439
440 mntname = fstype;
441 if (!strcmp(fstype, "ufs"))
442 return (MOUNT_UFS);
443 if (!strcmp(fstype, "nfs"))
444 return (MOUNT_NFS);
445 if (!strcmp(fstype, "mfs"))
446 return (MOUNT_MFS);
447 return (0);
448 }
449
450 usage()
451 {
452
453 (void) fprintf(stderr,
454 "usage:\n mount %s %s\n mount %s\n mount %s\n",
455 "[ -frwu ] [ -t nfs | ufs | external_type ]",
456 "[ -o options ] special node",
457 "[ -afrwu ] [ -t nfs | ufs | external_type ]",
458 "[ -frwu ] special | node");
459 exit(1);
460 }
461
462 getnoauto(options)
463 char *options;
464 {
465 register char *opt;
466 int noauto = 0;
467 char optbuf[BUFSIZ];
468 (void)strcpy(optbuf, options);
469 for (opt = strtok(optbuf, ","); opt; opt = strtok((char *)NULL, ","))
470 if (!strcasecmp(opt, "noauto"))
471 noauto = 1;
472 else if (!strcasecmp(opt, "auto"))
473 noauto = 0;
474 return noauto;
475 }
476
477 getstdopts(options, flagp)
478 char *options;
479 int *flagp;
480 {
481 register char *opt;
482 int negative;
483 char optbuf[BUFSIZ];
484
485 (void)strcpy(optbuf, options);
486 for (opt = strtok(optbuf, ","); opt; opt = strtok((char *)NULL, ",")) {
487 if (opt[0] == 'n' && opt[1] == 'o') {
488 negative++;
489 opt += 2;
490 } else {
491 negative = 0;
492 }
493 if (!negative && !strcasecmp(opt, FSTAB_RO)) {
494 *flagp |= MNT_RDONLY;
495 continue;
496 }
497 if (!negative && !strcasecmp(opt, FSTAB_RW)) {
498 *flagp &= ~MNT_RDONLY;
499 continue;
500 }
501 if (!strcasecmp(opt, "exec")) {
502 if (negative)
503 *flagp |= MNT_NOEXEC;
504 else
505 *flagp &= ~MNT_NOEXEC;
506 continue;
507 }
508 if (!strcasecmp(opt, "suid")) {
509 if (negative)
510 *flagp |= MNT_NOSUID;
511 else
512 *flagp &= ~MNT_NOSUID;
513 continue;
514 }
515 if (!strcasecmp(opt, "dev")) {
516 if (negative)
517 *flagp |= MNT_NODEV;
518 else
519 *flagp &= ~MNT_NODEV;
520 continue;
521 }
522 if (!strcasecmp(opt, "synchronous")) {
523 if (!negative)
524 *flagp |= MNT_SYNCHRONOUS;
525 else
526 *flagp &= ~MNT_SYNCHRONOUS;
527 continue;
528 }
529 if (!strcasecmp(opt, "union")) {
530 if (!negative)
531 *flagp |= MNT_UNION;
532 else
533 *flagp &= ~MNT_UNION;
534 continue;
535 }
536 }
537 }
538
539 /* ARGSUSED */
540 getufsopts(options, flagp)
541 char *options;
542 int *flagp;
543 {
544 return;
545 }
546
547 getexecopts(options, argv)
548 char *options;
549 char **argv;
550 {
551 register int argc = 0;
552 register char *opt;
553
554 for (opt = strtok(options, ","); opt; opt = strtok((char *)NULL, ",")) {
555 if (opt[0] != '-')
556 continue;
557 argv[argc++] = opt;
558 if (opt[2] == '\0' || opt[2] != '=')
559 continue;
560 opt[2] = '\0';
561 argv[argc++] = &opt[3];
562 }
563 return (argc);
564 }
565
566 struct statfs *
567 getmntpt(name)
568 char *name;
569 {
570 long mntsize;
571 register long i;
572 struct statfs *mntbuf;
573
574 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
575 for (i = 0; i < mntsize; i++) {
576 if (!strcmp(mntbuf[i].f_mntfromname, name) ||
577 !strcmp(mntbuf[i].f_mntonname, name))
578 return (&mntbuf[i]);
579 }
580 return ((struct statfs *)0);
581 }
582
583 static int skipvfs;
584
585 badvfstype(vfstype, vfslist)
586 short vfstype;
587 char **vfslist;
588 {
589
590 if (vfslist == 0)
591 return(0);
592 while (*vfslist) {
593 if (vfstype == getmnttype(*vfslist))
594 return(skipvfs);
595 vfslist++;
596 }
597 return (!skipvfs);
598 }
599
600 badvfsname(vfsname, vfslist)
601 char *vfsname;
602 char **vfslist;
603 {
604
605 if (vfslist == 0)
606 return(0);
607 while (*vfslist) {
608 if (strcmp(vfsname, *vfslist) == 0)
609 return(skipvfs);
610 vfslist++;
611 }
612 return (!skipvfs);
613 }
614
615 char **
616 makevfslist(fslist)
617 char *fslist;
618 {
619 register char **av, *nextcp;
620 register int i;
621
622 if (fslist == NULL)
623 return (NULL);
624 if (fslist[0] == 'n' && fslist[1] == 'o') {
625 fslist += 2;
626 skipvfs = 1;
627 }
628 for (i = 0, nextcp = fslist; *nextcp; nextcp++)
629 if (*nextcp == ',')
630 i++;
631 av = (char **)malloc((size_t)(i+2) * sizeof(char *));
632 if (av == NULL)
633 return (NULL);
634 nextcp = fslist;
635 i = 0;
636 av[i++] = nextcp;
637 while (nextcp = index(nextcp, ',')) {
638 *nextcp++ = '\0';
639 av[i++] = nextcp;
640 }
641 av[i++] = 0;
642 return (av);
643 }
644
645 #ifdef NFS
646 exclusive(a, b)
647 char *a, *b;
648 {
649
650 (void) fprintf(stderr, "mount: Options %s, %s mutually exclusive\n",
651 a, b);
652 exit(1);
653 }
654
655 /*
656 * Handle the getoption arg.
657 * Essentially update "opflags", "retrycnt" and "nfsargs"
658 */
659 getnfsopts(optarg, nfsargsp, opflagsp, retrycntp)
660 char *optarg;
661 register struct nfs_args *nfsargsp;
662 int *opflagsp;
663 int *retrycntp;
664 {
665 register char *cp, *nextcp;
666 int num;
667 char *nump;
668
669 for (cp = optarg; cp != NULL && *cp != '\0'; cp = nextcp) {
670 if ((nextcp = index(cp, ',')) != NULL)
671 *nextcp++ = '\0';
672 if ((nump = index(cp, '=')) != NULL) {
673 *nump++ = '\0';
674 num = atoi(nump);
675 } else
676 num = -1;
677 /*
678 * Just test for a string match and do it
679 */
680 if (!strcmp(cp, "bg")) {
681 *opflagsp |= BGRND;
682 } else if (!strcmp(cp, "soft")) {
683 if (nfsargsp->flags & NFSMNT_SPONGY)
684 exclusive("soft, spongy");
685 nfsargsp->flags |= NFSMNT_SOFT;
686 } else if (!strcmp(cp, "spongy")) {
687 if (nfsargsp->flags & NFSMNT_SOFT)
688 exclusive("soft, spongy");
689 nfsargsp->flags |= NFSMNT_SPONGY;
690 } else if (!strcmp(cp, "compress")) {
691 nfsargsp->flags |= NFSMNT_COMPRESS;
692 } else if (!strcmp(cp, "intr")) {
693 nfsargsp->flags |= NFSMNT_INT;
694 } else if (!strcmp(cp, "tcp")) {
695 nfsargsp->sotype = SOCK_STREAM;
696 } else if (!strcmp(cp, "noconn")) {
697 nfsargsp->flags |= NFSMNT_NOCONN;
698 } else if (!strcmp(cp, "retry") && num > 0) {
699 *retrycntp = num;
700 } else if (!strcmp(cp, "rsize") && num > 0) {
701 nfsargsp->rsize = num;
702 nfsargsp->flags |= NFSMNT_RSIZE;
703 } else if (!strcmp(cp, "wsize") && num > 0) {
704 nfsargsp->wsize = num;
705 nfsargsp->flags |= NFSMNT_WSIZE;
706 } else if (!strcmp(cp, "timeo") && num > 0) {
707 nfsargsp->timeo = num;
708 nfsargsp->flags |= NFSMNT_TIMEO;
709 } else if (!strcmp(cp, "retrans") && num > 0) {
710 nfsargsp->retrans = num;
711 nfsargsp->flags |= NFSMNT_RETRANS;
712 }
713 }
714 if (nfsargsp->sotype == SOCK_DGRAM) {
715 if (nfsargsp->rsize > NFS_MAXDGRAMDATA)
716 nfsargsp->rsize = NFS_MAXDGRAMDATA;
717 if (nfsargsp->wsize > NFS_MAXDGRAMDATA)
718 nfsargsp->wsize = NFS_MAXDGRAMDATA;
719 }
720 }
721
722 char *
723 getnfsargs(spec, nfsargsp)
724 char *spec;
725 struct nfs_args *nfsargsp;
726 {
727 register CLIENT *clp;
728 struct hostent *hp;
729 static struct sockaddr_in saddr;
730 struct timeval pertry, try;
731 enum clnt_stat clnt_stat;
732 int so = RPC_ANYSOCK;
733 char *fsp, *hostp, *delimp;
734 u_short tport;
735 static struct nfhret nfhret;
736 static char nam[MNAMELEN + 1];
737 char buf[MAXPATHLEN + 1];
738
739 strncpy(buf, spec, MAXPATHLEN);
740 buf[MAXPATHLEN] = '\0';
741 strncpy(nam, spec, MNAMELEN);
742 nam[MNAMELEN] = '\0';
743 if ((delimp = index(buf, '@')) != NULL) {
744 hostp = delimp + 1;
745 fsp = buf;
746 } else if ((delimp = index(buf, ':')) != NULL) {
747 hostp = buf;
748 fsp = delimp + 1;
749 } else {
750 (void) fprintf(stderr,
751 "mount: No <host>:<dirpath> or <dirpath>@<host> spec\n");
752 return (0);
753 }
754 *delimp = '\0';
755 if ((hp = gethostbyname(hostp)) == NULL) {
756 (void) fprintf(stderr, "mount: Can't get net id for host\n");
757 return (0);
758 }
759 bzero((char *)&saddr, sizeof saddr);
760 bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
761 nfhret.stat = ETIMEDOUT; /* Mark not yet successful */
762 while (retrycnt > 0) {
763 saddr.sin_family = AF_INET;
764 saddr.sin_port = htons(PMAPPORT);
765 if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
766 NFS_VER2, IPPROTO_UDP)) == 0) {
767 if ((opflags & ISBGRND) == 0)
768 clnt_pcreateerror("NFS Portmap");
769 } else {
770 saddr.sin_port = 0;
771 pertry.tv_sec = 10;
772 pertry.tv_usec = 0;
773 if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
774 RPCMNT_VER1, pertry, &so)) == NULL) {
775 if ((opflags & ISBGRND) == 0)
776 clnt_pcreateerror("Cannot MNT PRC");
777 } else {
778 clp->cl_auth = authunix_create_default();
779 try.tv_sec = 10;
780 try.tv_usec = 0;
781 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
782 xdr_dir, fsp, xdr_fh, &nfhret, try);
783 if (clnt_stat != RPC_SUCCESS) {
784 if ((opflags & ISBGRND) == 0)
785 clnt_perror(clp, "Bad MNT RPC");
786 } else {
787 auth_destroy(clp->cl_auth);
788 clnt_destroy(clp);
789 retrycnt = 0;
790 }
791 }
792 }
793 if (--retrycnt > 0) {
794 if (opflags & BGRND) {
795 opflags &= ~BGRND;
796 if (fork())
797 return (0);
798 else
799 opflags |= ISBGRND;
800 }
801 sleep(10);
802 }
803 }
804 if (nfhret.stat) {
805 if (opflags & ISBGRND)
806 exit(1);
807 (void) fprintf(stderr, "Mount RPC error on %s: ", spec);
808 errno = nfhret.stat;
809 perror((char *)NULL);
810 return (0);
811 }
812 saddr.sin_port = htons(tport);
813 nfsargsp->addr = (struct sockaddr *) &saddr;
814 nfsargsp->fh = &nfhret.nfh;
815 nfsargsp->hostname = nam;
816 return ((caddr_t)nfsargsp);
817 }
818
819 /*
820 * xdr routines for mount rpc's
821 */
822 xdr_dir(xdrsp, dirp)
823 XDR *xdrsp;
824 char *dirp;
825 {
826 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
827 }
828
829 xdr_fh(xdrsp, np)
830 XDR *xdrsp;
831 struct nfhret *np;
832 {
833 if (!xdr_u_long(xdrsp, &(np->stat)))
834 return (0);
835 if (np->stat)
836 return (1);
837 return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
838 }
839 #endif /* NFS */
840