r3183: moved the unlink of the messaging unixdom socket to the messaging destructor
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket / socket_unix.c
index 7e169c47a7ef5e96540cca47f228dae90d822273..df929d92b4b4a1d16a4db9b71a00ad4b852a47dd 100644 (file)
 
 #include "includes.h"
 
+
+
+/*
+  approximate errno mapping
+*/
+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;
+}
+
 static NTSTATUS unixdom_init(struct socket_context *sock)
 {
        sock->fd = socket(PF_UNIX, SOCK_STREAM, 0);
@@ -37,11 +71,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 (sock->private_data) {
-               unlink((const char *)sock->private_data);
-       }
 }
 
 static NTSTATUS unixdom_connect(struct socket_context *sock,
@@ -69,7 +98,7 @@ static NTSTATUS unixdom_connect(struct socket_context *sock,
 
        ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
        if (ret == -1) {
-               return NT_STATUS_CONNECTION_REFUSED;
+               return unixdom_error(errno);
        }
 
        sock->state = SOCKET_STATE_CLIENT_CONNECTED;
@@ -97,18 +126,18 @@ static NTSTATUS unixdom_listen(struct socket_context *sock,
 
        ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
        if (ret == -1) {
-               return NT_STATUS_UNSUCCESSFUL;
+               return unixdom_error(errno);
        }
 
        ret = listen(sock->fd, queue_size);
        if (ret == -1) {
-               return NT_STATUS_INSUFFICIENT_RESOURCES;
+               return unixdom_error(errno);
        }
 
        if (!(flags & SOCKET_FLAG_BLOCK)) {
                ret = set_blocking(sock->fd, False);
                if (ret == -1) {
-                       return NT_STATUS_INVALID_PARAMETER;
+                       return unixdom_error(errno);
                }
        }
 
@@ -128,7 +157,7 @@ static NTSTATUS unixdom_accept(struct socket_context *sock,
 
        new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
        if (new_fd == -1) {
-               return NT_STATUS_INSUFFICIENT_RESOURCES;
+               return unixdom_error(errno);
        }
 
        (*new_sock) = talloc_p(NULL, struct socket_context);
@@ -180,23 +209,7 @@ static NTSTATUS unixdom_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx,
                talloc_free(buf);
                return NT_STATUS_END_OF_FILE;
        } else if (gotlen == -1) {
-               NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
-               switch (errno) {
-                       case EBADF:
-                       case ENOTCONN:
-                       case ENOTSOCK:
-                       case EFAULT:
-                       case EINVAL:
-                               status = NT_STATUS_INVALID_PARAMETER;
-                               break;
-                       case EAGAIN:
-                       case EINTR:
-                               status = STATUS_MORE_ENTRIES;
-                               break;
-                       case ECONNREFUSED:
-                               status = NT_STATUS_CONNECTION_REFUSED;
-                               break;
-               }
+               NTSTATUS status = unixdom_error(errno);
                talloc_free(buf);
                return status;
        }
@@ -216,6 +229,8 @@ static NTSTATUS unixdom_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
        ssize_t len;
        int flgs = 0;
 
+       *sendlen = 0;
+
        /* TODO: we need to map all flags here */
        if (!(flags & SOCKET_FLAG_BLOCK)) {
                flgs |= MSG_DONTWAIT;
@@ -223,34 +238,7 @@ static NTSTATUS unixdom_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
 
        len = send(sock->fd, blob->data, blob->length, flgs);
        if (len == -1) {
-               NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
-               switch (errno) {
-                       case EBADF:
-                       case ENOTSOCK:
-                       case EFAULT:
-                       case EINVAL:
-                               status = NT_STATUS_INVALID_PARAMETER;
-                               break;
-                       case EMSGSIZE:
-                               status = NT_STATUS_INVALID_BUFFER_SIZE;
-                               break;
-                       case EAGAIN:
-                       /*case EWOULDBLOCK: this is an alis of EAGAIN --metze */
-                       case EINTR:
-                               *sendlen = 0;
-                               status = STATUS_MORE_ENTRIES;
-                               break;
-                       case ENOBUFS:
-                               status = NT_STATUS_FOOBAR;
-                               break;
-                       case ENOMEM:
-                               status = NT_STATUS_NO_MEMORY;
-                               break;
-                       case EPIPE:
-                               status = NT_STATUS_CONNECTION_DISCONNECTED;
-                               break;
-               }
-               return status;
+               return unixdom_error(errno);
        }       
 
        *sendlen = len;