unix_msg: Lift sockaddr_un handling from unix_msg_send
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_source.c
source3/lib/unix_msg/tests.c
source3/lib/unix_msg/unix_msg.c
source3/lib/unix_msg/unix_msg.h

index f71ec85959f023f771193ec1a63f115c19bb3a19..edef8892d3590cd36c03ed01c044b656a49bbcfe 100644 (file)
@@ -300,21 +300,19 @@ static NTSTATUS messaging_dgm_send(struct server_id src,
 {
        struct messaging_dgm_context *ctx = talloc_get_type_abort(
                backend->private_data, struct messaging_dgm_context);
-       fstring pid_str;
-       char buf[PATH_MAX];
-       char *dst_sock, *to_free;
        struct messaging_dgm_hdr hdr;
        struct iovec iov2[iovlen + 1];
-       ssize_t pathlen;
        struct server_id_buf idbuf;
+       struct sockaddr_un dst;
+       ssize_t dst_pathlen;
        int ret;
 
-       fstr_sprintf(pid_str, "msg/%u", (unsigned)pid.pid);
+       dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
 
-       pathlen = full_path_tos(ctx->cache_dir, pid_str, buf, sizeof(buf),
-                               &dst_sock, &to_free);
-       if (pathlen == -1) {
-               return NT_STATUS_NO_MEMORY;
+       dst_pathlen = snprintf(dst.sun_path, sizeof(dst.sun_path),
+                              "%s/msg/%u", ctx->cache_dir, (unsigned)pid.pid);
+       if (dst_pathlen >= sizeof(dst.sun_path)) {
+               return NT_STATUS_NAME_TOO_LONG;
        }
 
        hdr.msg_version = MESSAGE_VERSION;
@@ -331,11 +329,9 @@ static NTSTATUS messaging_dgm_send(struct server_id src,
        memcpy(iov2+1, iov, iovlen*sizeof(struct iovec));
 
        become_root();
-       ret = unix_msg_send(ctx->dgm_ctx, dst_sock, iov2, iovlen + 1);
+       ret = unix_msg_send(ctx->dgm_ctx, &dst, iov2, iovlen + 1);
        unbecome_root();
 
-       TALLOC_FREE(to_free);
-
        if (ret != 0) {
                return map_nt_error_from_unix(ret);
        }
index 94984d88523ba31bc9679fac85eb6d248ac76282..7f6a7a5e8c07cb56c395803201d0887922b26d71 100644 (file)
@@ -13,6 +13,7 @@ int main(int argc, const char *argv[])
        int ret;
        unsigned i;
        unsigned num_ctxs = 1;
+       struct sockaddr_un dst;
 
        if (argc < 2) {
                fprintf(stderr, "Usage: %s <sockname> [num_contexts]\n", argv[0]);
@@ -57,11 +58,14 @@ int main(int argc, const char *argv[])
        iov.iov_base = &i;
        iov.iov_len = sizeof(i);
 
+       dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
+       strlcpy(dst.sun_path, argv[1], sizeof(dst.sun_path));
+
        for (i=0; i<num_ctxs; i++) {
                unsigned j;
 
                for (j=0; j<100000; j++) {
-                       ret = unix_msg_send(ctxs[i], argv[1], &iov, 1);
+                       ret = unix_msg_send(ctxs[i], &dst, &iov, 1);
                        if (ret != 0) {
                                fprintf(stderr, "unix_msg_send failed: %s\n",
                                        strerror(ret));
index f5a2e8c020266c073da66fae09360c7f04995225..37ff3040a4e6866543d18bc7db833523804035f2 100644 (file)
@@ -102,7 +102,7 @@ int main(void)
        state.buf = NULL;
        state.buflen = 0;
 
-       ret = unix_msg_send(ctx1, addr2.sun_path, NULL, 0);
+       ret = unix_msg_send(ctx1, &addr2, NULL, 0);
        if (ret != 0) {
                fprintf(stderr, "unix_msg_send failed: %s\n",
                        strerror(ret));
@@ -119,7 +119,7 @@ int main(void)
        state.buf = &msg;
        state.buflen = sizeof(msg);
 
-       ret = unix_msg_send(ctx1, addr2.sun_path, &iov, 1);
+       ret = unix_msg_send(ctx1, &addr2, &iov, 1);
        if (ret != 0) {
                fprintf(stderr, "unix_msg_send failed: %s\n",
                        strerror(ret));
@@ -140,13 +140,13 @@ int main(void)
        state.buflen = sizeof(buf);
 
        for (i=0; i<3; i++) {
-               ret = unix_msg_send(ctx1, addr2.sun_path, &iov, 1);
+               ret = unix_msg_send(ctx1, &addr2, &iov, 1);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
                        return 1;
                }
-               ret = unix_msg_send(ctx2, addr2.sun_path, &iov, 1);
+               ret = unix_msg_send(ctx2, &addr2, &iov, 1);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
@@ -185,7 +185,7 @@ int main(void)
                        j++;
                }
 
-               ret = unix_msg_send(ctx1, addr1.sun_path, iovs, j);
+               ret = unix_msg_send(ctx1, &addr1, iovs, j);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
@@ -198,13 +198,13 @@ int main(void)
        printf("Filling send queues before freeing\n");
 
        for (i=0; i<5; i++) {
-               ret = unix_msg_send(ctx1, addr2.sun_path, &iov, 1);
+               ret = unix_msg_send(ctx1, &addr2, &iov, 1);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
                        return 1;
                }
-               ret = unix_msg_send(ctx1, addr1.sun_path, &iov, 1);
+               ret = unix_msg_send(ctx1, &addr1, &iov, 1);
                if (ret != 0) {
                        fprintf(stderr, "unix_msg_send failed: %s\n",
                                strerror(ret));
index 40876837170e1a81b66a6401148716dda5f72999..f3185a3b52657944753a78aa7d09f4264ca1aed6 100644 (file)
@@ -637,7 +637,7 @@ int unix_msg_init(const struct sockaddr_un *addr,
        return 0;
 }
 
-int unix_msg_send(struct unix_msg_ctx *ctx, const char *dst_sock,
+int unix_msg_send(struct unix_msg_ctx *ctx, const struct sockaddr_un *dst,
                  const struct iovec *iov, int iovlen)
 {
        ssize_t msglen;
@@ -646,15 +646,6 @@ int unix_msg_send(struct unix_msg_ctx *ctx, const char *dst_sock,
        struct iovec *iov_copy;
        struct unix_msg_hdr hdr;
        struct iovec src_iov;
-       struct sockaddr_un dst;
-       size_t dst_len;
-
-       dst_len = strlen(dst_sock)+1;
-       if (dst_len >= sizeof(dst.sun_path)) {
-               return ENAMETOOLONG;
-       }
-       dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
-       memcpy(dst.sun_path, dst_sock, dst_len);
 
        if (iovlen < 0) {
                return EINVAL;
@@ -676,7 +667,7 @@ int unix_msg_send(struct unix_msg_ctx *ctx, const char *dst_sock,
                               sizeof(struct iovec) * iovlen);
                }
 
-               return unix_dgram_send(ctx->dgram, &dst, tmp_iov, iovlen+1);
+               return unix_dgram_send(ctx->dgram, dst, tmp_iov, iovlen+1);
        }
 
        hdr.msglen = msglen;
@@ -734,7 +725,7 @@ int unix_msg_send(struct unix_msg_ctx *ctx, const char *dst_sock,
                }
                sent += (fragment_len - sizeof(ctx->cookie) - sizeof(hdr));
 
-               ret = unix_dgram_send(ctx->dgram, &dst, iov_copy, iov_index);
+               ret = unix_dgram_send(ctx->dgram, dst, iov_copy, iov_index);
                if (ret != 0) {
                        break;
                }
index bf9efe7c9e40f18dc820b5f6404356bdee403fbe..a712a8f1eed2f93e0b43378639446024ae495352 100644 (file)
@@ -95,7 +95,7 @@ int unix_msg_init(const struct sockaddr_un *addr,
  * @return 0 on success, errno on failure
  */
 
-int unix_msg_send(struct unix_msg_ctx *ctx, const char *dst_sock,
+int unix_msg_send(struct unix_msg_ctx *ctx, const struct sockaddr_un *dst,
                  const struct iovec *iov, int iovlen);
 
 /**