bus: always explicitly close bus from main programs

Since b5eca3a205 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.
This commit is contained in:
Lennart Poettering 2014-08-04 16:22:09 +02:00
parent ef392da6c5
commit 249968612f
23 changed files with 42 additions and 41 deletions

View 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) {

View 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);

View 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();

View 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;
}

View 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);

View 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, "");

View 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);

View 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)

View 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();

View 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, "");

View 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);

View 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();

View 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, "");

View 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;

View 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, "");

View 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;

View 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;

View 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;

View 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();

View 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;

View 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, "");

View 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, "");

View 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);