eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

dbus.c (3274B)


      1 /*
      2  *
      3  *  Ethernet daemon for Linux
      4  *
      5  *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <signal.h>
     28 #include <ell/ell.h>
     29 
     30 #include "wired/dbus.h"
     31 
     32 static struct l_dbus *dbus = NULL;
     33 
     34 struct l_dbus *dbus_app_get(void)
     35 {
     36 	return dbus;
     37 }
     38 
     39 struct dbus_info {
     40 	const struct dbus_app *app;
     41 	void *user_data;
     42 };
     43 
     44 void dbus_app_shutdown_complete(void)
     45 {
     46 	l_main_quit();
     47 }
     48 
     49 static void dbus_shutdown(struct dbus_info *info)
     50 {
     51 	static bool terminated = false;
     52 
     53 	if (!terminated) {
     54 		terminated = true;
     55 
     56 		if (info->app->shutdown)
     57 			info->app->shutdown(dbus, info->user_data);
     58 		else
     59 			l_main_quit();
     60 	}
     61 }
     62 
     63 static void request_name_callback(struct l_dbus *dbus, bool success,
     64 						bool queued, void *user_data)
     65 {
     66 	struct dbus_info *info = user_data;
     67 
     68 	if (!success) {
     69 		l_error("Failed to request D-Bus service Name");
     70 		l_main_quit();
     71 		return;
     72 	}
     73 
     74 	if (!l_dbus_object_manager_enable(dbus, "/"))
     75 		l_warn("Unable to register ObjectManager interface");
     76 
     77 	if (info->app->ready)
     78 		info->app->ready(dbus, info->user_data);
     79 }
     80 
     81 static void dbus_ready(void *user_data)
     82 {
     83 	struct dbus_info *info = user_data;
     84 
     85 	if (info->app->name) {
     86 		l_dbus_name_acquire(dbus, info->app->name, false, false, true,
     87 						request_name_callback, info);
     88 		return;
     89 	}
     90 
     91 	if (info->app->ready)
     92 		info->app->ready(dbus, info->user_data);
     93 }
     94 
     95 static void dbus_disconnected(void *user_data)
     96 {
     97 	struct dbus_info *info = user_data;
     98 
     99 	l_info("D-Bus disconnected");
    100 	dbus_shutdown(info);
    101 }
    102 
    103 static void dbus_signal_handler(uint32_t signo, void *user_data)
    104 {
    105 	struct dbus_info *info = user_data;
    106 
    107 	switch (signo) {
    108 	case SIGINT:
    109 	case SIGTERM:
    110 		l_info("Termination signal");
    111 		dbus_shutdown(info);
    112 		break;
    113 	}
    114 }
    115 
    116 int dbus_app_run(const struct dbus_app *app, void *user_data,
    117 					dbus_app_destroy_func_t destroy)
    118 {
    119 	struct dbus_info *info;
    120 	int exit_status;
    121 
    122 	if (dbus || !app)
    123 		return EXIT_FAILURE;
    124 
    125 	if (!l_main_init())
    126 		return EXIT_FAILURE;
    127 
    128 	dbus = l_dbus_new_default(app->bus);
    129 	if (!dbus) {
    130 		l_error("Failed to initialize D-Bus");
    131 		return EXIT_FAILURE;
    132 	}
    133 
    134 	info = l_new(struct dbus_info, 1);
    135 	info->app = app;
    136 	info->user_data = user_data;
    137 
    138 	l_dbus_set_ready_handler(dbus, dbus_ready, info, NULL);
    139 	l_dbus_set_disconnect_handler(dbus, dbus_disconnected, info, NULL);
    140 
    141 	exit_status = l_main_run_with_signal(dbus_signal_handler, info);
    142 
    143 	l_dbus_destroy(dbus);
    144 	dbus = NULL;
    145 
    146 	l_main_exit();
    147 
    148 	if (destroy)
    149 		destroy(info->user_data);
    150 
    151 	l_free(info);
    152 
    153 	return exit_status;
    154 }