mirror of
https://github.com/samba-team/samba.git
synced 2024-12-23 17:34:34 +03:00
lib: Remove unix_msg
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
parent
40bbf9494f
commit
f0e3cebc54
@ -1,83 +0,0 @@
|
||||
#include "replace.h"
|
||||
#include "unix_msg.h"
|
||||
#include "poll_funcs/poll_funcs_tevent.h"
|
||||
#include "tevent.h"
|
||||
#include "system/select.h"
|
||||
|
||||
struct cb_state {
|
||||
unsigned num_received;
|
||||
uint8_t *buf;
|
||||
size_t buflen;
|
||||
};
|
||||
|
||||
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[])
|
||||
{
|
||||
struct poll_funcs *funcs;
|
||||
void *handle;
|
||||
struct sockaddr_un addr;
|
||||
struct unix_msg_ctx *ctx;
|
||||
struct tevent_context *ev;
|
||||
int ret;
|
||||
|
||||
struct cb_state state;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: %s <sockname>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
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) {
|
||||
perror("tevent_context_init failed");
|
||||
return 1;
|
||||
}
|
||||
funcs = poll_funcs_init_tevent(ev);
|
||||
if (funcs == NULL) {
|
||||
fprintf(stderr, "poll_funcs_init_tevent failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
handle = poll_funcs_tevent_register(ev, funcs, ev);
|
||||
if (handle == NULL) {
|
||||
fprintf(stderr, "poll_funcs_tevent_register failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ret = unix_msg_init(&addr, funcs, 256, recv_cb, &state, &ctx);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_init failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
ret = tevent_loop_once(ev);
|
||||
if (ret == -1) {
|
||||
fprintf(stderr, "tevent_loop_once failed: %s\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
if (msg_len == sizeof(num)) {
|
||||
memcpy(&num, msg, msg_len);
|
||||
printf("%u\n", num);
|
||||
}
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
#include "replace.h"
|
||||
#include "unix_msg.h"
|
||||
#include "poll_funcs/poll_funcs_tevent.h"
|
||||
#include "tevent.h"
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
struct poll_funcs *funcs;
|
||||
void *tevent_handle;
|
||||
struct unix_msg_ctx **ctxs;
|
||||
struct tevent_context *ev;
|
||||
struct iovec iov;
|
||||
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]);
|
||||
return 1;
|
||||
}
|
||||
if (argc > 2) {
|
||||
num_ctxs = atoi(argv[2]);
|
||||
}
|
||||
|
||||
ev = tevent_context_init(NULL);
|
||||
if (ev == NULL) {
|
||||
perror("tevent_context_init failed");
|
||||
return 1;
|
||||
}
|
||||
funcs = poll_funcs_init_tevent(NULL);
|
||||
if (funcs == NULL) {
|
||||
fprintf(stderr, "poll_funcs_init_tevent failed\n");
|
||||
return 1;
|
||||
}
|
||||
tevent_handle = poll_funcs_tevent_register(NULL, funcs, ev);
|
||||
if (tevent_handle == NULL) {
|
||||
fprintf(stderr, "poll_funcs_tevent_register failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
ctxs = talloc_array(ev, struct unix_msg_ctx *, num_ctxs);
|
||||
if (ctxs == NULL) {
|
||||
fprintf(stderr, "talloc failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (i=0; i<num_ctxs; i++) {
|
||||
ret = unix_msg_init(NULL, funcs, 256, NULL, NULL,
|
||||
&ctxs[i]);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_init failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
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], &dst, &iov, 1, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
ret = tevent_loop_once(ev);
|
||||
if (ret == -1) {
|
||||
fprintf(stderr, "tevent_loop_once failed: %s\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0; i<num_ctxs; i++) {
|
||||
unix_msg_free(ctxs[i]);
|
||||
}
|
||||
|
||||
talloc_free(ev);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,271 +0,0 @@
|
||||
#include "replace.h"
|
||||
#include "unix_msg.h"
|
||||
#include "poll_funcs/poll_funcs_tevent.h"
|
||||
#include "tevent.h"
|
||||
|
||||
struct cb_state {
|
||||
unsigned num_received;
|
||||
uint8_t *buf;
|
||||
size_t buflen;
|
||||
};
|
||||
|
||||
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,
|
||||
unsigned num_msgs)
|
||||
{
|
||||
state->num_received = 0;
|
||||
|
||||
while (state->num_received < num_msgs) {
|
||||
int ret;
|
||||
|
||||
ret = tevent_loop_once(ev);
|
||||
if (ret == -1) {
|
||||
fprintf(stderr, "tevent_loop_once failed: %s\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct poll_funcs *funcs;
|
||||
void *tevent_handle;
|
||||
struct sockaddr_un addr1, addr2;
|
||||
struct unix_msg_ctx *ctx1, *ctx2;
|
||||
struct tevent_context *ev;
|
||||
struct iovec iov;
|
||||
uint8_t msg;
|
||||
int i, ret;
|
||||
static uint8_t buf[1755];
|
||||
|
||||
struct cb_state state;
|
||||
|
||||
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) {
|
||||
perror("tevent_context_init failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
funcs = poll_funcs_init_tevent(ev);
|
||||
if (funcs == NULL) {
|
||||
fprintf(stderr, "poll_funcs_init_tevent failed\n");
|
||||
return 1;
|
||||
}
|
||||
tevent_handle = poll_funcs_tevent_register(ev, funcs, ev);
|
||||
if (tevent_handle == NULL) {
|
||||
fprintf(stderr, "poll_funcs_register_tevent failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
ret = unix_msg_init(&addr1, funcs, 256, recv_cb, &state, &ctx1);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_init failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
ret = unix_msg_init(&addr1, funcs, 256, recv_cb, &state, &ctx1);
|
||||
if (ret == 0) {
|
||||
fprintf(stderr, "unix_msg_init succeeded unexpectedly\n");
|
||||
return 1;
|
||||
}
|
||||
if (ret != EADDRINUSE) {
|
||||
fprintf(stderr, "unix_msg_init returned %s, expected "
|
||||
"EADDRINUSE\n", strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
ret = unix_msg_init(&addr2, funcs, 256, recv_cb, &state, &ctx2);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_init failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("sending a 0-length message\n");
|
||||
|
||||
state.buf = NULL;
|
||||
state.buflen = 0;
|
||||
|
||||
ret = unix_msg_send(ctx1, &addr2, NULL, 0, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
expect_messages(ev, &state, 1);
|
||||
|
||||
printf("sending a small message\n");
|
||||
|
||||
msg = random();
|
||||
iov.iov_base = &msg;
|
||||
iov.iov_len = sizeof(msg);
|
||||
state.buf = &msg;
|
||||
state.buflen = sizeof(msg);
|
||||
|
||||
ret = unix_msg_send(ctx1, &addr2, &iov, 1, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
expect_messages(ev, &state, 1);
|
||||
|
||||
printf("test send queue caching\n");
|
||||
|
||||
/*
|
||||
* queues are cached for some time, so this tests sending
|
||||
* still works after the cache expires and the queue was
|
||||
* freed.
|
||||
*/
|
||||
sleep(SENDQ_CACHE_TIME_SECS + 1);
|
||||
ret = tevent_loop_once(ev);
|
||||
if (ret == -1) {
|
||||
fprintf(stderr, "tevent_loop_once failed: %s\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
msg = random();
|
||||
iov.iov_base = &msg;
|
||||
iov.iov_len = sizeof(msg);
|
||||
state.buf = &msg;
|
||||
state.buflen = sizeof(msg);
|
||||
|
||||
ret = unix_msg_send(ctx1, &addr2, &iov, 1, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
expect_messages(ev, &state, 1);
|
||||
|
||||
printf("sending six large, interleaved messages\n");
|
||||
|
||||
for (i=0; i<sizeof(buf); i++) {
|
||||
buf[i] = random();
|
||||
}
|
||||
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = sizeof(buf);
|
||||
state.buf = buf;
|
||||
state.buflen = sizeof(buf);
|
||||
|
||||
for (i=0; i<3; i++) {
|
||||
ret = unix_msg_send(ctx1, &addr2, &iov, 1, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
ret = unix_msg_send(ctx2, &addr2, &iov, 1, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
expect_messages(ev, &state, 6);
|
||||
|
||||
printf("sending a few messages in small pieces\n");
|
||||
|
||||
for (i = 0; i<5; i++) {
|
||||
struct iovec iovs[20];
|
||||
const size_t num_iovs = ARRAY_SIZE(iovs);
|
||||
uint8_t *p = buf;
|
||||
size_t j;
|
||||
|
||||
for (j=0; j<num_iovs-1; j++) {
|
||||
size_t chunk = (random() % ((sizeof(buf) * 2) / num_iovs));
|
||||
size_t space = (sizeof(buf) - (p - buf));
|
||||
|
||||
if (space == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
chunk = MIN(chunk, space);
|
||||
|
||||
iovs[j].iov_base = p;
|
||||
iovs[j].iov_len = chunk;
|
||||
p += chunk;
|
||||
}
|
||||
|
||||
if (p < (buf + sizeof(buf))) {
|
||||
iovs[j].iov_base = p;
|
||||
iovs[j].iov_len = (sizeof(buf) - (p - buf));
|
||||
j++;
|
||||
}
|
||||
|
||||
ret = unix_msg_send(ctx1, &addr1, iovs, j, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
expect_messages(ev, &state, 5);
|
||||
|
||||
printf("Filling send queues before freeing\n");
|
||||
|
||||
for (i=0; i<5; i++) {
|
||||
ret = unix_msg_send(ctx1, &addr2, &iov, 1, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
ret = unix_msg_send(ctx1, &addr1, &iov, 1, NULL, 0);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "unix_msg_send failed: %s\n",
|
||||
strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
expect_messages(ev, &state, 1); /* Read just one msg */
|
||||
|
||||
unix_msg_free(ctx1);
|
||||
unix_msg_free(ctx2);
|
||||
talloc_free(tevent_handle);
|
||||
talloc_free(funcs);
|
||||
talloc_free(ev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (msg_len != state->buflen) {
|
||||
fprintf(stderr, "expected %u bytes, got %u\n",
|
||||
(unsigned)state->buflen, (unsigned)msg_len);
|
||||
exit(1);
|
||||
}
|
||||
if ((msg_len != 0) && (memcmp(msg, state->buf, msg_len) != 0)) {
|
||||
fprintf(stderr, "message content differs\n");
|
||||
exit(1);
|
||||
}
|
||||
state->num_received += 1;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,121 +0,0 @@
|
||||
/*
|
||||
* Unix SMB/CIFS implementation.
|
||||
* Copyright (C) Volker Lendecke 2013
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __UNIX_MSG_H__
|
||||
#define __UNIX_MSG_H__
|
||||
|
||||
#include "replace.h"
|
||||
#include "poll_funcs/poll_funcs.h"
|
||||
#include "system/network.h"
|
||||
|
||||
/**
|
||||
* @file unix_msg.h
|
||||
*
|
||||
* @brief Send large messages over unix domain datagram sockets
|
||||
*
|
||||
* A unix_msg_ctx represents a unix domain datagram socket.
|
||||
*
|
||||
* Unix domain datagram sockets have some unique properties compared with UDP
|
||||
* sockets:
|
||||
*
|
||||
* - They are reliable, i.e. as long as both sender and receiver are processes
|
||||
* that are alive, nothing is lost.
|
||||
*
|
||||
* - They preserve sequencing
|
||||
*
|
||||
* Based on these two properties, this code implements sending of large
|
||||
* messages. It aims at being maximally efficient for short, single-datagram
|
||||
* messages. Ideally, if the receiver queue is not full, sending a message
|
||||
* should be a single syscall without malloc. Receiving a message should also
|
||||
* not malloc anything before the data is shipped to the user.
|
||||
*
|
||||
* If unix_msg_send meets a full receive buffer, more effort is required: The
|
||||
* socket behind unix_msg_send is not pollable for POLLOUT, it will always be
|
||||
* writable: A datagram socket can send anywhere, the full queue is a property
|
||||
* of of the receiving socket. unix_msg_send creates a new unnamed socket that
|
||||
* it will connect(2) to the target socket. This unnamed socket is then
|
||||
* pollable for POLLOUT. The socket will be writable when the destination
|
||||
* socket's queue is drained sufficiently.
|
||||
*
|
||||
* If unix_msg_send is asked to send a message larger than fragment_size, it
|
||||
* will try sending the message in pieces with proper framing, the receiving
|
||||
* side will reassemble the messages.
|
||||
*
|
||||
* fd-passing is supported.
|
||||
* Note that by default the fds passed to recv_callback are closed by
|
||||
* the receive handler in order to avoid fd-leaks. If the provider of
|
||||
* the recv_callback wants to use a passed file descriptor after the
|
||||
* callback returns, it must copy the fd away and set the corresponding
|
||||
* entry in the "fds" array to -1.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Abstract structure representing a unix domain datagram socket
|
||||
*/
|
||||
struct unix_msg_ctx;
|
||||
|
||||
/**
|
||||
* @brief Initialize a struct unix_msg_ctx
|
||||
*
|
||||
* @param[in] path The socket path
|
||||
* @param[in] ev_funcs The event callback functions to use
|
||||
* @param[in] fragment_size Maximum datagram size to send/receive
|
||||
* @param[in] recv_callback Function called when a message is received
|
||||
* @param[in] private_data Private pointer for recv_callback
|
||||
* @param[out] result The new struct unix_msg_ctx
|
||||
* @return 0 on success, errno on failure
|
||||
*/
|
||||
|
||||
|
||||
int unix_msg_init(const struct sockaddr_un *addr,
|
||||
const struct poll_funcs *ev_funcs,
|
||||
size_t fragment_size,
|
||||
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);
|
||||
|
||||
/**
|
||||
* @brief Send a message
|
||||
*
|
||||
* @param[in] ctx The context to send across
|
||||
* @param[in] dst_sock The destination socket path
|
||||
* @param[in] iov The message
|
||||
* @param[in] iovlen The number of iov structs
|
||||
* @param[in] fds - optional fd array
|
||||
* @param[in] num_fds - fd array size
|
||||
* @return 0 on success, errno on failure
|
||||
*/
|
||||
|
||||
int unix_msg_send(struct unix_msg_ctx *ctx, const struct sockaddr_un *dst,
|
||||
const struct iovec *iov, int iovlen,
|
||||
const int *fds, size_t num_fds);
|
||||
|
||||
/**
|
||||
* @brief Free a unix_msg_ctx
|
||||
*
|
||||
* @param[in] ctx The message context to free
|
||||
* @return 0 on success, errno on failure (EBUSY)
|
||||
*/
|
||||
int unix_msg_free(struct unix_msg_ctx *ctx);
|
||||
|
||||
#define SENDQ_CACHE_TIME_SECS 10
|
||||
|
||||
#endif
|
@ -1,18 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
bld.SAMBA3_SUBSYSTEM('UNIX_MSG',
|
||||
source='unix_msg.c',
|
||||
deps='replace PTHREADPOOL iov_buf msghdr time-basic')
|
||||
|
||||
bld.SAMBA3_BINARY('unix_msg_test',
|
||||
source='tests.c',
|
||||
deps='UNIX_MSG POLL_FUNCS_TEVENT',
|
||||
install=False)
|
||||
bld.SAMBA3_BINARY('unix_msg_test_drain',
|
||||
source='test_drain.c',
|
||||
deps='UNIX_MSG POLL_FUNCS_TEVENT',
|
||||
install=False)
|
||||
bld.SAMBA3_BINARY('unix_msg_test_source',
|
||||
source='test_source.c',
|
||||
deps='UNIX_MSG POLL_FUNCS_TEVENT',
|
||||
install=False)
|
Loading…
Reference in New Issue
Block a user