| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gio/gio.h> |
| 5 |
#include <signal.h> |
| 6 |
|
| 7 |
#include "daemon-process.h" |
| 8 |
|
| 9 |
struct _GpiodbusDaemonProcess { |
| 10 |
GObject parent_instance; |
| 11 |
GSubprocess *proc; |
| 12 |
}; |
| 13 |
|
| 14 |
G_DEFINE_TYPE(GpiodbusDaemonProcess, gpiodbus_daemon_process, G_TYPE_OBJECT); |
| 15 |
|
| 16 |
static gboolean on_timeout(gpointer data G_GNUC_UNUSED) |
| 17 |
{ |
| 18 |
g_error("timeout reached waiting for the daemon name to appear on the system bus"); |
| 19 |
|
| 20 |
return G_SOURCE_REMOVE; |
| 21 |
} |
| 22 |
|
| 23 |
static void on_name_appeared(GDBusConnection *con G_GNUC_UNUSED, |
| 24 |
const gchar *name G_GNUC_UNUSED, |
| 25 |
const gchar *name_owner G_GNUC_UNUSED, |
| 26 |
gpointer data) |
| 27 |
{ |
| 28 |
gboolean *name_state = data; |
| 29 |
|
| 30 |
*name_state = TRUE; |
| 31 |
} |
| 32 |
|
| 33 |
static void gpiodbus_daemon_process_constructed(GObject *obj) |
| 34 |
{ |
| 35 |
GpiodbusDaemonProcess *self = GPIODBUS_DAEMON_PROCESS_OBJ(obj); |
| 36 |
const gchar *path = g_getenv("GPIODBUS_TEST_DAEMON_PATH"); |
| 37 |
g_autoptr(GDBusConnection) con = NULL; |
| 38 |
g_autofree gchar *addr = NULL; |
| 39 |
g_autoptr(GError) err = NULL; |
| 40 |
gboolean name_state = FALSE; |
| 41 |
guint watch_id, timeout_id; |
| 42 |
|
| 43 |
if (!path) |
| 44 |
g_error("GPIODBUS_TEST_DAEMON_PATH environment variable must be set"); |
| 45 |
|
| 46 |
addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &err); |
| 47 |
if (!addr) |
| 48 |
g_error("failed to get an address for system bus: %s", |
| 49 |
err->message); |
| 50 |
|
| 51 |
con = g_dbus_connection_new_for_address_sync(addr, |
| 52 |
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | |
| 53 |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, |
| 54 |
NULL, NULL, &err); |
| 55 |
if (!con) |
| 56 |
g_error("failed to get a dbus connection: %s", err->message); |
| 57 |
|
| 58 |
watch_id = g_bus_watch_name_on_connection(con, "io.gpiod1", |
| 59 |
G_BUS_NAME_WATCHER_FLAGS_NONE, |
| 60 |
on_name_appeared, NULL, |
| 61 |
&name_state, NULL); |
| 62 |
|
| 63 |
self->proc = g_subprocess_new(G_SUBPROCESS_FLAGS_STDOUT_SILENCE | |
| 64 |
G_SUBPROCESS_FLAGS_STDERR_SILENCE, |
| 65 |
&err, path, NULL); |
| 66 |
if (!self->proc) |
| 67 |
g_error("failed to launch the gpio-manager process: %s", |
| 68 |
err->message); |
| 69 |
|
| 70 |
timeout_id = g_timeout_add_seconds(5, on_timeout, NULL); |
| 71 |
|
| 72 |
while (!name_state) |
| 73 |
g_main_context_iteration(NULL, TRUE); |
| 74 |
|
| 75 |
g_bus_unwatch_name(watch_id); |
| 76 |
g_source_remove(timeout_id); |
| 77 |
|
| 78 |
G_OBJECT_CLASS(gpiodbus_daemon_process_parent_class)->constructed(obj); |
| 79 |
} |
| 80 |
|
| 81 |
static void gpiodbus_daemon_process_kill(GSubprocess *proc) |
| 82 |
{ |
| 83 |
g_autoptr(GError) err = NULL; |
| 84 |
gint status; |
| 85 |
|
| 86 |
g_subprocess_send_signal(proc, SIGTERM); |
| 87 |
g_subprocess_wait(proc, NULL, &err); |
| 88 |
if (err) |
| 89 |
g_error("failed to collect the exit status of gpio-manager: %s", |
| 90 |
err->message); |
| 91 |
|
| 92 |
if (!g_subprocess_get_if_exited(proc)) |
| 93 |
g_error("dbus-manager process did not exit normally"); |
| 94 |
|
| 95 |
status = g_subprocess_get_exit_status(proc); |
| 96 |
if (status != 0) |
| 97 |
g_error("dbus-manager process exited with a non-zero status: %d", |
| 98 |
status); |
| 99 |
|
| 100 |
g_object_unref(proc); |
| 101 |
} |
| 102 |
|
| 103 |
static void gpiodbus_daemon_process_dispose(GObject *obj) |
| 104 |
{ |
| 105 |
GpiodbusDaemonProcess *self = GPIODBUS_DAEMON_PROCESS_OBJ(obj); |
| 106 |
|
| 107 |
g_clear_pointer(&self->proc, gpiodbus_daemon_process_kill); |
| 108 |
|
| 109 |
G_OBJECT_CLASS(gpiodbus_daemon_process_parent_class)->dispose(obj); |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
gpiodbus_daemon_process_class_init(GpiodbusDaemonProcessClass *proc_class) |
| 114 |
{ |
| 115 |
GObjectClass *class = G_OBJECT_CLASS(proc_class); |
| 116 |
|
| 117 |
class->constructed = gpiodbus_daemon_process_constructed; |
| 118 |
class->dispose = gpiodbus_daemon_process_dispose; |
| 119 |
} |
| 120 |
|
| 121 |
static void gpiodbus_daemon_process_init(GpiodbusDaemonProcess *self) |
| 122 |
{ |
| 123 |
self->proc = NULL; |
| 124 |
} |
| 125 |
|
| 126 |
GpiodbusDaemonProcess *gpiodbus_daemon_process_new(void) |
| 127 |
{ |
| 128 |
return g_object_new(GPIODBUS_DAEMON_PROCESS_TYPE, NULL); |
| 129 |
} |