GCC Code Coverage Report


Directory: ./
File: panels/system/secure-shell/cc-systemd-service.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 77 0.0%
Functions: 0 3 0.0%
Branches: 0 28 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2013 Intel, Inc
3 * Copyright (C) 2022 Red Hat, Inc
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Author: Thomas Wood <thomas.wood@intel.com>
20 *
21 */
22
23 #include "cc-systemd-service.h"
24
25 gboolean
26 cc_is_service_active (const char *service,
27 GBusType bus_type)
28 {
29 g_autoptr(GError) error = NULL;
30 g_autoptr(GDBusConnection) connection = NULL;
31 g_autoptr(GVariant) unit_path_variant = NULL;
32 g_autofree char *unit_path = NULL;
33 g_autoptr(GVariant) active_state_prop = NULL;
34 g_autoptr(GVariant) active_state_variant = NULL;
35 const char *active_state = NULL;
36 g_autoptr(GVariant) unit_state_prop = NULL;
37 g_autoptr(GVariant) unit_state_variant = NULL;
38 const char *unit_state = NULL;
39
40 connection = g_bus_get_sync (bus_type, NULL, &error);
41 if (!connection)
42 {
43 g_warning ("Failed connecting to D-Bus system bus: %s", error->message);
44 return FALSE;
45 }
46
47 unit_path_variant =
48 g_dbus_connection_call_sync (connection,
49 "org.freedesktop.systemd1",
50 "/org/freedesktop/systemd1",
51 "org.freedesktop.systemd1.Manager",
52 "GetUnit",
53 g_variant_new ("(s)",
54 service),
55 (GVariantType *) "(o)",
56 G_DBUS_CALL_FLAGS_NONE,
57 -1,
58 NULL,
59 NULL);
60
61 if (!unit_path_variant)
62 return FALSE;
63 g_variant_get_child (unit_path_variant, 0, "o", &unit_path);
64
65 active_state_prop =
66 g_dbus_connection_call_sync (connection,
67 "org.freedesktop.systemd1",
68 unit_path,
69 "org.freedesktop.DBus.Properties",
70 "Get",
71 g_variant_new ("(ss)",
72 "org.freedesktop.systemd1.Unit",
73 "ActiveState"),
74 (GVariantType *) "(v)",
75 G_DBUS_CALL_FLAGS_NONE,
76 -1,
77 NULL,
78 &error);
79
80 if (!active_state_prop)
81 {
82 g_warning ("Failed to get service active state: %s", error->message);
83 return FALSE;
84 }
85 g_variant_get_child (active_state_prop, 0, "v", &active_state_variant);
86 active_state = g_variant_get_string (active_state_variant, NULL);
87
88 if (g_strcmp0 (active_state, "active") != 0 &&
89 g_strcmp0 (active_state, "activating") != 0)
90 return FALSE;
91
92 unit_state_prop =
93 g_dbus_connection_call_sync (connection,
94 "org.freedesktop.systemd1",
95 unit_path,
96 "org.freedesktop.DBus.Properties",
97 "Get",
98 g_variant_new ("(ss)",
99 "org.freedesktop.systemd1.Unit",
100 "UnitFileState"),
101 (GVariantType *) "(v)",
102 G_DBUS_CALL_FLAGS_NONE,
103 -1,
104 NULL,
105 &error);
106
107 if (!unit_state_prop)
108 {
109 g_warning ("Failed to get service active state: %s", error->message);
110 return FALSE;
111 }
112 g_variant_get_child (unit_state_prop, 0, "v", &unit_state_variant);
113 unit_state = g_variant_get_string (unit_state_variant, NULL);
114
115 if (g_strcmp0 (unit_state, "enabled") == 0 ||
116 g_strcmp0 (unit_state, "static") == 0)
117 return TRUE;
118 else
119 return FALSE;
120 }
121
122 gboolean
123 cc_enable_service (const char *service,
124 GBusType bus_type,
125 GError **error)
126 {
127 g_autoptr(GDBusConnection) connection = NULL;
128 g_autoptr(GVariant) start_result = NULL;
129 g_autoptr(GVariant) enable_result = NULL;
130 const char *service_list[] = { service, NULL };
131
132 connection = g_bus_get_sync (bus_type, NULL, error);
133 if (!connection)
134 {
135 g_prefix_error_literal (error, "Failed connecting to D-Bus system bus: ");
136 return FALSE;
137 }
138
139 start_result = g_dbus_connection_call_sync (connection,
140 "org.freedesktop.systemd1",
141 "/org/freedesktop/systemd1",
142 "org.freedesktop.systemd1.Manager",
143 "StartUnit",
144 g_variant_new ("(ss)",
145 service,
146 "replace"),
147 (GVariantType *) "(o)",
148 G_DBUS_CALL_FLAGS_NONE,
149 -1,
150 NULL,
151 error);
152
153 if (!start_result)
154 {
155 g_prefix_error_literal (error, "Failed to start service: ");
156 return FALSE;
157 }
158
159 enable_result = g_dbus_connection_call_sync (connection,
160 "org.freedesktop.systemd1",
161 "/org/freedesktop/systemd1",
162 "org.freedesktop.systemd1.Manager",
163 "EnableUnitFiles",
164 g_variant_new ("(^asbb)",
165 service_list,
166 FALSE, FALSE),
167 (GVariantType *) "(ba(sss))",
168 G_DBUS_CALL_FLAGS_NONE,
169 -1,
170 NULL,
171 error);
172
173 if (!enable_result)
174 {
175 g_prefix_error_literal (error, "Failed to enable service: ");
176 return FALSE;
177 }
178
179 return TRUE;
180 }
181
182 gboolean
183 cc_disable_service (const char *service,
184 GBusType bus_type,
185 GError **error)
186 {
187 g_autoptr(GDBusConnection) connection = NULL;
188 g_autoptr(GVariant) stop_result = NULL;
189 g_autoptr(GVariant) disable_result = NULL;
190 const char *service_list[] = { service, NULL };
191
192 connection = g_bus_get_sync (bus_type, NULL, error);
193 if (!connection)
194 {
195 g_prefix_error_literal (error, "Failed connecting to D-Bus system bus: ");
196 return FALSE;
197 }
198
199 stop_result = g_dbus_connection_call_sync (connection,
200 "org.freedesktop.systemd1",
201 "/org/freedesktop/systemd1",
202 "org.freedesktop.systemd1.Manager",
203 "StopUnit",
204 g_variant_new ("(ss)", service, "replace"),
205 (GVariantType *) "(o)",
206 G_DBUS_CALL_FLAGS_NONE,
207 -1,
208 NULL,
209 error);
210 if (!stop_result)
211 {
212 g_prefix_error_literal (error, "Failed to stop service: ");
213 return FALSE;
214 }
215
216 disable_result = g_dbus_connection_call_sync (connection,
217 "org.freedesktop.systemd1",
218 "/org/freedesktop/systemd1",
219 "org.freedesktop.systemd1.Manager",
220 "DisableUnitFiles",
221 g_variant_new ("(^asb)", service_list, FALSE,
222 FALSE),
223 (GVariantType *) "(a(sss))",
224 G_DBUS_CALL_FLAGS_NONE,
225 -1,
226 NULL,
227 error);
228
229 if (!stop_result)
230 {
231 g_prefix_error_literal (error, "Failed to disable service: ");
232 return FALSE;
233 }
234
235 return TRUE;
236 }
237