X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=utils%2Fshowmount%2Fshowmount.c;h=394f5284a219d04c134fe54d59dfb40150925fc1;hp=2695c510652910aca30601c25e27dea1d3359257;hb=014e00dfaea0efc92150e2aedc5ca43aa337545e;hpb=29ac873f9024c8fcbca38ab09ba54cda3765b746 diff --git a/utils/showmount/showmount.c b/utils/showmount/showmount.c index 2695c51..394f528 100644 --- a/utils/showmount/showmount.c +++ b/utils/showmount/showmount.c @@ -78,31 +78,36 @@ static void usage(FILE *fp, int n) exit(n); } -#ifdef HAVE_CLNT_CREATE - -static const char *nfs_sm_pgmtbl[] = { +static const char *mount_pgm_tbl[] = { "showmount", "mount", "mountd", NULL, }; +static const rpcvers_t mount_vers_tbl[] = { + MOUNTVERS_NFSV3, + MOUNTVERS_POSIX, + MOUNTVERS, +}; +static const unsigned int max_vers_tblsz = + (sizeof(mount_vers_tbl)/sizeof(mount_vers_tbl[0])); + /* * Generate an RPC client handle connected to the mountd service * at @hostname, or die trying. * * Supports both AF_INET and AF_INET6 server addresses. */ -static CLIENT *nfs_get_mount_client(const char *hostname) +static CLIENT *nfs_get_mount_client(const char *hostname, rpcvers_t vers) { - rpcprog_t program = nfs_getrpcbyname(MOUNTPROG, nfs_sm_pgmtbl); + rpcprog_t program = nfs_getrpcbyname(MOUNTPROG, mount_pgm_tbl); CLIENT *client; - client = clnt_create(hostname, program, MOUNTVERS, "tcp"); + client = clnt_create(hostname, program, vers, "tcp"); if (client) return client; - - client = clnt_create(hostname, program, MOUNTVERS, "udp"); + client = clnt_create(hostname, program, vers, "udp"); if (client) return client; @@ -110,145 +115,6 @@ static CLIENT *nfs_get_mount_client(const char *hostname) exit(1); } -#else /* HAVE_CLNT_CREATE */ - -/* - * Perform a non-blocking connect on the socket fd. - * - * tout contains the timeout. It will be modified to contain the time - * remaining (i.e. time provided - time elasped). - * - * Returns zero on success; otherwise, -1 is returned and errno is set - * to reflect the nature of the error. - */ -static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout) -{ - int flags, ret; - socklen_t len; - fd_set rset; - - flags = fcntl(fd, F_GETFL, 0); - if (flags < 0) - return -1; - - ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); - if (ret < 0) - return -1; - - /* - * From here on subsequent sys calls could change errno so - * we set ret = -errno to capture it in case we decide to - * use it later. - */ - len = sizeof(struct sockaddr); - ret = connect(fd, (struct sockaddr *)addr, len); - if (ret < 0 && errno != EINPROGRESS) { - ret = -1; - goto done; - } - - if (ret == 0) - goto done; - - /* now wait */ - FD_ZERO(&rset); - FD_SET(fd, &rset); - - ret = select(fd + 1, NULL, &rset, NULL, tout); - if (ret <= 0) { - if (ret == 0) - errno = ETIMEDOUT; - ret = -1; - goto done; - } - - if (FD_ISSET(fd, &rset)) { - int status; - - len = sizeof(ret); - status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len); - if (status < 0) { - ret = -1; - goto done; - } - - /* Oops - something wrong with connect */ - if (ret != 0) { - errno = ret; - ret = -1; - } - } - -done: - fcntl(fd, F_SETFL, flags); - return ret; -} - -/* - * Generate an RPC client handle connected to the mountd service - * at @hostname, or die trying. - * - * Supports only AF_INET server addresses. - */ -static CLIENT *nfs_get_mount_client(const char *hostname) -{ - struct hostent *hp; - struct sockaddr_in server_addr; - struct timeval pertry_timeout; - CLIENT *mclient = NULL; - int ret, msock; - - if (inet_aton(hostname, &server_addr.sin_addr)) { - server_addr.sin_family = AF_INET; - } - else { - if ((hp = gethostbyname(hostname)) == NULL) { - fprintf(stderr, "%s: can't get address for %s\n", - program_name, hostname); - exit(1); - } - server_addr.sin_family = AF_INET; - memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length); - } - - msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (msock != -1) { - if (nfs_getport_ping((struct sockaddr *)&server_addr, - sizeof(server_addr), MOUNTPROG, - MOUNTVERS, IPPROTO_TCP)) { - ret = connect_nb(msock, &server_addr, 0); - if (ret == 0) - mclient = clnttcp_create(&server_addr, - MOUNTPROG, MOUNTVERS, &msock, - 0, 0); - else - close(msock); - } else - close(msock); - } - - if (!mclient) { - if (nfs_getport_ping((struct sockaddr *)&server_addr, - sizeof(server_addr), MOUNTPROG, - MOUNTVERS, IPPROTO_UDP)) { - clnt_pcreateerror("showmount"); - exit(1); - } - msock = RPC_ANYSOCK; - pertry_timeout.tv_sec = TIMEOUT_UDP; - pertry_timeout.tv_usec = 0; - if ((mclient = clntudp_create(&server_addr, - MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) { - clnt_pcreateerror("mount clntudp_create"); - exit(1); - } - } - - return mclient; -} - -#endif /* HAVE_CLNT_CREATE */ - int main(int argc, char **argv) { char hostname_buf[MAXHOSTLEN]; @@ -264,6 +130,7 @@ int main(int argc, char **argv) int i; int n; int maxlen; + int unsigned vers=0; char **dumpv; program_name = argv[0]; @@ -326,11 +193,18 @@ int main(int argc, char **argv) break; } - mclient = nfs_get_mount_client(hostname); - mclient->cl_auth = authunix_create_default(); + mclient = nfs_get_mount_client(hostname, mount_vers_tbl[vers]); + mclient->cl_auth = nfs_authsys_create(); + if (mclient->cl_auth == NULL) { + fprintf(stderr, "%s: unable to create RPC auth handle.\n", + program_name); + clnt_destroy(mclient); + exit(1); + } total_timeout.tv_sec = TOTAL_TIMEOUT; total_timeout.tv_usec = 0; +again: if (eflag) { memset(&exportlist, '\0', sizeof(exportlist)); @@ -338,6 +212,13 @@ int main(int argc, char **argv) (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_exports, (caddr_t) &exportlist, total_timeout); + if (clnt_stat == RPC_PROGVERSMISMATCH) { + if (++vers < max_vers_tblsz) { + (void)CLNT_CONTROL(mclient, CLSET_VERS, + (void *)&mount_vers_tbl[vers]); + goto again; + } + } if (clnt_stat != RPC_SUCCESS) { clnt_perror(mclient, "rpc mount export"); clnt_destroy(mclient); @@ -373,6 +254,13 @@ int main(int argc, char **argv) (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_mountlist, (caddr_t) &dumplist, total_timeout); + if (clnt_stat == RPC_PROGVERSMISMATCH) { + if (++vers < max_vers_tblsz) { + (void)CLNT_CONTROL(mclient, CLSET_VERS, + (void *)&mount_vers_tbl[vers]); + goto again; + } + } if (clnt_stat != RPC_SUCCESS) { clnt_perror(mclient, "rpc mount dump"); clnt_destroy(mclient);