+typedef struct {
+ int fd;
+} ClientContext;
+
+static ClientContext *client_context_free(ClientContext *c) {
+ if (!c)
+ return NULL;
+
+ close(c->fd);
+ free(c);
+
+ return NULL;
+}
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(ClientContext*, client_context_free);
+
+static int client_context_new(ClientContext **out, int fd) {
+ _cleanup_(client_context_freep) ClientContext *c = NULL;
+
+ c = new0(ClientContext, 1);
+ if (!c)
+ return log_oom();
+
+ c->fd = fd;
+
+ *out = c;
+ c = NULL;
+ return 0;
+}
+
+static void *run_client(void *userdata) {
+ _cleanup_(client_context_freep) ClientContext *c = userdata;
+ _cleanup_(proxy_freep) Proxy *p = NULL;
+ int r;
+
+ r = proxy_new(&p, c->fd, c->fd, arg_address);
+ if (r < 0)
+ goto exit;
+
+ r = proxy_load_policy(p, arg_configuration);
+ if (r < 0)
+ goto exit;
+
+ r = proxy_hello_policy(p, getuid());
+ if (r < 0)
+ goto exit;
+
+ r = proxy_run(p);
+
+exit:
+ return NULL;
+}
+
+static int loop_clients(int accept_fd) {
+ pthread_attr_t attr;
+ int r;
+
+ r = pthread_attr_init(&attr);
+ if (r < 0) {
+ r = log_error_errno(errno, "Cannot initialize pthread attributes: %m");
+ goto exit;
+ }
+
+ r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ if (r < 0) {
+ r = log_error_errno(errno, "Cannot mark pthread attributes as detached: %m");
+ goto exit_attr;
+ }
+
+ for (;;) {
+ ClientContext *c;
+ pthread_t tid;
+ int fd;
+
+ fd = accept4(accept_fd, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
+ if (fd < 0) {
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
+
+ r = log_error_errno(errno, "accept4() failed: %m");
+ break;
+ }
+
+ r = client_context_new(&c, fd);
+ if (r < 0) {
+ log_oom();
+ close(fd);
+ continue;
+ }
+
+ r = pthread_create(&tid, &attr, run_client, c);
+ if (r < 0) {
+ log_error("Cannot spawn thread: %m");
+ client_context_free(c);
+ continue;
+ }
+ }
+
+exit_attr:
+ pthread_attr_destroy(&attr);
+exit:
+ return r;
+}
+