/
usb_moded-systemd.c
103 lines (83 loc) · 2.66 KB
1
/**
2
@file usb_moded-systemd.c
3
4
Copyright (C) 2013-2016 Jolla oy. All rights reserved.
5
6
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
7
@author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
8
9
This program is free software; you can redistribute it and/or
10
11
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
12
13
14
15
16
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.
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
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
*/
#define _GNU_SOURCE
#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.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded.h"
#include "usb_moded-log.h"
#include "usb_moded-systemd.h"
static DBusConnection * get_systemd_dbus_connection(void)
{
DBusError error;
DBusConnection *conn = 0;
dbus_error_init(&error);
conn = dbus_connection_open_private("unix:path=/run/systemd/private", &error);
if (!conn)
{
if (dbus_error_is_set(&error))
log_err("Cannot connect to systemd: %s", error.message);
else
log_err("Cannot connect to systemd");
52
dbus_error_free(&error);
53
54
55
56
57
58
return 0;
}
return conn;
}
59
// mode = replace
60
// method = StartUnit or StopUnit
61
gboolean systemd_control_service(const char *name, const char *method)
62
63
64
65
66
{
DBusConnection *bus;
DBusError error;
DBusMessage *msg = NULL, *reply = NULL;
67
gboolean ret = FALSE;
68
const char * replace = "replace";
69
70
71
dbus_error_init(&error);
72
log_debug("Handling %s, with systemd, method %s\n", name, method);
73
74
bus = get_systemd_dbus_connection();
75
76
if(!bus)
return(ret);
77
78
79
80
81
msg = dbus_message_new_method_call("org.freedesktop.systemd1",
"/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", method);
if(msg)
{
82
83
84
85
86
87
if(!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &replace, DBUS_TYPE_INVALID))
{
log_debug("error appending arguments\n");
dbus_message_unref(msg);
goto quit;
}
88
89
reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &error);
if(reply)
90
{
91
dbus_message_unref(reply);
92
ret = TRUE;
93
}
94
95
96
dbus_message_unref(msg);
}
97
quit:
98
99
dbus_connection_close(bus);
dbus_connection_unref(bus);
100
dbus_error_free(&error);
101
102
103
return(ret);
}