]>
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 | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
5cb5a6ff | 22 | #include <errno.h> |
034c6ed7 | 23 | #include <signal.h> |
2c4104f0 LP |
24 | #include <dirent.h> |
25 | #include <unistd.h> | |
5cb5a6ff | 26 | |
87f0e418 | 27 | #include "unit.h" |
5cb5a6ff LP |
28 | #include "service.h" |
29 | #include "load-fragment.h" | |
30 | #include "load-dropin.h" | |
034c6ed7 | 31 | #include "log.h" |
2c4104f0 | 32 | #include "strv.h" |
9e2f7c11 | 33 | #include "unit-name.h" |
4139c1b2 | 34 | #include "dbus-service.h" |
514f4ef5 | 35 | #include "special.h" |
398ef8ba | 36 | #include "bus-errors.h" |
2c4104f0 LP |
37 | |
38 | #define COMMENTS "#;\n" | |
39 | #define NEWLINES "\n\r" | |
034c6ed7 | 40 | |
07459bb6 | 41 | #ifdef HAVE_SYSV_COMPAT |
f34277d9 LP |
42 | |
43 | #define DEFAULT_SYSV_TIMEOUT_USEC (3*USEC_PER_MINUTE) | |
44 | ||
09cd1ab1 LP |
45 | typedef enum RunlevelType { |
46 | RUNLEVEL_UP, | |
47 | RUNLEVEL_DOWN, | |
fc5df99e | 48 | RUNLEVEL_SYSINIT |
09cd1ab1 LP |
49 | } RunlevelType; |
50 | ||
51 | static const struct { | |
52 | const char *path; | |
53 | const char *target; | |
54 | const RunlevelType type; | |
55 | } rcnd_table[] = { | |
9d25f5ed | 56 | /* Standard SysV runlevels for start-up */ |
514f4ef5 | 57 | { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP }, |
fbe9f3a9 LP |
58 | { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP }, |
59 | { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP }, | |
60 | { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP }, | |
61 | { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP }, | |
fbe9f3a9 | 62 | |
d4054675 | 63 | #ifdef TARGET_SUSE |
cfe243e3 | 64 | /* SUSE style boot.d */ |
fc5df99e | 65 | { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT }, |
d4054675 | 66 | #endif |
fbe9f3a9 | 67 | |
858dae18 | 68 | #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) |
fbe9f3a9 | 69 | /* Debian style rcS.d */ |
fc5df99e | 70 | { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT }, |
d4054675 | 71 | #endif |
9d25f5ed LP |
72 | |
73 | /* Standard SysV runlevels for shutdown */ | |
74 | { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN }, | |
75 | { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN } | |
76 | ||
77 | /* Note that the order here matters, as we read the | |
78 | directories in this order, and we want to make sure that | |
79 | sysv_start_priority is known when we first load the | |
80 | unit. And that value we only know from S links. Hence | |
81 | UP/SYSINIT must be read before DOWN */ | |
23a177ef LP |
82 | }; |
83 | ||
09cd1ab1 | 84 | #define RUNLEVELS_UP "12345" |
fbe9f3a9 LP |
85 | /* #define RUNLEVELS_DOWN "06" */ |
86 | /* #define RUNLEVELS_BOOT "bBsS" */ | |
07459bb6 | 87 | #endif |
09cd1ab1 | 88 | |
acbb0225 | 89 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
87f0e418 LP |
90 | [SERVICE_DEAD] = UNIT_INACTIVE, |
91 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
92 | [SERVICE_START] = UNIT_ACTIVATING, | |
93 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
94 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
80876c20 | 95 | [SERVICE_EXITED] = UNIT_ACTIVE, |
032ff4af | 96 | [SERVICE_RELOAD] = UNIT_RELOADING, |
87f0e418 LP |
97 | [SERVICE_STOP] = UNIT_DEACTIVATING, |
98 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
99 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
100 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
101 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
102 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 103 | [SERVICE_FAILED] = UNIT_FAILED, |
6124958c | 104 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING |
034c6ed7 | 105 | }; |
5cb5a6ff | 106 | |
a16e1123 LP |
107 | static void service_init(Unit *u) { |
108 | Service *s = SERVICE(u); | |
109 | ||
110 | assert(u); | |
111 | assert(u->meta.load_state == UNIT_STUB); | |
112 | ||
113 | s->timeout_usec = DEFAULT_TIMEOUT_USEC; | |
114 | s->restart_usec = DEFAULT_RESTART_USEC; | |
115 | s->timer_watch.type = WATCH_INVALID; | |
07459bb6 | 116 | #ifdef HAVE_SYSV_COMPAT |
a16e1123 | 117 | s->sysv_start_priority = -1; |
07459bb6 | 118 | #endif |
a16e1123 LP |
119 | s->socket_fd = -1; |
120 | ||
121 | exec_context_init(&s->exec_context); | |
122 | ||
123 | RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5); | |
124 | ||
125 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
126 | } | |
127 | ||
5e94833f LP |
128 | static void service_unwatch_control_pid(Service *s) { |
129 | assert(s); | |
130 | ||
131 | if (s->control_pid <= 0) | |
132 | return; | |
133 | ||
134 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
135 | s->control_pid = 0; | |
136 | } | |
137 | ||
138 | static void service_unwatch_main_pid(Service *s) { | |
139 | assert(s); | |
140 | ||
141 | if (s->main_pid <= 0) | |
142 | return; | |
143 | ||
144 | unit_unwatch_pid(UNIT(s), s->main_pid); | |
145 | s->main_pid = 0; | |
146 | } | |
147 | ||
5925dd3c | 148 | static int service_set_main_pid(Service *s, pid_t pid) { |
e55224ca LP |
149 | pid_t ppid; |
150 | ||
5925dd3c LP |
151 | assert(s); |
152 | ||
153 | if (pid <= 1) | |
154 | return -EINVAL; | |
155 | ||
156 | if (pid == getpid()) | |
157 | return -EINVAL; | |
158 | ||
e55224ca LP |
159 | if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) |
160 | log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.", | |
161 | s->meta.id, (unsigned long) pid); | |
162 | ||
5925dd3c LP |
163 | s->main_pid = pid; |
164 | s->main_pid_known = true; | |
165 | ||
b58b4116 LP |
166 | exec_status_start(&s->main_exec_status, pid); |
167 | ||
5925dd3c LP |
168 | return 0; |
169 | } | |
170 | ||
4f2d528d LP |
171 | static void service_close_socket_fd(Service *s) { |
172 | assert(s); | |
173 | ||
174 | if (s->socket_fd < 0) | |
175 | return; | |
176 | ||
177 | close_nointr_nofail(s->socket_fd); | |
178 | s->socket_fd = -1; | |
179 | } | |
180 | ||
6cf6bbc2 LP |
181 | static void service_connection_unref(Service *s) { |
182 | assert(s); | |
183 | ||
f976f3f6 | 184 | if (!s->accept_socket) |
6cf6bbc2 LP |
185 | return; |
186 | ||
f976f3f6 LP |
187 | socket_connection_unref(s->accept_socket); |
188 | s->accept_socket = NULL; | |
6cf6bbc2 LP |
189 | } |
190 | ||
87f0e418 LP |
191 | static void service_done(Unit *u) { |
192 | Service *s = SERVICE(u); | |
44d8db9e LP |
193 | |
194 | assert(s); | |
195 | ||
196 | free(s->pid_file); | |
197 | s->pid_file = NULL; | |
198 | ||
07459bb6 | 199 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 LP |
200 | free(s->sysv_path); |
201 | s->sysv_path = NULL; | |
202 | ||
8309400a LP |
203 | free(s->sysv_runlevels); |
204 | s->sysv_runlevels = NULL; | |
07459bb6 | 205 | #endif |
8309400a | 206 | |
8c47c732 LP |
207 | free(s->status_text); |
208 | s->status_text = NULL; | |
209 | ||
44d8db9e | 210 | exec_context_done(&s->exec_context); |
e537352b | 211 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
44d8db9e LP |
212 | s->control_command = NULL; |
213 | ||
214 | /* This will leak a process, but at least no memory or any of | |
215 | * our resources */ | |
5e94833f LP |
216 | service_unwatch_main_pid(s); |
217 | service_unwatch_control_pid(s); | |
44d8db9e | 218 | |
05e343b7 LP |
219 | if (s->bus_name) { |
220 | unit_unwatch_bus_name(UNIT(u), s->bus_name); | |
221 | free(s->bus_name); | |
222 | s->bus_name = NULL; | |
223 | } | |
224 | ||
4f2d528d | 225 | service_close_socket_fd(s); |
6cf6bbc2 | 226 | service_connection_unref(s); |
4f2d528d | 227 | |
f976f3f6 LP |
228 | set_free(s->configured_sockets); |
229 | ||
acbb0225 | 230 | unit_unwatch_timer(u, &s->timer_watch); |
44d8db9e LP |
231 | } |
232 | ||
07459bb6 | 233 | #ifdef HAVE_SYSV_COMPAT |
b7ccee3c LP |
234 | static char *sysv_translate_name(const char *name) { |
235 | char *r; | |
236 | ||
237 | if (!(r = new(char, strlen(name) + sizeof(".service")))) | |
238 | return NULL; | |
239 | ||
240 | if (startswith(name, "boot.")) | |
241 | /* Drop SuSE-style boot. prefix */ | |
242 | strcpy(stpcpy(r, name + 5), ".service"); | |
243 | else if (endswith(name, ".sh")) | |
244 | /* Drop Debian-style .sh suffix */ | |
245 | strcpy(stpcpy(r, name) - 3, ".service"); | |
980900c1 | 246 | #ifdef TARGET_ARCH |
9f151f29 LP |
247 | else if (startswith(name, "@")) |
248 | /* Drop Arch-style background prefix */ | |
249 | strcpy(stpcpy(r, name + 1), ".service"); | |
980900c1 | 250 | #endif |
b7ccee3c LP |
251 | else |
252 | /* Normal init scripts */ | |
253 | strcpy(stpcpy(r, name), ".service"); | |
254 | ||
255 | return r; | |
256 | } | |
257 | ||
ee95669f | 258 | static int sysv_translate_facility(const char *name, const char *filename, char **_r) { |
2c4104f0 | 259 | |
a7d3cc26 LP |
260 | /* We silently ignore the $ prefix here. According to the LSB |
261 | * spec it simply indicates whether something is a | |
262 | * standardized name or a distribution-specific one. Since we | |
263 | * just follow what already exists and do not introduce new | |
264 | * uses or names we don't care who introduced a new name. */ | |
265 | ||
2c4104f0 | 266 | static const char * const table[] = { |
6464aa08 | 267 | /* LSB defined facilities */ |
a7d3cc26 LP |
268 | "local_fs", SPECIAL_LOCAL_FS_TARGET, |
269 | "network", SPECIAL_NETWORK_TARGET, | |
270 | "named", SPECIAL_NSS_LOOKUP_TARGET, | |
271 | "portmap", SPECIAL_RPCBIND_TARGET, | |
272 | "remote_fs", SPECIAL_REMOTE_FS_TARGET, | |
273 | "syslog", SPECIAL_SYSLOG_TARGET, | |
274 | "time", SPECIAL_RTC_SET_TARGET, | |
6464aa08 LP |
275 | |
276 | /* Debian extensions */ | |
858dae18 | 277 | #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) |
a7d3cc26 | 278 | "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, |
3177a49c | 279 | #endif |
a7d3cc26 LP |
280 | "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, |
281 | "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE, | |
3177a49c LP |
282 | |
283 | #ifdef TARGET_FEDORA | |
a7d3cc26 LP |
284 | /* Fedora extensions */ |
285 | "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, | |
286 | "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, | |
287 | "httpd", SPECIAL_HTTP_DAEMON_TARGET, | |
3177a49c | 288 | #endif |
a7d3cc26 LP |
289 | |
290 | /* SuSE extensions */ | |
291 | "null", NULL | |
292 | ||
2c4104f0 LP |
293 | }; |
294 | ||
295 | unsigned i; | |
296 | char *r; | |
ee95669f | 297 | const char *n; |
2c4104f0 | 298 | |
ee95669f LP |
299 | assert(name); |
300 | assert(_r); | |
a7d3cc26 | 301 | |
ee95669f | 302 | n = *name == '$' ? name + 1 : name; |
a7d3cc26 | 303 | |
ee95669f | 304 | for (i = 0; i < ELEMENTSOF(table); i += 2) { |
a7d3cc26 | 305 | |
ee95669f LP |
306 | if (!streq(table[i], n)) |
307 | continue; | |
2c4104f0 | 308 | |
ee95669f LP |
309 | if (!table[i+1]) |
310 | return 0; | |
311 | ||
312 | if (!(r = strdup(table[i+1]))) | |
313 | return -ENOMEM; | |
314 | ||
315 | goto finish; | |
316 | } | |
2c4104f0 | 317 | |
a7d3cc26 | 318 | /* If we don't know this name, fallback heuristics to figure |
0003d1ab | 319 | * out whether something is a target or a service alias. */ |
a7d3cc26 | 320 | |
2c4104f0 | 321 | if (*name == '$') |
ee95669f LP |
322 | /* Facilities starting with $ are most likely targets */ |
323 | r = unit_name_build(n, NULL, ".target"); | |
324 | else if (filename && streq(name, filename)) | |
325 | /* Names equalling the file name of the services are redundant */ | |
326 | return 0; | |
32159d3a | 327 | else |
ee95669f LP |
328 | /* Everything else we assume to be normal service names */ |
329 | r = sysv_translate_name(n); | |
2c4104f0 | 330 | |
32159d3a | 331 | if (!r) |
2c4104f0 LP |
332 | return -ENOMEM; |
333 | ||
334 | finish: | |
335 | ||
336 | if (_r) | |
337 | *_r = r; | |
338 | ||
339 | return 1; | |
340 | } | |
341 | ||
56d748b4 | 342 | static int sysv_fix_order(Service *s) { |
2c4104f0 LP |
343 | Meta *other; |
344 | int r; | |
345 | ||
346 | assert(s); | |
347 | ||
348 | if (s->sysv_start_priority < 0) | |
349 | return 0; | |
350 | ||
23a177ef LP |
351 | /* For each pair of services where at least one lacks a LSB |
352 | * header, we use the start priority value to order things. */ | |
2c4104f0 | 353 | |
4cd1fbcc | 354 | LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) { |
2c4104f0 LP |
355 | Service *t; |
356 | UnitDependency d; | |
eeaafddc | 357 | bool special_s, special_t; |
2c4104f0 LP |
358 | |
359 | t = (Service*) other; | |
360 | ||
361 | if (s == t) | |
362 | continue; | |
9f151f29 LP |
363 | |
364 | if (t->meta.load_state != UNIT_LOADED) | |
365 | continue; | |
2c4104f0 LP |
366 | |
367 | if (t->sysv_start_priority < 0) | |
368 | continue; | |
369 | ||
51a1a79d LP |
370 | /* If both units have modern headers we don't care |
371 | * about the priorities */ | |
f21781d5 LP |
372 | if ((s->meta.fragment_path || s->sysv_has_lsb) && |
373 | (t->meta.fragment_path || t->sysv_has_lsb)) | |
23a177ef LP |
374 | continue; |
375 | ||
eeaafddc LP |
376 | special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels); |
377 | special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels); | |
378 | ||
379 | if (special_t && !special_s) | |
380 | d = UNIT_AFTER; | |
381 | else if (special_s && !special_t) | |
382 | d = UNIT_BEFORE; | |
383 | else if (t->sysv_start_priority < s->sysv_start_priority) | |
2c4104f0 LP |
384 | d = UNIT_AFTER; |
385 | else if (t->sysv_start_priority > s->sysv_start_priority) | |
386 | d = UNIT_BEFORE; | |
387 | else | |
388 | continue; | |
389 | ||
390 | /* FIXME: Maybe we should compare the name here lexicographically? */ | |
391 | ||
701cc384 | 392 | if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0) |
2c4104f0 LP |
393 | return r; |
394 | } | |
395 | ||
396 | return 0; | |
397 | } | |
398 | ||
399 | static ExecCommand *exec_command_new(const char *path, const char *arg1) { | |
400 | ExecCommand *c; | |
401 | ||
402 | if (!(c = new0(ExecCommand, 1))) | |
403 | return NULL; | |
404 | ||
405 | if (!(c->path = strdup(path))) { | |
406 | free(c); | |
407 | return NULL; | |
408 | } | |
409 | ||
410 | if (!(c->argv = strv_new(path, arg1, NULL))) { | |
411 | free(c->path); | |
412 | free(c); | |
413 | return NULL; | |
414 | } | |
415 | ||
416 | return c; | |
417 | } | |
418 | ||
419 | static int sysv_exec_commands(Service *s) { | |
420 | ExecCommand *c; | |
421 | ||
422 | assert(s); | |
423 | assert(s->sysv_path); | |
424 | ||
425 | if (!(c = exec_command_new(s->sysv_path, "start"))) | |
426 | return -ENOMEM; | |
427 | exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c); | |
428 | ||
429 | if (!(c = exec_command_new(s->sysv_path, "stop"))) | |
430 | return -ENOMEM; | |
431 | exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c); | |
432 | ||
433 | if (!(c = exec_command_new(s->sysv_path, "reload"))) | |
434 | return -ENOMEM; | |
435 | exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c); | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
e537352b | 440 | static int service_load_sysv_path(Service *s, const char *path) { |
2c4104f0 LP |
441 | FILE *f; |
442 | Unit *u; | |
443 | unsigned line = 0; | |
444 | int r; | |
445 | enum { | |
446 | NORMAL, | |
447 | DESCRIPTION, | |
448 | LSB, | |
449 | LSB_DESCRIPTION | |
450 | } state = NORMAL; | |
0b5d26f9 | 451 | char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description; |
23a177ef LP |
452 | |
453 | assert(s); | |
454 | assert(path); | |
2c4104f0 LP |
455 | |
456 | u = UNIT(s); | |
457 | ||
458 | if (!(f = fopen(path, "re"))) { | |
459 | r = errno == ENOENT ? 0 : -errno; | |
460 | goto finish; | |
461 | } | |
462 | ||
2c4104f0 LP |
463 | free(s->sysv_path); |
464 | if (!(s->sysv_path = strdup(path))) { | |
465 | r = -ENOMEM; | |
466 | goto finish; | |
467 | } | |
468 | ||
469 | while (!feof(f)) { | |
470 | char l[LINE_MAX], *t; | |
471 | ||
472 | if (!fgets(l, sizeof(l), f)) { | |
473 | if (feof(f)) | |
474 | break; | |
475 | ||
476 | r = -errno; | |
477 | log_error("Failed to read configuration file '%s': %s", path, strerror(-r)); | |
478 | goto finish; | |
479 | } | |
480 | ||
481 | line++; | |
482 | ||
483 | t = strstrip(l); | |
484 | if (*t != '#') | |
485 | continue; | |
486 | ||
487 | if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) { | |
488 | state = LSB; | |
23a177ef | 489 | s->sysv_has_lsb = true; |
2c4104f0 LP |
490 | continue; |
491 | } | |
492 | ||
493 | if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) { | |
494 | state = NORMAL; | |
495 | continue; | |
496 | } | |
497 | ||
498 | t++; | |
499 | t += strspn(t, WHITESPACE); | |
500 | ||
501 | if (state == NORMAL) { | |
502 | ||
503 | /* Try to parse Red Hat style chkconfig headers */ | |
504 | ||
c2b35af6 | 505 | if (startswith_no_case(t, "chkconfig:")) { |
2c4104f0 | 506 | int start_priority; |
8309400a | 507 | char runlevels[16], *k; |
2c4104f0 LP |
508 | |
509 | state = NORMAL; | |
510 | ||
8309400a LP |
511 | if (sscanf(t+10, "%15s %i %*i", |
512 | runlevels, | |
513 | &start_priority) != 2) { | |
2c4104f0 LP |
514 | |
515 | log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line); | |
516 | continue; | |
517 | } | |
518 | ||
fbe9f3a9 LP |
519 | /* A start priority gathered from the |
520 | * symlink farms is preferred over the | |
521 | * data from the LSB header. */ | |
8309400a | 522 | if (start_priority < 0 || start_priority > 99) |
2c4104f0 | 523 | log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line); |
db06e3b6 | 524 | else if (s->sysv_start_priority < 0) |
8309400a LP |
525 | s->sysv_start_priority = start_priority; |
526 | ||
527 | char_array_0(runlevels); | |
528 | k = delete_chars(runlevels, WHITESPACE "-"); | |
529 | ||
530 | if (k[0]) { | |
531 | char *d; | |
532 | ||
533 | if (!(d = strdup(k))) { | |
534 | r = -ENOMEM; | |
535 | goto finish; | |
536 | } | |
537 | ||
538 | free(s->sysv_runlevels); | |
539 | s->sysv_runlevels = d; | |
2c4104f0 LP |
540 | } |
541 | ||
0b5d26f9 | 542 | } else if (startswith_no_case(t, "description:")) { |
2c4104f0 LP |
543 | |
544 | size_t k = strlen(t); | |
545 | char *d; | |
0b5d26f9 | 546 | const char *j; |
2c4104f0 LP |
547 | |
548 | if (t[k-1] == '\\') { | |
549 | state = DESCRIPTION; | |
550 | t[k-1] = 0; | |
551 | } | |
552 | ||
0b5d26f9 LP |
553 | if ((j = strstrip(t+12)) && *j) { |
554 | if (!(d = strdup(j))) { | |
555 | r = -ENOMEM; | |
556 | goto finish; | |
557 | } | |
558 | } else | |
559 | d = NULL; | |
2c4104f0 | 560 | |
0b5d26f9 LP |
561 | free(chkconfig_description); |
562 | chkconfig_description = d; | |
2c4104f0 | 563 | |
c2b35af6 | 564 | } else if (startswith_no_case(t, "pidfile:")) { |
2c4104f0 LP |
565 | |
566 | char *fn; | |
567 | ||
568 | state = NORMAL; | |
569 | ||
570 | fn = strstrip(t+8); | |
571 | if (!path_is_absolute(fn)) { | |
572 | log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line); | |
573 | continue; | |
574 | } | |
575 | ||
576 | if (!(fn = strdup(fn))) { | |
577 | r = -ENOMEM; | |
578 | goto finish; | |
579 | } | |
580 | ||
581 | free(s->pid_file); | |
582 | s->pid_file = fn; | |
583 | } | |
584 | ||
585 | } else if (state == DESCRIPTION) { | |
586 | ||
587 | /* Try to parse Red Hat style description | |
588 | * continuation */ | |
589 | ||
590 | size_t k = strlen(t); | |
0b5d26f9 | 591 | char *j; |
2c4104f0 LP |
592 | |
593 | if (t[k-1] == '\\') | |
594 | t[k-1] = 0; | |
595 | else | |
596 | state = NORMAL; | |
597 | ||
0b5d26f9 LP |
598 | if ((j = strstrip(t)) && *j) { |
599 | char *d = NULL; | |
600 | ||
601 | if (chkconfig_description) | |
602 | asprintf(&d, "%s %s", chkconfig_description, j); | |
603 | else | |
604 | d = strdup(j); | |
2c4104f0 | 605 | |
0b5d26f9 LP |
606 | if (!d) { |
607 | r = -ENOMEM; | |
608 | goto finish; | |
609 | } | |
610 | ||
611 | free(chkconfig_description); | |
612 | chkconfig_description = d; | |
613 | } | |
2c4104f0 LP |
614 | |
615 | } else if (state == LSB || state == LSB_DESCRIPTION) { | |
616 | ||
c2b35af6 | 617 | if (startswith_no_case(t, "Provides:")) { |
2c4104f0 LP |
618 | char *i, *w; |
619 | size_t z; | |
620 | ||
621 | state = LSB; | |
622 | ||
f60f22df | 623 | FOREACH_WORD_QUOTED(w, z, t+9, i) { |
2c4104f0 LP |
624 | char *n, *m; |
625 | ||
626 | if (!(n = strndup(w, z))) { | |
627 | r = -ENOMEM; | |
628 | goto finish; | |
629 | } | |
630 | ||
ee95669f | 631 | r = sysv_translate_facility(n, file_name_from_path(path), &m); |
2c4104f0 LP |
632 | free(n); |
633 | ||
634 | if (r < 0) | |
635 | goto finish; | |
636 | ||
637 | if (r == 0) | |
638 | continue; | |
639 | ||
bd77d0fc LP |
640 | if (unit_name_to_type(m) == UNIT_SERVICE) |
641 | r = unit_add_name(u, m); | |
c68364b7 LP |
642 | else { |
643 | r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true); | |
644 | ||
645 | if (s->sysv_enabled) { | |
646 | int k; | |
647 | ||
648 | if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0) | |
649 | r = k; | |
650 | } | |
651 | } | |
bd77d0fc | 652 | |
2c4104f0 | 653 | if (r < 0) |
43a37549 | 654 | log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r)); |
42a097a2 LP |
655 | |
656 | free(m); | |
2c4104f0 LP |
657 | } |
658 | ||
c2b35af6 LP |
659 | } else if (startswith_no_case(t, "Required-Start:") || |
660 | startswith_no_case(t, "Should-Start:") || | |
661 | startswith_no_case(t, "X-Start-Before:") || | |
662 | startswith_no_case(t, "X-Start-After:")) { | |
2c4104f0 LP |
663 | char *i, *w; |
664 | size_t z; | |
665 | ||
666 | state = LSB; | |
667 | ||
f60f22df | 668 | FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) { |
2c4104f0 LP |
669 | char *n, *m; |
670 | ||
671 | if (!(n = strndup(w, z))) { | |
672 | r = -ENOMEM; | |
673 | goto finish; | |
674 | } | |
675 | ||
ee95669f | 676 | r = sysv_translate_facility(n, file_name_from_path(path), &m); |
2c4104f0 LP |
677 | free(n); |
678 | ||
679 | if (r < 0) | |
680 | goto finish; | |
681 | ||
682 | if (r == 0) | |
683 | continue; | |
684 | ||
c2b35af6 | 685 | r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true); |
2c4104f0 LP |
686 | |
687 | if (r < 0) | |
43a37549 | 688 | log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r)); |
42a097a2 LP |
689 | |
690 | free(m); | |
2c4104f0 | 691 | } |
c2b35af6 | 692 | } else if (startswith_no_case(t, "Default-Start:")) { |
8309400a LP |
693 | char *k, *d; |
694 | ||
695 | state = LSB; | |
696 | ||
697 | k = delete_chars(t+14, WHITESPACE "-"); | |
698 | ||
699 | if (k[0] != 0) { | |
700 | if (!(d = strdup(k))) { | |
701 | r = -ENOMEM; | |
702 | goto finish; | |
703 | } | |
704 | ||
705 | free(s->sysv_runlevels); | |
706 | s->sysv_runlevels = d; | |
707 | } | |
2c4104f0 | 708 | |
0b5d26f9 LP |
709 | } else if (startswith_no_case(t, "Description:")) { |
710 | char *d, *j; | |
ed4c1cc6 | 711 | |
2c4104f0 LP |
712 | state = LSB_DESCRIPTION; |
713 | ||
0b5d26f9 LP |
714 | if ((j = strstrip(t+12)) && *j) { |
715 | if (!(d = strdup(j))) { | |
716 | r = -ENOMEM; | |
717 | goto finish; | |
718 | } | |
719 | } else | |
720 | d = NULL; | |
2c4104f0 | 721 | |
0b5d26f9 LP |
722 | free(long_description); |
723 | long_description = d; | |
2c4104f0 | 724 | |
ed4c1cc6 | 725 | } else if (startswith_no_case(t, "Short-Description:")) { |
0b5d26f9 | 726 | char *d, *j; |
2c4104f0 | 727 | |
2c4104f0 LP |
728 | state = LSB; |
729 | ||
0b5d26f9 LP |
730 | if ((j = strstrip(t+18)) && *j) { |
731 | if (!(d = strdup(j))) { | |
732 | r = -ENOMEM; | |
733 | goto finish; | |
734 | } | |
735 | } else | |
736 | d = NULL; | |
2c4104f0 | 737 | |
0b5d26f9 LP |
738 | free(short_description); |
739 | short_description = d; | |
2c4104f0 | 740 | |
723c83fd LP |
741 | } else if (startswith_no_case(t, "X-Interactive:")) { |
742 | int b; | |
743 | ||
744 | if ((b = parse_boolean(strstrip(t+14))) < 0) { | |
745 | log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line); | |
746 | continue; | |
747 | } | |
748 | ||
749 | if (b) | |
750 | s->exec_context.std_input = EXEC_INPUT_TTY; | |
751 | else | |
752 | s->exec_context.std_input = EXEC_INPUT_NULL; | |
753 | ||
2c4104f0 LP |
754 | } else if (state == LSB_DESCRIPTION) { |
755 | ||
756 | if (startswith(l, "#\t") || startswith(l, "# ")) { | |
0b5d26f9 | 757 | char *j; |
2c4104f0 | 758 | |
0b5d26f9 LP |
759 | if ((j = strstrip(t)) && *j) { |
760 | char *d = NULL; | |
761 | ||
762 | if (long_description) | |
763 | asprintf(&d, "%s %s", long_description, t); | |
764 | else | |
765 | d = strdup(j); | |
766 | ||
767 | if (!d) { | |
768 | r = -ENOMEM; | |
769 | goto finish; | |
770 | } | |
771 | ||
772 | free(long_description); | |
773 | long_description = d; | |
2c4104f0 LP |
774 | } |
775 | ||
2c4104f0 LP |
776 | } else |
777 | state = LSB; | |
778 | } | |
779 | } | |
780 | } | |
781 | ||
2c4104f0 LP |
782 | if ((r = sysv_exec_commands(s)) < 0) |
783 | goto finish; | |
784 | ||
a40eb732 | 785 | if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) { |
0bc824be LP |
786 | /* If there a runlevels configured for this service |
787 | * but none of the standard ones, then we assume this | |
788 | * is some special kind of service (which might be | |
789 | * needed for early boot) and don't create any links | |
790 | * to it. */ | |
791 | ||
a40eb732 | 792 | s->meta.default_dependencies = false; |
09cd1ab1 | 793 | |
09cd1ab1 LP |
794 | /* Don't timeout special services during boot (like fsck) */ |
795 | s->timeout_usec = 0; | |
f34277d9 LP |
796 | } else |
797 | s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC; | |
0fd030be | 798 | |
80876c20 | 799 | /* Special setting for all SysV services */ |
1f48cf56 | 800 | s->type = SERVICE_FORKING; |
02ee865a | 801 | s->remain_after_exit = true; |
525ee6f4 | 802 | s->restart = SERVICE_RESTART_NO; |
8a2b3c09 LP |
803 | s->exec_context.std_output = |
804 | (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY) | |
805 | ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL; | |
2e22afe9 | 806 | s->exec_context.kill_mode = KILL_PROCESS_GROUP; |
80876c20 | 807 | |
0b5d26f9 LP |
808 | /* We use the long description only if |
809 | * no short description is set. */ | |
810 | ||
811 | if (short_description) | |
812 | description = short_description; | |
813 | else if (chkconfig_description) | |
814 | description = chkconfig_description; | |
815 | else if (long_description) | |
816 | description = long_description; | |
817 | else | |
818 | description = NULL; | |
819 | ||
820 | if (description) { | |
821 | char *d; | |
822 | ||
823 | if (!(d = strappend("LSB: ", description))) { | |
824 | r = -ENOMEM; | |
825 | goto finish; | |
826 | } | |
827 | ||
828 | u->meta.description = d; | |
829 | } | |
830 | ||
e537352b | 831 | u->meta.load_state = UNIT_LOADED; |
23a177ef | 832 | r = 0; |
2c4104f0 LP |
833 | |
834 | finish: | |
835 | ||
836 | if (f) | |
837 | fclose(f); | |
838 | ||
0b5d26f9 LP |
839 | free(short_description); |
840 | free(long_description); | |
841 | free(chkconfig_description); | |
842 | ||
2c4104f0 LP |
843 | return r; |
844 | } | |
845 | ||
e537352b | 846 | static int service_load_sysv_name(Service *s, const char *name) { |
2c4104f0 LP |
847 | char **p; |
848 | ||
849 | assert(s); | |
850 | assert(name); | |
851 | ||
d017c6ca LP |
852 | /* For SysV services we strip the boot. or .sh |
853 | * prefixes/suffixes. */ | |
854 | if (startswith(name, "boot.") || | |
855 | endswith(name, ".sh.service")) | |
856 | return -ENOENT; | |
857 | ||
4cd1fbcc | 858 | STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) { |
2c4104f0 LP |
859 | char *path; |
860 | int r; | |
861 | ||
862 | if (asprintf(&path, "%s/%s", *p, name) < 0) | |
863 | return -ENOMEM; | |
864 | ||
865 | assert(endswith(path, ".service")); | |
866 | path[strlen(path)-8] = 0; | |
867 | ||
e537352b | 868 | r = service_load_sysv_path(s, path); |
fbe9f3a9 | 869 | |
4cd1fbcc | 870 | if (r >= 0 && s->meta.load_state == UNIT_STUB) { |
d017c6ca | 871 | /* Try Debian style xxx.sh source'able init scripts */ |
fbe9f3a9 LP |
872 | strcat(path, ".sh"); |
873 | r = service_load_sysv_path(s, path); | |
874 | } | |
875 | ||
2c4104f0 LP |
876 | free(path); |
877 | ||
4cd1fbcc | 878 | if (r >= 0 && s->meta.load_state == UNIT_STUB) { |
cfe243e3 | 879 | /* Try SUSE style boot.xxx init scripts */ |
fbe9f3a9 LP |
880 | |
881 | if (asprintf(&path, "%s/boot.%s", *p, name) < 0) | |
882 | return -ENOMEM; | |
883 | ||
884 | path[strlen(path)-8] = 0; | |
885 | r = service_load_sysv_path(s, path); | |
886 | free(path); | |
887 | } | |
888 | ||
23a177ef | 889 | if (r < 0) |
2c4104f0 | 890 | return r; |
23a177ef | 891 | |
4cd1fbcc | 892 | if ((s->meta.load_state != UNIT_STUB)) |
23a177ef | 893 | break; |
2c4104f0 LP |
894 | } |
895 | ||
896 | return 0; | |
897 | } | |
898 | ||
e537352b | 899 | static int service_load_sysv(Service *s) { |
2c4104f0 LP |
900 | const char *t; |
901 | Iterator i; | |
902 | int r; | |
903 | ||
5cb5a6ff LP |
904 | assert(s); |
905 | ||
906 | /* Load service data from SysV init scripts, preferably with | |
907 | * LSB headers ... */ | |
908 | ||
4cd1fbcc | 909 | if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path)) |
2c4104f0 LP |
910 | return 0; |
911 | ||
4cd1fbcc | 912 | if ((t = s->meta.id)) |
e537352b | 913 | if ((r = service_load_sysv_name(s, t)) < 0) |
2c4104f0 LP |
914 | return r; |
915 | ||
4cd1fbcc LP |
916 | if (s->meta.load_state == UNIT_STUB) |
917 | SET_FOREACH(t, s->meta.names, i) { | |
918 | if (t == s->meta.id) | |
e537352b LP |
919 | continue; |
920 | ||
e364ad06 | 921 | if ((r = service_load_sysv_name(s, t)) < 0) |
23a177ef LP |
922 | return r; |
923 | ||
4cd1fbcc | 924 | if (s->meta.load_state != UNIT_STUB) |
23a177ef LP |
925 | break; |
926 | } | |
2c4104f0 LP |
927 | |
928 | return 0; | |
5cb5a6ff | 929 | } |
07459bb6 | 930 | #endif |
5cb5a6ff | 931 | |
9fff8a1f LP |
932 | static int fsck_fix_order(Service *s) { |
933 | Meta *other; | |
934 | int r; | |
935 | ||
936 | assert(s); | |
937 | ||
938 | if (s->fsck_passno <= 0) | |
939 | return 0; | |
940 | ||
941 | /* For each pair of services where both have an fsck priority | |
942 | * we order things based on it. */ | |
943 | ||
944 | LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) { | |
945 | Service *t; | |
946 | UnitDependency d; | |
947 | ||
948 | t = (Service*) other; | |
949 | ||
950 | if (s == t) | |
951 | continue; | |
952 | ||
953 | if (t->meta.load_state != UNIT_LOADED) | |
954 | continue; | |
955 | ||
956 | if (t->fsck_passno <= 0) | |
957 | continue; | |
958 | ||
959 | if (t->fsck_passno < s->fsck_passno) | |
960 | d = UNIT_AFTER; | |
961 | else if (t->fsck_passno > s->fsck_passno) | |
962 | d = UNIT_BEFORE; | |
963 | else | |
964 | continue; | |
965 | ||
966 | if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0) | |
967 | return r; | |
968 | } | |
969 | ||
970 | return 0; | |
971 | } | |
972 | ||
243b1432 LP |
973 | static int service_verify(Service *s) { |
974 | assert(s); | |
975 | ||
4cd1fbcc | 976 | if (s->meta.load_state != UNIT_LOADED) |
243b1432 LP |
977 | return 0; |
978 | ||
979 | if (!s->exec_command[SERVICE_EXEC_START]) { | |
4cd1fbcc | 980 | log_error("%s lacks ExecStart setting. Refusing.", s->meta.id); |
243b1432 LP |
981 | return -EINVAL; |
982 | } | |
983 | ||
34e9ba66 LP |
984 | if (s->type != SERVICE_ONESHOT && |
985 | s->exec_command[SERVICE_EXEC_START]->command_next) { | |
986 | log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id); | |
6cf6bbc2 LP |
987 | return -EINVAL; |
988 | } | |
989 | ||
05e343b7 | 990 | if (s->type == SERVICE_DBUS && !s->bus_name) { |
4d0e5dbd LP |
991 | log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id); |
992 | return -EINVAL; | |
993 | } | |
994 | ||
2e22afe9 | 995 | if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) { |
4d0e5dbd | 996 | log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id); |
05e343b7 LP |
997 | return -EINVAL; |
998 | } | |
999 | ||
243b1432 LP |
1000 | return 0; |
1001 | } | |
1002 | ||
a40eb732 LP |
1003 | static int service_add_default_dependencies(Service *s) { |
1004 | int r; | |
1005 | ||
1006 | assert(s); | |
1007 | ||
1008 | /* Add a number of automatic dependencies useful for the | |
1009 | * majority of services. */ | |
1010 | ||
1011 | /* First, pull in base system */ | |
1012 | if (s->meta.manager->running_as == MANAGER_SYSTEM) { | |
1013 | ||
1014 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0) | |
1015 | return r; | |
1016 | ||
1017 | } else if (s->meta.manager->running_as == MANAGER_SESSION) { | |
1018 | ||
1019 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0) | |
1020 | return r; | |
1021 | } | |
1022 | ||
1023 | /* Second, activate normal shutdown */ | |
69dd2852 | 1024 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); |
a40eb732 LP |
1025 | } |
1026 | ||
e537352b LP |
1027 | static int service_load(Unit *u) { |
1028 | int r; | |
1029 | Service *s = SERVICE(u); | |
1030 | ||
1031 | assert(s); | |
1e2e8133 | 1032 | |
5cb5a6ff | 1033 | /* Load a .service file */ |
e537352b | 1034 | if ((r = unit_load_fragment(u)) < 0) |
5cb5a6ff LP |
1035 | return r; |
1036 | ||
07459bb6 | 1037 | #ifdef HAVE_SYSV_COMPAT |
bd77d0fc | 1038 | /* Load a classic init script as a fallback, if we couldn't find anything */ |
e537352b LP |
1039 | if (u->meta.load_state == UNIT_STUB) |
1040 | if ((r = service_load_sysv(s)) < 0) | |
23a177ef | 1041 | return r; |
07459bb6 | 1042 | #endif |
d46de8a1 | 1043 | |
23a177ef | 1044 | /* Still nothing found? Then let's give up */ |
e537352b | 1045 | if (u->meta.load_state == UNIT_STUB) |
23a177ef | 1046 | return -ENOENT; |
034c6ed7 | 1047 | |
23a177ef LP |
1048 | /* We were able to load something, then let's add in the |
1049 | * dropin directories. */ | |
1050 | if ((r = unit_load_dropin(unit_follow_merge(u))) < 0) | |
8e274523 | 1051 | return r; |
23a177ef LP |
1052 | |
1053 | /* This is a new unit? Then let's add in some extras */ | |
e537352b | 1054 | if (u->meta.load_state == UNIT_LOADED) { |
23a177ef LP |
1055 | if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) |
1056 | return r; | |
1057 | ||
1058 | if ((r = unit_add_default_cgroup(u)) < 0) | |
1059 | return r; | |
1060 | ||
07459bb6 | 1061 | #ifdef HAVE_SYSV_COMPAT |
56d748b4 | 1062 | if ((r = sysv_fix_order(s)) < 0) |
23a177ef | 1063 | return r; |
07459bb6 | 1064 | #endif |
05e343b7 | 1065 | |
9fff8a1f LP |
1066 | if ((r = fsck_fix_order(s)) < 0) |
1067 | return r; | |
1068 | ||
ee0dd802 | 1069 | if (s->bus_name) |
05e343b7 | 1070 | if ((r = unit_watch_bus_name(u, s->bus_name)) < 0) |
a40eb732 | 1071 | return r; |
c952c6ec LP |
1072 | |
1073 | if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE) | |
1074 | s->notify_access = NOTIFY_MAIN; | |
a40eb732 LP |
1075 | |
1076 | if (s->type == SERVICE_DBUS || s->bus_name) | |
1077 | if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0) | |
1078 | return r; | |
1079 | ||
1080 | if (s->meta.default_dependencies) | |
1081 | if ((r = service_add_default_dependencies(s)) < 0) | |
1082 | return r; | |
8e274523 LP |
1083 | } |
1084 | ||
243b1432 | 1085 | return service_verify(s); |
034c6ed7 LP |
1086 | } |
1087 | ||
87f0e418 | 1088 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 1089 | |
5cb5a6ff | 1090 | ServiceExecCommand c; |
87f0e418 | 1091 | Service *s = SERVICE(u); |
47be870b LP |
1092 | const char *prefix2; |
1093 | char *p2; | |
5cb5a6ff LP |
1094 | |
1095 | assert(s); | |
1096 | ||
47be870b LP |
1097 | p2 = strappend(prefix, "\t"); |
1098 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e | 1099 | |
5cb5a6ff | 1100 | fprintf(f, |
81a2b7ce LP |
1101 | "%sService State: %s\n" |
1102 | "%sPermissionsStartOnly: %s\n" | |
8e274523 | 1103 | "%sRootDirectoryStartOnly: %s\n" |
02ee865a | 1104 | "%sRemainAfterExit: %s\n" |
c952c6ec | 1105 | "%sType: %s\n" |
2cf3143a | 1106 | "%sRestart: %s\n" |
c952c6ec | 1107 | "%sNotifyAccess: %s\n", |
81a2b7ce LP |
1108 | prefix, service_state_to_string(s->state), |
1109 | prefix, yes_no(s->permissions_start_only), | |
8e274523 | 1110 | prefix, yes_no(s->root_directory_start_only), |
02ee865a | 1111 | prefix, yes_no(s->remain_after_exit), |
c952c6ec | 1112 | prefix, service_type_to_string(s->type), |
2cf3143a | 1113 | prefix, service_restart_to_string(s->restart), |
c952c6ec | 1114 | prefix, notify_access_to_string(s->notify_access)); |
5cb5a6ff | 1115 | |
70123e68 LP |
1116 | if (s->control_pid > 0) |
1117 | fprintf(f, | |
bb00e604 LP |
1118 | "%sControl PID: %lu\n", |
1119 | prefix, (unsigned long) s->control_pid); | |
70123e68 LP |
1120 | |
1121 | if (s->main_pid > 0) | |
1122 | fprintf(f, | |
bb00e604 LP |
1123 | "%sMain PID: %lu\n", |
1124 | prefix, (unsigned long) s->main_pid); | |
70123e68 | 1125 | |
034c6ed7 LP |
1126 | if (s->pid_file) |
1127 | fprintf(f, | |
1128 | "%sPIDFile: %s\n", | |
1129 | prefix, s->pid_file); | |
1130 | ||
05e343b7 LP |
1131 | if (s->bus_name) |
1132 | fprintf(f, | |
1133 | "%sBusName: %s\n" | |
1134 | "%sBus Name Good: %s\n", | |
1135 | prefix, s->bus_name, | |
1136 | prefix, yes_no(s->bus_name_good)); | |
1137 | ||
5cb5a6ff LP |
1138 | exec_context_dump(&s->exec_context, f, prefix); |
1139 | ||
e537352b | 1140 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
5cb5a6ff | 1141 | |
44d8db9e LP |
1142 | if (!s->exec_command[c]) |
1143 | continue; | |
1144 | ||
40d50879 | 1145 | fprintf(f, "%s-> %s:\n", |
94f04347 | 1146 | prefix, service_exec_command_to_string(c)); |
44d8db9e LP |
1147 | |
1148 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 1149 | } |
44d8db9e | 1150 | |
07459bb6 | 1151 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 LP |
1152 | if (s->sysv_path) |
1153 | fprintf(f, | |
23a177ef | 1154 | "%sSysV Init Script Path: %s\n" |
9fff8a1f LP |
1155 | "%sSysV Init Script has LSB Header: %s\n" |
1156 | "%sSysVEnabled: %s\n", | |
23a177ef | 1157 | prefix, s->sysv_path, |
9fff8a1f LP |
1158 | prefix, yes_no(s->sysv_has_lsb), |
1159 | prefix, yes_no(s->sysv_enabled)); | |
2c4104f0 LP |
1160 | |
1161 | if (s->sysv_start_priority >= 0) | |
1162 | fprintf(f, | |
9fff8a1f LP |
1163 | "%sSysVStartPriority: %i\n", |
1164 | prefix, s->sysv_start_priority); | |
2c4104f0 | 1165 | |
8309400a LP |
1166 | if (s->sysv_runlevels) |
1167 | fprintf(f, "%sSysVRunLevels: %s\n", | |
1168 | prefix, s->sysv_runlevels); | |
07459bb6 | 1169 | #endif |
23a177ef | 1170 | |
9fff8a1f LP |
1171 | if (s->fsck_passno > 0) |
1172 | fprintf(f, | |
1173 | "%sFsckPassNo: %i\n", | |
1174 | prefix, s->fsck_passno); | |
1175 | ||
8c47c732 LP |
1176 | if (s->status_text) |
1177 | fprintf(f, "%sStatus Text: %s\n", | |
1178 | prefix, s->status_text); | |
1179 | ||
47be870b | 1180 | free(p2); |
5cb5a6ff LP |
1181 | } |
1182 | ||
034c6ed7 LP |
1183 | static int service_load_pid_file(Service *s) { |
1184 | char *k; | |
034c6ed7 | 1185 | int r; |
5925dd3c | 1186 | pid_t pid; |
034c6ed7 LP |
1187 | |
1188 | assert(s); | |
1189 | ||
1190 | if (s->main_pid_known) | |
1191 | return 0; | |
1192 | ||
5e94833f LP |
1193 | assert(s->main_pid <= 0); |
1194 | ||
034c6ed7 LP |
1195 | if (!s->pid_file) |
1196 | return -ENOENT; | |
1197 | ||
1198 | if ((r = read_one_line_file(s->pid_file, &k)) < 0) | |
1199 | return r; | |
1200 | ||
5925dd3c LP |
1201 | r = parse_pid(k, &pid); |
1202 | free(k); | |
034c6ed7 | 1203 | |
5925dd3c LP |
1204 | if (r < 0) |
1205 | return r; | |
406eaf93 | 1206 | |
5925dd3c LP |
1207 | if (kill(pid, 0) < 0 && errno != EPERM) { |
1208 | log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.", | |
1209 | (unsigned long) pid, s->pid_file); | |
b8c597d5 LP |
1210 | return -ESRCH; |
1211 | } | |
1212 | ||
5925dd3c | 1213 | if ((r = service_set_main_pid(s, pid)) < 0) |
16f6025e LP |
1214 | return r; |
1215 | ||
5925dd3c LP |
1216 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
1217 | /* FIXME: we need to do something here */ | |
1218 | return r; | |
034c6ed7 LP |
1219 | |
1220 | return 0; | |
1221 | } | |
1222 | ||
3e33402a LP |
1223 | static int service_get_sockets(Service *s, Set **_set) { |
1224 | Set *set; | |
ceee3d82 LP |
1225 | Iterator i; |
1226 | char *t; | |
3e33402a | 1227 | int r; |
ceee3d82 LP |
1228 | |
1229 | assert(s); | |
3e33402a LP |
1230 | assert(_set); |
1231 | ||
6cf6bbc2 LP |
1232 | if (s->socket_fd >= 0) |
1233 | return 0; | |
1234 | ||
f976f3f6 LP |
1235 | if (!set_isempty(s->configured_sockets)) |
1236 | return 0; | |
1237 | ||
3e33402a LP |
1238 | /* Collects all Socket objects that belong to this |
1239 | * service. Note that a service might have multiple sockets | |
1240 | * via multiple names. */ | |
1241 | ||
1242 | if (!(set = set_new(NULL, NULL))) | |
1243 | return -ENOMEM; | |
ceee3d82 | 1244 | |
4cd1fbcc | 1245 | SET_FOREACH(t, s->meta.names, i) { |
ceee3d82 LP |
1246 | char *k; |
1247 | Unit *p; | |
1248 | ||
1249 | /* Look for all socket objects that go by any of our | |
1250 | * units and collect their fds */ | |
1251 | ||
3e33402a LP |
1252 | if (!(k = unit_name_change_suffix(t, ".socket"))) { |
1253 | r = -ENOMEM; | |
1254 | goto fail; | |
1255 | } | |
ceee3d82 | 1256 | |
4cd1fbcc | 1257 | p = manager_get_unit(s->meta.manager, k); |
ceee3d82 LP |
1258 | free(k); |
1259 | ||
8d567588 LP |
1260 | if (!p) |
1261 | continue; | |
ceee3d82 | 1262 | |
3e33402a LP |
1263 | if ((r = set_put(set, p)) < 0) |
1264 | goto fail; | |
ceee3d82 LP |
1265 | } |
1266 | ||
3e33402a LP |
1267 | *_set = set; |
1268 | return 0; | |
1269 | ||
1270 | fail: | |
1271 | set_free(set); | |
1272 | return r; | |
1273 | } | |
1274 | ||
e537352b | 1275 | static int service_notify_sockets_dead(Service *s) { |
3e33402a | 1276 | Iterator i; |
f976f3f6 | 1277 | Set *set, *free_set = NULL; |
47be870b | 1278 | Socket *sock; |
3e33402a LP |
1279 | int r; |
1280 | ||
1281 | assert(s); | |
1282 | ||
f976f3f6 LP |
1283 | /* Notifies all our sockets when we die */ |
1284 | ||
6cf6bbc2 LP |
1285 | if (s->socket_fd >= 0) |
1286 | return 0; | |
1287 | ||
f976f3f6 LP |
1288 | if (!set_isempty(s->configured_sockets)) |
1289 | set = s->configured_sockets; | |
1290 | else { | |
1291 | if ((r = service_get_sockets(s, &free_set)) < 0) | |
1292 | return r; | |
1293 | ||
1294 | set = free_set; | |
1295 | } | |
3e33402a | 1296 | |
47be870b LP |
1297 | SET_FOREACH(sock, set, i) |
1298 | socket_notify_service_dead(sock); | |
3e33402a | 1299 | |
f976f3f6 | 1300 | set_free(free_set); |
3e33402a | 1301 | |
ceee3d82 LP |
1302 | return 0; |
1303 | } | |
1304 | ||
034c6ed7 LP |
1305 | static void service_set_state(Service *s, ServiceState state) { |
1306 | ServiceState old_state; | |
5cb5a6ff LP |
1307 | assert(s); |
1308 | ||
034c6ed7 | 1309 | old_state = s->state; |
5cb5a6ff | 1310 | s->state = state; |
034c6ed7 LP |
1311 | |
1312 | if (state != SERVICE_START_PRE && | |
1313 | state != SERVICE_START && | |
1314 | state != SERVICE_START_POST && | |
1315 | state != SERVICE_RELOAD && | |
1316 | state != SERVICE_STOP && | |
1317 | state != SERVICE_STOP_SIGTERM && | |
1318 | state != SERVICE_STOP_SIGKILL && | |
1319 | state != SERVICE_STOP_POST && | |
1320 | state != SERVICE_FINAL_SIGTERM && | |
1321 | state != SERVICE_FINAL_SIGKILL && | |
1322 | state != SERVICE_AUTO_RESTART) | |
acbb0225 | 1323 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 | 1324 | |
7d55e835 LP |
1325 | if (state != SERVICE_START && |
1326 | state != SERVICE_START_POST && | |
034c6ed7 LP |
1327 | state != SERVICE_RUNNING && |
1328 | state != SERVICE_RELOAD && | |
1329 | state != SERVICE_STOP && | |
1330 | state != SERVICE_STOP_SIGTERM && | |
1331 | state != SERVICE_STOP_SIGKILL) | |
5e94833f | 1332 | service_unwatch_main_pid(s); |
034c6ed7 LP |
1333 | |
1334 | if (state != SERVICE_START_PRE && | |
1335 | state != SERVICE_START && | |
1336 | state != SERVICE_START_POST && | |
1337 | state != SERVICE_RELOAD && | |
1338 | state != SERVICE_STOP && | |
1339 | state != SERVICE_STOP_SIGTERM && | |
1340 | state != SERVICE_STOP_SIGKILL && | |
1341 | state != SERVICE_STOP_POST && | |
1342 | state != SERVICE_FINAL_SIGTERM && | |
e537352b | 1343 | state != SERVICE_FINAL_SIGKILL) { |
5e94833f | 1344 | service_unwatch_control_pid(s); |
034c6ed7 | 1345 | s->control_command = NULL; |
a16e1123 | 1346 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
e537352b | 1347 | } |
034c6ed7 | 1348 | |
ceee3d82 LP |
1349 | if (state == SERVICE_DEAD || |
1350 | state == SERVICE_STOP || | |
1351 | state == SERVICE_STOP_SIGTERM || | |
1352 | state == SERVICE_STOP_SIGKILL || | |
1353 | state == SERVICE_STOP_POST || | |
1354 | state == SERVICE_FINAL_SIGTERM || | |
1355 | state == SERVICE_FINAL_SIGKILL || | |
fdf20a31 | 1356 | state == SERVICE_FAILED || |
ceee3d82 | 1357 | state == SERVICE_AUTO_RESTART) |
e537352b | 1358 | service_notify_sockets_dead(s); |
ceee3d82 | 1359 | |
4f2d528d LP |
1360 | if (state != SERVICE_START_PRE && |
1361 | state != SERVICE_START && | |
6cf6bbc2 LP |
1362 | state != SERVICE_START_POST && |
1363 | state != SERVICE_RUNNING && | |
1364 | state != SERVICE_RELOAD && | |
1365 | state != SERVICE_STOP && | |
1366 | state != SERVICE_STOP_SIGTERM && | |
1367 | state != SERVICE_STOP_SIGKILL && | |
1368 | state != SERVICE_STOP_POST && | |
1369 | state != SERVICE_FINAL_SIGTERM && | |
1370 | state != SERVICE_FINAL_SIGKILL && | |
4cd1fbcc | 1371 | !(state == SERVICE_DEAD && s->meta.job)) { |
4f2d528d | 1372 | service_close_socket_fd(s); |
6cf6bbc2 LP |
1373 | service_connection_unref(s); |
1374 | } | |
4f2d528d | 1375 | |
f6023656 LP |
1376 | /* For the inactive states unit_notify() will trim the cgroup, |
1377 | * but for exit we have to do that ourselves... */ | |
1378 | if (state == SERVICE_EXITED) | |
1379 | cgroup_bonding_trim_list(s->meta.cgroup_bondings, true); | |
1380 | ||
e537352b | 1381 | if (old_state != state) |
4cd1fbcc | 1382 | log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state)); |
acbb0225 LP |
1383 | |
1384 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); | |
034c6ed7 LP |
1385 | } |
1386 | ||
a16e1123 LP |
1387 | static int service_coldplug(Unit *u) { |
1388 | Service *s = SERVICE(u); | |
1389 | int r; | |
1390 | ||
1391 | assert(s); | |
1392 | assert(s->state == SERVICE_DEAD); | |
1393 | ||
1394 | if (s->deserialized_state != s->state) { | |
1395 | ||
1396 | if (s->deserialized_state == SERVICE_START_PRE || | |
1397 | s->deserialized_state == SERVICE_START || | |
1398 | s->deserialized_state == SERVICE_START_POST || | |
1399 | s->deserialized_state == SERVICE_RELOAD || | |
1400 | s->deserialized_state == SERVICE_STOP || | |
1401 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1402 | s->deserialized_state == SERVICE_STOP_SIGKILL || | |
1403 | s->deserialized_state == SERVICE_STOP_POST || | |
1404 | s->deserialized_state == SERVICE_FINAL_SIGTERM || | |
1405 | s->deserialized_state == SERVICE_FINAL_SIGKILL || | |
e558336f LP |
1406 | s->deserialized_state == SERVICE_AUTO_RESTART) { |
1407 | ||
1408 | if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) { | |
1409 | usec_t k; | |
1410 | ||
1411 | k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec; | |
1412 | ||
1413 | if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0) | |
1414 | return r; | |
1415 | } | |
1416 | } | |
a16e1123 LP |
1417 | |
1418 | if ((s->deserialized_state == SERVICE_START && | |
1419 | (s->type == SERVICE_FORKING || | |
8c47c732 | 1420 | s->type == SERVICE_DBUS || |
34e9ba66 | 1421 | s->type == SERVICE_ONESHOT || |
8c47c732 | 1422 | s->type == SERVICE_NOTIFY)) || |
a16e1123 LP |
1423 | s->deserialized_state == SERVICE_START_POST || |
1424 | s->deserialized_state == SERVICE_RUNNING || | |
1425 | s->deserialized_state == SERVICE_RELOAD || | |
1426 | s->deserialized_state == SERVICE_STOP || | |
1427 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1428 | s->deserialized_state == SERVICE_STOP_SIGKILL) | |
1429 | if (s->main_pid > 0) | |
1430 | if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0) | |
1431 | return r; | |
1432 | ||
1433 | if (s->deserialized_state == SERVICE_START_PRE || | |
1434 | s->deserialized_state == SERVICE_START || | |
1435 | s->deserialized_state == SERVICE_START_POST || | |
1436 | s->deserialized_state == SERVICE_RELOAD || | |
1437 | s->deserialized_state == SERVICE_STOP || | |
1438 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1439 | s->deserialized_state == SERVICE_STOP_SIGKILL || | |
1440 | s->deserialized_state == SERVICE_STOP_POST || | |
1441 | s->deserialized_state == SERVICE_FINAL_SIGTERM || | |
1442 | s->deserialized_state == SERVICE_FINAL_SIGKILL) | |
1443 | if (s->control_pid > 0) | |
1444 | if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0) | |
1445 | return r; | |
1446 | ||
1447 | service_set_state(s, s->deserialized_state); | |
1448 | } | |
1449 | ||
1450 | return 0; | |
1451 | } | |
1452 | ||
44d8db9e LP |
1453 | static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { |
1454 | Iterator i; | |
1455 | int r; | |
1456 | int *rfds = NULL; | |
1457 | unsigned rn_fds = 0; | |
f976f3f6 | 1458 | Set *set, *free_set = NULL; |
47be870b | 1459 | Socket *sock; |
44d8db9e LP |
1460 | |
1461 | assert(s); | |
1462 | assert(fds); | |
1463 | assert(n_fds); | |
1464 | ||
6cf6bbc2 LP |
1465 | if (s->socket_fd >= 0) |
1466 | return 0; | |
1467 | ||
f976f3f6 LP |
1468 | if (!set_isempty(s->configured_sockets)) |
1469 | set = s->configured_sockets; | |
1470 | else { | |
1471 | if ((r = service_get_sockets(s, &free_set)) < 0) | |
1472 | return r; | |
1473 | ||
1474 | set = free_set; | |
1475 | } | |
3e33402a | 1476 | |
47be870b | 1477 | SET_FOREACH(sock, set, i) { |
44d8db9e LP |
1478 | int *cfds; |
1479 | unsigned cn_fds; | |
1480 | ||
47be870b | 1481 | if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0) |
44d8db9e LP |
1482 | goto fail; |
1483 | ||
1484 | if (!cfds) | |
1485 | continue; | |
1486 | ||
1487 | if (!rfds) { | |
1488 | rfds = cfds; | |
1489 | rn_fds = cn_fds; | |
1490 | } else { | |
1491 | int *t; | |
1492 | ||
1493 | if (!(t = new(int, rn_fds+cn_fds))) { | |
1494 | free(cfds); | |
1495 | r = -ENOMEM; | |
1496 | goto fail; | |
1497 | } | |
1498 | ||
1499 | memcpy(t, rfds, rn_fds); | |
1500 | memcpy(t+rn_fds, cfds, cn_fds); | |
1501 | free(rfds); | |
1502 | free(cfds); | |
1503 | ||
1504 | rfds = t; | |
1505 | rn_fds = rn_fds+cn_fds; | |
1506 | } | |
1507 | } | |
1508 | ||
1509 | *fds = rfds; | |
1510 | *n_fds = rn_fds; | |
3e33402a | 1511 | |
f976f3f6 | 1512 | set_free(free_set); |
3e33402a | 1513 | |
44d8db9e LP |
1514 | return 0; |
1515 | ||
1516 | fail: | |
3e33402a | 1517 | set_free(set); |
44d8db9e | 1518 | free(rfds); |
3e33402a | 1519 | |
44d8db9e LP |
1520 | return r; |
1521 | } | |
1522 | ||
81a2b7ce LP |
1523 | static int service_spawn( |
1524 | Service *s, | |
1525 | ExecCommand *c, | |
1526 | bool timeout, | |
1527 | bool pass_fds, | |
1528 | bool apply_permissions, | |
1529 | bool apply_chroot, | |
1e3ad081 | 1530 | bool apply_tty_stdin, |
c952c6ec | 1531 | bool set_notify_socket, |
81a2b7ce LP |
1532 | pid_t *_pid) { |
1533 | ||
034c6ed7 LP |
1534 | pid_t pid; |
1535 | int r; | |
6cf6bbc2 | 1536 | int *fds = NULL, *fdsbuf = NULL; |
2105e76a LP |
1537 | unsigned n_fds = 0, n_env = 0; |
1538 | char **argv = NULL, **final_env = NULL, **our_env = NULL; | |
034c6ed7 LP |
1539 | |
1540 | assert(s); | |
1541 | assert(c); | |
1542 | assert(_pid); | |
1543 | ||
6cf6bbc2 LP |
1544 | if (pass_fds || |
1545 | s->exec_context.std_input == EXEC_INPUT_SOCKET || | |
1546 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1547 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1548 | ||
4f2d528d LP |
1549 | if (s->socket_fd >= 0) { |
1550 | fds = &s->socket_fd; | |
1551 | n_fds = 1; | |
6cf6bbc2 LP |
1552 | } else { |
1553 | if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0) | |
1554 | goto fail; | |
1555 | ||
1556 | fds = fdsbuf; | |
1557 | } | |
4f2d528d | 1558 | } |
44d8db9e | 1559 | |
e558336f | 1560 | if (timeout && s->timeout_usec) { |
acbb0225 | 1561 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
034c6ed7 LP |
1562 | goto fail; |
1563 | } else | |
acbb0225 | 1564 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 | 1565 | |
9e2f7c11 LP |
1566 | if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) { |
1567 | r = -ENOMEM; | |
1568 | goto fail; | |
1569 | } | |
1570 | ||
9865f3b4 | 1571 | if (!(our_env = new0(char*, 4))) { |
2105e76a LP |
1572 | r = -ENOMEM; |
1573 | goto fail; | |
1574 | } | |
c952c6ec | 1575 | |
2105e76a LP |
1576 | if (set_notify_socket) |
1577 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) { | |
c952c6ec LP |
1578 | r = -ENOMEM; |
1579 | goto fail; | |
1580 | } | |
1581 | ||
2105e76a LP |
1582 | if (s->main_pid > 0) |
1583 | if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) { | |
c952c6ec LP |
1584 | r = -ENOMEM; |
1585 | goto fail; | |
1586 | } | |
2105e76a | 1587 | |
0320828c | 1588 | #ifdef HAVE_SYSV_COMPAT |
9865f3b4 LP |
1589 | /* Make sure we set TERM=linux for SysV scripts, since some |
1590 | * require it to be set from the kernel */ | |
1591 | if (s->sysv_path && !strv_env_get(s->meta.manager->environment, "TERM")) | |
1592 | if (!(our_env[n_env++] = strdup("TERM=linux"))) { | |
1593 | r = -ENOMEM; | |
1594 | goto fail; | |
1595 | } | |
0320828c | 1596 | #endif |
9865f3b4 | 1597 | |
2105e76a LP |
1598 | if (!(final_env = strv_env_merge(2, |
1599 | s->meta.manager->environment, | |
1600 | our_env, | |
1601 | NULL))) { | |
1602 | r = -ENOMEM; | |
1603 | goto fail; | |
1604 | } | |
c952c6ec | 1605 | |
9e2f7c11 LP |
1606 | r = exec_spawn(c, |
1607 | argv, | |
1608 | &s->exec_context, | |
1609 | fds, n_fds, | |
2105e76a | 1610 | final_env, |
9e2f7c11 LP |
1611 | apply_permissions, |
1612 | apply_chroot, | |
1e3ad081 | 1613 | apply_tty_stdin, |
4cd1fbcc LP |
1614 | s->meta.manager->confirm_spawn, |
1615 | s->meta.cgroup_bondings, | |
9e2f7c11 LP |
1616 | &pid); |
1617 | ||
9e2f7c11 | 1618 | if (r < 0) |
034c6ed7 LP |
1619 | goto fail; |
1620 | ||
4f2d528d | 1621 | |
87f0e418 | 1622 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
034c6ed7 LP |
1623 | /* FIXME: we need to do something here */ |
1624 | goto fail; | |
1625 | ||
2105e76a LP |
1626 | free(fdsbuf); |
1627 | strv_free(argv); | |
1628 | strv_free(our_env); | |
1629 | strv_free(final_env); | |
1630 | ||
034c6ed7 LP |
1631 | *_pid = pid; |
1632 | ||
5cb5a6ff | 1633 | return 0; |
034c6ed7 LP |
1634 | |
1635 | fail: | |
2105e76a | 1636 | free(fdsbuf); |
c952c6ec | 1637 | strv_free(argv); |
2105e76a LP |
1638 | strv_free(our_env); |
1639 | strv_free(final_env); | |
c952c6ec | 1640 | |
034c6ed7 | 1641 | if (timeout) |
acbb0225 | 1642 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 LP |
1643 | |
1644 | return r; | |
1645 | } | |
1646 | ||
80876c20 LP |
1647 | static int main_pid_good(Service *s) { |
1648 | assert(s); | |
1649 | ||
1650 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
1651 | * don't know */ | |
1652 | ||
1653 | /* If we know the pid file, then lets just check if it is | |
1654 | * still valid */ | |
1655 | if (s->main_pid_known) | |
1656 | return s->main_pid > 0; | |
1657 | ||
1658 | /* We don't know the pid */ | |
1659 | return -EAGAIN; | |
1660 | } | |
1661 | ||
1662 | static int control_pid_good(Service *s) { | |
1663 | assert(s); | |
1664 | ||
1665 | return s->control_pid > 0; | |
1666 | } | |
1667 | ||
1668 | static int cgroup_good(Service *s) { | |
1669 | int r; | |
1670 | ||
1671 | assert(s); | |
1672 | ||
4cd1fbcc | 1673 | if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0) |
80876c20 LP |
1674 | return r; |
1675 | ||
1676 | return !r; | |
1677 | } | |
1678 | ||
034c6ed7 LP |
1679 | static void service_enter_dead(Service *s, bool success, bool allow_restart) { |
1680 | int r; | |
1681 | assert(s); | |
1682 | ||
1683 | if (!success) | |
1684 | s->failure = true; | |
1685 | ||
1686 | if (allow_restart && | |
47342320 | 1687 | !s->forbid_restart && |
034c6ed7 | 1688 | (s->restart == SERVICE_RESTART_ALWAYS || |
50caaedb LP |
1689 | (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure) || |
1690 | (s->restart == SERVICE_RESTART_ON_FAILURE && s->failure) || | |
1691 | (s->restart == SERVICE_RESTART_ON_ABORT && s->failure && | |
1692 | (s->main_exec_status.code == CLD_KILLED || | |
1693 | s->main_exec_status.code == CLD_DUMPED)))) { | |
034c6ed7 | 1694 | |
acbb0225 | 1695 | if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0) |
034c6ed7 LP |
1696 | goto fail; |
1697 | ||
1698 | service_set_state(s, SERVICE_AUTO_RESTART); | |
1699 | } else | |
fdf20a31 | 1700 | service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD); |
034c6ed7 | 1701 | |
47342320 LP |
1702 | s->forbid_restart = false; |
1703 | ||
034c6ed7 LP |
1704 | return; |
1705 | ||
1706 | fail: | |
4cd1fbcc | 1707 | log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1708 | service_enter_dead(s, false, false); |
1709 | } | |
1710 | ||
1711 | static void service_enter_signal(Service *s, ServiceState state, bool success); | |
1712 | ||
1713 | static void service_enter_stop_post(Service *s, bool success) { | |
1714 | int r; | |
1715 | assert(s); | |
1716 | ||
1717 | if (!success) | |
1718 | s->failure = true; | |
1719 | ||
5e94833f LP |
1720 | service_unwatch_control_pid(s); |
1721 | ||
a16e1123 | 1722 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
80876c20 | 1723 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) { |
81a2b7ce LP |
1724 | if ((r = service_spawn(s, |
1725 | s->control_command, | |
1726 | true, | |
1727 | false, | |
1728 | !s->permissions_start_only, | |
1729 | !s->root_directory_start_only, | |
1e3ad081 | 1730 | true, |
c952c6ec | 1731 | false, |
81a2b7ce | 1732 | &s->control_pid)) < 0) |
034c6ed7 LP |
1733 | goto fail; |
1734 | ||
d6ea93e3 | 1735 | |
80876c20 LP |
1736 | service_set_state(s, SERVICE_STOP_POST); |
1737 | } else | |
1738 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, true); | |
034c6ed7 LP |
1739 | |
1740 | return; | |
1741 | ||
1742 | fail: | |
4cd1fbcc | 1743 | log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1744 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
1745 | } | |
1746 | ||
1747 | static void service_enter_signal(Service *s, ServiceState state, bool success) { | |
1748 | int r; | |
ca949c9d LP |
1749 | Set *pid_set = NULL; |
1750 | bool wait_for_exit = false; | |
034c6ed7 LP |
1751 | |
1752 | assert(s); | |
1753 | ||
1754 | if (!success) | |
1755 | s->failure = true; | |
1756 | ||
2e22afe9 LP |
1757 | if (s->exec_context.kill_mode != KILL_NONE) { |
1758 | int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL; | |
034c6ed7 | 1759 | |
ca949c9d LP |
1760 | if (s->main_pid > 0) { |
1761 | if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ? | |
1762 | -s->main_pid : | |
1763 | s->main_pid, sig) < 0 && errno != ESRCH) | |
034c6ed7 | 1764 | |
ca949c9d LP |
1765 | log_warning("Failed to kill main process %li: %m", (long) s->main_pid); |
1766 | else | |
1767 | wait_for_exit = true; | |
034c6ed7 LP |
1768 | } |
1769 | ||
ca949c9d LP |
1770 | if (s->control_pid > 0) { |
1771 | if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ? | |
1772 | -s->control_pid : | |
1773 | s->control_pid, sig) < 0 && errno != ESRCH) | |
80876c20 | 1774 | |
ca949c9d LP |
1775 | log_warning("Failed to kill control process %li: %m", (long) s->control_pid); |
1776 | else | |
1777 | wait_for_exit = true; | |
1778 | } | |
50159e6a | 1779 | |
ca949c9d | 1780 | if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) { |
50159e6a | 1781 | |
ca949c9d LP |
1782 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) { |
1783 | r = -ENOMEM; | |
50159e6a | 1784 | goto fail; |
ca949c9d LP |
1785 | } |
1786 | ||
1787 | /* Exclude the main/control pids from being killed via the cgroup */ | |
1788 | if (s->main_pid > 0) | |
1789 | if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) | |
1790 | goto fail; | |
1791 | ||
1792 | if (s->control_pid > 0) | |
1793 | if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) | |
1794 | goto fail; | |
1795 | ||
1796 | if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) { | |
1797 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
1798 | log_warning("Failed to kill control group: %s", strerror(-r)); | |
f5a50114 | 1799 | } else if (r > 0) |
ca949c9d LP |
1800 | wait_for_exit = true; |
1801 | ||
1802 | set_free(pid_set); | |
50159e6a | 1803 | } |
d6ea93e3 | 1804 | } |
034c6ed7 | 1805 | |
ca949c9d | 1806 | if (wait_for_exit) { |
e558336f LP |
1807 | if (s->timeout_usec > 0) |
1808 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
1809 | goto fail; | |
d6ea93e3 | 1810 | |
80876c20 LP |
1811 | service_set_state(s, state); |
1812 | } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) | |
1813 | service_enter_stop_post(s, true); | |
1814 | else | |
034c6ed7 LP |
1815 | service_enter_dead(s, true, true); |
1816 | ||
1817 | return; | |
1818 | ||
1819 | fail: | |
4cd1fbcc | 1820 | log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r)); |
034c6ed7 | 1821 | |
80876c20 | 1822 | if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) |
034c6ed7 LP |
1823 | service_enter_stop_post(s, false); |
1824 | else | |
1825 | service_enter_dead(s, false, true); | |
ca949c9d LP |
1826 | |
1827 | if (pid_set) | |
1828 | set_free(pid_set); | |
034c6ed7 LP |
1829 | } |
1830 | ||
1831 | static void service_enter_stop(Service *s, bool success) { | |
1832 | int r; | |
5925dd3c | 1833 | |
034c6ed7 LP |
1834 | assert(s); |
1835 | ||
1836 | if (!success) | |
1837 | s->failure = true; | |
1838 | ||
5e94833f LP |
1839 | service_unwatch_control_pid(s); |
1840 | ||
a16e1123 | 1841 | s->control_command_id = SERVICE_EXEC_STOP; |
80876c20 | 1842 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) { |
81a2b7ce LP |
1843 | if ((r = service_spawn(s, |
1844 | s->control_command, | |
1845 | true, | |
1846 | false, | |
1847 | !s->permissions_start_only, | |
1848 | !s->root_directory_start_only, | |
c952c6ec | 1849 | false, |
1e3ad081 | 1850 | false, |
e55224ca | 1851 | &s->control_pid)) < 0) |
034c6ed7 LP |
1852 | goto fail; |
1853 | ||
80876c20 LP |
1854 | service_set_state(s, SERVICE_STOP); |
1855 | } else | |
034c6ed7 LP |
1856 | service_enter_signal(s, SERVICE_STOP_SIGTERM, true); |
1857 | ||
1858 | return; | |
1859 | ||
1860 | fail: | |
4cd1fbcc | 1861 | log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1862 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); |
1863 | } | |
1864 | ||
80876c20 | 1865 | static void service_enter_running(Service *s, bool success) { |
4eab639f | 1866 | int main_pid_ok, cgroup_ok; |
80876c20 LP |
1867 | assert(s); |
1868 | ||
1869 | if (!success) | |
1870 | s->failure = true; | |
1871 | ||
4eab639f LP |
1872 | main_pid_ok = main_pid_good(s); |
1873 | cgroup_ok = cgroup_good(s); | |
1874 | ||
1875 | if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) && | |
05e343b7 | 1876 | (s->bus_name_good || s->type != SERVICE_DBUS)) |
80876c20 | 1877 | service_set_state(s, SERVICE_RUNNING); |
02ee865a | 1878 | else if (s->remain_after_exit) |
80876c20 LP |
1879 | service_set_state(s, SERVICE_EXITED); |
1880 | else | |
1881 | service_enter_stop(s, true); | |
1882 | } | |
1883 | ||
034c6ed7 LP |
1884 | static void service_enter_start_post(Service *s) { |
1885 | int r; | |
1886 | assert(s); | |
1887 | ||
5e94833f LP |
1888 | service_unwatch_control_pid(s); |
1889 | ||
a16e1123 | 1890 | s->control_command_id = SERVICE_EXEC_START_POST; |
80876c20 | 1891 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) { |
81a2b7ce LP |
1892 | if ((r = service_spawn(s, |
1893 | s->control_command, | |
1894 | true, | |
1895 | false, | |
1896 | !s->permissions_start_only, | |
1897 | !s->root_directory_start_only, | |
c952c6ec | 1898 | false, |
1e3ad081 | 1899 | false, |
e55224ca | 1900 | &s->control_pid)) < 0) |
034c6ed7 LP |
1901 | goto fail; |
1902 | ||
80876c20 LP |
1903 | service_set_state(s, SERVICE_START_POST); |
1904 | } else | |
1905 | service_enter_running(s, true); | |
034c6ed7 LP |
1906 | |
1907 | return; | |
1908 | ||
1909 | fail: | |
4cd1fbcc | 1910 | log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1911 | service_enter_stop(s, false); |
1912 | } | |
1913 | ||
1914 | static void service_enter_start(Service *s) { | |
1915 | pid_t pid; | |
1916 | int r; | |
1917 | ||
1918 | assert(s); | |
1919 | ||
1920 | assert(s->exec_command[SERVICE_EXEC_START]); | |
34e9ba66 | 1921 | assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT); |
034c6ed7 | 1922 | |
80876c20 LP |
1923 | if (s->type == SERVICE_FORKING) |
1924 | service_unwatch_control_pid(s); | |
1925 | else | |
1926 | service_unwatch_main_pid(s); | |
1927 | ||
34e9ba66 LP |
1928 | s->control_command_id = SERVICE_EXEC_START; |
1929 | s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
1930 | ||
81a2b7ce | 1931 | if ((r = service_spawn(s, |
34e9ba66 | 1932 | s->control_command, |
8c47c732 | 1933 | s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY, |
81a2b7ce LP |
1934 | true, |
1935 | true, | |
1936 | true, | |
1e3ad081 | 1937 | true, |
c952c6ec | 1938 | s->notify_access != NOTIFY_NONE, |
81a2b7ce | 1939 | &pid)) < 0) |
034c6ed7 LP |
1940 | goto fail; |
1941 | ||
1942 | if (s->type == SERVICE_SIMPLE) { | |
1943 | /* For simple services we immediately start | |
1944 | * the START_POST binaries. */ | |
1945 | ||
5925dd3c | 1946 | service_set_main_pid(s, pid); |
034c6ed7 LP |
1947 | service_enter_start_post(s); |
1948 | ||
1949 | } else if (s->type == SERVICE_FORKING) { | |
1950 | ||
1951 | /* For forking services we wait until the start | |
1952 | * process exited. */ | |
1953 | ||
e55224ca | 1954 | s->control_pid = pid; |
80876c20 LP |
1955 | service_set_state(s, SERVICE_START); |
1956 | ||
34e9ba66 | 1957 | } else if (s->type == SERVICE_ONESHOT || |
8c47c732 LP |
1958 | s->type == SERVICE_DBUS || |
1959 | s->type == SERVICE_NOTIFY) { | |
7d55e835 | 1960 | |
34e9ba66 | 1961 | /* For oneshot services we wait until the start |
7d55e835 LP |
1962 | * process exited, too, but it is our main process. */ |
1963 | ||
05e343b7 | 1964 | /* For D-Bus services we know the main pid right away, |
8c47c732 LP |
1965 | * but wait for the bus name to appear on the |
1966 | * bus. Notify services are similar. */ | |
05e343b7 | 1967 | |
5925dd3c | 1968 | service_set_main_pid(s, pid); |
80876c20 | 1969 | service_set_state(s, SERVICE_START); |
034c6ed7 LP |
1970 | } else |
1971 | assert_not_reached("Unknown service type"); | |
1972 | ||
1973 | return; | |
1974 | ||
1975 | fail: | |
4cd1fbcc | 1976 | log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r)); |
80876c20 | 1977 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
034c6ed7 LP |
1978 | } |
1979 | ||
1980 | static void service_enter_start_pre(Service *s) { | |
1981 | int r; | |
1982 | ||
1983 | assert(s); | |
1984 | ||
5e94833f LP |
1985 | service_unwatch_control_pid(s); |
1986 | ||
a16e1123 | 1987 | s->control_command_id = SERVICE_EXEC_START_PRE; |
80876c20 | 1988 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) { |
81a2b7ce LP |
1989 | if ((r = service_spawn(s, |
1990 | s->control_command, | |
1991 | true, | |
1992 | false, | |
1993 | !s->permissions_start_only, | |
1994 | !s->root_directory_start_only, | |
1e3ad081 | 1995 | true, |
c952c6ec | 1996 | false, |
e55224ca | 1997 | &s->control_pid)) < 0) |
034c6ed7 LP |
1998 | goto fail; |
1999 | ||
80876c20 LP |
2000 | service_set_state(s, SERVICE_START_PRE); |
2001 | } else | |
034c6ed7 LP |
2002 | service_enter_start(s); |
2003 | ||
2004 | return; | |
2005 | ||
2006 | fail: | |
4cd1fbcc | 2007 | log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
2008 | service_enter_dead(s, false, true); |
2009 | } | |
2010 | ||
2011 | static void service_enter_restart(Service *s) { | |
2012 | int r; | |
398ef8ba LP |
2013 | DBusError error; |
2014 | ||
034c6ed7 | 2015 | assert(s); |
398ef8ba | 2016 | dbus_error_init(&error); |
034c6ed7 | 2017 | |
9ea9a0c8 LP |
2018 | service_enter_dead(s, true, false); |
2019 | ||
7b2603e6 | 2020 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0) |
034c6ed7 LP |
2021 | goto fail; |
2022 | ||
4cd1fbcc | 2023 | log_debug("%s scheduled restart job.", s->meta.id); |
034c6ed7 LP |
2024 | return; |
2025 | ||
2026 | fail: | |
398ef8ba | 2027 | log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r)); |
034c6ed7 | 2028 | service_enter_dead(s, false, false); |
398ef8ba LP |
2029 | |
2030 | dbus_error_free(&error); | |
034c6ed7 LP |
2031 | } |
2032 | ||
2033 | static void service_enter_reload(Service *s) { | |
2034 | int r; | |
2035 | ||
2036 | assert(s); | |
2037 | ||
5e94833f LP |
2038 | service_unwatch_control_pid(s); |
2039 | ||
a16e1123 | 2040 | s->control_command_id = SERVICE_EXEC_RELOAD; |
80876c20 | 2041 | if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) { |
81a2b7ce LP |
2042 | if ((r = service_spawn(s, |
2043 | s->control_command, | |
2044 | true, | |
2045 | false, | |
2046 | !s->permissions_start_only, | |
2047 | !s->root_directory_start_only, | |
c952c6ec | 2048 | false, |
1e3ad081 | 2049 | false, |
e55224ca | 2050 | &s->control_pid)) < 0) |
034c6ed7 LP |
2051 | goto fail; |
2052 | ||
80876c20 LP |
2053 | service_set_state(s, SERVICE_RELOAD); |
2054 | } else | |
2055 | service_enter_running(s, true); | |
034c6ed7 LP |
2056 | |
2057 | return; | |
2058 | ||
2059 | fail: | |
4cd1fbcc | 2060 | log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
2061 | service_enter_stop(s, false); |
2062 | } | |
2063 | ||
34e9ba66 | 2064 | static void service_run_next_control(Service *s, bool success) { |
034c6ed7 LP |
2065 | int r; |
2066 | ||
2067 | assert(s); | |
2068 | assert(s->control_command); | |
2069 | assert(s->control_command->command_next); | |
2070 | ||
2071 | if (!success) | |
2072 | s->failure = true; | |
2073 | ||
34e9ba66 | 2074 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 2075 | |
34e9ba66 | 2076 | s->control_command = s->control_command->command_next; |
5e94833f LP |
2077 | service_unwatch_control_pid(s); |
2078 | ||
81a2b7ce LP |
2079 | if ((r = service_spawn(s, |
2080 | s->control_command, | |
2081 | true, | |
2082 | false, | |
2083 | !s->permissions_start_only, | |
2084 | !s->root_directory_start_only, | |
5830833f LP |
2085 | s->control_command_id == SERVICE_EXEC_START_PRE || |
2086 | s->control_command_id == SERVICE_EXEC_STOP_POST, | |
1e3ad081 | 2087 | false, |
e55224ca | 2088 | &s->control_pid)) < 0) |
034c6ed7 LP |
2089 | goto fail; |
2090 | ||
2091 | return; | |
2092 | ||
2093 | fail: | |
34e9ba66 | 2094 | log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r)); |
034c6ed7 | 2095 | |
80876c20 LP |
2096 | if (s->state == SERVICE_START_PRE) |
2097 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2098 | else if (s->state == SERVICE_STOP) | |
2099 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); | |
034c6ed7 LP |
2100 | else if (s->state == SERVICE_STOP_POST) |
2101 | service_enter_dead(s, false, true); | |
2102 | else | |
2103 | service_enter_stop(s, false); | |
5cb5a6ff LP |
2104 | } |
2105 | ||
34e9ba66 LP |
2106 | static void service_run_next_main(Service *s, bool success) { |
2107 | pid_t pid; | |
2108 | int r; | |
2109 | ||
2110 | assert(s); | |
2111 | assert(s->control_command); | |
2112 | assert(s->control_command->command_next); | |
2113 | ||
2114 | if (!success) | |
2115 | s->failure = true; | |
2116 | ||
2117 | assert(s->control_command_id == SERVICE_EXEC_START); | |
2118 | assert(s->type == SERVICE_ONESHOT); | |
2119 | ||
2120 | s->control_command = s->control_command->command_next; | |
2121 | service_unwatch_main_pid(s); | |
2122 | ||
2123 | if ((r = service_spawn(s, | |
2124 | s->control_command, | |
2125 | false, | |
2126 | true, | |
2127 | true, | |
2128 | true, | |
2129 | true, | |
2130 | s->notify_access != NOTIFY_NONE, | |
2131 | &pid)) < 0) | |
2132 | goto fail; | |
2133 | ||
2134 | service_set_main_pid(s, pid); | |
2135 | ||
2136 | return; | |
2137 | ||
2138 | fail: | |
2139 | log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r)); | |
2140 | service_enter_stop(s, false); | |
2141 | } | |
2142 | ||
87f0e418 LP |
2143 | static int service_start(Unit *u) { |
2144 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
2145 | |
2146 | assert(s); | |
2147 | ||
034c6ed7 LP |
2148 | /* We cannot fulfill this request right now, try again later |
2149 | * please! */ | |
2150 | if (s->state == SERVICE_STOP || | |
2151 | s->state == SERVICE_STOP_SIGTERM || | |
2152 | s->state == SERVICE_STOP_SIGKILL || | |
2153 | s->state == SERVICE_STOP_POST || | |
2154 | s->state == SERVICE_FINAL_SIGTERM || | |
2155 | s->state == SERVICE_FINAL_SIGKILL) | |
5cb5a6ff LP |
2156 | return -EAGAIN; |
2157 | ||
034c6ed7 LP |
2158 | /* Already on it! */ |
2159 | if (s->state == SERVICE_START_PRE || | |
2160 | s->state == SERVICE_START || | |
2161 | s->state == SERVICE_START_POST) | |
2162 | return 0; | |
2163 | ||
fdf20a31 | 2164 | assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART); |
5cb5a6ff | 2165 | |
1e2e8133 LP |
2166 | /* Make sure we don't enter a busy loop of some kind. */ |
2167 | if (!ratelimit_test(&s->ratelimit)) { | |
9e2f7c11 | 2168 | log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id); |
d5159713 | 2169 | return -ECANCELED; |
1e2e8133 LP |
2170 | } |
2171 | ||
034c6ed7 LP |
2172 | s->failure = false; |
2173 | s->main_pid_known = false; | |
47342320 | 2174 | s->forbid_restart = false; |
034c6ed7 LP |
2175 | |
2176 | service_enter_start_pre(s); | |
2177 | return 0; | |
5cb5a6ff LP |
2178 | } |
2179 | ||
87f0e418 LP |
2180 | static int service_stop(Unit *u) { |
2181 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
2182 | |
2183 | assert(s); | |
2184 | ||
3f6c78dc LP |
2185 | /* This is a user request, so don't do restarts on this |
2186 | * shutdown. */ | |
47342320 | 2187 | s->forbid_restart = true; |
034c6ed7 | 2188 | |
e537352b LP |
2189 | /* Already on it */ |
2190 | if (s->state == SERVICE_STOP || | |
2191 | s->state == SERVICE_STOP_SIGTERM || | |
2192 | s->state == SERVICE_STOP_SIGKILL || | |
2193 | s->state == SERVICE_STOP_POST || | |
2194 | s->state == SERVICE_FINAL_SIGTERM || | |
2195 | s->state == SERVICE_FINAL_SIGKILL) | |
2196 | return 0; | |
2197 | ||
3f6c78dc | 2198 | /* Don't allow a restart */ |
034c6ed7 LP |
2199 | if (s->state == SERVICE_AUTO_RESTART) { |
2200 | service_set_state(s, SERVICE_DEAD); | |
2201 | return 0; | |
2202 | } | |
2203 | ||
3f6c78dc LP |
2204 | /* If there's already something running we go directly into |
2205 | * kill mode. */ | |
2206 | if (s->state == SERVICE_START_PRE || | |
2207 | s->state == SERVICE_START || | |
2208 | s->state == SERVICE_START_POST || | |
2209 | s->state == SERVICE_RELOAD) { | |
2210 | service_enter_signal(s, SERVICE_STOP_SIGTERM, true); | |
2211 | return 0; | |
2212 | } | |
5cb5a6ff | 2213 | |
3f6c78dc LP |
2214 | assert(s->state == SERVICE_RUNNING || |
2215 | s->state == SERVICE_EXITED); | |
3a762661 | 2216 | |
034c6ed7 | 2217 | service_enter_stop(s, true); |
5cb5a6ff LP |
2218 | return 0; |
2219 | } | |
2220 | ||
87f0e418 LP |
2221 | static int service_reload(Unit *u) { |
2222 | Service *s = SERVICE(u); | |
034c6ed7 LP |
2223 | |
2224 | assert(s); | |
2225 | ||
80876c20 | 2226 | assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED); |
034c6ed7 LP |
2227 | |
2228 | service_enter_reload(s); | |
5cb5a6ff LP |
2229 | return 0; |
2230 | } | |
2231 | ||
87f0e418 LP |
2232 | static bool service_can_reload(Unit *u) { |
2233 | Service *s = SERVICE(u); | |
034c6ed7 LP |
2234 | |
2235 | assert(s); | |
2236 | ||
2237 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
2238 | } | |
2239 | ||
a16e1123 LP |
2240 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
2241 | Service *s = SERVICE(u); | |
2242 | ||
2243 | assert(u); | |
2244 | assert(f); | |
2245 | assert(fds); | |
2246 | ||
2247 | unit_serialize_item(u, f, "state", service_state_to_string(s->state)); | |
2248 | unit_serialize_item(u, f, "failure", yes_no(s->failure)); | |
2249 | ||
2250 | if (s->control_pid > 0) | |
5925dd3c | 2251 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); |
a16e1123 | 2252 | |
5925dd3c LP |
2253 | if (s->main_pid_known && s->main_pid > 0) |
2254 | unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid); | |
a16e1123 LP |
2255 | |
2256 | unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known)); | |
2257 | ||
3a2776bc LP |
2258 | if (s->status_text) |
2259 | unit_serialize_item(u, f, "status-text", s->status_text); | |
2260 | ||
a16e1123 LP |
2261 | /* There's a minor uncleanliness here: if there are multiple |
2262 | * commands attached here, we will start from the first one | |
2263 | * again */ | |
2264 | if (s->control_command_id >= 0) | |
825636e5 | 2265 | unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id)); |
a16e1123 LP |
2266 | |
2267 | if (s->socket_fd >= 0) { | |
2268 | int copy; | |
2269 | ||
2270 | if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0) | |
2271 | return copy; | |
2272 | ||
2273 | unit_serialize_item_format(u, f, "socket-fd", "%i", copy); | |
2274 | } | |
2275 | ||
ecdbca40 LP |
2276 | if (s->main_exec_status.pid > 0) { |
2277 | unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid); | |
2278 | ||
2279 | if (s->main_exec_status.start_timestamp.realtime > 0) { | |
2280 | unit_serialize_item_format(u, f, "main-exec-status-start-realtime", | |
2281 | "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime); | |
2282 | ||
2283 | unit_serialize_item_format(u, f, "main-exec-status-start-monotonic", | |
2284 | "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic); | |
2285 | } | |
2286 | ||
2287 | if (s->main_exec_status.exit_timestamp.realtime > 0) { | |
2288 | unit_serialize_item_format(u, f, "main-exec-status-exit-realtime", | |
2289 | "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime); | |
2290 | unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic", | |
2291 | "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic); | |
2292 | ||
2293 | unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code); | |
2294 | unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
2295 | } | |
2296 | } | |
2297 | ||
a16e1123 LP |
2298 | return 0; |
2299 | } | |
2300 | ||
2301 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
2302 | Service *s = SERVICE(u); | |
a16e1123 LP |
2303 | |
2304 | assert(u); | |
2305 | assert(key); | |
2306 | assert(value); | |
2307 | assert(fds); | |
2308 | ||
2309 | if (streq(key, "state")) { | |
2310 | ServiceState state; | |
2311 | ||
2312 | if ((state = service_state_from_string(value)) < 0) | |
2313 | log_debug("Failed to parse state value %s", value); | |
2314 | else | |
2315 | s->deserialized_state = state; | |
2316 | } else if (streq(key, "failure")) { | |
2317 | int b; | |
2318 | ||
2319 | if ((b = parse_boolean(value)) < 0) | |
2320 | log_debug("Failed to parse failure value %s", value); | |
2321 | else | |
2322 | s->failure = b || s->failure; | |
2323 | } else if (streq(key, "control-pid")) { | |
5925dd3c | 2324 | pid_t pid; |
a16e1123 | 2325 | |
e364ad06 | 2326 | if (parse_pid(value, &pid) < 0) |
a16e1123 LP |
2327 | log_debug("Failed to parse control-pid value %s", value); |
2328 | else | |
e55224ca | 2329 | s->control_pid = pid; |
a16e1123 | 2330 | } else if (streq(key, "main-pid")) { |
5925dd3c | 2331 | pid_t pid; |
a16e1123 | 2332 | |
e364ad06 | 2333 | if (parse_pid(value, &pid) < 0) |
a16e1123 LP |
2334 | log_debug("Failed to parse main-pid value %s", value); |
2335 | else | |
5925dd3c | 2336 | service_set_main_pid(s, (pid_t) pid); |
a16e1123 LP |
2337 | } else if (streq(key, "main-pid-known")) { |
2338 | int b; | |
2339 | ||
2340 | if ((b = parse_boolean(value)) < 0) | |
2341 | log_debug("Failed to parse main-pid-known value %s", value); | |
2342 | else | |
2343 | s->main_pid_known = b; | |
3a2776bc LP |
2344 | } else if (streq(key, "status-text")) { |
2345 | char *t; | |
2346 | ||
2347 | if ((t = strdup(value))) { | |
2348 | free(s->status_text); | |
2349 | s->status_text = t; | |
2350 | } | |
2351 | ||
a16e1123 LP |
2352 | } else if (streq(key, "control-command")) { |
2353 | ServiceExecCommand id; | |
2354 | ||
2355 | if ((id = service_exec_command_from_string(value)) < 0) | |
2356 | log_debug("Failed to parse exec-command value %s", value); | |
2357 | else { | |
2358 | s->control_command_id = id; | |
2359 | s->control_command = s->exec_command[id]; | |
2360 | } | |
2361 | } else if (streq(key, "socket-fd")) { | |
2362 | int fd; | |
2363 | ||
2364 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2365 | log_debug("Failed to parse socket-fd value %s", value); | |
2366 | else { | |
2367 | ||
2368 | if (s->socket_fd >= 0) | |
2369 | close_nointr_nofail(s->socket_fd); | |
2370 | s->socket_fd = fdset_remove(fds, fd); | |
2371 | } | |
ecdbca40 LP |
2372 | } else if (streq(key, "main-exec-status-pid")) { |
2373 | pid_t pid; | |
2374 | ||
e364ad06 | 2375 | if (parse_pid(value, &pid) < 0) |
ecdbca40 LP |
2376 | log_debug("Failed to parse main-exec-status-pid value %s", value); |
2377 | else | |
2378 | s->main_exec_status.pid = pid; | |
2379 | } else if (streq(key, "main-exec-status-code")) { | |
2380 | int i; | |
2381 | ||
e364ad06 | 2382 | if (safe_atoi(value, &i) < 0) |
ecdbca40 LP |
2383 | log_debug("Failed to parse main-exec-status-code value %s", value); |
2384 | else | |
2385 | s->main_exec_status.code = i; | |
2386 | } else if (streq(key, "main-exec-status-status")) { | |
2387 | int i; | |
2388 | ||
e364ad06 | 2389 | if (safe_atoi(value, &i) < 0) |
ecdbca40 LP |
2390 | log_debug("Failed to parse main-exec-status-status value %s", value); |
2391 | else | |
2392 | s->main_exec_status.status = i; | |
2393 | } else if (streq(key, "main-exec-status-start-realtime")) { | |
2394 | uint64_t k; | |
2395 | ||
e364ad06 | 2396 | if (safe_atou64(value, &k) < 0) |
ecdbca40 LP |
2397 | log_debug("Failed to parse main-exec-status-start-realtime value %s", value); |
2398 | else | |
2399 | s->main_exec_status.start_timestamp.realtime = (usec_t) k; | |
2400 | } else if (streq(key, "main-exec-status-start-monotonic")) { | |
2401 | uint64_t k; | |
2402 | ||
e364ad06 | 2403 | if (safe_atou64(value, &k) < 0) |
ecdbca40 LP |
2404 | log_debug("Failed to parse main-exec-status-start-monotonic value %s", value); |
2405 | else | |
2406 | s->main_exec_status.start_timestamp.monotonic = (usec_t) k; | |
2407 | } else if (streq(key, "main-exec-status-exit-realtime")) { | |
2408 | uint64_t k; | |
2409 | ||
e364ad06 | 2410 | if (safe_atou64(value, &k) < 0) |
ecdbca40 LP |
2411 | log_debug("Failed to parse main-exec-status-exit-realtime value %s", value); |
2412 | else | |
2413 | s->main_exec_status.exit_timestamp.realtime = (usec_t) k; | |
2414 | } else if (streq(key, "main-exec-status-exit-monotonic")) { | |
2415 | uint64_t k; | |
2416 | ||
e364ad06 | 2417 | if (safe_atou64(value, &k) < 0) |
ecdbca40 LP |
2418 | log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value); |
2419 | else | |
2420 | s->main_exec_status.exit_timestamp.monotonic = (usec_t) k; | |
a16e1123 LP |
2421 | } else |
2422 | log_debug("Unknown serialization key '%s'", key); | |
2423 | ||
2424 | return 0; | |
2425 | } | |
2426 | ||
87f0e418 LP |
2427 | static UnitActiveState service_active_state(Unit *u) { |
2428 | assert(u); | |
5cb5a6ff | 2429 | |
acbb0225 | 2430 | return state_translation_table[SERVICE(u)->state]; |
034c6ed7 LP |
2431 | } |
2432 | ||
10a94420 LP |
2433 | static const char *service_sub_state_to_string(Unit *u) { |
2434 | assert(u); | |
2435 | ||
2436 | return service_state_to_string(SERVICE(u)->state); | |
2437 | } | |
2438 | ||
07459bb6 | 2439 | #ifdef HAVE_SYSV_COMPAT |
701cc384 LP |
2440 | static bool service_check_gc(Unit *u) { |
2441 | Service *s = SERVICE(u); | |
2442 | ||
2443 | assert(s); | |
2444 | ||
2445 | return !!s->sysv_path; | |
2446 | } | |
07459bb6 | 2447 | #endif |
701cc384 LP |
2448 | |
2449 | static bool service_check_snapshot(Unit *u) { | |
2450 | Service *s = SERVICE(u); | |
2451 | ||
2452 | assert(s); | |
2453 | ||
2454 | return !s->got_socket_fd; | |
2455 | } | |
2456 | ||
87f0e418 LP |
2457 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
2458 | Service *s = SERVICE(u); | |
034c6ed7 | 2459 | bool success; |
5cb5a6ff LP |
2460 | |
2461 | assert(s); | |
034c6ed7 LP |
2462 | assert(pid >= 0); |
2463 | ||
f21781d5 | 2464 | if (!s->meta.fragment_path) |
d06dacd0 LP |
2465 | success = is_clean_exit_lsb(code, status); |
2466 | else | |
2467 | success = is_clean_exit(code, status); | |
034c6ed7 LP |
2468 | |
2469 | if (s->main_pid == pid) { | |
2470 | ||
034c6ed7 | 2471 | s->main_pid = 0; |
169c1bda | 2472 | exec_status_exit(&s->main_exec_status, pid, code, status, s->exec_context.utmp_id); |
034c6ed7 | 2473 | |
34e9ba66 LP |
2474 | if (s->type != SERVICE_FORKING && s->control_command) { |
2475 | s->control_command->exec_status = s->main_exec_status; | |
b708e7ce | 2476 | |
34e9ba66 | 2477 | if (s->control_command->ignore) |
b708e7ce | 2478 | success = true; |
034c6ed7 LP |
2479 | } |
2480 | ||
92abbefb LP |
2481 | log_full(success ? LOG_DEBUG : LOG_NOTICE, |
2482 | "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status); | |
b708e7ce | 2483 | s->failure = s->failure || !success; |
034c6ed7 | 2484 | |
34e9ba66 LP |
2485 | if (s->control_command && |
2486 | s->control_command->command_next && | |
2487 | success) { | |
034c6ed7 | 2488 | |
34e9ba66 LP |
2489 | /* There is another command to * |
2490 | * execute, so let's do that. */ | |
034c6ed7 | 2491 | |
34e9ba66 LP |
2492 | log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state)); |
2493 | service_run_next_main(s, success); | |
034c6ed7 | 2494 | |
34e9ba66 LP |
2495 | } else { |
2496 | ||
2497 | /* The service exited, so the service is officially | |
2498 | * gone. */ | |
2499 | ||
2500 | s->control_command = NULL; | |
2501 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2502 | ||
2503 | switch (s->state) { | |
2504 | ||
2505 | case SERVICE_START_POST: | |
2506 | case SERVICE_RELOAD: | |
2507 | case SERVICE_STOP: | |
2508 | /* Need to wait until the operation is | |
2509 | * done */ | |
c4653a4d | 2510 | break; |
7d55e835 | 2511 | |
34e9ba66 LP |
2512 | case SERVICE_START: |
2513 | if (s->type == SERVICE_ONESHOT) { | |
2514 | /* This was our main goal, so let's go on */ | |
2515 | if (success) | |
2516 | service_enter_start_post(s); | |
2517 | else | |
2518 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2519 | break; | |
2520 | } else { | |
2521 | assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY); | |
7d55e835 | 2522 | |
34e9ba66 LP |
2523 | /* Fall through */ |
2524 | } | |
034c6ed7 | 2525 | |
34e9ba66 LP |
2526 | case SERVICE_RUNNING: |
2527 | service_enter_running(s, success); | |
2528 | break; | |
034c6ed7 | 2529 | |
34e9ba66 LP |
2530 | case SERVICE_STOP_SIGTERM: |
2531 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 2532 | |
34e9ba66 LP |
2533 | if (!control_pid_good(s)) |
2534 | service_enter_stop_post(s, success); | |
5cb5a6ff | 2535 | |
34e9ba66 LP |
2536 | /* If there is still a control process, wait for that first */ |
2537 | break; | |
2538 | ||
2539 | default: | |
2540 | assert_not_reached("Uh, main process died at wrong time."); | |
2541 | } | |
034c6ed7 | 2542 | } |
5cb5a6ff | 2543 | |
034c6ed7 | 2544 | } else if (s->control_pid == pid) { |
034c6ed7 | 2545 | |
34e9ba66 LP |
2546 | s->control_pid = 0; |
2547 | ||
b708e7ce | 2548 | if (s->control_command) { |
169c1bda | 2549 | exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id); |
a16e1123 | 2550 | |
b708e7ce LP |
2551 | if (s->control_command->ignore) |
2552 | success = true; | |
2553 | } | |
2554 | ||
34e9ba66 | 2555 | log_full(success ? LOG_DEBUG : LOG_NOTICE, |
92abbefb | 2556 | "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); |
b708e7ce | 2557 | s->failure = s->failure || !success; |
034c6ed7 | 2558 | |
34e9ba66 LP |
2559 | if (s->control_command && |
2560 | s->control_command->command_next && | |
2561 | success) { | |
034c6ed7 LP |
2562 | |
2563 | /* There is another command to * | |
2564 | * execute, so let's do that. */ | |
2565 | ||
34e9ba66 LP |
2566 | log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state)); |
2567 | service_run_next_control(s, success); | |
034c6ed7 | 2568 | |
80876c20 | 2569 | } else { |
034c6ed7 LP |
2570 | /* No further commands for this step, so let's |
2571 | * figure out what to do next */ | |
2572 | ||
a16e1123 LP |
2573 | s->control_command = NULL; |
2574 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2575 | ||
9e2f7c11 | 2576 | log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state)); |
bd982a8b | 2577 | |
034c6ed7 LP |
2578 | switch (s->state) { |
2579 | ||
2580 | case SERVICE_START_PRE: | |
2581 | if (success) | |
2582 | service_enter_start(s); | |
2583 | else | |
80876c20 | 2584 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
034c6ed7 LP |
2585 | break; |
2586 | ||
2587 | case SERVICE_START: | |
2588 | assert(s->type == SERVICE_FORKING); | |
2589 | ||
2590 | /* Let's try to load the pid | |
2591 | * file here if we can. We | |
2592 | * ignore the return value, | |
2593 | * since the PID file might | |
2594 | * actually be created by a | |
2595 | * START_POST script */ | |
2596 | ||
2597 | if (success) { | |
2598 | if (s->pid_file) | |
2599 | service_load_pid_file(s); | |
2600 | ||
2601 | service_enter_start_post(s); | |
2602 | } else | |
80876c20 | 2603 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
034c6ed7 LP |
2604 | |
2605 | break; | |
2606 | ||
2607 | case SERVICE_START_POST: | |
2608 | if (success && s->pid_file && !s->main_pid_known) { | |
2609 | int r; | |
2610 | ||
2611 | /* Hmm, let's see if we can | |
2612 | * load the pid now after the | |
2613 | * start-post scripts got | |
2614 | * executed. */ | |
2615 | ||
2616 | if ((r = service_load_pid_file(s)) < 0) | |
4cd1fbcc | 2617 | log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r)); |
034c6ed7 LP |
2618 | } |
2619 | ||
2620 | /* Fall through */ | |
2621 | ||
2622 | case SERVICE_RELOAD: | |
80876c20 LP |
2623 | if (success) |
2624 | service_enter_running(s, true); | |
2625 | else | |
034c6ed7 LP |
2626 | service_enter_stop(s, false); |
2627 | ||
2628 | break; | |
2629 | ||
2630 | case SERVICE_STOP: | |
80876c20 | 2631 | service_enter_signal(s, SERVICE_STOP_SIGTERM, success); |
034c6ed7 LP |
2632 | break; |
2633 | ||
2634 | case SERVICE_STOP_SIGTERM: | |
2635 | case SERVICE_STOP_SIGKILL: | |
2636 | if (main_pid_good(s) <= 0) | |
2637 | service_enter_stop_post(s, success); | |
2638 | ||
2639 | /* If there is still a service | |
2640 | * process around, wait until | |
2641 | * that one quit, too */ | |
2642 | break; | |
2643 | ||
2644 | case SERVICE_STOP_POST: | |
2645 | case SERVICE_FINAL_SIGTERM: | |
2646 | case SERVICE_FINAL_SIGKILL: | |
2647 | service_enter_dead(s, success, true); | |
2648 | break; | |
2649 | ||
2650 | default: | |
2651 | assert_not_reached("Uh, control process died at wrong time."); | |
2652 | } | |
2653 | } | |
8c47c732 | 2654 | } |
c4e2ceae LP |
2655 | |
2656 | /* Notify clients about changed exit status */ | |
2657 | unit_add_to_dbus_queue(u); | |
034c6ed7 LP |
2658 | } |
2659 | ||
acbb0225 | 2660 | static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) { |
87f0e418 | 2661 | Service *s = SERVICE(u); |
034c6ed7 LP |
2662 | |
2663 | assert(s); | |
2664 | assert(elapsed == 1); | |
2665 | ||
acbb0225 | 2666 | assert(w == &s->timer_watch); |
034c6ed7 LP |
2667 | |
2668 | switch (s->state) { | |
2669 | ||
2670 | case SERVICE_START_PRE: | |
2671 | case SERVICE_START: | |
9e2f7c11 | 2672 | log_warning("%s operation timed out. Terminating.", u->meta.id); |
80876c20 LP |
2673 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
2674 | break; | |
2675 | ||
034c6ed7 LP |
2676 | case SERVICE_START_POST: |
2677 | case SERVICE_RELOAD: | |
9e2f7c11 | 2678 | log_warning("%s operation timed out. Stopping.", u->meta.id); |
034c6ed7 LP |
2679 | service_enter_stop(s, false); |
2680 | break; | |
2681 | ||
2682 | case SERVICE_STOP: | |
9e2f7c11 | 2683 | log_warning("%s stopping timed out. Terminating.", u->meta.id); |
034c6ed7 LP |
2684 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); |
2685 | break; | |
2686 | ||
2687 | case SERVICE_STOP_SIGTERM: | |
9e2f7c11 | 2688 | log_warning("%s stopping timed out. Killing.", u->meta.id); |
034c6ed7 LP |
2689 | service_enter_signal(s, SERVICE_STOP_SIGKILL, false); |
2690 | break; | |
2691 | ||
2692 | case SERVICE_STOP_SIGKILL: | |
2693 | /* Uh, wie sent a SIGKILL and it is still not gone? | |
2694 | * Must be something we cannot kill, so let's just be | |
2695 | * weirded out and continue */ | |
2696 | ||
9e2f7c11 | 2697 | log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id); |
034c6ed7 LP |
2698 | service_enter_stop_post(s, false); |
2699 | break; | |
2700 | ||
2701 | case SERVICE_STOP_POST: | |
9e2f7c11 | 2702 | log_warning("%s stopping timed out (2). Terminating.", u->meta.id); |
034c6ed7 LP |
2703 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
2704 | break; | |
2705 | ||
2706 | case SERVICE_FINAL_SIGTERM: | |
9e2f7c11 | 2707 | log_warning("%s stopping timed out (2). Killing.", u->meta.id); |
034c6ed7 LP |
2708 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, false); |
2709 | break; | |
2710 | ||
2711 | case SERVICE_FINAL_SIGKILL: | |
fdf20a31 | 2712 | log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id); |
034c6ed7 LP |
2713 | service_enter_dead(s, false, true); |
2714 | break; | |
2715 | ||
2716 | case SERVICE_AUTO_RESTART: | |
54165a39 | 2717 | log_info("%s holdoff time over, scheduling restart.", u->meta.id); |
034c6ed7 LP |
2718 | service_enter_restart(s); |
2719 | break; | |
2720 | ||
2721 | default: | |
2722 | assert_not_reached("Timeout at wrong time."); | |
2723 | } | |
5cb5a6ff LP |
2724 | } |
2725 | ||
8e274523 LP |
2726 | static void service_cgroup_notify_event(Unit *u) { |
2727 | Service *s = SERVICE(u); | |
2728 | ||
2729 | assert(u); | |
2730 | ||
9e2f7c11 | 2731 | log_debug("%s: cgroup is empty", u->meta.id); |
8e274523 LP |
2732 | |
2733 | switch (s->state) { | |
2734 | ||
2735 | /* Waiting for SIGCHLD is usually more interesting, | |
2736 | * because it includes return codes/signals. Which is | |
2737 | * why we ignore the cgroup events for most cases, | |
2738 | * except when we don't know pid which to expect the | |
2739 | * SIGCHLD for. */ | |
2740 | ||
2741 | case SERVICE_RUNNING: | |
80876c20 | 2742 | service_enter_running(s, true); |
8e274523 LP |
2743 | break; |
2744 | ||
28708d8a LP |
2745 | case SERVICE_STOP_SIGTERM: |
2746 | case SERVICE_STOP_SIGKILL: | |
2747 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2748 | service_enter_stop_post(s, true); | |
2749 | ||
2750 | break; | |
2751 | ||
7f97f0fe LP |
2752 | case SERVICE_FINAL_SIGTERM: |
2753 | case SERVICE_FINAL_SIGKILL: | |
2754 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2755 | service_enter_dead(s, true, true); | |
2756 | ||
2757 | break; | |
2758 | ||
8e274523 LP |
2759 | default: |
2760 | ; | |
2761 | } | |
2762 | } | |
2763 | ||
c952c6ec | 2764 | static void service_notify_message(Unit *u, pid_t pid, char **tags) { |
8c47c732 LP |
2765 | Service *s = SERVICE(u); |
2766 | const char *e; | |
2767 | ||
2768 | assert(u); | |
2769 | ||
c952c6ec LP |
2770 | if (s->notify_access == NOTIFY_NONE) { |
2771 | log_warning("%s: Got notification message from PID %lu, but reception is disabled.", | |
2772 | u->meta.id, (unsigned long) pid); | |
2773 | return; | |
2774 | } | |
2775 | ||
2776 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { | |
2777 | log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu", | |
2778 | u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid); | |
2779 | return; | |
2780 | } | |
2781 | ||
8c47c732 LP |
2782 | log_debug("%s: Got message", u->meta.id); |
2783 | ||
2784 | /* Interpret MAINPID= */ | |
2785 | if ((e = strv_find_prefix(tags, "MAINPID=")) && | |
2786 | (s->state == SERVICE_START || | |
2787 | s->state == SERVICE_START_POST || | |
2788 | s->state == SERVICE_RUNNING || | |
2789 | s->state == SERVICE_RELOAD)) { | |
8c47c732 | 2790 | |
5925dd3c | 2791 | if (parse_pid(e + 8, &pid) < 0) |
92abbefb | 2792 | log_warning("Failed to parse notification message %s", e); |
8c47c732 LP |
2793 | else { |
2794 | log_debug("%s: got %s", u->meta.id, e); | |
5925dd3c | 2795 | service_set_main_pid(s, pid); |
8c47c732 LP |
2796 | } |
2797 | } | |
2798 | ||
2799 | /* Interpret READY= */ | |
2800 | if (s->type == SERVICE_NOTIFY && | |
2801 | s->state == SERVICE_START && | |
2802 | strv_find(tags, "READY=1")) { | |
2803 | log_debug("%s: got READY=1", u->meta.id); | |
2804 | ||
2805 | service_enter_start_post(s); | |
2806 | } | |
2807 | ||
2808 | /* Interpret STATUS= */ | |
2809 | if ((e = strv_find_prefix(tags, "STATUS="))) { | |
2810 | char *t; | |
2811 | ||
3a2776bc LP |
2812 | if (e[7]) { |
2813 | if (!(t = strdup(e+7))) { | |
2814 | log_error("Failed to allocate string."); | |
2815 | return; | |
2816 | } | |
2817 | ||
2818 | log_debug("%s: got %s", u->meta.id, e); | |
8c47c732 | 2819 | |
3a2776bc LP |
2820 | free(s->status_text); |
2821 | s->status_text = t; | |
2822 | } else { | |
2823 | free(s->status_text); | |
2824 | s->status_text = NULL; | |
2825 | } | |
8c47c732 | 2826 | |
8c47c732 | 2827 | } |
c4e2ceae LP |
2828 | |
2829 | /* Notify clients about changed status or main pid */ | |
2830 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
2831 | } |
2832 | ||
07459bb6 | 2833 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 | 2834 | static int service_enumerate(Manager *m) { |
2c4104f0 LP |
2835 | char **p; |
2836 | unsigned i; | |
2837 | DIR *d = NULL; | |
2838 | char *path = NULL, *fpath = NULL, *name = NULL; | |
c68364b7 LP |
2839 | Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL; |
2840 | Unit *service; | |
2841 | Iterator j; | |
2c4104f0 | 2842 | int r; |
980900c1 TG |
2843 | #ifdef TARGET_ARCH |
2844 | Unit *previous = NULL; | |
2845 | char *arch_daemons = NULL; | |
2846 | char *arch_daemons_stripped = NULL; | |
2847 | char **arch_daemons_split = NULL; | |
2848 | #endif | |
2c4104f0 LP |
2849 | |
2850 | assert(m); | |
2851 | ||
980900c1 TG |
2852 | #ifdef TARGET_ARCH |
2853 | if ((r = parse_env_file("/etc/rc.conf", NEWLINE, | |
2854 | "DAEMONS", &arch_daemons, | |
2855 | NULL)) < 0) { | |
2856 | ||
2857 | if (r != -ENOENT) | |
2858 | log_warning("Failed to read /etc/rc.conf: %s", strerror(-r)); | |
2859 | ||
2860 | } else if (arch_daemons) { | |
2861 | if (!(arch_daemons_stripped = strchr(arch_daemons, '('))) | |
2862 | arch_daemons_stripped = arch_daemons; | |
2863 | else | |
2864 | arch_daemons_stripped++; /* strip start paren */ | |
2865 | ||
2866 | arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */ | |
2867 | ||
2868 | if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) { | |
2869 | r = -ENOMEM; | |
2870 | goto finish; | |
2871 | } | |
2872 | ||
2873 | STRV_FOREACH(p, arch_daemons_split) { | |
2874 | ||
2875 | free(name); | |
409db002 | 2876 | name = NULL; |
980900c1 TG |
2877 | |
2878 | if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */ | |
2879 | continue; | |
2880 | ||
2881 | if (!(name = sysv_translate_name(*p))) { | |
2882 | r = -ENOMEM; | |
2883 | goto finish; | |
2884 | } | |
2885 | ||
2886 | if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) { | |
2887 | log_warning("Failed to prepare unit %s: %s", name, strerror(-r)); | |
2888 | continue; | |
2889 | } | |
2890 | ||
2891 | if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0) | |
2892 | goto finish; | |
2893 | ||
2894 | if (previous) | |
2895 | if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0) | |
2896 | goto finish; | |
2897 | ||
2898 | if (**p != '@') /* daemons prefixed with @ can be started in the background */ | |
2899 | previous = service; | |
2900 | } | |
2901 | } | |
2902 | #endif | |
2903 | ||
c68364b7 LP |
2904 | zero(runlevel_services); |
2905 | ||
84e3543e | 2906 | STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path) |
09cd1ab1 | 2907 | for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) { |
2c4104f0 LP |
2908 | struct dirent *de; |
2909 | ||
2910 | free(path); | |
2911 | path = NULL; | |
09cd1ab1 | 2912 | if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) { |
2c4104f0 LP |
2913 | r = -ENOMEM; |
2914 | goto finish; | |
2915 | } | |
2916 | ||
2917 | if (d) | |
2918 | closedir(d); | |
2919 | ||
2920 | if (!(d = opendir(path))) { | |
2921 | if (errno != ENOENT) | |
2922 | log_warning("opendir() failed on %s: %s", path, strerror(errno)); | |
2923 | ||
2924 | continue; | |
2925 | } | |
2926 | ||
2927 | while ((de = readdir(d))) { | |
db06e3b6 | 2928 | int a, b; |
2c4104f0 LP |
2929 | |
2930 | if (ignore_file(de->d_name)) | |
2931 | continue; | |
2932 | ||
2933 | if (de->d_name[0] != 'S' && de->d_name[0] != 'K') | |
2934 | continue; | |
2935 | ||
2936 | if (strlen(de->d_name) < 4) | |
2937 | continue; | |
2938 | ||
db06e3b6 LP |
2939 | a = undecchar(de->d_name[1]); |
2940 | b = undecchar(de->d_name[2]); | |
2941 | ||
2942 | if (a < 0 || b < 0) | |
2943 | continue; | |
2944 | ||
2c4104f0 LP |
2945 | free(fpath); |
2946 | fpath = NULL; | |
09cd1ab1 | 2947 | if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) { |
2c4104f0 LP |
2948 | r = -ENOMEM; |
2949 | goto finish; | |
2950 | } | |
2951 | ||
2952 | if (access(fpath, X_OK) < 0) { | |
2953 | ||
2954 | if (errno != ENOENT) | |
2955 | log_warning("access() failed on %s: %s", fpath, strerror(errno)); | |
2956 | ||
2957 | continue; | |
2958 | } | |
2959 | ||
2960 | free(name); | |
b7ccee3c | 2961 | if (!(name = sysv_translate_name(de->d_name + 3))) { |
2c4104f0 LP |
2962 | r = -ENOMEM; |
2963 | goto finish; | |
2964 | } | |
2965 | ||
398ef8ba | 2966 | if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) { |
fbe9f3a9 LP |
2967 | log_warning("Failed to prepare unit %s: %s", name, strerror(-r)); |
2968 | continue; | |
2969 | } | |
2c4104f0 | 2970 | |
c68364b7 LP |
2971 | if (de->d_name[0] == 'S') { |
2972 | ||
f73d93a4 LP |
2973 | if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) { |
2974 | SERVICE(service)->sysv_start_priority = | |
2975 | MAX(a*10 + b, SERVICE(service)->sysv_start_priority); | |
db06e3b6 | 2976 | |
c68364b7 | 2977 | SERVICE(service)->sysv_enabled = true; |
f73d93a4 | 2978 | } |
db06e3b6 | 2979 | |
c68364b7 LP |
2980 | if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0) |
2981 | goto finish; | |
2c4104f0 | 2982 | |
c68364b7 | 2983 | if ((r = set_put(runlevel_services[i], service)) < 0) |
2c4104f0 | 2984 | goto finish; |
23a177ef | 2985 | |
fc5df99e LP |
2986 | } else if (de->d_name[0] == 'K' && |
2987 | (rcnd_table[i].type == RUNLEVEL_DOWN || | |
2988 | rcnd_table[i].type == RUNLEVEL_SYSINIT)) { | |
6542952f | 2989 | |
c68364b7 LP |
2990 | if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0) |
2991 | goto finish; | |
2992 | ||
2993 | if ((r = set_put(shutdown_services, service)) < 0) | |
2c4104f0 LP |
2994 | goto finish; |
2995 | } | |
2996 | } | |
2997 | } | |
2998 | ||
c68364b7 LP |
2999 | /* Now we loaded all stubs and are aware of the lowest |
3000 | start-up priority for all services, not let's actually load | |
3001 | the services, this will also tell us which services are | |
3002 | actually native now */ | |
3003 | manager_dispatch_load_queue(m); | |
3004 | ||
3005 | /* If this is a native service, rely on native ways to pull in | |
3006 | * a service, don't pull it in via sysv rcN.d links. */ | |
3007 | for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) | |
3008 | SET_FOREACH(service, runlevel_services[i], j) { | |
3009 | service = unit_follow_merge(service); | |
3010 | ||
3011 | if (service->meta.fragment_path) | |
3012 | continue; | |
3013 | ||
3014 | if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0) | |
3015 | goto finish; | |
3016 | } | |
3017 | ||
3018 | /* We honour K links only for halt/reboot. For the normal | |
3019 | * runlevels we assume the stop jobs will be implicitly added | |
3020 | * by the core logic. Also, we don't really distuingish here | |
3021 | * between the runlevels 0 and 6 and just add them to the | |
3022 | * special shutdown target. On SUSE the boot.d/ runlevel is | |
3023 | * also used for shutdown, so we add links for that too to the | |
3024 | * shutdown target.*/ | |
3025 | SET_FOREACH(service, shutdown_services, j) { | |
3026 | service = unit_follow_merge(service); | |
3027 | ||
3028 | if (service->meta.fragment_path) | |
3029 | continue; | |
3030 | ||
3031 | if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0) | |
3032 | goto finish; | |
3033 | } | |
3034 | ||
2c4104f0 LP |
3035 | r = 0; |
3036 | ||
3037 | finish: | |
3038 | free(path); | |
3039 | free(fpath); | |
3040 | free(name); | |
980900c1 TG |
3041 | #ifdef TARGET_ARCH |
3042 | free(arch_daemons); | |
3043 | free(arch_daemons_split); | |
3044 | #endif | |
fbe9f3a9 | 3045 | |
c68364b7 LP |
3046 | for (i = 0; i < ELEMENTSOF(rcnd_table); i++) |
3047 | set_free(runlevel_services[i]); | |
3048 | set_free(shutdown_services); | |
3049 | ||
fbe9f3a9 LP |
3050 | if (d) |
3051 | closedir(d); | |
2c4104f0 LP |
3052 | |
3053 | return r; | |
3054 | } | |
07459bb6 | 3055 | #endif |
2c4104f0 | 3056 | |
05e343b7 LP |
3057 | static void service_bus_name_owner_change( |
3058 | Unit *u, | |
3059 | const char *name, | |
3060 | const char *old_owner, | |
3061 | const char *new_owner) { | |
3062 | ||
3063 | Service *s = SERVICE(u); | |
3064 | ||
3065 | assert(s); | |
3066 | assert(name); | |
3067 | ||
3068 | assert(streq(s->bus_name, name)); | |
3069 | assert(old_owner || new_owner); | |
3070 | ||
3071 | if (old_owner && new_owner) | |
3072 | log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner); | |
3073 | else if (old_owner) | |
3074 | log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner); | |
3075 | else | |
3076 | log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner); | |
3077 | ||
3078 | s->bus_name_good = !!new_owner; | |
3079 | ||
3080 | if (s->type == SERVICE_DBUS) { | |
3081 | ||
3082 | /* service_enter_running() will figure out what to | |
3083 | * do */ | |
3084 | if (s->state == SERVICE_RUNNING) | |
3085 | service_enter_running(s, true); | |
3086 | else if (s->state == SERVICE_START && new_owner) | |
3087 | service_enter_start_post(s); | |
3088 | ||
3089 | } else if (new_owner && | |
3090 | s->main_pid <= 0 && | |
3091 | (s->state == SERVICE_START || | |
3092 | s->state == SERVICE_START_POST || | |
3093 | s->state == SERVICE_RUNNING || | |
3094 | s->state == SERVICE_RELOAD)) { | |
3095 | ||
3096 | /* Try to acquire PID from bus service */ | |
3097 | log_debug("Trying to acquire PID from D-Bus name..."); | |
3098 | ||
3099 | bus_query_pid(u->meta.manager, name); | |
3100 | } | |
3101 | } | |
3102 | ||
3103 | static void service_bus_query_pid_done( | |
3104 | Unit *u, | |
3105 | const char *name, | |
3106 | pid_t pid) { | |
3107 | ||
3108 | Service *s = SERVICE(u); | |
3109 | ||
3110 | assert(s); | |
3111 | assert(name); | |
3112 | ||
3113 | log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid); | |
3114 | ||
3115 | if (s->main_pid <= 0 && | |
3116 | (s->state == SERVICE_START || | |
3117 | s->state == SERVICE_START_POST || | |
3118 | s->state == SERVICE_RUNNING || | |
3119 | s->state == SERVICE_RELOAD)) | |
5925dd3c | 3120 | service_set_main_pid(s, pid); |
05e343b7 LP |
3121 | } |
3122 | ||
6cf6bbc2 | 3123 | int service_set_socket_fd(Service *s, int fd, Socket *sock) { |
4f2d528d LP |
3124 | assert(s); |
3125 | assert(fd >= 0); | |
3126 | ||
3127 | /* This is called by the socket code when instantiating a new | |
3128 | * service for a stream socket and the socket needs to be | |
3129 | * configured. */ | |
3130 | ||
4cd1fbcc | 3131 | if (s->meta.load_state != UNIT_LOADED) |
4f2d528d LP |
3132 | return -EINVAL; |
3133 | ||
3134 | if (s->socket_fd >= 0) | |
3135 | return -EBUSY; | |
3136 | ||
3137 | if (s->state != SERVICE_DEAD) | |
3138 | return -EAGAIN; | |
3139 | ||
3140 | s->socket_fd = fd; | |
701cc384 | 3141 | s->got_socket_fd = true; |
f976f3f6 | 3142 | s->accept_socket = sock; |
6cf6bbc2 | 3143 | |
4f2d528d LP |
3144 | return 0; |
3145 | } | |
3146 | ||
fdf20a31 | 3147 | static void service_reset_failed(Unit *u) { |
5632e374 LP |
3148 | Service *s = SERVICE(u); |
3149 | ||
3150 | assert(s); | |
3151 | ||
fdf20a31 | 3152 | if (s->state == SERVICE_FAILED) |
5632e374 LP |
3153 | service_set_state(s, SERVICE_DEAD); |
3154 | ||
3155 | s->failure = false; | |
3156 | } | |
3157 | ||
8a0867d6 LP |
3158 | static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) { |
3159 | Service *s = SERVICE(u); | |
3160 | int r = 0; | |
3161 | Set *pid_set = NULL; | |
3162 | ||
3163 | assert(s); | |
3164 | ||
3165 | if (s->main_pid <= 0 && who == KILL_MAIN) { | |
3166 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); | |
3167 | return -EINVAL; | |
3168 | } | |
3169 | ||
3170 | if (s->control_pid <= 0 && who == KILL_CONTROL) { | |
3171 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); | |
3172 | return -ENOENT; | |
3173 | } | |
3174 | ||
3175 | if (s->control_pid > 0) | |
3176 | if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0) | |
3177 | r = -errno; | |
3178 | ||
3179 | if (s->main_pid > 0) | |
3180 | if (kill(mode == KILL_PROCESS_GROUP ? -s->main_pid : s->main_pid, signo) < 0) | |
3181 | r = -errno; | |
3182 | ||
3183 | if (mode == KILL_CONTROL_GROUP) { | |
3184 | int q; | |
3185 | ||
3186 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) | |
3187 | return -ENOMEM; | |
3188 | ||
3189 | /* Exclude the control/main pid from being killed via the cgroup */ | |
3190 | if (s->control_pid > 0) | |
3191 | if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) { | |
3192 | r = q; | |
3193 | goto finish; | |
3194 | } | |
3195 | ||
3196 | if (s->main_pid > 0) | |
3197 | if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) { | |
3198 | r = q; | |
3199 | goto finish; | |
3200 | } | |
3201 | ||
3202 | if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, pid_set)) < 0) | |
3203 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
3204 | r = q; | |
3205 | } | |
3206 | ||
3207 | finish: | |
3208 | if (pid_set) | |
3209 | set_free(pid_set); | |
3210 | ||
3211 | return r; | |
3212 | } | |
3213 | ||
94f04347 LP |
3214 | static const char* const service_state_table[_SERVICE_STATE_MAX] = { |
3215 | [SERVICE_DEAD] = "dead", | |
3216 | [SERVICE_START_PRE] = "start-pre", | |
3217 | [SERVICE_START] = "start", | |
3218 | [SERVICE_START_POST] = "start-post", | |
3219 | [SERVICE_RUNNING] = "running", | |
80876c20 | 3220 | [SERVICE_EXITED] = "exited", |
94f04347 LP |
3221 | [SERVICE_RELOAD] = "reload", |
3222 | [SERVICE_STOP] = "stop", | |
3223 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", | |
3224 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
3225 | [SERVICE_STOP_POST] = "stop-post", | |
3226 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
3227 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 3228 | [SERVICE_FAILED] = "failed", |
94f04347 LP |
3229 | [SERVICE_AUTO_RESTART] = "auto-restart", |
3230 | }; | |
3231 | ||
3232 | DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); | |
3233 | ||
3234 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { | |
525ee6f4 LP |
3235 | [SERVICE_RESTART_NO] = "no", |
3236 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
50caaedb LP |
3237 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", |
3238 | [SERVICE_RESTART_ON_ABORT] = "on-abort", | |
3239 | [SERVICE_RESTART_ALWAYS] = "always" | |
94f04347 LP |
3240 | }; |
3241 | ||
3242 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
3243 | ||
3244 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
94f04347 | 3245 | [SERVICE_SIMPLE] = "simple", |
0d624a78 | 3246 | [SERVICE_FORKING] = "forking", |
34e9ba66 | 3247 | [SERVICE_ONESHOT] = "oneshot", |
8c47c732 LP |
3248 | [SERVICE_DBUS] = "dbus", |
3249 | [SERVICE_NOTIFY] = "notify" | |
94f04347 LP |
3250 | }; |
3251 | ||
3252 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
3253 | ||
e537352b | 3254 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
94f04347 LP |
3255 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", |
3256 | [SERVICE_EXEC_START] = "ExecStart", | |
3257 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
3258 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
3259 | [SERVICE_EXEC_STOP] = "ExecStop", | |
3260 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
3261 | }; | |
3262 | ||
3263 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
3264 | ||
c952c6ec LP |
3265 | static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = { |
3266 | [NOTIFY_NONE] = "none", | |
3267 | [NOTIFY_MAIN] = "main", | |
3268 | [NOTIFY_ALL] = "all" | |
3269 | }; | |
3270 | ||
3271 | DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess); | |
3272 | ||
87f0e418 | 3273 | const UnitVTable service_vtable = { |
5cb5a6ff | 3274 | .suffix = ".service", |
9e58ff9c | 3275 | .show_status = true, |
5cb5a6ff | 3276 | |
034c6ed7 LP |
3277 | .init = service_init, |
3278 | .done = service_done, | |
a16e1123 LP |
3279 | .load = service_load, |
3280 | ||
3281 | .coldplug = service_coldplug, | |
034c6ed7 | 3282 | |
5cb5a6ff LP |
3283 | .dump = service_dump, |
3284 | ||
3285 | .start = service_start, | |
3286 | .stop = service_stop, | |
3287 | .reload = service_reload, | |
3288 | ||
034c6ed7 LP |
3289 | .can_reload = service_can_reload, |
3290 | ||
8a0867d6 LP |
3291 | .kill = service_kill, |
3292 | ||
a16e1123 LP |
3293 | .serialize = service_serialize, |
3294 | .deserialize_item = service_deserialize_item, | |
3295 | ||
5cb5a6ff | 3296 | .active_state = service_active_state, |
10a94420 | 3297 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 3298 | |
07459bb6 | 3299 | #ifdef HAVE_SYSV_COMPAT |
701cc384 | 3300 | .check_gc = service_check_gc, |
07459bb6 | 3301 | #endif |
701cc384 LP |
3302 | .check_snapshot = service_check_snapshot, |
3303 | ||
034c6ed7 LP |
3304 | .sigchld_event = service_sigchld_event, |
3305 | .timer_event = service_timer_event, | |
2c4104f0 | 3306 | |
fdf20a31 | 3307 | .reset_failed = service_reset_failed, |
5632e374 | 3308 | |
8e274523 | 3309 | .cgroup_notify_empty = service_cgroup_notify_event, |
8c47c732 | 3310 | .notify_message = service_notify_message, |
8e274523 | 3311 | |
05e343b7 LP |
3312 | .bus_name_owner_change = service_bus_name_owner_change, |
3313 | .bus_query_pid_done = service_bus_query_pid_done, | |
3314 | ||
c4e2ceae | 3315 | .bus_interface = "org.freedesktop.systemd1.Service", |
4139c1b2 | 3316 | .bus_message_handler = bus_service_message_handler, |
c4e2ceae | 3317 | .bus_invalidating_properties = bus_service_invalidating_properties, |
4139c1b2 | 3318 | |
07459bb6 | 3319 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 | 3320 | .enumerate = service_enumerate |
07459bb6 | 3321 | #endif |
5cb5a6ff | 3322 | }; |