[systemd-commits] 5 commits - src/bus-proxyd src/libsystemd

Lennart Poettering lennart at kemper.freedesktop.org
Fri Feb 13 08:19:31 PST 2015


 src/bus-proxyd/bus-proxyd.c         |   32 ++++++++++++++------------------
 src/bus-proxyd/proxy.c              |   28 ++++++++++++++++++++--------
 src/libsystemd/sd-bus/bus-control.c |   19 +++++++++----------
 3 files changed, 43 insertions(+), 36 deletions(-)

New commits:
commit c74f883c6f7d5901b3c543d47f64082ccd91a895
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Feb 13 17:17:45 2015 +0100

    bus-proxy: ECONNRESET/ENOTCONN can hit us on every step, hence handle it on every step

diff --git a/src/bus-proxyd/proxy.c b/src/bus-proxyd/proxy.c
index a34c431..73f68b7 100644
--- a/src/bus-proxyd/proxy.c
+++ b/src/bus-proxyd/proxy.c
@@ -376,7 +376,7 @@ static int proxy_wait(Proxy *p) {
         }
 
         pollfd = (struct pollfd[3]) {
-                { .fd = fd,           .events = events_destination,            },
+                { .fd = fd,           .events = events_destination,     },
                 { .fd = p->local_in,  .events = events_local & POLLIN,  },
                 { .fd = p->local_out, .events = events_local & POLLOUT, },
         };
