r3314: added a option "socket:testnonblock" to the generic socket code. If
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / socket_unix.c
index 038ce3b3b6882c8795e5750e062808eb7d5908e3..d160d897ee21ec9cb17cacd2e3b369aa2beef9f2 100644 (file)
 */
 static NTSTATUS unixdom_error(int ernum)
 {
-       switch (ernum) {
-       case EBADF:
-       case ENOTCONN:
-       case ENOTSOCK:
-       case EFAULT:
-       case EINVAL:
-               return NT_STATUS_INVALID_PARAMETER;
-       case EAGAIN:
-       case EINTR:
-               return STATUS_MORE_ENTRIES;
-       case ECONNREFUSED:
-               return NT_STATUS_CONNECTION_REFUSED;
-       case ENOBUFS:
-       case ENOMEM:
-               return NT_STATUS_NO_MEMORY;
-       case ENFILE:
-       case EMFILE:
-               return NT_STATUS_INSUFFICIENT_RESOURCES;
-       case EPIPE:
-               return NT_STATUS_CONNECTION_DISCONNECTED;
-       case EMSGSIZE:
-               return NT_STATUS_INVALID_BUFFER_SIZE;
-       }
-       
-       return NT_STATUS_UNSUCCESSFUL;
+       return map_nt_error_from_unix(ernum);
 }
 
 static NTSTATUS unixdom_init(struct socket_context *sock)
@@ -71,15 +47,6 @@ static NTSTATUS unixdom_init(struct socket_context *sock)
 static void unixdom_close(struct socket_context *sock)
 {
        close(sock->fd);
-       /* if we were listening, then don't leave the socket lying
-          around in the filesystem */
-
-#if 0
-       /* FIXME - this doesn't work after fork(), etc */
-       if (sock->private_data) {
-               unlink((const char *)sock->private_data);
-       }
-#endif
 }
 
 static NTSTATUS unixdom_connect(struct socket_context *sock,
@@ -157,8 +124,7 @@ static NTSTATUS unixdom_listen(struct socket_context *sock,
 }
 
 static NTSTATUS unixdom_accept(struct socket_context *sock, 
-                              struct socket_context **new_sock, 
-                              uint32_t flags)
+                              struct socket_context **new_sock)
 {
        struct sockaddr_un cli_addr;
        socklen_t cli_addr_len = sizeof(cli_addr);
@@ -169,6 +135,14 @@ static NTSTATUS unixdom_accept(struct socket_context *sock,
                return unixdom_error(errno);
        }
 
+       if (!(sock->flags & SOCKET_FLAG_BLOCK)) {
+               int ret = set_blocking(new_fd, False);
+               if (ret == -1) {
+                       close(new_fd);
+                       return map_nt_error_from_unix(errno);
+               }
+       }
+
        (*new_sock) = talloc_p(NULL, struct socket_context);
        if (!(*new_sock)) {
                close(new_fd);
@@ -178,7 +152,7 @@ static NTSTATUS unixdom_accept(struct socket_context *sock,
        /* copy the socket_context */
        (*new_sock)->type               = sock->type;
        (*new_sock)->state              = SOCKET_STATE_SERVER_CONNECTED;
-       (*new_sock)->flags              = flags;
+       (*new_sock)->flags              = sock->flags;
 
        (*new_sock)->fd                 = new_fd;
 
@@ -188,51 +162,36 @@ static NTSTATUS unixdom_accept(struct socket_context *sock,
        return NT_STATUS_OK;
 }
 
-static NTSTATUS unixdom_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx,
-                            DATA_BLOB *blob, size_t wantlen, uint32_t flags)
+static NTSTATUS unixdom_recv(struct socket_context *sock, void *buf, 
+                            size_t wantlen, size_t *nread, uint32_t flags)
 {
        ssize_t gotlen;
-       void *buf;
        int flgs = 0;
 
-       buf = talloc(mem_ctx, wantlen);
-       if (!buf) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
        /* TODO: we need to map all flags here */
        if (flags & SOCKET_FLAG_PEEK) {
                flgs |= MSG_PEEK;
        }
 
-       if (!(flags & SOCKET_FLAG_BLOCK)) {
-               flgs |= MSG_DONTWAIT;
-       }
-
        if (flags & SOCKET_FLAG_BLOCK) {
                flgs |= MSG_WAITALL;
        }
 
+       *nread = 0;
+
        gotlen = recv(sock->fd, buf, wantlen, flgs);
        if (gotlen == 0) {
-               talloc_free(buf);
                return NT_STATUS_END_OF_FILE;
        } else if (gotlen == -1) {
-               NTSTATUS status = unixdom_error(errno);
-               talloc_free(buf);
-               return status;
+               return unixdom_error(errno);
        }
 
-       blob->length = gotlen;
-       blob->data = talloc_realloc(mem_ctx, buf, gotlen);
-       if (!blob->data) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       *nread = gotlen;
 
        return NT_STATUS_OK;
 }
 
-static NTSTATUS unixdom_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
+static NTSTATUS unixdom_send(struct socket_context *sock,
                             const DATA_BLOB *blob, size_t *sendlen, uint32_t flags)
 {
        ssize_t len;
@@ -240,11 +199,6 @@ static NTSTATUS unixdom_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
 
        *sendlen = 0;
 
-       /* TODO: we need to map all flags here */
-       if (!(flags & SOCKET_FLAG_BLOCK)) {
-               flgs |= MSG_DONTWAIT;
-       }
-
        len = send(sock->fd, blob->data, blob->length, flgs);
        if (len == -1) {
                return unixdom_error(errno);
@@ -258,7 +212,6 @@ static NTSTATUS unixdom_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
 static NTSTATUS unixdom_set_option(struct socket_context *sock, 
                                   const char *option, const char *val)
 {
-       set_socket_options(sock->fd, option);
        return NT_STATUS_OK;
 }