X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=utils%2Fshowmount%2Fshowmount.c;h=2695c510652910aca30601c25e27dea1d3359257;hp=1ff3fa11c620ab5e1d9016331a32df50e961ef2d;hb=29ac873f9024c8fcbca38ab09ba54cda3765b746;hpb=ac84b80afc13d5a96813e678ad5c86c6036f022e diff --git a/utils/showmount/showmount.c b/utils/showmount/showmount.c index 1ff3fa1..2695c51 100644 --- a/utils/showmount/showmount.c +++ b/utils/showmount/showmount.c @@ -13,10 +13,13 @@ * GNU General Public License for more details. */ -#include "config.h" +#ifdef HAVE_CONFIG_H +#include +#endif #include #include +#include #include #include #include @@ -25,6 +28,7 @@ #include #include #include +#include #include #include @@ -33,6 +37,11 @@ #include #include +#include "nfsrpc.h" + +#define TIMEOUT_UDP 3 +#define TOTAL_TIMEOUT 20 + static char * version = "showmount for " VERSION; static char * program_name; static int headers = 1; @@ -54,16 +63,14 @@ static struct option longopts[] = #define MAXHOSTLEN 256 -int dump_cmp(p, q) -char **p; -char **q; +static int dump_cmp(const void *pv, const void *qv) { + const char **p = (const char **)pv; + const char **q = (const char **)qv; return strcmp(*p, *q); } -static void usage(fp, n) -FILE *fp; -int n; +static void usage(FILE *fp, int n) { fprintf(fp, "Usage: %s [-adehv]\n", program_name); fprintf(fp, " [--all] [--directories] [--exports]\n"); @@ -71,18 +78,183 @@ int n; exit(n); } -int main(argc, argv) -int argc; -char **argv; +#ifdef HAVE_CLNT_CREATE + +static const char *nfs_sm_pgmtbl[] = { + "showmount", + "mount", + "mountd", + NULL, +}; + +/* + * 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) +{ + rpcprog_t program = nfs_getrpcbyname(MOUNTPROG, nfs_sm_pgmtbl); + CLIENT *client; + + client = clnt_create(hostname, program, MOUNTVERS, "tcp"); + if (client) + return client; + + client = clnt_create(hostname, program, MOUNTVERS, "udp"); + if (client) + return client; + + clnt_pcreateerror("clnt_create"); + 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]; char *hostname; enum clnt_stat clnt_stat; - struct hostent *hp; - struct sockaddr_in server_addr; - int msock; struct timeval total_timeout; - struct timeval pertry_timeout; int c; CLIENT *mclient; groups grouplist; @@ -154,47 +326,21 @@ char **argv; break; } - if (inet_aton(hostname, &server_addr.sin_addr.s_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); - } - - /* create mount deamon client */ - - server_addr.sin_port = 0; - msock = RPC_ANYSOCK; - if ((mclient = clnttcp_create(&server_addr, - MOUNTPROG, MOUNTVERS, &msock, 0, 0)) == NULL) { - server_addr.sin_port = 0; - msock = RPC_ANYSOCK; - pertry_timeout.tv_sec = 3; - pertry_timeout.tv_usec = 0; - if ((mclient = clntudp_create(&server_addr, - MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) { - clnt_pcreateerror("mount clntudp_create"); - exit(1); - } - } + mclient = nfs_get_mount_client(hostname); mclient->cl_auth = authunix_create_default(); - total_timeout.tv_sec = 20; + total_timeout.tv_sec = TOTAL_TIMEOUT; total_timeout.tv_usec = 0; if (eflag) { memset(&exportlist, '\0', sizeof(exportlist)); + clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT, (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_exports, (caddr_t) &exportlist, total_timeout); if (clnt_stat != RPC_SUCCESS) { clnt_perror(mclient, "rpc mount export"); + clnt_destroy(mclient); exit(1); } if (headers) @@ -218,6 +364,7 @@ char **argv; printf("\n"); exportlist = exportlist->ex_next; } + clnt_destroy(mclient); exit(0); } @@ -228,8 +375,10 @@ char **argv; total_timeout); if (clnt_stat != RPC_SUCCESS) { clnt_perror(mclient, "rpc mount dump"); + clnt_destroy(mclient); exit(1); } + clnt_destroy(mclient); n = 0; for (list = dumplist; list; list = list->ml_next)