chiark / gitweb /
bus: always explicitly close bus from main programs
authorLennart Poettering <lennart@poettering.net>
Mon, 4 Aug 2014 14:22:09 +0000 (16:22 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 4 Aug 2014 14:25:24 +0000 (16:25 +0200)
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.

23 files changed:
src/analyze/analyze.c
src/bus-proxyd/bus-proxyd.c
src/cgls/cgls.c
src/cgroups-agent/cgroups-agent.c
src/fsck/fsck.c
src/hostname/hostnamectl.c
src/hostname/hostnamed.c
src/libsystemd/sd-bus/bus-util.h
src/libsystemd/sd-bus/busctl.c
src/locale/localectl.c
src/locale/localed.c
src/login/inhibit.c
src/login/loginctl.c
src/login/pam_systemd.c
src/machine/machinectl.c
src/nspawn/nspawn.c
src/nss-mymachines/nss-mymachines.c
src/nss-resolve/nss-resolve.c
src/resolve-host/resolve-host.c
src/run/run.c
src/systemctl/systemctl.c
src/timedate/timedatectl.c
src/timedate/timedated.c

index d0bf01468a36b0f46d1aed412a51a428b0da23fa..fdbce7cda87a09fd7c88f73105adb7d971b8375d 100644 (file)
@@ -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) {
index e5713d6af95a173f757888964a70e7f8ca1a532f..4f24e331ecd9d8d0ea6c3471ee54201d6bea2759 100644 (file)
@@ -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);
index 6e9bd232a9d088c77b8a5c1648e1c4a34a785fa4..7070503d68c1f3c6aeb92fe354806181aefcda97 100644 (file)
@@ -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();
index d1d843bda71e46bd117f937890e28aa91206fc14..e581fbc9b6afb64cd2cf6c4fc2a103369b6cfa1e 100644 (file)
@@ -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;
 }
index e8596d90f805df3d35218dc9735cf484016c8f4c..dfe97bc260a031885b48202772563edcdf86ad80 100644 (file)
@@ -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);
index 69da54f1f3233e8e0c1e446005f203badd82b190..f8e1a811f8ccdce639145921c31e91c69e267980 100644 (file)
@@ -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, "");
index 74d9dab073d25852fc676c295082648bca9f968e..4acdb72b123caa6918a7dc2b0bb0eb39fd39424f 100644 (file)
@@ -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);
index 0a3d5dd567637330fce5f6470e831a228d727a79..af50553926dcec0626e9d121ae9242a8566ba268 100644 (file)
@@ -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)
index b6839c13e73f84321c1b3568d956fe901428f4fb..af71804410d20b53e3dd9a7a132dd3182ad850bd 100644 (file)
@@ -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();
index 69d50076aad0f75028b11761016355f2d533d436..2bb0d3b6be4e570dcb49edf3fb0de051c14f8940 100644 (file)
@@ -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, "");
index bce99b8cbaae0817eabf668d4db880af06f123c3..3cfc2ea4d3029d0b476581c18eb87e2b6cff60db 100644 (file)
@@ -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);
index 02b6b257511197b3506983d58a27229b649bd427..d5ea1d913cd91b404e1ace06ced17004ce93fb7a 100644 (file)
@@ -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();
index 1773276b95c85ac7d3c0829612b92dc6466cac6c..fcdf6275e9cd6fef21cabaece41053fc452eebe9 100644 (file)
@@ -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, "");
index f522d6ec4f20fa83b8b1ecefa22bc8cbb0019174..0c71177deb96c48354e495d13d3f2f4559edf6db 100644 (file)
@@ -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;
index 02d5d301c002342eab588510f7ae5f5c32e87773..7491d69c8b2d4f170b18a330231e4c5f44ea501f 100644 (file)
@@ -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, "");
index 76e86b7e05ea1593c2f9be3374840df574580adb..ae09f936f56b1f0cd7b62b7062aa26ee277ed445 100644 (file)
@@ -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;
 
index cc185b0c3df96f5d585b35d0466ea6c7135bbdd5..c87f436aea90c3de536caaedf4748f3149fa3a50 100644 (file)
@@ -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;
index 555800c10ad70956fe13ebb451b3f889dc2bfa77..ccbc7317f647593a16a4640df9920c5f1f79d73b 100644 (file)
@@ -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;
index 43c8a229eb09deddd57709ff39ee9565693c1cc6..3641923e1ca99dc1b84fc4931f835007d6e418b8 100644 (file)
@@ -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();
index f8f0ea2832851f8f0fdb48d105eab70f1010e58e..e3b62939c7328e0b6f68f8cbeaea6b6994205bfe 100644 (file)
@@ -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;
 
index daf7e31de9cf13c99794ec9c1965bf29921a919e..540b4a64c1f5077f7c5364fb2bb37f5419ec4d97 100644 (file)
@@ -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, "");
index 36c3f3cfcd083d913d3dfe828de0a0df8188be84..0ba32d31c4015e186598d6e3a518f2b5291a0c5f 100644 (file)
@@ -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, "");
index 936a902be693ecdc63dcb7d6ce1a9104e0c85e2f..791e2b436ff1385bf1da097c1b74ddd6f0db855b 100644 (file)
@@ -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);