]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/showmount/showmount.c
showmount command: Remove unused local getport() implementation
[nfs-utils.git] / utils / showmount / showmount.c
index 4e10a292846c50610ab636e2b2c6e618ec66d95b..5951033373ca1273f399398535b7a1ac0f62190a 100644 (file)
@@ -37,8 +37,9 @@
 #include <mount.h>
 #include <unistd.h>
 
+#include "nfsrpc.h"
+
 #define TIMEOUT_UDP    3
-#define TIMEOUT_TCP    10
 #define TOTAL_TIMEOUT  20
 
 static char *  version = "showmount for " VERSION;
@@ -82,6 +83,9 @@ static void usage(FILE *fp, int n)
  *
  *  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)
 {
@@ -105,7 +109,7 @@ static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
        len = sizeof(struct sockaddr);
        ret = connect(fd, (struct sockaddr *)addr, len);
        if (ret < 0 && errno != EINPROGRESS) {
-               ret = -errno;
+               ret = -1;
                goto done;
        }
 
@@ -119,9 +123,8 @@ static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
        ret = select(fd + 1, NULL, &rset, NULL, tout);
        if (ret <= 0) {
                if (ret == 0)
-                       ret = -ETIMEDOUT;
-               else
-                       ret = -errno;
+                       errno = ETIMEDOUT;
+               ret = -1;
                goto done;
        }
 
@@ -131,13 +134,15 @@ static int connect_nb(int fd, struct sockaddr_in *addr, struct timeval *tout)
                len = sizeof(ret);
                status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
                if (status < 0) {
-                       ret = -errno;
+                       ret = -1;
                        goto done;
                }
 
                /* Oops - something wrong with connect */
-               if (ret)
-                       ret = -ret;
+               if (ret != 0) {
+                       errno = ret;
+                       ret = -1;
+               }
        }
 
 done:
@@ -145,108 +150,6 @@ done:
        return ret;
 }
 
-static unsigned short getport(struct sockaddr_in *addr,
-                        unsigned long prog, unsigned long vers, int prot)
-{
-       CLIENT *client;
-       enum clnt_stat status;
-       struct pmap parms;
-       int ret, sock;
-       struct sockaddr_in laddr, saddr;
-       struct timeval tout = {0, 0};
-       socklen_t len;
-       unsigned int send_sz = 0;
-       unsigned int recv_sz = 0;
-       unsigned short port;
-
-       memset(&laddr, 0, sizeof(laddr));
-       memset(&saddr, 0, sizeof(saddr));
-       memset(&parms, 0, sizeof(parms));
-
-       memcpy(&saddr, addr, sizeof(saddr));
-       saddr.sin_port = htons(PMAPPORT);
-
-       if (prot == IPPROTO_TCP) {
-               sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
-               if (sock == -1) {
-                       rpc_createerr.cf_stat = RPC_SYSTEMERROR;
-                       rpc_createerr.cf_error.re_errno = errno;
-                       return 0;
-               }
-
-               tout.tv_sec = TIMEOUT_TCP;
-
-               ret = connect_nb(sock, &saddr, &tout);
-               if (ret == -1) {
-                       close(sock);
-                       rpc_createerr.cf_stat = RPC_SYSTEMERROR;
-                       rpc_createerr.cf_error.re_errno = errno;
-                       return 0;
-               }
-               client = clnttcp_create(&saddr,
-                                       PMAPPROG, PMAPVERS, &sock,
-                                       0, 0);
-       } else {
-               /*
-                * bind to any unused port.  If we left this up to the rpc
-                * layer, it would bind to a reserved port, which has been shown
-                * to exhaust the reserved port range in some situations.
-                */
-               sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
-               if (sock == -1) {
-                       rpc_createerr.cf_stat = RPC_SYSTEMERROR;
-                       rpc_createerr.cf_error.re_errno = errno;
-                       return 0;
-               }
-
-               laddr.sin_family = AF_INET;
-               laddr.sin_port = 0;
-               laddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
-               tout.tv_sec = TIMEOUT_UDP;
-
-               send_sz = RPCSMALLMSGSIZE;
-               recv_sz = RPCSMALLMSGSIZE;
-
-               len = sizeof(struct sockaddr_in);
-               if (bind(sock, (struct sockaddr *)&laddr, len) < 0) {
-                       close(sock);
-                       sock = RPC_ANYSOCK;
-                       /* FALLTHROUGH */
-               }
-               client = clntudp_bufcreate(&saddr, PMAPPROG, PMAPVERS,
-                                          tout, &sock, send_sz, recv_sz);
-       }
-
-       if (!client) {
-               close(sock);
-               rpc_createerr.cf_stat = RPC_RPCBFAILURE;
-               return 0;
-       }
-
-       clnt_control(client, CLSET_FD_CLOSE, NULL);
-
-       parms.pm_prog = prog;
-       parms.pm_vers = vers;
-       parms.pm_prot = prot;
-
-       status = clnt_call(client, PMAPPROC_GETPORT,
-                          (xdrproc_t) xdr_pmap, (caddr_t) &parms,
-                          (xdrproc_t) xdr_u_short, (caddr_t) &port,
-                          tout);
-
-       if (status != RPC_SUCCESS) {
-               clnt_geterr(client, &rpc_createerr.cf_error);
-               rpc_createerr.cf_stat = status;
-               clnt_destroy(client);
-               return 0;
-       }
-
-       clnt_destroy(client);
-
-       return htons(port);
-}
-
 int main(int argc, char **argv)
 {
        char hostname_buf[MAXHOSTLEN];
@@ -346,11 +249,11 @@ int main(int argc, char **argv)
        mclient = NULL;
        msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (msock != -1) {
-               server_addr.sin_port = getport(&server_addr,
-                                        MOUNTPROG, MOUNTVERS, IPPROTO_TCP);
-               if (server_addr.sin_port) {
+               if (nfs_getport_ping((struct sockaddr *)&server_addr,
+                                       sizeof(server_addr), MOUNTPROG,
+                                       MOUNTVERS, IPPROTO_TCP)) {
                        ret = connect_nb(msock, &server_addr, 0);
-                       if (ret != -1)
+                       if (ret == 0) /* success */
                                mclient = clnttcp_create(&server_addr,
                                                MOUNTPROG, MOUNTVERS, &msock,
                                                0, 0);
@@ -361,10 +264,10 @@ int main(int argc, char **argv)
        }
 
        if (!mclient) {
-               server_addr.sin_port = getport(&server_addr,
-                                        MOUNTPROG, MOUNTVERS, IPPROTO_UDP);
-               if (!server_addr.sin_port) {
-                       clnt_pcreateerror("portmap getport");
+               if (nfs_getport_ping((struct sockaddr *)&server_addr,
+                                       sizeof(server_addr), MOUNTPROG,
+                                       MOUNTVERS, IPPROTO_UDP)) {
+                       clnt_pcreateerror("showmount");
                        exit(1);
                }
                msock = RPC_ANYSOCK;
@@ -389,6 +292,7 @@ int main(int argc, char **argv)
                        total_timeout);
                if (clnt_stat != RPC_SUCCESS) {
                        clnt_perror(mclient, "rpc mount export");
+                       clnt_destroy(mclient);
                        exit(1);
                }
                if (headers)
@@ -412,6 +316,7 @@ int main(int argc, char **argv)
                        printf("\n");
                        exportlist = exportlist->ex_next;
                }
+               clnt_destroy(mclient);
                exit(0);
        }
 
@@ -422,8 +327,10 @@ int main(int argc, 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)