Skip to content

Latest commit

 

History

History
103 lines (83 loc) · 2.66 KB

usb_moded-systemd.c

File metadata and controls

103 lines (83 loc) · 2.66 KB
 
Jun 5, 2013
Jun 5, 2013
1
/**
Nov 7, 2016
Nov 7, 2016
2
@file usb_moded-systemd.c
Jun 5, 2013
Jun 5, 2013
3
Nov 7, 2016
Nov 7, 2016
4
Copyright (C) 2013-2016 Jolla oy. All rights reserved.
Jun 5, 2013
Jun 5, 2013
5
6
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
Nov 7, 2016
Nov 7, 2016
7
@author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
Jun 5, 2013
Jun 5, 2013
8
9
This program is free software; you can redistribute it and/or
Nov 7, 2016
Nov 7, 2016
10
11
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
Jun 5, 2013
Jun 5, 2013
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.
Nov 7, 2016
Nov 7, 2016
17
Jun 5, 2013
Jun 5, 2013
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");
Oct 22, 2013
Oct 22, 2013
52
dbus_error_free(&error);
Jun 5, 2013
Jun 5, 2013
53
54
55
56
57
58
return 0;
}
return conn;
}
Jul 12, 2013
Jul 12, 2013
59
// mode = replace
Jun 5, 2013
Jun 5, 2013
60
// method = StartUnit or StopUnit
Nov 7, 2016
Nov 7, 2016
61
gboolean systemd_control_service(const char *name, const char *method)
Jun 5, 2013
Jun 5, 2013
62
63
64
65
66
{
DBusConnection *bus;
DBusError error;
DBusMessage *msg = NULL, *reply = NULL;
Nov 7, 2016
Nov 7, 2016
67
gboolean ret = FALSE;
Jul 12, 2013
Jul 12, 2013
68
const char * replace = "replace";
Jun 5, 2013
Jun 5, 2013
69
Jul 12, 2013
Jul 12, 2013
70
71
dbus_error_init(&error);
Sep 19, 2013
Sep 19, 2013
72
log_debug("Handling %s, with systemd, method %s\n", name, method);
Jul 11, 2013
Jul 11, 2013
73
Jun 5, 2013
Jun 5, 2013
74
bus = get_systemd_dbus_connection();
Oct 22, 2013
Oct 22, 2013
75
76
if(!bus)
return(ret);
Jun 5, 2013
Jun 5, 2013
77
78
79
80
81
msg = dbus_message_new_method_call("org.freedesktop.systemd1",
"/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", method);
if(msg)
{
Jul 12, 2013
Jul 12, 2013
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;
}
Jun 5, 2013
Jun 5, 2013
88
89
reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &error);
if(reply)
Jul 11, 2013
Jul 11, 2013
90
{
Mar 30, 2015
Mar 30, 2015
91
dbus_message_unref(reply);
Nov 7, 2016
Nov 7, 2016
92
ret = TRUE;
Jul 11, 2013
Jul 11, 2013
93
}
Jun 5, 2013
Jun 5, 2013
94
95
96
dbus_message_unref(msg);
}
Jul 12, 2013
Jul 12, 2013
97
quit:
Jun 5, 2013
Jun 5, 2013
98
99
dbus_connection_close(bus);
dbus_connection_unref(bus);
Jul 7, 2016
Jul 7, 2016
100
dbus_error_free(&error);
Jun 5, 2013
Jun 5, 2013
101
102
103
return(ret);
}