} Context;
typedef struct Connection {
+ Context *context;
+
int server_fd, client_fd;
int server_to_client_buffer[2]; /* a pipe */
int client_to_server_buffer[2]; /* a pipe */
static void connection_free(Connection *c) {
assert(c);
+ if (c->context)
+ set_remove(c->context->connections, c);
+
sd_event_source_unref(c->server_event_source);
sd_event_source_unref(c->client_event_source);
while ((es = set_steal_first(context->listen)))
sd_event_source_unref(es);
- while ((c = set_steal_first(context->connections)))
+ while ((c = set_first(context->connections)))
connection_free(c);
set_free(context->listen);
if (c->client_fd == -1 && c->client_to_server_buffer_full <= 0)
goto quit;
- r = connection_enable_event_sources(c, sd_event_get(s));
+ r = connection_enable_event_sources(c, sd_event_source_get_event(s));
if (r < 0)
goto quit;
if (r < 0)
goto fail;
- r = connection_enable_event_sources(c, sd_event_get(s));
+ r = connection_enable_event_sources(c, sd_event_source_get_event(s));
if (r < 0)
goto fail;
if (!c)
return log_oom();
+ c->context = context;
c->server_fd = fd;
c->client_fd = -1;
c->server_to_client_buffer[0] = c->server_to_client_buffer[1] = -1;
c->client_to_server_buffer[0] = c->client_to_server_buffer[1] = -1;
+ r = set_put(context->connections, c);
+ if (r < 0) {
+ free(c);
+ return log_oom();
+ }
+
r = get_remote_sockaddr(&sa, &salen);
if (r < 0)
goto fail;
}
static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+ _cleanup_free_ char *peer = NULL;
Context *context = userdata;
int nfd = -1, r;
assert(context);
nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
- if (nfd >= 0) {
- _cleanup_free_ char *peer = NULL;
-
+ if (nfd < 0) {
+ if (errno != -EAGAIN)
+ log_warning("Failed to accept() socket: %m");
+ } else {
getpeername_pretty(nfd, &peer);
log_debug("New connection from %s", strna(peer));
- r = add_connection_socket(context, sd_event_get(s), nfd);
+ r = add_connection_socket(context, sd_event_source_get_event(s), nfd);
if (r < 0) {
+ log_error("Failed to accept connection, ignoring: %s", strerror(-r));
close_nointr_nofail(fd);
- return r;
}
-
- } else if (errno != -EAGAIN)
- log_warning("Failed to accept() socket: %m");
+ }
r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
if (r < 0) {
- log_error("Error %d while re-enabling listener with ONESHOT: %s", r, strerror(-r));
+ log_error("Error while re-enabling listener with ONESHOT: %s", strerror(-r));
+ sd_event_exit(sd_event_source_get_event(s), r);
return r;
}
assert(event);
assert(fd >= 0);
- log_info("Listening on %i", fd);
-
r = set_ensure_allocated(&context->listen, trivial_hash_func, trivial_compare_func);
if (r < 0) {
log_oom();
if (r <= 0)
goto finish;
- r = sd_event_new(&event);
+ r = sd_event_default(&event);
if (r < 0) {
log_error("Failed to allocate event loop: %s", strerror(-r));
goto finish;
}
+ sd_event_set_watchdog(event, true);
+
n = sd_listen_fds(1);
if (n < 0) {
log_error("Failed to receive sockets from parent.");