2 This file is part of systemd.
4 Copyright 2014 Ronny Chevalier
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.
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.
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/>.
23 #include <sys/prctl.h>
24 #include <sys/types.h>
31 #include "path-util.h"
33 #include "test-helper.h"
37 typedef void (*test_function_t
)(Manager
*m
);
39 static void check(Manager
*m
, Unit
*unit
, int status_expected
, int code_expected
) {
40 Service
*service
= NULL
;
42 usec_t timeout
= 2 * USEC_PER_SEC
;
47 service
= SERVICE(unit
);
48 printf("%s\n", unit
->id
);
49 exec_context_dump(&service
->exec_context
, stdout
, "\t");
50 ts
= now(CLOCK_MONOTONIC
);
51 while (service
->state
!= SERVICE_DEAD
&& service
->state
!= SERVICE_FAILED
) {
55 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
58 n
= now(CLOCK_MONOTONIC
);
59 if (ts
+ timeout
< n
) {
60 log_error("Test timeout when testing %s", unit
->id
);
64 exec_status_dump(&service
->main_exec_status
, stdout
, "\t");
65 assert_se(service
->main_exec_status
.status
== status_expected
);
66 assert_se(service
->main_exec_status
.code
== code_expected
);
69 static void test(Manager
*m
, const char *unit_name
, int status_expected
, int code_expected
) {
74 assert_se(manager_load_unit(m
, unit_name
, NULL
, NULL
, &unit
) >= 0);
75 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
76 check(m
, unit
, status_expected
, code_expected
);
79 static void test_exec_workingdirectory(Manager
*m
) {
80 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
82 test(m
, "exec-workingdirectory.service", 0, CLD_EXITED
);
84 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT
|REMOVE_PHYSICAL
);
87 static void test_exec_personality(Manager
*m
) {
88 #if defined(__x86_64__)
89 test(m
, "exec-personality-x86-64.service", 0, CLD_EXITED
);
91 #elif defined(__s390__)
92 test(m
, "exec-personality-s390.service", 0, CLD_EXITED
);
95 test(m
, "exec-personality-x86.service", 0, CLD_EXITED
);
99 static void test_exec_ignoresigpipe(Manager
*m
) {
100 test(m
, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED
);
101 test(m
, "exec-ignoresigpipe-no.service", SIGPIPE
, CLD_KILLED
);
104 static void test_exec_privatetmp(Manager
*m
) {
105 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
107 test(m
, "exec-privatetmp-yes.service", 0, CLD_EXITED
);
108 test(m
, "exec-privatetmp-no.service", 0, CLD_EXITED
);
110 unlink("/tmp/test-exec_privatetmp");
113 static void test_exec_privatedevices(Manager
*m
) {
114 test(m
, "exec-privatedevices-yes.service", 0, CLD_EXITED
);
115 test(m
, "exec-privatedevices-no.service", 0, CLD_EXITED
);
118 static void test_exec_systemcallfilter(Manager
*m
) {
120 test(m
, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED
);
121 test(m
, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED
);
122 test(m
, "exec-systemcallfilter-failing.service", SIGSYS
, CLD_KILLED
);
123 test(m
, "exec-systemcallfilter-failing2.service", SIGSYS
, CLD_KILLED
);
127 static void test_exec_systemcallerrornumber(Manager
*m
) {
129 test(m
, "exec-systemcallerrornumber.service", 1, CLD_EXITED
);
133 static void test_exec_user(Manager
*m
) {
134 if (getpwnam("nobody"))
135 test(m
, "exec-user.service", 0, CLD_EXITED
);
137 log_error_errno(errno
, "Skipping test_exec_user, could not find nobody user: %m");
140 static void test_exec_group(Manager
*m
) {
141 if (getgrnam("nobody"))
142 test(m
, "exec-group.service", 0, CLD_EXITED
);
144 log_error_errno(errno
, "Skipping test_exec_group, could not find nobody group: %m");
147 static void test_exec_environment(Manager
*m
) {
148 test(m
, "exec-environment.service", 0, CLD_EXITED
);
149 test(m
, "exec-environment-multiple.service", 0, CLD_EXITED
);
150 test(m
, "exec-environment-empty.service", 0, CLD_EXITED
);
153 static void test_exec_environmentfile(Manager
*m
) {
154 static const char e
[] =
155 "VAR1='word1 word2'\n"
161 "line without an equal\n"
162 "VAR3='$word 5 6'\n";
165 r
= write_string_file("/tmp/test-exec_environmentfile.conf", e
, WRITE_STRING_FILE_CREATE
);
168 test(m
, "exec-environmentfile.service", 0, CLD_EXITED
);
170 unlink("/tmp/test-exec_environmentfile.conf");
173 static void test_exec_passenvironment(Manager
*m
) {
174 /* test-execute runs under MANAGER_USER which, by default, forwards all
175 * variables present in the environment, but only those that are
176 * present _at the time it is created_!
178 * So these PassEnvironment checks are still expected to work, since we
179 * are ensuring the variables are not present at manager creation (they
180 * are unset explicitly in main) and are only set here.
182 * This is still a good approximation of how a test for MANAGER_SYSTEM
185 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
186 assert_se(setenv("VAR2", "word3", 1) == 0);
187 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
188 test(m
, "exec-passenvironment.service", 0, CLD_EXITED
);
189 test(m
, "exec-passenvironment-repeated.service", 0, CLD_EXITED
);
190 test(m
, "exec-passenvironment-empty.service", 0, CLD_EXITED
);
191 assert_se(unsetenv("VAR1") == 0);
192 assert_se(unsetenv("VAR2") == 0);
193 assert_se(unsetenv("VAR3") == 0);
194 test(m
, "exec-passenvironment-absent.service", 0, CLD_EXITED
);
197 static void test_exec_umask(Manager
*m
) {
198 test(m
, "exec-umask-default.service", 0, CLD_EXITED
);
199 test(m
, "exec-umask-0177.service", 0, CLD_EXITED
);
202 static void test_exec_runtimedirectory(Manager
*m
) {
203 test(m
, "exec-runtimedirectory.service", 0, CLD_EXITED
);
204 test(m
, "exec-runtimedirectory-mode.service", 0, CLD_EXITED
);
205 if (getgrnam("nobody"))
206 test(m
, "exec-runtimedirectory-owner.service", 0, CLD_EXITED
);
208 log_error_errno(errno
, "Skipping test_exec_runtimedirectory-owner, could not find nobody group: %m");
211 static void test_exec_capabilityboundingset(Manager
*m
) {
214 /* We use capsh to test if the capabilities are
215 * properly set, so be sure that it exists */
216 r
= find_binary("capsh", NULL
);
218 log_error_errno(r
, "Skipping test_exec_capabilityboundingset, could not find capsh binary: %m");
222 test(m
, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED
);
223 test(m
, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED
);
224 test(m
, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED
);
225 test(m
, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED
);
228 static void test_exec_capabilityambientset(Manager
*m
) {
231 /* Check if the kernel has support for ambient capabilities. Run
232 * the tests only if that's the case. Clearing all ambient
233 * capabilities is fine, since we are expecting them to be unset
234 * in the first place for the tests. */
235 r
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_CLEAR_ALL
, 0, 0, 0);
236 if (r
>= 0 || errno
!= EINVAL
) {
237 test(m
, "exec-capabilityambientset.service", 0, CLD_EXITED
);
238 test(m
, "exec-capabilityambientset-merge.service", 0, CLD_EXITED
);
242 static void test_exec_privatenetwork(Manager
*m
) {
245 r
= find_binary("ip", NULL
);
247 log_error_errno(r
, "Skipping test_exec_privatenetwork, could not find ip binary: %m");
251 test(m
, "exec-privatenetwork-yes.service", 0, CLD_EXITED
);
254 static void test_exec_oomscoreadjust(Manager
*m
) {
255 test(m
, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED
);
256 test(m
, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED
);
259 static void test_exec_ioschedulingclass(Manager
*m
) {
260 test(m
, "exec-ioschedulingclass-none.service", 0, CLD_EXITED
);
261 test(m
, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED
);
262 test(m
, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED
);
263 test(m
, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED
);
266 int main(int argc
, char *argv
[]) {
267 test_function_t tests
[] = {
268 test_exec_workingdirectory
,
269 test_exec_personality
,
270 test_exec_ignoresigpipe
,
271 test_exec_privatetmp
,
272 test_exec_privatedevices
,
273 test_exec_privatenetwork
,
274 test_exec_systemcallfilter
,
275 test_exec_systemcallerrornumber
,
278 test_exec_environment
,
279 test_exec_environmentfile
,
280 test_exec_passenvironment
,
282 test_exec_runtimedirectory
,
283 test_exec_capabilityboundingset
,
284 test_exec_capabilityambientset
,
285 test_exec_oomscoreadjust
,
286 test_exec_ioschedulingclass
,
289 test_function_t
*test
= NULL
;
293 log_parse_environment();
296 /* It is needed otherwise cgroup creation fails */
298 printf("Skipping test: not root\n");
299 return EXIT_TEST_SKIP
;
302 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
303 assert_se(set_unit_path(TEST_DIR
"/test-execute/") >= 0);
305 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
306 * cases, otherwise (and if they are present in the environment),
307 * `manager_default_environment` will copy them into the default
308 * environment which is passed to each created job, which will make the
309 * tests that expect those not to be present to fail.
311 assert_se(unsetenv("VAR1") == 0);
312 assert_se(unsetenv("VAR2") == 0);
313 assert_se(unsetenv("VAR3") == 0);
315 r
= manager_new(MANAGER_USER
, true, &m
);
316 if (MANAGER_SKIP_TEST(r
)) {
317 printf("Skipping test: manager_new: %s\n", strerror(-r
));
318 return EXIT_TEST_SKIP
;
321 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
323 for (test
= tests
; test
&& *test
; test
++)