From 247b2ccc5cbb81c5c793b2f4756fd6fb5f1164ab Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 14 Sep 2018 23:29:46 +0200 Subject: [PATCH] wired: Move D-Bus setup into separate source file --- Makefile.am | 3 ++- wired/dbus.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ wired/dbus.h | 24 +++++++++++++++++ wired/main.c | 39 +++------------------------ 4 files changed, 105 insertions(+), 37 deletions(-) create mode 100644 wired/dbus.c create mode 100644 wired/dbus.h diff --git a/Makefile.am b/Makefile.am index dabf0745..224a0e0d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -197,7 +197,8 @@ if WIRED libexec_PROGRAMS += wired/ead wired_ead_SOURCES = wired/main.c wired/ethdev.h wired/ethdev.c \ - wired/network.h wired/network.c $(eap_sources) + wired/network.h wired/network.c \ + wired/dbus.h wired/dbus.c $(eap_sources) wired_ead_LDADD = ell/libell-internal.la wired_ead_DEPENDENCIES = ell/libell-internal.la diff --git a/wired/dbus.c b/wired/dbus.c new file mode 100644 index 00000000..1eeaf9c2 --- /dev/null +++ b/wired/dbus.c @@ -0,0 +1,76 @@ +/* + * + * Ethernet daemon for Linux + * + * Copyright (C) 2017-2018 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "wired/dbus.h" + +static struct l_dbus *dbus; + +static void request_name_callback(struct l_dbus *dbus, bool success, + bool queued, void *user_data) +{ + if (!success) { + l_error("Failed to request D-Bus service Name"); + l_main_quit(); + return; + } + + if (!l_dbus_object_manager_enable(dbus)) + l_warn("Unable to register ObjectManager interface"); +} + +static void dbus_ready(void *user_data) +{ + l_dbus_name_acquire(dbus, "net.connman.ead", false, false, true, + request_name_callback, NULL); +} + +static void dbus_disconnected(void *user_data) +{ + l_info("D-Bus disconnected, quitting..."); + l_main_quit(); +} + +bool dbus_init(void) +{ + dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS); + if (!dbus) { + l_error("Failed to initialize D-Bus"); + return false; + } + + l_dbus_set_ready_handler(dbus, dbus_ready, dbus, NULL); + l_dbus_set_disconnect_handler(dbus, dbus_disconnected, NULL, NULL); + + return true; +} + +void dbus_exit(void) +{ + l_dbus_destroy(dbus); + dbus = NULL; +} diff --git a/wired/dbus.h b/wired/dbus.h new file mode 100644 index 00000000..e11597d3 --- /dev/null +++ b/wired/dbus.h @@ -0,0 +1,24 @@ +/* + * + * Ethernet daemon for Linux + * + * Copyright (C) 2017-2018 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +bool dbus_init(void); +void dbus_exit(void); diff --git a/wired/main.c b/wired/main.c index ff68de0e..fcf08ba3 100644 --- a/wired/main.c +++ b/wired/main.c @@ -31,6 +31,7 @@ #include #include "src/eap.h" +#include "wired/dbus.h" #include "wired/ethdev.h" #include "wired/network.h" @@ -46,33 +47,6 @@ static void signal_handler(struct l_signal *signal, uint32_t signo, } } -static void request_name_callback(struct l_dbus *dbus, bool success, - bool queued, void *user_data) -{ - if (!success) { - l_error("Failed to request D-Bus service Name"); - l_main_quit(); - return; - } - - if (!l_dbus_object_manager_enable(dbus)) - l_warn("Unable to register ObjectManager interface"); -} - -static void dbus_ready(void *user_data) -{ - struct l_dbus *dbus = user_data; - - l_dbus_name_acquire(dbus, "net.connman.ead", false, false, true, - request_name_callback, NULL); -} - -static void dbus_disconnected(void *user_data) -{ - l_info("D-Bus disconnected, quitting..."); - l_main_quit(); -} - static void usage(void) { printf("ead - Authentication daemon\n" @@ -99,7 +73,6 @@ int main(int argc, char *argv[]) struct l_signal *signal; sigset_t mask; int exit_status; - struct l_dbus *dbus; const char *interfaces = NULL; const char *nointerfaces = NULL; const char *debugopt = NULL; @@ -160,14 +133,8 @@ int main(int argc, char *argv[]) exit_status = EXIT_FAILURE; - dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS); - if (!dbus) { - l_error("Failed to initialize D-Bus"); + if (!dbus_init()) goto done; - } - - l_dbus_set_ready_handler(dbus, dbus_ready, dbus, NULL); - l_dbus_set_disconnect_handler(dbus, dbus_disconnected, NULL, NULL); eap_init(0); network_init(); @@ -181,7 +148,7 @@ int main(int argc, char *argv[]) network_exit(); eap_exit(); - l_dbus_destroy(dbus); + dbus_exit(); done: l_signal_remove(signal);