121156Sdist /* 239322Smckusick * Copyright (c) 1980, 1989 The Regents of the University of California. 339322Smckusick * All rights reserved. 439322Smckusick * 539322Smckusick * Redistribution and use in source and binary forms are permitted 639322Smckusick * provided that the above copyright notice and this paragraph are 739322Smckusick * duplicated in all such forms and that any documentation, 839322Smckusick * advertising materials, and other materials related to such 939322Smckusick * distribution and use acknowledge that the software was developed 1039322Smckusick * by the University of California, Berkeley. The name of the 1139322Smckusick * University may not be used to endorse or promote products derived 1239322Smckusick * from this software without specific prior written permission. 1339322Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1439322Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1539322Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621156Sdist */ 1721156Sdist 1810811Ssam #ifndef lint 1921156Sdist char copyright[] = 2039322Smckusick "@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\ 2121156Sdist All rights reserved.\n"; 2239322Smckusick #endif /* not lint */ 231057Sbill 2421156Sdist #ifndef lint 25*39465Smckusick static char sccsid[] = "@(#)mount.c 5.18 (Berkeley) 10/31/89"; 2639322Smckusick #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 5939333Smckusick int fake, verbose, updateflg, mnttype; 6039322Smckusick 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; 9539333Smckusick int all, ch, rval, flags, i; 9638632Smckusick long mntsize; 97*39465Smckusick struct statfs *mntbuf, *getmntpt(); 9839131Smckusick char *type, *options = NULL; 991057Sbill 10039131Smckusick envp = arge; 10135339Sbostic all = 0; 10235339Sbostic type = NULL; 10338445Smckusick mnttype = MOUNT_UFS; 10439322Smckusick mntname = "ufs"; 10539333Smckusick while ((ch = getopt(argc, argv, "afrwuvt: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; 11639333Smckusick case 'u': 11739333Smckusick updateflg = M_UPDATE; 11839333Smckusick break; 11935339Sbostic case 'v': 12035339Sbostic verbose = 1; 12135339Sbostic break; 12235339Sbostic case 'w': 12335339Sbostic type = FSTAB_RW; 12435339Sbostic break; 12539131Smckusick case 'o': 12639131Smckusick options = optarg; 12739131Smckusick break; 12838445Smckusick case 't': 12939322Smckusick mnttype = getmnttype(optarg); 13039322Smckusick break; 13135339Sbostic case '?': 13235339Sbostic default: 13335339Sbostic usage(); 13439131Smckusick /* NOTREACHED */ 1355073Sroot } 13635339Sbostic argc -= optind; 13735339Sbostic argv += optind; 13835339Sbostic 13935339Sbostic /* NOSTRICT */ 14035339Sbostic 1414460Sroot if (all) { 14235369Sbostic rval = 0; 14339333Smckusick while (fs = getfsent()) { 14435372Sbostic if (BADTYPE(fs->fs_type)) 14535372Sbostic continue; 14635372Sbostic /* `/' is special, it's always mounted */ 14735372Sbostic if (!strcmp(fs->fs_file, "/")) 14839333Smckusick flags = M_UPDATE; 14939333Smckusick else 15039333Smckusick flags = updateflg; 15139333Smckusick mnttype = getmnttype(fs->fs_vfstype); 15239333Smckusick rval |= mountfs(fs->fs_spec, fs->fs_file, flags, 15339333Smckusick 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 172*39465Smckusick if (argc == 1 && updateflg) { 173*39465Smckusick if ((mntbuf = getmntpt(*argv)) == NULL) { 174*39465Smckusick fprintf(stderr, 175*39465Smckusick "mount: unknown special file or file system %s.\n", 176*39465Smckusick *argv); 177*39465Smckusick exit(1); 178*39465Smckusick } 179*39465Smckusick mnttype = mntbuf->f_type; 180*39465Smckusick exit(mountfs(mntbuf->f_mntfromname, mntbuf->f_mntonname, 181*39465Smckusick updateflg, type, options, NULL)); 182*39465Smckusick } 183*39465Smckusick 18435339Sbostic if (argc == 1) { 18535339Sbostic if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) { 18635339Sbostic fprintf(stderr, 18735339Sbostic "mount: unknown special file or file system %s.\n", 18835339Sbostic *argv); 18935339Sbostic exit(1); 19035339Sbostic } 19135339Sbostic if (BADTYPE(fs->fs_type)) { 19235339Sbostic fprintf(stderr, 19335339Sbostic "mount: %s has unknown file system type.\n", *argv); 19435339Sbostic exit(1); 19535339Sbostic } 19639333Smckusick mnttype = getmnttype(fs->fs_vfstype); 19739333Smckusick exit(mountfs(fs->fs_spec, fs->fs_file, updateflg, 19839333Smckusick type, options, fs->fs_mntops)); 19912808Ssam } 2001057Sbill 20135339Sbostic if (argc != 2) 20235339Sbostic usage(); 20312808Ssam 20439333Smckusick exit(mountfs(argv[0], argv[1], updateflg, type, options, NULL)); 20512808Ssam } 20612808Ssam 20739333Smckusick mountfs(spec, name, flags, type, options, mntopts) 20839131Smckusick char *spec, *name, *type, *options, *mntopts; 20939333Smckusick int flags; 21012808Ssam { 21135339Sbostic extern int errno; 21235339Sbostic register int cnt; 21339333Smckusick int argc, status, i; 21438070Smckusick struct ufs_args args; 21539131Smckusick char *argp, *argv[50]; 21639322Smckusick char execname[MAXPATHLEN + 1], flagval[12]; 2171057Sbill 21839333Smckusick if (mntopts) 21939333Smckusick getstdopts(mntopts, &flags); 22039316Smckusick if (options) 22139316Smckusick getstdopts(options, &flags); 22239333Smckusick if (type) 22339333Smckusick getstdopts(type, &flags); 22439316Smckusick switch (mnttype) { 22539316Smckusick case MOUNT_UFS: 22639333Smckusick if (mntopts) 22739333Smckusick getufsopts(mntopts, &flags); 22839316Smckusick if (options) 22939316Smckusick getufsopts(options, &flags); 23039316Smckusick args.fspec = spec; 23139316Smckusick argp = (caddr_t)&args; 23239316Smckusick break; 23338632Smckusick 23438632Smckusick #ifdef NFS 23539316Smckusick case MOUNT_NFS: 23639333Smckusick if (mntopts) 23739333Smckusick getnfsopts(mntopts, &nfsargs, &opflags, &retrycnt); 23839316Smckusick if (options) 23939333Smckusick getnfsopts(options, &nfsargs, &opflags, &retrycnt); 24039316Smckusick if (argp = getnfsargs(spec, name, type)) 24139316Smckusick break; 24239316Smckusick return (1); 24338632Smckusick #endif /* NFS */ 24438632Smckusick 24539316Smckusick case MOUNT_MFS: 24639322Smckusick default: 24739322Smckusick argv[0] = mntname; 24839329Smckusick argc = 1; 24939322Smckusick if (flags) { 25039322Smckusick argv[argc++] = "-F"; 25139322Smckusick sprintf(flagval, "%d", flags); 25239322Smckusick argv[argc++] = flagval; 25339322Smckusick } 25439333Smckusick if (mntopts) 25539333Smckusick argc += getexecopts(mntopts, &argv[argc]); 25639329Smckusick if (options) 25739329Smckusick argc += getexecopts(options, &argv[argc]); 25839316Smckusick argv[argc++] = spec; 25939316Smckusick argv[argc++] = name; 26039322Smckusick sprintf(execname, "%s/%s", _PATH_EXECDIR, mntname); 26139316Smckusick if (verbose) { 26239322Smckusick printf("exec: %s", execname); 26339322Smckusick 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) { 27139322Smckusick 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)); 27839322Smckusick spec = mntname; 27939316Smckusick goto out; 28039316Smckusick } 28139322Smckusick execve(execname, argv, envp); 28239322Smckusick 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: 29639333Smckusick if (flags & M_UPDATE) 29739333Smckusick fprintf(stderr, "Specified device does %s\n", 29839333Smckusick "not match mounted device"); 29939333Smckusick else 30039333Smckusick fprintf(stderr, "Bogus super block\n"); 30139316Smckusick break; 30239333Smckusick case EOPNOTSUPP: 30339333Smckusick fprintf(stderr, "Operation not supported\n"); 30439333Smckusick break; 30539316Smckusick default: 30639316Smckusick perror((char *)NULL); 30739316Smckusick break; 3084460Sroot } 30939316Smckusick return(1); 3104460Sroot } 31135339Sbostic 31239131Smckusick out: 31312808Ssam if (verbose) 31439316Smckusick prmount(spec, name, flags); 31535339Sbostic 31638445Smckusick if (opflags & ISBGRND) 31738445Smckusick exit(); 31838445Smckusick else 31938445Smckusick return(0); 3201057Sbill } 32135339Sbostic 32235339Sbostic static 32339316Smckusick prmount(spec, name, flags) 32439316Smckusick char *spec, *name; 32539316Smckusick long flags; 32635339Sbostic { 32738632Smckusick register char *root; 32838632Smckusick 32938445Smckusick if (opflags & ISBGRND) 33038445Smckusick return; 33138632Smckusick /* 33238632Smckusick * trim trailing /'s and find last component of name 33338632Smckusick */ 33438632Smckusick for (root = index(spec, '\0'); *--root == '/';) 33538632Smckusick /* void */; 33638632Smckusick *++root = '\0'; 33738632Smckusick if (root = rindex(spec, '/')) 33838632Smckusick spec = root + 1; 33938632Smckusick printf("%s on %s", spec, name); 34039316Smckusick if (flags & M_RDONLY) 34139316Smckusick printf(" (read-only)"); 34239316Smckusick if (flags & M_NOEXEC) 34339316Smckusick printf(" (noexec)"); 34439316Smckusick if (flags & M_NOSUID) 34539316Smckusick printf(" (nosuid)"); 34639316Smckusick if (flags & M_NODEV) 34739316Smckusick printf(" (nodev)"); 34839316Smckusick if (flags & M_SYNCHRONOUS) 34939316Smckusick printf(" (synchronous)"); 35039333Smckusick if (flags & M_UPDATE) 35139333Smckusick printf(" (update only)"); 35235339Sbostic printf("\n"); 35335339Sbostic } 35435339Sbostic 35539133Smckusick getmnttype(fstype) 35639133Smckusick char *fstype; 35739133Smckusick { 35839133Smckusick 35939322Smckusick mntname = fstype; 36039133Smckusick if (!strcmp(fstype, "ufs")) 36139133Smckusick return (MOUNT_UFS); 36239133Smckusick if (!strcmp(fstype, "nfs")) 36339133Smckusick return (MOUNT_NFS); 36439133Smckusick if (!strcmp(fstype, "mfs")) 36539133Smckusick return (MOUNT_MFS); 36639133Smckusick return (0); 36739133Smckusick } 36839133Smckusick 36938632Smckusick usage() 37035339Sbostic { 37139333Smckusick fprintf(stderr, "usage: mount [-afurw]\nor mount [-furw] special | node\nor mount [-furw] special node\n"); 37235339Sbostic exit(1); 37335339Sbostic } 37435339Sbostic 37539316Smckusick getstdopts(options, flagp) 37639316Smckusick char *options; 37739316Smckusick long *flagp; 37839316Smckusick { 37939316Smckusick register char *opt; 38039316Smckusick int negative; 38139316Smckusick char *optbuf[BUFSIZ], *strtok(); 38239316Smckusick 38339316Smckusick strcpy(optbuf, options); 38439316Smckusick for (opt = strtok(optbuf, ","); opt; opt = strtok(NULL, ",")) { 38539316Smckusick if (opt[0] == 'n' && opt[1] == 'o') { 38639316Smckusick negative++; 38739316Smckusick opt += 2; 38839316Smckusick } else { 38939316Smckusick negative = 0; 39039316Smckusick } 39139333Smckusick if (!negative && !strcasecmp(opt, FSTAB_RO)) { 39239333Smckusick *flagp |= M_RDONLY; 39339333Smckusick continue; 39439333Smckusick } 39539333Smckusick if (!negative && !strcasecmp(opt, FSTAB_RW)) { 39639333Smckusick *flagp &= ~M_RDONLY; 39739333Smckusick continue; 39839333Smckusick } 39939316Smckusick if (!strcasecmp(opt, "exec")) { 40039316Smckusick if (negative) 40139316Smckusick *flagp |= M_NOEXEC; 40239333Smckusick else 40339333Smckusick *flagp &= ~M_NOEXEC; 40439316Smckusick continue; 40539316Smckusick } 40639316Smckusick if (!strcasecmp(opt, "suid")) { 40739316Smckusick if (negative) 40839316Smckusick *flagp |= M_NOSUID; 40939333Smckusick else 41039333Smckusick *flagp &= ~M_NOSUID; 41139316Smckusick continue; 41239316Smckusick } 41339316Smckusick if (!strcasecmp(opt, "dev")) { 41439316Smckusick if (negative) 41539316Smckusick *flagp |= M_NODEV; 41639333Smckusick else 41739333Smckusick *flagp &= ~M_NODEV; 41839316Smckusick continue; 41939316Smckusick } 42039316Smckusick if (!strcasecmp(opt, "synchronous")) { 42139316Smckusick if (!negative) 42239316Smckusick *flagp |= M_SYNCHRONOUS; 42339333Smckusick else 42439333Smckusick *flagp &= ~M_SYNCHRONOUS; 42539316Smckusick continue; 42639316Smckusick } 42739316Smckusick } 42839316Smckusick } 42939316Smckusick 43039131Smckusick getufsopts(options, flagp) 43139131Smckusick char *options; 43239131Smckusick long *flagp; 43339131Smckusick { 43439131Smckusick 43539131Smckusick return; 43639131Smckusick } 43739131Smckusick 43839329Smckusick getexecopts(options, argv) 43939131Smckusick char *options; 44039131Smckusick char **argv; 44139131Smckusick { 44239131Smckusick register int argc = 0; 44339131Smckusick register char *opt; 44439131Smckusick char *strtok(); 44539131Smckusick 44639131Smckusick for (opt = strtok(options, ","); opt; opt = strtok(NULL, ",")) { 44739131Smckusick if (opt[0] != '-') 44839131Smckusick continue; 44939131Smckusick argv[argc++] = opt; 45039131Smckusick if (opt[2] == '\0' || opt[2] != '=') 45139131Smckusick continue; 45239131Smckusick opt[2] = '\0'; 45339131Smckusick argv[argc++] = &opt[3]; 45439131Smckusick } 45539131Smckusick return (argc); 45639131Smckusick } 45739131Smckusick 458*39465Smckusick struct statfs * 459*39465Smckusick getmntpt(name) 460*39465Smckusick char *name; 461*39465Smckusick { 462*39465Smckusick long mntsize; 463*39465Smckusick register long i; 464*39465Smckusick struct statfs *mntbuf; 465*39465Smckusick 466*39465Smckusick mntsize = getmntinfo(&mntbuf); 467*39465Smckusick for (i = 0; i < mntsize; i++) { 468*39465Smckusick if (!strcmp(mntbuf[i].f_mntfromname, name) || 469*39465Smckusick !strcmp(mntbuf[i].f_mntonname, name)) 470*39465Smckusick return (&mntbuf[i]); 471*39465Smckusick } 472*39465Smckusick return ((struct statfs *)0); 473*39465Smckusick } 474*39465Smckusick 47538632Smckusick #ifdef NFS 47639131Smckusick /* 47739131Smckusick * Handle the getoption arg. 47839131Smckusick * Essentially update "opflags", "retrycnt" and "nfsargs" 47939131Smckusick */ 48039131Smckusick getnfsopts(optarg, nfsargsp, opflagsp, retrycntp) 48139131Smckusick char *optarg; 48239131Smckusick struct nfs_args *nfsargsp; 48339131Smckusick int *opflagsp; 48439131Smckusick int *retrycntp; 48539131Smckusick { 48639131Smckusick register char *cp, *nextcp; 48739131Smckusick int num; 48839131Smckusick char *nump; 48939131Smckusick 49039131Smckusick cp = optarg; 49139131Smckusick while (cp != NULL && *cp != '\0') { 49239131Smckusick if ((nextcp = index(cp, ',')) != NULL) 49339131Smckusick *nextcp++ = '\0'; 49439131Smckusick if ((nump = index(cp, '=')) != NULL) { 49539131Smckusick *nump++ = '\0'; 49639131Smckusick num = atoi(nump); 49739131Smckusick } else 49839131Smckusick num = -1; 49939131Smckusick /* 50039131Smckusick * Just test for a string match and do it 50139131Smckusick */ 50239131Smckusick if (!strcmp(cp, "bg")) { 50339131Smckusick *opflagsp |= BGRND; 50439131Smckusick } else if (!strcmp(cp, "soft")) { 50539131Smckusick nfsargsp->flags |= NFSMNT_SOFT; 50639131Smckusick } else if (!strcmp(cp, "intr")) { 50739131Smckusick nfsargsp->flags |= NFSMNT_INT; 50839131Smckusick } else if (!strcmp(cp, "retry") && num > 0) { 50939131Smckusick *retrycntp = num; 51039131Smckusick } else if (!strcmp(cp, "rsize") && num > 0) { 51139131Smckusick nfsargsp->rsize = num; 51239131Smckusick nfsargsp->flags |= NFSMNT_RSIZE; 51339131Smckusick } else if (!strcmp(cp, "wsize") && num > 0) { 51439131Smckusick nfsargsp->wsize = num; 51539131Smckusick nfsargsp->flags |= NFSMNT_WSIZE; 51639131Smckusick } else if (!strcmp(cp, "timeo") && num > 0) { 51739131Smckusick nfsargsp->timeo = num; 51839131Smckusick nfsargsp->flags |= NFSMNT_TIMEO; 51939131Smckusick } else if (!strcmp(cp, "retrans") && num > 0) { 52039131Smckusick nfsargsp->retrans = num; 52139131Smckusick nfsargsp->flags |= NFSMNT_RETRANS; 52239131Smckusick } 52339131Smckusick cp = nextcp; 52439131Smckusick } 52539131Smckusick } 52639131Smckusick 52738632Smckusick char * 52838632Smckusick getnfsargs(spec) 52938632Smckusick char *spec; 53035339Sbostic { 53138632Smckusick register CLIENT *clp; 53238632Smckusick struct hostent *hp; 53338632Smckusick struct sockaddr_in saddr; 53438632Smckusick struct timeval pertry, try; 53538632Smckusick enum clnt_stat clnt_stat; 53638632Smckusick int so = RPC_ANYSOCK; 53738632Smckusick char *hostp, *delimp; 53838632Smckusick u_short tport; 53938632Smckusick struct nfhret nfhret; 54038632Smckusick char nam[MNAMELEN + 1]; 54138632Smckusick 54238632Smckusick strncpy(nam, spec, MNAMELEN); 54338632Smckusick nam[MNAMELEN] = '\0'; 54438632Smckusick if ((delimp = index(spec, '@')) != NULL) { 54538632Smckusick hostp = delimp + 1; 54638632Smckusick } else if ((delimp = index(spec, ':')) != NULL) { 54738632Smckusick hostp = spec; 54838632Smckusick spec = delimp + 1; 54938632Smckusick } else { 55038632Smckusick fprintf(stderr, 55138632Smckusick "No <host>:<dirpath> or <dirpath>@<host> spec\n"); 55238632Smckusick return (0); 55338632Smckusick } 55438632Smckusick *delimp = '\0'; 55538632Smckusick if ((hp = gethostbyname(hostp)) == NULL) { 55638632Smckusick fprintf(stderr, "Can't get net id for host\n"); 55738632Smckusick return (0); 55838632Smckusick } 55938632Smckusick bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length); 56038632Smckusick nfhret.stat = EACCES; /* Mark not yet successful */ 56138632Smckusick while (retrycnt > 0) { 56238632Smckusick saddr.sin_family = AF_INET; 56338632Smckusick saddr.sin_port = htons(PMAPPORT); 56438632Smckusick if ((tport = pmap_getport(&saddr, RPCPROG_NFS, 56538632Smckusick NFS_VER2, IPPROTO_UDP)) == 0) { 56638632Smckusick if ((opflags & ISBGRND) == 0) 56738632Smckusick clnt_pcreateerror("NFS Portmap"); 56838632Smckusick } else { 56938632Smckusick saddr.sin_port = 0; 57038632Smckusick pertry.tv_sec = 10; 57138632Smckusick pertry.tv_usec = 0; 57238632Smckusick if ((clp = clntudp_create(&saddr, RPCPROG_MNT, 57338632Smckusick RPCMNT_VER1, pertry, &so)) == NULL) { 57438632Smckusick if ((opflags & ISBGRND) == 0) 57538632Smckusick clnt_pcreateerror("Cannot MNT PRC"); 57638632Smckusick } else { 57738632Smckusick clp->cl_auth = authunix_create_default(); 57838632Smckusick try.tv_sec = 10; 57938632Smckusick try.tv_usec = 0; 58038632Smckusick clnt_stat = clnt_call(clp, RPCMNT_MOUNT, 58138632Smckusick xdr_dir, spec, xdr_fh, &nfhret, try); 58238632Smckusick if (clnt_stat != RPC_SUCCESS) { 58338632Smckusick if ((opflags & ISBGRND) == 0) 58438632Smckusick clnt_perror(clp, "Bad MNT RPC"); 58538632Smckusick } else { 58638632Smckusick auth_destroy(clp->cl_auth); 58738632Smckusick clnt_destroy(clp); 58838632Smckusick retrycnt = 0; 58938632Smckusick } 59038632Smckusick } 59138632Smckusick } 59238632Smckusick if (--retrycnt > 0) { 59338632Smckusick if (opflags & BGRND) { 59438632Smckusick opflags &= ~BGRND; 59538632Smckusick if (fork()) 59638632Smckusick return (0); 59738632Smckusick else 59838632Smckusick opflags |= ISBGRND; 59938632Smckusick } 60038632Smckusick sleep(10); 60138632Smckusick } 60238632Smckusick } 60338632Smckusick if (nfhret.stat) { 60438632Smckusick if (opflags & ISBGRND) 60538632Smckusick exit(1); 60638632Smckusick fprintf(stderr, "Can't access %s, errno=%d\n", spec, 60738632Smckusick nfhret.stat); 60838632Smckusick return (0); 60938632Smckusick } 61038632Smckusick saddr.sin_port = htons(tport); 61138632Smckusick nfsargs.addr = &saddr; 61238632Smckusick nfsargs.fh = &nfhret.nfh; 61338632Smckusick nfsargs.hostname = nam; 61438632Smckusick return ((caddr_t)&nfsargs); 61535339Sbostic } 61638445Smckusick 61738445Smckusick /* 61838445Smckusick * xdr routines for mount rpc's 61938445Smckusick */ 62038445Smckusick xdr_dir(xdrsp, dirp) 62138445Smckusick XDR *xdrsp; 62238445Smckusick char *dirp; 62338445Smckusick { 62438445Smckusick return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 62538445Smckusick } 62638445Smckusick 62738445Smckusick xdr_fh(xdrsp, np) 62838445Smckusick XDR *xdrsp; 62938445Smckusick struct nfhret *np; 63038445Smckusick { 63138445Smckusick if (!xdr_u_long(xdrsp, &(np->stat))) 63238445Smckusick return (0); 63338445Smckusick if (np->stat) 63438445Smckusick return (1); 63538445Smckusick return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH)); 63638445Smckusick } 63738632Smckusick #endif /* NFS */ 638