1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-01 04:58:35 +03:00

unix_msg: Lift sockaddr_un handling from unix_msg_init

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
Volker Lendecke 2014-06-01 20:57:21 +02:00 committed by Jeremy Allison
parent 0d81063ea9
commit 73a1205fe8
5 changed files with 43 additions and 43 deletions

View 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);

View 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));

View 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));

View 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;
}

View 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,