s4:auth Move struct auth_usersupplied_info to a common location
[samba.git] / lib / async_req / async_sock.c
index 09eec10fc5e5d6258bc4f1350620c9683478a641..18adb42a0cb6aeeedbaf26429155db50ed3d1cd8 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
-#include "lib/talloc/talloc.h"
-#include "lib/tevent/tevent.h"
+#include "replace.h"
+#include "system/network.h"
+#include "system/filesys.h"
+#include <talloc.h>
+#include <tevent.h>
 #include "lib/async_req/async_sock.h"
+
+/* Note: lib/util/ is currently GPL */
 #include "lib/util/tevent_unix.h"
-#include <fcntl.h>
+#include "lib/util/util.h"
 
 #ifndef TALLOC_FREE
 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
@@ -81,6 +85,10 @@ static void async_send_handler(struct tevent_context *ev,
                tevent_req_data(req, struct async_send_state);
 
        state->sent = send(state->fd, state->buf, state->len, state->flags);
+       if ((state->sent == -1) && (errno == EINTR)) {
+               /* retry */
+               return;
+       }
        if (state->sent == -1) {
                tevent_req_error(req, errno);
                return;
@@ -148,6 +156,14 @@ static void async_recv_handler(struct tevent_context *ev,
 
        state->received = recv(state->fd, state->buf, state->len,
                               state->flags);
+       if ((state->received == -1) && (errno == EINTR)) {
+               /* retry */
+               return;
+       }
+       if (state->received == 0) {
+               tevent_req_error(req, EPIPE);
+               return;
+       }
        if (state->received == -1) {
                tevent_req_error(req, errno);
                return;
@@ -215,6 +231,11 @@ struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx,
        state->fd = fd;
        state->sys_errno = 0;
 
+       state->old_sockflags = fcntl(fd, F_GETFL, 0);
+       if (state->old_sockflags == -1) {
+               goto post_errno;
+       }
+
        state->address_len = address_len;
        if (address_len > sizeof(state->address)) {
                errno = EINVAL;
@@ -222,11 +243,6 @@ struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx,
        }
        memcpy(&state->address, address, address_len);
 
-       state->old_sockflags = fcntl(fd, F_GETFL, 0);
-       if (state->old_sockflags == -1) {
-               goto post_errno;
-       }
-
        set_blocking(fd, false);
 
        state->result = connect(fd, address, address_len);
@@ -417,7 +433,7 @@ static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
 
        to_write = 0;
 
-       if (flags & TEVENT_FD_READ) {
+       if ((state->flags & TEVENT_FD_READ) && (flags & TEVENT_FD_READ)) {
                tevent_req_error(req, EPIPE);
                return;
        }
@@ -427,6 +443,10 @@ static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
        }
 
        written = writev(state->fd, state->iov, state->count);
+       if ((written == -1) && (errno == EINTR)) {
+               /* retry */
+               return;
+       }
        if (written == -1) {
                tevent_req_error(req, errno);
                return;
@@ -534,6 +554,10 @@ static void read_packet_handler(struct tevent_context *ev,
 
        nread = recv(state->fd, state->buf+state->nread, total-state->nread,
                     0);
+       if ((nread == -1) && (errno == EINTR)) {
+               /* retry */
+               return;
+       }
        if (nread == -1) {
                tevent_req_error(req, errno);
                return;