]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/showmount/showmount.c
showmount command: clean up error returns from connect_nb()
[nfs-utils.git] / utils / showmount / showmount.c
index 4e10a292846c50610ab636e2b2c6e618ec66d95b..5e3e419fa908dbea75421c53c3211f874dc3430d 100644 (file)
@@ -82,6 +82,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 +108,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 +122,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 +133,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:
@@ -177,10 +181,10 @@ static unsigned short getport(struct sockaddr_in *addr,
                tout.tv_sec = TIMEOUT_TCP;
 
                ret = connect_nb(sock, &saddr, &tout);
-               if (ret == -1) {
-                       close(sock);
+               if (ret != 0) {
                        rpc_createerr.cf_stat = RPC_SYSTEMERROR;
                        rpc_createerr.cf_error.re_errno = errno;
+                       close(sock);
                        return 0;
                }
                client = clnttcp_create(&saddr,
@@ -240,6 +244,8 @@ static unsigned short getport(struct sockaddr_in *addr,
                rpc_createerr.cf_stat = status;
                clnt_destroy(client);
                return 0;
+       } else if (port == 0) {
+               rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
        }
 
        clnt_destroy(client);
@@ -350,7 +356,7 @@ int main(int argc, char **argv)
                                         MOUNTPROG, MOUNTVERS, IPPROTO_TCP);
                if (server_addr.sin_port) {
                        ret = connect_nb(msock, &server_addr, 0);
-                       if (ret != -1)
+                       if (ret == 0) /* success */
                                mclient = clnttcp_create(&server_addr,
                                                MOUNTPROG, MOUNTVERS, &msock,
                                                0, 0);
@@ -364,7 +370,7 @@ int main(int argc, char **argv)
                server_addr.sin_port = getport(&server_addr,
                                         MOUNTPROG, MOUNTVERS, IPPROTO_UDP);
                if (!server_addr.sin_port) {
-                       clnt_pcreateerror("portmap getport");
+                       clnt_pcreateerror("showmount");
                        exit(1);
                }
                msock = RPC_ANYSOCK;