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