3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-22 23:09:34 +01:00

wired: Add concept of D-Bus application and async shutdown handling

This commit is contained in:
Marcel Holtmann 2018-09-16 20:48:32 +02:00
parent 8bc97d7caa
commit 05ccd73818
3 changed files with 65 additions and 34 deletions

View File

@ -30,17 +30,35 @@
static struct l_dbus *dbus = NULL; static struct l_dbus *dbus = NULL;
struct l_dbus *dbus_get(void) struct l_dbus *dbus_app_get(void)
{ {
return dbus; return dbus;
} }
struct dbus_info { struct dbus_info {
char *name; const struct dbus_app *app;
dbus_ready_func_t ready_func;
void *user_data; void *user_data;
}; };
void dbus_app_shutdown_complete(void)
{
l_main_quit();
}
static void dbus_shutdown(struct dbus_info *info)
{
static bool terminated = false;
if (!terminated) {
terminated = true;
if (info->app->shutdown)
info->app->shutdown(dbus, info->user_data);
else
l_main_quit();
}
}
static void request_name_callback(struct l_dbus *dbus, bool success, static void request_name_callback(struct l_dbus *dbus, bool success,
bool queued, void *user_data) bool queued, void *user_data)
{ {
@ -55,59 +73,65 @@ static void request_name_callback(struct l_dbus *dbus, bool success,
if (!l_dbus_object_manager_enable(dbus)) if (!l_dbus_object_manager_enable(dbus))
l_warn("Unable to register ObjectManager interface"); l_warn("Unable to register ObjectManager interface");
if (info->ready_func) if (info->app->ready)
info->ready_func(dbus, info->user_data); info->app->ready(dbus, info->user_data);
} }
static void dbus_ready(void *user_data) static void dbus_ready(void *user_data)
{ {
struct dbus_info *info = user_data; struct dbus_info *info = user_data;
l_dbus_name_acquire(dbus, info->name, false, false, true, if (info->app->name) {
l_dbus_name_acquire(dbus, info->app->name, false, false, true,
request_name_callback, info); request_name_callback, info);
return;
}
if (info->app->ready)
info->app->ready(dbus, info->user_data);
} }
static void dbus_disconnected(void *user_data) static void dbus_disconnected(void *user_data)
{ {
l_info("D-Bus disconnected, quitting..."); struct dbus_info *info = user_data;
l_main_quit();
l_info("D-Bus disconnected");
dbus_shutdown(info);
} }
static void dbus_signal_handler(uint32_t signo, void *user_data) static void dbus_signal_handler(uint32_t signo, void *user_data)
{ {
struct dbus_info *info = user_data;
switch (signo) { switch (signo) {
case SIGINT: case SIGINT:
case SIGTERM: case SIGTERM:
l_info("Terminate"); l_info("Termination signal");
l_main_quit(); dbus_shutdown(info);
break; break;
} }
} }
int dbus_run(enum l_dbus_bus bus, const char *name, int dbus_app_run(const struct dbus_app *app, void *user_data,
dbus_ready_func_t ready_func, dbus_app_destroy_func_t destroy)
dbus_shutdown_func_t shutdown_func,
void *user_data,
dbus_destroy_func_t destroy)
{ {
struct dbus_info *info; struct dbus_info *info;
int exit_status; int exit_status;
if (dbus) if (dbus || !app)
return EXIT_FAILURE; return EXIT_FAILURE;
if (!l_main_init()) if (!l_main_init())
return EXIT_FAILURE; return EXIT_FAILURE;
dbus = l_dbus_new_default(bus); dbus = l_dbus_new_default(app->bus);
if (!dbus) { if (!dbus) {
l_error("Failed to initialize D-Bus"); l_error("Failed to initialize D-Bus");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
info = l_new(struct dbus_info, 1); info = l_new(struct dbus_info, 1);
info->name = l_strdup(name); info->app = app;
info->ready_func = ready_func;
info->user_data = user_data; info->user_data = user_data;
l_dbus_set_ready_handler(dbus, dbus_ready, info, NULL); l_dbus_set_ready_handler(dbus, dbus_ready, info, NULL);
@ -115,16 +139,12 @@ int dbus_run(enum l_dbus_bus bus, const char *name,
exit_status = l_main_run_with_signal(dbus_signal_handler, info); exit_status = l_main_run_with_signal(dbus_signal_handler, info);
if (shutdown_func)
shutdown_func(dbus, info->user_data);
l_dbus_destroy(dbus); l_dbus_destroy(dbus);
dbus = NULL; dbus = NULL;
if (destroy) if (destroy)
destroy(info->user_data); destroy(info->user_data);
l_free(info->name);
l_free(info); l_free(info);
return exit_status; return exit_status;

View File

@ -23,15 +23,18 @@
enum l_dbus_bus; enum l_dbus_bus;
struct l_dbus; struct l_dbus;
struct l_dbus *dbus_get(void); struct l_dbus *dbus_app_get(void);
typedef void (*dbus_ready_func_t) (struct l_dbus *dbus, void *user_data); typedef void (*dbus_app_destroy_func_t) (void *user_data);
typedef void (*dbus_shutdown_func_t) (struct l_dbus *dbus, void *user_data);
typedef void (*dbus_destroy_func_t) (void *user_data); struct dbus_app {
enum l_dbus_bus bus;
const char *name;
void (*ready) (struct l_dbus *dbus, void *user_data);
void (*shutdown) (struct l_dbus *dbus, void *user_data);
};
int dbus_run(enum l_dbus_bus bus, const char *name, void dbus_app_shutdown_complete(void);
dbus_ready_func_t ready_func,
dbus_shutdown_func_t shutdown_func, int dbus_app_run(const struct dbus_app *app, void *user_data,
void *user_data, dbus_app_destroy_func_t destroy);
dbus_destroy_func_t destroy);

View File

@ -58,8 +58,17 @@ static void dbus_shutdown(struct l_dbus *dbus, void *user_data)
ethdev_exit(); ethdev_exit();
network_exit(); network_exit();
eap_exit(); eap_exit();
dbus_app_shutdown_complete();
} }
static const struct dbus_app app = {
.bus = L_DBUS_SYSTEM_BUS,
.name = "net.connman.ead",
.ready = dbus_ready,
.shutdown = dbus_shutdown,
};
static void usage(void) static void usage(void)
{ {
printf("ead - Authentication daemon\n" printf("ead - Authentication daemon\n"
@ -131,6 +140,5 @@ int main(int argc, char *argv[])
l_info("Authentication daemon version %s", VERSION); l_info("Authentication daemon version %s", VERSION);
return dbus_run(L_DBUS_SYSTEM_BUS, "net.connman.ead", return dbus_app_run(&app, &opts, NULL);
dbus_ready, dbus_shutdown, &opts, NULL);
} }