Based on 249968612f16a71df909d6e73785c18a9ff36a65
with src/core/dbus.c as well and corrected systemctl
error messages for killing a unit.

---
 src/analyze/analyze.c             |    2 +-
 src/bus-proxyd/bus-proxyd.c       |    4 +---
 src/cgroups-agent/cgroups-agent.c |    4 +---
 src/core/dbus.c                   |    2 +-
 src/fsck/fsck.c                   |    2 +-
 src/hostname/hostnamectl.c        |    2 +-
 src/hostname/hostnamed.c          |    4 ++--
 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-module.c            |    4 ++--
 src/machine/machinectl.c          |    4 ++--
 src/nspawn/nspawn.c               |    4 ++--
 src/run/run.c                     |    2 +-
 src/systemctl/systemctl.c         |    2 +-
 src/timedate/timedatectl.c        |    2 +-
 src/timedate/timedated.c          |    4 ++--
 20 files changed, 34 insertions(+), 29 deletions(-)

--- src/analyze/analyze.c
+++ src/analyze/analyze.c	2014-10-21 11:13:56.113045992 +0000
@@ -1264,7 +1264,7 @@ static int parse_argv(int argc, char *ar
 }
 
 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, "");
--- src/bus-proxyd/bus-proxyd.c
+++ src/bus-proxyd/bus-proxyd.c	2014-10-21 11:16:16.303837968 +0000
@@ -427,7 +427,7 @@ static int patch_sender(sd_bus *a, sd_bu
 
 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;
@@ -777,8 +777,6 @@ int main(int argc, char *argv[]) {
         r = 0;
 
 finish:
-        sd_bus_flush(a);
-        sd_bus_flush(b);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
--- src/cgroups-agent/cgroups-agent.c
+++ src/cgroups-agent/cgroups-agent.c	2014-10-21 11:17:37.400197469 +0000
@@ -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;
 }
--- src/core/dbus.c
+++ src/core/dbus.c	2014-10-17 09:52:47.000000000 +0000
@@ -639,7 +639,7 @@ static int bus_setup_disconnected_match(
 }
 
 static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
-        _cleanup_bus_unref_ sd_bus *bus = NULL;
+        _cleanup_bus_close_unref_ sd_bus *bus = NULL;
         _cleanup_close_ int nfd = -1;
         Manager *m = userdata;
         sd_id128_t id;
--- src/fsck/fsck.c
+++ src/fsck/fsck.c	2014-10-21 11:18:06.003838037 +0000
@@ -45,7 +45,7 @@ static bool arg_show_progress = false;
 
 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);
--- src/hostname/hostnamectl.c
+++ src/hostname/hostnamectl.c	2014-10-21 11:18:19.915858156 +0000
@@ -468,7 +468,7 @@ static int hostnamectl_main(sd_bus *bus,
 }
 
 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, "");
--- src/hostname/hostnamed.c
+++ src/hostname/hostnamed.c	2014-10-21 11:24:07.737447673 +0000
@@ -566,7 +566,7 @@ static const sd_bus_vtable hostname_vtab
 };
 
 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);
@@ -607,7 +607,7 @@ 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);
--- src/libsystemd/sd-bus/bus-util.h
+++ src/libsystemd/sd-bus/bus-util.h	2014-10-17 09:51:34.000000000 +0000
@@ -137,11 +137,20 @@ 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_message*, sd_bus_message_unref);
 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_creds*, sd_bus_creds_unref);
 
 #define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp)
+#define _cleanup_bus_close_unref_ _cleanup_(sd_bus_close_unrefp)
 #define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp)
 #define _cleanup_bus_creds_unref_ _cleanup_(sd_bus_creds_unrefp)
 #define _cleanup_bus_error_free_ _cleanup_(sd_bus_error_free)
--- src/libsystemd/sd-bus/busctl.c
+++ src/libsystemd/sd-bus/busctl.c	2014-10-21 11:19:01.472337887 +0000
@@ -484,7 +484,7 @@ static int busctl_main(sd_bus *bus, int
 }
 
 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();
--- src/locale/localectl.c
+++ src/locale/localectl.c	2014-10-21 11:19:13.811947994 +0000
@@ -796,7 +796,7 @@ static int localectl_main(sd_bus *bus, i
 }
 
 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, "");
--- src/locale/localed.c
+++ src/locale/localed.c	2014-10-21 11:19:28.415837424 +0000
@@ -1076,7 +1076,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);
@@ -1116,7 +1116,7 @@ static int connect_bus(Context *c, sd_ev
 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);
--- src/login/inhibit.c
+++ src/login/inhibit.c	2014-10-21 11:19:44.470844885 +0000
@@ -221,7 +221,7 @@ static int parse_argv(int argc, char *ar
 
 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();
--- src/login/loginctl.c
+++ src/login/loginctl.c	2014-10-21 11:20:16.695837857 +0000
@@ -1298,7 +1298,7 @@ static int loginctl_main(sd_bus *bus, in
 }
 
 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, "");
--- src/login/pam-module.c
+++ src/login/pam-module.c	2014-10-21 10:48:20.000000000 +0000
@@ -213,7 +213,7 @@ _public_ PAM_EXTERN int pam_sm_open_sess
                 *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;
@@ -517,7 +517,7 @@ _public_ PAM_EXTERN int pam_sm_close_ses
                 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;
--- src/machine/machinectl.c
+++ src/machine/machinectl.c	2014-10-21 11:20:45.747838104 +0000
@@ -550,7 +550,7 @@ static int openpt_in_namespace(pid_t pid
 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;
@@ -879,7 +879,7 @@ static int machinectl_main(sd_bus *bus,
 }
 
 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, "");
--- src/nspawn/nspawn.c
+++ src/nspawn/nspawn.c	2014-10-21 11:21:05.561523449 +0000
@@ -1163,7 +1163,7 @@ static int drop_capabilities(void) {
 
 static int register_machine(pid_t pid) {
         _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)
@@ -1285,7 +1285,7 @@ static int register_machine(pid_t pid) {
 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;
 
--- src/run/run.c
+++ src/run/run.c	2014-10-21 11:22:35.163838021 +0000
@@ -462,7 +462,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;
 
--- src/systemctl/systemctl.c
+++ src/systemctl/systemctl.c	2014-10-22 09:36:33.399838318 +0000
@@ -2601,7 +2601,7 @@ static int kill_unit(sd_bus *bus, char *
                                 "ssi", *names, arg_kill_who, arg_signal);
                 if (q < 0) {
                         log_error("Failed to kill unit %s: %s",
-                                  *names, bus_error_message(&error, r));
+                                  *names, bus_error_message(&error, q));
                         if (r == 0)
                                 r = q;
                 }
@@ -6436,7 +6436,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, "");
--- src/timedate/timedatectl.c
+++ src/timedate/timedatectl.c	2014-10-21 11:23:06.107837988 +0000
@@ -593,7 +593,7 @@ static int timedatectl_main(sd_bus *bus,
 }
 
 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, "");
--- src/timedate/timedated.c
+++ src/timedate/timedated.c	2014-10-21 11:23:26.047838675 +0000
@@ -779,7 +779,7 @@ static const sd_bus_vtable timedate_vtab
 };
 
 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);
@@ -825,7 +825,7 @@ int main(int argc, char *argv[]) {
         };
 
         _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);
