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