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