s3:unix_msg: pass the fd array to the unix_msg recv_callback function
authorStefan Metzmacher <metze@samba.org>
Mon, 23 Jun 2014 15:16:32 +0000 (17:16 +0200)
committerMichael Adam <obnox@samba.org>
Wed, 24 Sep 2014 06:44:11 +0000 (08:44 +0200)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
source3/lib/messages_dgm.c
source3/lib/unix_msg/test_drain.c
source3/lib/unix_msg/tests.c
source3/lib/unix_msg/unix_msg.c
source3/lib/unix_msg/unix_msg.h

index 471aed464c084fa308fed4f5ca28eed71f9726ec..cadcb4be724fcecb2135e42a8fc65e8851dab0a6 100644 (file)
@@ -54,6 +54,7 @@ static struct messaging_dgm_context *global_dgm_context;
 
 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
                               uint8_t *msg, size_t msg_len,
+                              int *fds, size_t num_fds,
                               void *private_data);
 
 static int messaging_dgm_lockfile_name(struct sun_path_buf *buf,
@@ -324,10 +325,18 @@ int messaging_dgm_send(pid_t pid, const struct iovec *iov, int iovlen)
 
 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
                               uint8_t *msg, size_t msg_len,
+                              int *fds, size_t num_fds,
                               void *private_data)
 {
        struct messaging_dgm_context *dgm_ctx = talloc_get_type_abort(
                private_data, struct messaging_dgm_context);
+       size_t i;
+
+       /* for now we ignore passed file descriptors */
+       for (i = 0; i < num_fds; i++) {
+               close(fds[i]);
+               fds[i] = -1;
+       }
 
        dgm_ctx->recv_cb(msg, msg_len, dgm_ctx->recv_cb_private_data);
 }
index abaf5ef3c8856bafd99a90ad44d1f2c16dbe6b96..5b6a9304ce447788063a4f535223c7b8f60fdcc1 100644 (file)
@@ -12,6 +12,7 @@ struct cb_state {
 
 static void recv_cb(struct unix_msg_ctx *ctx,
                    uint8_t *msg, size_t msg_len,
+                   int *fds, size_t num_fds,
                    void *private_data);
 
 int main(int argc, const char *argv[])
@@ -64,6 +65,7 @@ int main(int argc, const char *argv[])
 
 static void recv_cb(struct unix_msg_ctx *ctx,
                    uint8_t *msg, size_t msg_len,
+                   int *fds, size_t num_fds,
                    void *private_data)
 {
        unsigned num;
index 37ff3040a4e6866543d18bc7db833523804035f2..a213cc22a67280e14423037ee164c31a0a80efc4 100644 (file)
@@ -11,6 +11,7 @@ struct cb_state {
 
 static void recv_cb(struct unix_msg_ctx *ctx,
                    uint8_t *msg, size_t msg_len,
+                   int *fds, size_t num_fds,
                    void *private_data);
 
 static void expect_messages(struct tevent_context *ev, struct cb_state *state,
@@ -225,6 +226,7 @@ int main(void)
 
 static void recv_cb(struct unix_msg_ctx *ctx,
                    uint8_t *msg, size_t msg_len,
+                   int *fds, size_t num_fds,
                    void *private_data)
 {
        struct cb_state *state = (struct cb_state *)private_data;
index 3a9ce092828963ebd774b709a1f4147d4e5e665f..e32a4d80bcd275d92caa6dfddd83a1973686ae4c 100644 (file)
@@ -802,6 +802,7 @@ struct unix_msg_ctx {
 
        void (*recv_callback)(struct unix_msg_ctx *ctx,
                              uint8_t *msg, size_t msg_len,
+                             int *fds, size_t num_fds,
                              void *private_data);
        void *private_data;
 
@@ -818,6 +819,7 @@ int unix_msg_init(const struct sockaddr_un *addr,
                  size_t fragment_len, uint64_t cookie,
                  void (*recv_callback)(struct unix_msg_ctx *ctx,
                                        uint8_t *msg, size_t msg_len,
+                                       int *fds, size_t num_fds,
                                        void *private_data),
                  void *private_data,
                  struct unix_msg_ctx **result)
@@ -960,10 +962,8 @@ static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
        size_t space;
        uint64_t cookie;
 
-       /* for now we ignore passed file descriptors */
-       close_fd_array(fds, num_fds);
-
        if (buflen < sizeof(cookie)) {
+               close_fd_array(fds, num_fds);
                return;
        }
        memcpy(&cookie, buf, sizeof(cookie));
@@ -972,11 +972,12 @@ static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
        buflen -= sizeof(cookie);
 
        if (cookie == 0) {
-               ctx->recv_callback(ctx, buf, buflen, ctx->private_data);
+               ctx->recv_callback(ctx, buf, buflen, fds, num_fds, ctx->private_data);
                return;
        }
 
        if (buflen < sizeof(hdr)) {
+               close_fd_array(fds, num_fds);
                return;
        }
        memcpy(&hdr, buf, sizeof(hdr));
@@ -1000,6 +1001,7 @@ static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
        if (msg == NULL) {
                msg = malloc(offsetof(struct unix_msg, buf) + hdr.msglen);
                if (msg == NULL) {
+                       close_fd_array(fds, num_fds);
                        return;
                }
                *msg = (struct unix_msg) {
@@ -1013,6 +1015,7 @@ static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
 
        space = msg->msglen - msg->received;
        if (buflen > space) {
+               close_fd_array(fds, num_fds);
                return;
        }
 
@@ -1020,11 +1023,12 @@ static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
        msg->received += buflen;
 
        if (msg->received < msg->msglen) {
+               close_fd_array(fds, num_fds);
                return;
        }
 
        DLIST_REMOVE(ctx->msgs, msg);
-       ctx->recv_callback(ctx, msg->buf, msg->msglen, ctx->private_data);
+       ctx->recv_callback(ctx, msg->buf, msg->msglen, fds, num_fds, ctx->private_data);
        free(msg);
 }
 
index a712a8f1eed2f93e0b43378639446024ae495352..16c7c83aaa015d17f1a0b809732751c934909134 100644 (file)
@@ -81,6 +81,7 @@ int unix_msg_init(const struct sockaddr_un *addr,
                  size_t fragment_size, uint64_t cookie,
                  void (*recv_callback)(struct unix_msg_ctx *ctx,
                                        uint8_t *msg, size_t msg_len,
+                                       int *fds, size_t num_fds,
                                        void *private_data),
                  void *private_data,
                  struct unix_msg_ctx **result);