]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-execute.c
capabilities: added support for ambient capabilities.
[thirdparty/systemd.git] / src / test / test-execute.c
CommitLineData
281e05b6
RC
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Ronny Chevalier
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
cc7fa4fb
RC
20#include <grp.h>
21#include <pwd.h>
ff4ca461
RC
22#include <stdio.h>
23#include <sys/types.h>
281e05b6 24
03bd70dd 25#include "fileio.h"
f4f15635 26#include "fs-util.h"
281e05b6 27#include "macro.h"
f4f15635 28#include "manager.h"
281e05b6 29#include "mkdir.h"
ff4ca461 30#include "path-util.h"
c6878637 31#include "rm-rf.h"
8b3aa503 32#include "test-helper.h"
f4f15635
LP
33#include "unit.h"
34#include "util.h"
281e05b6
RC
35
36typedef void (*test_function_t)(Manager *m);
37
38static void check(Manager *m, Unit *unit, int status_expected, int code_expected) {
39 Service *service = NULL;
40 usec_t ts;
41 usec_t timeout = 2 * USEC_PER_SEC;
42
43 assert_se(m);
44 assert_se(unit);
45
46 service = SERVICE(unit);
47 printf("%s\n", unit->id);
48 exec_context_dump(&service->exec_context, stdout, "\t");
49 ts = now(CLOCK_MONOTONIC);
50 while (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED) {
51 int r;
52 usec_t n;
53
54 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
55 assert_se(r >= 0);
56
57 n = now(CLOCK_MONOTONIC);
58 if (ts + timeout < n) {
59 log_error("Test timeout when testing %s", unit->id);
60 exit(EXIT_FAILURE);
61 }
62 }
63 exec_status_dump(&service->main_exec_status, stdout, "\t");
64 assert_se(service->main_exec_status.status == status_expected);
65 assert_se(service->main_exec_status.code == code_expected);
66}
67
68static void test(Manager *m, const char *unit_name, int status_expected, int code_expected) {
69 Unit *unit;
70
71 assert_se(unit_name);
72
73 assert_se(manager_load_unit(m, unit_name, NULL, NULL, &unit) >= 0);
74 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
75 check(m, unit, status_expected, code_expected);
76}
77
78static void test_exec_workingdirectory(Manager *m) {
79 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
80
81 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
82
c6878637 83 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
281e05b6
RC
84}
85
86static void test_exec_personality(Manager *m) {
281e05b6
RC
87#if defined(__x86_64__)
88 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
7517f51e
HB
89
90#elif defined(__s390__)
91 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
92
93#else
94 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
281e05b6
RC
95#endif
96}
97
98static void test_exec_ignoresigpipe(Manager *m) {
99 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
100 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
101}
102
103static void test_exec_privatetmp(Manager *m) {
104 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
105
106 test(m, "exec-privatetmp-yes.service", 0, CLD_EXITED);
107 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
108
109 unlink("/tmp/test-exec_privatetmp");
110}
111
112static void test_exec_privatedevices(Manager *m) {
113 test(m, "exec-privatedevices-yes.service", 0, CLD_EXITED);
114 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
115}
116
117static void test_exec_systemcallfilter(Manager *m) {
118#ifdef HAVE_SECCOMP
119 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
120 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
121 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
122 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
123#endif
124}
125
126static void test_exec_systemcallerrornumber(Manager *m) {
127#ifdef HAVE_SECCOMP
128 test(m, "exec-systemcallerrornumber.service", 1, CLD_EXITED);
129#endif
130}
131
132static void test_exec_user(Manager *m) {
cc7fa4fb
RC
133 if (getpwnam("nobody"))
134 test(m, "exec-user.service", 0, CLD_EXITED);
ff4ca461
RC
135 else
136 log_error_errno(errno, "Skipping test_exec_user, could not find nobody user: %m");
281e05b6
RC
137}
138
139static void test_exec_group(Manager *m) {
cc7fa4fb
RC
140 if (getgrnam("nobody"))
141 test(m, "exec-group.service", 0, CLD_EXITED);
ff4ca461
RC
142 else
143 log_error_errno(errno, "Skipping test_exec_group, could not find nobody group: %m");
281e05b6
RC
144}
145
146static void test_exec_environment(Manager *m) {
147 test(m, "exec-environment.service", 0, CLD_EXITED);
148 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
149 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
150}
151
03bd70dd
RC
152static void test_exec_environmentfile(Manager *m) {
153 static const char e[] =
154 "VAR1='word1 word2'\n"
155 "VAR2=word3 \n"
156 "# comment1\n"
157 "\n"
158 "; comment2\n"
159 " ; # comment3\n"
160 "line without an equal\n"
161 "VAR3='$word 5 6'\n";
162 int r;
163
164 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
165 assert_se(r == 0);
166
167 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
168
169 unlink("/tmp/test-exec_environmentfile.conf");
170}
171
4c80d201 172static void test_exec_passenvironment(Manager *m) {
e1abca2e
FB
173 /* test-execute runs under MANAGER_USER which, by default, forwards all
174 * variables present in the environment, but only those that are
175 * present _at the time it is created_!
176 *
177 * So these PassEnvironment checks are still expected to work, since we
178 * are ensuring the variables are not present at manager creation (they
179 * are unset explicitly in main) and are only set here.
180 *
181 * This is still a good approximation of how a test for MANAGER_SYSTEM
182 * would work.
183 */
4c80d201
FB
184 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
185 assert_se(setenv("VAR2", "word3", 1) == 0);
186 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
187 test(m, "exec-passenvironment.service", 0, CLD_EXITED);
188 test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
189 test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
190 assert_se(unsetenv("VAR1") == 0);
191 assert_se(unsetenv("VAR2") == 0);
192 assert_se(unsetenv("VAR3") == 0);
193 test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
194}
195
27c5347c
RC
196static void test_exec_umask(Manager *m) {
197 test(m, "exec-umask-default.service", 0, CLD_EXITED);
198 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
199}
200
cc3ddc85
RC
201static void test_exec_runtimedirectory(Manager *m) {
202 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
203 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
cc7fa4fb
RC
204 if (getgrnam("nobody"))
205 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
ff4ca461
RC
206 else
207 log_error_errno(errno, "Skipping test_exec_runtimedirectory-owner, could not find nobody group: %m");
208}
209
210static void test_exec_capabilityboundingset(Manager *m) {
211 int r;
212
213 /* We use capsh to test if the capabilities are
214 * properly set, so be sure that it exists */
215 r = find_binary("capsh", NULL);
216 if (r < 0) {
217 log_error_errno(r, "Skipping test_exec_capabilityboundingset, could not find capsh binary: %m");
218 return;
219 }
220
221 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
222 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
223 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
224 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
cc3ddc85
RC
225}
226
63447f11
RC
227static void test_exec_privatenetwork(Manager *m) {
228 int r;
229
230 r = find_binary("ip", NULL);
231 if (r < 0) {
232 log_error_errno(r, "Skipping test_exec_privatenetwork, could not find ip binary: %m");
233 return;
234 }
235
236 test(m, "exec-privatenetwork-yes.service", 0, CLD_EXITED);
237}
238
c388dfea
RC
239static void test_exec_oomscoreadjust(Manager *m) {
240 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
241 test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED);
242}
243
a6226758
RC
244static void test_exec_ioschedulingclass(Manager *m) {
245 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
246 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
247 test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED);
248 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
249}
250
281e05b6
RC
251int main(int argc, char *argv[]) {
252 test_function_t tests[] = {
253 test_exec_workingdirectory,
254 test_exec_personality,
255 test_exec_ignoresigpipe,
256 test_exec_privatetmp,
257 test_exec_privatedevices,
63447f11 258 test_exec_privatenetwork,
281e05b6
RC
259 test_exec_systemcallfilter,
260 test_exec_systemcallerrornumber,
261 test_exec_user,
262 test_exec_group,
263 test_exec_environment,
03bd70dd 264 test_exec_environmentfile,
4c80d201 265 test_exec_passenvironment,
27c5347c 266 test_exec_umask,
cc3ddc85 267 test_exec_runtimedirectory,
ff4ca461 268 test_exec_capabilityboundingset,
c388dfea 269 test_exec_oomscoreadjust,
a6226758 270 test_exec_ioschedulingclass,
281e05b6
RC
271 NULL,
272 };
273 test_function_t *test = NULL;
274 Manager *m = NULL;
275 int r;
276
277 log_parse_environment();
278 log_open();
279
607ff5f9
DH
280 /* It is needed otherwise cgroup creation fails */
281 if (getuid() != 0) {
282 printf("Skipping test: not root\n");
283 return EXIT_TEST_SKIP;
284 }
285
cc3ddc85 286 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
ac400816 287 assert_se(set_unit_path(TEST_DIR "/test-execute/") >= 0);
281e05b6 288
e1abca2e
FB
289 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
290 * cases, otherwise (and if they are present in the environment),
291 * `manager_default_environment` will copy them into the default
292 * environment which is passed to each created job, which will make the
293 * tests that expect those not to be present to fail.
294 */
295 assert_se(unsetenv("VAR1") == 0);
296 assert_se(unsetenv("VAR2") == 0);
297 assert_se(unsetenv("VAR3") == 0);
298
b2c23da8 299 r = manager_new(MANAGER_USER, true, &m);
8b3aa503 300 if (MANAGER_SKIP_TEST(r)) {
1d31abf7 301 printf("Skipping test: manager_new: %s\n", strerror(-r));
0eb3cc88 302 return EXIT_TEST_SKIP;
281e05b6
RC
303 }
304 assert_se(r >= 0);
305 assert_se(manager_startup(m, NULL, NULL) >= 0);
306
307 for (test = tests; test && *test; test++)
308 (*test)(m);
309
310 manager_free(m);
311
312 return 0;
313}