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