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