From 249968612f16a71df909d6e73785c18a9ff36a65 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 4 Aug 2014 16:22:09 +0200 Subject: [PATCH] bus: always explicitly close bus from main programs Since b5eca3a2059f9399d1dc52cbcf9698674c4b1cf0 we don't attempt to GC busses anymore when unsent messages remain that keep their reference, when they otherwise are not referenced anymore. This means that if we explicitly want connections to go away, we need to close them. With this change we will no do so explicitly wherver we connect to the bus from a main program (and thus know when the bus connection should go away), or when we create a private bus connection, that really should go away after our use. This fixes connection leaks in the NSS and PAM modules. --- src/analyze/analyze.c | 2 +- src/bus-proxyd/bus-proxyd.c | 7 +------ src/cgls/cgls.c | 2 +- src/cgroups-agent/cgroups-agent.c | 4 +--- src/fsck/fsck.c | 2 +- src/hostname/hostnamectl.c | 2 +- src/hostname/hostnamed.c | 5 ++--- src/libsystemd/sd-bus/bus-util.h | 9 +++++++++ src/libsystemd/sd-bus/busctl.c | 2 +- src/locale/localectl.c | 2 +- src/locale/localed.c | 4 ++-- src/login/inhibit.c | 2 +- src/login/loginctl.c | 2 +- src/login/pam_systemd.c | 4 ++-- src/machine/machinectl.c | 4 ++-- src/nspawn/nspawn.c | 4 ++-- src/nss-mymachines/nss-mymachines.c | 4 ++-- src/nss-resolve/nss-resolve.c | 6 +++--- src/resolve-host/resolve-host.c | 2 +- src/run/run.c | 2 +- src/systemctl/systemctl.c | 6 +++--- src/timedate/timedatectl.c | 2 +- src/timedate/timedated.c | 4 ++-- 23 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index d0bf01468..fdbce7cda 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -1346,7 +1346,7 @@ int main(int argc, char *argv[]) { arg_user ? SYSTEMD_USER : SYSTEMD_SYSTEM, arg_man); else { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; r = bus_open_transport_systemd(arg_transport, arg_host, arg_user, &bus); if (r < 0) { diff --git a/src/bus-proxyd/bus-proxyd.c b/src/bus-proxyd/bus-proxyd.c index e5713d6af..4f24e331e 100644 --- a/src/bus-proxyd/bus-proxyd.c +++ b/src/bus-proxyd/bus-proxyd.c @@ -1090,7 +1090,7 @@ static int patch_sender(sd_bus *a, sd_bus_message *m) { int main(int argc, char *argv[]) { - _cleanup_bus_unref_ sd_bus *a = NULL, *b = NULL; + _cleanup_bus_close_unref_ sd_bus *a = NULL, *b = NULL; sd_id128_t server_id; int r, in_fd, out_fd; bool got_hello = false; @@ -1474,11 +1474,6 @@ int main(int argc, char *argv[]) { } finish: - sd_bus_flush(a); - sd_bus_flush(b); - sd_bus_close(a); - sd_bus_close(b); - policy_free(&policy); strv_free(arg_configuration); free(arg_address); diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c index 6e9bd232a..7070503d6 100644 --- a/src/cgls/cgls.c +++ b/src/cgls/cgls.c @@ -128,7 +128,7 @@ int main(int argc, char *argv[]) { int r = 0, retval = EXIT_FAILURE; int output_flags; _cleanup_free_ char *root = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; log_parse_environment(); log_open(); diff --git a/src/cgroups-agent/cgroups-agent.c b/src/cgroups-agent/cgroups-agent.c index d1d843bda..e581fbc9b 100644 --- a/src/cgroups-agent/cgroups-agent.c +++ b/src/cgroups-agent/cgroups-agent.c @@ -26,7 +26,7 @@ #include "bus-util.h" int main(int argc, char *argv[]) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; if (argc != 2) { @@ -62,7 +62,5 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - sd_bus_flush(bus); - return EXIT_SUCCESS; } diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index e8596d90f..dfe97bc26 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -47,7 +47,7 @@ static const char *arg_repair = "-a"; static void start_target(const char *target) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; assert(target); diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 69da54f1f..f8e1a811f 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -497,7 +497,7 @@ static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) { } int main(int argc, char *argv[]) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; setlocale(LC_ALL, ""); diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 74d9dab07..4acdb72b1 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -634,7 +634,7 @@ static const sd_bus_vtable hostname_vtable[] = { }; static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; assert(c); @@ -673,9 +673,8 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { int main(int argc, char *argv[]) { Context context = {}; - _cleanup_event_unref_ sd_event *event = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; log_set_target(LOG_TARGET_AUTO); diff --git a/src/libsystemd/sd-bus/bus-util.h b/src/libsystemd/sd-bus/bus-util.h index 0a3d5dd56..af5055392 100644 --- a/src/libsystemd/sd-bus/bus-util.h +++ b/src/libsystemd/sd-bus/bus-util.h @@ -137,6 +137,14 @@ typedef struct UnitInfo { int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u); +static inline void sd_bus_close_unrefp(sd_bus **bus) { + if (*bus) { + sd_bus_flush(*bus); + sd_bus_close(*bus); + sd_bus_unref(*bus); + } +} + DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_slot*, sd_bus_slot_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_message*, sd_bus_message_unref); @@ -144,6 +152,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_creds*, sd_bus_creds_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_track*, sd_bus_track_unref); #define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp) +#define _cleanup_bus_close_unref_ _cleanup_(sd_bus_close_unrefp) #define _cleanup_bus_slot_unref_ _cleanup_(sd_bus_slot_unrefp) #define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp) #define _cleanup_bus_creds_unref_ _cleanup_(sd_bus_creds_unrefp) diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index b6839c13e..af7180441 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -482,7 +482,7 @@ static int busctl_main(sd_bus *bus, int argc, char *argv[]) { } int main(int argc, char *argv[]) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; log_parse_environment(); diff --git a/src/locale/localectl.c b/src/locale/localectl.c index 69d50076a..2bb0d3b6b 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -621,7 +621,7 @@ static int localectl_main(sd_bus *bus, int argc, char *argv[]) { } int main(int argc, char*argv[]) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; setlocale(LC_ALL, ""); diff --git a/src/locale/localed.c b/src/locale/localed.c index bce99b8cb..3cfc2ea4d 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -1079,7 +1079,7 @@ static const sd_bus_vtable locale_vtable[] = { }; static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; assert(c); @@ -1119,7 +1119,7 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { int main(int argc, char *argv[]) { Context context = {}; _cleanup_event_unref_ sd_event *event = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; log_set_target(LOG_TARGET_AUTO); diff --git a/src/login/inhibit.c b/src/login/inhibit.c index 02b6b2575..d5ea1d913 100644 --- a/src/login/inhibit.c +++ b/src/login/inhibit.c @@ -218,7 +218,7 @@ static int parse_argv(int argc, char *argv[]) { int main(int argc, char *argv[]) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; log_parse_environment(); diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 1773276b9..fcdf6275e 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -1295,7 +1295,7 @@ static int loginctl_main(sd_bus *bus, int argc, char *argv[]) { } int main(int argc, char *argv[]) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; setlocale(LC_ALL, ""); diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c index f522d6ec4..0c71177de 100644 --- a/src/login/pam_systemd.c +++ b/src/login/pam_systemd.c @@ -213,7 +213,7 @@ _public_ PAM_EXTERN int pam_sm_open_session( *seat = NULL, *type = NULL, *class = NULL, *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int session_fd = -1, existing, r; bool debug = false, remote; struct passwd *pw; @@ -496,7 +496,7 @@ _public_ PAM_EXTERN int pam_sm_close_session( int argc, const char **argv) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; const void *existing = NULL; const char *id; int r; diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 02d5d301c..7491d69c8 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -661,7 +661,7 @@ static int openpt_in_namespace(pid_t pid, int flags) { static int login_machine(sd_bus *bus, char **args, unsigned n) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *reply2 = NULL, *reply3 = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_unref_ sd_bus *container_bus = NULL; + _cleanup_bus_close_unref_ sd_bus *container_bus = NULL; _cleanup_close_ int master = -1; _cleanup_free_ char *getty = NULL; const char *path, *pty, *p; @@ -989,7 +989,7 @@ static int machinectl_main(sd_bus *bus, int argc, char *argv[]) { } int main(int argc, char*argv[]) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; setlocale(LC_ALL, ""); diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 76e86b7e0..ae09f936f 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -1386,7 +1386,7 @@ static int drop_capabilities(void) { static int register_machine(pid_t pid, int local_ifindex) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; if (!arg_register) @@ -1521,7 +1521,7 @@ static int register_machine(pid_t pid, int local_ifindex) { static int terminate_machine(pid_t pid) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; const char *path; int r; diff --git a/src/nss-mymachines/nss-mymachines.c b/src/nss-mymachines/nss-mymachines.c index cc185b0c3..c87f436ae 100644 --- a/src/nss-mymachines/nss-mymachines.c +++ b/src/nss-mymachines/nss-mymachines.c @@ -79,7 +79,7 @@ enum nss_status _nss_mymachines_gethostbyname4_r( struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL; _cleanup_bus_message_unref_ sd_bus_message* reply = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; _cleanup_free_ int *ifindices = NULL; _cleanup_free_ char *class = NULL; size_t l, ms, idx; @@ -228,7 +228,7 @@ enum nss_status _nss_mymachines_gethostbyname3_r( char **canonp) { _cleanup_bus_message_unref_ sd_bus_message* reply = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; _cleanup_free_ char *class = NULL; unsigned c = 0, i = 0; char *r_name, *r_aliases, *r_addr, *r_addr_list; diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c index 555800c10..ccbc7317f 100644 --- a/src/nss-resolve/nss-resolve.c +++ b/src/nss-resolve/nss-resolve.c @@ -123,7 +123,7 @@ enum nss_status _nss_resolve_gethostbyname4_r( _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; const char *canonical = NULL; size_t l, ms, idx; char *r_name; @@ -306,7 +306,7 @@ enum nss_status _nss_resolve_gethostbyname3_r( _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; char *r_name, *r_aliases, *r_addr, *r_addr_list; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; size_t l, idx, ms, alen; const char *canonical; int c, r, i = 0; @@ -517,7 +517,7 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; char *r_name, *r_aliases, *r_addr, *r_addr_list; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; unsigned c = 0, i = 0; size_t ms = 0, idx; const char *n; diff --git a/src/resolve-host/resolve-host.c b/src/resolve-host/resolve-host.c index 43c8a229e..3641923e1 100644 --- a/src/resolve-host/resolve-host.c +++ b/src/resolve-host/resolve-host.c @@ -529,7 +529,7 @@ static int parse_argv(int argc, char *argv[]) { } int main(int argc, char **argv) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; log_parse_environment(); diff --git a/src/run/run.c b/src/run/run.c index f8f0ea283..e3b62939c 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -562,7 +562,7 @@ static int start_transient_scope( int main(int argc, char* argv[]) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; _cleanup_free_ char *description = NULL, *command = NULL; int r; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index daf7e31de..540b4a64c 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -617,7 +617,7 @@ static int get_unit_list_recursive( return r; STRV_FOREACH(i, machines) { - _cleanup_bus_unref_ sd_bus *container = NULL; + _cleanup_bus_close_unref_ sd_bus *container = NULL; int k; r = sd_bus_open_system_container(&container, *i); @@ -1688,7 +1688,7 @@ static int compare_machine_info(const void *a, const void *b) { } static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { - _cleanup_bus_unref_ sd_bus *container = NULL; + _cleanup_bus_close_unref_ sd_bus *container = NULL; int r; assert(mi); @@ -6828,7 +6828,7 @@ static int runlevel_main(void) { } int main(int argc, char*argv[]) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; setlocale(LC_ALL, ""); diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 36c3f3cfc..0ba32d31c 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -545,7 +545,7 @@ static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) { } int main(int argc, char *argv[]) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; setlocale(LC_ALL, ""); diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 936a902be..791e2b436 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -641,7 +641,7 @@ static const sd_bus_vtable timedate_vtable[] = { }; static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; assert(c); @@ -681,7 +681,7 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { int main(int argc, char *argv[]) { Context context = {}; _cleanup_event_unref_ sd_event *event = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; log_set_target(LOG_TARGET_AUTO); -- 2.30.2