chiark / gitweb /
bus-proxy: share policy between threads
[elogind.git] / src / bus-proxyd / bus-proxyd.c
index 702f021a6bdcfed8df3082e4a7a53394b80fc546..72e11467bda144dc12d43a24c6d70bda1567e2de 100644 (file)
@@ -30,6 +30,7 @@
 #include <string.h>
 #include <errno.h>
 #include <sys/poll.h>
+#include <sys/prctl.h>
 #include <stddef.h>
 #include <getopt.h>
 #include <pthread.h>
@@ -60,6 +61,7 @@ static char **arg_configuration = NULL;
 
 typedef struct {
         int fd;
+        SharedPolicy *policy;
 } ClientContext;
 
 static ClientContext *client_context_free(ClientContext *c) {
@@ -74,14 +76,14 @@ static ClientContext *client_context_free(ClientContext *c) {
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(ClientContext*, client_context_free);
 
-static int client_context_new(ClientContext **out, int fd) {
+static int client_context_new(ClientContext **out) {
         _cleanup_(client_context_freep) ClientContext *c = NULL;
 
         c = new0(ClientContext, 1);
         if (!c)
                 return log_oom();
 
-        c->fd = fd;
+        c->fd = -1;
 
         *out = c;
         c = NULL;
@@ -91,13 +93,20 @@ static int client_context_new(ClientContext **out, int fd) {
 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;
 
-        r = proxy_load_policy(p, arg_configuration);
+        /* 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;
 
@@ -112,6 +121,7 @@ exit:
 }
 
 static int loop_clients(int accept_fd) {
+        _cleanup_(shared_policy_freep) SharedPolicy *sp = NULL;
         pthread_attr_t attr;
         int r;
 
@@ -127,6 +137,10 @@ static int loop_clients(int accept_fd) {
                 goto exit_attr;
         }
 
+        r = shared_policy_new(&sp);
+        if (r < 0)
+                goto exit_attr;
+
         for (;;) {
                 ClientContext *c;
                 pthread_t tid;
@@ -141,13 +155,16 @@ static int loop_clients(int accept_fd) {
                         break;
                 }
 
-                r = client_context_new(&c, fd);
+                r = client_context_new(&c);
                 if (r < 0) {
                         log_oom();
                         close(fd);
                         continue;
                 }
 
+                c->fd = fd;
+                c->policy = sp;
+
                 r = pthread_create(&tid, &attr, run_client, c);
                 if (r < 0) {
                         log_error("Cannot spawn thread: %m");