mount.c revision 1.11 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.11 1994/04/18 06:15:08 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;
78 char mnttype[MFSNAMELEN+1], *mntname, **envp;
79 char **vfslist;
80
81 char **makevfslist();
82 static void prmount();
83 struct statfs *getmntpt();
84 char *getmnttype __P((char *));
85
86 #ifdef NFS
87 int xdr_dir(), xdr_fh();
88 char *getnfsargs();
89 struct nfs_args nfsdefargs = {
90 (struct sockaddr *)0,
91 SOCK_DGRAM,
92 0,
93 (nfsv2fh_t *)0,
94 0,
95 NFS_WSIZE,
96 NFS_RSIZE,
97 NFS_TIMEO,
98 NFS_RETRANS,
99 (char *)0,
100 };
101
102 struct nfhret {
103 u_long stat;
104 nfsv2fh_t nfh;
105 };
106 #define DEF_RETRY 10000
107 int retrycnt;
108 #define BGRND 1
109 #define ISBGRND 2
110 int opflags = 0;
111 #endif
112
113 main(argc, argv, arge)
114 int argc;
115 char **argv;
116 char **arge;
117 {
118 extern char *optarg;
119 extern int optind;
120 register struct fstab *fs;
121 int all, ch, rval, flags, ret, pid, i;
122 long mntsize;
123 struct statfs *mntbuf;
124 char *type, *options = NULL;
125 FILE *pidfile;
126
127 envp = arge;
128 all = 0;
129 type = NULL;
130 strncpy(mnttype, MOUNT_UFS, MFSNAMELEN);
131 mnttype[MFSNAMELEN] = '\0';
132 mntname = MOUNT_UFS;
133 while ((ch = getopt(argc, argv, "afrwuvt:o:")) != EOF)
134 switch((char)ch) {
135 case 'a':
136 all = 1;
137 break;
138 case 'f':
139 fake = 1;
140 break;
141 case 'r':
142 type = FSTAB_RO;
143 break;
144 case 'u':
145 updateflg = MNT_UPDATE;
146 break;
147 case 'v':
148 verbose = 1;
149 break;
150 case 'w':
151 type = FSTAB_RW;
152 break;
153 case 'o':
154 options = optarg;
155 break;
156 case 't':
157 vfslist = makevfslist(optarg);
158 strncpy(mnttype, getmnttype(optarg), MFSNAMELEN);
159 mnttype[MFSNAMELEN] = '\0';
160 break;
161 case '?':
162 default:
163 usage();
164 /* NOTREACHED */
165 }
166 argc -= optind;
167 argv += optind;
168
169 /* NOSTRICT */
170
171 if (all) {
172 rval = 0;
173 while (fs = getfsent()) {
174 if (fs->fs_mntops && getnoauto(fs->fs_mntops))
175 continue;
176 if (BADTYPE(fs->fs_type))
177 continue;
178 if (badvfsname(fs->fs_vfstype, vfslist))
179 continue;
180 /* `/' is special, it's always mounted */
181 if (!strcmp(fs->fs_file, "/"))
182 flags = MNT_UPDATE;
183 else
184 flags = updateflg;
185 strncpy(mnttype, getmnttype(fs->fs_vfstype),
186 MFSNAMELEN);
187 mnttype[MFSNAMELEN] = '\0';
188 rval |= mountfs(fs->fs_spec, fs->fs_file, flags,
189 type, options, fs->fs_mntops);
190 }
191 exit(rval);
192 }
193
194 if (argc == 0) {
195 if (verbose || fake || type)
196 usage();
197 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
198 (void) fprintf(stderr,
199 "mount: cannot get mount information\n");
200 exit(1);
201 }
202 for (i = 0; i < mntsize; i++) {
203 if (badvfstype(mntbuf[i].f_fstypename, vfslist))
204 continue;
205 prmount(mntbuf[i].f_mntfromname, mntbuf[i].f_mntonname,
206 mntbuf[i].f_flags);
207 }
208 exit(0);
209 }
210
211 if (argc == 1 && updateflg) {
212 if ((mntbuf = getmntpt(*argv)) == NULL) {
213 (void) fprintf(stderr,
214 "mount: unknown special file or file system %s.\n",
215 *argv);
216 exit(1);
217 }
218 strncpy(mnttype, mntbuf->f_fstypename, MFSNAMELEN);
219 mnttype[MFSNAMELEN] = '\0';
220 #ifndef LETS_GET_SMALL
221 if (!strcmp(mntbuf->f_mntfromname, "root_device")) {
222 fs = getfsfile("/");
223 strcpy(mntbuf->f_mntfromname, fs->fs_spec);
224 }
225 #endif
226 ret = mountfs(mntbuf->f_mntfromname, mntbuf->f_mntonname,
227 updateflg, type, options, (char *)NULL);
228 } else if (argc == 1) {
229 #ifndef LETS_GET_SMALL
230 if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) {
231 (void) fprintf(stderr,
232 "mount: unknown special file or file system %s.\n",
233 *argv);
234 exit(1);
235 }
236 if (BADTYPE(fs->fs_type)) {
237 (void) fprintf(stderr,
238 "mount: %s has unknown file system type.\n", *argv);
239 exit(1);
240 }
241 strncpy(mnttype, getmnttype(fs->fs_vfstype), MFSNAMELEN);
242 mnttype[MFSNAMELEN] = '\0';
243 ret = mountfs(fs->fs_spec, fs->fs_file, updateflg,
244 type, options, fs->fs_mntops);
245 #else
246 exit(1);
247 #endif
248 } else if (argc != 2) {
249 usage();
250 ret = 1;
251 } else {
252 /*
253 * If -t flag has not been specified, and spec
254 * contains either a ':' or a '@' then assume that
255 * an NFS filesystem is being specified ala Sun.
256 */
257 if (vfslist == (char **)0 &&
258 (index(argv[0], ':') || index(argv[0], '@'))) {
259 strncpy(mnttype, MOUNT_NFS, MFSNAMELEN);
260 mnttype[MFSNAMELEN] = '\0';
261 }
262 ret = mountfs(argv[0], argv[1], updateflg, type, options,
263 (char *)NULL);
264 }
265 #ifndef LETS_GET_SMALL
266 if ((pidfile = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
267 pid = 0;
268 fscanf(pidfile, "%d", &pid);
269 fclose(pidfile);
270 if (pid > 0)
271 kill(pid, SIGHUP);
272 }
273 #endif
274 exit (ret);
275 }
276
277 mountfs(spec, name, flags, type, options, mntopts)
278 char *spec, *name, *type, *options, *mntopts;
279 int flags;
280 {
281 union wait status;
282 pid_t pid;
283 int argc, i;
284 struct ufs_args args;
285 #ifdef NFS
286 struct nfs_args nfsargs;
287 #endif
288 char *argp, *argv[50];
289 char execname[MAXPATHLEN + 1], flagval[12];
290
291 #ifdef NFS
292 nfsargs = nfsdefargs;
293 #endif
294 if (mntopts)
295 getstdopts(mntopts, &flags);
296 if (options)
297 getstdopts(options, &flags);
298 if (type)
299 getstdopts(type, &flags);
300
301 if (!strncmp(mnttype, MOUNT_UFS, MFSNAMELEN)) {
302 if (mntopts)
303 getufsopts(mntopts, &flags);
304 if (options)
305 getufsopts(options, &flags);
306 args.fspec = spec;
307 argp = (caddr_t)&args;
308 } else
309 #ifdef NFS
310 if (!strncmp(mnttype, MOUNT_NFS, MFSNAMELEN)) {
311 retrycnt = DEF_RETRY;
312 if (mntopts)
313 getnfsopts(mntopts, &nfsargs, &opflags, &retrycnt);
314 if (options)
315 getnfsopts(options, &nfsargs, &opflags, &retrycnt);
316 if ((argp = getnfsargs(spec, &nfsargs)) == NULL)
317 return (1);
318 } else
319 #endif /* NFS */
320 {
321 argv[0] = mntname;
322 argc = 1;
323 if (flags) {
324 argv[argc++] = "-F";
325 sprintf(flagval, "%d", flags);
326 argv[argc++] = flagval;
327 }
328 if (mntopts)
329 argc += getexecopts(mntopts, &argv[argc]);
330 if (options)
331 argc += getexecopts(options, &argv[argc]);
332 argv[argc++] = spec;
333 argv[argc++] = name;
334 argv[argc++] = NULL;
335 sprintf(execname, "%s/mount_%s", _PATH_EXECDIR, mntname);
336 if (verbose) {
337 (void)printf("exec: %s", execname);
338 for (i = 1; i < argc - 1; i++)
339 (void)printf(" %s", argv[i]);
340 (void)printf("\n");
341 }
342 if (!fake) {
343 if (pid = vfork()) {
344 if (pid == -1) {
345 perror("mount: vfork starting file system");
346 return (1);
347 }
348 if (waitpid(pid, (int *)&status, 0) != -1 &&
349 WIFEXITED(status) &&
350 WEXITSTATUS(status) != 0)
351 return (WEXITSTATUS(status));
352 spec = mntname;
353 goto out;
354 }
355 execve(execname, argv, envp);
356 (void) fprintf(stderr, "mount: cannot exec %s for %s: ",
357 execname, name);
358 perror((char *)NULL);
359 exit (1);
360 }
361 }
362 if (!fake && mount(mnttype, name, flags, argp)) {
363 #ifdef NFS
364 if (opflags & ISBGRND)
365 exit(1);
366 #endif
367 (void) fprintf(stderr, "%s on %s: ", spec, name);
368 switch (errno) {
369 case EMFILE:
370 (void) fprintf(stderr, "Mount table full\n");
371 break;
372 case EINVAL:
373 if (flags & MNT_UPDATE)
374 (void) fprintf(stderr, "Specified device %s\n",
375 "does not match mounted device");
376 else if (mnttype == MOUNT_UFS)
377 (void) fprintf(stderr, "Bogus super block\n");
378 else
379 perror((char *)NULL);
380 break;
381 default:
382 perror((char *)NULL);
383 break;
384 }
385 return(1);
386 }
387
388 out:
389 if (verbose)
390 prmount(spec, name, flags);
391
392 #ifdef NFS
393 if (opflags & ISBGRND)
394 exit(1);
395 #endif
396 return(0);
397 }
398
399 static void
400 prmount(spec, name, flags)
401 char *spec, *name;
402 register short flags;
403 {
404 register int first;
405
406 #ifdef NFS
407 if (opflags & ISBGRND)
408 return;
409 #endif
410 (void)printf("%s on %s", spec, name);
411 if (!(flags & MNT_VISFLAGMASK)) {
412 (void)printf("\n");
413 return;
414 }
415 first = 0;
416 #define PR(msg) (void)printf("%s%s", !first++ ? " (" : ", ", msg)
417 if (flags & MNT_RDONLY)
418 PR("read-only");
419 if (flags & MNT_NOEXEC)
420 PR("noexec");
421 if (flags & MNT_NOSUID)
422 PR("nosuid");
423 if (flags & MNT_NODEV)
424 PR("nodev");
425 if (flags & MNT_SYNCHRONOUS)
426 PR("synchronous");
427 if (flags & MNT_QUOTA)
428 PR("with quotas");
429 if (flags & MNT_LOCAL)
430 PR("local");
431 if (flags & MNT_EXPORTED)
432 if (flags & MNT_EXRDONLY)
433 PR("NFS exported read-only");
434 else
435 PR("NFS exported");
436 if (flags & MNT_UNION)
437 PR("union");
438 (void)printf(")\n");
439 }
440
441 char *
442 getmnttype(fstype)
443 char *fstype;
444 {
445
446 mntname = fstype;
447 return (fstype);
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 char *vfstype;
587 char **vfslist;
588 {
589
590 if (vfslist == 0)
591 return(0);
592 while (*vfslist) {
593 if (!strncmp(vfstype, getmnttype(*vfslist), MFSNAMELEN))
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