usb_moded 0.86.0+mer64
usb_moded-systemd.c
Go to the documentation of this file.
1
25
26#include "usb_moded-systemd.h"
27
29#include "usb_moded-log.h"
30
31/* ========================================================================= *
32 * Constants
33 * ========================================================================= */
34
35#define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1"
36#define SYSTEMD_DBUS_PATH "/org/freedesktop/systemd1"
37#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.systemd1.Manager"
38
39/* ========================================================================= *
40 * Prototypes
41 * ========================================================================= */
42
43/* ------------------------------------------------------------------------- *
44 * SYSTEMD
45 * ------------------------------------------------------------------------- */
46
47gboolean systemd_control_service(const char *name, const char *method);
48gboolean systemd_control_start (void);
49void systemd_control_stop (void);
50
51/* ========================================================================= *
52 * Data
53 * ========================================================================= */
54
55/* SystemBus connection ref used for systemd control ipc */
56static DBusConnection *systemd_con = NULL;
57
58/* ========================================================================= *
59 * Functions
60 * ========================================================================= */
61
62// QDBusObjectPath org.freedesktop.systemd1.Manager.StartUnit(QString name, QString mode)
63// QDBusObjectPath org.freedesktop.systemd1.Manager.StopUnit(QString name, QString mode)
64
65// mode = replace
66// method = StartUnit or StopUnit
67gboolean systemd_control_service(const char *name, const char *method)
68{
69 LOG_REGISTER_CONTEXT;
70
71 DBusMessage *req = NULL;
72 DBusMessage *rsp = NULL;
73 DBusError err = DBUS_ERROR_INIT;
74 const char *arg = "replace";
75 const char *res = 0;
76
77 log_debug("%s(%s) ...", method, name);
78
79 if( !systemd_con ) {
80 log_err("not connected to system bus; skip systemd unit control");
81 goto EXIT;
82 }
83
84 req = dbus_message_new_method_call(SYSTEMD_DBUS_SERVICE,
85 SYSTEMD_DBUS_PATH,
86 SYSTEMD_DBUS_INTERFACE,
87 method);
88 if( !req ) {
89 log_err("failed to construct %s.%s request",
90 SYSTEMD_DBUS_INTERFACE,
91 method);
92 goto EXIT;
93 }
94
95 if( !dbus_message_append_args(req,
96 DBUS_TYPE_STRING, &name,
97 DBUS_TYPE_STRING, &arg,
98 DBUS_TYPE_INVALID))
99 {
100 log_debug("error appending arguments");
101 goto EXIT;
102 }
103
104 rsp = dbus_connection_send_with_reply_and_block(systemd_con, req, -1, &err);
105 if( !rsp ) {
106 log_err("no reply to %s.%s request: %s: %s",
107 SYSTEMD_DBUS_INTERFACE,
108 method,
109 err.name, err.message);
110 goto EXIT;
111 }
112
113 if( dbus_set_error_from_message(&err, rsp) ) {
114 log_err("got error reply to %s.%s request: %s: %s",
115 SYSTEMD_DBUS_INTERFACE,
116 method,
117 err.name, err.message);
118 goto EXIT;
119 }
120
121 if( !dbus_message_get_args(rsp, &err,
122 DBUS_TYPE_OBJECT_PATH, &res,
123 DBUS_TYPE_INVALID) ) {
124 log_err("failed to parse reply to %s.%s request: %s: %s",
125 SYSTEMD_DBUS_INTERFACE,
126 method,
127 err.name, err.message);
128 goto EXIT;
129 }
130
131EXIT:
132
133 dbus_error_free(&err);
134
135 log_debug("%s(%s) -> %s", method, name, res ?: "N/A");
136
137 if( rsp ) dbus_message_unref(rsp);
138 if( req ) dbus_message_unref(req);
139
140 return res != 0;
141}
142
143/* ========================================================================= *
144 * start/stop systemd control availability
145 * ========================================================================= */
146
147gboolean
148systemd_control_start(void)
149{
150 LOG_REGISTER_CONTEXT;
151
152 gboolean ack = FALSE;
153
154 log_debug("starting systemd control");
155
156 /* Get connection ref */
157 if( (systemd_con = umdbus_get_connection()) == 0 )
158 {
159 log_err("Could not connect to dbus for systemd control\n");
160 goto cleanup;
161 }
162 ack = TRUE;
163
164cleanup:
165
166 return ack;
167}
168
169void
170systemd_control_stop(void)
171{
172 LOG_REGISTER_CONTEXT;
173
174 log_debug("stopping systemd control");
175
176 if(systemd_con)
177 {
178 /* Let go of connection ref */
179 dbus_connection_unref(systemd_con),
180 systemd_con = 0;
181 }
182}