From b181604c184c856e4249012b83924f3d41177a20 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Tue, 11 Sep 2018 20:06:00 -0500 Subject: [PATCH] main: Don't init nl80211 until dbus name is owned --- src/dbus.c | 52 +++----------------------------- src/dbus.h | 4 +-- src/iwd.h | 2 -- src/main.c | 87 ++++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 71 insertions(+), 74 deletions(-) diff --git a/src/dbus.c b/src/dbus.c index 554c92b3..96e2ea24 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -39,13 +39,6 @@ struct l_dbus *g_dbus = 0; -static void do_debug(const char *str, void *user_data) -{ - const char *prefix = user_data; - - l_info("%s%s", prefix, str); -} - const char *dbus_iftype_to_string(uint32_t iftype) { switch (iftype) { @@ -204,58 +197,21 @@ void dbus_pending_reply(struct l_dbus_message **msg, *msg = NULL; } -static void request_name_callback(struct l_dbus *dbus, bool success, - bool queued, void *user_data) -{ - if (!success) - l_error("Name request failed"); -} - -static void ready_callback(void *user_data) -{ - l_dbus_name_acquire(g_dbus, "net.connman.iwd", false, false, true, - request_name_callback, NULL); - - if (!l_dbus_object_manager_enable(g_dbus)) - l_info("Unable to register the ObjectManager"); - - agent_init(g_dbus); -} - -static void disconnect_callback(void *user_data) -{ - l_info("D-Bus disconnected, quitting..."); - iwd_shutdown(); -} - struct l_dbus *dbus_get_bus(void) { return g_dbus; } -bool dbus_init(bool enable_debug) +bool dbus_init(struct l_dbus *dbus) { - g_dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS); - if (!g_dbus) - return false; - - if (enable_debug) - l_dbus_set_debug(g_dbus, do_debug, "[DBUS] ", NULL); - - l_dbus_set_ready_handler(g_dbus, ready_callback, g_dbus, NULL); - l_dbus_set_disconnect_handler(g_dbus, disconnect_callback, NULL, NULL); - - return true; + g_dbus = dbus; + return agent_init(dbus); } -bool dbus_exit(void) +void dbus_exit(void) { agent_exit(g_dbus); - - l_dbus_destroy(g_dbus); g_dbus = NULL; - - return true; } void dbus_shutdown(void) diff --git a/src/dbus.h b/src/dbus.h index 22afd762..3af15460 100644 --- a/src/dbus.h +++ b/src/dbus.h @@ -69,6 +69,6 @@ struct l_dbus_message *dbus_error_not_hidden(struct l_dbus_message *msg); struct l_dbus_message *dbus_error_from_errno(int err, struct l_dbus_message *msg); -bool dbus_init(bool enable_debug); -bool dbus_exit(void); +bool dbus_init(struct l_dbus *dbus); +void dbus_exit(void); void dbus_shutdown(void); diff --git a/src/iwd.h b/src/iwd.h index 788c0e14..a0178d2e 100644 --- a/src/iwd.h +++ b/src/iwd.h @@ -26,8 +26,6 @@ struct l_genl_family; const struct l_settings *iwd_get_config(void); -void iwd_shutdown(void); - bool netdev_init(const char *whitelist, const char *blacklist); void netdev_exit(void); void netdev_set_nl80211(struct l_genl_family *nl80211); diff --git a/src/main.c b/src/main.c index 1ee33d0a..92240318 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,8 @@ #include "src/backtrace.h" +struct l_genl *genl; +struct l_genl_family *nl80211; static struct l_settings *iwd_config; static struct l_timeout *timeout; static const char *interfaces; @@ -64,13 +66,18 @@ static void main_loop_quit(struct l_timeout *timeout, void *user_data) l_main_quit(); } -void iwd_shutdown(void) +static void iwd_shutdown(void) { if (terminating) return; terminating = true; + if (!nl80211) { + l_main_quit(); + return; + } + dbus_shutdown(); netdev_shutdown(); @@ -163,6 +170,54 @@ static void nl80211_vanished(void *user_data) wiphy_exit(); } +static void request_name_callback(struct l_dbus *dbus, bool success, + bool queued, void *user_data) +{ + if (!success) { + l_error("Name request failed"); + goto fail_exit; + } + + if (!l_dbus_object_manager_enable(dbus)) + l_warn("Unable to register the ObjectManager"); + + genl = l_genl_new_default(); + if (!genl) { + l_error("Failed to open generic netlink socket"); + goto fail_exit; + } + + if (getenv("IWD_GENL_DEBUG")) + l_genl_set_debug(genl, do_debug, "[GENL] ", NULL); + + nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME); + if (!nl80211) { + l_error("Failed to open nl80211 interface"); + goto fail_exit; + } + + l_genl_family_set_watches(nl80211, nl80211_appeared, nl80211_vanished, + nl80211, NULL); + return; + +fail_exit: + l_main_quit(); +} + +static void dbus_ready(void *user_data) +{ + struct l_dbus *dbus = user_data; + + l_dbus_name_acquire(dbus, "net.connman.iwd", false, false, true, + request_name_callback, NULL); +} + +static void dbus_disconnected(void *user_data) +{ + l_info("D-Bus disconnected, quitting..."); + iwd_shutdown(); +} + static void print_koption(const void *key, void *value, void *user_data) { l_info("\t%s", (const char *) key); @@ -297,8 +352,7 @@ int main(int argc, char *argv[]) struct l_signal *signal; sigset_t mask; int exit_status; - struct l_genl *genl; - struct l_genl_family *nl80211; + struct l_dbus *dbus; char *config_path; uint32_t eap_mtu; @@ -400,28 +454,18 @@ int main(int argc, char *argv[]) exit_status = EXIT_FAILURE; - if (!dbus_init(enable_dbus_debug)) { + dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS); + if (!dbus) { l_error("Failed to initialize D-Bus"); goto fail_dbus; } - genl = l_genl_new_default(); - if (!genl) { - l_error("Failed to open generic netlink socket"); - goto fail_genl; - } + if (enable_dbus_debug) + l_dbus_set_debug(dbus, do_debug, "[DBUS] ", NULL); - if (getenv("IWD_GENL_DEBUG")) - l_genl_set_debug(genl, do_debug, "[GENL] ", NULL); - - nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME); - if (!nl80211) { - l_error("Failed to open nl80211 interface"); - goto fail_nl80211; - } - - l_genl_family_set_watches(nl80211, nl80211_appeared, nl80211_vanished, - nl80211, NULL); + l_dbus_set_ready_handler(dbus, dbus_ready, dbus, NULL); + l_dbus_set_disconnect_handler(dbus, dbus_disconnected, NULL, NULL); + dbus_init(dbus); eap_init(eap_mtu); eapol_init(); @@ -460,10 +504,9 @@ fail_netdev: eap_exit(); l_genl_family_unref(nl80211); -fail_nl80211: l_genl_unref(genl); -fail_genl: dbus_exit(); + l_dbus_destroy(dbus); fail_dbus: l_settings_free(iwd_config);