]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-execute.c
tests: add test for config_parse_pass_environ
[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"
f4f15635
LP
32#include "unit.h"
33#include "util.h"
281e05b6
RC
34
35typedef void (*test_function_t)(Manager *m);
36
37static void check(Manager *m, Unit *unit, int status_expected, int code_expected) {
38 Service *service = NULL;
39 usec_t ts;
40 usec_t timeout = 2 * USEC_PER_SEC;
41
42 assert_se(m);
43 assert_se(unit);
44
45 service = SERVICE(unit);
46 printf("%s\n", unit->id);
47 exec_context_dump(&service->exec_context, stdout, "\t");
48 ts = now(CLOCK_MONOTONIC);
49 while (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED) {
50 int r;
51 usec_t n;
52
53 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
54 assert_se(r >= 0);
55
56 n = now(CLOCK_MONOTONIC);
57 if (ts + timeout < n) {
58 log_error("Test timeout when testing %s", unit->id);
59 exit(EXIT_FAILURE);
60 }
61 }
62 exec_status_dump(&service->main_exec_status, stdout, "\t");
63 assert_se(service->main_exec_status.status == status_expected);
64 assert_se(service->main_exec_status.code == code_expected);
65}
66
67static void test(Manager *m, const char *unit_name, int status_expected, int code_expected) {
68 Unit *unit;
69
70 assert_se(unit_name);
71
72 assert_se(manager_load_unit(m, unit_name, NULL, NULL, &unit) >= 0);
73 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
74 check(m, unit, status_expected, code_expected);
75}
76
77static void test_exec_workingdirectory(Manager *m) {
78 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
79
80 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
81
c6878637 82 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
281e05b6
RC
83}
84
85static void test_exec_personality(Manager *m) {
281e05b6
RC
86#if defined(__x86_64__)
87 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
7517f51e
HB
88
89#elif defined(__s390__)
90 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
91
92#else
93 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
281e05b6
RC
94#endif
95}
96
97static void test_exec_ignoresigpipe(Manager *m) {
98 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
99 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
100}
101
102static void test_exec_privatetmp(Manager *m) {
103 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
104
105 test(m, "exec-privatetmp-yes.service", 0, CLD_EXITED);
106 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
107
108 unlink("/tmp/test-exec_privatetmp");
109}
110
111static void test_exec_privatedevices(Manager *m) {
112 test(m, "exec-privatedevices-yes.service", 0, CLD_EXITED);
113 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
114}
115
116static void test_exec_systemcallfilter(Manager *m) {
117#ifdef HAVE_SECCOMP
118 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
119 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
120 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
121 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
122#endif
123}
124
125static void test_exec_systemcallerrornumber(Manager *m) {
126#ifdef HAVE_SECCOMP
127 test(m, "exec-systemcallerrornumber.service", 1, CLD_EXITED);
128#endif
129}
130
131static void test_exec_user(Manager *m) {
cc7fa4fb
RC
132 if (getpwnam("nobody"))
133 test(m, "exec-user.service", 0, CLD_EXITED);
ff4ca461
RC
134 else
135 log_error_errno(errno, "Skipping test_exec_user, could not find nobody user: %m");
281e05b6
RC
136}
137
138static void test_exec_group(Manager *m) {
cc7fa4fb
RC
139 if (getgrnam("nobody"))
140 test(m, "exec-group.service", 0, CLD_EXITED);
ff4ca461
RC
141 else
142 log_error_errno(errno, "Skipping test_exec_group, could not find nobody group: %m");
281e05b6
RC
143}
144
145static void test_exec_environment(Manager *m) {
146 test(m, "exec-environment.service", 0, CLD_EXITED);
147 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
148 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
149}
150
03bd70dd
RC
151static void test_exec_environmentfile(Manager *m) {
152 static const char e[] =
153 "VAR1='word1 word2'\n"
154 "VAR2=word3 \n"
155 "# comment1\n"
156 "\n"
157 "; comment2\n"
158 " ; # comment3\n"
159 "line without an equal\n"
160 "VAR3='$word 5 6'\n";
161 int r;
162
163 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
164 assert_se(r == 0);
165
166 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
167
168 unlink("/tmp/test-exec_environmentfile.conf");
169}
170
27c5347c
RC
171static void test_exec_umask(Manager *m) {
172 test(m, "exec-umask-default.service", 0, CLD_EXITED);
173 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
174}
175
cc3ddc85
RC
176static void test_exec_runtimedirectory(Manager *m) {
177 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
178 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
cc7fa4fb
RC
179 if (getgrnam("nobody"))
180 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
ff4ca461
RC
181 else
182 log_error_errno(errno, "Skipping test_exec_runtimedirectory-owner, could not find nobody group: %m");
183}
184
185static void test_exec_capabilityboundingset(Manager *m) {
186 int r;
187
188 /* We use capsh to test if the capabilities are
189 * properly set, so be sure that it exists */
190 r = find_binary("capsh", NULL);
191 if (r < 0) {
192 log_error_errno(r, "Skipping test_exec_capabilityboundingset, could not find capsh binary: %m");
193 return;
194 }
195
196 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
197 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
198 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
199 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
cc3ddc85
RC
200}
201
63447f11
RC
202static void test_exec_privatenetwork(Manager *m) {
203 int r;
204
205 r = find_binary("ip", NULL);
206 if (r < 0) {
207 log_error_errno(r, "Skipping test_exec_privatenetwork, could not find ip binary: %m");
208 return;
209 }
210
211 test(m, "exec-privatenetwork-yes.service", 0, CLD_EXITED);
212}
213
c388dfea
RC
214static void test_exec_oomscoreadjust(Manager *m) {
215 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
216 test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED);
217}
218
a6226758
RC
219static void test_exec_ioschedulingclass(Manager *m) {
220 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
221 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
222 test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED);
223 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
224}
225
281e05b6
RC
226int main(int argc, char *argv[]) {
227 test_function_t tests[] = {
228 test_exec_workingdirectory,
229 test_exec_personality,
230 test_exec_ignoresigpipe,
231 test_exec_privatetmp,
232 test_exec_privatedevices,
63447f11 233 test_exec_privatenetwork,
281e05b6
RC
234 test_exec_systemcallfilter,
235 test_exec_systemcallerrornumber,
236 test_exec_user,
237 test_exec_group,
238 test_exec_environment,
03bd70dd 239 test_exec_environmentfile,
27c5347c 240 test_exec_umask,
cc3ddc85 241 test_exec_runtimedirectory,
ff4ca461 242 test_exec_capabilityboundingset,
c388dfea 243 test_exec_oomscoreadjust,
a6226758 244 test_exec_ioschedulingclass,
281e05b6
RC
245 NULL,
246 };
247 test_function_t *test = NULL;
248 Manager *m = NULL;
249 int r;
250
251 log_parse_environment();
252 log_open();
253
607ff5f9
DH
254 /* It is needed otherwise cgroup creation fails */
255 if (getuid() != 0) {
256 printf("Skipping test: not root\n");
257 return EXIT_TEST_SKIP;
258 }
259
cc3ddc85 260 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
ac400816 261 assert_se(set_unit_path(TEST_DIR "/test-execute/") >= 0);
281e05b6 262
b2c23da8 263 r = manager_new(MANAGER_USER, true, &m);
281e05b6
RC
264 if (IN_SET(r, -EPERM, -EACCES, -EADDRINUSE, -EHOSTDOWN, -ENOENT)) {
265 printf("Skipping test: manager_new: %s", strerror(-r));
0eb3cc88 266 return EXIT_TEST_SKIP;
281e05b6
RC
267 }
268 assert_se(r >= 0);
269 assert_se(manager_startup(m, NULL, NULL) >= 0);
270
271 for (test = tests; test && *test; test++)
272 (*test)(m);
273
274 manager_free(m);
275
276 return 0;
277}