]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5cb5a6ff | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
5cb5a6ff | 22 | #include <errno.h> |
034c6ed7 | 23 | #include <signal.h> |
2c4104f0 LP |
24 | #include <dirent.h> |
25 | #include <unistd.h> | |
4b939747 | 26 | #include <sys/reboot.h> |
efe6e7d3 MO |
27 | #include <linux/reboot.h> |
28 | #include <sys/syscall.h> | |
5cb5a6ff | 29 | |
574634bc | 30 | #include "async.h" |
4b939747 | 31 | #include "manager.h" |
87f0e418 | 32 | #include "unit.h" |
5cb5a6ff LP |
33 | #include "service.h" |
34 | #include "load-fragment.h" | |
35 | #include "load-dropin.h" | |
034c6ed7 | 36 | #include "log.h" |
2c4104f0 | 37 | #include "strv.h" |
9e2f7c11 | 38 | #include "unit-name.h" |
41f9172f | 39 | #include "unit-printf.h" |
4139c1b2 | 40 | #include "dbus-service.h" |
514f4ef5 | 41 | #include "special.h" |
9a57c629 | 42 | #include "exit-status.h" |
f6a6225e | 43 | #include "def.h" |
9eb977db | 44 | #include "path-util.h" |
f6a6225e | 45 | #include "util.h" |
7f110ff9 | 46 | #include "utf8.h" |
4d1a6904 | 47 | #include "env-util.h" |
a5c32cff | 48 | #include "fileio.h" |
718db961 LP |
49 | #include "bus-error.h" |
50 | #include "bus-util.h" | |
034c6ed7 | 51 | |
acbb0225 | 52 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
87f0e418 LP |
53 | [SERVICE_DEAD] = UNIT_INACTIVE, |
54 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
55 | [SERVICE_START] = UNIT_ACTIVATING, | |
56 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
57 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
80876c20 | 58 | [SERVICE_EXITED] = UNIT_ACTIVE, |
032ff4af | 59 | [SERVICE_RELOAD] = UNIT_RELOADING, |
87f0e418 LP |
60 | [SERVICE_STOP] = UNIT_DEACTIVATING, |
61 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
62 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
63 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
64 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
65 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 66 | [SERVICE_FAILED] = UNIT_FAILED, |
6124958c | 67 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING |
034c6ed7 | 68 | }; |
5cb5a6ff | 69 | |
e056b01d LP |
70 | /* For Type=idle we never want to delay any other jobs, hence we |
71 | * consider idle jobs active as soon as we start working on them */ | |
72 | static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = { | |
73 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
74 | [SERVICE_START_PRE] = UNIT_ACTIVE, | |
75 | [SERVICE_START] = UNIT_ACTIVE, | |
76 | [SERVICE_START_POST] = UNIT_ACTIVE, | |
77 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
78 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
79 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
80 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
81 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
82 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
83 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
84 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
85 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
86 | [SERVICE_FAILED] = UNIT_FAILED, | |
87 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING | |
88 | }; | |
89 | ||
718db961 LP |
90 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
91 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); | |
92 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata); | |
93 | ||
842129f5 LP |
94 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f); |
95 | ||
a16e1123 LP |
96 | static void service_init(Unit *u) { |
97 | Service *s = SERVICE(u); | |
98 | ||
99 | assert(u); | |
ac155bb8 | 100 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 101 | |
1f19a534 OS |
102 | s->timeout_start_usec = u->manager->default_timeout_start_usec; |
103 | s->timeout_stop_usec = u->manager->default_timeout_stop_usec; | |
104 | s->restart_usec = u->manager->default_restart_usec; | |
0b86feac | 105 | s->type = _SERVICE_TYPE_INVALID; |
a16e1123 | 106 | s->socket_fd = -1; |
3185a36b | 107 | s->guess_main_pid = true; |
a16e1123 | 108 | |
085afe36 | 109 | RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst); |
a16e1123 LP |
110 | |
111 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
112 | } | |
113 | ||
5e94833f LP |
114 | static void service_unwatch_control_pid(Service *s) { |
115 | assert(s); | |
116 | ||
117 | if (s->control_pid <= 0) | |
118 | return; | |
119 | ||
120 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
121 | s->control_pid = 0; | |
122 | } | |
123 | ||
124 | static void service_unwatch_main_pid(Service *s) { | |
125 | assert(s); | |
126 | ||
127 | if (s->main_pid <= 0) | |
128 | return; | |
129 | ||
130 | unit_unwatch_pid(UNIT(s), s->main_pid); | |
131 | s->main_pid = 0; | |
132 | } | |
133 | ||
3e52541e MS |
134 | static void service_unwatch_pid_file(Service *s) { |
135 | if (!s->pid_file_pathspec) | |
136 | return; | |
137 | ||
66870f90 ZJS |
138 | log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s", |
139 | UNIT(s)->id, s->pid_file_pathspec->path); | |
718db961 | 140 | path_spec_unwatch(s->pid_file_pathspec); |
3e52541e MS |
141 | path_spec_done(s->pid_file_pathspec); |
142 | free(s->pid_file_pathspec); | |
143 | s->pid_file_pathspec = NULL; | |
144 | } | |
145 | ||
5925dd3c | 146 | static int service_set_main_pid(Service *s, pid_t pid) { |
e55224ca LP |
147 | pid_t ppid; |
148 | ||
5925dd3c LP |
149 | assert(s); |
150 | ||
151 | if (pid <= 1) | |
152 | return -EINVAL; | |
153 | ||
154 | if (pid == getpid()) | |
155 | return -EINVAL; | |
156 | ||
7400b9d2 LP |
157 | if (s->main_pid == pid && s->main_pid_known) |
158 | return 0; | |
159 | ||
160 | if (s->main_pid != pid) { | |
161 | service_unwatch_main_pid(s); | |
162 | exec_status_start(&s->main_exec_status, pid); | |
163 | } | |
41efeaec | 164 | |
6dfa5494 LP |
165 | s->main_pid = pid; |
166 | s->main_pid_known = true; | |
167 | ||
168 | if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) { | |
66870f90 | 169 | log_warning_unit(UNIT(s)->id, |
ccd06097 ZJS |
170 | "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", |
171 | UNIT(s)->id, pid); | |
e55224ca | 172 | |
6dfa5494 LP |
173 | s->main_pid_alien = true; |
174 | } else | |
175 | s->main_pid_alien = false; | |
5925dd3c LP |
176 | |
177 | return 0; | |
178 | } | |
179 | ||
4f2d528d LP |
180 | static void service_close_socket_fd(Service *s) { |
181 | assert(s); | |
182 | ||
183 | if (s->socket_fd < 0) | |
184 | return; | |
185 | ||
574634bc | 186 | s->socket_fd = asynchronous_close(s->socket_fd); |
4f2d528d LP |
187 | } |
188 | ||
6cf6bbc2 LP |
189 | static void service_connection_unref(Service *s) { |
190 | assert(s); | |
191 | ||
9444b1f2 | 192 | if (!UNIT_ISSET(s->accept_socket)) |
6cf6bbc2 LP |
193 | return; |
194 | ||
57020a3a LP |
195 | socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket))); |
196 | unit_ref_unset(&s->accept_socket); | |
6cf6bbc2 LP |
197 | } |
198 | ||
a6927d7f MO |
199 | static void service_stop_watchdog(Service *s) { |
200 | assert(s); | |
201 | ||
718db961 | 202 | s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source); |
842129f5 | 203 | s->watchdog_timestamp = DUAL_TIMESTAMP_NULL; |
a6927d7f MO |
204 | } |
205 | ||
842129f5 | 206 | static void service_start_watchdog(Service *s) { |
bb242b7b MO |
207 | int r; |
208 | ||
209 | assert(s); | |
210 | ||
842129f5 | 211 | if (s->watchdog_usec <= 0) |
bb242b7b MO |
212 | return; |
213 | ||
718db961 LP |
214 | if (s->watchdog_event_source) { |
215 | r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec); | |
216 | if (r < 0) { | |
217 | log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r)); | |
218 | return; | |
219 | } | |
220 | ||
842129f5 | 221 | r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT); |
c4ef3317 | 222 | } else { |
6a0f1f6d LP |
223 | r = sd_event_add_time( |
224 | UNIT(s)->manager->event, | |
225 | &s->watchdog_event_source, | |
226 | CLOCK_MONOTONIC, | |
227 | s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, | |
228 | service_dispatch_watchdog, s); | |
c4ef3317 LP |
229 | if (r < 0) { |
230 | log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r)); | |
231 | return; | |
232 | } | |
233 | ||
234 | /* Let's process everything else which might be a sign | |
235 | * of living before we consider a service died. */ | |
236 | r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE); | |
237 | } | |
718db961 | 238 | |
bb242b7b | 239 | if (r < 0) |
842129f5 | 240 | log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r)); |
bb242b7b MO |
241 | } |
242 | ||
a6927d7f MO |
243 | static void service_reset_watchdog(Service *s) { |
244 | assert(s); | |
245 | ||
246 | dual_timestamp_get(&s->watchdog_timestamp); | |
842129f5 | 247 | service_start_watchdog(s); |
a6927d7f MO |
248 | } |
249 | ||
87f0e418 LP |
250 | static void service_done(Unit *u) { |
251 | Service *s = SERVICE(u); | |
44d8db9e LP |
252 | |
253 | assert(s); | |
254 | ||
255 | free(s->pid_file); | |
256 | s->pid_file = NULL; | |
257 | ||
8c47c732 LP |
258 | free(s->status_text); |
259 | s->status_text = NULL; | |
260 | ||
efe6e7d3 MO |
261 | free(s->reboot_arg); |
262 | s->reboot_arg = NULL; | |
263 | ||
613b411c | 264 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); |
e537352b | 265 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
44d8db9e | 266 | s->control_command = NULL; |
867b3b7d | 267 | s->main_command = NULL; |
44d8db9e | 268 | |
37520c1b LP |
269 | exit_status_set_free(&s->restart_prevent_status); |
270 | exit_status_set_free(&s->restart_force_status); | |
271 | exit_status_set_free(&s->success_status); | |
96342de6 | 272 | |
44d8db9e LP |
273 | /* This will leak a process, but at least no memory or any of |
274 | * our resources */ | |
5e94833f LP |
275 | service_unwatch_main_pid(s); |
276 | service_unwatch_control_pid(s); | |
3e52541e | 277 | service_unwatch_pid_file(s); |
44d8db9e | 278 | |
05e343b7 | 279 | if (s->bus_name) { |
ac155bb8 | 280 | unit_unwatch_bus_name(u, s->bus_name); |
05e343b7 LP |
281 | free(s->bus_name); |
282 | s->bus_name = NULL; | |
283 | } | |
284 | ||
4f2d528d | 285 | service_close_socket_fd(s); |
6cf6bbc2 | 286 | service_connection_unref(s); |
4f2d528d | 287 | |
57020a3a | 288 | unit_ref_unset(&s->accept_socket); |
f976f3f6 | 289 | |
bb242b7b MO |
290 | service_stop_watchdog(s); |
291 | ||
718db961 LP |
292 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
293 | } | |
294 | ||
295 | static int service_arm_timer(Service *s, usec_t usec) { | |
296 | int r; | |
297 | ||
298 | assert(s); | |
299 | ||
718db961 LP |
300 | if (s->timer_event_source) { |
301 | r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec); | |
302 | if (r < 0) | |
303 | return r; | |
304 | ||
305 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
306 | } | |
307 | ||
6a0f1f6d LP |
308 | return sd_event_add_time( |
309 | UNIT(s)->manager->event, | |
310 | &s->timer_event_source, | |
311 | CLOCK_MONOTONIC, | |
312 | now(CLOCK_MONOTONIC) + usec, 0, | |
313 | service_dispatch_timer, s); | |
44d8db9e LP |
314 | } |
315 | ||
243b1432 LP |
316 | static int service_verify(Service *s) { |
317 | assert(s); | |
318 | ||
1124fe6f | 319 | if (UNIT(s)->load_state != UNIT_LOADED) |
243b1432 LP |
320 | return 0; |
321 | ||
322 | if (!s->exec_command[SERVICE_EXEC_START]) { | |
ebc2259d | 323 | log_error_unit(UNIT(s)->id, "%s lacks ExecStart setting. Refusing.", UNIT(s)->id); |
243b1432 LP |
324 | return -EINVAL; |
325 | } | |
326 | ||
34e9ba66 LP |
327 | if (s->type != SERVICE_ONESHOT && |
328 | s->exec_command[SERVICE_EXEC_START]->command_next) { | |
ebc2259d | 329 | log_error_unit(UNIT(s)->id, "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id); |
6cf6bbc2 LP |
330 | return -EINVAL; |
331 | } | |
332 | ||
b0693d30 | 333 | if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) { |
37520c1b LP |
334 | log_error_unit(UNIT(s)->id, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id); |
335 | return -EINVAL; | |
336 | } | |
337 | ||
55ebf98c | 338 | if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) { |
37520c1b | 339 | log_error_unit(UNIT(s)->id, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id); |
b0693d30 MW |
340 | return -EINVAL; |
341 | } | |
342 | ||
05e343b7 | 343 | if (s->type == SERVICE_DBUS && !s->bus_name) { |
ebc2259d | 344 | log_error_unit(UNIT(s)->id, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id); |
4d0e5dbd LP |
345 | return -EINVAL; |
346 | } | |
347 | ||
7e2668c6 | 348 | if (s->bus_name && s->type != SERVICE_DBUS) |
ebc2259d | 349 | log_warning_unit(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id); |
7e2668c6 | 350 | |
ebc2259d LP |
351 | if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) { |
352 | log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id); | |
05e343b7 LP |
353 | return -EINVAL; |
354 | } | |
355 | ||
243b1432 LP |
356 | return 0; |
357 | } | |
358 | ||
a40eb732 LP |
359 | static int service_add_default_dependencies(Service *s) { |
360 | int r; | |
361 | ||
362 | assert(s); | |
363 | ||
364 | /* Add a number of automatic dependencies useful for the | |
365 | * majority of services. */ | |
366 | ||
367 | /* First, pull in base system */ | |
fccd44ec KS |
368 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, |
369 | SPECIAL_BASIC_TARGET, NULL, true); | |
370 | if (r < 0) | |
371 | return r; | |
a40eb732 LP |
372 | |
373 | /* Second, activate normal shutdown */ | |
117dcc57 ZJS |
374 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, |
375 | SPECIAL_SHUTDOWN_TARGET, NULL, true); | |
376 | return r; | |
a40eb732 LP |
377 | } |
378 | ||
4dfc092a LP |
379 | static void service_fix_output(Service *s) { |
380 | assert(s); | |
381 | ||
382 | /* If nothing has been explicitly configured, patch default | |
383 | * output in. If input is socket/tty we avoid this however, | |
384 | * since in that case we want output to default to the same | |
385 | * place as we read input from. */ | |
386 | ||
387 | if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT && | |
388 | s->exec_context.std_output == EXEC_OUTPUT_INHERIT && | |
389 | s->exec_context.std_input == EXEC_INPUT_NULL) | |
1124fe6f | 390 | s->exec_context.std_error = UNIT(s)->manager->default_std_error; |
4dfc092a LP |
391 | |
392 | if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT && | |
393 | s->exec_context.std_input == EXEC_INPUT_NULL) | |
1124fe6f | 394 | s->exec_context.std_output = UNIT(s)->manager->default_std_output; |
4dfc092a LP |
395 | } |
396 | ||
e537352b LP |
397 | static int service_load(Unit *u) { |
398 | int r; | |
399 | Service *s = SERVICE(u); | |
400 | ||
401 | assert(s); | |
1e2e8133 | 402 | |
5cb5a6ff | 403 | /* Load a .service file */ |
c2756a68 LP |
404 | r = unit_load_fragment(u); |
405 | if (r < 0) | |
5cb5a6ff LP |
406 | return r; |
407 | ||
23a177ef | 408 | /* Still nothing found? Then let's give up */ |
ac155bb8 | 409 | if (u->load_state == UNIT_STUB) |
23a177ef | 410 | return -ENOENT; |
034c6ed7 | 411 | |
23a177ef | 412 | /* This is a new unit? Then let's add in some extras */ |
ac155bb8 | 413 | if (u->load_state == UNIT_LOADED) { |
c2756a68 LP |
414 | |
415 | /* We were able to load something, then let's add in | |
416 | * the dropin directories. */ | |
417 | r = unit_load_dropin(u); | |
418 | if (r < 0) | |
419 | return r; | |
420 | ||
0b86feac LP |
421 | if (s->type == _SERVICE_TYPE_INVALID) |
422 | s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE; | |
423 | ||
d568a335 MS |
424 | /* Oneshot services have disabled start timeout by default */ |
425 | if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined) | |
426 | s->timeout_start_usec = 0; | |
98709151 | 427 | |
4e2b0f9b LP |
428 | service_fix_output(s); |
429 | ||
598459ce LP |
430 | r = unit_patch_contexts(u); |
431 | if (r < 0) | |
432 | return r; | |
433 | ||
117dcc57 ZJS |
434 | r = unit_add_exec_dependencies(u, &s->exec_context); |
435 | if (r < 0) | |
23a177ef LP |
436 | return r; |
437 | ||
598459ce | 438 | r = unit_add_default_slice(u, &s->cgroup_context); |
a016b922 LP |
439 | if (r < 0) |
440 | return r; | |
441 | ||
c952c6ec LP |
442 | if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE) |
443 | s->notify_access = NOTIFY_MAIN; | |
a40eb732 | 444 | |
02c4ef9c LP |
445 | if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE) |
446 | s->notify_access = NOTIFY_MAIN; | |
447 | ||
598459ce LP |
448 | if (s->bus_name) { |
449 | r = unit_watch_bus_name(u, s->bus_name); | |
117dcc57 | 450 | if (r < 0) |
a40eb732 | 451 | return r; |
117dcc57 | 452 | } |
e06c73cc | 453 | |
598459ce LP |
454 | if (u->default_dependencies) { |
455 | r = service_add_default_dependencies(s); | |
456 | if (r < 0) | |
817e224b | 457 | |
598459ce LP |
458 | return r; |
459 | } | |
8e274523 LP |
460 | } |
461 | ||
243b1432 | 462 | return service_verify(s); |
034c6ed7 LP |
463 | } |
464 | ||
87f0e418 | 465 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 466 | |
5cb5a6ff | 467 | ServiceExecCommand c; |
87f0e418 | 468 | Service *s = SERVICE(u); |
47be870b | 469 | const char *prefix2; |
7fd1b19b | 470 | _cleanup_free_ char *p2 = NULL; |
5cb5a6ff LP |
471 | |
472 | assert(s); | |
473 | ||
47be870b LP |
474 | p2 = strappend(prefix, "\t"); |
475 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e | 476 | |
5cb5a6ff | 477 | fprintf(f, |
81a2b7ce | 478 | "%sService State: %s\n" |
f42806df LP |
479 | "%sResult: %s\n" |
480 | "%sReload Result: %s\n" | |
81a2b7ce | 481 | "%sPermissionsStartOnly: %s\n" |
8e274523 | 482 | "%sRootDirectoryStartOnly: %s\n" |
02ee865a | 483 | "%sRemainAfterExit: %s\n" |
3185a36b | 484 | "%sGuessMainPID: %s\n" |
c952c6ec | 485 | "%sType: %s\n" |
2cf3143a | 486 | "%sRestart: %s\n" |
c952c6ec | 487 | "%sNotifyAccess: %s\n", |
81a2b7ce | 488 | prefix, service_state_to_string(s->state), |
f42806df LP |
489 | prefix, service_result_to_string(s->result), |
490 | prefix, service_result_to_string(s->reload_result), | |
81a2b7ce | 491 | prefix, yes_no(s->permissions_start_only), |
8e274523 | 492 | prefix, yes_no(s->root_directory_start_only), |
02ee865a | 493 | prefix, yes_no(s->remain_after_exit), |
3185a36b | 494 | prefix, yes_no(s->guess_main_pid), |
c952c6ec | 495 | prefix, service_type_to_string(s->type), |
2cf3143a | 496 | prefix, service_restart_to_string(s->restart), |
c952c6ec | 497 | prefix, notify_access_to_string(s->notify_access)); |
5cb5a6ff | 498 | |
70123e68 LP |
499 | if (s->control_pid > 0) |
500 | fprintf(f, | |
ccd06097 ZJS |
501 | "%sControl PID: "PID_FMT"\n", |
502 | prefix, s->control_pid); | |
70123e68 LP |
503 | |
504 | if (s->main_pid > 0) | |
505 | fprintf(f, | |
ccd06097 | 506 | "%sMain PID: "PID_FMT"\n" |
6dfa5494 LP |
507 | "%sMain PID Known: %s\n" |
508 | "%sMain PID Alien: %s\n", | |
ccd06097 | 509 | prefix, s->main_pid, |
6dfa5494 LP |
510 | prefix, yes_no(s->main_pid_known), |
511 | prefix, yes_no(s->main_pid_alien)); | |
70123e68 | 512 | |
034c6ed7 LP |
513 | if (s->pid_file) |
514 | fprintf(f, | |
515 | "%sPIDFile: %s\n", | |
516 | prefix, s->pid_file); | |
517 | ||
05e343b7 LP |
518 | if (s->bus_name) |
519 | fprintf(f, | |
520 | "%sBusName: %s\n" | |
521 | "%sBus Name Good: %s\n", | |
522 | prefix, s->bus_name, | |
523 | prefix, yes_no(s->bus_name_good)); | |
524 | ||
4819ff03 | 525 | kill_context_dump(&s->kill_context, f, prefix); |
5cb5a6ff LP |
526 | exec_context_dump(&s->exec_context, f, prefix); |
527 | ||
e537352b | 528 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
5cb5a6ff | 529 | |
44d8db9e LP |
530 | if (!s->exec_command[c]) |
531 | continue; | |
532 | ||
40d50879 | 533 | fprintf(f, "%s-> %s:\n", |
94f04347 | 534 | prefix, service_exec_command_to_string(c)); |
44d8db9e LP |
535 | |
536 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 537 | } |
44d8db9e | 538 | |
07459bb6 | 539 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 LP |
540 | if (s->sysv_start_priority >= 0) |
541 | fprintf(f, | |
9fff8a1f LP |
542 | "%sSysVStartPriority: %i\n", |
543 | prefix, s->sysv_start_priority); | |
07459bb6 | 544 | #endif |
23a177ef | 545 | |
8c47c732 LP |
546 | if (s->status_text) |
547 | fprintf(f, "%sStatus Text: %s\n", | |
548 | prefix, s->status_text); | |
5cb5a6ff LP |
549 | } |
550 | ||
c5419d42 | 551 | static int service_load_pid_file(Service *s, bool may_warn) { |
7fd1b19b | 552 | _cleanup_free_ char *k = NULL; |
034c6ed7 | 553 | int r; |
5925dd3c | 554 | pid_t pid; |
034c6ed7 LP |
555 | |
556 | assert(s); | |
557 | ||
034c6ed7 | 558 | if (!s->pid_file) |
13230d5d | 559 | return -ENOENT; |
034c6ed7 | 560 | |
117dcc57 ZJS |
561 | r = read_one_line_file(s->pid_file, &k); |
562 | if (r < 0) { | |
c5419d42 | 563 | if (may_warn) |
66870f90 ZJS |
564 | log_info_unit(UNIT(s)->id, |
565 | "PID file %s not readable (yet?) after %s.", | |
566 | s->pid_file, service_state_to_string(s->state)); | |
034c6ed7 | 567 | return r; |
5375410b | 568 | } |
034c6ed7 | 569 | |
5925dd3c | 570 | r = parse_pid(k, &pid); |
bc41f93e ZJS |
571 | if (r < 0) { |
572 | if (may_warn) | |
573 | log_info_unit(UNIT(s)->id, | |
574 | "Failed to read PID from file %s: %s", | |
575 | s->pid_file, strerror(-r)); | |
5925dd3c | 576 | return r; |
bc41f93e | 577 | } |
406eaf93 | 578 | |
9f5650ae | 579 | if (!pid_is_alive(pid)) { |
c5419d42 | 580 | if (may_warn) |
9f5650ae | 581 | log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file); |
b8c597d5 | 582 | |
e10c9985 YS |
583 | return -ESRCH; |
584 | } | |
585 | ||
db01f8b3 MS |
586 | if (s->main_pid_known) { |
587 | if (pid == s->main_pid) | |
588 | return 0; | |
589 | ||
66870f90 | 590 | log_debug_unit(UNIT(s)->id, |
ccd06097 ZJS |
591 | "Main PID changing: "PID_FMT" -> "PID_FMT, |
592 | s->main_pid, pid); | |
db01f8b3 MS |
593 | service_unwatch_main_pid(s); |
594 | s->main_pid_known = false; | |
3a111838 | 595 | } else |
66870f90 | 596 | log_debug_unit(UNIT(s)->id, |
ccd06097 | 597 | "Main PID loaded: "PID_FMT, pid); |
db01f8b3 | 598 | |
117dcc57 ZJS |
599 | r = service_set_main_pid(s, pid); |
600 | if (r < 0) | |
16f6025e LP |
601 | return r; |
602 | ||
117dcc57 | 603 | r = unit_watch_pid(UNIT(s), pid); |
bc41f93e | 604 | if (r < 0) { |
5925dd3c | 605 | /* FIXME: we need to do something here */ |
bc41f93e | 606 | log_warning_unit(UNIT(s)->id, |
ccd06097 ZJS |
607 | "Failed to watch PID "PID_FMT" from service %s", |
608 | pid, UNIT(s)->id); | |
5925dd3c | 609 | return r; |
bc41f93e | 610 | } |
034c6ed7 LP |
611 | |
612 | return 0; | |
613 | } | |
614 | ||
4fbf50b3 LP |
615 | static int service_search_main_pid(Service *s) { |
616 | pid_t pid; | |
617 | int r; | |
618 | ||
619 | assert(s); | |
620 | ||
3185a36b LP |
621 | /* If we know it anyway, don't ever fallback to unreliable |
622 | * heuristics */ | |
4fbf50b3 LP |
623 | if (s->main_pid_known) |
624 | return 0; | |
625 | ||
3185a36b LP |
626 | if (!s->guess_main_pid) |
627 | return 0; | |
628 | ||
4fbf50b3 LP |
629 | assert(s->main_pid <= 0); |
630 | ||
4ad49000 | 631 | pid = unit_search_main_pid(UNIT(s)); |
117dcc57 | 632 | if (pid <= 0) |
4fbf50b3 LP |
633 | return -ENOENT; |
634 | ||
66870f90 | 635 | log_debug_unit(UNIT(s)->id, |
ccd06097 | 636 | "Main PID guessed: "PID_FMT, pid); |
117dcc57 ZJS |
637 | r = service_set_main_pid(s, pid); |
638 | if (r < 0) | |
4fbf50b3 LP |
639 | return r; |
640 | ||
117dcc57 ZJS |
641 | r = unit_watch_pid(UNIT(s), pid); |
642 | if (r < 0) | |
4fbf50b3 | 643 | /* FIXME: we need to do something here */ |
a827e373 | 644 | log_warning_unit(UNIT(s)->id, |
ccd06097 ZJS |
645 | "Failed to watch PID "PID_FMT" from service %s", |
646 | pid, UNIT(s)->id); | |
bdd13f6b | 647 | return r; |
4fbf50b3 LP |
648 | } |
649 | ||
034c6ed7 LP |
650 | static void service_set_state(Service *s, ServiceState state) { |
651 | ServiceState old_state; | |
e056b01d | 652 | const UnitActiveState *table; |
842129f5 | 653 | |
5cb5a6ff LP |
654 | assert(s); |
655 | ||
e056b01d LP |
656 | table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
657 | ||
034c6ed7 | 658 | old_state = s->state; |
5cb5a6ff | 659 | s->state = state; |
034c6ed7 | 660 | |
3a111838 MS |
661 | service_unwatch_pid_file(s); |
662 | ||
842129f5 LP |
663 | if (!IN_SET(state, |
664 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
665 | SERVICE_RELOAD, | |
bf108e55 LP |
666 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, |
667 | SERVICE_STOP_POST, | |
842129f5 LP |
668 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
669 | SERVICE_AUTO_RESTART)) | |
718db961 | 670 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
034c6ed7 | 671 | |
842129f5 LP |
672 | if (!IN_SET(state, |
673 | SERVICE_START, SERVICE_START_POST, | |
674 | SERVICE_RUNNING, SERVICE_RELOAD, | |
bf108e55 LP |
675 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, |
676 | SERVICE_STOP_POST, | |
677 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { | |
5e94833f | 678 | service_unwatch_main_pid(s); |
867b3b7d LP |
679 | s->main_command = NULL; |
680 | } | |
034c6ed7 | 681 | |
842129f5 LP |
682 | if (!IN_SET(state, |
683 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
684 | SERVICE_RELOAD, | |
bf108e55 LP |
685 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, |
686 | SERVICE_STOP_POST, | |
842129f5 | 687 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
5e94833f | 688 | service_unwatch_control_pid(s); |
034c6ed7 | 689 | s->control_command = NULL; |
a16e1123 | 690 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
e537352b | 691 | } |
034c6ed7 | 692 | |
a911bb9a LP |
693 | if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
694 | unit_unwatch_all_pids(UNIT(s)); | |
695 | ||
842129f5 LP |
696 | if (!IN_SET(state, |
697 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
698 | SERVICE_RUNNING, SERVICE_RELOAD, | |
699 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
700 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) && | |
1124fe6f | 701 | !(state == SERVICE_DEAD && UNIT(s)->job)) { |
4f2d528d | 702 | service_close_socket_fd(s); |
6cf6bbc2 LP |
703 | service_connection_unref(s); |
704 | } | |
4f2d528d | 705 | |
7596e9e1 | 706 | if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
a6927d7f MO |
707 | service_stop_watchdog(s); |
708 | ||
f6023656 LP |
709 | /* For the inactive states unit_notify() will trim the cgroup, |
710 | * but for exit we have to do that ourselves... */ | |
1124fe6f | 711 | if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0) |
4ad49000 | 712 | unit_destroy_cgroup(UNIT(s)); |
f6023656 | 713 | |
9cd86184 OB |
714 | /* For remain_after_exit services, let's see if we can "release" the |
715 | * hold on the console, since unit_notify() only does that in case of | |
716 | * change of state */ | |
717 | if (state == SERVICE_EXITED && s->remain_after_exit && | |
718 | UNIT(s)->manager->n_on_console > 0) { | |
719 | ExecContext *ec = unit_get_exec_context(UNIT(s)); | |
720 | if (ec && exec_context_may_touch_console(ec)) { | |
721 | Manager *m = UNIT(s)->manager; | |
722 | ||
723 | m->n_on_console --; | |
724 | if (m->n_on_console == 0) | |
725 | /* unset no_console_output flag, since the console is free */ | |
726 | m->no_console_output = false; | |
727 | } | |
728 | } | |
729 | ||
e537352b | 730 | if (old_state != state) |
842129f5 | 731 | log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state)); |
acbb0225 | 732 | |
e056b01d | 733 | unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS); |
f42806df | 734 | s->reload_result = SERVICE_SUCCESS; |
034c6ed7 LP |
735 | } |
736 | ||
a16e1123 LP |
737 | static int service_coldplug(Unit *u) { |
738 | Service *s = SERVICE(u); | |
739 | int r; | |
740 | ||
741 | assert(s); | |
742 | assert(s->state == SERVICE_DEAD); | |
743 | ||
744 | if (s->deserialized_state != s->state) { | |
745 | ||
bf108e55 LP |
746 | if (IN_SET(s->deserialized_state, |
747 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
748 | SERVICE_RELOAD, | |
749 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, | |
750 | SERVICE_STOP_POST, | |
751 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { | |
92c1622e LP |
752 | |
753 | usec_t k; | |
6c12b52e | 754 | |
bf108e55 | 755 | k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec; |
e558336f | 756 | |
92c1622e LP |
757 | /* For the start/stop timeouts 0 means off */ |
758 | if (k > 0) { | |
759 | r = service_arm_timer(s, k); | |
36697dc0 | 760 | if (r < 0) |
e558336f LP |
761 | return r; |
762 | } | |
763 | } | |
a16e1123 | 764 | |
92c1622e LP |
765 | if (s->deserialized_state == SERVICE_AUTO_RESTART) { |
766 | ||
767 | /* The restart timeouts 0 means immediately */ | |
768 | r = service_arm_timer(s, s->restart_usec); | |
769 | if (r < 0) | |
770 | return r; | |
771 | } | |
772 | ||
9f5650ae | 773 | if (pid_is_unwaited(s->main_pid) && |
bf108e55 LP |
774 | ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) || |
775 | IN_SET(s->deserialized_state, | |
776 | SERVICE_START, SERVICE_START_POST, | |
777 | SERVICE_RUNNING, SERVICE_RELOAD, | |
778 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, | |
779 | SERVICE_STOP_POST, | |
780 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) { | |
781 | r = unit_watch_pid(UNIT(s), s->main_pid); | |
782 | if (r < 0) | |
783 | return r; | |
784 | } | |
a16e1123 | 785 | |
9f5650ae | 786 | if (pid_is_unwaited(s->control_pid) && |
bf108e55 LP |
787 | IN_SET(s->deserialized_state, |
788 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
789 | SERVICE_RELOAD, | |
790 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, | |
791 | SERVICE_STOP_POST, | |
792 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { | |
793 | r = unit_watch_pid(UNIT(s), s->control_pid); | |
794 | if (r < 0) | |
795 | return r; | |
796 | } | |
a16e1123 | 797 | |
a911bb9a LP |
798 | if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
799 | unit_watch_all_pids(UNIT(s)); | |
800 | ||
842129f5 LP |
801 | if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
802 | service_start_watchdog(s); | |
bb242b7b | 803 | |
a16e1123 LP |
804 | service_set_state(s, s->deserialized_state); |
805 | } | |
92c1622e | 806 | |
a16e1123 LP |
807 | return 0; |
808 | } | |
809 | ||
44d8db9e LP |
810 | static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { |
811 | Iterator i; | |
812 | int r; | |
813 | int *rfds = NULL; | |
814 | unsigned rn_fds = 0; | |
57020a3a | 815 | Unit *u; |
44d8db9e LP |
816 | |
817 | assert(s); | |
818 | assert(fds); | |
819 | assert(n_fds); | |
820 | ||
6cf6bbc2 LP |
821 | if (s->socket_fd >= 0) |
822 | return 0; | |
823 | ||
1124fe6f | 824 | SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) { |
44d8db9e LP |
825 | int *cfds; |
826 | unsigned cn_fds; | |
57020a3a LP |
827 | Socket *sock; |
828 | ||
ac155bb8 | 829 | if (u->type != UNIT_SOCKET) |
57020a3a LP |
830 | continue; |
831 | ||
832 | sock = SOCKET(u); | |
44d8db9e | 833 | |
117dcc57 ZJS |
834 | r = socket_collect_fds(sock, &cfds, &cn_fds); |
835 | if (r < 0) | |
44d8db9e LP |
836 | goto fail; |
837 | ||
838 | if (!cfds) | |
839 | continue; | |
840 | ||
841 | if (!rfds) { | |
842 | rfds = cfds; | |
843 | rn_fds = cn_fds; | |
844 | } else { | |
845 | int *t; | |
846 | ||
117dcc57 ZJS |
847 | t = new(int, rn_fds+cn_fds); |
848 | if (!t) { | |
44d8db9e LP |
849 | free(cfds); |
850 | r = -ENOMEM; | |
851 | goto fail; | |
852 | } | |
853 | ||
9c1b183c LP |
854 | memcpy(t, rfds, rn_fds * sizeof(int)); |
855 | memcpy(t+rn_fds, cfds, cn_fds * sizeof(int)); | |
44d8db9e LP |
856 | free(rfds); |
857 | free(cfds); | |
858 | ||
859 | rfds = t; | |
860 | rn_fds = rn_fds+cn_fds; | |
861 | } | |
862 | } | |
863 | ||
864 | *fds = rfds; | |
865 | *n_fds = rn_fds; | |
3e33402a | 866 | |
44d8db9e LP |
867 | return 0; |
868 | ||
869 | fail: | |
870 | free(rfds); | |
3e33402a | 871 | |
44d8db9e LP |
872 | return r; |
873 | } | |
874 | ||
81a2b7ce LP |
875 | static int service_spawn( |
876 | Service *s, | |
877 | ExecCommand *c, | |
878 | bool timeout, | |
879 | bool pass_fds, | |
880 | bool apply_permissions, | |
881 | bool apply_chroot, | |
1e3ad081 | 882 | bool apply_tty_stdin, |
c952c6ec | 883 | bool set_notify_socket, |
ecedd90f | 884 | bool is_control, |
81a2b7ce LP |
885 | pid_t *_pid) { |
886 | ||
034c6ed7 LP |
887 | pid_t pid; |
888 | int r; | |
117dcc57 | 889 | int *fds = NULL; |
7fd1b19b | 890 | _cleanup_free_ int *fdsbuf = NULL; |
2105e76a | 891 | unsigned n_fds = 0, n_env = 0; |
7fd1b19b | 892 | _cleanup_strv_free_ char |
117dcc57 | 893 | **argv = NULL, **final_env = NULL, **our_env = NULL; |
4ad49000 | 894 | const char *path; |
034c6ed7 LP |
895 | |
896 | assert(s); | |
897 | assert(c); | |
898 | assert(_pid); | |
899 | ||
4ad49000 LP |
900 | unit_realize_cgroup(UNIT(s)); |
901 | ||
613b411c LP |
902 | r = unit_setup_exec_runtime(UNIT(s)); |
903 | if (r < 0) | |
904 | goto fail; | |
905 | ||
6cf6bbc2 LP |
906 | if (pass_fds || |
907 | s->exec_context.std_input == EXEC_INPUT_SOCKET || | |
908 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
909 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
910 | ||
4f2d528d LP |
911 | if (s->socket_fd >= 0) { |
912 | fds = &s->socket_fd; | |
913 | n_fds = 1; | |
6cf6bbc2 | 914 | } else { |
117dcc57 ZJS |
915 | r = service_collect_fds(s, &fdsbuf, &n_fds); |
916 | if (r < 0) | |
6cf6bbc2 LP |
917 | goto fail; |
918 | ||
919 | fds = fdsbuf; | |
920 | } | |
4f2d528d | 921 | } |
44d8db9e | 922 | |
92c1622e LP |
923 | if (timeout && s->timeout_start_usec > 0) { |
924 | r = service_arm_timer(s, s->timeout_start_usec); | |
925 | if (r < 0) | |
926 | goto fail; | |
927 | } else | |
928 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
034c6ed7 | 929 | |
19f6d710 LP |
930 | r = unit_full_printf_strv(UNIT(s), c->argv, &argv); |
931 | if (r < 0) | |
9e2f7c11 | 932 | goto fail; |
9e2f7c11 | 933 | |
09812eb7 | 934 | our_env = new0(char*, 4); |
97ae63e2 | 935 | if (!our_env) { |
2105e76a LP |
936 | r = -ENOMEM; |
937 | goto fail; | |
938 | } | |
c952c6ec | 939 | |
2105e76a | 940 | if (set_notify_socket) |
1124fe6f | 941 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) { |
c952c6ec LP |
942 | r = -ENOMEM; |
943 | goto fail; | |
944 | } | |
945 | ||
2105e76a | 946 | if (s->main_pid > 0) |
ccd06097 | 947 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) { |
c952c6ec LP |
948 | r = -ENOMEM; |
949 | goto fail; | |
950 | } | |
2105e76a | 951 | |
4ad49000 | 952 | if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM) |
ccd06097 | 953 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) { |
97ae63e2 LP |
954 | r = -ENOMEM; |
955 | goto fail; | |
956 | } | |
957 | ||
958 | final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL); | |
959 | if (!final_env) { | |
2105e76a LP |
960 | r = -ENOMEM; |
961 | goto fail; | |
962 | } | |
c952c6ec | 963 | |
4ad49000 LP |
964 | if (is_control && UNIT(s)->cgroup_path) { |
965 | path = strappenda(UNIT(s)->cgroup_path, "/control"); | |
966 | cg_create(SYSTEMD_CGROUP_CONTROLLER, path); | |
967 | } else | |
968 | path = UNIT(s)->cgroup_path; | |
969 | ||
9e2f7c11 LP |
970 | r = exec_spawn(c, |
971 | argv, | |
972 | &s->exec_context, | |
973 | fds, n_fds, | |
2105e76a | 974 | final_env, |
9e2f7c11 LP |
975 | apply_permissions, |
976 | apply_chroot, | |
1e3ad081 | 977 | apply_tty_stdin, |
1124fe6f | 978 | UNIT(s)->manager->confirm_spawn, |
13b84ec7 | 979 | UNIT(s)->manager->cgroup_supported, |
4ad49000 | 980 | path, |
e66cf1a3 | 981 | manager_get_runtime_prefix(UNIT(s)->manager), |
62bca2c6 | 982 | UNIT(s)->id, |
09812eb7 | 983 | s->watchdog_usec, |
f2b68789 | 984 | s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL, |
613b411c | 985 | s->exec_runtime, |
9e2f7c11 | 986 | &pid); |
9e2f7c11 | 987 | if (r < 0) |
034c6ed7 LP |
988 | goto fail; |
989 | ||
117dcc57 ZJS |
990 | r = unit_watch_pid(UNIT(s), pid); |
991 | if (r < 0) | |
034c6ed7 LP |
992 | /* FIXME: we need to do something here */ |
993 | goto fail; | |
994 | ||
995 | *_pid = pid; | |
996 | ||
5cb5a6ff | 997 | return 0; |
034c6ed7 LP |
998 | |
999 | fail: | |
1000 | if (timeout) | |
718db961 | 1001 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
034c6ed7 LP |
1002 | |
1003 | return r; | |
1004 | } | |
1005 | ||
80876c20 LP |
1006 | static int main_pid_good(Service *s) { |
1007 | assert(s); | |
1008 | ||
1009 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
1010 | * don't know */ | |
1011 | ||
1012 | /* If we know the pid file, then lets just check if it is | |
1013 | * still valid */ | |
6dfa5494 LP |
1014 | if (s->main_pid_known) { |
1015 | ||
1016 | /* If it's an alien child let's check if it is still | |
1017 | * alive ... */ | |
62220cf7 | 1018 | if (s->main_pid_alien && s->main_pid > 0) |
9f5650ae | 1019 | return pid_is_alive(s->main_pid); |
6dfa5494 LP |
1020 | |
1021 | /* .. otherwise assume we'll get a SIGCHLD for it, | |
1022 | * which we really should wait for to collect exit | |
1023 | * status and code */ | |
80876c20 | 1024 | return s->main_pid > 0; |
6dfa5494 | 1025 | } |
80876c20 LP |
1026 | |
1027 | /* We don't know the pid */ | |
1028 | return -EAGAIN; | |
1029 | } | |
1030 | ||
44a6b1b6 | 1031 | _pure_ static int control_pid_good(Service *s) { |
80876c20 LP |
1032 | assert(s); |
1033 | ||
1034 | return s->control_pid > 0; | |
1035 | } | |
1036 | ||
1037 | static int cgroup_good(Service *s) { | |
1038 | int r; | |
1039 | ||
1040 | assert(s); | |
1041 | ||
4ad49000 LP |
1042 | if (!UNIT(s)->cgroup_path) |
1043 | return 0; | |
1044 | ||
1045 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true); | |
117dcc57 | 1046 | if (r < 0) |
80876c20 LP |
1047 | return r; |
1048 | ||
1049 | return !r; | |
1050 | } | |
1051 | ||
bf500566 | 1052 | static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none); |
93ae25e6 | 1053 | |
f42806df | 1054 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
034c6ed7 LP |
1055 | int r; |
1056 | assert(s); | |
1057 | ||
f42806df LP |
1058 | if (f != SERVICE_SUCCESS) |
1059 | s->result = f; | |
034c6ed7 | 1060 | |
0c7f15b3 MS |
1061 | service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD); |
1062 | ||
93ae25e6 MO |
1063 | if (s->result != SERVICE_SUCCESS) |
1064 | service_execute_action(s, s->failure_action, "failed", false); | |
1065 | ||
034c6ed7 | 1066 | if (allow_restart && |
47342320 | 1067 | !s->forbid_restart && |
034c6ed7 | 1068 | (s->restart == SERVICE_RESTART_ALWAYS || |
f42806df LP |
1069 | (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) || |
1070 | (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) || | |
6cfe2fde | 1071 | (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) || |
dc99a976 | 1072 | (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) || |
37520c1b | 1073 | (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) || |
3e2d435b | 1074 | (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) || |
37520c1b | 1075 | (IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) && set_contains(s->restart_force_status.signal, INT_TO_PTR(s->main_exec_status.status)))) && |
3e2d435b | 1076 | (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) && |
37520c1b | 1077 | (!IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) || !set_contains(s->restart_prevent_status.signal, INT_TO_PTR(s->main_exec_status.status)))) { |
034c6ed7 | 1078 | |
718db961 | 1079 | r = service_arm_timer(s, s->restart_usec); |
f42806df | 1080 | if (r < 0) |
034c6ed7 LP |
1081 | goto fail; |
1082 | ||
1083 | service_set_state(s, SERVICE_AUTO_RESTART); | |
0c7f15b3 | 1084 | } |
034c6ed7 | 1085 | |
47342320 LP |
1086 | s->forbid_restart = false; |
1087 | ||
e66cf1a3 | 1088 | /* We want fresh tmpdirs in case service is started again immediately */ |
613b411c LP |
1089 | exec_runtime_destroy(s->exec_runtime); |
1090 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); | |
c17ec25e | 1091 | |
e66cf1a3 LP |
1092 | /* Also, remove the runtime directory in */ |
1093 | exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager)); | |
1094 | ||
9285c9ff LN |
1095 | /* Try to delete the pid file. At this point it will be |
1096 | * out-of-date, and some software might be confused by it, so | |
1097 | * let's remove it. */ | |
1098 | if (s->pid_file) | |
1099 | unlink_noerrno(s->pid_file); | |
1100 | ||
034c6ed7 LP |
1101 | return; |
1102 | ||
1103 | fail: | |
66870f90 ZJS |
1104 | log_warning_unit(UNIT(s)->id, |
1105 | "%s failed to run install restart timer: %s", | |
1106 | UNIT(s)->id, strerror(-r)); | |
f42806df | 1107 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1108 | } |
1109 | ||
f42806df | 1110 | static void service_enter_stop_post(Service *s, ServiceResult f) { |
034c6ed7 LP |
1111 | int r; |
1112 | assert(s); | |
1113 | ||
f42806df LP |
1114 | if (f != SERVICE_SUCCESS) |
1115 | s->result = f; | |
034c6ed7 | 1116 | |
5e94833f | 1117 | service_unwatch_control_pid(s); |
a911bb9a | 1118 | unit_watch_all_pids(UNIT(s)); |
5e94833f | 1119 | |
117dcc57 ZJS |
1120 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; |
1121 | if (s->control_command) { | |
867b3b7d LP |
1122 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
1123 | ||
ecedd90f LP |
1124 | r = service_spawn(s, |
1125 | s->control_command, | |
1126 | true, | |
1127 | false, | |
1128 | !s->permissions_start_only, | |
1129 | !s->root_directory_start_only, | |
1130 | true, | |
1131 | false, | |
1132 | true, | |
1133 | &s->control_pid); | |
1134 | if (r < 0) | |
034c6ed7 LP |
1135 | goto fail; |
1136 | ||
80876c20 LP |
1137 | service_set_state(s, SERVICE_STOP_POST); |
1138 | } else | |
ac84d1fb | 1139 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1140 | |
1141 | return; | |
1142 | ||
1143 | fail: | |
66870f90 ZJS |
1144 | log_warning_unit(UNIT(s)->id, |
1145 | "%s failed to run 'stop-post' task: %s", | |
1146 | UNIT(s)->id, strerror(-r)); | |
f42806df | 1147 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1148 | } |
1149 | ||
f42806df | 1150 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
034c6ed7 | 1151 | int r; |
034c6ed7 LP |
1152 | |
1153 | assert(s); | |
1154 | ||
f42806df LP |
1155 | if (f != SERVICE_SUCCESS) |
1156 | s->result = f; | |
034c6ed7 | 1157 | |
a911bb9a LP |
1158 | unit_watch_all_pids(UNIT(s)); |
1159 | ||
cd2086fe LP |
1160 | r = unit_kill_context( |
1161 | UNIT(s), | |
1162 | &s->kill_context, | |
1163 | state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM, | |
1164 | s->main_pid, | |
1165 | s->control_pid, | |
1166 | s->main_pid_alien); | |
ac84d1fb | 1167 | |
cd2086fe LP |
1168 | if (r < 0) |
1169 | goto fail; | |
034c6ed7 | 1170 | |
cd2086fe | 1171 | if (r > 0) { |
d568a335 | 1172 | if (s->timeout_stop_usec > 0) { |
718db961 | 1173 | r = service_arm_timer(s, s->timeout_stop_usec); |
d568a335 | 1174 | if (r < 0) |
e558336f | 1175 | goto fail; |
d568a335 | 1176 | } |
d6ea93e3 | 1177 | |
80876c20 | 1178 | service_set_state(s, state); |
ac84d1fb LP |
1179 | } else if (state == SERVICE_STOP_SIGTERM) |
1180 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); | |
1181 | else if (state == SERVICE_STOP_SIGKILL) | |
f42806df | 1182 | service_enter_stop_post(s, SERVICE_SUCCESS); |
ac84d1fb LP |
1183 | else if (state == SERVICE_FINAL_SIGTERM) |
1184 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); | |
80876c20 | 1185 | else |
f42806df | 1186 | service_enter_dead(s, SERVICE_SUCCESS, true); |
034c6ed7 LP |
1187 | |
1188 | return; | |
1189 | ||
1190 | fail: | |
66870f90 ZJS |
1191 | log_warning_unit(UNIT(s)->id, |
1192 | "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r)); | |
034c6ed7 | 1193 | |
80876c20 | 1194 | if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) |
f42806df | 1195 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 1196 | else |
f42806df | 1197 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
1198 | } |
1199 | ||
f42806df | 1200 | static void service_enter_stop(Service *s, ServiceResult f) { |
034c6ed7 | 1201 | int r; |
5925dd3c | 1202 | |
034c6ed7 LP |
1203 | assert(s); |
1204 | ||
f42806df LP |
1205 | if (f != SERVICE_SUCCESS) |
1206 | s->result = f; | |
034c6ed7 | 1207 | |
5e94833f | 1208 | service_unwatch_control_pid(s); |
a911bb9a | 1209 | unit_watch_all_pids(UNIT(s)); |
5e94833f | 1210 | |
117dcc57 ZJS |
1211 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; |
1212 | if (s->control_command) { | |
867b3b7d LP |
1213 | s->control_command_id = SERVICE_EXEC_STOP; |
1214 | ||
ecedd90f LP |
1215 | r = service_spawn(s, |
1216 | s->control_command, | |
1217 | true, | |
1218 | false, | |
1219 | !s->permissions_start_only, | |
1220 | !s->root_directory_start_only, | |
1221 | false, | |
1222 | false, | |
1223 | true, | |
1224 | &s->control_pid); | |
1225 | if (r < 0) | |
034c6ed7 LP |
1226 | goto fail; |
1227 | ||
80876c20 LP |
1228 | service_set_state(s, SERVICE_STOP); |
1229 | } else | |
f42806df | 1230 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1231 | |
1232 | return; | |
1233 | ||
1234 | fail: | |
66870f90 ZJS |
1235 | log_warning_unit(UNIT(s)->id, |
1236 | "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r)); | |
f42806df | 1237 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1238 | } |
1239 | ||
f42806df | 1240 | static void service_enter_running(Service *s, ServiceResult f) { |
4eab639f | 1241 | int main_pid_ok, cgroup_ok; |
80876c20 LP |
1242 | assert(s); |
1243 | ||
f42806df LP |
1244 | if (f != SERVICE_SUCCESS) |
1245 | s->result = f; | |
80876c20 | 1246 | |
4eab639f LP |
1247 | main_pid_ok = main_pid_good(s); |
1248 | cgroup_ok = cgroup_good(s); | |
1249 | ||
1250 | if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) && | |
05e343b7 | 1251 | (s->bus_name_good || s->type != SERVICE_DBUS)) |
80876c20 | 1252 | service_set_state(s, SERVICE_RUNNING); |
02ee865a | 1253 | else if (s->remain_after_exit) |
80876c20 LP |
1254 | service_set_state(s, SERVICE_EXITED); |
1255 | else | |
f42806df | 1256 | service_enter_stop(s, SERVICE_SUCCESS); |
80876c20 LP |
1257 | } |
1258 | ||
034c6ed7 LP |
1259 | static void service_enter_start_post(Service *s) { |
1260 | int r; | |
1261 | assert(s); | |
1262 | ||
5e94833f | 1263 | service_unwatch_control_pid(s); |
842129f5 | 1264 | service_reset_watchdog(s); |
bb242b7b | 1265 | |
117dcc57 ZJS |
1266 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; |
1267 | if (s->control_command) { | |
867b3b7d LP |
1268 | s->control_command_id = SERVICE_EXEC_START_POST; |
1269 | ||
ecedd90f LP |
1270 | r = service_spawn(s, |
1271 | s->control_command, | |
1272 | true, | |
1273 | false, | |
1274 | !s->permissions_start_only, | |
1275 | !s->root_directory_start_only, | |
1276 | false, | |
1277 | false, | |
1278 | true, | |
1279 | &s->control_pid); | |
1280 | if (r < 0) | |
034c6ed7 LP |
1281 | goto fail; |
1282 | ||
80876c20 LP |
1283 | service_set_state(s, SERVICE_START_POST); |
1284 | } else | |
f42806df | 1285 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
1286 | |
1287 | return; | |
1288 | ||
1289 | fail: | |
66870f90 ZJS |
1290 | log_warning_unit(UNIT(s)->id, |
1291 | "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r)); | |
f42806df | 1292 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1293 | } |
1294 | ||
4ad49000 LP |
1295 | static void service_kill_control_processes(Service *s) { |
1296 | char *p; | |
1297 | ||
1298 | if (!UNIT(s)->cgroup_path) | |
1299 | return; | |
1300 | ||
1301 | p = strappenda(UNIT(s)->cgroup_path, "/control"); | |
4ad49000 LP |
1302 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL); |
1303 | } | |
1304 | ||
034c6ed7 | 1305 | static void service_enter_start(Service *s) { |
4ad49000 | 1306 | ExecCommand *c; |
034c6ed7 LP |
1307 | pid_t pid; |
1308 | int r; | |
1309 | ||
1310 | assert(s); | |
1311 | ||
1312 | assert(s->exec_command[SERVICE_EXEC_START]); | |
34e9ba66 | 1313 | assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT); |
034c6ed7 | 1314 | |
41efeaec LP |
1315 | service_unwatch_control_pid(s); |
1316 | service_unwatch_main_pid(s); | |
80876c20 | 1317 | |
8f53a7b8 LP |
1318 | /* We want to ensure that nobody leaks processes from |
1319 | * START_PRE here, so let's go on a killing spree, People | |
1320 | * should not spawn long running processes from START_PRE. */ | |
4ad49000 | 1321 | service_kill_control_processes(s); |
8f53a7b8 | 1322 | |
867b3b7d LP |
1323 | if (s->type == SERVICE_FORKING) { |
1324 | s->control_command_id = SERVICE_EXEC_START; | |
1325 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
1326 | ||
1327 | s->main_command = NULL; | |
1328 | } else { | |
1329 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
1330 | s->control_command = NULL; | |
1331 | ||
1332 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
1333 | } | |
34e9ba66 | 1334 | |
ecedd90f LP |
1335 | r = service_spawn(s, |
1336 | c, | |
117dcc57 ZJS |
1337 | s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || |
1338 | s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT, | |
ecedd90f LP |
1339 | true, |
1340 | true, | |
1341 | true, | |
1342 | true, | |
1343 | s->notify_access != NOTIFY_NONE, | |
1344 | false, | |
1345 | &pid); | |
1346 | if (r < 0) | |
034c6ed7 LP |
1347 | goto fail; |
1348 | ||
f2b68789 | 1349 | if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) { |
034c6ed7 LP |
1350 | /* For simple services we immediately start |
1351 | * the START_POST binaries. */ | |
1352 | ||
5925dd3c | 1353 | service_set_main_pid(s, pid); |
034c6ed7 LP |
1354 | service_enter_start_post(s); |
1355 | ||
1356 | } else if (s->type == SERVICE_FORKING) { | |
1357 | ||
1358 | /* For forking services we wait until the start | |
1359 | * process exited. */ | |
1360 | ||
e55224ca | 1361 | s->control_pid = pid; |
80876c20 LP |
1362 | service_set_state(s, SERVICE_START); |
1363 | ||
34e9ba66 | 1364 | } else if (s->type == SERVICE_ONESHOT || |
8c47c732 LP |
1365 | s->type == SERVICE_DBUS || |
1366 | s->type == SERVICE_NOTIFY) { | |
7d55e835 | 1367 | |
34e9ba66 | 1368 | /* For oneshot services we wait until the start |
7d55e835 LP |
1369 | * process exited, too, but it is our main process. */ |
1370 | ||
05e343b7 | 1371 | /* For D-Bus services we know the main pid right away, |
8c47c732 LP |
1372 | * but wait for the bus name to appear on the |
1373 | * bus. Notify services are similar. */ | |
05e343b7 | 1374 | |
5925dd3c | 1375 | service_set_main_pid(s, pid); |
80876c20 | 1376 | service_set_state(s, SERVICE_START); |
034c6ed7 LP |
1377 | } else |
1378 | assert_not_reached("Unknown service type"); | |
1379 | ||
1380 | return; | |
1381 | ||
1382 | fail: | |
66870f90 ZJS |
1383 | log_warning_unit(UNIT(s)->id, |
1384 | "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r)); | |
f42806df | 1385 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1386 | } |
1387 | ||
1388 | static void service_enter_start_pre(Service *s) { | |
1389 | int r; | |
1390 | ||
1391 | assert(s); | |
1392 | ||
5e94833f LP |
1393 | service_unwatch_control_pid(s); |
1394 | ||
117dcc57 ZJS |
1395 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; |
1396 | if (s->control_command) { | |
8f53a7b8 LP |
1397 | /* Before we start anything, let's clear up what might |
1398 | * be left from previous runs. */ | |
4ad49000 | 1399 | service_kill_control_processes(s); |
8f53a7b8 | 1400 | |
867b3b7d LP |
1401 | s->control_command_id = SERVICE_EXEC_START_PRE; |
1402 | ||
ecedd90f LP |
1403 | r = service_spawn(s, |
1404 | s->control_command, | |
1405 | true, | |
1406 | false, | |
1407 | !s->permissions_start_only, | |
1408 | !s->root_directory_start_only, | |
1409 | true, | |
1410 | false, | |
1411 | true, | |
1412 | &s->control_pid); | |
1413 | if (r < 0) | |
034c6ed7 LP |
1414 | goto fail; |
1415 | ||
80876c20 LP |
1416 | service_set_state(s, SERVICE_START_PRE); |
1417 | } else | |
034c6ed7 LP |
1418 | service_enter_start(s); |
1419 | ||
1420 | return; | |
1421 | ||
1422 | fail: | |
37520c1b | 1423 | log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r)); |
f42806df | 1424 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
1425 | } |
1426 | ||
1427 | static void service_enter_restart(Service *s) { | |
718db961 | 1428 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 | 1429 | int r; |
398ef8ba | 1430 | |
034c6ed7 LP |
1431 | assert(s); |
1432 | ||
a8bb2e65 LP |
1433 | if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) { |
1434 | /* Don't restart things if we are going down anyway */ | |
37520c1b | 1435 | log_info_unit(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart."); |
2edfa366 | 1436 | |
718db961 | 1437 | r = service_arm_timer(s, s->restart_usec); |
a8bb2e65 | 1438 | if (r < 0) |
2edfa366 | 1439 | goto fail; |
feae8adb DW |
1440 | |
1441 | return; | |
2edfa366 LP |
1442 | } |
1443 | ||
48bb5876 DW |
1444 | /* Any units that are bound to this service must also be |
1445 | * restarted. We use JOB_RESTART (instead of the more obvious | |
1446 | * JOB_START) here so that those dependency jobs will be added | |
1447 | * as well. */ | |
1448 | r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL); | |
1449 | if (r < 0) | |
034c6ed7 LP |
1450 | goto fail; |
1451 | ||
a8bb2e65 LP |
1452 | /* Note that we stay in the SERVICE_AUTO_RESTART state here, |
1453 | * it will be canceled as part of the service_stop() call that | |
1454 | * is executed as part of JOB_RESTART. */ | |
1455 | ||
37520c1b | 1456 | log_debug_unit(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id); |
034c6ed7 LP |
1457 | return; |
1458 | ||
1459 | fail: | |
66870f90 ZJS |
1460 | log_warning_unit(UNIT(s)->id, |
1461 | "%s failed to schedule restart job: %s", | |
718db961 | 1462 | UNIT(s)->id, bus_error_message(&error, -r)); |
f42806df | 1463 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1464 | } |
1465 | ||
1466 | static void service_enter_reload(Service *s) { | |
1467 | int r; | |
1468 | ||
1469 | assert(s); | |
1470 | ||
5e94833f LP |
1471 | service_unwatch_control_pid(s); |
1472 | ||
117dcc57 ZJS |
1473 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; |
1474 | if (s->control_command) { | |
867b3b7d LP |
1475 | s->control_command_id = SERVICE_EXEC_RELOAD; |
1476 | ||
ecedd90f LP |
1477 | r = service_spawn(s, |
1478 | s->control_command, | |
1479 | true, | |
1480 | false, | |
1481 | !s->permissions_start_only, | |
1482 | !s->root_directory_start_only, | |
1483 | false, | |
1484 | false, | |
1485 | true, | |
1486 | &s->control_pid); | |
1487 | if (r < 0) | |
034c6ed7 LP |
1488 | goto fail; |
1489 | ||
80876c20 LP |
1490 | service_set_state(s, SERVICE_RELOAD); |
1491 | } else | |
f42806df | 1492 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
1493 | |
1494 | return; | |
1495 | ||
1496 | fail: | |
66870f90 ZJS |
1497 | log_warning_unit(UNIT(s)->id, |
1498 | "%s failed to run 'reload' task: %s", | |
1499 | UNIT(s)->id, strerror(-r)); | |
f42806df LP |
1500 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
1501 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
1502 | } |
1503 | ||
f42806df | 1504 | static void service_run_next_control(Service *s) { |
034c6ed7 LP |
1505 | int r; |
1506 | ||
1507 | assert(s); | |
1508 | assert(s->control_command); | |
1509 | assert(s->control_command->command_next); | |
1510 | ||
34e9ba66 | 1511 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 1512 | |
34e9ba66 | 1513 | s->control_command = s->control_command->command_next; |
5e94833f LP |
1514 | service_unwatch_control_pid(s); |
1515 | ||
ecedd90f LP |
1516 | r = service_spawn(s, |
1517 | s->control_command, | |
1518 | true, | |
1519 | false, | |
1520 | !s->permissions_start_only, | |
1521 | !s->root_directory_start_only, | |
1522 | s->control_command_id == SERVICE_EXEC_START_PRE || | |
1523 | s->control_command_id == SERVICE_EXEC_STOP_POST, | |
1524 | false, | |
1525 | true, | |
1526 | &s->control_pid); | |
1527 | if (r < 0) | |
034c6ed7 LP |
1528 | goto fail; |
1529 | ||
1530 | return; | |
1531 | ||
1532 | fail: | |
66870f90 ZJS |
1533 | log_warning_unit(UNIT(s)->id, |
1534 | "%s failed to run next control task: %s", | |
1535 | UNIT(s)->id, strerror(-r)); | |
034c6ed7 | 1536 | |
80876c20 | 1537 | if (s->state == SERVICE_START_PRE) |
f42806df | 1538 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
80876c20 | 1539 | else if (s->state == SERVICE_STOP) |
f42806df | 1540 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 1541 | else if (s->state == SERVICE_STOP_POST) |
f42806df | 1542 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
e2f3b44c | 1543 | else if (s->state == SERVICE_RELOAD) { |
f42806df LP |
1544 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
1545 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c | 1546 | } else |
f42806df | 1547 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
5cb5a6ff LP |
1548 | } |
1549 | ||
f42806df | 1550 | static void service_run_next_main(Service *s) { |
34e9ba66 LP |
1551 | pid_t pid; |
1552 | int r; | |
1553 | ||
1554 | assert(s); | |
867b3b7d LP |
1555 | assert(s->main_command); |
1556 | assert(s->main_command->command_next); | |
1557 | assert(s->type == SERVICE_ONESHOT); | |
34e9ba66 | 1558 | |
867b3b7d | 1559 | s->main_command = s->main_command->command_next; |
34e9ba66 LP |
1560 | service_unwatch_main_pid(s); |
1561 | ||
ecedd90f LP |
1562 | r = service_spawn(s, |
1563 | s->main_command, | |
98709151 | 1564 | true, |
ecedd90f LP |
1565 | true, |
1566 | true, | |
1567 | true, | |
1568 | true, | |
1569 | s->notify_access != NOTIFY_NONE, | |
1570 | false, | |
1571 | &pid); | |
1572 | if (r < 0) | |
34e9ba66 LP |
1573 | goto fail; |
1574 | ||
1575 | service_set_main_pid(s, pid); | |
1576 | ||
1577 | return; | |
1578 | ||
1579 | fail: | |
66870f90 ZJS |
1580 | log_warning_unit(UNIT(s)->id, |
1581 | "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r)); | |
f42806df | 1582 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
34e9ba66 LP |
1583 | } |
1584 | ||
bf500566 | 1585 | static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) { |
4b939747 MO |
1586 | assert(s); |
1587 | ||
bf500566 MO |
1588 | if (action == SERVICE_FAILURE_ACTION_REBOOT || |
1589 | action == SERVICE_FAILURE_ACTION_REBOOT_FORCE) | |
efe6e7d3 MO |
1590 | update_reboot_param_file(s->reboot_arg); |
1591 | ||
93ae25e6 | 1592 | switch (action) { |
4b939747 | 1593 | |
bf500566 | 1594 | case SERVICE_FAILURE_ACTION_NONE: |
93ae25e6 MO |
1595 | if (log_action_none) |
1596 | log_warning_unit(UNIT(s)->id, | |
1597 | "%s %s, refusing to start.", UNIT(s)->id, reason); | |
4b939747 MO |
1598 | break; |
1599 | ||
bf500566 | 1600 | case SERVICE_FAILURE_ACTION_REBOOT: { |
718db961 | 1601 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
4b939747 MO |
1602 | int r; |
1603 | ||
66870f90 | 1604 | log_warning_unit(UNIT(s)->id, |
93ae25e6 | 1605 | "%s %s, rebooting.", UNIT(s)->id, reason); |
4b939747 | 1606 | |
117dcc57 ZJS |
1607 | r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, |
1608 | SPECIAL_REBOOT_TARGET, JOB_REPLACE, | |
1609 | true, &error, NULL); | |
718db961 | 1610 | if (r < 0) |
66870f90 | 1611 | log_error_unit(UNIT(s)->id, |
718db961 | 1612 | "Failed to reboot: %s.", bus_error_message(&error, r)); |
4b939747 MO |
1613 | |
1614 | break; | |
1615 | } | |
1616 | ||
bf500566 | 1617 | case SERVICE_FAILURE_ACTION_REBOOT_FORCE: |
66870f90 | 1618 | log_warning_unit(UNIT(s)->id, |
93ae25e6 | 1619 | "%s %s, forcibly rebooting.", UNIT(s)->id, reason); |
4b939747 MO |
1620 | UNIT(s)->manager->exit_code = MANAGER_REBOOT; |
1621 | break; | |
1622 | ||
bf500566 | 1623 | case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE: |
66870f90 | 1624 | log_warning_unit(UNIT(s)->id, |
93ae25e6 | 1625 | "%s %s, rebooting immediately.", UNIT(s)->id, reason); |
0049f05a | 1626 | sync(); |
efe6e7d3 MO |
1627 | if (s->reboot_arg) { |
1628 | log_info("Rebooting with argument '%s'.", s->reboot_arg); | |
1629 | syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, | |
1630 | LINUX_REBOOT_CMD_RESTART2, s->reboot_arg); | |
1631 | } | |
1632 | ||
1633 | log_info("Rebooting."); | |
4b939747 MO |
1634 | reboot(RB_AUTOBOOT); |
1635 | break; | |
1636 | ||
1637 | default: | |
66870f90 | 1638 | log_error_unit(UNIT(s)->id, |
bf500566 MO |
1639 | "failure action=%i", action); |
1640 | assert_not_reached("Unknown FailureAction."); | |
4b939747 MO |
1641 | } |
1642 | ||
1643 | return -ECANCELED; | |
1644 | } | |
1645 | ||
93ae25e6 MO |
1646 | static int service_start_limit_test(Service *s) { |
1647 | assert(s); | |
1648 | ||
1649 | if (ratelimit_test(&s->start_limit)) | |
1650 | return 0; | |
1651 | ||
1652 | return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true); | |
1653 | } | |
1654 | ||
87f0e418 LP |
1655 | static int service_start(Unit *u) { |
1656 | Service *s = SERVICE(u); | |
4b939747 | 1657 | int r; |
5cb5a6ff LP |
1658 | |
1659 | assert(s); | |
1660 | ||
034c6ed7 LP |
1661 | /* We cannot fulfill this request right now, try again later |
1662 | * please! */ | |
1663 | if (s->state == SERVICE_STOP || | |
1664 | s->state == SERVICE_STOP_SIGTERM || | |
1665 | s->state == SERVICE_STOP_SIGKILL || | |
1666 | s->state == SERVICE_STOP_POST || | |
1667 | s->state == SERVICE_FINAL_SIGTERM || | |
1668 | s->state == SERVICE_FINAL_SIGKILL) | |
5cb5a6ff LP |
1669 | return -EAGAIN; |
1670 | ||
034c6ed7 LP |
1671 | /* Already on it! */ |
1672 | if (s->state == SERVICE_START_PRE || | |
1673 | s->state == SERVICE_START || | |
1674 | s->state == SERVICE_START_POST) | |
1675 | return 0; | |
1676 | ||
2e9d6c12 | 1677 | /* A service that will be restarted must be stopped first to |
7f2cddae | 1678 | * trigger BindsTo and/or OnFailure dependencies. If a user |
2e9d6c12 | 1679 | * does not want to wait for the holdoff time to elapse, the |
d4943dc7 LP |
1680 | * service should be manually restarted, not started. We |
1681 | * simply return EAGAIN here, so that any start jobs stay | |
1682 | * queued, and assume that the auto restart timer will | |
1683 | * eventually trigger the restart. */ | |
1684 | if (s->state == SERVICE_AUTO_RESTART) | |
a8bb2e65 | 1685 | return -EAGAIN; |
2e9d6c12 DW |
1686 | |
1687 | assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED); | |
5cb5a6ff | 1688 | |
1e2e8133 | 1689 | /* Make sure we don't enter a busy loop of some kind. */ |
4b939747 | 1690 | r = service_start_limit_test(s); |
c2f34808 | 1691 | if (r < 0) { |
8d1b002a | 1692 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false); |
4b939747 | 1693 | return r; |
c2f34808 | 1694 | } |
1e2e8133 | 1695 | |
f42806df LP |
1696 | s->result = SERVICE_SUCCESS; |
1697 | s->reload_result = SERVICE_SUCCESS; | |
034c6ed7 | 1698 | s->main_pid_known = false; |
6dfa5494 | 1699 | s->main_pid_alien = false; |
47342320 | 1700 | s->forbid_restart = false; |
034c6ed7 LP |
1701 | |
1702 | service_enter_start_pre(s); | |
1703 | return 0; | |
5cb5a6ff LP |
1704 | } |
1705 | ||
87f0e418 LP |
1706 | static int service_stop(Unit *u) { |
1707 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
1708 | |
1709 | assert(s); | |
1710 | ||
f0c7b229 | 1711 | /* Don't create restart jobs from here. */ |
47342320 | 1712 | s->forbid_restart = true; |
034c6ed7 | 1713 | |
e537352b LP |
1714 | /* Already on it */ |
1715 | if (s->state == SERVICE_STOP || | |
1716 | s->state == SERVICE_STOP_SIGTERM || | |
1717 | s->state == SERVICE_STOP_SIGKILL || | |
1718 | s->state == SERVICE_STOP_POST || | |
1719 | s->state == SERVICE_FINAL_SIGTERM || | |
1720 | s->state == SERVICE_FINAL_SIGKILL) | |
1721 | return 0; | |
1722 | ||
f0c7b229 | 1723 | /* A restart will be scheduled or is in progress. */ |
034c6ed7 | 1724 | if (s->state == SERVICE_AUTO_RESTART) { |
0c7f15b3 | 1725 | service_set_state(s, SERVICE_DEAD); |
034c6ed7 LP |
1726 | return 0; |
1727 | } | |
1728 | ||
3f6c78dc LP |
1729 | /* If there's already something running we go directly into |
1730 | * kill mode. */ | |
1731 | if (s->state == SERVICE_START_PRE || | |
1732 | s->state == SERVICE_START || | |
1733 | s->state == SERVICE_START_POST || | |
1734 | s->state == SERVICE_RELOAD) { | |
f42806df | 1735 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
3f6c78dc LP |
1736 | return 0; |
1737 | } | |
5cb5a6ff | 1738 | |
3f6c78dc LP |
1739 | assert(s->state == SERVICE_RUNNING || |
1740 | s->state == SERVICE_EXITED); | |
3a762661 | 1741 | |
f42806df | 1742 | service_enter_stop(s, SERVICE_SUCCESS); |
5cb5a6ff LP |
1743 | return 0; |
1744 | } | |
1745 | ||
87f0e418 LP |
1746 | static int service_reload(Unit *u) { |
1747 | Service *s = SERVICE(u); | |
034c6ed7 LP |
1748 | |
1749 | assert(s); | |
1750 | ||
80876c20 | 1751 | assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED); |
034c6ed7 LP |
1752 | |
1753 | service_enter_reload(s); | |
5cb5a6ff LP |
1754 | return 0; |
1755 | } | |
1756 | ||
44a6b1b6 | 1757 | _pure_ static bool service_can_reload(Unit *u) { |
87f0e418 | 1758 | Service *s = SERVICE(u); |
034c6ed7 LP |
1759 | |
1760 | assert(s); | |
1761 | ||
1762 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
1763 | } | |
1764 | ||
a16e1123 LP |
1765 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
1766 | Service *s = SERVICE(u); | |
1767 | ||
1768 | assert(u); | |
1769 | assert(f); | |
1770 | assert(fds); | |
1771 | ||
1772 | unit_serialize_item(u, f, "state", service_state_to_string(s->state)); | |
f42806df LP |
1773 | unit_serialize_item(u, f, "result", service_result_to_string(s->result)); |
1774 | unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result)); | |
a16e1123 LP |
1775 | |
1776 | if (s->control_pid > 0) | |
ccd06097 ZJS |
1777 | unit_serialize_item_format(u, f, "control-pid", PID_FMT, |
1778 | s->control_pid); | |
a16e1123 | 1779 | |
5925dd3c | 1780 | if (s->main_pid_known && s->main_pid > 0) |
ccd06097 | 1781 | unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid); |
a16e1123 LP |
1782 | |
1783 | unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known)); | |
1784 | ||
3a2776bc LP |
1785 | if (s->status_text) |
1786 | unit_serialize_item(u, f, "status-text", s->status_text); | |
1787 | ||
cfc4eb4c LP |
1788 | /* FIXME: There's a minor uncleanliness here: if there are |
1789 | * multiple commands attached here, we will start from the | |
1790 | * first one again */ | |
a16e1123 | 1791 | if (s->control_command_id >= 0) |
117dcc57 ZJS |
1792 | unit_serialize_item(u, f, "control-command", |
1793 | service_exec_command_to_string(s->control_command_id)); | |
a16e1123 LP |
1794 | |
1795 | if (s->socket_fd >= 0) { | |
1796 | int copy; | |
1797 | ||
1798 | if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0) | |
1799 | return copy; | |
1800 | ||
1801 | unit_serialize_item_format(u, f, "socket-fd", "%i", copy); | |
1802 | } | |
1803 | ||
ecdbca40 | 1804 | if (s->main_exec_status.pid > 0) { |
ccd06097 ZJS |
1805 | unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT, |
1806 | s->main_exec_status.pid); | |
117dcc57 ZJS |
1807 | dual_timestamp_serialize(f, "main-exec-status-start", |
1808 | &s->main_exec_status.start_timestamp); | |
1809 | dual_timestamp_serialize(f, "main-exec-status-exit", | |
1810 | &s->main_exec_status.exit_timestamp); | |
ecdbca40 | 1811 | |
799fd0fd | 1812 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { |
117dcc57 ZJS |
1813 | unit_serialize_item_format(u, f, "main-exec-status-code", "%i", |
1814 | s->main_exec_status.code); | |
1815 | unit_serialize_item_format(u, f, "main-exec-status-status", "%i", | |
1816 | s->main_exec_status.status); | |
ecdbca40 LP |
1817 | } |
1818 | } | |
a6927d7f | 1819 | if (dual_timestamp_is_set(&s->watchdog_timestamp)) |
842129f5 | 1820 | dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp); |
ecdbca40 | 1821 | |
6aca9a58 | 1822 | if (s->forbid_restart) |
8d1a2802 | 1823 | unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart)); |
6aca9a58 | 1824 | |
a16e1123 LP |
1825 | return 0; |
1826 | } | |
1827 | ||
1828 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
1829 | Service *s = SERVICE(u); | |
a16e1123 LP |
1830 | |
1831 | assert(u); | |
1832 | assert(key); | |
1833 | assert(value); | |
1834 | assert(fds); | |
1835 | ||
1836 | if (streq(key, "state")) { | |
1837 | ServiceState state; | |
1838 | ||
117dcc57 ZJS |
1839 | state = service_state_from_string(value); |
1840 | if (state < 0) | |
66870f90 | 1841 | log_debug_unit(u->id, "Failed to parse state value %s", value); |
a16e1123 LP |
1842 | else |
1843 | s->deserialized_state = state; | |
f42806df LP |
1844 | } else if (streq(key, "result")) { |
1845 | ServiceResult f; | |
1846 | ||
1847 | f = service_result_from_string(value); | |
1848 | if (f < 0) | |
66870f90 | 1849 | log_debug_unit(u->id, "Failed to parse result value %s", value); |
f42806df LP |
1850 | else if (f != SERVICE_SUCCESS) |
1851 | s->result = f; | |
1852 | ||
1853 | } else if (streq(key, "reload-result")) { | |
1854 | ServiceResult f; | |
1855 | ||
1856 | f = service_result_from_string(value); | |
1857 | if (f < 0) | |
66870f90 | 1858 | log_debug_unit(u->id, "Failed to parse reload result value %s", value); |
f42806df LP |
1859 | else if (f != SERVICE_SUCCESS) |
1860 | s->reload_result = f; | |
a16e1123 | 1861 | |
a16e1123 | 1862 | } else if (streq(key, "control-pid")) { |
5925dd3c | 1863 | pid_t pid; |
a16e1123 | 1864 | |
e364ad06 | 1865 | if (parse_pid(value, &pid) < 0) |
66870f90 | 1866 | log_debug_unit(u->id, "Failed to parse control-pid value %s", value); |
a16e1123 | 1867 | else |
e55224ca | 1868 | s->control_pid = pid; |
a16e1123 | 1869 | } else if (streq(key, "main-pid")) { |
5925dd3c | 1870 | pid_t pid; |
a16e1123 | 1871 | |
e364ad06 | 1872 | if (parse_pid(value, &pid) < 0) |
66870f90 | 1873 | log_debug_unit(u->id, "Failed to parse main-pid value %s", value); |
7400b9d2 LP |
1874 | else { |
1875 | service_set_main_pid(s, pid); | |
1876 | unit_watch_pid(UNIT(s), pid); | |
1877 | } | |
a16e1123 LP |
1878 | } else if (streq(key, "main-pid-known")) { |
1879 | int b; | |
1880 | ||
117dcc57 ZJS |
1881 | b = parse_boolean(value); |
1882 | if (b < 0) | |
66870f90 | 1883 | log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value); |
a16e1123 LP |
1884 | else |
1885 | s->main_pid_known = b; | |
3a2776bc LP |
1886 | } else if (streq(key, "status-text")) { |
1887 | char *t; | |
1888 | ||
117dcc57 ZJS |
1889 | t = strdup(value); |
1890 | if (!t) | |
1891 | log_oom(); | |
1892 | else { | |
3a2776bc LP |
1893 | free(s->status_text); |
1894 | s->status_text = t; | |
1895 | } | |
1896 | ||
a16e1123 LP |
1897 | } else if (streq(key, "control-command")) { |
1898 | ServiceExecCommand id; | |
1899 | ||
117dcc57 ZJS |
1900 | id = service_exec_command_from_string(value); |
1901 | if (id < 0) | |
66870f90 | 1902 | log_debug_unit(u->id, "Failed to parse exec-command value %s", value); |
a16e1123 LP |
1903 | else { |
1904 | s->control_command_id = id; | |
1905 | s->control_command = s->exec_command[id]; | |
1906 | } | |
1907 | } else if (streq(key, "socket-fd")) { | |
1908 | int fd; | |
1909 | ||
1910 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
66870f90 | 1911 | log_debug_unit(u->id, "Failed to parse socket-fd value %s", value); |
a16e1123 LP |
1912 | else { |
1913 | ||
574634bc | 1914 | asynchronous_close(s->socket_fd); |
a16e1123 LP |
1915 | s->socket_fd = fdset_remove(fds, fd); |
1916 | } | |
ecdbca40 LP |
1917 | } else if (streq(key, "main-exec-status-pid")) { |
1918 | pid_t pid; | |
1919 | ||
e364ad06 | 1920 | if (parse_pid(value, &pid) < 0) |
66870f90 | 1921 | log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value); |
ecdbca40 LP |
1922 | else |
1923 | s->main_exec_status.pid = pid; | |
1924 | } else if (streq(key, "main-exec-status-code")) { | |
1925 | int i; | |
1926 | ||
e364ad06 | 1927 | if (safe_atoi(value, &i) < 0) |
66870f90 | 1928 | log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value); |
ecdbca40 LP |
1929 | else |
1930 | s->main_exec_status.code = i; | |
1931 | } else if (streq(key, "main-exec-status-status")) { | |
1932 | int i; | |
1933 | ||
e364ad06 | 1934 | if (safe_atoi(value, &i) < 0) |
66870f90 | 1935 | log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value); |
ecdbca40 LP |
1936 | else |
1937 | s->main_exec_status.status = i; | |
799fd0fd LP |
1938 | } else if (streq(key, "main-exec-status-start")) |
1939 | dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp); | |
1940 | else if (streq(key, "main-exec-status-exit")) | |
1941 | dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp); | |
a6927d7f MO |
1942 | else if (streq(key, "watchdog-timestamp")) |
1943 | dual_timestamp_deserialize(value, &s->watchdog_timestamp); | |
613b411c | 1944 | else if (streq(key, "forbid-restart")) { |
6aca9a58 SE |
1945 | int b; |
1946 | ||
1947 | b = parse_boolean(value); | |
1948 | if (b < 0) | |
8d1a2802 | 1949 | log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value); |
6aca9a58 SE |
1950 | else |
1951 | s->forbid_restart = b; | |
c17ec25e | 1952 | } else |
66870f90 | 1953 | log_debug_unit(u->id, "Unknown serialization key '%s'", key); |
a16e1123 LP |
1954 | |
1955 | return 0; | |
1956 | } | |
1957 | ||
44a6b1b6 | 1958 | _pure_ static UnitActiveState service_active_state(Unit *u) { |
e056b01d LP |
1959 | const UnitActiveState *table; |
1960 | ||
87f0e418 | 1961 | assert(u); |
5cb5a6ff | 1962 | |
e056b01d LP |
1963 | table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
1964 | ||
1965 | return table[SERVICE(u)->state]; | |
034c6ed7 LP |
1966 | } |
1967 | ||
10a94420 LP |
1968 | static const char *service_sub_state_to_string(Unit *u) { |
1969 | assert(u); | |
1970 | ||
1971 | return service_state_to_string(SERVICE(u)->state); | |
1972 | } | |
1973 | ||
701cc384 LP |
1974 | static bool service_check_gc(Unit *u) { |
1975 | Service *s = SERVICE(u); | |
1976 | ||
1977 | assert(s); | |
1978 | ||
6d55002a LP |
1979 | /* Never clean up services that still have a process around, |
1980 | * even if the service is formally dead. */ | |
1981 | if (cgroup_good(s) > 0 || | |
1982 | main_pid_good(s) > 0 || | |
1983 | control_pid_good(s) > 0) | |
1984 | return true; | |
1985 | ||
6d55002a LP |
1986 | return false; |
1987 | } | |
1988 | ||
44a6b1b6 | 1989 | _pure_ static bool service_check_snapshot(Unit *u) { |
701cc384 LP |
1990 | Service *s = SERVICE(u); |
1991 | ||
1992 | assert(s); | |
1993 | ||
99f37ad8 | 1994 | return (s->socket_fd < 0); |
701cc384 LP |
1995 | } |
1996 | ||
3a111838 MS |
1997 | static int service_retry_pid_file(Service *s) { |
1998 | int r; | |
1999 | ||
2000 | assert(s->pid_file); | |
2001 | assert(s->state == SERVICE_START || s->state == SERVICE_START_POST); | |
2002 | ||
2003 | r = service_load_pid_file(s, false); | |
2004 | if (r < 0) | |
2005 | return r; | |
2006 | ||
2007 | service_unwatch_pid_file(s); | |
2008 | ||
f42806df | 2009 | service_enter_running(s, SERVICE_SUCCESS); |
3a111838 MS |
2010 | return 0; |
2011 | } | |
2012 | ||
2013 | static int service_watch_pid_file(Service *s) { | |
2014 | int r; | |
2015 | ||
66870f90 ZJS |
2016 | log_debug_unit(UNIT(s)->id, |
2017 | "Setting watch for %s's PID file %s", | |
2018 | UNIT(s)->id, s->pid_file_pathspec->path); | |
718db961 | 2019 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io); |
3a111838 MS |
2020 | if (r < 0) |
2021 | goto fail; | |
2022 | ||
2023 | /* the pidfile might have appeared just before we set the watch */ | |
bc41f93e ZJS |
2024 | log_debug_unit(UNIT(s)->id, |
2025 | "Trying to read %s's PID file %s in case it changed", | |
2026 | UNIT(s)->id, s->pid_file_pathspec->path); | |
3a111838 MS |
2027 | service_retry_pid_file(s); |
2028 | ||
2029 | return 0; | |
2030 | fail: | |
66870f90 ZJS |
2031 | log_error_unit(UNIT(s)->id, |
2032 | "Failed to set a watch for %s's PID file %s: %s", | |
2033 | UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r)); | |
3a111838 MS |
2034 | service_unwatch_pid_file(s); |
2035 | return r; | |
2036 | } | |
2037 | ||
2038 | static int service_demand_pid_file(Service *s) { | |
2039 | PathSpec *ps; | |
2040 | ||
2041 | assert(s->pid_file); | |
2042 | assert(!s->pid_file_pathspec); | |
2043 | ||
2044 | ps = new0(PathSpec, 1); | |
2045 | if (!ps) | |
2046 | return -ENOMEM; | |
2047 | ||
718db961 | 2048 | ps->unit = UNIT(s); |
3a111838 MS |
2049 | ps->path = strdup(s->pid_file); |
2050 | if (!ps->path) { | |
2051 | free(ps); | |
2052 | return -ENOMEM; | |
2053 | } | |
2054 | ||
2055 | path_kill_slashes(ps->path); | |
2056 | ||
2057 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that | |
2058 | * keep their PID file open all the time. */ | |
2059 | ps->type = PATH_MODIFIED; | |
2060 | ps->inotify_fd = -1; | |
2061 | ||
2062 | s->pid_file_pathspec = ps; | |
2063 | ||
2064 | return service_watch_pid_file(s); | |
2065 | } | |
2066 | ||
718db961 | 2067 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
e14c2802 LP |
2068 | PathSpec *p = userdata; |
2069 | Service *s; | |
2070 | ||
2071 | assert(p); | |
2072 | ||
2073 | s = SERVICE(p->unit); | |
3a111838 MS |
2074 | |
2075 | assert(s); | |
2076 | assert(fd >= 0); | |
2077 | assert(s->state == SERVICE_START || s->state == SERVICE_START_POST); | |
2078 | assert(s->pid_file_pathspec); | |
57020a3a | 2079 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); |
3a111838 | 2080 | |
718db961 | 2081 | log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id); |
3a111838 | 2082 | |
e14c2802 | 2083 | if (path_spec_fd_event(p, events) < 0) |
3a111838 MS |
2084 | goto fail; |
2085 | ||
2086 | if (service_retry_pid_file(s) == 0) | |
718db961 | 2087 | return 0; |
3a111838 MS |
2088 | |
2089 | if (service_watch_pid_file(s) < 0) | |
2090 | goto fail; | |
2091 | ||
718db961 LP |
2092 | return 0; |
2093 | ||
3a111838 MS |
2094 | fail: |
2095 | service_unwatch_pid_file(s); | |
f42806df | 2096 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
718db961 | 2097 | return 0; |
3a111838 MS |
2098 | } |
2099 | ||
a911bb9a LP |
2100 | static void service_notify_cgroup_empty_event(Unit *u) { |
2101 | Service *s = SERVICE(u); | |
2102 | ||
2103 | assert(u); | |
2104 | ||
2105 | log_debug_unit(u->id, "%s: cgroup is empty", u->id); | |
2106 | ||
2107 | switch (s->state) { | |
2108 | ||
2109 | /* Waiting for SIGCHLD is usually more interesting, | |
2110 | * because it includes return codes/signals. Which is | |
2111 | * why we ignore the cgroup events for most cases, | |
2112 | * except when we don't know pid which to expect the | |
2113 | * SIGCHLD for. */ | |
2114 | ||
2115 | case SERVICE_START: | |
2116 | case SERVICE_START_POST: | |
2117 | /* If we were hoping for the daemon to write its PID file, | |
2118 | * we can give up now. */ | |
2119 | if (s->pid_file_pathspec) { | |
2120 | log_warning_unit(u->id, | |
2121 | "%s never wrote its PID file. Failing.", UNIT(s)->id); | |
2122 | service_unwatch_pid_file(s); | |
2123 | if (s->state == SERVICE_START) | |
2124 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2125 | else | |
2126 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2127 | } | |
2128 | break; | |
2129 | ||
2130 | case SERVICE_RUNNING: | |
2131 | /* service_enter_running() will figure out what to do */ | |
2132 | service_enter_running(s, SERVICE_SUCCESS); | |
2133 | break; | |
2134 | ||
2135 | case SERVICE_STOP_SIGTERM: | |
2136 | case SERVICE_STOP_SIGKILL: | |
2137 | ||
2138 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2139 | service_enter_stop_post(s, SERVICE_SUCCESS); | |
2140 | ||
2141 | break; | |
2142 | ||
2143 | case SERVICE_STOP_POST: | |
2144 | case SERVICE_FINAL_SIGTERM: | |
2145 | case SERVICE_FINAL_SIGKILL: | |
2146 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2147 | service_enter_dead(s, SERVICE_SUCCESS, true); | |
2148 | ||
2149 | break; | |
2150 | ||
2151 | default: | |
2152 | ; | |
2153 | } | |
2154 | } | |
2155 | ||
87f0e418 LP |
2156 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
2157 | Service *s = SERVICE(u); | |
f42806df | 2158 | ServiceResult f; |
5cb5a6ff LP |
2159 | |
2160 | assert(s); | |
034c6ed7 LP |
2161 | assert(pid >= 0); |
2162 | ||
96342de6 LN |
2163 | if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) : |
2164 | is_clean_exit_lsb(code, status, &s->success_status)) | |
f42806df LP |
2165 | f = SERVICE_SUCCESS; |
2166 | else if (code == CLD_EXITED) | |
2167 | f = SERVICE_FAILURE_EXIT_CODE; | |
2168 | else if (code == CLD_KILLED) | |
2169 | f = SERVICE_FAILURE_SIGNAL; | |
2170 | else if (code == CLD_DUMPED) | |
2171 | f = SERVICE_FAILURE_CORE_DUMP; | |
d06dacd0 | 2172 | else |
cfc4eb4c | 2173 | assert_not_reached("Unknown code"); |
034c6ed7 LP |
2174 | |
2175 | if (s->main_pid == pid) { | |
db01f8b3 MS |
2176 | /* Forking services may occasionally move to a new PID. |
2177 | * As long as they update the PID file before exiting the old | |
2178 | * PID, they're fine. */ | |
5375410b | 2179 | if (service_load_pid_file(s, false) == 0) |
db01f8b3 | 2180 | return; |
034c6ed7 | 2181 | |
034c6ed7 | 2182 | s->main_pid = 0; |
6ea832a2 | 2183 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); |
034c6ed7 | 2184 | |
867b3b7d | 2185 | if (s->main_command) { |
fbeefb45 LP |
2186 | /* If this is not a forking service than the |
2187 | * main process got started and hence we copy | |
2188 | * the exit status so that it is recorded both | |
2189 | * as main and as control process exit | |
2190 | * status */ | |
2191 | ||
867b3b7d | 2192 | s->main_command->exec_status = s->main_exec_status; |
b708e7ce | 2193 | |
867b3b7d | 2194 | if (s->main_command->ignore) |
f42806df | 2195 | f = SERVICE_SUCCESS; |
fbeefb45 LP |
2196 | } else if (s->exec_command[SERVICE_EXEC_START]) { |
2197 | ||
2198 | /* If this is a forked process, then we should | |
2199 | * ignore the return value if this was | |
2200 | * configured for the starter process */ | |
2201 | ||
2202 | if (s->exec_command[SERVICE_EXEC_START]->ignore) | |
2203 | f = SERVICE_SUCCESS; | |
034c6ed7 LP |
2204 | } |
2205 | ||
bbc9006e MT |
2206 | log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, |
2207 | u->id, | |
23635a85 ZJS |
2208 | "MESSAGE=%s: main process exited, code=%s, status=%i/%s", |
2209 | u->id, sigchld_code_to_string(code), status, | |
2210 | strna(code == CLD_EXITED | |
2211 | ? exit_status_to_string(status, EXIT_STATUS_FULL) | |
2212 | : signal_to_string(status)), | |
23635a85 ZJS |
2213 | "EXIT_CODE=%s", sigchld_code_to_string(code), |
2214 | "EXIT_STATUS=%i", status, | |
2215 | NULL); | |
f42806df LP |
2216 | |
2217 | if (f != SERVICE_SUCCESS) | |
2218 | s->result = f; | |
034c6ed7 | 2219 | |
867b3b7d LP |
2220 | if (s->main_command && |
2221 | s->main_command->command_next && | |
f42806df | 2222 | f == SERVICE_SUCCESS) { |
034c6ed7 | 2223 | |
34e9ba66 LP |
2224 | /* There is another command to * |
2225 | * execute, so let's do that. */ | |
034c6ed7 | 2226 | |
66870f90 ZJS |
2227 | log_debug_unit(u->id, |
2228 | "%s running next main command for state %s", | |
2229 | u->id, service_state_to_string(s->state)); | |
f42806df | 2230 | service_run_next_main(s); |
034c6ed7 | 2231 | |
34e9ba66 LP |
2232 | } else { |
2233 | ||
2234 | /* The service exited, so the service is officially | |
2235 | * gone. */ | |
867b3b7d | 2236 | s->main_command = NULL; |
34e9ba66 LP |
2237 | |
2238 | switch (s->state) { | |
2239 | ||
2240 | case SERVICE_START_POST: | |
2241 | case SERVICE_RELOAD: | |
2242 | case SERVICE_STOP: | |
2243 | /* Need to wait until the operation is | |
2244 | * done */ | |
c4653a4d | 2245 | break; |
7d55e835 | 2246 | |
34e9ba66 LP |
2247 | case SERVICE_START: |
2248 | if (s->type == SERVICE_ONESHOT) { | |
2249 | /* This was our main goal, so let's go on */ | |
f42806df | 2250 | if (f == SERVICE_SUCCESS) |
34e9ba66 LP |
2251 | service_enter_start_post(s); |
2252 | else | |
f42806df | 2253 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
34e9ba66 | 2254 | break; |
34e9ba66 | 2255 | } |
034c6ed7 | 2256 | |
bfba3256 LP |
2257 | /* Fall through */ |
2258 | ||
34e9ba66 | 2259 | case SERVICE_RUNNING: |
f42806df | 2260 | service_enter_running(s, f); |
34e9ba66 | 2261 | break; |
034c6ed7 | 2262 | |
34e9ba66 LP |
2263 | case SERVICE_STOP_SIGTERM: |
2264 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 2265 | |
34e9ba66 | 2266 | if (!control_pid_good(s)) |
f42806df | 2267 | service_enter_stop_post(s, f); |
5cb5a6ff | 2268 | |
34e9ba66 LP |
2269 | /* If there is still a control process, wait for that first */ |
2270 | break; | |
2271 | ||
bf108e55 LP |
2272 | case SERVICE_STOP_POST: |
2273 | case SERVICE_FINAL_SIGTERM: | |
2274 | case SERVICE_FINAL_SIGKILL: | |
2275 | ||
2276 | if (!control_pid_good(s)) | |
2277 | service_enter_dead(s, f, true); | |
2278 | break; | |
2279 | ||
34e9ba66 LP |
2280 | default: |
2281 | assert_not_reached("Uh, main process died at wrong time."); | |
2282 | } | |
034c6ed7 | 2283 | } |
5cb5a6ff | 2284 | |
034c6ed7 | 2285 | } else if (s->control_pid == pid) { |
34e9ba66 LP |
2286 | s->control_pid = 0; |
2287 | ||
b708e7ce | 2288 | if (s->control_command) { |
117dcc57 ZJS |
2289 | exec_status_exit(&s->control_command->exec_status, |
2290 | &s->exec_context, pid, code, status); | |
a16e1123 | 2291 | |
b708e7ce | 2292 | if (s->control_command->ignore) |
f42806df | 2293 | f = SERVICE_SUCCESS; |
b708e7ce LP |
2294 | } |
2295 | ||
66870f90 ZJS |
2296 | log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id, |
2297 | "%s: control process exited, code=%s status=%i", | |
2298 | u->id, sigchld_code_to_string(code), status); | |
f42806df LP |
2299 | |
2300 | if (f != SERVICE_SUCCESS) | |
2301 | s->result = f; | |
034c6ed7 | 2302 | |
88f3e0c9 LP |
2303 | /* Immediately get rid of the cgroup, so that the |
2304 | * kernel doesn't delay the cgroup empty messages for | |
2305 | * the service cgroup any longer than necessary */ | |
4ad49000 | 2306 | service_kill_control_processes(s); |
88f3e0c9 | 2307 | |
34e9ba66 LP |
2308 | if (s->control_command && |
2309 | s->control_command->command_next && | |
f42806df | 2310 | f == SERVICE_SUCCESS) { |
034c6ed7 LP |
2311 | |
2312 | /* There is another command to * | |
2313 | * execute, so let's do that. */ | |
2314 | ||
66870f90 ZJS |
2315 | log_debug_unit(u->id, |
2316 | "%s running next control command for state %s", | |
2317 | u->id, service_state_to_string(s->state)); | |
f42806df | 2318 | service_run_next_control(s); |
034c6ed7 | 2319 | |
80876c20 | 2320 | } else { |
034c6ed7 LP |
2321 | /* No further commands for this step, so let's |
2322 | * figure out what to do next */ | |
2323 | ||
a16e1123 LP |
2324 | s->control_command = NULL; |
2325 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2326 | ||
66870f90 ZJS |
2327 | log_debug_unit(u->id, |
2328 | "%s got final SIGCHLD for state %s", | |
2329 | u->id, service_state_to_string(s->state)); | |
bd982a8b | 2330 | |
034c6ed7 LP |
2331 | switch (s->state) { |
2332 | ||
2333 | case SERVICE_START_PRE: | |
f42806df | 2334 | if (f == SERVICE_SUCCESS) |
034c6ed7 LP |
2335 | service_enter_start(s); |
2336 | else | |
f42806df | 2337 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
034c6ed7 LP |
2338 | break; |
2339 | ||
2340 | case SERVICE_START: | |
bfba3256 LP |
2341 | if (s->type != SERVICE_FORKING) |
2342 | /* Maybe spurious event due to a reload that changed the type? */ | |
2343 | break; | |
034c6ed7 | 2344 | |
f42806df LP |
2345 | if (f != SERVICE_SUCCESS) { |
2346 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
3a111838 MS |
2347 | break; |
2348 | } | |
034c6ed7 | 2349 | |
3a111838 | 2350 | if (s->pid_file) { |
f42806df LP |
2351 | bool has_start_post; |
2352 | int r; | |
2353 | ||
3a111838 MS |
2354 | /* Let's try to load the pid file here if we can. |
2355 | * The PID file might actually be created by a START_POST | |
2356 | * script. In that case don't worry if the loading fails. */ | |
f42806df LP |
2357 | |
2358 | has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST]; | |
2359 | r = service_load_pid_file(s, !has_start_post); | |
3a111838 MS |
2360 | if (!has_start_post && r < 0) { |
2361 | r = service_demand_pid_file(s); | |
2362 | if (r < 0 || !cgroup_good(s)) | |
f42806df | 2363 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
3a111838 MS |
2364 | break; |
2365 | } | |
034c6ed7 | 2366 | } else |
3a111838 | 2367 | service_search_main_pid(s); |
034c6ed7 | 2368 | |
3a111838 | 2369 | service_enter_start_post(s); |
034c6ed7 LP |
2370 | break; |
2371 | ||
2372 | case SERVICE_START_POST: | |
f42806df LP |
2373 | if (f != SERVICE_SUCCESS) { |
2374 | service_enter_stop(s, f); | |
2096e009 | 2375 | break; |
034c6ed7 LP |
2376 | } |
2377 | ||
2096e009 | 2378 | if (s->pid_file) { |
f42806df LP |
2379 | int r; |
2380 | ||
2381 | r = service_load_pid_file(s, true); | |
2096e009 MS |
2382 | if (r < 0) { |
2383 | r = service_demand_pid_file(s); | |
2384 | if (r < 0 || !cgroup_good(s)) | |
f42806df | 2385 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
2096e009 MS |
2386 | break; |
2387 | } | |
2388 | } else | |
2389 | service_search_main_pid(s); | |
2390 | ||
f42806df | 2391 | service_enter_running(s, SERVICE_SUCCESS); |
3185a36b | 2392 | break; |
034c6ed7 LP |
2393 | |
2394 | case SERVICE_RELOAD: | |
f42806df | 2395 | if (f == SERVICE_SUCCESS) { |
5375410b | 2396 | service_load_pid_file(s, true); |
3185a36b LP |
2397 | service_search_main_pid(s); |
2398 | } | |
2399 | ||
f42806df LP |
2400 | s->reload_result = f; |
2401 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
2402 | break; |
2403 | ||
2404 | case SERVICE_STOP: | |
f42806df | 2405 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
2406 | break; |
2407 | ||
2408 | case SERVICE_STOP_SIGTERM: | |
2409 | case SERVICE_STOP_SIGKILL: | |
2410 | if (main_pid_good(s) <= 0) | |
f42806df | 2411 | service_enter_stop_post(s, f); |
034c6ed7 LP |
2412 | |
2413 | /* If there is still a service | |
2414 | * process around, wait until | |
2415 | * that one quit, too */ | |
2416 | break; | |
2417 | ||
2418 | case SERVICE_STOP_POST: | |
2419 | case SERVICE_FINAL_SIGTERM: | |
2420 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 LP |
2421 | if (main_pid_good(s) <= 0) |
2422 | service_enter_dead(s, f, true); | |
034c6ed7 LP |
2423 | break; |
2424 | ||
2425 | default: | |
2426 | assert_not_reached("Uh, control process died at wrong time."); | |
2427 | } | |
2428 | } | |
8c47c732 | 2429 | } |
c4e2ceae LP |
2430 | |
2431 | /* Notify clients about changed exit status */ | |
2432 | unit_add_to_dbus_queue(u); | |
a911bb9a LP |
2433 | |
2434 | /* We got one SIGCHLD for the service, let's watch all | |
2435 | * processes that are now running of the service, and watch | |
2436 | * that. Among the PIDs we then watch will be children | |
2437 | * reassigned to us, which hopefully allows us to identify | |
2438 | * when all children are gone */ | |
2439 | unit_tidy_watch_pids(u, s->main_pid, s->control_pid); | |
2440 | unit_watch_all_pids(u); | |
2441 | ||
2442 | /* If the PID set is empty now, then let's finish this off */ | |
2443 | if (set_isempty(u->pids)) | |
2444 | service_notify_cgroup_empty_event(u); | |
034c6ed7 LP |
2445 | } |
2446 | ||
718db961 LP |
2447 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
2448 | Service *s = SERVICE(userdata); | |
034c6ed7 LP |
2449 | |
2450 | assert(s); | |
718db961 | 2451 | assert(source == s->timer_event_source); |
034c6ed7 LP |
2452 | |
2453 | switch (s->state) { | |
2454 | ||
2455 | case SERVICE_START_PRE: | |
2456 | case SERVICE_START: | |
718db961 | 2457 | log_warning_unit(UNIT(s)->id, |
41aef6fc ZJS |
2458 | "%s %s operation timed out. Terminating.", |
2459 | UNIT(s)->id, | |
2460 | s->state == SERVICE_START ? "start" : "start-pre"); | |
f42806df | 2461 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
80876c20 LP |
2462 | break; |
2463 | ||
034c6ed7 | 2464 | case SERVICE_START_POST: |
718db961 | 2465 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2466 | "%s start-post operation timed out. Stopping.", UNIT(s)->id); |
f42806df | 2467 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2468 | break; |
2469 | ||
e2f3b44c | 2470 | case SERVICE_RELOAD: |
718db961 | 2471 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2472 | "%s reload operation timed out. Stopping.", UNIT(s)->id); |
f42806df LP |
2473 | s->reload_result = SERVICE_FAILURE_TIMEOUT; |
2474 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c LP |
2475 | break; |
2476 | ||
034c6ed7 | 2477 | case SERVICE_STOP: |
718db961 LP |
2478 | log_warning_unit(UNIT(s)->id, |
2479 | "%s stopping timed out. Terminating.", UNIT(s)->id); | |
f42806df | 2480 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2481 | break; |
2482 | ||
2483 | case SERVICE_STOP_SIGTERM: | |
4819ff03 | 2484 | if (s->kill_context.send_sigkill) { |
718db961 | 2485 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2486 | "%s stop-sigterm timed out. Killing.", UNIT(s)->id); |
f42806df | 2487 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 2488 | } else { |
718db961 | 2489 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2490 | "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id); |
f42806df | 2491 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
ba035df2 LP |
2492 | } |
2493 | ||
034c6ed7 LP |
2494 | break; |
2495 | ||
2496 | case SERVICE_STOP_SIGKILL: | |
35b8ca3a | 2497 | /* Uh, we sent a SIGKILL and it is still not gone? |
034c6ed7 LP |
2498 | * Must be something we cannot kill, so let's just be |
2499 | * weirded out and continue */ | |
2500 | ||
718db961 LP |
2501 | log_warning_unit(UNIT(s)->id, |
2502 | "%s still around after SIGKILL. Ignoring.", UNIT(s)->id); | |
f42806df | 2503 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2504 | break; |
2505 | ||
2506 | case SERVICE_STOP_POST: | |
718db961 | 2507 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2508 | "%s stop-post timed out. Terminating.", UNIT(s)->id); |
f42806df | 2509 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2510 | break; |
2511 | ||
2512 | case SERVICE_FINAL_SIGTERM: | |
4819ff03 | 2513 | if (s->kill_context.send_sigkill) { |
718db961 | 2514 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2515 | "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id); |
f42806df | 2516 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 2517 | } else { |
718db961 | 2518 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2519 | "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", |
718db961 | 2520 | UNIT(s)->id); |
f42806df | 2521 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
ba035df2 LP |
2522 | } |
2523 | ||
034c6ed7 LP |
2524 | break; |
2525 | ||
2526 | case SERVICE_FINAL_SIGKILL: | |
718db961 | 2527 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2528 | "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id); |
f42806df | 2529 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
034c6ed7 LP |
2530 | break; |
2531 | ||
2532 | case SERVICE_AUTO_RESTART: | |
718db961 | 2533 | log_info_unit(UNIT(s)->id, |
ef417cfd ZJS |
2534 | s->restart_usec > 0 ? |
2535 | "%s holdoff time over, scheduling restart." : | |
2536 | "%s has no holdoff time, scheduling restart.", | |
2537 | UNIT(s)->id); | |
034c6ed7 LP |
2538 | service_enter_restart(s); |
2539 | break; | |
2540 | ||
2541 | default: | |
2542 | assert_not_reached("Timeout at wrong time."); | |
2543 | } | |
718db961 LP |
2544 | |
2545 | return 0; | |
2546 | } | |
2547 | ||
2548 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { | |
2549 | Service *s = SERVICE(userdata); | |
2550 | ||
2551 | assert(s); | |
2552 | assert(source == s->watchdog_event_source); | |
2553 | ||
842129f5 LP |
2554 | log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id); |
2555 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG); | |
2556 | ||
718db961 | 2557 | return 0; |
5cb5a6ff LP |
2558 | } |
2559 | ||
c952c6ec | 2560 | static void service_notify_message(Unit *u, pid_t pid, char **tags) { |
8c47c732 LP |
2561 | Service *s = SERVICE(u); |
2562 | const char *e; | |
30b5275a | 2563 | bool notify_dbus = false; |
8c47c732 LP |
2564 | |
2565 | assert(u); | |
2566 | ||
ccd06097 ZJS |
2567 | log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)", |
2568 | u->id, pid, tags && *tags ? tags[0] : "(empty)"); | |
da13d4d2 | 2569 | |
c952c6ec | 2570 | if (s->notify_access == NOTIFY_NONE) { |
66870f90 | 2571 | log_warning_unit(u->id, |
ccd06097 ZJS |
2572 | "%s: Got notification message from PID "PID_FMT", but reception is disabled.", |
2573 | u->id, pid); | |
c952c6ec LP |
2574 | return; |
2575 | } | |
2576 | ||
336c6e46 LP |
2577 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { |
2578 | ||
2579 | if (s->main_pid != 0) | |
2580 | log_warning_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, u->id, pid, s->main_pid); | |
2581 | else | |
3a33e61d | 2582 | log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", u->id, pid); |
c952c6ec LP |
2583 | return; |
2584 | } | |
2585 | ||
8c47c732 LP |
2586 | /* Interpret MAINPID= */ |
2587 | if ((e = strv_find_prefix(tags, "MAINPID=")) && | |
2588 | (s->state == SERVICE_START || | |
2589 | s->state == SERVICE_START_POST || | |
2590 | s->state == SERVICE_RUNNING || | |
2591 | s->state == SERVICE_RELOAD)) { | |
8c47c732 | 2592 | |
5925dd3c | 2593 | if (parse_pid(e + 8, &pid) < 0) |
30b5275a | 2594 | log_warning_unit(u->id, "Failed to parse notification message %s", e); |
8c47c732 | 2595 | else { |
30b5275a | 2596 | log_debug_unit(u->id, "%s: got %s", u->id, e); |
5925dd3c | 2597 | service_set_main_pid(s, pid); |
7400b9d2 | 2598 | unit_watch_pid(UNIT(s), pid); |
30b5275a | 2599 | notify_dbus = true; |
8c47c732 LP |
2600 | } |
2601 | } | |
2602 | ||
2603 | /* Interpret READY= */ | |
30b5275a LP |
2604 | if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) { |
2605 | log_debug_unit(u->id, "%s: got READY=1", u->id); | |
8c47c732 | 2606 | service_enter_start_post(s); |
30b5275a | 2607 | notify_dbus = true; |
8c47c732 LP |
2608 | } |
2609 | ||
2610 | /* Interpret STATUS= */ | |
7f110ff9 LP |
2611 | e = strv_find_prefix(tags, "STATUS="); |
2612 | if (e) { | |
8c47c732 LP |
2613 | char *t; |
2614 | ||
3a2776bc | 2615 | if (e[7]) { |
7f110ff9 | 2616 | if (!utf8_is_valid(e+7)) { |
30b5275a | 2617 | log_warning_unit(u->id, "Status message in notification is not UTF-8 clean."); |
7f110ff9 LP |
2618 | return; |
2619 | } | |
2620 | ||
30b5275a LP |
2621 | log_debug_unit(u->id, "%s: got %s", u->id, e); |
2622 | ||
7f110ff9 LP |
2623 | t = strdup(e+7); |
2624 | if (!t) { | |
30b5275a | 2625 | log_oom(); |
3a2776bc LP |
2626 | return; |
2627 | } | |
2628 | ||
30b5275a LP |
2629 | } else |
2630 | t = NULL; | |
8c47c732 | 2631 | |
30b5275a | 2632 | if (!streq_ptr(s->status_text, t)) { |
3a2776bc LP |
2633 | free(s->status_text); |
2634 | s->status_text = t; | |
30b5275a LP |
2635 | notify_dbus = true; |
2636 | } else | |
2637 | free(t); | |
8c47c732 | 2638 | } |
842129f5 | 2639 | |
4774e357 MAA |
2640 | /* Interpret ERRNO= */ |
2641 | e = strv_find_prefix(tags, "ERRNO="); | |
2642 | if (e) { | |
2643 | int status_errno; | |
2644 | ||
2645 | if (safe_atoi(e + 6, &status_errno) < 0) | |
2646 | log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e); | |
2647 | else { | |
2648 | log_debug_unit(u->id, "%s: got %s", u->id, e); | |
2649 | ||
2650 | if (s->status_errno != status_errno) { | |
2651 | s->status_errno = status_errno; | |
2652 | notify_dbus = true; | |
2653 | } | |
2654 | } | |
2655 | } | |
2656 | ||
6f285378 | 2657 | /* Interpret WATCHDOG= */ |
a6927d7f | 2658 | if (strv_find(tags, "WATCHDOG=1")) { |
842129f5 LP |
2659 | log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id); |
2660 | service_reset_watchdog(s); | |
a6927d7f | 2661 | } |
c4e2ceae LP |
2662 | |
2663 | /* Notify clients about changed status or main pid */ | |
30b5275a LP |
2664 | if (notify_dbus) |
2665 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
2666 | } |
2667 | ||
68db7a3b ZJS |
2668 | static int service_get_timeout(Unit *u, uint64_t *timeout) { |
2669 | Service *s = SERVICE(u); | |
2670 | int r; | |
2671 | ||
2672 | if (!s->timer_event_source) | |
2673 | return 0; | |
2674 | ||
2675 | r = sd_event_source_get_time(s->timer_event_source, timeout); | |
2676 | if (r < 0) | |
2677 | return r; | |
2678 | ||
2679 | return 1; | |
2680 | } | |
2681 | ||
05e343b7 LP |
2682 | static void service_bus_name_owner_change( |
2683 | Unit *u, | |
2684 | const char *name, | |
2685 | const char *old_owner, | |
2686 | const char *new_owner) { | |
2687 | ||
2688 | Service *s = SERVICE(u); | |
718db961 | 2689 | int r; |
05e343b7 LP |
2690 | |
2691 | assert(s); | |
2692 | assert(name); | |
2693 | ||
2694 | assert(streq(s->bus_name, name)); | |
2695 | assert(old_owner || new_owner); | |
2696 | ||
2697 | if (old_owner && new_owner) | |
66870f90 ZJS |
2698 | log_debug_unit(u->id, |
2699 | "%s's D-Bus name %s changed owner from %s to %s", | |
2700 | u->id, name, old_owner, new_owner); | |
05e343b7 | 2701 | else if (old_owner) |
66870f90 ZJS |
2702 | log_debug_unit(u->id, |
2703 | "%s's D-Bus name %s no longer registered by %s", | |
2704 | u->id, name, old_owner); | |
05e343b7 | 2705 | else |
66870f90 ZJS |
2706 | log_debug_unit(u->id, |
2707 | "%s's D-Bus name %s now registered by %s", | |
2708 | u->id, name, new_owner); | |
05e343b7 LP |
2709 | |
2710 | s->bus_name_good = !!new_owner; | |
2711 | ||
2712 | if (s->type == SERVICE_DBUS) { | |
2713 | ||
2714 | /* service_enter_running() will figure out what to | |
2715 | * do */ | |
2716 | if (s->state == SERVICE_RUNNING) | |
f42806df | 2717 | service_enter_running(s, SERVICE_SUCCESS); |
05e343b7 LP |
2718 | else if (s->state == SERVICE_START && new_owner) |
2719 | service_enter_start_post(s); | |
2720 | ||
2721 | } else if (new_owner && | |
2722 | s->main_pid <= 0 && | |
2723 | (s->state == SERVICE_START || | |
2724 | s->state == SERVICE_START_POST || | |
2725 | s->state == SERVICE_RUNNING || | |
2726 | s->state == SERVICE_RELOAD)) { | |
2727 | ||
5b12334d | 2728 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
718db961 | 2729 | pid_t pid; |
05e343b7 | 2730 | |
718db961 | 2731 | /* Try to acquire PID from bus service */ |
05e343b7 | 2732 | |
49b832c5 | 2733 | r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds); |
5b12334d LP |
2734 | if (r >= 0) |
2735 | r = sd_bus_creds_get_pid(creds, &pid); | |
718db961 LP |
2736 | if (r >= 0) { |
2737 | log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid); | |
05e343b7 | 2738 | |
718db961 LP |
2739 | service_set_main_pid(s, pid); |
2740 | unit_watch_pid(UNIT(s), pid); | |
2741 | } | |
7400b9d2 | 2742 | } |
05e343b7 LP |
2743 | } |
2744 | ||
6cf6bbc2 | 2745 | int service_set_socket_fd(Service *s, int fd, Socket *sock) { |
79a98c60 LP |
2746 | _cleanup_free_ char *peer = NULL; |
2747 | int r; | |
57020a3a | 2748 | |
4f2d528d LP |
2749 | assert(s); |
2750 | assert(fd >= 0); | |
2751 | ||
2752 | /* This is called by the socket code when instantiating a new | |
2753 | * service for a stream socket and the socket needs to be | |
2754 | * configured. */ | |
2755 | ||
1124fe6f | 2756 | if (UNIT(s)->load_state != UNIT_LOADED) |
4f2d528d LP |
2757 | return -EINVAL; |
2758 | ||
2759 | if (s->socket_fd >= 0) | |
2760 | return -EBUSY; | |
2761 | ||
2762 | if (s->state != SERVICE_DEAD) | |
2763 | return -EAGAIN; | |
2764 | ||
79a98c60 LP |
2765 | if (getpeername_pretty(fd, &peer) >= 0) { |
2766 | ||
2767 | if (UNIT(s)->description) { | |
2768 | _cleanup_free_ char *a; | |
2769 | ||
2770 | a = strjoin(UNIT(s)->description, " (", peer, ")", NULL); | |
2771 | if (!a) | |
2772 | return -ENOMEM; | |
2773 | ||
2774 | r = unit_set_description(UNIT(s), a); | |
2775 | } else | |
2776 | r = unit_set_description(UNIT(s), peer); | |
2777 | ||
2778 | if (r < 0) | |
2779 | return r; | |
2780 | } | |
2781 | ||
4f2d528d | 2782 | s->socket_fd = fd; |
6cf6bbc2 | 2783 | |
57020a3a LP |
2784 | unit_ref_set(&s->accept_socket, UNIT(sock)); |
2785 | ||
2786 | return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false); | |
4f2d528d LP |
2787 | } |
2788 | ||
fdf20a31 | 2789 | static void service_reset_failed(Unit *u) { |
5632e374 LP |
2790 | Service *s = SERVICE(u); |
2791 | ||
2792 | assert(s); | |
2793 | ||
fdf20a31 | 2794 | if (s->state == SERVICE_FAILED) |
5632e374 LP |
2795 | service_set_state(s, SERVICE_DEAD); |
2796 | ||
f42806df LP |
2797 | s->result = SERVICE_SUCCESS; |
2798 | s->reload_result = SERVICE_SUCCESS; | |
451b34cc LP |
2799 | |
2800 | RATELIMIT_RESET(s->start_limit); | |
5632e374 LP |
2801 | } |
2802 | ||
718db961 | 2803 | static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
8a0867d6 | 2804 | Service *s = SERVICE(u); |
41efeaec | 2805 | |
814cc562 | 2806 | return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error); |
8a0867d6 LP |
2807 | } |
2808 | ||
94f04347 LP |
2809 | static const char* const service_state_table[_SERVICE_STATE_MAX] = { |
2810 | [SERVICE_DEAD] = "dead", | |
2811 | [SERVICE_START_PRE] = "start-pre", | |
2812 | [SERVICE_START] = "start", | |
2813 | [SERVICE_START_POST] = "start-post", | |
2814 | [SERVICE_RUNNING] = "running", | |
80876c20 | 2815 | [SERVICE_EXITED] = "exited", |
94f04347 LP |
2816 | [SERVICE_RELOAD] = "reload", |
2817 | [SERVICE_STOP] = "stop", | |
2818 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", | |
2819 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
2820 | [SERVICE_STOP_POST] = "stop-post", | |
2821 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
2822 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 2823 | [SERVICE_FAILED] = "failed", |
94f04347 LP |
2824 | [SERVICE_AUTO_RESTART] = "auto-restart", |
2825 | }; | |
2826 | ||
2827 | DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); | |
2828 | ||
2829 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { | |
525ee6f4 LP |
2830 | [SERVICE_RESTART_NO] = "no", |
2831 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
50caaedb | 2832 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", |
6cfe2fde | 2833 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
dc99a976 | 2834 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
50caaedb | 2835 | [SERVICE_RESTART_ON_ABORT] = "on-abort", |
6cfe2fde | 2836 | [SERVICE_RESTART_ALWAYS] = "always", |
94f04347 LP |
2837 | }; |
2838 | ||
2839 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
2840 | ||
2841 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
94f04347 | 2842 | [SERVICE_SIMPLE] = "simple", |
0d624a78 | 2843 | [SERVICE_FORKING] = "forking", |
34e9ba66 | 2844 | [SERVICE_ONESHOT] = "oneshot", |
8c47c732 | 2845 | [SERVICE_DBUS] = "dbus", |
f2b68789 LP |
2846 | [SERVICE_NOTIFY] = "notify", |
2847 | [SERVICE_IDLE] = "idle" | |
94f04347 LP |
2848 | }; |
2849 | ||
2850 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
2851 | ||
e537352b | 2852 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
94f04347 LP |
2853 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", |
2854 | [SERVICE_EXEC_START] = "ExecStart", | |
2855 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
2856 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
2857 | [SERVICE_EXEC_STOP] = "ExecStop", | |
2858 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
2859 | }; | |
2860 | ||
2861 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
2862 | ||
c952c6ec LP |
2863 | static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = { |
2864 | [NOTIFY_NONE] = "none", | |
2865 | [NOTIFY_MAIN] = "main", | |
2866 | [NOTIFY_ALL] = "all" | |
2867 | }; | |
2868 | ||
2869 | DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess); | |
2870 | ||
f42806df LP |
2871 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
2872 | [SERVICE_SUCCESS] = "success", | |
2873 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
2874 | [SERVICE_FAILURE_TIMEOUT] = "timeout", | |
2875 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
2876 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
bb242b7b | 2877 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", |
8d1b002a LP |
2878 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", |
2879 | [SERVICE_FAILURE_START_LIMIT] = "start-limit" | |
f42806df LP |
2880 | }; |
2881 | ||
2882 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
2883 | ||
bf500566 MO |
2884 | static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = { |
2885 | [SERVICE_FAILURE_ACTION_NONE] = "none", | |
2886 | [SERVICE_FAILURE_ACTION_REBOOT] = "reboot", | |
2887 | [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force", | |
2888 | [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate" | |
4b939747 | 2889 | }; |
bf500566 | 2890 | DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction); |
4b939747 | 2891 | |
87f0e418 | 2892 | const UnitVTable service_vtable = { |
7d17cfbc | 2893 | .object_size = sizeof(Service), |
718db961 LP |
2894 | .exec_context_offset = offsetof(Service, exec_context), |
2895 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
2896 | .kill_context_offset = offsetof(Service, kill_context), | |
613b411c | 2897 | .exec_runtime_offset = offsetof(Service, exec_runtime), |
3ef63c31 | 2898 | |
f975e971 LP |
2899 | .sections = |
2900 | "Unit\0" | |
2901 | "Service\0" | |
2902 | "Install\0", | |
4ad49000 | 2903 | .private_section = "Service", |
71645aca | 2904 | |
034c6ed7 LP |
2905 | .init = service_init, |
2906 | .done = service_done, | |
a16e1123 LP |
2907 | .load = service_load, |
2908 | ||
2909 | .coldplug = service_coldplug, | |
034c6ed7 | 2910 | |
5cb5a6ff LP |
2911 | .dump = service_dump, |
2912 | ||
2913 | .start = service_start, | |
2914 | .stop = service_stop, | |
2915 | .reload = service_reload, | |
2916 | ||
034c6ed7 LP |
2917 | .can_reload = service_can_reload, |
2918 | ||
8a0867d6 LP |
2919 | .kill = service_kill, |
2920 | ||
a16e1123 LP |
2921 | .serialize = service_serialize, |
2922 | .deserialize_item = service_deserialize_item, | |
2923 | ||
5cb5a6ff | 2924 | .active_state = service_active_state, |
10a94420 | 2925 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 2926 | |
701cc384 LP |
2927 | .check_gc = service_check_gc, |
2928 | .check_snapshot = service_check_snapshot, | |
2929 | ||
034c6ed7 | 2930 | .sigchld_event = service_sigchld_event, |
2c4104f0 | 2931 | |
fdf20a31 | 2932 | .reset_failed = service_reset_failed, |
5632e374 | 2933 | |
4ad49000 | 2934 | .notify_cgroup_empty = service_notify_cgroup_empty_event, |
8c47c732 | 2935 | .notify_message = service_notify_message, |
8e274523 | 2936 | |
05e343b7 | 2937 | .bus_name_owner_change = service_bus_name_owner_change, |
05e343b7 | 2938 | |
c4e2ceae | 2939 | .bus_interface = "org.freedesktop.systemd1.Service", |
718db961 | 2940 | .bus_vtable = bus_service_vtable, |
74c964d3 LP |
2941 | .bus_set_property = bus_service_set_property, |
2942 | .bus_commit_properties = bus_service_commit_properties, | |
4139c1b2 | 2943 | |
68db7a3b | 2944 | .get_timeout = service_get_timeout, |
718db961 LP |
2945 | .can_transient = true, |
2946 | ||
c6918296 MS |
2947 | .status_message_formats = { |
2948 | .starting_stopping = { | |
2949 | [0] = "Starting %s...", | |
2950 | [1] = "Stopping %s...", | |
2951 | }, | |
2952 | .finished_start_job = { | |
2953 | [JOB_DONE] = "Started %s.", | |
2954 | [JOB_FAILED] = "Failed to start %s.", | |
2955 | [JOB_DEPENDENCY] = "Dependency failed for %s.", | |
2956 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
2957 | }, | |
2958 | .finished_stop_job = { | |
2959 | [JOB_DONE] = "Stopped %s.", | |
2960 | [JOB_FAILED] = "Stopped (with error) %s.", | |
2961 | [JOB_TIMEOUT] = "Timed out stopping %s.", | |
2962 | }, | |
2963 | }, | |
5cb5a6ff | 2964 | }; |