]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-execute.c
util-lib: move a number of fs operations into fs-util.[ch]
[thirdparty/systemd.git] / src / test / test-execute.c
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
20 #include <stdio.h>
21
22 #include "fs-util.h"
23 #include "macro.h"
24 #include "manager.h"
25 #include "mkdir.h"
26 #include "rm-rf.h"
27 #include "unit.h"
28 #include "util.h"
29
30 typedef void (*test_function_t)(Manager *m);
31
32 static void check(Manager *m, Unit *unit, int status_expected, int code_expected) {
33 Service *service = NULL;
34 usec_t ts;
35 usec_t timeout = 2 * USEC_PER_SEC;
36
37 assert_se(m);
38 assert_se(unit);
39
40 service = SERVICE(unit);
41 printf("%s\n", unit->id);
42 exec_context_dump(&service->exec_context, stdout, "\t");
43 ts = now(CLOCK_MONOTONIC);
44 while (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED) {
45 int r;
46 usec_t n;
47
48 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
49 assert_se(r >= 0);
50
51 n = now(CLOCK_MONOTONIC);
52 if (ts + timeout < n) {
53 log_error("Test timeout when testing %s", unit->id);
54 exit(EXIT_FAILURE);
55 }
56 }
57 exec_status_dump(&service->main_exec_status, stdout, "\t");
58 assert_se(service->main_exec_status.status == status_expected);
59 assert_se(service->main_exec_status.code == code_expected);
60 }
61
62 static void test(Manager *m, const char *unit_name, int status_expected, int code_expected) {
63 Unit *unit;
64
65 assert_se(unit_name);
66
67 assert_se(manager_load_unit(m, unit_name, NULL, NULL, &unit) >= 0);
68 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
69 check(m, unit, status_expected, code_expected);
70 }
71
72 static void test_exec_workingdirectory(Manager *m) {
73 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
74
75 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
76
77 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
78 }
79
80 static void test_exec_personality(Manager *m) {
81 #if defined(__x86_64__)
82 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
83
84 #elif defined(__s390__)
85 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
86
87 #else
88 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
89 #endif
90 }
91
92 static void test_exec_ignoresigpipe(Manager *m) {
93 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
94 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
95 }
96
97 static void test_exec_privatetmp(Manager *m) {
98 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
99
100 test(m, "exec-privatetmp-yes.service", 0, CLD_EXITED);
101 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
102
103 unlink("/tmp/test-exec_privatetmp");
104 }
105
106 static void test_exec_privatedevices(Manager *m) {
107 test(m, "exec-privatedevices-yes.service", 0, CLD_EXITED);
108 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
109 }
110
111 static void test_exec_systemcallfilter(Manager *m) {
112 #ifdef HAVE_SECCOMP
113 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
114 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
115 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
116 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
117 #endif
118 }
119
120 static void test_exec_systemcallerrornumber(Manager *m) {
121 #ifdef HAVE_SECCOMP
122 test(m, "exec-systemcallerrornumber.service", 1, CLD_EXITED);
123 #endif
124 }
125
126 static void test_exec_user(Manager *m) {
127 test(m, "exec-user.service", 0, CLD_EXITED);
128 }
129
130 static void test_exec_group(Manager *m) {
131 test(m, "exec-group.service", 0, CLD_EXITED);
132 }
133
134 static void test_exec_environment(Manager *m) {
135 test(m, "exec-environment.service", 0, CLD_EXITED);
136 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
137 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
138 }
139
140 static void test_exec_umask(Manager *m) {
141 test(m, "exec-umask-default.service", 0, CLD_EXITED);
142 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
143 }
144
145 static void test_exec_runtimedirectory(Manager *m) {
146 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
147 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
148 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
149 }
150
151 int main(int argc, char *argv[]) {
152 test_function_t tests[] = {
153 test_exec_workingdirectory,
154 test_exec_personality,
155 test_exec_ignoresigpipe,
156 test_exec_privatetmp,
157 test_exec_privatedevices,
158 test_exec_systemcallfilter,
159 test_exec_systemcallerrornumber,
160 test_exec_user,
161 test_exec_group,
162 test_exec_environment,
163 test_exec_umask,
164 test_exec_runtimedirectory,
165 NULL,
166 };
167 test_function_t *test = NULL;
168 Manager *m = NULL;
169 int r;
170
171 log_parse_environment();
172 log_open();
173
174 /* It is needed otherwise cgroup creation fails */
175 if (getuid() != 0) {
176 printf("Skipping test: not root\n");
177 return EXIT_TEST_SKIP;
178 }
179
180 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
181 assert_se(set_unit_path(TEST_DIR) >= 0);
182
183 r = manager_new(MANAGER_USER, true, &m);
184 if (IN_SET(r, -EPERM, -EACCES, -EADDRINUSE, -EHOSTDOWN, -ENOENT)) {
185 printf("Skipping test: manager_new: %s", strerror(-r));
186 return EXIT_TEST_SKIP;
187 }
188 assert_se(r >= 0);
189 assert_se(manager_startup(m, NULL, NULL) >= 0);
190
191 for (test = tests; test && *test; test++)
192 (*test)(m);
193
194 manager_free(m);
195
196 return 0;
197 }