File: dbus.c

package info (click to toggle)
iwd 3.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,164 kB
  • sloc: ansic: 140,705; sh: 5,514; makefile: 714
file content (154 lines) | stat: -rw-r--r-- 3,274 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 *
 *  Ethernet daemon for Linux
 *
 *  Copyright (C) 2017-2019  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

#include <signal.h>
#include <ell/ell.h>

#include "wired/dbus.h"

static struct l_dbus *dbus = NULL;

struct l_dbus *dbus_app_get(void)
{
	return dbus;
}

struct dbus_info {
	const struct dbus_app *app;
	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,
						bool queued, void *user_data)
{
	struct dbus_info *info = 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");

	if (info->app->ready)
		info->app->ready(dbus, info->user_data);
}

static void dbus_ready(void *user_data)
{
	struct dbus_info *info = user_data;

	if (info->app->name) {
		l_dbus_name_acquire(dbus, info->app->name, false, false, true,
						request_name_callback, info);
		return;
	}

	if (info->app->ready)
		info->app->ready(dbus, info->user_data);
}

static void dbus_disconnected(void *user_data)
{
	struct dbus_info *info = user_data;

	l_info("D-Bus disconnected");
	dbus_shutdown(info);
}

static void dbus_signal_handler(uint32_t signo, void *user_data)
{
	struct dbus_info *info = user_data;

	switch (signo) {
	case SIGINT:
	case SIGTERM:
		l_info("Termination signal");
		dbus_shutdown(info);
		break;
	}
}

int dbus_app_run(const struct dbus_app *app, void *user_data,
					dbus_app_destroy_func_t destroy)
{
	struct dbus_info *info;
	int exit_status;

	if (dbus || !app)
		return EXIT_FAILURE;

	if (!l_main_init())
		return EXIT_FAILURE;

	dbus = l_dbus_new_default(app->bus);
	if (!dbus) {
		l_error("Failed to initialize D-Bus");
		return EXIT_FAILURE;
	}

	info = l_new(struct dbus_info, 1);
	info->app = app;
	info->user_data = user_data;

	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);

	l_dbus_destroy(dbus);
	dbus = NULL;

	l_main_exit();

	if (destroy)
		destroy(info->user_data);

	l_free(info);

	return exit_status;
}