-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpld.c
222 lines (174 loc) · 4.59 KB
/
jpld.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* SPDX-FileCopyrightText: Copyright © 2023 Nedko Arnaudov
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <cdbus/cdbus.h>
#include <cdbus/log.h>
#if HAVE_GITVERSION_H
#include "gitversion.h"
#endif
#include "loader.h"
bool g_quit;
const char * g_dbus_unique_name;
cdbus_object_path g_dbus_object;
extern const struct cdbus_interface_descriptor jpl_appman_cdbus_interface_org_ladish_ApplicationManager0;
#define SERVICE_NAME DBUS_NAME_BASE
#define DBUS_OBJECT_PATH DBUS_BASE_PATH
void
jpld_log(
void * ctx,
bool error,
const char * format,
...)
{
va_list ap;
FILE * stream;
//printf("jpl_log()\n");
stream = error ? stderr : stdout;
va_start(ap, format);
vfprintf(stream, format, ap);
va_end(ap);
fflush(stream);
}
void
jpl_cdbus_log(
unsigned int level,
const char * file,
unsigned int line,
const char * func,
const char * format,
...)
{
va_list ap;
FILE * stream;
//printf("jpl_cdbus_log()\n");
stream = stdout;
va_start(ap, format);
vfprintf(stream, format, ap);
va_end(ap);
fputc('\n', stream);
fflush(stream);
}
#define log_error(fmt, args...) jpld_log(NULL, true, fmt "\n", ## args)
#define log_info( fmt, args...) jpld_log(NULL, false, fmt "\n", ## args)
#define log_debug(fmt, args...) jpld_log(NULL, false, fmt "\n", ## args)
static bool connect_dbus(void)
{
int ret;
dbus_error_init(&cdbus_g_dbus_error);
cdbus_g_dbus_connection = dbus_bus_get(DBUS_BUS_SESSION, &cdbus_g_dbus_error);
if (dbus_error_is_set(&cdbus_g_dbus_error))
{
log_error("Failed to get bus: %s", cdbus_g_dbus_error.message);
dbus_error_free(&cdbus_g_dbus_error);
goto fail;
}
g_dbus_unique_name = dbus_bus_get_unique_name(cdbus_g_dbus_connection);
if (g_dbus_unique_name == NULL)
{
log_error("Failed to read unique bus name");
goto unref_connection;
}
log_info("Connected to local session bus, unique name is \"%s\"", g_dbus_unique_name);
ret = dbus_bus_request_name(cdbus_g_dbus_connection, SERVICE_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &cdbus_g_dbus_error);
if (ret == -1)
{
log_error("Failed to acquire bus name: %s", cdbus_g_dbus_error.message);
dbus_error_free(&cdbus_g_dbus_error);
goto unref_connection;
}
if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
{
log_error("Requested connection name already exists");
goto unref_connection;
}
g_dbus_object = cdbus_object_path_new(
DBUS_OBJECT_PATH,
&jpl_appman_cdbus_interface_org_ladish_ApplicationManager0,
NULL,
NULL);
if (g_dbus_object == NULL)
{
goto unref_connection;
}
if (!cdbus_object_path_register(cdbus_g_dbus_connection, g_dbus_object))
{
goto destroy_dbus_object;
}
return true;
destroy_dbus_object:
cdbus_object_path_destroy(cdbus_g_dbus_connection, g_dbus_object);
unref_connection:
dbus_connection_unref(cdbus_g_dbus_connection);
fail:
return false;
}
static void disconnect_dbus(void)
{
cdbus_object_path_destroy(cdbus_g_dbus_connection, g_dbus_object);
dbus_connection_unref(cdbus_g_dbus_connection);
cdbus_call_last_error_cleanup();
}
void term_signal_handler(int signum)
{
log_info("Caught signal %d (%s), terminating", signum, strsignal(signum));
g_quit = true;
}
bool install_term_signal_handler(int signum, bool ignore_if_already_ignored)
{
sig_t sigh;
sigh = signal(signum, term_signal_handler);
if (sigh == SIG_ERR)
{
log_error("signal() failed to install handler function for signal %d.", signum);
return false;
}
if (sigh == SIG_IGN && ignore_if_already_ignored)
{
signal(SIGTERM, SIG_IGN);
}
return true;
}
int main(void)
{
int ret;
ret = EXIT_FAILURE;
cdbus_log_setup(jpl_cdbus_log);
log_info("JACK Plugin Launcher daemon, version " JPL_VERSION);
log_info("Copyleft JACK Plugin Authors");
#if defined(GIT_VERSION)
log_info("built from " GIT_VERSION);
#endif
log_info("built on " BUILD_TIMESTAMP);
/* install the signal handlers */
install_term_signal_handler(SIGTERM, false);
install_term_signal_handler(SIGINT, true);
install_term_signal_handler(SIGHUP, true);
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
{
log_error("signal(SIGPIPE, SIG_IGN).");
}
if (!connect_dbus())
{
log_error("Failed to connect to D-Bus");
goto exit;
}
while (!g_quit)
{
dbus_connection_read_write_dispatch(cdbus_g_dbus_connection, 50);
//jpl_run();
}
ret = EXIT_SUCCESS;
log_debug("Finished, cleaning up");
disconnect_dbus();
exit:
return ret;
}