diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-12-21 11:23:08 -0500 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-12-21 18:34:40 -0500 |
commit | 9818fa6d6d32d87a3e1b96934a54523ea6b02879 (patch) | |
tree | 043715494baf123dc42cb33bed31bacf1433eab3 /src | |
parent | cc4e8b6f70a42b7bf8f6761006ab95fcd195a3be (diff) |
bus-proxyd: use a loop instead of c&p
Diffstat (limited to 'src')
-rw-r--r-- | src/bus-proxyd/bus-proxyd.c | 136 |
1 files changed, 55 insertions, 81 deletions
diff --git a/src/bus-proxyd/bus-proxyd.c b/src/bus-proxyd/bus-proxyd.c index 059f3f6d1a..2944596b09 100644 --- a/src/bus-proxyd/bus-proxyd.c +++ b/src/bus-proxyd/bus-proxyd.c @@ -268,103 +268,77 @@ int main(int argc, char *argv[]) { for (;;) { _cleanup_bus_message_unref_ sd_bus_message *m = NULL; - int events_a, events_b, fd; - uint64_t timeout_a, timeout_b, t; + uint64_t t; struct timespec _ts, *ts; struct pollfd *pollfd; - int k; - - r = sd_bus_process(a, &m); - if (r < 0) { - /* treat 'connection reset by peer' as clean exit condition */ - if (r == -ECONNRESET) - r = 0; - else - log_error("Failed to process bus a: %s", strerror(-r)); - - goto finish; - } + int k, i, fd; + + struct bus_bus { + sd_bus *bus; + const char *name; + int events; + uint64_t timeout; + } busses[2] = { + {a, "a"}, + {b, "b"}, + }; - if (m) { - /* We officially got EOF, let's quit */ - if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) { - r = 0; + for (i = 0; i < 2; i ++) { + r = sd_bus_process(busses[i].bus, &m); + if (r < 0) { + /* treat 'connection reset by peer' as clean exit condition */ + if (r == -ECONNRESET) + r = 0; + else + log_error("Failed to process bus %s: %s", + busses[i].name, strerror(-r)); goto finish; } - k = sd_bus_send(b, m, NULL); - if (k < 0) { - r = k; - log_error("Failed to send message: %s", strerror(-r)); - goto finish; + if (m) { + /* We officially got EOF, let's quit */ + if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) { + r = 0; + goto finish; + } + + k = sd_bus_send(busses[1-i].bus, m, NULL); + if (k < 0) { + r = k; + log_error("Failed to send message to bus %s: %s", + busses[1-i].name, strerror(-r)); + goto finish; + } } - } - - if (r > 0) - continue; - r = sd_bus_process(b, &m); - if (r < 0) { - /* treat 'connection reset by peer' as clean exit condition */ - if (r == -ECONNRESET) - r = 0; - else - log_error("Failed to process bus b: %s", strerror(-r)); - - goto finish; + if (r > 0) + continue; } - if (m) { - /* We officially got EOF, let's quit */ - if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) { - r = 0; - goto finish; - } - - k = sd_bus_send(a, m, NULL); - if (k < 0) { - r = k; - log_error("Failed to send message: %s", strerror(-r)); - goto finish; - } - } - - if (r > 0) - continue; - fd = sd_bus_get_fd(a); if (fd < 0) { log_error("Failed to get fd: %s", strerror(-r)); goto finish; } - events_a = sd_bus_get_events(a); - if (events_a < 0) { - log_error("Failed to get events mask: %s", strerror(-r)); - goto finish; - } - - r = sd_bus_get_timeout(a, &timeout_a); - if (r < 0) { - log_error("Failed to get timeout: %s", strerror(-r)); - goto finish; - } - - events_b = sd_bus_get_events(b); - if (events_b < 0) { - log_error("Failed to get events mask: %s", strerror(-r)); - goto finish; - } + for (i = 0; i < 2; i ++) { + busses[i].events = sd_bus_get_events(a); + if (busses[i].events < 0) { + log_error("Failed to get events mask: %s", strerror(-r)); + goto finish; + } - r = sd_bus_get_timeout(b, &timeout_b); - if (r < 0) { - log_error("Failed to get timeout: %s", strerror(-r)); - goto finish; + r = sd_bus_get_timeout(a, &busses[i].timeout); + if (r < 0) { + log_error("Failed to get timeout: %s", strerror(-r)); + goto finish; + } } - t = timeout_a; - if (t == (uint64_t) -1 || (timeout_b != (uint64_t) -1 && timeout_b < timeout_a)) - t = timeout_b; + t = busses[0].timeout; + if (t == (uint64_t) -1 || + (busses[1].timeout != (uint64_t) -1 && busses[1].timeout < t)) + t = busses[1].timeout; if (t == (uint64_t) -1) ts = NULL; @@ -381,9 +355,9 @@ int main(int argc, char *argv[]) { } pollfd = (struct pollfd[3]) { - {.fd = fd, .events = events_a, }, - {.fd = in_fd, .events = events_b & POLLIN, }, - {.fd = out_fd, .events = events_b & POLLOUT, } + {.fd = fd, .events = busses[0].events }, + {.fd = in_fd, .events = busses[1].events & POLLIN }, + {.fd = out_fd, .events = busses[1].events & POLLOUT }, }; r = ppoll(pollfd, 3, ts, NULL); |