2018-09-14 23:29:46 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* 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 <config.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-01 22:09:19 +01:00
|
|
|
#include <signal.h>
|
2018-09-14 23:29:46 +02:00
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "wired/dbus.h"
|
|
|
|
|
2018-09-15 21:40:32 +02:00
|
|
|
static struct l_dbus *dbus = NULL;
|
|
|
|
|
2018-09-16 20:48:32 +02:00
|
|
|
struct l_dbus *dbus_app_get(void)
|
2018-09-16 00:45:59 +02:00
|
|
|
{
|
|
|
|
return dbus;
|
|
|
|
}
|
|
|
|
|
2018-09-15 21:40:32 +02:00
|
|
|
struct dbus_info {
|
2018-09-16 20:48:32 +02:00
|
|
|
const struct dbus_app *app;
|
2018-09-15 21:40:32 +02:00
|
|
|
void *user_data;
|
|
|
|
};
|
2018-09-14 23:29:46 +02:00
|
|
|
|
2018-09-16 20:48:32 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 23:29:46 +02:00
|
|
|
static void request_name_callback(struct l_dbus *dbus, bool success,
|
|
|
|
bool queued, void *user_data)
|
|
|
|
{
|
2018-09-15 21:40:32 +02:00
|
|
|
struct dbus_info *info = user_data;
|
|
|
|
|
2018-09-14 23:29:46 +02:00
|
|
|
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");
|
2018-09-15 21:40:32 +02:00
|
|
|
|
2018-09-16 20:48:32 +02:00
|
|
|
if (info->app->ready)
|
|
|
|
info->app->ready(dbus, info->user_data);
|
2018-09-14 23:29:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dbus_ready(void *user_data)
|
|
|
|
{
|
2018-09-15 21:40:32 +02:00
|
|
|
struct dbus_info *info = user_data;
|
|
|
|
|
2018-09-16 20:48:32 +02:00
|
|
|
if (info->app->name) {
|
|
|
|
l_dbus_name_acquire(dbus, info->app->name, false, false, true,
|
2018-09-15 21:40:32 +02:00
|
|
|
request_name_callback, info);
|
2018-09-16 20:48:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->app->ready)
|
|
|
|
info->app->ready(dbus, info->user_data);
|
2018-09-14 23:29:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dbus_disconnected(void *user_data)
|
|
|
|
{
|
2018-09-16 20:48:32 +02:00
|
|
|
struct dbus_info *info = user_data;
|
|
|
|
|
|
|
|
l_info("D-Bus disconnected");
|
|
|
|
dbus_shutdown(info);
|
2018-09-14 23:29:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 21:40:32 +02:00
|
|
|
static void dbus_signal_handler(uint32_t signo, void *user_data)
|
|
|
|
{
|
2018-09-16 20:48:32 +02:00
|
|
|
struct dbus_info *info = user_data;
|
|
|
|
|
2018-09-15 21:40:32 +02:00
|
|
|
switch (signo) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
2018-09-16 20:48:32 +02:00
|
|
|
l_info("Termination signal");
|
|
|
|
dbus_shutdown(info);
|
2018-09-15 21:40:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 20:48:32 +02:00
|
|
|
int dbus_app_run(const struct dbus_app *app, void *user_data,
|
|
|
|
dbus_app_destroy_func_t destroy)
|
2018-09-14 23:29:46 +02:00
|
|
|
{
|
2018-09-15 21:40:32 +02:00
|
|
|
struct dbus_info *info;
|
|
|
|
int exit_status;
|
|
|
|
|
2018-09-16 20:48:32 +02:00
|
|
|
if (dbus || !app)
|
2018-09-15 21:40:32 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
if (!l_main_init())
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2018-09-16 20:48:32 +02:00
|
|
|
dbus = l_dbus_new_default(app->bus);
|
2018-09-14 23:29:46 +02:00
|
|
|
if (!dbus) {
|
|
|
|
l_error("Failed to initialize D-Bus");
|
2018-09-15 21:40:32 +02:00
|
|
|
return EXIT_FAILURE;
|
2018-09-14 23:29:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 21:40:32 +02:00
|
|
|
info = l_new(struct dbus_info, 1);
|
2018-09-16 20:48:32 +02:00
|
|
|
info->app = app;
|
2018-09-15 21:40:32 +02:00
|
|
|
info->user_data = user_data;
|
2018-09-14 23:29:46 +02:00
|
|
|
|
2018-09-15 21:40:32 +02:00
|
|
|
l_dbus_set_ready_handler(dbus, dbus_ready, info, NULL);
|
|
|
|
l_dbus_set_disconnect_handler(dbus, dbus_disconnected, info, NULL);
|
|
|
|
|
|
|
|
exit_status = l_main_run_with_signal(dbus_signal_handler, info);
|
|
|
|
|
2018-09-14 23:29:46 +02:00
|
|
|
l_dbus_destroy(dbus);
|
|
|
|
dbus = NULL;
|
2018-09-15 21:40:32 +02:00
|
|
|
|
2018-10-16 17:59:53 +02:00
|
|
|
l_main_exit();
|
|
|
|
|
2018-09-15 21:40:32 +02:00
|
|
|
if (destroy)
|
|
|
|
destroy(info->user_data);
|
|
|
|
|
|
|
|
l_free(info);
|
|
|
|
|
|
|
|
return exit_status;
|
2018-09-14 23:29:46 +02:00
|
|
|
}
|