Skip to content

Latest commit

 

History

History
180 lines (147 loc) · 5.38 KB

usb_moded-systemd.c

File metadata and controls

180 lines (147 loc) · 5.38 KB
 
Jun 5, 2013
Jun 5, 2013
1
/**
Aug 24, 2018
Aug 24, 2018
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
* @file usb_moded-systemd.c
*
* Copyright (C) 2013-2018 Jolla oy. All rights reserved.
*
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Slava Monich <slava.monich@jolla.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
Jun 5, 2013
Jun 5, 2013
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <string.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "usb_moded-dbus-private.h"
#include "usb_moded.h"
#include "usb_moded-log.h"
#include "usb_moded-systemd.h"
Aug 24, 2018
Aug 24, 2018
37
38
39
40
/* ========================================================================= *
* Constants
* ========================================================================= */
Nov 7, 2016
Nov 7, 2016
41
42
43
44
#define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1"
#define SYSTEMD_DBUS_PATH "/org/freedesktop/systemd1"
#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.systemd1.Manager"
Aug 24, 2018
Aug 24, 2018
45
46
47
48
49
50
51
52
53
54
55
56
57
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- systemd -- */
gboolean systemd_control_service(const char *name, const char *method);
gboolean systemd_control_start (void);
void systemd_control_stop (void);
/* ========================================================================= *
* Data
* ========================================================================= */
Nov 7, 2016
Nov 7, 2016
58
59
60
61
/* SystemBus connection ref used for systemd control ipc */
static DBusConnection *systemd_con = NULL;
Aug 24, 2018
Aug 24, 2018
62
63
64
65
/* ========================================================================= *
* Functions
* ========================================================================= */
Nov 7, 2016
Nov 7, 2016
66
67
68
// QDBusObjectPath org.freedesktop.systemd1.Manager.StartUnit(QString name, QString mode)
// QDBusObjectPath org.freedesktop.systemd1.Manager.StopUnit(QString name, QString mode)
Jul 12, 2013
Jul 12, 2013
69
// mode = replace
Jun 5, 2013
Jun 5, 2013
70
// method = StartUnit or StopUnit
Nov 7, 2016
Nov 7, 2016
71
gboolean systemd_control_service(const char *name, const char *method)
Jun 5, 2013
Jun 5, 2013
72
{
Nov 7, 2016
Nov 7, 2016
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
DBusMessage *req = NULL;
DBusMessage *rsp = NULL;
DBusError err = DBUS_ERROR_INIT;
const char *arg = "replace";
const char *res = 0;
log_debug("%s(%s) ...", method, name);
if( !systemd_con ) {
log_err("not connected to system bus; skip systemd unit control");
goto EXIT;
}
req = dbus_message_new_method_call(SYSTEMD_DBUS_SERVICE,
SYSTEMD_DBUS_PATH,
SYSTEMD_DBUS_INTERFACE,
method);
if( !req ) {
log_err("failed to construct %s.%s request",
SYSTEMD_DBUS_INTERFACE,
method);
goto EXIT;
}
if( !dbus_message_append_args(req,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &arg,
DBUS_TYPE_INVALID))
{
log_debug("error appending arguments");
goto EXIT;
}
rsp = dbus_connection_send_with_reply_and_block(systemd_con, req, -1, &err);
if( !rsp ) {
log_err("no reply to %s.%s request: %s: %s",
SYSTEMD_DBUS_INTERFACE,
method,
err.name, err.message);
goto EXIT;
}
if( dbus_set_error_from_message(&err, rsp) ) {
log_err("got error reply to %s.%s request: %s: %s",
SYSTEMD_DBUS_INTERFACE,
method,
err.name, err.message);
goto EXIT;
}
if( !dbus_message_get_args(rsp, &err,
DBUS_TYPE_OBJECT_PATH, &res,
DBUS_TYPE_INVALID) ) {
log_err("failed to parse reply to %s.%s request: %s: %s",
SYSTEMD_DBUS_INTERFACE,
method,
err.name, err.message);
goto EXIT;
}
EXIT:
dbus_error_free(&err);
if( rsp ) dbus_message_unref(rsp);
if( req ) dbus_message_unref(req);
log_debug("%s(%s) -> %s", method, name, res ?: "N/A");
return res != 0;
}
/* ========================================================================= *
* start/stop systemd control availability
* ========================================================================= */
gboolean
systemd_control_start(void)
{
gboolean ack = FALSE;
log_debug("starting systemd control");
/* Get connection ref */
Aug 24, 2018
Aug 24, 2018
157
if( (systemd_con = umdbus_get_connection()) == 0 )
Nov 7, 2016
Nov 7, 2016
158
159
160
161
162
{
log_err("Could not connect to dbus for systemd control\n");
goto cleanup;
}
ack = TRUE;
Jun 5, 2013
Jun 5, 2013
163
Nov 7, 2016
Nov 7, 2016
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
cleanup:
return ack;
}
void
systemd_control_stop(void)
{
log_debug("stopping systemd control");
if(systemd_con)
{
/* Let go of connection ref */
dbus_connection_unref(systemd_con),
systemd_con = 0;
}
Jun 5, 2013
Jun 5, 2013
180
}