]>
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, | |
57614eb1 | 810 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
842129f5 LP |
811 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
812 | SERVICE_AUTO_RESTART)) | |
718db961 | 813 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
034c6ed7 | 814 | |
842129f5 LP |
815 | if (!IN_SET(state, |
816 | SERVICE_START, SERVICE_START_POST, | |
817 | SERVICE_RUNNING, SERVICE_RELOAD, | |
57614eb1 | 818 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf108e55 | 819 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
5e94833f | 820 | service_unwatch_main_pid(s); |
867b3b7d LP |
821 | s->main_command = NULL; |
822 | } | |
034c6ed7 | 823 | |
842129f5 LP |
824 | if (!IN_SET(state, |
825 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
826 | SERVICE_RELOAD, | |
57614eb1 | 827 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
842129f5 | 828 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
5e94833f | 829 | service_unwatch_control_pid(s); |
034c6ed7 | 830 | s->control_command = NULL; |
a16e1123 | 831 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
e537352b | 832 | } |
034c6ed7 | 833 | |
a911bb9a LP |
834 | if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
835 | unit_unwatch_all_pids(UNIT(s)); | |
836 | ||
842129f5 LP |
837 | if (!IN_SET(state, |
838 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
839 | SERVICE_RUNNING, SERVICE_RELOAD, | |
57614eb1 LP |
840 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
841 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) && | |
1124fe6f | 842 | !(state == SERVICE_DEAD && UNIT(s)->job)) { |
4f2d528d | 843 | service_close_socket_fd(s); |
6cf6bbc2 LP |
844 | service_connection_unref(s); |
845 | } | |
4f2d528d | 846 | |
7596e9e1 | 847 | if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
a6927d7f MO |
848 | service_stop_watchdog(s); |
849 | ||
f6023656 LP |
850 | /* For the inactive states unit_notify() will trim the cgroup, |
851 | * but for exit we have to do that ourselves... */ | |
1124fe6f | 852 | if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0) |
b1491eba | 853 | unit_destroy_cgroup_if_empty(UNIT(s)); |
f6023656 | 854 | |
9cd86184 OB |
855 | /* For remain_after_exit services, let's see if we can "release" the |
856 | * hold on the console, since unit_notify() only does that in case of | |
857 | * change of state */ | |
f49650ce LP |
858 | if (state == SERVICE_EXITED && |
859 | s->remain_after_exit && | |
9cd86184 | 860 | UNIT(s)->manager->n_on_console > 0) { |
f49650ce LP |
861 | |
862 | ExecContext *ec; | |
863 | ||
864 | ec = unit_get_exec_context(UNIT(s)); | |
9cd86184 OB |
865 | if (ec && exec_context_may_touch_console(ec)) { |
866 | Manager *m = UNIT(s)->manager; | |
867 | ||
868 | m->n_on_console --; | |
869 | if (m->n_on_console == 0) | |
870 | /* unset no_console_output flag, since the console is free */ | |
871 | m->no_console_output = false; | |
872 | } | |
873 | } | |
874 | ||
e537352b | 875 | if (old_state != state) |
79008bdd | 876 | 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 | 877 | |
e056b01d | 878 | unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS); |
f42806df | 879 | s->reload_result = SERVICE_SUCCESS; |
034c6ed7 LP |
880 | } |
881 | ||
6e392c9c | 882 | static int service_coldplug(Unit *u, Hashmap *deferred_work) { |
a16e1123 LP |
883 | Service *s = SERVICE(u); |
884 | int r; | |
885 | ||
886 | assert(s); | |
887 | assert(s->state == SERVICE_DEAD); | |
888 | ||
889 | if (s->deserialized_state != s->state) { | |
890 | ||
bf108e55 LP |
891 | if (IN_SET(s->deserialized_state, |
892 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
893 | SERVICE_RELOAD, | |
57614eb1 | 894 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf108e55 | 895 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
92c1622e LP |
896 | |
897 | usec_t k; | |
6c12b52e | 898 | |
bf108e55 | 899 | 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 | 900 | |
92c1622e LP |
901 | /* For the start/stop timeouts 0 means off */ |
902 | if (k > 0) { | |
903 | r = service_arm_timer(s, k); | |
36697dc0 | 904 | if (r < 0) |
e558336f LP |
905 | return r; |
906 | } | |
907 | } | |
a16e1123 | 908 | |
92c1622e LP |
909 | if (s->deserialized_state == SERVICE_AUTO_RESTART) { |
910 | ||
911 | /* The restart timeouts 0 means immediately */ | |
912 | r = service_arm_timer(s, s->restart_usec); | |
913 | if (r < 0) | |
914 | return r; | |
915 | } | |
916 | ||
9f5650ae | 917 | if (pid_is_unwaited(s->main_pid) && |
bf108e55 LP |
918 | ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) || |
919 | IN_SET(s->deserialized_state, | |
920 | SERVICE_START, SERVICE_START_POST, | |
921 | SERVICE_RUNNING, SERVICE_RELOAD, | |
57614eb1 | 922 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf108e55 LP |
923 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) { |
924 | r = unit_watch_pid(UNIT(s), s->main_pid); | |
925 | if (r < 0) | |
926 | return r; | |
927 | } | |
a16e1123 | 928 | |
9f5650ae | 929 | if (pid_is_unwaited(s->control_pid) && |
bf108e55 LP |
930 | IN_SET(s->deserialized_state, |
931 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
932 | SERVICE_RELOAD, | |
57614eb1 | 933 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf108e55 LP |
934 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
935 | r = unit_watch_pid(UNIT(s), s->control_pid); | |
936 | if (r < 0) | |
937 | return r; | |
938 | } | |
a16e1123 | 939 | |
a911bb9a LP |
940 | if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
941 | unit_watch_all_pids(UNIT(s)); | |
942 | ||
842129f5 LP |
943 | if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
944 | service_start_watchdog(s); | |
bb242b7b | 945 | |
a16e1123 LP |
946 | service_set_state(s, s->deserialized_state); |
947 | } | |
92c1622e | 948 | |
a16e1123 LP |
949 | return 0; |
950 | } | |
951 | ||
44d8db9e | 952 | static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { |
a354329f LP |
953 | _cleanup_free_ int *rfds = NULL; |
954 | unsigned rn_fds = 0; | |
44d8db9e LP |
955 | Iterator i; |
956 | int r; | |
57020a3a | 957 | Unit *u; |
44d8db9e LP |
958 | |
959 | assert(s); | |
960 | assert(fds); | |
961 | assert(n_fds); | |
962 | ||
6cf6bbc2 LP |
963 | if (s->socket_fd >= 0) |
964 | return 0; | |
965 | ||
1124fe6f | 966 | SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) { |
44d8db9e LP |
967 | int *cfds; |
968 | unsigned cn_fds; | |
57020a3a LP |
969 | Socket *sock; |
970 | ||
ac155bb8 | 971 | if (u->type != UNIT_SOCKET) |
57020a3a LP |
972 | continue; |
973 | ||
974 | sock = SOCKET(u); | |
44d8db9e | 975 | |
117dcc57 ZJS |
976 | r = socket_collect_fds(sock, &cfds, &cn_fds); |
977 | if (r < 0) | |
a354329f | 978 | return r; |
44d8db9e | 979 | |
a354329f LP |
980 | if (cn_fds <= 0) { |
981 | free(cfds); | |
44d8db9e | 982 | continue; |
a354329f | 983 | } |
44d8db9e LP |
984 | |
985 | if (!rfds) { | |
986 | rfds = cfds; | |
987 | rn_fds = cn_fds; | |
988 | } else { | |
989 | int *t; | |
990 | ||
a354329f | 991 | t = realloc(rfds, (rn_fds + cn_fds) * sizeof(int)); |
117dcc57 | 992 | if (!t) { |
44d8db9e | 993 | free(cfds); |
a354329f | 994 | return -ENOMEM; |
44d8db9e LP |
995 | } |
996 | ||
a354329f LP |
997 | memcpy(t + rn_fds, cfds, cn_fds * sizeof(int)); |
998 | rfds = t; | |
999 | rn_fds += cn_fds; | |
1000 | ||
44d8db9e LP |
1001 | free(cfds); |
1002 | ||
44d8db9e LP |
1003 | } |
1004 | } | |
1005 | ||
a354329f LP |
1006 | if (s->n_fd_store > 0) { |
1007 | ServiceFDStore *fs; | |
1008 | int *t; | |
1009 | ||
1010 | t = realloc(rfds, (rn_fds + s->n_fd_store) * sizeof(int)); | |
1011 | if (!t) | |
1012 | return -ENOMEM; | |
1013 | ||
1014 | rfds = t; | |
1015 | LIST_FOREACH(fd_store, fs, s->fd_store) | |
1016 | rfds[rn_fds++] = fs->fd; | |
1017 | } | |
1018 | ||
44d8db9e LP |
1019 | *fds = rfds; |
1020 | *n_fds = rn_fds; | |
3e33402a | 1021 | |
a354329f | 1022 | rfds = NULL; |
44d8db9e | 1023 | return 0; |
44d8db9e LP |
1024 | } |
1025 | ||
81a2b7ce LP |
1026 | static int service_spawn( |
1027 | Service *s, | |
1028 | ExecCommand *c, | |
21b2ce39 | 1029 | usec_t timeout, |
81a2b7ce LP |
1030 | bool pass_fds, |
1031 | bool apply_permissions, | |
1032 | bool apply_chroot, | |
1e3ad081 | 1033 | bool apply_tty_stdin, |
ecedd90f | 1034 | bool is_control, |
81a2b7ce LP |
1035 | pid_t *_pid) { |
1036 | ||
034c6ed7 LP |
1037 | pid_t pid; |
1038 | int r; | |
117dcc57 | 1039 | int *fds = NULL; |
7fd1b19b | 1040 | _cleanup_free_ int *fdsbuf = NULL; |
2105e76a | 1041 | unsigned n_fds = 0, n_env = 0; |
e44da745 | 1042 | _cleanup_free_ char *bus_endpoint_path = NULL; |
7fd1b19b | 1043 | _cleanup_strv_free_ char |
117dcc57 | 1044 | **argv = NULL, **final_env = NULL, **our_env = NULL; |
4ad49000 | 1045 | const char *path; |
9fa95f85 DM |
1046 | ExecParameters exec_params = { |
1047 | .apply_permissions = apply_permissions, | |
1048 | .apply_chroot = apply_chroot, | |
1049 | .apply_tty_stdin = apply_tty_stdin, | |
e44da745 | 1050 | .bus_endpoint_fd = -1, |
16115b0a | 1051 | .selinux_context_net = s->socket_fd_selinux_context_net |
9fa95f85 | 1052 | }; |
034c6ed7 LP |
1053 | |
1054 | assert(s); | |
1055 | assert(c); | |
1056 | assert(_pid); | |
1057 | ||
5ad096b3 LP |
1058 | (void) unit_realize_cgroup(UNIT(s)); |
1059 | if (s->reset_cpu_usage) { | |
1060 | (void) unit_reset_cpu_usage(UNIT(s)); | |
1061 | s->reset_cpu_usage = false; | |
1062 | } | |
4ad49000 | 1063 | |
613b411c LP |
1064 | r = unit_setup_exec_runtime(UNIT(s)); |
1065 | if (r < 0) | |
1066 | goto fail; | |
1067 | ||
6cf6bbc2 LP |
1068 | if (pass_fds || |
1069 | s->exec_context.std_input == EXEC_INPUT_SOCKET || | |
1070 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1071 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1072 | ||
4f2d528d LP |
1073 | if (s->socket_fd >= 0) { |
1074 | fds = &s->socket_fd; | |
1075 | n_fds = 1; | |
6cf6bbc2 | 1076 | } else { |
117dcc57 ZJS |
1077 | r = service_collect_fds(s, &fdsbuf, &n_fds); |
1078 | if (r < 0) | |
6cf6bbc2 LP |
1079 | goto fail; |
1080 | ||
1081 | fds = fdsbuf; | |
1082 | } | |
4f2d528d | 1083 | } |
44d8db9e | 1084 | |
21b2ce39 LP |
1085 | if (timeout > 0) { |
1086 | r = service_arm_timer(s, timeout); | |
92c1622e LP |
1087 | if (r < 0) |
1088 | goto fail; | |
1089 | } else | |
1090 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
034c6ed7 | 1091 | |
19f6d710 LP |
1092 | r = unit_full_printf_strv(UNIT(s), c->argv, &argv); |
1093 | if (r < 0) | |
9e2f7c11 | 1094 | goto fail; |
9e2f7c11 | 1095 | |
3b1c5241 | 1096 | our_env = new0(char*, 6); |
97ae63e2 | 1097 | if (!our_env) { |
2105e76a LP |
1098 | r = -ENOMEM; |
1099 | goto fail; | |
1100 | } | |
c952c6ec | 1101 | |
a158dbf1 | 1102 | if (is_control ? s->notify_access == NOTIFY_ALL : s->notify_access != NOTIFY_NONE) |
1124fe6f | 1103 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) { |
c952c6ec LP |
1104 | r = -ENOMEM; |
1105 | goto fail; | |
1106 | } | |
1107 | ||
2105e76a | 1108 | if (s->main_pid > 0) |
ccd06097 | 1109 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) { |
c952c6ec LP |
1110 | r = -ENOMEM; |
1111 | goto fail; | |
1112 | } | |
2105e76a | 1113 | |
4ad49000 | 1114 | if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM) |
ccd06097 | 1115 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) { |
97ae63e2 LP |
1116 | r = -ENOMEM; |
1117 | goto fail; | |
1118 | } | |
1119 | ||
3b1c5241 SL |
1120 | if (UNIT_DEREF(s->accept_socket)) { |
1121 | union sockaddr_union sa; | |
1122 | socklen_t salen = sizeof(sa); | |
1123 | ||
1124 | r = getpeername(s->socket_fd, &sa.sa, &salen); | |
1125 | if (r < 0) { | |
1126 | r = -errno; | |
1127 | goto fail; | |
1128 | } | |
1129 | ||
1130 | if (IN_SET(sa.sa.sa_family, AF_INET, AF_INET6)) { | |
1131 | _cleanup_free_ char *addr = NULL; | |
1132 | char *t; | |
1133 | int port; | |
1134 | ||
1135 | r = sockaddr_pretty(&sa.sa, salen, true, false, &addr); | |
1136 | if (r < 0) | |
1137 | goto fail; | |
1138 | ||
1139 | t = strappend("REMOTE_ADDR=", addr); | |
1140 | if (!t) { | |
1141 | r = -ENOMEM; | |
1142 | goto fail; | |
1143 | } | |
1144 | our_env[n_env++] = t; | |
1145 | ||
1146 | port = sockaddr_port(&sa.sa); | |
1147 | if (port < 0) { | |
1148 | r = port; | |
1149 | goto fail; | |
1150 | } | |
1151 | ||
1152 | if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) { | |
1153 | r = -ENOMEM; | |
1154 | goto fail; | |
1155 | } | |
1156 | our_env[n_env++] = t; | |
1157 | } | |
1158 | } | |
1159 | ||
97ae63e2 LP |
1160 | final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL); |
1161 | if (!final_env) { | |
2105e76a LP |
1162 | r = -ENOMEM; |
1163 | goto fail; | |
1164 | } | |
c952c6ec | 1165 | |
4ad49000 | 1166 | if (is_control && UNIT(s)->cgroup_path) { |
63c372cb | 1167 | path = strjoina(UNIT(s)->cgroup_path, "/control"); |
4ad49000 LP |
1168 | cg_create(SYSTEMD_CGROUP_CONTROLLER, path); |
1169 | } else | |
1170 | path = UNIT(s)->cgroup_path; | |
1171 | ||
e44da745 DM |
1172 | #ifdef ENABLE_KDBUS |
1173 | if (s->exec_context.bus_endpoint) { | |
1174 | r = bus_kernel_create_endpoint(UNIT(s)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user", | |
1175 | UNIT(s)->id, &bus_endpoint_path); | |
1176 | if (r < 0) | |
1177 | goto fail; | |
1178 | ||
1179 | /* Pass the fd to the exec_params so that the child process can upload the policy. | |
1180 | * Keep a reference to the fd in the service, so the endpoint is kept alive as long | |
1181 | * as the service is running. */ | |
1182 | exec_params.bus_endpoint_fd = s->bus_endpoint_fd = r; | |
1183 | } | |
1184 | #endif | |
1185 | ||
9fa95f85 DM |
1186 | exec_params.argv = argv; |
1187 | exec_params.fds = fds; | |
1188 | exec_params.n_fds = n_fds; | |
1189 | exec_params.environment = final_env; | |
1190 | exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn; | |
1191 | exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported; | |
1192 | exec_params.cgroup_path = path; | |
a931ad47 | 1193 | exec_params.cgroup_delegate = s->cgroup_context.delegate; |
9fa95f85 DM |
1194 | exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager); |
1195 | exec_params.unit_id = UNIT(s)->id; | |
1196 | exec_params.watchdog_usec = s->watchdog_usec; | |
e44da745 | 1197 | exec_params.bus_endpoint_path = bus_endpoint_path; |
9fa95f85 DM |
1198 | if (s->type == SERVICE_IDLE) |
1199 | exec_params.idle_pipe = UNIT(s)->manager->idle_pipe; | |
1200 | ||
9e2f7c11 | 1201 | r = exec_spawn(c, |
9e2f7c11 | 1202 | &s->exec_context, |
9fa95f85 | 1203 | &exec_params, |
613b411c | 1204 | s->exec_runtime, |
9e2f7c11 | 1205 | &pid); |
9e2f7c11 | 1206 | if (r < 0) |
034c6ed7 LP |
1207 | goto fail; |
1208 | ||
117dcc57 ZJS |
1209 | r = unit_watch_pid(UNIT(s), pid); |
1210 | if (r < 0) | |
034c6ed7 LP |
1211 | /* FIXME: we need to do something here */ |
1212 | goto fail; | |
1213 | ||
1214 | *_pid = pid; | |
1215 | ||
5cb5a6ff | 1216 | return 0; |
034c6ed7 LP |
1217 | |
1218 | fail: | |
1219 | if (timeout) | |
718db961 | 1220 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
034c6ed7 LP |
1221 | |
1222 | return r; | |
1223 | } | |
1224 | ||
80876c20 LP |
1225 | static int main_pid_good(Service *s) { |
1226 | assert(s); | |
1227 | ||
1228 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
1229 | * don't know */ | |
1230 | ||
1231 | /* If we know the pid file, then lets just check if it is | |
1232 | * still valid */ | |
6dfa5494 LP |
1233 | if (s->main_pid_known) { |
1234 | ||
1235 | /* If it's an alien child let's check if it is still | |
1236 | * alive ... */ | |
62220cf7 | 1237 | if (s->main_pid_alien && s->main_pid > 0) |
9f5650ae | 1238 | return pid_is_alive(s->main_pid); |
6dfa5494 LP |
1239 | |
1240 | /* .. otherwise assume we'll get a SIGCHLD for it, | |
1241 | * which we really should wait for to collect exit | |
1242 | * status and code */ | |
80876c20 | 1243 | return s->main_pid > 0; |
6dfa5494 | 1244 | } |
80876c20 LP |
1245 | |
1246 | /* We don't know the pid */ | |
1247 | return -EAGAIN; | |
1248 | } | |
1249 | ||
44a6b1b6 | 1250 | _pure_ static int control_pid_good(Service *s) { |
80876c20 LP |
1251 | assert(s); |
1252 | ||
1253 | return s->control_pid > 0; | |
1254 | } | |
1255 | ||
1256 | static int cgroup_good(Service *s) { | |
1257 | int r; | |
1258 | ||
1259 | assert(s); | |
1260 | ||
4ad49000 LP |
1261 | if (!UNIT(s)->cgroup_path) |
1262 | return 0; | |
1263 | ||
1264 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true); | |
117dcc57 | 1265 | if (r < 0) |
80876c20 LP |
1266 | return r; |
1267 | ||
1268 | return !r; | |
1269 | } | |
1270 | ||
f42806df | 1271 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
034c6ed7 LP |
1272 | int r; |
1273 | assert(s); | |
1274 | ||
f42806df LP |
1275 | if (f != SERVICE_SUCCESS) |
1276 | s->result = f; | |
034c6ed7 | 1277 | |
0c7f15b3 MS |
1278 | service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD); |
1279 | ||
2928b0a8 | 1280 | if (s->result != SERVICE_SUCCESS) { |
79008bdd | 1281 | log_unit_warning(UNIT(s)->id, "%s failed.", UNIT(s)->id); |
2928b0a8 LP |
1282 | failure_action(UNIT(s)->manager, s->failure_action, s->reboot_arg); |
1283 | } | |
93ae25e6 | 1284 | |
034c6ed7 | 1285 | if (allow_restart && |
47342320 | 1286 | !s->forbid_restart && |
034c6ed7 | 1287 | (s->restart == SERVICE_RESTART_ALWAYS || |
f42806df LP |
1288 | (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) || |
1289 | (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) || | |
6cfe2fde | 1290 | (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) || |
dc99a976 | 1291 | (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) || |
37520c1b | 1292 | (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) || |
3e2d435b | 1293 | (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) || |
37520c1b | 1294 | (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 | 1295 | (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) && |
37520c1b | 1296 | (!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 | 1297 | |
718db961 | 1298 | r = service_arm_timer(s, s->restart_usec); |
f42806df | 1299 | if (r < 0) |
034c6ed7 LP |
1300 | goto fail; |
1301 | ||
1302 | service_set_state(s, SERVICE_AUTO_RESTART); | |
0c7f15b3 | 1303 | } |
034c6ed7 | 1304 | |
47342320 LP |
1305 | s->forbid_restart = false; |
1306 | ||
e66cf1a3 | 1307 | /* We want fresh tmpdirs in case service is started again immediately */ |
613b411c LP |
1308 | exec_runtime_destroy(s->exec_runtime); |
1309 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); | |
c17ec25e | 1310 | |
e66cf1a3 LP |
1311 | /* Also, remove the runtime directory in */ |
1312 | exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager)); | |
1313 | ||
9285c9ff LN |
1314 | /* Try to delete the pid file. At this point it will be |
1315 | * out-of-date, and some software might be confused by it, so | |
1316 | * let's remove it. */ | |
1317 | if (s->pid_file) | |
1318 | unlink_noerrno(s->pid_file); | |
1319 | ||
034c6ed7 LP |
1320 | return; |
1321 | ||
1322 | fail: | |
31938a85 | 1323 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run install restart timer: %m", UNIT(s)->id); |
f42806df | 1324 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1325 | } |
1326 | ||
f42806df | 1327 | static void service_enter_stop_post(Service *s, ServiceResult f) { |
034c6ed7 LP |
1328 | int r; |
1329 | assert(s); | |
1330 | ||
f42806df LP |
1331 | if (f != SERVICE_SUCCESS) |
1332 | s->result = f; | |
034c6ed7 | 1333 | |
5e94833f | 1334 | service_unwatch_control_pid(s); |
a911bb9a | 1335 | unit_watch_all_pids(UNIT(s)); |
5e94833f | 1336 | |
117dcc57 ZJS |
1337 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; |
1338 | if (s->control_command) { | |
867b3b7d LP |
1339 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
1340 | ||
ecedd90f LP |
1341 | r = service_spawn(s, |
1342 | s->control_command, | |
21b2ce39 | 1343 | s->timeout_stop_usec, |
ecedd90f LP |
1344 | false, |
1345 | !s->permissions_start_only, | |
1346 | !s->root_directory_start_only, | |
1347 | true, | |
ecedd90f LP |
1348 | true, |
1349 | &s->control_pid); | |
1350 | if (r < 0) | |
034c6ed7 LP |
1351 | goto fail; |
1352 | ||
80876c20 LP |
1353 | service_set_state(s, SERVICE_STOP_POST); |
1354 | } else | |
ac84d1fb | 1355 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1356 | |
1357 | return; | |
1358 | ||
1359 | fail: | |
31938a85 | 1360 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'stop-post' task: %m", UNIT(s)->id); |
f42806df | 1361 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1362 | } |
1363 | ||
4940c0b0 LP |
1364 | static int state_to_kill_operation(ServiceState state) { |
1365 | switch (state) { | |
1366 | ||
1367 | case SERVICE_STOP_SIGABRT: | |
1368 | return KILL_ABORT; | |
1369 | ||
1370 | case SERVICE_STOP_SIGTERM: | |
1371 | case SERVICE_FINAL_SIGTERM: | |
1372 | return KILL_TERMINATE; | |
1373 | ||
1374 | case SERVICE_STOP_SIGKILL: | |
1375 | case SERVICE_FINAL_SIGKILL: | |
1376 | return KILL_KILL; | |
1377 | ||
1378 | default: | |
1379 | return _KILL_OPERATION_INVALID; | |
1380 | } | |
1381 | } | |
1382 | ||
f42806df | 1383 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
034c6ed7 | 1384 | int r; |
034c6ed7 LP |
1385 | |
1386 | assert(s); | |
1387 | ||
f42806df LP |
1388 | if (f != SERVICE_SUCCESS) |
1389 | s->result = f; | |
034c6ed7 | 1390 | |
a911bb9a LP |
1391 | unit_watch_all_pids(UNIT(s)); |
1392 | ||
cd2086fe LP |
1393 | r = unit_kill_context( |
1394 | UNIT(s), | |
1395 | &s->kill_context, | |
4940c0b0 | 1396 | state_to_kill_operation(state), |
cd2086fe LP |
1397 | s->main_pid, |
1398 | s->control_pid, | |
1399 | s->main_pid_alien); | |
ac84d1fb | 1400 | |
cd2086fe LP |
1401 | if (r < 0) |
1402 | goto fail; | |
034c6ed7 | 1403 | |
cd2086fe | 1404 | if (r > 0) { |
d568a335 | 1405 | if (s->timeout_stop_usec > 0) { |
718db961 | 1406 | r = service_arm_timer(s, s->timeout_stop_usec); |
d568a335 | 1407 | if (r < 0) |
e558336f | 1408 | goto fail; |
d568a335 | 1409 | } |
d6ea93e3 | 1410 | |
80876c20 | 1411 | service_set_state(s, state); |
db2cb23b | 1412 | } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGABRT) |
ac84d1fb LP |
1413 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); |
1414 | else if (state == SERVICE_STOP_SIGKILL) | |
f42806df | 1415 | service_enter_stop_post(s, SERVICE_SUCCESS); |
ac84d1fb LP |
1416 | else if (state == SERVICE_FINAL_SIGTERM) |
1417 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); | |
80876c20 | 1418 | else |
f42806df | 1419 | service_enter_dead(s, SERVICE_SUCCESS, true); |
034c6ed7 LP |
1420 | |
1421 | return; | |
1422 | ||
1423 | fail: | |
31938a85 | 1424 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to kill processes: %m", UNIT(s)->id); |
034c6ed7 | 1425 | |
a00973af | 1426 | if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 1427 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 1428 | else |
f42806df | 1429 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
1430 | } |
1431 | ||
308d72dc LP |
1432 | static void service_enter_stop_by_notify(Service *s) { |
1433 | assert(s); | |
1434 | ||
1435 | unit_watch_all_pids(UNIT(s)); | |
1436 | ||
1437 | if (s->timeout_stop_usec > 0) | |
1438 | service_arm_timer(s, s->timeout_stop_usec); | |
1439 | ||
6041a7ee MS |
1440 | /* The service told us it's stopping, so it's as if we SIGTERM'd it. */ |
1441 | service_set_state(s, SERVICE_STOP_SIGTERM); | |
308d72dc LP |
1442 | } |
1443 | ||
f42806df | 1444 | static void service_enter_stop(Service *s, ServiceResult f) { |
034c6ed7 | 1445 | int r; |
5925dd3c | 1446 | |
034c6ed7 LP |
1447 | assert(s); |
1448 | ||
f42806df LP |
1449 | if (f != SERVICE_SUCCESS) |
1450 | s->result = f; | |
034c6ed7 | 1451 | |
5e94833f | 1452 | service_unwatch_control_pid(s); |
a911bb9a | 1453 | unit_watch_all_pids(UNIT(s)); |
5e94833f | 1454 | |
117dcc57 ZJS |
1455 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; |
1456 | if (s->control_command) { | |
867b3b7d LP |
1457 | s->control_command_id = SERVICE_EXEC_STOP; |
1458 | ||
ecedd90f LP |
1459 | r = service_spawn(s, |
1460 | s->control_command, | |
21b2ce39 | 1461 | s->timeout_stop_usec, |
ecedd90f LP |
1462 | false, |
1463 | !s->permissions_start_only, | |
1464 | !s->root_directory_start_only, | |
1465 | false, | |
ecedd90f LP |
1466 | true, |
1467 | &s->control_pid); | |
1468 | if (r < 0) | |
034c6ed7 LP |
1469 | goto fail; |
1470 | ||
80876c20 LP |
1471 | service_set_state(s, SERVICE_STOP); |
1472 | } else | |
f42806df | 1473 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1474 | |
1475 | return; | |
1476 | ||
1477 | fail: | |
31938a85 | 1478 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'stop' task: %m", UNIT(s)->id); |
f42806df | 1479 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1480 | } |
1481 | ||
f42806df | 1482 | static void service_enter_running(Service *s, ServiceResult f) { |
4eab639f | 1483 | int main_pid_ok, cgroup_ok; |
80876c20 LP |
1484 | assert(s); |
1485 | ||
f42806df LP |
1486 | if (f != SERVICE_SUCCESS) |
1487 | s->result = f; | |
80876c20 | 1488 | |
4eab639f LP |
1489 | main_pid_ok = main_pid_good(s); |
1490 | cgroup_ok = cgroup_good(s); | |
1491 | ||
1492 | if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) && | |
308d72dc LP |
1493 | (s->bus_name_good || s->type != SERVICE_DBUS)) { |
1494 | ||
1495 | /* If there are any queued up sd_notify() | |
1496 | * notifications, process them now */ | |
1497 | if (s->notify_state == NOTIFY_RELOADING) | |
1498 | service_enter_reload_by_notify(s); | |
1499 | else if (s->notify_state == NOTIFY_STOPPING) | |
1500 | service_enter_stop_by_notify(s); | |
1501 | else | |
1502 | service_set_state(s, SERVICE_RUNNING); | |
1503 | ||
1504 | } else if (s->remain_after_exit) | |
80876c20 LP |
1505 | service_set_state(s, SERVICE_EXITED); |
1506 | else | |
f42806df | 1507 | service_enter_stop(s, SERVICE_SUCCESS); |
80876c20 LP |
1508 | } |
1509 | ||
034c6ed7 LP |
1510 | static void service_enter_start_post(Service *s) { |
1511 | int r; | |
1512 | assert(s); | |
1513 | ||
5e94833f | 1514 | service_unwatch_control_pid(s); |
842129f5 | 1515 | service_reset_watchdog(s); |
bb242b7b | 1516 | |
117dcc57 ZJS |
1517 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; |
1518 | if (s->control_command) { | |
867b3b7d LP |
1519 | s->control_command_id = SERVICE_EXEC_START_POST; |
1520 | ||
ecedd90f LP |
1521 | r = service_spawn(s, |
1522 | s->control_command, | |
21b2ce39 | 1523 | s->timeout_start_usec, |
ecedd90f LP |
1524 | false, |
1525 | !s->permissions_start_only, | |
1526 | !s->root_directory_start_only, | |
1527 | false, | |
ecedd90f LP |
1528 | true, |
1529 | &s->control_pid); | |
1530 | if (r < 0) | |
034c6ed7 LP |
1531 | goto fail; |
1532 | ||
80876c20 LP |
1533 | service_set_state(s, SERVICE_START_POST); |
1534 | } else | |
f42806df | 1535 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
1536 | |
1537 | return; | |
1538 | ||
1539 | fail: | |
31938a85 | 1540 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start-post' task: %m", UNIT(s)->id); |
f42806df | 1541 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1542 | } |
1543 | ||
4ad49000 LP |
1544 | static void service_kill_control_processes(Service *s) { |
1545 | char *p; | |
1546 | ||
1547 | if (!UNIT(s)->cgroup_path) | |
1548 | return; | |
1549 | ||
63c372cb | 1550 | p = strjoina(UNIT(s)->cgroup_path, "/control"); |
4ad49000 LP |
1551 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL); |
1552 | } | |
1553 | ||
034c6ed7 | 1554 | static void service_enter_start(Service *s) { |
4ad49000 | 1555 | ExecCommand *c; |
034c6ed7 LP |
1556 | pid_t pid; |
1557 | int r; | |
1558 | ||
1559 | assert(s); | |
1560 | ||
41efeaec LP |
1561 | service_unwatch_control_pid(s); |
1562 | service_unwatch_main_pid(s); | |
80876c20 | 1563 | |
8f53a7b8 LP |
1564 | /* We want to ensure that nobody leaks processes from |
1565 | * START_PRE here, so let's go on a killing spree, People | |
1566 | * should not spawn long running processes from START_PRE. */ | |
4ad49000 | 1567 | service_kill_control_processes(s); |
8f53a7b8 | 1568 | |
867b3b7d LP |
1569 | if (s->type == SERVICE_FORKING) { |
1570 | s->control_command_id = SERVICE_EXEC_START; | |
1571 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
1572 | ||
1573 | s->main_command = NULL; | |
1574 | } else { | |
1575 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
1576 | s->control_command = NULL; | |
1577 | ||
1578 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
1579 | } | |
34e9ba66 | 1580 | |
96fb8242 LP |
1581 | if (!c) { |
1582 | assert(s->type == SERVICE_ONESHOT); | |
1583 | service_enter_start_post(s); | |
1584 | return; | |
1585 | } | |
1586 | ||
ecedd90f LP |
1587 | r = service_spawn(s, |
1588 | c, | |
21b2ce39 | 1589 | IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_ONESHOT) ? s->timeout_start_usec : 0, |
ecedd90f LP |
1590 | true, |
1591 | true, | |
1592 | true, | |
1593 | true, | |
ecedd90f LP |
1594 | false, |
1595 | &pid); | |
1596 | if (r < 0) | |
034c6ed7 LP |
1597 | goto fail; |
1598 | ||
f2b68789 | 1599 | if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) { |
034c6ed7 LP |
1600 | /* For simple services we immediately start |
1601 | * the START_POST binaries. */ | |
1602 | ||
5925dd3c | 1603 | service_set_main_pid(s, pid); |
034c6ed7 LP |
1604 | service_enter_start_post(s); |
1605 | ||
1606 | } else if (s->type == SERVICE_FORKING) { | |
1607 | ||
1608 | /* For forking services we wait until the start | |
1609 | * process exited. */ | |
1610 | ||
e55224ca | 1611 | s->control_pid = pid; |
80876c20 LP |
1612 | service_set_state(s, SERVICE_START); |
1613 | ||
34e9ba66 | 1614 | } else if (s->type == SERVICE_ONESHOT || |
8c47c732 LP |
1615 | s->type == SERVICE_DBUS || |
1616 | s->type == SERVICE_NOTIFY) { | |
7d55e835 | 1617 | |
34e9ba66 | 1618 | /* For oneshot services we wait until the start |
7d55e835 LP |
1619 | * process exited, too, but it is our main process. */ |
1620 | ||
05e343b7 | 1621 | /* For D-Bus services we know the main pid right away, |
8c47c732 LP |
1622 | * but wait for the bus name to appear on the |
1623 | * bus. Notify services are similar. */ | |
05e343b7 | 1624 | |
5925dd3c | 1625 | service_set_main_pid(s, pid); |
80876c20 | 1626 | service_set_state(s, SERVICE_START); |
034c6ed7 LP |
1627 | } else |
1628 | assert_not_reached("Unknown service type"); | |
1629 | ||
1630 | return; | |
1631 | ||
1632 | fail: | |
31938a85 | 1633 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start' task: %m", UNIT(s)->id); |
f42806df | 1634 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1635 | } |
1636 | ||
1637 | static void service_enter_start_pre(Service *s) { | |
1638 | int r; | |
1639 | ||
1640 | assert(s); | |
1641 | ||
5e94833f LP |
1642 | service_unwatch_control_pid(s); |
1643 | ||
117dcc57 ZJS |
1644 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; |
1645 | if (s->control_command) { | |
8f53a7b8 LP |
1646 | /* Before we start anything, let's clear up what might |
1647 | * be left from previous runs. */ | |
4ad49000 | 1648 | service_kill_control_processes(s); |
8f53a7b8 | 1649 | |
867b3b7d LP |
1650 | s->control_command_id = SERVICE_EXEC_START_PRE; |
1651 | ||
ecedd90f LP |
1652 | r = service_spawn(s, |
1653 | s->control_command, | |
21b2ce39 | 1654 | s->timeout_start_usec, |
ecedd90f LP |
1655 | false, |
1656 | !s->permissions_start_only, | |
1657 | !s->root_directory_start_only, | |
1658 | true, | |
ecedd90f LP |
1659 | true, |
1660 | &s->control_pid); | |
1661 | if (r < 0) | |
034c6ed7 LP |
1662 | goto fail; |
1663 | ||
80876c20 LP |
1664 | service_set_state(s, SERVICE_START_PRE); |
1665 | } else | |
034c6ed7 LP |
1666 | service_enter_start(s); |
1667 | ||
1668 | return; | |
1669 | ||
1670 | fail: | |
31938a85 | 1671 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start-pre' task: %m", UNIT(s)->id); |
f42806df | 1672 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
1673 | } |
1674 | ||
1675 | static void service_enter_restart(Service *s) { | |
718db961 | 1676 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 | 1677 | int r; |
398ef8ba | 1678 | |
034c6ed7 LP |
1679 | assert(s); |
1680 | ||
a8bb2e65 LP |
1681 | if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) { |
1682 | /* Don't restart things if we are going down anyway */ | |
79008bdd | 1683 | log_unit_info(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart."); |
2edfa366 | 1684 | |
718db961 | 1685 | r = service_arm_timer(s, s->restart_usec); |
a8bb2e65 | 1686 | if (r < 0) |
2edfa366 | 1687 | goto fail; |
feae8adb DW |
1688 | |
1689 | return; | |
2edfa366 LP |
1690 | } |
1691 | ||
48bb5876 DW |
1692 | /* Any units that are bound to this service must also be |
1693 | * restarted. We use JOB_RESTART (instead of the more obvious | |
1694 | * JOB_START) here so that those dependency jobs will be added | |
1695 | * as well. */ | |
1696 | r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL); | |
1697 | if (r < 0) | |
034c6ed7 LP |
1698 | goto fail; |
1699 | ||
a8bb2e65 LP |
1700 | /* Note that we stay in the SERVICE_AUTO_RESTART state here, |
1701 | * it will be canceled as part of the service_stop() call that | |
1702 | * is executed as part of JOB_RESTART. */ | |
1703 | ||
79008bdd | 1704 | log_unit_debug(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id); |
034c6ed7 LP |
1705 | return; |
1706 | ||
1707 | fail: | |
79008bdd | 1708 | log_unit_warning(UNIT(s)->id, "%s failed to schedule restart job: %s", UNIT(s)->id, bus_error_message(&error, -r)); |
f42806df | 1709 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1710 | } |
1711 | ||
308d72dc LP |
1712 | static void service_enter_reload_by_notify(Service *s) { |
1713 | assert(s); | |
1714 | ||
1715 | if (s->timeout_start_usec > 0) | |
1716 | service_arm_timer(s, s->timeout_start_usec); | |
1717 | ||
1718 | service_set_state(s, SERVICE_RELOAD); | |
1719 | } | |
1720 | ||
034c6ed7 LP |
1721 | static void service_enter_reload(Service *s) { |
1722 | int r; | |
1723 | ||
1724 | assert(s); | |
1725 | ||
5e94833f LP |
1726 | service_unwatch_control_pid(s); |
1727 | ||
117dcc57 ZJS |
1728 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; |
1729 | if (s->control_command) { | |
867b3b7d LP |
1730 | s->control_command_id = SERVICE_EXEC_RELOAD; |
1731 | ||
ecedd90f LP |
1732 | r = service_spawn(s, |
1733 | s->control_command, | |
21b2ce39 | 1734 | s->timeout_start_usec, |
ecedd90f LP |
1735 | false, |
1736 | !s->permissions_start_only, | |
1737 | !s->root_directory_start_only, | |
1738 | false, | |
ecedd90f LP |
1739 | true, |
1740 | &s->control_pid); | |
1741 | if (r < 0) | |
034c6ed7 LP |
1742 | goto fail; |
1743 | ||
80876c20 LP |
1744 | service_set_state(s, SERVICE_RELOAD); |
1745 | } else | |
f42806df | 1746 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
1747 | |
1748 | return; | |
1749 | ||
1750 | fail: | |
31938a85 | 1751 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'reload' task: %m", UNIT(s)->id); |
f42806df LP |
1752 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
1753 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
1754 | } |
1755 | ||
f42806df | 1756 | static void service_run_next_control(Service *s) { |
034c6ed7 LP |
1757 | int r; |
1758 | ||
1759 | assert(s); | |
1760 | assert(s->control_command); | |
1761 | assert(s->control_command->command_next); | |
1762 | ||
34e9ba66 | 1763 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 1764 | |
34e9ba66 | 1765 | s->control_command = s->control_command->command_next; |
5e94833f LP |
1766 | service_unwatch_control_pid(s); |
1767 | ||
ecedd90f LP |
1768 | r = service_spawn(s, |
1769 | s->control_command, | |
21b2ce39 | 1770 | 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 |
1771 | false, |
1772 | !s->permissions_start_only, | |
1773 | !s->root_directory_start_only, | |
1774 | s->control_command_id == SERVICE_EXEC_START_PRE || | |
1775 | s->control_command_id == SERVICE_EXEC_STOP_POST, | |
ecedd90f LP |
1776 | true, |
1777 | &s->control_pid); | |
1778 | if (r < 0) | |
034c6ed7 LP |
1779 | goto fail; |
1780 | ||
1781 | return; | |
1782 | ||
1783 | fail: | |
31938a85 | 1784 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run next control task: %m", UNIT(s)->id); |
034c6ed7 | 1785 | |
80876c20 | 1786 | if (s->state == SERVICE_START_PRE) |
f42806df | 1787 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
80876c20 | 1788 | else if (s->state == SERVICE_STOP) |
f42806df | 1789 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 1790 | else if (s->state == SERVICE_STOP_POST) |
f42806df | 1791 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
e2f3b44c | 1792 | else if (s->state == SERVICE_RELOAD) { |
f42806df LP |
1793 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
1794 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c | 1795 | } else |
f42806df | 1796 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
5cb5a6ff LP |
1797 | } |
1798 | ||
f42806df | 1799 | static void service_run_next_main(Service *s) { |
34e9ba66 LP |
1800 | pid_t pid; |
1801 | int r; | |
1802 | ||
1803 | assert(s); | |
867b3b7d LP |
1804 | assert(s->main_command); |
1805 | assert(s->main_command->command_next); | |
1806 | assert(s->type == SERVICE_ONESHOT); | |
34e9ba66 | 1807 | |
867b3b7d | 1808 | s->main_command = s->main_command->command_next; |
34e9ba66 LP |
1809 | service_unwatch_main_pid(s); |
1810 | ||
ecedd90f LP |
1811 | r = service_spawn(s, |
1812 | s->main_command, | |
21b2ce39 | 1813 | s->timeout_start_usec, |
ecedd90f LP |
1814 | true, |
1815 | true, | |
1816 | true, | |
1817 | true, | |
ecedd90f LP |
1818 | false, |
1819 | &pid); | |
1820 | if (r < 0) | |
34e9ba66 LP |
1821 | goto fail; |
1822 | ||
1823 | service_set_main_pid(s, pid); | |
1824 | ||
1825 | return; | |
1826 | ||
1827 | fail: | |
31938a85 | 1828 | log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run next main task: %m", UNIT(s)->id); |
f42806df | 1829 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
34e9ba66 LP |
1830 | } |
1831 | ||
93ae25e6 MO |
1832 | static int service_start_limit_test(Service *s) { |
1833 | assert(s); | |
1834 | ||
1835 | if (ratelimit_test(&s->start_limit)) | |
1836 | return 0; | |
1837 | ||
79008bdd | 1838 | log_unit_warning(UNIT(s)->id, "start request repeated too quickly for %s", UNIT(s)->id); |
2928b0a8 LP |
1839 | |
1840 | return failure_action(UNIT(s)->manager, s->start_limit_action, s->reboot_arg); | |
93ae25e6 MO |
1841 | } |
1842 | ||
87f0e418 LP |
1843 | static int service_start(Unit *u) { |
1844 | Service *s = SERVICE(u); | |
4b939747 | 1845 | int r; |
5cb5a6ff LP |
1846 | |
1847 | assert(s); | |
1848 | ||
034c6ed7 LP |
1849 | /* We cannot fulfill this request right now, try again later |
1850 | * please! */ | |
a00973af LP |
1851 | if (IN_SET(s->state, |
1852 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1853 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) | |
5cb5a6ff LP |
1854 | return -EAGAIN; |
1855 | ||
034c6ed7 | 1856 | /* Already on it! */ |
a00973af | 1857 | if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST)) |
034c6ed7 LP |
1858 | return 0; |
1859 | ||
2e9d6c12 | 1860 | /* A service that will be restarted must be stopped first to |
7f2cddae | 1861 | * trigger BindsTo and/or OnFailure dependencies. If a user |
2e9d6c12 | 1862 | * does not want to wait for the holdoff time to elapse, the |
d4943dc7 LP |
1863 | * service should be manually restarted, not started. We |
1864 | * simply return EAGAIN here, so that any start jobs stay | |
1865 | * queued, and assume that the auto restart timer will | |
1866 | * eventually trigger the restart. */ | |
1867 | if (s->state == SERVICE_AUTO_RESTART) | |
a8bb2e65 | 1868 | return -EAGAIN; |
2e9d6c12 | 1869 | |
a00973af | 1870 | assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED)); |
5cb5a6ff | 1871 | |
1e2e8133 | 1872 | /* Make sure we don't enter a busy loop of some kind. */ |
4b939747 | 1873 | r = service_start_limit_test(s); |
c2f34808 | 1874 | if (r < 0) { |
8d1b002a | 1875 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false); |
4b939747 | 1876 | return r; |
c2f34808 | 1877 | } |
1e2e8133 | 1878 | |
f42806df LP |
1879 | s->result = SERVICE_SUCCESS; |
1880 | s->reload_result = SERVICE_SUCCESS; | |
034c6ed7 | 1881 | s->main_pid_known = false; |
6dfa5494 | 1882 | s->main_pid_alien = false; |
47342320 | 1883 | s->forbid_restart = false; |
5ad096b3 | 1884 | s->reset_cpu_usage = true; |
034c6ed7 | 1885 | |
8cfdb077 LP |
1886 | free(s->status_text); |
1887 | s->status_text = NULL; | |
1888 | s->status_errno = 0; | |
1889 | ||
308d72dc LP |
1890 | s->notify_state = NOTIFY_UNKNOWN; |
1891 | ||
034c6ed7 | 1892 | service_enter_start_pre(s); |
82a2b6bb | 1893 | return 1; |
5cb5a6ff LP |
1894 | } |
1895 | ||
87f0e418 LP |
1896 | static int service_stop(Unit *u) { |
1897 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
1898 | |
1899 | assert(s); | |
1900 | ||
f0c7b229 | 1901 | /* Don't create restart jobs from here. */ |
47342320 | 1902 | s->forbid_restart = true; |
034c6ed7 | 1903 | |
e537352b | 1904 | /* Already on it */ |
a00973af LP |
1905 | if (IN_SET(s->state, |
1906 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1907 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) | |
e537352b LP |
1908 | return 0; |
1909 | ||
f0c7b229 | 1910 | /* A restart will be scheduled or is in progress. */ |
034c6ed7 | 1911 | if (s->state == SERVICE_AUTO_RESTART) { |
0c7f15b3 | 1912 | service_set_state(s, SERVICE_DEAD); |
034c6ed7 LP |
1913 | return 0; |
1914 | } | |
1915 | ||
3f6c78dc LP |
1916 | /* If there's already something running we go directly into |
1917 | * kill mode. */ | |
a00973af | 1918 | if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD)) { |
f42806df | 1919 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
3f6c78dc LP |
1920 | return 0; |
1921 | } | |
5cb5a6ff | 1922 | |
a00973af | 1923 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
3a762661 | 1924 | |
f42806df | 1925 | service_enter_stop(s, SERVICE_SUCCESS); |
82a2b6bb | 1926 | return 1; |
5cb5a6ff LP |
1927 | } |
1928 | ||
87f0e418 LP |
1929 | static int service_reload(Unit *u) { |
1930 | Service *s = SERVICE(u); | |
034c6ed7 LP |
1931 | |
1932 | assert(s); | |
1933 | ||
80876c20 | 1934 | assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED); |
034c6ed7 LP |
1935 | |
1936 | service_enter_reload(s); | |
5cb5a6ff LP |
1937 | return 0; |
1938 | } | |
1939 | ||
44a6b1b6 | 1940 | _pure_ static bool service_can_reload(Unit *u) { |
87f0e418 | 1941 | Service *s = SERVICE(u); |
034c6ed7 LP |
1942 | |
1943 | assert(s); | |
1944 | ||
1945 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
1946 | } | |
1947 | ||
a16e1123 LP |
1948 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
1949 | Service *s = SERVICE(u); | |
2339fc93 | 1950 | ServiceFDStore *fs; |
a16e1123 LP |
1951 | |
1952 | assert(u); | |
1953 | assert(f); | |
1954 | assert(fds); | |
1955 | ||
1956 | unit_serialize_item(u, f, "state", service_state_to_string(s->state)); | |
f42806df LP |
1957 | unit_serialize_item(u, f, "result", service_result_to_string(s->result)); |
1958 | unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result)); | |
a16e1123 LP |
1959 | |
1960 | if (s->control_pid > 0) | |
ccd06097 ZJS |
1961 | unit_serialize_item_format(u, f, "control-pid", PID_FMT, |
1962 | s->control_pid); | |
a16e1123 | 1963 | |
5925dd3c | 1964 | if (s->main_pid_known && s->main_pid > 0) |
ccd06097 | 1965 | unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid); |
a16e1123 LP |
1966 | |
1967 | unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known)); | |
1968 | ||
3a2776bc LP |
1969 | if (s->status_text) |
1970 | unit_serialize_item(u, f, "status-text", s->status_text); | |
1971 | ||
cfc4eb4c LP |
1972 | /* FIXME: There's a minor uncleanliness here: if there are |
1973 | * multiple commands attached here, we will start from the | |
1974 | * first one again */ | |
a16e1123 | 1975 | if (s->control_command_id >= 0) |
117dcc57 ZJS |
1976 | unit_serialize_item(u, f, "control-command", |
1977 | service_exec_command_to_string(s->control_command_id)); | |
a16e1123 LP |
1978 | |
1979 | if (s->socket_fd >= 0) { | |
1980 | int copy; | |
1981 | ||
2339fc93 LP |
1982 | copy = fdset_put_dup(fds, s->socket_fd); |
1983 | if (copy < 0) | |
a16e1123 LP |
1984 | return copy; |
1985 | ||
1986 | unit_serialize_item_format(u, f, "socket-fd", "%i", copy); | |
1987 | } | |
1988 | ||
e44da745 DM |
1989 | if (s->bus_endpoint_fd >= 0) { |
1990 | int copy; | |
1991 | ||
2339fc93 LP |
1992 | copy = fdset_put_dup(fds, s->bus_endpoint_fd); |
1993 | if (copy < 0) | |
e44da745 DM |
1994 | return copy; |
1995 | ||
1996 | unit_serialize_item_format(u, f, "endpoint-fd", "%i", copy); | |
1997 | } | |
1998 | ||
2339fc93 LP |
1999 | LIST_FOREACH(fd_store, fs, s->fd_store) { |
2000 | int copy; | |
2001 | ||
2002 | copy = fdset_put_dup(fds, fs->fd); | |
2003 | if (copy < 0) | |
2004 | return copy; | |
2005 | ||
2006 | unit_serialize_item_format(u, f, "fd-store-fd", "%i", copy); | |
2007 | } | |
2008 | ||
ecdbca40 | 2009 | if (s->main_exec_status.pid > 0) { |
ccd06097 ZJS |
2010 | unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT, |
2011 | s->main_exec_status.pid); | |
117dcc57 ZJS |
2012 | dual_timestamp_serialize(f, "main-exec-status-start", |
2013 | &s->main_exec_status.start_timestamp); | |
2014 | dual_timestamp_serialize(f, "main-exec-status-exit", | |
2015 | &s->main_exec_status.exit_timestamp); | |
ecdbca40 | 2016 | |
799fd0fd | 2017 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { |
117dcc57 ZJS |
2018 | unit_serialize_item_format(u, f, "main-exec-status-code", "%i", |
2019 | s->main_exec_status.code); | |
2020 | unit_serialize_item_format(u, f, "main-exec-status-status", "%i", | |
2021 | s->main_exec_status.status); | |
ecdbca40 LP |
2022 | } |
2023 | } | |
a6927d7f | 2024 | if (dual_timestamp_is_set(&s->watchdog_timestamp)) |
842129f5 | 2025 | dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp); |
ecdbca40 | 2026 | |
6aca9a58 | 2027 | if (s->forbid_restart) |
8d1a2802 | 2028 | unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart)); |
6aca9a58 | 2029 | |
a16e1123 LP |
2030 | return 0; |
2031 | } | |
2032 | ||
2033 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
2034 | Service *s = SERVICE(u); | |
2339fc93 | 2035 | int r; |
a16e1123 LP |
2036 | |
2037 | assert(u); | |
2038 | assert(key); | |
2039 | assert(value); | |
2040 | assert(fds); | |
2041 | ||
2042 | if (streq(key, "state")) { | |
2043 | ServiceState state; | |
2044 | ||
117dcc57 ZJS |
2045 | state = service_state_from_string(value); |
2046 | if (state < 0) | |
79008bdd | 2047 | log_unit_debug(u->id, "Failed to parse state value %s", value); |
a16e1123 LP |
2048 | else |
2049 | s->deserialized_state = state; | |
f42806df LP |
2050 | } else if (streq(key, "result")) { |
2051 | ServiceResult f; | |
2052 | ||
2053 | f = service_result_from_string(value); | |
2054 | if (f < 0) | |
79008bdd | 2055 | log_unit_debug(u->id, "Failed to parse result value %s", value); |
f42806df LP |
2056 | else if (f != SERVICE_SUCCESS) |
2057 | s->result = f; | |
2058 | ||
2059 | } else if (streq(key, "reload-result")) { | |
2060 | ServiceResult f; | |
2061 | ||
2062 | f = service_result_from_string(value); | |
2063 | if (f < 0) | |
79008bdd | 2064 | log_unit_debug(u->id, "Failed to parse reload result value %s", value); |
f42806df LP |
2065 | else if (f != SERVICE_SUCCESS) |
2066 | s->reload_result = f; | |
a16e1123 | 2067 | |
a16e1123 | 2068 | } else if (streq(key, "control-pid")) { |
5925dd3c | 2069 | pid_t pid; |
a16e1123 | 2070 | |
e364ad06 | 2071 | if (parse_pid(value, &pid) < 0) |
79008bdd | 2072 | log_unit_debug(u->id, "Failed to parse control-pid value %s", value); |
a16e1123 | 2073 | else |
e55224ca | 2074 | s->control_pid = pid; |
a16e1123 | 2075 | } else if (streq(key, "main-pid")) { |
5925dd3c | 2076 | pid_t pid; |
a16e1123 | 2077 | |
e364ad06 | 2078 | if (parse_pid(value, &pid) < 0) |
79008bdd | 2079 | log_unit_debug(u->id, "Failed to parse main-pid value %s", value); |
7400b9d2 LP |
2080 | else { |
2081 | service_set_main_pid(s, pid); | |
2082 | unit_watch_pid(UNIT(s), pid); | |
2083 | } | |
a16e1123 LP |
2084 | } else if (streq(key, "main-pid-known")) { |
2085 | int b; | |
2086 | ||
117dcc57 ZJS |
2087 | b = parse_boolean(value); |
2088 | if (b < 0) | |
79008bdd | 2089 | log_unit_debug(u->id, "Failed to parse main-pid-known value %s", value); |
a16e1123 LP |
2090 | else |
2091 | s->main_pid_known = b; | |
3a2776bc LP |
2092 | } else if (streq(key, "status-text")) { |
2093 | char *t; | |
2094 | ||
117dcc57 ZJS |
2095 | t = strdup(value); |
2096 | if (!t) | |
2097 | log_oom(); | |
2098 | else { | |
3a2776bc LP |
2099 | free(s->status_text); |
2100 | s->status_text = t; | |
2101 | } | |
2102 | ||
a16e1123 LP |
2103 | } else if (streq(key, "control-command")) { |
2104 | ServiceExecCommand id; | |
2105 | ||
117dcc57 ZJS |
2106 | id = service_exec_command_from_string(value); |
2107 | if (id < 0) | |
79008bdd | 2108 | log_unit_debug(u->id, "Failed to parse exec-command value %s", value); |
a16e1123 LP |
2109 | else { |
2110 | s->control_command_id = id; | |
2111 | s->control_command = s->exec_command[id]; | |
2112 | } | |
2113 | } else if (streq(key, "socket-fd")) { | |
2114 | int fd; | |
2115 | ||
2116 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
79008bdd | 2117 | log_unit_debug(u->id, "Failed to parse socket-fd value %s", value); |
a16e1123 | 2118 | else { |
574634bc | 2119 | asynchronous_close(s->socket_fd); |
a16e1123 LP |
2120 | s->socket_fd = fdset_remove(fds, fd); |
2121 | } | |
e44da745 DM |
2122 | } else if (streq(key, "endpoint-fd")) { |
2123 | int fd; | |
2124 | ||
2125 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
79008bdd | 2126 | log_unit_debug(u->id, "Failed to parse endpoint-fd value %s", value); |
e44da745 DM |
2127 | else { |
2128 | safe_close(s->bus_endpoint_fd); | |
2129 | s->bus_endpoint_fd = fdset_remove(fds, fd); | |
2130 | } | |
2339fc93 LP |
2131 | } else if (streq(key, "fd-store-fd")) { |
2132 | int fd; | |
2133 | ||
2134 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2135 | log_unit_debug(u->id, "Failed to parse fd-store-fd value %s", value); | |
2136 | else { | |
2137 | r = service_add_fd_store(s, fd); | |
2138 | if (r < 0) | |
2139 | log_unit_error_errno(u->id, r, "Failed to add fd to store: %m"); | |
2140 | else if (r > 0) | |
2141 | fdset_remove(fds, fd); | |
2142 | } | |
2143 | ||
ecdbca40 LP |
2144 | } else if (streq(key, "main-exec-status-pid")) { |
2145 | pid_t pid; | |
2146 | ||
e364ad06 | 2147 | if (parse_pid(value, &pid) < 0) |
79008bdd | 2148 | log_unit_debug(u->id, "Failed to parse main-exec-status-pid value %s", value); |
ecdbca40 LP |
2149 | else |
2150 | s->main_exec_status.pid = pid; | |
2151 | } else if (streq(key, "main-exec-status-code")) { | |
2152 | int i; | |
2153 | ||
e364ad06 | 2154 | if (safe_atoi(value, &i) < 0) |
79008bdd | 2155 | log_unit_debug(u->id, "Failed to parse main-exec-status-code value %s", value); |
ecdbca40 LP |
2156 | else |
2157 | s->main_exec_status.code = i; | |
2158 | } else if (streq(key, "main-exec-status-status")) { | |
2159 | int i; | |
2160 | ||
e364ad06 | 2161 | if (safe_atoi(value, &i) < 0) |
79008bdd | 2162 | log_unit_debug(u->id, "Failed to parse main-exec-status-status value %s", value); |
ecdbca40 LP |
2163 | else |
2164 | s->main_exec_status.status = i; | |
799fd0fd LP |
2165 | } else if (streq(key, "main-exec-status-start")) |
2166 | dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp); | |
2167 | else if (streq(key, "main-exec-status-exit")) | |
2168 | dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp); | |
a6927d7f MO |
2169 | else if (streq(key, "watchdog-timestamp")) |
2170 | dual_timestamp_deserialize(value, &s->watchdog_timestamp); | |
613b411c | 2171 | else if (streq(key, "forbid-restart")) { |
6aca9a58 SE |
2172 | int b; |
2173 | ||
2174 | b = parse_boolean(value); | |
2175 | if (b < 0) | |
79008bdd | 2176 | log_unit_debug(u->id, "Failed to parse forbid-restart value %s", value); |
6aca9a58 SE |
2177 | else |
2178 | s->forbid_restart = b; | |
c17ec25e | 2179 | } else |
79008bdd | 2180 | log_unit_debug(u->id, "Unknown serialization key '%s'", key); |
a16e1123 LP |
2181 | |
2182 | return 0; | |
2183 | } | |
2184 | ||
44a6b1b6 | 2185 | _pure_ static UnitActiveState service_active_state(Unit *u) { |
e056b01d LP |
2186 | const UnitActiveState *table; |
2187 | ||
87f0e418 | 2188 | assert(u); |
5cb5a6ff | 2189 | |
e056b01d LP |
2190 | table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
2191 | ||
2192 | return table[SERVICE(u)->state]; | |
034c6ed7 LP |
2193 | } |
2194 | ||
10a94420 LP |
2195 | static const char *service_sub_state_to_string(Unit *u) { |
2196 | assert(u); | |
2197 | ||
2198 | return service_state_to_string(SERVICE(u)->state); | |
2199 | } | |
2200 | ||
701cc384 LP |
2201 | static bool service_check_gc(Unit *u) { |
2202 | Service *s = SERVICE(u); | |
2203 | ||
2204 | assert(s); | |
2205 | ||
6d55002a LP |
2206 | /* Never clean up services that still have a process around, |
2207 | * even if the service is formally dead. */ | |
2208 | if (cgroup_good(s) > 0 || | |
2209 | main_pid_good(s) > 0 || | |
2210 | control_pid_good(s) > 0) | |
2211 | return true; | |
2212 | ||
6d55002a LP |
2213 | return false; |
2214 | } | |
2215 | ||
44a6b1b6 | 2216 | _pure_ static bool service_check_snapshot(Unit *u) { |
701cc384 LP |
2217 | Service *s = SERVICE(u); |
2218 | ||
2219 | assert(s); | |
2220 | ||
8bb2d17d | 2221 | return s->socket_fd < 0; |
701cc384 LP |
2222 | } |
2223 | ||
3a111838 MS |
2224 | static int service_retry_pid_file(Service *s) { |
2225 | int r; | |
2226 | ||
2227 | assert(s->pid_file); | |
2228 | assert(s->state == SERVICE_START || s->state == SERVICE_START_POST); | |
2229 | ||
2230 | r = service_load_pid_file(s, false); | |
2231 | if (r < 0) | |
2232 | return r; | |
2233 | ||
2234 | service_unwatch_pid_file(s); | |
2235 | ||
f42806df | 2236 | service_enter_running(s, SERVICE_SUCCESS); |
3a111838 MS |
2237 | return 0; |
2238 | } | |
2239 | ||
2240 | static int service_watch_pid_file(Service *s) { | |
2241 | int r; | |
2242 | ||
79008bdd | 2243 | log_unit_debug(UNIT(s)->id, "Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path); |
8bb2d17d | 2244 | |
718db961 | 2245 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io); |
3a111838 MS |
2246 | if (r < 0) |
2247 | goto fail; | |
2248 | ||
2249 | /* the pidfile might have appeared just before we set the watch */ | |
79008bdd | 2250 | 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 |
2251 | service_retry_pid_file(s); |
2252 | ||
2253 | return 0; | |
2254 | fail: | |
31938a85 | 2255 | 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 |
2256 | service_unwatch_pid_file(s); |
2257 | return r; | |
2258 | } | |
2259 | ||
2260 | static int service_demand_pid_file(Service *s) { | |
2261 | PathSpec *ps; | |
2262 | ||
2263 | assert(s->pid_file); | |
2264 | assert(!s->pid_file_pathspec); | |
2265 | ||
2266 | ps = new0(PathSpec, 1); | |
2267 | if (!ps) | |
2268 | return -ENOMEM; | |
2269 | ||
718db961 | 2270 | ps->unit = UNIT(s); |
3a111838 MS |
2271 | ps->path = strdup(s->pid_file); |
2272 | if (!ps->path) { | |
2273 | free(ps); | |
2274 | return -ENOMEM; | |
2275 | } | |
2276 | ||
2277 | path_kill_slashes(ps->path); | |
2278 | ||
2279 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that | |
2280 | * keep their PID file open all the time. */ | |
2281 | ps->type = PATH_MODIFIED; | |
2282 | ps->inotify_fd = -1; | |
2283 | ||
2284 | s->pid_file_pathspec = ps; | |
2285 | ||
2286 | return service_watch_pid_file(s); | |
2287 | } | |
2288 | ||
718db961 | 2289 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
e14c2802 LP |
2290 | PathSpec *p = userdata; |
2291 | Service *s; | |
2292 | ||
2293 | assert(p); | |
2294 | ||
2295 | s = SERVICE(p->unit); | |
3a111838 MS |
2296 | |
2297 | assert(s); | |
2298 | assert(fd >= 0); | |
2299 | assert(s->state == SERVICE_START || s->state == SERVICE_START_POST); | |
2300 | assert(s->pid_file_pathspec); | |
57020a3a | 2301 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); |
3a111838 | 2302 | |
79008bdd | 2303 | log_unit_debug(UNIT(s)->id, "inotify event for %s", UNIT(s)->id); |
3a111838 | 2304 | |
e14c2802 | 2305 | if (path_spec_fd_event(p, events) < 0) |
3a111838 MS |
2306 | goto fail; |
2307 | ||
2308 | if (service_retry_pid_file(s) == 0) | |
718db961 | 2309 | return 0; |
3a111838 MS |
2310 | |
2311 | if (service_watch_pid_file(s) < 0) | |
2312 | goto fail; | |
2313 | ||
718db961 LP |
2314 | return 0; |
2315 | ||
3a111838 MS |
2316 | fail: |
2317 | service_unwatch_pid_file(s); | |
f42806df | 2318 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
718db961 | 2319 | return 0; |
3a111838 MS |
2320 | } |
2321 | ||
a911bb9a LP |
2322 | static void service_notify_cgroup_empty_event(Unit *u) { |
2323 | Service *s = SERVICE(u); | |
2324 | ||
2325 | assert(u); | |
2326 | ||
79008bdd | 2327 | log_unit_debug(u->id, "%s: cgroup is empty", u->id); |
a911bb9a LP |
2328 | |
2329 | switch (s->state) { | |
2330 | ||
2331 | /* Waiting for SIGCHLD is usually more interesting, | |
2332 | * because it includes return codes/signals. Which is | |
2333 | * why we ignore the cgroup events for most cases, | |
2334 | * except when we don't know pid which to expect the | |
2335 | * SIGCHLD for. */ | |
2336 | ||
2337 | case SERVICE_START: | |
2338 | case SERVICE_START_POST: | |
2339 | /* If we were hoping for the daemon to write its PID file, | |
2340 | * we can give up now. */ | |
2341 | if (s->pid_file_pathspec) { | |
79008bdd | 2342 | log_unit_warning(u->id, "%s never wrote its PID file. Failing.", UNIT(s)->id); |
8bb2d17d | 2343 | |
a911bb9a LP |
2344 | service_unwatch_pid_file(s); |
2345 | if (s->state == SERVICE_START) | |
2346 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2347 | else | |
2348 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2349 | } | |
2350 | break; | |
2351 | ||
2352 | case SERVICE_RUNNING: | |
2353 | /* service_enter_running() will figure out what to do */ | |
2354 | service_enter_running(s, SERVICE_SUCCESS); | |
2355 | break; | |
2356 | ||
db2cb23b | 2357 | case SERVICE_STOP_SIGABRT: |
a911bb9a LP |
2358 | case SERVICE_STOP_SIGTERM: |
2359 | case SERVICE_STOP_SIGKILL: | |
2360 | ||
2361 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2362 | service_enter_stop_post(s, SERVICE_SUCCESS); | |
2363 | ||
2364 | break; | |
2365 | ||
2366 | case SERVICE_STOP_POST: | |
2367 | case SERVICE_FINAL_SIGTERM: | |
2368 | case SERVICE_FINAL_SIGKILL: | |
2369 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2370 | service_enter_dead(s, SERVICE_SUCCESS, true); | |
2371 | ||
2372 | break; | |
2373 | ||
2374 | default: | |
2375 | ; | |
2376 | } | |
2377 | } | |
2378 | ||
87f0e418 LP |
2379 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
2380 | Service *s = SERVICE(u); | |
f42806df | 2381 | ServiceResult f; |
5cb5a6ff LP |
2382 | |
2383 | assert(s); | |
034c6ed7 LP |
2384 | assert(pid >= 0); |
2385 | ||
96342de6 LN |
2386 | if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) : |
2387 | is_clean_exit_lsb(code, status, &s->success_status)) | |
f42806df LP |
2388 | f = SERVICE_SUCCESS; |
2389 | else if (code == CLD_EXITED) | |
2390 | f = SERVICE_FAILURE_EXIT_CODE; | |
2391 | else if (code == CLD_KILLED) | |
2392 | f = SERVICE_FAILURE_SIGNAL; | |
2393 | else if (code == CLD_DUMPED) | |
2394 | f = SERVICE_FAILURE_CORE_DUMP; | |
d06dacd0 | 2395 | else |
cfc4eb4c | 2396 | assert_not_reached("Unknown code"); |
034c6ed7 LP |
2397 | |
2398 | if (s->main_pid == pid) { | |
db01f8b3 MS |
2399 | /* Forking services may occasionally move to a new PID. |
2400 | * As long as they update the PID file before exiting the old | |
2401 | * PID, they're fine. */ | |
5375410b | 2402 | if (service_load_pid_file(s, false) == 0) |
db01f8b3 | 2403 | return; |
034c6ed7 | 2404 | |
034c6ed7 | 2405 | s->main_pid = 0; |
6ea832a2 | 2406 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); |
034c6ed7 | 2407 | |
867b3b7d | 2408 | if (s->main_command) { |
fbeefb45 LP |
2409 | /* If this is not a forking service than the |
2410 | * main process got started and hence we copy | |
2411 | * the exit status so that it is recorded both | |
2412 | * as main and as control process exit | |
2413 | * status */ | |
2414 | ||
867b3b7d | 2415 | s->main_command->exec_status = s->main_exec_status; |
b708e7ce | 2416 | |
867b3b7d | 2417 | if (s->main_command->ignore) |
f42806df | 2418 | f = SERVICE_SUCCESS; |
fbeefb45 LP |
2419 | } else if (s->exec_command[SERVICE_EXEC_START]) { |
2420 | ||
2421 | /* If this is a forked process, then we should | |
2422 | * ignore the return value if this was | |
2423 | * configured for the starter process */ | |
2424 | ||
2425 | if (s->exec_command[SERVICE_EXEC_START]->ignore) | |
2426 | f = SERVICE_SUCCESS; | |
034c6ed7 LP |
2427 | } |
2428 | ||
e2cc6eca LP |
2429 | log_unit_struct(u->id, |
2430 | f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, | |
2431 | LOG_MESSAGE("%s: main process exited, code=%s, status=%i/%s", | |
2432 | u->id, sigchld_code_to_string(code), status, | |
2433 | strna(code == CLD_EXITED | |
2434 | ? exit_status_to_string(status, EXIT_STATUS_FULL) | |
2435 | : signal_to_string(status))), | |
2436 | "EXIT_CODE=%s", sigchld_code_to_string(code), | |
2437 | "EXIT_STATUS=%i", status, | |
2438 | NULL); | |
f42806df LP |
2439 | |
2440 | if (f != SERVICE_SUCCESS) | |
2441 | s->result = f; | |
034c6ed7 | 2442 | |
867b3b7d LP |
2443 | if (s->main_command && |
2444 | s->main_command->command_next && | |
f42806df | 2445 | f == SERVICE_SUCCESS) { |
034c6ed7 | 2446 | |
34e9ba66 LP |
2447 | /* There is another command to * |
2448 | * execute, so let's do that. */ | |
034c6ed7 | 2449 | |
79008bdd | 2450 | log_unit_debug(u->id, "%s running next main command for state %s", u->id, service_state_to_string(s->state)); |
f42806df | 2451 | service_run_next_main(s); |
034c6ed7 | 2452 | |
34e9ba66 LP |
2453 | } else { |
2454 | ||
2455 | /* The service exited, so the service is officially | |
2456 | * gone. */ | |
867b3b7d | 2457 | s->main_command = NULL; |
34e9ba66 LP |
2458 | |
2459 | switch (s->state) { | |
2460 | ||
2461 | case SERVICE_START_POST: | |
2462 | case SERVICE_RELOAD: | |
2463 | case SERVICE_STOP: | |
2464 | /* Need to wait until the operation is | |
2465 | * done */ | |
c4653a4d | 2466 | break; |
7d55e835 | 2467 | |
34e9ba66 LP |
2468 | case SERVICE_START: |
2469 | if (s->type == SERVICE_ONESHOT) { | |
2470 | /* This was our main goal, so let's go on */ | |
f42806df | 2471 | if (f == SERVICE_SUCCESS) |
34e9ba66 LP |
2472 | service_enter_start_post(s); |
2473 | else | |
f42806df | 2474 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
34e9ba66 | 2475 | break; |
34e9ba66 | 2476 | } |
034c6ed7 | 2477 | |
bfba3256 LP |
2478 | /* Fall through */ |
2479 | ||
34e9ba66 | 2480 | case SERVICE_RUNNING: |
f42806df | 2481 | service_enter_running(s, f); |
34e9ba66 | 2482 | break; |
034c6ed7 | 2483 | |
db2cb23b | 2484 | case SERVICE_STOP_SIGABRT: |
34e9ba66 LP |
2485 | case SERVICE_STOP_SIGTERM: |
2486 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 2487 | |
34e9ba66 | 2488 | if (!control_pid_good(s)) |
f42806df | 2489 | service_enter_stop_post(s, f); |
5cb5a6ff | 2490 | |
34e9ba66 LP |
2491 | /* If there is still a control process, wait for that first */ |
2492 | break; | |
2493 | ||
bf108e55 LP |
2494 | case SERVICE_STOP_POST: |
2495 | case SERVICE_FINAL_SIGTERM: | |
2496 | case SERVICE_FINAL_SIGKILL: | |
2497 | ||
2498 | if (!control_pid_good(s)) | |
2499 | service_enter_dead(s, f, true); | |
2500 | break; | |
2501 | ||
34e9ba66 LP |
2502 | default: |
2503 | assert_not_reached("Uh, main process died at wrong time."); | |
2504 | } | |
034c6ed7 | 2505 | } |
5cb5a6ff | 2506 | |
034c6ed7 | 2507 | } else if (s->control_pid == pid) { |
34e9ba66 LP |
2508 | s->control_pid = 0; |
2509 | ||
b708e7ce | 2510 | if (s->control_command) { |
8bb2d17d | 2511 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 2512 | |
b708e7ce | 2513 | if (s->control_command->ignore) |
f42806df | 2514 | f = SERVICE_SUCCESS; |
b708e7ce LP |
2515 | } |
2516 | ||
79008bdd LP |
2517 | log_unit_full(u->id, |
2518 | f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, | |
66870f90 ZJS |
2519 | "%s: control process exited, code=%s status=%i", |
2520 | u->id, sigchld_code_to_string(code), status); | |
f42806df LP |
2521 | |
2522 | if (f != SERVICE_SUCCESS) | |
2523 | s->result = f; | |
034c6ed7 | 2524 | |
88f3e0c9 LP |
2525 | /* Immediately get rid of the cgroup, so that the |
2526 | * kernel doesn't delay the cgroup empty messages for | |
2527 | * the service cgroup any longer than necessary */ | |
4ad49000 | 2528 | service_kill_control_processes(s); |
88f3e0c9 | 2529 | |
34e9ba66 LP |
2530 | if (s->control_command && |
2531 | s->control_command->command_next && | |
f42806df | 2532 | f == SERVICE_SUCCESS) { |
034c6ed7 LP |
2533 | |
2534 | /* There is another command to * | |
2535 | * execute, so let's do that. */ | |
2536 | ||
79008bdd | 2537 | log_unit_debug(u->id, "%s running next control command for state %s", u->id, service_state_to_string(s->state)); |
f42806df | 2538 | service_run_next_control(s); |
034c6ed7 | 2539 | |
80876c20 | 2540 | } else { |
034c6ed7 LP |
2541 | /* No further commands for this step, so let's |
2542 | * figure out what to do next */ | |
2543 | ||
a16e1123 LP |
2544 | s->control_command = NULL; |
2545 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2546 | ||
79008bdd | 2547 | log_unit_debug(u->id, "%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state)); |
bd982a8b | 2548 | |
034c6ed7 LP |
2549 | switch (s->state) { |
2550 | ||
2551 | case SERVICE_START_PRE: | |
f42806df | 2552 | if (f == SERVICE_SUCCESS) |
034c6ed7 LP |
2553 | service_enter_start(s); |
2554 | else | |
f42806df | 2555 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
034c6ed7 LP |
2556 | break; |
2557 | ||
2558 | case SERVICE_START: | |
bfba3256 LP |
2559 | if (s->type != SERVICE_FORKING) |
2560 | /* Maybe spurious event due to a reload that changed the type? */ | |
2561 | break; | |
034c6ed7 | 2562 | |
f42806df LP |
2563 | if (f != SERVICE_SUCCESS) { |
2564 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
3a111838 MS |
2565 | break; |
2566 | } | |
034c6ed7 | 2567 | |
3a111838 | 2568 | if (s->pid_file) { |
f42806df LP |
2569 | bool has_start_post; |
2570 | int r; | |
2571 | ||
3a111838 MS |
2572 | /* Let's try to load the pid file here if we can. |
2573 | * The PID file might actually be created by a START_POST | |
2574 | * script. In that case don't worry if the loading fails. */ | |
f42806df LP |
2575 | |
2576 | has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST]; | |
2577 | r = service_load_pid_file(s, !has_start_post); | |
3a111838 MS |
2578 | if (!has_start_post && r < 0) { |
2579 | r = service_demand_pid_file(s); | |
2580 | if (r < 0 || !cgroup_good(s)) | |
f42806df | 2581 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
3a111838 MS |
2582 | break; |
2583 | } | |
034c6ed7 | 2584 | } else |
3a111838 | 2585 | service_search_main_pid(s); |
034c6ed7 | 2586 | |
3a111838 | 2587 | service_enter_start_post(s); |
034c6ed7 LP |
2588 | break; |
2589 | ||
2590 | case SERVICE_START_POST: | |
f42806df LP |
2591 | if (f != SERVICE_SUCCESS) { |
2592 | service_enter_stop(s, f); | |
2096e009 | 2593 | break; |
034c6ed7 LP |
2594 | } |
2595 | ||
2096e009 | 2596 | if (s->pid_file) { |
f42806df LP |
2597 | int r; |
2598 | ||
2599 | r = service_load_pid_file(s, true); | |
2096e009 MS |
2600 | if (r < 0) { |
2601 | r = service_demand_pid_file(s); | |
2602 | if (r < 0 || !cgroup_good(s)) | |
f42806df | 2603 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
2096e009 MS |
2604 | break; |
2605 | } | |
2606 | } else | |
2607 | service_search_main_pid(s); | |
2608 | ||
f42806df | 2609 | service_enter_running(s, SERVICE_SUCCESS); |
3185a36b | 2610 | break; |
034c6ed7 LP |
2611 | |
2612 | case SERVICE_RELOAD: | |
f42806df | 2613 | if (f == SERVICE_SUCCESS) { |
5375410b | 2614 | service_load_pid_file(s, true); |
3185a36b LP |
2615 | service_search_main_pid(s); |
2616 | } | |
2617 | ||
f42806df LP |
2618 | s->reload_result = f; |
2619 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
2620 | break; |
2621 | ||
2622 | case SERVICE_STOP: | |
f42806df | 2623 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
2624 | break; |
2625 | ||
db2cb23b | 2626 | case SERVICE_STOP_SIGABRT: |
034c6ed7 LP |
2627 | case SERVICE_STOP_SIGTERM: |
2628 | case SERVICE_STOP_SIGKILL: | |
2629 | if (main_pid_good(s) <= 0) | |
f42806df | 2630 | service_enter_stop_post(s, f); |
034c6ed7 LP |
2631 | |
2632 | /* If there is still a service | |
2633 | * process around, wait until | |
2634 | * that one quit, too */ | |
2635 | break; | |
2636 | ||
2637 | case SERVICE_STOP_POST: | |
2638 | case SERVICE_FINAL_SIGTERM: | |
2639 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 LP |
2640 | if (main_pid_good(s) <= 0) |
2641 | service_enter_dead(s, f, true); | |
034c6ed7 LP |
2642 | break; |
2643 | ||
2644 | default: | |
2645 | assert_not_reached("Uh, control process died at wrong time."); | |
2646 | } | |
2647 | } | |
8c47c732 | 2648 | } |
c4e2ceae LP |
2649 | |
2650 | /* Notify clients about changed exit status */ | |
2651 | unit_add_to_dbus_queue(u); | |
a911bb9a LP |
2652 | |
2653 | /* We got one SIGCHLD for the service, let's watch all | |
2654 | * processes that are now running of the service, and watch | |
2655 | * that. Among the PIDs we then watch will be children | |
2656 | * reassigned to us, which hopefully allows us to identify | |
2657 | * when all children are gone */ | |
2658 | unit_tidy_watch_pids(u, s->main_pid, s->control_pid); | |
2659 | unit_watch_all_pids(u); | |
2660 | ||
2661 | /* If the PID set is empty now, then let's finish this off */ | |
2662 | if (set_isempty(u->pids)) | |
2663 | service_notify_cgroup_empty_event(u); | |
034c6ed7 LP |
2664 | } |
2665 | ||
718db961 LP |
2666 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
2667 | Service *s = SERVICE(userdata); | |
034c6ed7 LP |
2668 | |
2669 | assert(s); | |
718db961 | 2670 | assert(source == s->timer_event_source); |
034c6ed7 LP |
2671 | |
2672 | switch (s->state) { | |
2673 | ||
2674 | case SERVICE_START_PRE: | |
2675 | case SERVICE_START: | |
79008bdd | 2676 | log_unit_warning(UNIT(s)->id, "%s %s operation timed out. Terminating.", UNIT(s)->id, s->state == SERVICE_START ? "start" : "start-pre"); |
f42806df | 2677 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
80876c20 LP |
2678 | break; |
2679 | ||
034c6ed7 | 2680 | case SERVICE_START_POST: |
79008bdd | 2681 | log_unit_warning(UNIT(s)->id, "%s start-post operation timed out. Stopping.", UNIT(s)->id); |
f42806df | 2682 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2683 | break; |
2684 | ||
e2f3b44c | 2685 | case SERVICE_RELOAD: |
79008bdd | 2686 | log_unit_warning(UNIT(s)->id, "%s reload operation timed out. Stopping.", UNIT(s)->id); |
f42806df LP |
2687 | s->reload_result = SERVICE_FAILURE_TIMEOUT; |
2688 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c LP |
2689 | break; |
2690 | ||
034c6ed7 | 2691 | case SERVICE_STOP: |
79008bdd | 2692 | log_unit_warning(UNIT(s)->id, "%s stopping timed out. Terminating.", UNIT(s)->id); |
f42806df | 2693 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2694 | break; |
2695 | ||
db2cb23b | 2696 | case SERVICE_STOP_SIGABRT: |
79008bdd | 2697 | log_unit_warning(UNIT(s)->id, |
db2cb23b UTL |
2698 | "%s stop-sigabrt timed out. Terminating.", UNIT(s)->id); |
2699 | service_enter_signal(s, SERVICE_STOP_SIGTERM, s->result); | |
2700 | break; | |
2701 | ||
034c6ed7 | 2702 | case SERVICE_STOP_SIGTERM: |
4819ff03 | 2703 | if (s->kill_context.send_sigkill) { |
79008bdd | 2704 | log_unit_warning(UNIT(s)->id, "%s stop-sigterm timed out. Killing.", UNIT(s)->id); |
f42806df | 2705 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 2706 | } else { |
79008bdd | 2707 | log_unit_warning(UNIT(s)->id, "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id); |
f42806df | 2708 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
ba035df2 LP |
2709 | } |
2710 | ||
034c6ed7 LP |
2711 | break; |
2712 | ||
2713 | case SERVICE_STOP_SIGKILL: | |
35b8ca3a | 2714 | /* Uh, we sent a SIGKILL and it is still not gone? |
034c6ed7 LP |
2715 | * Must be something we cannot kill, so let's just be |
2716 | * weirded out and continue */ | |
2717 | ||
79008bdd | 2718 | log_unit_warning(UNIT(s)->id, "%s still around after SIGKILL. Ignoring.", UNIT(s)->id); |
f42806df | 2719 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2720 | break; |
2721 | ||
2722 | case SERVICE_STOP_POST: | |
79008bdd | 2723 | log_unit_warning(UNIT(s)->id, "%s stop-post timed out. Terminating.", UNIT(s)->id); |
f42806df | 2724 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2725 | break; |
2726 | ||
2727 | case SERVICE_FINAL_SIGTERM: | |
4819ff03 | 2728 | if (s->kill_context.send_sigkill) { |
79008bdd | 2729 | log_unit_warning(UNIT(s)->id, "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id); |
f42806df | 2730 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 2731 | } else { |
79008bdd | 2732 | log_unit_warning(UNIT(s)->id, "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", UNIT(s)->id); |
f42806df | 2733 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
ba035df2 LP |
2734 | } |
2735 | ||
034c6ed7 LP |
2736 | break; |
2737 | ||
2738 | case SERVICE_FINAL_SIGKILL: | |
79008bdd | 2739 | log_unit_warning(UNIT(s)->id, "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id); |
f42806df | 2740 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
034c6ed7 LP |
2741 | break; |
2742 | ||
2743 | case SERVICE_AUTO_RESTART: | |
79008bdd | 2744 | log_unit_info(UNIT(s)->id, |
ef417cfd ZJS |
2745 | s->restart_usec > 0 ? |
2746 | "%s holdoff time over, scheduling restart." : | |
2747 | "%s has no holdoff time, scheduling restart.", | |
2748 | UNIT(s)->id); | |
034c6ed7 LP |
2749 | service_enter_restart(s); |
2750 | break; | |
2751 | ||
2752 | default: | |
2753 | assert_not_reached("Timeout at wrong time."); | |
2754 | } | |
718db961 LP |
2755 | |
2756 | return 0; | |
2757 | } | |
2758 | ||
2759 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { | |
2760 | Service *s = SERVICE(userdata); | |
a7850c7d | 2761 | char t[FORMAT_TIMESPAN_MAX]; |
718db961 LP |
2762 | |
2763 | assert(s); | |
2764 | assert(source == s->watchdog_event_source); | |
2765 | ||
79008bdd | 2766 | log_unit_error(UNIT(s)->id, "%s watchdog timeout (limit %s)!", UNIT(s)->id, |
a7850c7d | 2767 | format_timespan(t, sizeof(t), s->watchdog_usec, 1)); |
8bb2d17d | 2768 | |
db2cb23b | 2769 | service_enter_signal(s, SERVICE_STOP_SIGABRT, SERVICE_FAILURE_WATCHDOG); |
842129f5 | 2770 | |
718db961 | 2771 | return 0; |
5cb5a6ff LP |
2772 | } |
2773 | ||
a354329f | 2774 | static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds) { |
8c47c732 | 2775 | Service *s = SERVICE(u); |
308d72dc | 2776 | _cleanup_free_ char *cc = NULL; |
30b5275a | 2777 | bool notify_dbus = false; |
308d72dc | 2778 | const char *e; |
8c47c732 LP |
2779 | |
2780 | assert(u); | |
2781 | ||
308d72dc | 2782 | cc = strv_join(tags, ", "); |
79008bdd | 2783 | log_unit_debug(u->id, "%s: Got notification message from PID "PID_FMT" (%s)", |
308d72dc | 2784 | u->id, pid, isempty(cc) ? "n/a" : cc); |
da13d4d2 | 2785 | |
c952c6ec | 2786 | if (s->notify_access == NOTIFY_NONE) { |
79008bdd | 2787 | log_unit_warning(u->id, "%s: Got notification message from PID "PID_FMT", but reception is disabled.", u->id, pid); |
c952c6ec LP |
2788 | return; |
2789 | } | |
2790 | ||
336c6e46 | 2791 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { |
336c6e46 | 2792 | if (s->main_pid != 0) |
79008bdd | 2793 | 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 | 2794 | else |
79008bdd | 2795 | 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 |
2796 | return; |
2797 | } | |
2798 | ||
8c47c732 | 2799 | /* Interpret MAINPID= */ |
28849dba | 2800 | e = strv_find_startswith(tags, "MAINPID="); |
5e56b378 | 2801 | if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) { |
28849dba | 2802 | if (parse_pid(e, &pid) < 0) |
79008bdd | 2803 | log_unit_warning(u->id, "Failed to parse MAINPID= field in notification message: %s", e); |
8c47c732 | 2804 | else { |
79008bdd | 2805 | log_unit_debug(u->id, "%s: got MAINPID=%s", u->id, e); |
28849dba | 2806 | |
5925dd3c | 2807 | service_set_main_pid(s, pid); |
7400b9d2 | 2808 | unit_watch_pid(UNIT(s), pid); |
30b5275a | 2809 | notify_dbus = true; |
8c47c732 LP |
2810 | } |
2811 | } | |
2812 | ||
308d72dc LP |
2813 | /* Interpret RELOADING= */ |
2814 | if (strv_find(tags, "RELOADING=1")) { | |
2815 | ||
79008bdd | 2816 | log_unit_debug(u->id, "%s: got RELOADING=1", u->id); |
308d72dc LP |
2817 | s->notify_state = NOTIFY_RELOADING; |
2818 | ||
2819 | if (s->state == SERVICE_RUNNING) | |
2820 | service_enter_reload_by_notify(s); | |
2821 | ||
2822 | notify_dbus = true; | |
2823 | } | |
2824 | ||
8c47c732 | 2825 | /* Interpret READY= */ |
308d72dc LP |
2826 | if (strv_find(tags, "READY=1")) { |
2827 | ||
79008bdd | 2828 | log_unit_debug(u->id, "%s: got READY=1", u->id); |
308d72dc LP |
2829 | s->notify_state = NOTIFY_READY; |
2830 | ||
2831 | /* Type=notify services inform us about completed | |
2832 | * initialization with READY=1 */ | |
2833 | if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START) | |
2834 | service_enter_start_post(s); | |
2835 | ||
2836 | /* Sending READY=1 while we are reloading informs us | |
2837 | * that the reloading is complete */ | |
2838 | if (s->state == SERVICE_RELOAD && s->control_pid == 0) | |
2839 | service_enter_running(s, SERVICE_SUCCESS); | |
2840 | ||
2841 | notify_dbus = true; | |
2842 | } | |
2843 | ||
2844 | /* Interpret STOPPING= */ | |
2845 | if (strv_find(tags, "STOPPING=1")) { | |
2846 | ||
79008bdd | 2847 | log_unit_debug(u->id, "%s: got STOPPING=1", u->id); |
308d72dc LP |
2848 | s->notify_state = NOTIFY_STOPPING; |
2849 | ||
2850 | if (s->state == SERVICE_RUNNING) | |
2851 | service_enter_stop_by_notify(s); | |
2852 | ||
30b5275a | 2853 | notify_dbus = true; |
8c47c732 LP |
2854 | } |
2855 | ||
2856 | /* Interpret STATUS= */ | |
28849dba | 2857 | e = strv_find_startswith(tags, "STATUS="); |
7f110ff9 | 2858 | if (e) { |
28849dba | 2859 | _cleanup_free_ char *t = NULL; |
8c47c732 | 2860 | |
28849dba LP |
2861 | if (!isempty(e)) { |
2862 | if (!utf8_is_valid(e)) | |
79008bdd | 2863 | log_unit_warning(u->id, "Status message in notification is not UTF-8 clean."); |
28849dba | 2864 | else { |
79008bdd | 2865 | log_unit_debug(u->id, "%s: got STATUS=%s", u->id, e); |
7f110ff9 | 2866 | |
28849dba LP |
2867 | t = strdup(e); |
2868 | if (!t) | |
2869 | log_oom(); | |
3a2776bc | 2870 | } |
28849dba | 2871 | } |
8c47c732 | 2872 | |
30b5275a | 2873 | if (!streq_ptr(s->status_text, t)) { |
28849dba | 2874 | |
3a2776bc LP |
2875 | free(s->status_text); |
2876 | s->status_text = t; | |
28849dba LP |
2877 | t = NULL; |
2878 | ||
30b5275a | 2879 | notify_dbus = true; |
28849dba | 2880 | } |
8c47c732 | 2881 | } |
842129f5 | 2882 | |
4774e357 | 2883 | /* Interpret ERRNO= */ |
28849dba | 2884 | e = strv_find_startswith(tags, "ERRNO="); |
4774e357 MAA |
2885 | if (e) { |
2886 | int status_errno; | |
2887 | ||
28849dba | 2888 | if (safe_atoi(e, &status_errno) < 0 || status_errno < 0) |
79008bdd | 2889 | log_unit_warning(u->id, "Failed to parse ERRNO= field in notification message: %s", e); |
4774e357 | 2890 | else { |
79008bdd | 2891 | log_unit_debug(u->id, "%s: got ERRNO=%s", u->id, e); |
4774e357 MAA |
2892 | |
2893 | if (s->status_errno != status_errno) { | |
2894 | s->status_errno = status_errno; | |
2895 | notify_dbus = true; | |
2896 | } | |
2897 | } | |
2898 | } | |
2899 | ||
6f285378 | 2900 | /* Interpret WATCHDOG= */ |
a6927d7f | 2901 | if (strv_find(tags, "WATCHDOG=1")) { |
79008bdd | 2902 | log_unit_debug(u->id, "%s: got WATCHDOG=1", u->id); |
842129f5 | 2903 | service_reset_watchdog(s); |
a6927d7f | 2904 | } |
c4e2ceae | 2905 | |
a354329f LP |
2906 | /* Add the passed fds to the fd store */ |
2907 | if (strv_find(tags, "FDSTORE=1")) { | |
2908 | log_unit_debug(u->id, "%s: got FDSTORE=1", u->id); | |
2339fc93 | 2909 | service_add_fd_store_set(s, fds); |
a354329f LP |
2910 | } |
2911 | ||
c4e2ceae | 2912 | /* Notify clients about changed status or main pid */ |
30b5275a LP |
2913 | if (notify_dbus) |
2914 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
2915 | } |
2916 | ||
68db7a3b ZJS |
2917 | static int service_get_timeout(Unit *u, uint64_t *timeout) { |
2918 | Service *s = SERVICE(u); | |
2919 | int r; | |
2920 | ||
2921 | if (!s->timer_event_source) | |
2922 | return 0; | |
2923 | ||
2924 | r = sd_event_source_get_time(s->timer_event_source, timeout); | |
2925 | if (r < 0) | |
2926 | return r; | |
2927 | ||
2928 | return 1; | |
2929 | } | |
2930 | ||
05e343b7 LP |
2931 | static void service_bus_name_owner_change( |
2932 | Unit *u, | |
2933 | const char *name, | |
2934 | const char *old_owner, | |
2935 | const char *new_owner) { | |
2936 | ||
2937 | Service *s = SERVICE(u); | |
718db961 | 2938 | int r; |
05e343b7 LP |
2939 | |
2940 | assert(s); | |
2941 | assert(name); | |
2942 | ||
2943 | assert(streq(s->bus_name, name)); | |
2944 | assert(old_owner || new_owner); | |
2945 | ||
2946 | if (old_owner && new_owner) | |
79008bdd | 2947 | 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 | 2948 | else if (old_owner) |
79008bdd | 2949 | log_unit_debug(u->id, "%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner); |
05e343b7 | 2950 | else |
79008bdd | 2951 | log_unit_debug(u->id, "%s's D-Bus name %s now registered by %s", u->id, name, new_owner); |
05e343b7 LP |
2952 | |
2953 | s->bus_name_good = !!new_owner; | |
2954 | ||
2955 | if (s->type == SERVICE_DBUS) { | |
2956 | ||
2957 | /* service_enter_running() will figure out what to | |
2958 | * do */ | |
2959 | if (s->state == SERVICE_RUNNING) | |
f42806df | 2960 | service_enter_running(s, SERVICE_SUCCESS); |
05e343b7 LP |
2961 | else if (s->state == SERVICE_START && new_owner) |
2962 | service_enter_start_post(s); | |
2963 | ||
2964 | } else if (new_owner && | |
2965 | s->main_pid <= 0 && | |
2966 | (s->state == SERVICE_START || | |
2967 | s->state == SERVICE_START_POST || | |
2968 | s->state == SERVICE_RUNNING || | |
2969 | s->state == SERVICE_RELOAD)) { | |
2970 | ||
5b12334d | 2971 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
718db961 | 2972 | pid_t pid; |
05e343b7 | 2973 | |
718db961 | 2974 | /* Try to acquire PID from bus service */ |
05e343b7 | 2975 | |
056f95d0 | 2976 | r = sd_bus_get_name_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds); |
5b12334d LP |
2977 | if (r >= 0) |
2978 | r = sd_bus_creds_get_pid(creds, &pid); | |
718db961 | 2979 | if (r >= 0) { |
79008bdd | 2980 | log_unit_debug(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid); |
05e343b7 | 2981 | |
718db961 LP |
2982 | service_set_main_pid(s, pid); |
2983 | unit_watch_pid(UNIT(s), pid); | |
2984 | } | |
7400b9d2 | 2985 | } |
05e343b7 LP |
2986 | } |
2987 | ||
16115b0a | 2988 | int service_set_socket_fd(Service *s, int fd, Socket *sock, bool selinux_context_net) { |
79a98c60 LP |
2989 | _cleanup_free_ char *peer = NULL; |
2990 | int r; | |
57020a3a | 2991 | |
4f2d528d LP |
2992 | assert(s); |
2993 | assert(fd >= 0); | |
2994 | ||
2995 | /* This is called by the socket code when instantiating a new | |
2996 | * service for a stream socket and the socket needs to be | |
2997 | * configured. */ | |
2998 | ||
1124fe6f | 2999 | if (UNIT(s)->load_state != UNIT_LOADED) |
4f2d528d LP |
3000 | return -EINVAL; |
3001 | ||
3002 | if (s->socket_fd >= 0) | |
3003 | return -EBUSY; | |
3004 | ||
3005 | if (s->state != SERVICE_DEAD) | |
3006 | return -EAGAIN; | |
3007 | ||
79a98c60 LP |
3008 | if (getpeername_pretty(fd, &peer) >= 0) { |
3009 | ||
3010 | if (UNIT(s)->description) { | |
3011 | _cleanup_free_ char *a; | |
3012 | ||
3013 | a = strjoin(UNIT(s)->description, " (", peer, ")", NULL); | |
3014 | if (!a) | |
3015 | return -ENOMEM; | |
3016 | ||
3017 | r = unit_set_description(UNIT(s), a); | |
3018 | } else | |
3019 | r = unit_set_description(UNIT(s), peer); | |
3020 | ||
3021 | if (r < 0) | |
3022 | return r; | |
3023 | } | |
3024 | ||
4f2d528d | 3025 | s->socket_fd = fd; |
16115b0a | 3026 | s->socket_fd_selinux_context_net = selinux_context_net; |
6cf6bbc2 | 3027 | |
57020a3a LP |
3028 | unit_ref_set(&s->accept_socket, UNIT(sock)); |
3029 | ||
3030 | return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false); | |
4f2d528d LP |
3031 | } |
3032 | ||
fdf20a31 | 3033 | static void service_reset_failed(Unit *u) { |
5632e374 LP |
3034 | Service *s = SERVICE(u); |
3035 | ||
3036 | assert(s); | |
3037 | ||
fdf20a31 | 3038 | if (s->state == SERVICE_FAILED) |
5632e374 LP |
3039 | service_set_state(s, SERVICE_DEAD); |
3040 | ||
f42806df LP |
3041 | s->result = SERVICE_SUCCESS; |
3042 | s->reload_result = SERVICE_SUCCESS; | |
451b34cc LP |
3043 | |
3044 | RATELIMIT_RESET(s->start_limit); | |
5632e374 LP |
3045 | } |
3046 | ||
718db961 | 3047 | static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
8a0867d6 | 3048 | Service *s = SERVICE(u); |
41efeaec | 3049 | |
814cc562 | 3050 | return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error); |
8a0867d6 LP |
3051 | } |
3052 | ||
94f04347 LP |
3053 | static const char* const service_state_table[_SERVICE_STATE_MAX] = { |
3054 | [SERVICE_DEAD] = "dead", | |
3055 | [SERVICE_START_PRE] = "start-pre", | |
3056 | [SERVICE_START] = "start", | |
3057 | [SERVICE_START_POST] = "start-post", | |
3058 | [SERVICE_RUNNING] = "running", | |
80876c20 | 3059 | [SERVICE_EXITED] = "exited", |
94f04347 LP |
3060 | [SERVICE_RELOAD] = "reload", |
3061 | [SERVICE_STOP] = "stop", | |
1378a3b5 | 3062 | [SERVICE_STOP_SIGABRT] = "stop-sigabrt", |
94f04347 LP |
3063 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", |
3064 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
3065 | [SERVICE_STOP_POST] = "stop-post", | |
3066 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
3067 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 3068 | [SERVICE_FAILED] = "failed", |
94f04347 LP |
3069 | [SERVICE_AUTO_RESTART] = "auto-restart", |
3070 | }; | |
3071 | ||
3072 | DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); | |
3073 | ||
3074 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { | |
525ee6f4 LP |
3075 | [SERVICE_RESTART_NO] = "no", |
3076 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
50caaedb | 3077 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", |
6cfe2fde | 3078 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
dc99a976 | 3079 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
50caaedb | 3080 | [SERVICE_RESTART_ON_ABORT] = "on-abort", |
6cfe2fde | 3081 | [SERVICE_RESTART_ALWAYS] = "always", |
94f04347 LP |
3082 | }; |
3083 | ||
3084 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
3085 | ||
3086 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
94f04347 | 3087 | [SERVICE_SIMPLE] = "simple", |
0d624a78 | 3088 | [SERVICE_FORKING] = "forking", |
34e9ba66 | 3089 | [SERVICE_ONESHOT] = "oneshot", |
8c47c732 | 3090 | [SERVICE_DBUS] = "dbus", |
f2b68789 LP |
3091 | [SERVICE_NOTIFY] = "notify", |
3092 | [SERVICE_IDLE] = "idle" | |
94f04347 LP |
3093 | }; |
3094 | ||
3095 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
3096 | ||
e537352b | 3097 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
94f04347 LP |
3098 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", |
3099 | [SERVICE_EXEC_START] = "ExecStart", | |
3100 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
3101 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
3102 | [SERVICE_EXEC_STOP] = "ExecStop", | |
3103 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
3104 | }; | |
3105 | ||
3106 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
3107 | ||
c952c6ec LP |
3108 | static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = { |
3109 | [NOTIFY_NONE] = "none", | |
3110 | [NOTIFY_MAIN] = "main", | |
3111 | [NOTIFY_ALL] = "all" | |
3112 | }; | |
3113 | ||
3114 | DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess); | |
3115 | ||
308d72dc LP |
3116 | static const char* const notify_state_table[_NOTIFY_STATE_MAX] = { |
3117 | [NOTIFY_UNKNOWN] = "unknown", | |
3118 | [NOTIFY_READY] = "ready", | |
3119 | [NOTIFY_RELOADING] = "reloading", | |
3120 | [NOTIFY_STOPPING] = "stopping", | |
3121 | }; | |
3122 | ||
3123 | DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); | |
3124 | ||
f42806df LP |
3125 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
3126 | [SERVICE_SUCCESS] = "success", | |
3127 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
3128 | [SERVICE_FAILURE_TIMEOUT] = "timeout", | |
3129 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
3130 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
bb242b7b | 3131 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", |
8d1b002a LP |
3132 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", |
3133 | [SERVICE_FAILURE_START_LIMIT] = "start-limit" | |
f42806df LP |
3134 | }; |
3135 | ||
3136 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
3137 | ||
87f0e418 | 3138 | const UnitVTable service_vtable = { |
7d17cfbc | 3139 | .object_size = sizeof(Service), |
718db961 LP |
3140 | .exec_context_offset = offsetof(Service, exec_context), |
3141 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
3142 | .kill_context_offset = offsetof(Service, kill_context), | |
613b411c | 3143 | .exec_runtime_offset = offsetof(Service, exec_runtime), |
3ef63c31 | 3144 | |
f975e971 LP |
3145 | .sections = |
3146 | "Unit\0" | |
3147 | "Service\0" | |
3148 | "Install\0", | |
4ad49000 | 3149 | .private_section = "Service", |
71645aca | 3150 | |
034c6ed7 LP |
3151 | .init = service_init, |
3152 | .done = service_done, | |
a16e1123 | 3153 | .load = service_load, |
a354329f | 3154 | .release_resources = service_release_resources, |
a16e1123 LP |
3155 | |
3156 | .coldplug = service_coldplug, | |
034c6ed7 | 3157 | |
5cb5a6ff LP |
3158 | .dump = service_dump, |
3159 | ||
3160 | .start = service_start, | |
3161 | .stop = service_stop, | |
3162 | .reload = service_reload, | |
3163 | ||
034c6ed7 LP |
3164 | .can_reload = service_can_reload, |
3165 | ||
8a0867d6 LP |
3166 | .kill = service_kill, |
3167 | ||
a16e1123 LP |
3168 | .serialize = service_serialize, |
3169 | .deserialize_item = service_deserialize_item, | |
3170 | ||
5cb5a6ff | 3171 | .active_state = service_active_state, |
10a94420 | 3172 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 3173 | |
701cc384 LP |
3174 | .check_gc = service_check_gc, |
3175 | .check_snapshot = service_check_snapshot, | |
3176 | ||
034c6ed7 | 3177 | .sigchld_event = service_sigchld_event, |
2c4104f0 | 3178 | |
fdf20a31 | 3179 | .reset_failed = service_reset_failed, |
5632e374 | 3180 | |
4ad49000 | 3181 | .notify_cgroup_empty = service_notify_cgroup_empty_event, |
8c47c732 | 3182 | .notify_message = service_notify_message, |
8e274523 | 3183 | |
05e343b7 | 3184 | .bus_name_owner_change = service_bus_name_owner_change, |
05e343b7 | 3185 | |
c4e2ceae | 3186 | .bus_interface = "org.freedesktop.systemd1.Service", |
718db961 | 3187 | .bus_vtable = bus_service_vtable, |
74c964d3 LP |
3188 | .bus_set_property = bus_service_set_property, |
3189 | .bus_commit_properties = bus_service_commit_properties, | |
4139c1b2 | 3190 | |
68db7a3b | 3191 | .get_timeout = service_get_timeout, |
718db961 LP |
3192 | .can_transient = true, |
3193 | ||
c6918296 MS |
3194 | .status_message_formats = { |
3195 | .starting_stopping = { | |
3196 | [0] = "Starting %s...", | |
3197 | [1] = "Stopping %s...", | |
3198 | }, | |
3199 | .finished_start_job = { | |
3200 | [JOB_DONE] = "Started %s.", | |
3201 | [JOB_FAILED] = "Failed to start %s.", | |
3202 | [JOB_DEPENDENCY] = "Dependency failed for %s.", | |
3203 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
3204 | }, | |
3205 | .finished_stop_job = { | |
3206 | [JOB_DONE] = "Stopped %s.", | |
3207 | [JOB_FAILED] = "Stopped (with error) %s.", | |
3208 | [JOB_TIMEOUT] = "Timed out stopping %s.", | |
3209 | }, | |
3210 | }, | |
5cb5a6ff | 3211 | }; |