+typedef struct {
+ int fd;
+ SharedPolicy *policy;
+ uid_t bus_uid;
+} ClientContext;
+
+static ClientContext *client_context_free(ClientContext *c) {
+ if (!c)
+ return NULL;
+
+ safe_close(c->fd);
+ free(c);
+
+ return NULL;
+}
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(ClientContext*, client_context_free);
+
+static int client_context_new(ClientContext **out) {
+ _cleanup_(client_context_freep) ClientContext *c = NULL;
+
+ c = new0(ClientContext, 1);
+ if (!c)
+ return -ENOMEM;
+
+ c->fd = -1;
+
+ *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;
+ char comm[16];
+ int r;
+
+ r = proxy_new(&p, c->fd, c->fd, arg_address);
+ if (r < 0)
+ goto exit;
+
+ c->fd = -1;
+
+ /* set comm to "p$PIDu$UID" and suffix with '*' if truncated */
+ r = snprintf(comm, sizeof(comm), "p" PID_FMT "u" UID_FMT, p->local_creds.pid, p->local_creds.uid);
+ if (r >= (ssize_t)sizeof(comm))
+ comm[sizeof(comm) - 2] = '*';
+ (void) prctl(PR_SET_NAME, comm);
+
+ r = proxy_set_policy(p, c->policy, arg_configuration);
+ if (r < 0)
+ goto exit;
+
+ r = proxy_hello_policy(p, c->bus_uid);
+ if (r < 0)
+ goto exit;
+
+ r = proxy_run(p);
+
+exit:
+ return NULL;
+}
+
+static int loop_clients(int accept_fd, uid_t bus_uid) {
+ _cleanup_(shared_policy_freep) SharedPolicy *sp = NULL;
+ pthread_attr_t attr;
+ int r;
+
+ r = pthread_attr_init(&attr);
+ if (r < 0) {
+ return log_error_errno(errno, "Cannot initialize pthread attributes: %m");
+ }
+
+ r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ if (r < 0) {
+ r = log_error_errno(errno, "Cannot mark pthread attributes as detached: %m");
+ goto finish;
+ }
+
+ r = shared_policy_new(&sp);
+ if (r < 0)
+ goto finish;
+
+ 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");
+ goto finish;
+ }
+
+ r = client_context_new(&c);
+ if (r < 0) {
+ log_oom();
+ close(fd);
+ continue;
+ }
+
+ c->fd = fd;
+ c->policy = sp;
+ c->bus_uid = bus_uid;
+
+ r = pthread_create(&tid, &attr, run_client, c);
+ if (r < 0) {
+ log_error("Cannot spawn thread: %m");
+ client_context_free(c);
+ continue;
+ }
+ }
+
+finish:
+ pthread_attr_destroy(&attr);
+ return r;
+}
+