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