chiark / gitweb /
util: unify SO_PEERCRED/SO_PEERSEC invocations
[elogind.git] / src / libsystemd-bus / bus-socket.c
index a449ce0eb3aa65d1877bac08336c6359169cb8ff..0c4b6af447297fbe8f1e348f096df36d879f37d9 100644 (file)
@@ -38,6 +38,8 @@
 #include "bus-internal.h"
 #include "bus-message.h"
 
+#define SNDBUF_SIZE (8*1024*1024)
+
 static void iovec_advance(struct iovec iov[], unsigned *idx, size_t size) {
 
         while (size > 0) {
@@ -600,9 +602,8 @@ static int bus_socket_read_auth(sd_bus *b) {
         return 1;
 }
 
-int bus_socket_setup(sd_bus *b) {
+void bus_socket_setup(sd_bus *b) {
         int enable;
-        socklen_t l;
 
         assert(b);
 
@@ -614,19 +615,20 @@ int bus_socket_setup(sd_bus *b) {
         enable = !b->bus_client && (b->attach_flags & KDBUS_ATTACH_SECLABEL);
         setsockopt(b->input_fd, SOL_SOCKET, SO_PASSSEC, &enable, sizeof(enable));
 
-        /* Increase the buffers to a MB */
-        fd_inc_rcvbuf(b->input_fd, 1024*1024);
-        fd_inc_sndbuf(b->output_fd, 1024*1024);
-
-        /* Get the peer for socketpair() sockets */
-        l = sizeof(b->ucred);
-        if (getsockopt(b->input_fd, SOL_SOCKET, SO_PEERCRED, &b->ucred, &l) >= 0 && l >= sizeof(b->ucred))
-                b->ucred_valid = b->ucred.pid > 0;
+        /* Increase the buffers to 8 MB */
+        fd_inc_rcvbuf(b->input_fd, SNDBUF_SIZE);
+        fd_inc_sndbuf(b->output_fd, SNDBUF_SIZE);
 
         b->is_kernel = false;
         b->message_version = 1;
+        b->message_endian = 0;
+}
 
-        return 0;
+static void bus_get_peercred(sd_bus *b) {
+        assert(b);
+
+        /* Get the peer for socketpair() sockets */
+        b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
 }
 
 static int bus_socket_start_auth_client(sd_bus *b) {
@@ -674,6 +676,8 @@ static int bus_socket_start_auth_client(sd_bus *b) {
 int bus_socket_start_auth(sd_bus *b) {
         assert(b);
 
+        bus_get_peercred(b);
+
         b->state = BUS_AUTHENTICATING;
         b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_DEFAULT_TIMEOUT;
 
@@ -704,9 +708,7 @@ int bus_socket_connect(sd_bus *b) {
 
         b->output_fd = b->input_fd;
 
-        r = bus_socket_setup(b);
-        if (r < 0)
-                return r;
+        bus_socket_setup(b);
 
         r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size);
         if (r < 0) {
@@ -768,20 +770,15 @@ int bus_socket_exec(sd_bus *b) {
         close_nointr_nofail(s[1]);
         b->output_fd = b->input_fd = s[0];
 
-        r = bus_socket_setup(b);
-        if (r < 0)
-                return r;
+        bus_socket_setup(b);
 
         return bus_socket_start_auth(b);
 }
 
 int bus_socket_take_fd(sd_bus *b) {
-        int  r;
         assert(b);
 
-        r = bus_socket_setup(b);
-        if (r < 0)
-                return r;
+        bus_socket_setup(b);
 
         return bus_socket_start_auth(b);
 }
@@ -922,8 +919,8 @@ static int bus_socket_make_message(sd_bus *bus, size_t size) {
         r = bus_message_from_malloc(bus,
                                     bus->rbuffer, size,
                                     bus->fds, bus->n_fds,
-                                    bus->ucred_valid ? &bus->ucred : NULL,
-                                    bus->label[0] ? bus->label : NULL,
+                                    !bus->bus_client && bus->ucred_valid ? &bus->ucred : NULL,
+                                    !bus->bus_client && bus->label[0] ? bus->label : NULL,
                                     &t);
         if (r < 0) {
                 free(b);