-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd-unit.c
97 lines (78 loc) · 2.39 KB
/
systemd-unit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
#include "systemd-unit.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include "dbus/dbus-shim.h"
#include "dbus/dbus-util.h"
static inline int dbus_try_read_object(dbus_bus *bus, dbus_message *reply, char **ret)
{
int type = dbus_message_type(reply);
const char *buf;
int rc = 0;
if (type == 'o') {
rc = dbus_message_read(reply, "o", &buf);
*ret = strdup(buf);
} else if (type == 's') {
rc = -1;
dbus_message_read(reply, "s", &buf);
free(bus->error);
bus->error = strdup(buf);
*ret = NULL;
}
return rc;
}
int get_unit_by_pid(dbus_bus *bus, uint32_t pid, char **ret)
{
int rc = 0;
dbus_message *m;
dbus_new_method_call("org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnitByPID", &m);
if (pid == 0)
pid = getpid();
dbus_message_append(m, "u", pid);
dbus_message *reply;
dbus_send_with_reply_and_block(bus, m, ret ? &reply : NULL);
dbus_message_free(m);
if (ret) {
rc = dbus_try_read_object(bus, reply, ret);
dbus_message_free(reply);
}
return rc;
}
int get_unit(dbus_bus *bus, const char *name, char **ret)
{
int rc = 0;
dbus_message *m;
dbus_new_method_call("org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit", &m);
dbus_message_append(m, "s", name);
dbus_message *reply;
dbus_send_with_reply_and_block(bus, m, ret ? &reply : NULL);
dbus_message_free(m);
if (ret) {
rc = dbus_try_read_object(bus, reply, ret);
dbus_message_free(reply);
}
return rc;
}
void unit_kill(dbus_bus *bus, const char *path, int32_t signal)
{
dbus_message *m;
dbus_new_method_call("org.freedesktop.systemd1",
path,
"org.freedesktop.systemd1.Unit",
"Stop", &m);
dbus_message_append(m, "si", "fail", signal);
dbus_send_with_reply_and_block(bus, m, NULL);
dbus_message_free(m);
}
int get_unit_state(dbus_bus *bus, const char *path, char **ret)
{
return query_property(bus, path, "org.freedesktop.systemd1.Unit",
"SubState", "s", ret);
}