@@ -689,6 +689,8 @@ static int proxy_process_destination_to_local(Proxy *p) {
                 return -ECONNRESET;
 
         r = synthesize_name_acquired(p->destination_bus, p->local_bus, m);
+        if (r == -ECONNRESET || r == -ENOTCONN)
+                return r;
         if (r < 0)
                 return log_error_errno(r, "Failed to synthesize message: %m");
 
@@ -696,6 +698,8 @@ static int proxy_process_destination_to_local(Proxy *p) {
 
         if (p->policy) {
                 r = process_policy(p->destination_bus, p->local_bus, m, p->policy, &p->local_creds, p->owned_names);
+                if (r == -ECONNRESET || r == -ENOTCONN)
+                        return r;
                 if (r < 0)
                         return log_error_errno(r, "Failed to process policy: %m");
                 if (r > 0)
@@ -755,12 +759,16 @@ static int proxy_process_local_to_destination(Proxy *p) {
                 return -ECONNRESET;
 
         r = process_hello(p, m);
+        if (r == -ECONNRESET || r == -ENOTCONN)
+                return r;
         if (r < 0)
                 return log_error_errno(r, "Failed to process HELLO: %m");
         if (r > 0)
                 return 1;
 
         r = bus_proxy_process_driver(p->destination_bus, p->local_bus, m, p->policy, &p->local_creds, p->owned_names);
+        if (r == -ECONNRESET || r == -ENOTCONN)
+                return r;
         if (r < 0)
                 return log_error_errno(r, "Failed to process driver calls: %m");
         if (r > 0)
@@ -769,9 +777,11 @@ static int proxy_process_local_to_destination(Proxy *p) {
         for (;;) {
                 if (p->policy) {
                         r = process_policy(p->local_bus, p->destination_bus, m, p->policy, &p->local_creds, p->owned_names);
+                        if (r == -ECONNRESET || r == -ENOTCONN)
+                                return r;
                         if (r < 0)
                                 return log_error_errno(r, "Failed to process policy: %m");
-                        else if (r > 0)
+                        if (r > 0)
                                 return 1;
                 }
 
@@ -829,6 +839,8 @@ int proxy_run(Proxy *p) {
 
                 if (!busy) {
                         r = proxy_wait(p);
+                        if (r == -ECONNRESET || r == -ENOTCONN)
+                                return 0;
                         if (r < 0)
                                 return r;
                 }

commit 5569b33a8ce1968200db801ef28585347103239c
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Feb 13 17:17:28 2015 +0100

    bus-proxy: a few simplifications

diff --git a/src/bus-proxyd/bus-proxyd.c b/src/bus-proxyd/bus-proxyd.c
index 478dd78..b6b0056 100644
--- a/src/bus-proxyd/bus-proxyd.c
+++ b/src/bus-proxyd/bus-proxyd.c
@@ -130,19 +130,18 @@ static int loop_clients(int accept_fd, uid_t bus_uid) {
 
         r = pthread_attr_init(&attr);
         if (r < 0) {
-                r = log_error_errno(errno, "Cannot initialize pthread attributes: %m");
-                goto exit;
+                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 exit_attr;
+                goto finish;
         }
 
         r = shared_policy_new(&sp);
         if (r < 0)
-                goto exit_attr;
+                goto finish;
 
         for (;;) {
                 ClientContext *c;
@@ -155,7 +154,7 @@ static int loop_clients(int accept_fd, uid_t bus_uid) {
                                 continue;
 
                         r = log_error_errno(errno, "accept4() failed: %m");
-                        break;
+                        goto finish;
                 }
 
                 r = client_context_new(&c);
@@ -177,9 +176,8 @@ static int loop_clients(int accept_fd, uid_t bus_uid) {
                 }
         }
 
-exit_attr:
+finish:
         pthread_attr_destroy(&attr);
-exit:
         return r;
 }
 
@@ -234,17 +232,11 @@ static int parse_argv(int argc, char *argv[]) {
                         puts(SYSTEMD_FEATURES);
                         return 0;
 
-                case ARG_ADDRESS: {
-                        char *a;
-
-                        a = strdup(optarg);
-                        if (!a)
+                case ARG_ADDRESS:
+                        r = free_and_strdup(&arg_address, optarg);
+                        if (r < 0)
                                 return log_oom();
-
-                        free(arg_address);
-                        arg_address = a;
                         break;
-                }
 
                 case ARG_CONFIGURATION:
                         r = strv_extend(&arg_configuration, optarg);
@@ -296,7 +288,6 @@ static int parse_argv(int argc, char *argv[]) {
 }
 
 int main(int argc, char *argv[]) {
-        const char *user = "systemd-bus-proxy";
         int r, accept_fd;
         uid_t uid, bus_uid;
         gid_t gid;
@@ -308,6 +299,8 @@ int main(int argc, char *argv[]) {
         bus_uid = getuid();
 
         if (geteuid() == 0) {
+                const char *user = "systemd-bus-proxy";
+
                 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
                 if (r < 0) {
                         log_error_errno(r, "Cannot resolve user name %s: %m", user);
@@ -332,6 +325,7 @@ int main(int argc, char *argv[]) {
         }
 
         accept_fd = SD_LISTEN_FDS_START;
+
         r = fd_nonblock(accept_fd, false);
         if (r < 0) {
                 log_error_errno(r, "Cannot mark accept-fd non-blocking: %m");

commit e044970a29b83e128c5a78f70e7d5ad1c74e7058
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Feb 13 17:16:51 2015 +0100

    sd-bus: initialize a few structs at time or declaration

diff --git a/src/libsystemd/sd-bus/bus-control.c b/src/libsystemd/sd-bus/bus-control.c
index 2d51c74..06e5b4f 100644
--- a/src/libsystemd/sd-bus/bus-control.c
+++ b/src/libsystemd/sd-bus/bus-control.c
@@ -233,16 +233,16 @@ _public_ int sd_bus_release_name(sd_bus *bus, const char *name) {
 }
 
 static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
-        struct kdbus_cmd_list cmd = {};
+        struct kdbus_cmd_list cmd = {
+                .size = sizeof(cmd),
+                .flags = flags,
+        };
         struct kdbus_info *name_list, *name;
         uint64_t previous_id = 0;
         int r;
 
         /* Caller will free half-constructed list on failure... */
 
-        cmd.size = sizeof(cmd);
-        cmd.flags = flags;
-
         r = ioctl(bus->input_fd, KDBUS_CMD_LIST, &cmd);
         if (r < 0)
                 return -errno;
@@ -896,7 +896,7 @@ _public_ int sd_bus_get_name_creds(
 static int bus_get_owner_creds_kdbus(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
         _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
         struct kdbus_cmd_info cmd = {
-                .size = sizeof(struct kdbus_cmd_info)
+                .size = sizeof(struct kdbus_cmd_info),
         };
         struct kdbus_info *creator_info;
         pid_t pid = 0;
@@ -1403,7 +1403,10 @@ int bus_remove_match_internal_kernel(
                 sd_bus *bus,
                 uint64_t cookie) {
 
-        struct kdbus_cmd_match m;
+        struct kdbus_cmd_match m = {
+                .size = offsetof(struct kdbus_cmd_match, items),
+                .cookie = cookie,
+        };
         int r;
 
         assert(bus);
@@ -1412,10 +1415,6 @@ int bus_remove_match_internal_kernel(
         if (bus->hello_flags & KDBUS_HELLO_MONITOR)
                 return 0;
 
-        zero(m);
-        m.size = offsetof(struct kdbus_cmd_match, items);
-        m.cookie = cookie;
-
         r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m);
         if (r < 0)
                 return -errno;

commit 95eb099fa8a90212ed2aef82bad360f147bc19c2
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Feb 13 17:15:41 2015 +0100

    bus-proxy: close each connection fd only once
    
    After passing the fds over to the sd_bus object, we should forget them,
    so that we don't close them a second time when the object goes away.

diff --git a/src/bus-proxyd/bus-proxyd.c b/src/bus-proxyd/bus-proxyd.c
index e07761a..478dd78 100644
--- a/src/bus-proxyd/bus-proxyd.c
+++ b/src/bus-proxyd/bus-proxyd.c
@@ -69,7 +69,7 @@ static ClientContext *client_context_free(ClientContext *c) {
         if (!c)
                 return NULL;
 
-        close(c->fd);
+        safe_close(c->fd);
         free(c);
 
         return NULL;
@@ -101,6 +101,8 @@ static void *run_client(void *userdata) {
         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))

commit 557b5d4a94967198b3181fcb83879d4569cbf456
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Feb 13 16:11:45 2015 +0100

    bus-proxy: also consider ENOTCONN a clean termination condition
    
    Sometimes, when we try to reply to messages we don't check return
    values. This means we might miss a ECONNRESET, and will get a ENOTCONN
    on next command. Treat both the same hence.

diff --git a/src/bus-proxyd/proxy.c b/src/bus-proxyd/proxy.c
index a07c403..a34c431 100644
--- a/src/bus-proxyd/proxy.c
+++ b/src/bus-proxyd/proxy.c
@@ -673,7 +673,7 @@ static int proxy_process_destination_to_local(Proxy *p) {
         assert(p);
 
         r = sd_bus_process(p->destination_bus, &m);
-        if (r == -ECONNRESET) /* Treat 'connection reset by peer' as clean exit condition */
+        if (r == -ECONNRESET || r == -ENOTCONN) /* Treat 'connection reset by peer' as clean exit condition */
                 return r;
         if (r < 0) {
                 log_error_errno(r, "Failed to process destination bus: %m");
@@ -704,7 +704,7 @@ static int proxy_process_destination_to_local(Proxy *p) {
 
         r = sd_bus_send(p->local_bus, m, NULL);
         if (r < 0) {
-                if (r == -ECONNRESET)
+                if (r == -ECONNRESET || r == -ENOTCONN)
                         return r;
 
                 /* If the peer tries to send a reply and it is
@@ -739,7 +739,7 @@ static int proxy_process_local_to_destination(Proxy *p) {
         assert(p);
 
         r = sd_bus_process(p->local_bus, &m);
-        if (r == -ECONNRESET) /* Treat 'connection reset by peer' as clean exit condition */
+        if (r == -ECONNRESET || r == -ENOTCONN) /* Treat 'connection reset by peer' as clean exit condition */
                 return r;
         if (r < 0) {
                 log_error_errno(r, "Failed to process local bus: %m");
@@ -777,7 +777,7 @@ static int proxy_process_local_to_destination(Proxy *p) {
 
                 r = sd_bus_send(p->destination_bus, m, NULL);
                 if (r < 0) {
-                        if (r == -ECONNRESET)
+                        if (r == -ECONNRESET || r == -ENOTCONN)
                                 return r;
 
                         /* The name database changed since the policy check, hence let's check again */
@@ -810,7 +810,7 @@ int proxy_run(Proxy *p) {
                 if (p->got_hello) {
                         /* Read messages from bus, to pass them on to our client */
                         r = proxy_process_destination_to_local(p);
-                        if (r == -ECONNRESET)
+                        if (r == -ECONNRESET || r == -ENOTCONN)
                                 return 0;
                         if (r < 0)
                                 return r;
@@ -820,7 +820,7 @@ int proxy_run(Proxy *p) {
 
                 /* Read messages from our client, to pass them on to the bus */
                 r = proxy_process_local_to_destination(p);
-                if (r == -ECONNRESET)
+                if (r == -ECONNRESET || r == -ENOTCONN)
                         return 0;
                 if (r < 0)
                         return r;



More information about the systemd-commits mailing list