-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemd_manager.cpp
More file actions
166 lines (139 loc) · 4.77 KB
/
systemd_manager.cpp
File metadata and controls
166 lines (139 loc) · 4.77 KB
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
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
157
158
159
160
161
162
163
164
165
166
#include "systemd_manager.h"
SystemdManager::SystemdManager()
{
sd_bus_open_system(&m_bus);
}
SystemdManager::~SystemdManager()
{
sd_bus_unref(m_bus);
}
int SystemdManager::startService(std::string unit_name, bool stop = false)
{
int ret = -1;
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *reply = NULL;
char *object = nullptr;
if (!m_bus)
{
goto finish;
}
ret = sd_bus_call_method(m_bus,
SYSTEMD_SERVICE_NAME, /* service to contact */
SYSTEMD_OBJECT_PATH_NAME, /* object path */
SYSTEMD_MANAGER_INTERFACE_NAME, /* interface name */
stop ? SYSTEMD_STOP_METHOD : SYSTEMD_START_METHOD, /* method name */
&error, /* object to return error in */
&reply,
"ss",
unit_name.c_str(),
"replace");
/* input signature */
/* first argument */
/* second argument */
if (ret < 0)
{
std::cerr << "Failed to issue method call: " << error.message << std::endl;
goto finish;
}
/* Parse the response message */
ret = sd_bus_message_read(reply, "o", &object);
if (ret < 0)
{
std::cerr << "Failed to parse response message: " << std::strerror(-ret) << std::endl;
goto finish;
}
std::cout << "Object " << object << std::endl;
finish:
sd_bus_error_free(&error);
sd_bus_message_unref(reply);
return ret;
}
bool SystemdManager::isActive(std::string unit_name)
{
int ret;
bool state = false;
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *reply = NULL;
char *unit_path = nullptr;
char *active_state = nullptr;
if (!m_bus)
{
goto finish;
}
ret = sd_bus_call_method(m_bus,
SYSTEMD_SERVICE_NAME, /* service to contact */
SYSTEMD_OBJECT_PATH_NAME, /* object path */
SYSTEMD_MANAGER_INTERFACE_NAME, /* interface name */
SYSTEMD_LOAD_UNIT_METHOD, /* method name */
&error,/* object to return error in */
&reply,
"s",
unit_name.c_str()
);
/* input signature */
/* first argument */
/* second argument */
if (ret < 0) {
std::cerr << "Failed to issue method call: " << error.message << std::endl;
goto finish;
}
/* Parse the response message */
ret = sd_bus_message_read(reply, "o", &unit_path);
if (ret < 0) {
std::cerr << "Failed to parse response message: " << std::strerror(-ret) << std::endl;
goto finish;
}
std::cout << "Unit path :" << unit_path << std::endl;
ret = sd_bus_get_property_string(m_bus,
SYSTEMD_SERVICE_NAME, /* service to contact */
unit_path, /* object path */
SYSTEMD_UNIT_INTERFACE_NAME, /* interface name */
"ActiveState",
&error,/* object to return error in */
&active_state
);
if (ret < 0) {
std::cerr << "Failed to issue method call: " << error.message << std::endl;
goto finish;
}
if (strcmp(active_state, "active") == 0)
{
state = true;
}
finish:
sd_bus_error_free(&error);
sd_bus_message_unref(reply);
if (active_state) {
free(active_state);
}
return state;
}
int SystemdManager::reboot()
{
int ret = -1;
sd_bus_error error = SD_BUS_ERROR_NULL;
if (!m_bus)
{
goto finish;
}
ret = sd_bus_call_method(m_bus,
SYSTEMD_SERVICE_NAME, /* service to contact */
SYSTEMD_OBJECT_PATH_NAME, /* object path */
SYSTEMD_MANAGER_INTERFACE_NAME, /* interface name */
SYSTEMD_REBOOT_METHOD, /* method name */
&error, /* object to return error in */
nullptr, nullptr);
if (ret < 0)
{
std::cerr << "Failed to issue method call: " << error.message << std::endl;
goto finish;
}
finish:
sd_bus_error_free(&error);
return ret;
}
int main(int argc, char *argv[])
{
//SystemdManager s;
//s.startService("foo.service");
}