unix_msg: Lift sockaddr_un handling from unix_msg_init
authorVolker Lendecke <vl@samba.org>
Sun, 1 Jun 2014 18:57:21 +0000 (20:57 +0200)
committerJeremy Allison <jra@samba.org>
Wed, 18 Jun 2014 16:51:13 +0000 (18:51 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@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 c3ab0d166cfca3a11ac5672493bad6a1d98194f9..f71ec85959f023f771193ec1a63f115c19bb3a19 100644 (file)
@@ -179,7 +179,9 @@ NTSTATUS messaging_dgm_init(struct messaging_context *msg_ctx,
        int ret;
        bool ok;
        const char *cache_dir;
-       char *socket_dir, *socket_name;
+       char *socket_dir;
+       struct sockaddr_un socket_address;
+       size_t sockname_len;
        uint64_t cookie;
 
        cache_dir = lp_cache_directory();
@@ -209,10 +211,14 @@ NTSTATUS messaging_dgm_init(struct messaging_context *msg_ctx,
        if (socket_dir == NULL) {
                goto fail_nomem;
        }
-       socket_name = talloc_asprintf(ctx, "%s/%u", socket_dir,
-                                     (unsigned)pid.pid);
-       if (socket_name == NULL) {
-               goto fail_nomem;
+
+       socket_address = (struct sockaddr_un) { .sun_family = AF_UNIX };
+       sockname_len = snprintf(socket_address.sun_path,
+                               sizeof(socket_address.sun_path),
+                               "%s/%u", socket_dir, (unsigned)pid.pid);
+       if (sockname_len >= sizeof(socket_address.sun_path)) {
+               TALLOC_FREE(result);
+               return NT_STATUS_NAME_TOO_LONG;
        }
 
        sec_init();
@@ -249,13 +255,12 @@ NTSTATUS messaging_dgm_init(struct messaging_context *msg_ctx,
        }
        TALLOC_FREE(socket_dir);
 
-       unlink(socket_name);
+       unlink(socket_address.sun_path);
 
        generate_random_buffer((uint8_t *)&cookie, sizeof(cookie));
 
-       ret = unix_msg_init(socket_name, ctx->msg_callbacks, 1024, cookie,
+       ret = unix_msg_init(&socket_address, ctx->msg_callbacks, 1024, cookie,
                            messaging_dgm_recv, ctx, &ctx->dgm_ctx);
-       TALLOC_FREE(socket_name);
        if (ret != 0) {
                DEBUG(1, ("unix_msg_init failed: %s\n", strerror(ret)));
                TALLOC_FREE(result);
index c2568b6646b2632e829dce3205c35e12c2a625d6..abaf5ef3c8856bafd99a90ad44d1f2c16dbe6b96 100644 (file)
@@ -17,7 +17,7 @@ static void recv_cb(struct unix_msg_ctx *ctx,
 int main(int argc, const char *argv[])
 {
        struct poll_funcs *funcs;
-       const char *sock;
+       struct sockaddr_un addr;
        struct unix_msg_ctx *ctx;
        struct tevent_context *ev;
        int ret;
@@ -29,8 +29,9 @@ int main(int argc, const char *argv[])
                return 1;
        }
 
-       sock = argv[1];
-       unlink(sock);
+       addr = (struct sockaddr_un) { .sun_family = AF_UNIX };
+       strlcpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
+       unlink(addr.sun_path);
 
        ev = tevent_context_init(NULL);
        if (ev == NULL) {
@@ -43,7 +44,7 @@ int main(int argc, const char *argv[])
                return 1;
        }
 
-       ret = unix_msg_init(sock, funcs, 256, 1, recv_cb, &state, &ctx);
+       ret = unix_msg_init(&addr, funcs, 256, 1, recv_cb, &state, &ctx);
        if (ret != 0) {
                fprintf(stderr, "unix_msg_init failed: %s\n",
                        strerror(ret));
index 29d5dcb374596a572245cb984c31b08b9a2ece44..f5a2e8c020266c073da66fae09360c7f04995225 100644 (file)
@@ -34,8 +34,7 @@ int main(void)
 {
        struct poll_funcs *funcs;
        void *tevent_handle;
-       const char *sock1 = "sock1";
-       const char *sock2 = "sock2";
+       struct sockaddr_un addr1, addr2;
        struct unix_msg_ctx *ctx1, *ctx2;
        struct tevent_context *ev;
        struct iovec iov;
@@ -45,8 +44,13 @@ int main(void)
 
        struct cb_state state;
 
-       unlink(sock1);
-       unlink(sock2);
+       addr1 = (struct sockaddr_un) { .sun_family = AF_UNIX };
+       strlcpy(addr1.sun_path, "sock1", sizeof(addr1.sun_path));
+       unlink(addr1.sun_path);
+
+       addr2 = (struct sockaddr_un) { .sun_family = AF_UNIX };
+       strlcpy(addr2.sun_path, "sock2", sizeof(addr2.sun_path));
+       unlink(addr2.sun_path);
 
        ev = tevent_context_init(NULL);
        if (ev == NULL) {
@@ -65,7 +69,7 @@ int main(void)
                return 1;
        }
 
-       ret = unix_msg_init(sock1, funcs, 256, 1,
+       ret = unix_msg_init(&addr1, funcs, 256, 1,
                            recv_cb, &state, &ctx1);
        if (ret != 0) {
                fprintf(stderr, "unix_msg_init failed: %s\n",
@@ -73,7 +77,7 @@ int main(void)
                return 1;
        }
 
-       ret = unix_msg_init(sock1, funcs, 256, 1,
+       ret = unix_msg_init(&addr1, funcs, 256, 1,
                            recv_cb, &state, &ctx1);
        if (ret == 0) {
                fprintf(stderr, "unix_msg_init succeeded unexpectedly\n");
@@ -85,7 +89,7 @@ int main(void)
                return 1;
        }
 
-       ret = unix_msg_init(sock2, funcs, 256, 1,
+       ret = unix_msg_init(&addr2, funcs, 256, 1,
                            recv_cb, &state, &ctx2);
        if (ret != 0) {
                fprintf(stderr, "unix_msg_init failed: %s\n",
@@ -98,7 +102,7 @@ int main(void)
        state.buf = NULL;
        state.buflen = 0;
 
-       ret = unix_msg_send(ctx1, sock2, NULL, 0);
+       ret = unix_msg_send(ctx1, addr2.sun_path, NULL, 0);
        if (ret != 0) {
                fprintf(stderr, "unix_msg_send failed: %s\n",
                        strerror(ret));
@@ -115,7 +119,7 @@ int main(void)
        state.buf = &msg;
        state.buflen = sizeof(msg);
 
-       ret = unix_msg_send(ctx1, sock2, &iov, 1);
+       ret = unix_msg_send(ctx1, addr2.sun_path, &iov, 1);
        if (ret != 0) {
                fprintf(stderr, "unix_msg_send failed: %s\n",
                        strerror(ret));
@@ -136,13 +140,13 @@ int main(void)
        state.buflen = sizeof(buf);
 
        for (i=0; i<3; i++) {
-               ret = unix_msg_send(ctx1, sock2, &iov, 1);
+               ret = unix_msg_send(ctx1, addr2.sun_path, &iov, 1);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
                        return 1;
                }
-               ret = unix_msg_send(ctx2, sock2, &iov, 1);
+               ret = unix_msg_send(ctx2, addr2.sun_path, &iov, 1);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
@@ -181,7 +185,7 @@ int main(void)
                        j++;
                }
 
-               ret = unix_msg_send(ctx1, sock1, iovs, j);
+               ret = unix_msg_send(ctx1, addr1.sun_path, iovs, j);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
@@ -194,13 +198,13 @@ int main(void)
        printf("Filling send queues before freeing\n");
 
        for (i=0; i<5; i++) {
-               ret = unix_msg_send(ctx1, sock2, &iov, 1);
+               ret = unix_msg_send(ctx1, addr2.sun_path, &iov, 1);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
                        return 1;
                }
-               ret = unix_msg_send(ctx1, sock1, &iov, 1);
+               ret = unix_msg_send(ctx1, addr1.sun_path, &iov, 1);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
index b53a4c65a14ee51382c2d664e12f18018f31618e..40876837170e1a81b66a6401148716dda5f72999 100644 (file)
@@ -603,7 +603,8 @@ static void unix_msg_recv(struct unix_dgram_ctx *ctx,
                          uint8_t *msg, size_t msg_len,
                          void *private_data);
 
-int unix_msg_init(const char *path, const struct poll_funcs *ev_funcs,
+int unix_msg_init(const struct sockaddr_un *addr,
+                 const struct poll_funcs *ev_funcs,
                  size_t fragment_len, uint64_t cookie,
                  void (*recv_callback)(struct unix_msg_ctx *ctx,
                                        uint8_t *msg, size_t msg_len,
@@ -612,8 +613,6 @@ int unix_msg_init(const char *path, const struct poll_funcs *ev_funcs,
                  struct unix_msg_ctx **result)
 {
        struct unix_msg_ctx *ctx;
-       struct sockaddr_un addr;
-       struct sockaddr_un *paddr = NULL;
        int ret;
 
        ctx = malloc(sizeof(*ctx));
@@ -621,18 +620,7 @@ int unix_msg_init(const char *path, const struct poll_funcs *ev_funcs,
                return ENOMEM;
        }
 
-       if (path != NULL) {
-               size_t pathlen = strlen(path)+1;
-
-               if (pathlen > sizeof(addr.sun_path)) {
-                       return ENAMETOOLONG;
-               }
-               addr = (struct sockaddr_un) { .sun_family = AF_UNIX };
-               memcpy(addr.sun_path, path, pathlen);
-               paddr = &addr;
-       }
-
-       ret = unix_dgram_init(paddr, fragment_len, ev_funcs,
+       ret = unix_dgram_init(addr, fragment_len, ev_funcs,
                              unix_msg_recv, ctx, &ctx->dgram);
        if (ret != 0) {
                free(ctx);
@@ -661,7 +649,7 @@ int unix_msg_send(struct unix_msg_ctx *ctx, const char *dst_sock,
        struct sockaddr_un dst;
        size_t dst_len;
 
-       dst_len = strlen(dst_sock);
+       dst_len = strlen(dst_sock)+1;
        if (dst_len >= sizeof(dst.sun_path)) {
                return ENAMETOOLONG;
        }
index fc636d8b702b8adc2c7001ddd264462da9059adc..bf9efe7c9e40f18dc820b5f6404356bdee403fbe 100644 (file)
@@ -75,7 +75,9 @@ struct unix_msg_ctx;
  * @return 0 on success, errno on failure
  */
 
-int unix_msg_init(const char *path, const struct poll_funcs *ev_funcs,
+
+int unix_msg_init(const struct sockaddr_un *addr,
+                 const struct poll_funcs *ev_funcs,
                  size_t fragment_size, uint64_t cookie,
                  void (*recv_callback)(struct unix_msg_ctx *ctx,
                                        uint8_t *msg, size_t msg_len,