]>
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 | |
d4054675 | 68 | #ifdef TARGET_DEBIAN |
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 */ | |
3177a49c | 277 | #ifdef TARGET_DEBIAN |
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 LP |
318 | /* If we don't know this name, fallback heuristics to figure |
319 | * out whether something is a target or an service alias. */ | |
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 | |
243b1432 LP |
932 | static int service_verify(Service *s) { |
933 | assert(s); | |
934 | ||
4cd1fbcc | 935 | if (s->meta.load_state != UNIT_LOADED) |
243b1432 LP |
936 | return 0; |
937 | ||
938 | if (!s->exec_command[SERVICE_EXEC_START]) { | |
4cd1fbcc | 939 | log_error("%s lacks ExecStart setting. Refusing.", s->meta.id); |
243b1432 LP |
940 | return -EINVAL; |
941 | } | |
942 | ||
34e9ba66 LP |
943 | if (s->type != SERVICE_ONESHOT && |
944 | s->exec_command[SERVICE_EXEC_START]->command_next) { | |
945 | log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id); | |
6cf6bbc2 LP |
946 | return -EINVAL; |
947 | } | |
948 | ||
05e343b7 | 949 | if (s->type == SERVICE_DBUS && !s->bus_name) { |
4d0e5dbd LP |
950 | log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id); |
951 | return -EINVAL; | |
952 | } | |
953 | ||
2e22afe9 | 954 | if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) { |
4d0e5dbd | 955 | log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id); |
05e343b7 LP |
956 | return -EINVAL; |
957 | } | |
958 | ||
243b1432 LP |
959 | return 0; |
960 | } | |
961 | ||
a40eb732 LP |
962 | static int service_add_default_dependencies(Service *s) { |
963 | int r; | |
964 | ||
965 | assert(s); | |
966 | ||
967 | /* Add a number of automatic dependencies useful for the | |
968 | * majority of services. */ | |
969 | ||
970 | /* First, pull in base system */ | |
971 | if (s->meta.manager->running_as == MANAGER_SYSTEM) { | |
972 | ||
973 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0) | |
974 | return r; | |
975 | ||
976 | } else if (s->meta.manager->running_as == MANAGER_SESSION) { | |
977 | ||
978 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0) | |
979 | return r; | |
980 | } | |
981 | ||
982 | /* Second, activate normal shutdown */ | |
69dd2852 | 983 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true); |
a40eb732 LP |
984 | } |
985 | ||
e537352b LP |
986 | static int service_load(Unit *u) { |
987 | int r; | |
988 | Service *s = SERVICE(u); | |
989 | ||
990 | assert(s); | |
1e2e8133 | 991 | |
5cb5a6ff | 992 | /* Load a .service file */ |
e537352b | 993 | if ((r = unit_load_fragment(u)) < 0) |
5cb5a6ff LP |
994 | return r; |
995 | ||
07459bb6 | 996 | #ifdef HAVE_SYSV_COMPAT |
bd77d0fc | 997 | /* Load a classic init script as a fallback, if we couldn't find anything */ |
e537352b LP |
998 | if (u->meta.load_state == UNIT_STUB) |
999 | if ((r = service_load_sysv(s)) < 0) | |
23a177ef | 1000 | return r; |
07459bb6 | 1001 | #endif |
d46de8a1 | 1002 | |
23a177ef | 1003 | /* Still nothing found? Then let's give up */ |
e537352b | 1004 | if (u->meta.load_state == UNIT_STUB) |
23a177ef | 1005 | return -ENOENT; |
034c6ed7 | 1006 | |
23a177ef LP |
1007 | /* We were able to load something, then let's add in the |
1008 | * dropin directories. */ | |
1009 | if ((r = unit_load_dropin(unit_follow_merge(u))) < 0) | |
8e274523 | 1010 | return r; |
23a177ef LP |
1011 | |
1012 | /* This is a new unit? Then let's add in some extras */ | |
e537352b | 1013 | if (u->meta.load_state == UNIT_LOADED) { |
23a177ef LP |
1014 | if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) |
1015 | return r; | |
1016 | ||
1017 | if ((r = unit_add_default_cgroup(u)) < 0) | |
1018 | return r; | |
1019 | ||
07459bb6 | 1020 | #ifdef HAVE_SYSV_COMPAT |
56d748b4 | 1021 | if ((r = sysv_fix_order(s)) < 0) |
23a177ef | 1022 | return r; |
07459bb6 | 1023 | #endif |
05e343b7 | 1024 | |
ee0dd802 | 1025 | if (s->bus_name) |
05e343b7 | 1026 | if ((r = unit_watch_bus_name(u, s->bus_name)) < 0) |
a40eb732 | 1027 | return r; |
c952c6ec LP |
1028 | |
1029 | if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE) | |
1030 | s->notify_access = NOTIFY_MAIN; | |
a40eb732 LP |
1031 | |
1032 | if (s->type == SERVICE_DBUS || s->bus_name) | |
1033 | if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0) | |
1034 | return r; | |
1035 | ||
1036 | if (s->meta.default_dependencies) | |
1037 | if ((r = service_add_default_dependencies(s)) < 0) | |
1038 | return r; | |
8e274523 LP |
1039 | } |
1040 | ||
243b1432 | 1041 | return service_verify(s); |
034c6ed7 LP |
1042 | } |
1043 | ||
87f0e418 | 1044 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 1045 | |
5cb5a6ff | 1046 | ServiceExecCommand c; |
87f0e418 | 1047 | Service *s = SERVICE(u); |
47be870b LP |
1048 | const char *prefix2; |
1049 | char *p2; | |
5cb5a6ff LP |
1050 | |
1051 | assert(s); | |
1052 | ||
47be870b LP |
1053 | p2 = strappend(prefix, "\t"); |
1054 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e | 1055 | |
5cb5a6ff | 1056 | fprintf(f, |
81a2b7ce LP |
1057 | "%sService State: %s\n" |
1058 | "%sPermissionsStartOnly: %s\n" | |
8e274523 | 1059 | "%sRootDirectoryStartOnly: %s\n" |
02ee865a | 1060 | "%sRemainAfterExit: %s\n" |
c952c6ec | 1061 | "%sType: %s\n" |
2cf3143a | 1062 | "%sRestart: %s\n" |
c952c6ec | 1063 | "%sNotifyAccess: %s\n", |
81a2b7ce LP |
1064 | prefix, service_state_to_string(s->state), |
1065 | prefix, yes_no(s->permissions_start_only), | |
8e274523 | 1066 | prefix, yes_no(s->root_directory_start_only), |
02ee865a | 1067 | prefix, yes_no(s->remain_after_exit), |
c952c6ec | 1068 | prefix, service_type_to_string(s->type), |
2cf3143a | 1069 | prefix, service_restart_to_string(s->restart), |
c952c6ec | 1070 | prefix, notify_access_to_string(s->notify_access)); |
5cb5a6ff | 1071 | |
70123e68 LP |
1072 | if (s->control_pid > 0) |
1073 | fprintf(f, | |
bb00e604 LP |
1074 | "%sControl PID: %lu\n", |
1075 | prefix, (unsigned long) s->control_pid); | |
70123e68 LP |
1076 | |
1077 | if (s->main_pid > 0) | |
1078 | fprintf(f, | |
bb00e604 LP |
1079 | "%sMain PID: %lu\n", |
1080 | prefix, (unsigned long) s->main_pid); | |
70123e68 | 1081 | |
034c6ed7 LP |
1082 | if (s->pid_file) |
1083 | fprintf(f, | |
1084 | "%sPIDFile: %s\n", | |
1085 | prefix, s->pid_file); | |
1086 | ||
05e343b7 LP |
1087 | if (s->bus_name) |
1088 | fprintf(f, | |
1089 | "%sBusName: %s\n" | |
1090 | "%sBus Name Good: %s\n", | |
1091 | prefix, s->bus_name, | |
1092 | prefix, yes_no(s->bus_name_good)); | |
1093 | ||
5cb5a6ff LP |
1094 | exec_context_dump(&s->exec_context, f, prefix); |
1095 | ||
e537352b | 1096 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
5cb5a6ff | 1097 | |
44d8db9e LP |
1098 | if (!s->exec_command[c]) |
1099 | continue; | |
1100 | ||
40d50879 | 1101 | fprintf(f, "%s-> %s:\n", |
94f04347 | 1102 | prefix, service_exec_command_to_string(c)); |
44d8db9e LP |
1103 | |
1104 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 1105 | } |
44d8db9e | 1106 | |
07459bb6 | 1107 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 LP |
1108 | if (s->sysv_path) |
1109 | fprintf(f, | |
23a177ef LP |
1110 | "%sSysV Init Script Path: %s\n" |
1111 | "%sSysV Init Script has LSB Header: %s\n", | |
1112 | prefix, s->sysv_path, | |
1113 | prefix, yes_no(s->sysv_has_lsb)); | |
2c4104f0 LP |
1114 | |
1115 | if (s->sysv_start_priority >= 0) | |
1116 | fprintf(f, | |
5de6b302 LP |
1117 | "%sSysVStartPriority: %i\n" |
1118 | "%sSysVEnabled: %s\n", | |
1119 | prefix, s->sysv_start_priority, | |
1120 | prefix, yes_no(s->sysv_enabled)); | |
2c4104f0 | 1121 | |
8309400a LP |
1122 | if (s->sysv_runlevels) |
1123 | fprintf(f, "%sSysVRunLevels: %s\n", | |
1124 | prefix, s->sysv_runlevels); | |
07459bb6 | 1125 | #endif |
23a177ef | 1126 | |
8c47c732 LP |
1127 | if (s->status_text) |
1128 | fprintf(f, "%sStatus Text: %s\n", | |
1129 | prefix, s->status_text); | |
1130 | ||
47be870b | 1131 | free(p2); |
5cb5a6ff LP |
1132 | } |
1133 | ||
034c6ed7 LP |
1134 | static int service_load_pid_file(Service *s) { |
1135 | char *k; | |
034c6ed7 | 1136 | int r; |
5925dd3c | 1137 | pid_t pid; |
034c6ed7 LP |
1138 | |
1139 | assert(s); | |
1140 | ||
1141 | if (s->main_pid_known) | |
1142 | return 0; | |
1143 | ||
5e94833f LP |
1144 | assert(s->main_pid <= 0); |
1145 | ||
034c6ed7 LP |
1146 | if (!s->pid_file) |
1147 | return -ENOENT; | |
1148 | ||
1149 | if ((r = read_one_line_file(s->pid_file, &k)) < 0) | |
1150 | return r; | |
1151 | ||
5925dd3c LP |
1152 | r = parse_pid(k, &pid); |
1153 | free(k); | |
034c6ed7 | 1154 | |
5925dd3c LP |
1155 | if (r < 0) |
1156 | return r; | |
406eaf93 | 1157 | |
5925dd3c LP |
1158 | if (kill(pid, 0) < 0 && errno != EPERM) { |
1159 | log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.", | |
1160 | (unsigned long) pid, s->pid_file); | |
b8c597d5 LP |
1161 | return -ESRCH; |
1162 | } | |
1163 | ||
5925dd3c | 1164 | if ((r = service_set_main_pid(s, pid)) < 0) |
16f6025e LP |
1165 | return r; |
1166 | ||
5925dd3c LP |
1167 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
1168 | /* FIXME: we need to do something here */ | |
1169 | return r; | |
034c6ed7 LP |
1170 | |
1171 | return 0; | |
1172 | } | |
1173 | ||
3e33402a LP |
1174 | static int service_get_sockets(Service *s, Set **_set) { |
1175 | Set *set; | |
ceee3d82 LP |
1176 | Iterator i; |
1177 | char *t; | |
3e33402a | 1178 | int r; |
ceee3d82 LP |
1179 | |
1180 | assert(s); | |
3e33402a LP |
1181 | assert(_set); |
1182 | ||
6cf6bbc2 LP |
1183 | if (s->socket_fd >= 0) |
1184 | return 0; | |
1185 | ||
f976f3f6 LP |
1186 | if (!set_isempty(s->configured_sockets)) |
1187 | return 0; | |
1188 | ||
3e33402a LP |
1189 | /* Collects all Socket objects that belong to this |
1190 | * service. Note that a service might have multiple sockets | |
1191 | * via multiple names. */ | |
1192 | ||
1193 | if (!(set = set_new(NULL, NULL))) | |
1194 | return -ENOMEM; | |
ceee3d82 | 1195 | |
4cd1fbcc | 1196 | SET_FOREACH(t, s->meta.names, i) { |
ceee3d82 LP |
1197 | char *k; |
1198 | Unit *p; | |
1199 | ||
1200 | /* Look for all socket objects that go by any of our | |
1201 | * units and collect their fds */ | |
1202 | ||
3e33402a LP |
1203 | if (!(k = unit_name_change_suffix(t, ".socket"))) { |
1204 | r = -ENOMEM; | |
1205 | goto fail; | |
1206 | } | |
ceee3d82 | 1207 | |
4cd1fbcc | 1208 | p = manager_get_unit(s->meta.manager, k); |
ceee3d82 LP |
1209 | free(k); |
1210 | ||
8d567588 LP |
1211 | if (!p) |
1212 | continue; | |
ceee3d82 | 1213 | |
3e33402a LP |
1214 | if ((r = set_put(set, p)) < 0) |
1215 | goto fail; | |
ceee3d82 LP |
1216 | } |
1217 | ||
3e33402a LP |
1218 | *_set = set; |
1219 | return 0; | |
1220 | ||
1221 | fail: | |
1222 | set_free(set); | |
1223 | return r; | |
1224 | } | |
1225 | ||
e537352b | 1226 | static int service_notify_sockets_dead(Service *s) { |
3e33402a | 1227 | Iterator i; |
f976f3f6 | 1228 | Set *set, *free_set = NULL; |
47be870b | 1229 | Socket *sock; |
3e33402a LP |
1230 | int r; |
1231 | ||
1232 | assert(s); | |
1233 | ||
f976f3f6 LP |
1234 | /* Notifies all our sockets when we die */ |
1235 | ||
6cf6bbc2 LP |
1236 | if (s->socket_fd >= 0) |
1237 | return 0; | |
1238 | ||
f976f3f6 LP |
1239 | if (!set_isempty(s->configured_sockets)) |
1240 | set = s->configured_sockets; | |
1241 | else { | |
1242 | if ((r = service_get_sockets(s, &free_set)) < 0) | |
1243 | return r; | |
1244 | ||
1245 | set = free_set; | |
1246 | } | |
3e33402a | 1247 | |
47be870b LP |
1248 | SET_FOREACH(sock, set, i) |
1249 | socket_notify_service_dead(sock); | |
3e33402a | 1250 | |
f976f3f6 | 1251 | set_free(free_set); |
3e33402a | 1252 | |
ceee3d82 LP |
1253 | return 0; |
1254 | } | |
1255 | ||
034c6ed7 LP |
1256 | static void service_set_state(Service *s, ServiceState state) { |
1257 | ServiceState old_state; | |
5cb5a6ff LP |
1258 | assert(s); |
1259 | ||
034c6ed7 | 1260 | old_state = s->state; |
5cb5a6ff | 1261 | s->state = state; |
034c6ed7 LP |
1262 | |
1263 | if (state != SERVICE_START_PRE && | |
1264 | state != SERVICE_START && | |
1265 | state != SERVICE_START_POST && | |
1266 | state != SERVICE_RELOAD && | |
1267 | state != SERVICE_STOP && | |
1268 | state != SERVICE_STOP_SIGTERM && | |
1269 | state != SERVICE_STOP_SIGKILL && | |
1270 | state != SERVICE_STOP_POST && | |
1271 | state != SERVICE_FINAL_SIGTERM && | |
1272 | state != SERVICE_FINAL_SIGKILL && | |
1273 | state != SERVICE_AUTO_RESTART) | |
acbb0225 | 1274 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 | 1275 | |
7d55e835 LP |
1276 | if (state != SERVICE_START && |
1277 | state != SERVICE_START_POST && | |
034c6ed7 LP |
1278 | state != SERVICE_RUNNING && |
1279 | state != SERVICE_RELOAD && | |
1280 | state != SERVICE_STOP && | |
1281 | state != SERVICE_STOP_SIGTERM && | |
1282 | state != SERVICE_STOP_SIGKILL) | |
5e94833f | 1283 | service_unwatch_main_pid(s); |
034c6ed7 LP |
1284 | |
1285 | if (state != SERVICE_START_PRE && | |
1286 | state != SERVICE_START && | |
1287 | state != SERVICE_START_POST && | |
1288 | state != SERVICE_RELOAD && | |
1289 | state != SERVICE_STOP && | |
1290 | state != SERVICE_STOP_SIGTERM && | |
1291 | state != SERVICE_STOP_SIGKILL && | |
1292 | state != SERVICE_STOP_POST && | |
1293 | state != SERVICE_FINAL_SIGTERM && | |
e537352b | 1294 | state != SERVICE_FINAL_SIGKILL) { |
5e94833f | 1295 | service_unwatch_control_pid(s); |
034c6ed7 | 1296 | s->control_command = NULL; |
a16e1123 | 1297 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
e537352b | 1298 | } |
034c6ed7 | 1299 | |
ceee3d82 LP |
1300 | if (state == SERVICE_DEAD || |
1301 | state == SERVICE_STOP || | |
1302 | state == SERVICE_STOP_SIGTERM || | |
1303 | state == SERVICE_STOP_SIGKILL || | |
1304 | state == SERVICE_STOP_POST || | |
1305 | state == SERVICE_FINAL_SIGTERM || | |
1306 | state == SERVICE_FINAL_SIGKILL || | |
fdf20a31 | 1307 | state == SERVICE_FAILED || |
ceee3d82 | 1308 | state == SERVICE_AUTO_RESTART) |
e537352b | 1309 | service_notify_sockets_dead(s); |
ceee3d82 | 1310 | |
4f2d528d LP |
1311 | if (state != SERVICE_START_PRE && |
1312 | state != SERVICE_START && | |
6cf6bbc2 LP |
1313 | state != SERVICE_START_POST && |
1314 | state != SERVICE_RUNNING && | |
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 && | |
4cd1fbcc | 1322 | !(state == SERVICE_DEAD && s->meta.job)) { |
4f2d528d | 1323 | service_close_socket_fd(s); |
6cf6bbc2 LP |
1324 | service_connection_unref(s); |
1325 | } | |
4f2d528d | 1326 | |
f6023656 LP |
1327 | /* For the inactive states unit_notify() will trim the cgroup, |
1328 | * but for exit we have to do that ourselves... */ | |
1329 | if (state == SERVICE_EXITED) | |
1330 | cgroup_bonding_trim_list(s->meta.cgroup_bondings, true); | |
1331 | ||
e537352b | 1332 | if (old_state != state) |
4cd1fbcc | 1333 | log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state)); |
acbb0225 LP |
1334 | |
1335 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); | |
034c6ed7 LP |
1336 | } |
1337 | ||
a16e1123 LP |
1338 | static int service_coldplug(Unit *u) { |
1339 | Service *s = SERVICE(u); | |
1340 | int r; | |
1341 | ||
1342 | assert(s); | |
1343 | assert(s->state == SERVICE_DEAD); | |
1344 | ||
1345 | if (s->deserialized_state != s->state) { | |
1346 | ||
1347 | if (s->deserialized_state == SERVICE_START_PRE || | |
1348 | s->deserialized_state == SERVICE_START || | |
1349 | s->deserialized_state == SERVICE_START_POST || | |
1350 | s->deserialized_state == SERVICE_RELOAD || | |
1351 | s->deserialized_state == SERVICE_STOP || | |
1352 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1353 | s->deserialized_state == SERVICE_STOP_SIGKILL || | |
1354 | s->deserialized_state == SERVICE_STOP_POST || | |
1355 | s->deserialized_state == SERVICE_FINAL_SIGTERM || | |
1356 | s->deserialized_state == SERVICE_FINAL_SIGKILL || | |
e558336f LP |
1357 | s->deserialized_state == SERVICE_AUTO_RESTART) { |
1358 | ||
1359 | if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) { | |
1360 | usec_t k; | |
1361 | ||
1362 | k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec; | |
1363 | ||
1364 | if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0) | |
1365 | return r; | |
1366 | } | |
1367 | } | |
a16e1123 LP |
1368 | |
1369 | if ((s->deserialized_state == SERVICE_START && | |
1370 | (s->type == SERVICE_FORKING || | |
8c47c732 | 1371 | s->type == SERVICE_DBUS || |
34e9ba66 | 1372 | s->type == SERVICE_ONESHOT || |
8c47c732 | 1373 | s->type == SERVICE_NOTIFY)) || |
a16e1123 LP |
1374 | s->deserialized_state == SERVICE_START_POST || |
1375 | s->deserialized_state == SERVICE_RUNNING || | |
1376 | s->deserialized_state == SERVICE_RELOAD || | |
1377 | s->deserialized_state == SERVICE_STOP || | |
1378 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1379 | s->deserialized_state == SERVICE_STOP_SIGKILL) | |
1380 | if (s->main_pid > 0) | |
1381 | if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0) | |
1382 | return r; | |
1383 | ||
1384 | if (s->deserialized_state == SERVICE_START_PRE || | |
1385 | s->deserialized_state == SERVICE_START || | |
1386 | s->deserialized_state == SERVICE_START_POST || | |
1387 | s->deserialized_state == SERVICE_RELOAD || | |
1388 | s->deserialized_state == SERVICE_STOP || | |
1389 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1390 | s->deserialized_state == SERVICE_STOP_SIGKILL || | |
1391 | s->deserialized_state == SERVICE_STOP_POST || | |
1392 | s->deserialized_state == SERVICE_FINAL_SIGTERM || | |
1393 | s->deserialized_state == SERVICE_FINAL_SIGKILL) | |
1394 | if (s->control_pid > 0) | |
1395 | if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0) | |
1396 | return r; | |
1397 | ||
1398 | service_set_state(s, s->deserialized_state); | |
1399 | } | |
1400 | ||
1401 | return 0; | |
1402 | } | |
1403 | ||
44d8db9e LP |
1404 | static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { |
1405 | Iterator i; | |
1406 | int r; | |
1407 | int *rfds = NULL; | |
1408 | unsigned rn_fds = 0; | |
f976f3f6 | 1409 | Set *set, *free_set = NULL; |
47be870b | 1410 | Socket *sock; |
44d8db9e LP |
1411 | |
1412 | assert(s); | |
1413 | assert(fds); | |
1414 | assert(n_fds); | |
1415 | ||
6cf6bbc2 LP |
1416 | if (s->socket_fd >= 0) |
1417 | return 0; | |
1418 | ||
f976f3f6 LP |
1419 | if (!set_isempty(s->configured_sockets)) |
1420 | set = s->configured_sockets; | |
1421 | else { | |
1422 | if ((r = service_get_sockets(s, &free_set)) < 0) | |
1423 | return r; | |
1424 | ||
1425 | set = free_set; | |
1426 | } | |
3e33402a | 1427 | |
47be870b | 1428 | SET_FOREACH(sock, set, i) { |
44d8db9e LP |
1429 | int *cfds; |
1430 | unsigned cn_fds; | |
1431 | ||
47be870b | 1432 | if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0) |
44d8db9e LP |
1433 | goto fail; |
1434 | ||
1435 | if (!cfds) | |
1436 | continue; | |
1437 | ||
1438 | if (!rfds) { | |
1439 | rfds = cfds; | |
1440 | rn_fds = cn_fds; | |
1441 | } else { | |
1442 | int *t; | |
1443 | ||
1444 | if (!(t = new(int, rn_fds+cn_fds))) { | |
1445 | free(cfds); | |
1446 | r = -ENOMEM; | |
1447 | goto fail; | |
1448 | } | |
1449 | ||
1450 | memcpy(t, rfds, rn_fds); | |
1451 | memcpy(t+rn_fds, cfds, cn_fds); | |
1452 | free(rfds); | |
1453 | free(cfds); | |
1454 | ||
1455 | rfds = t; | |
1456 | rn_fds = rn_fds+cn_fds; | |
1457 | } | |
1458 | } | |
1459 | ||
1460 | *fds = rfds; | |
1461 | *n_fds = rn_fds; | |
3e33402a | 1462 | |
f976f3f6 | 1463 | set_free(free_set); |
3e33402a | 1464 | |
44d8db9e LP |
1465 | return 0; |
1466 | ||
1467 | fail: | |
3e33402a | 1468 | set_free(set); |
44d8db9e | 1469 | free(rfds); |
3e33402a | 1470 | |
44d8db9e LP |
1471 | return r; |
1472 | } | |
1473 | ||
81a2b7ce LP |
1474 | static int service_spawn( |
1475 | Service *s, | |
1476 | ExecCommand *c, | |
1477 | bool timeout, | |
1478 | bool pass_fds, | |
1479 | bool apply_permissions, | |
1480 | bool apply_chroot, | |
1e3ad081 | 1481 | bool apply_tty_stdin, |
c952c6ec | 1482 | bool set_notify_socket, |
81a2b7ce LP |
1483 | pid_t *_pid) { |
1484 | ||
034c6ed7 LP |
1485 | pid_t pid; |
1486 | int r; | |
6cf6bbc2 | 1487 | int *fds = NULL, *fdsbuf = NULL; |
2105e76a LP |
1488 | unsigned n_fds = 0, n_env = 0; |
1489 | char **argv = NULL, **final_env = NULL, **our_env = NULL; | |
034c6ed7 LP |
1490 | |
1491 | assert(s); | |
1492 | assert(c); | |
1493 | assert(_pid); | |
1494 | ||
6cf6bbc2 LP |
1495 | if (pass_fds || |
1496 | s->exec_context.std_input == EXEC_INPUT_SOCKET || | |
1497 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1498 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1499 | ||
4f2d528d LP |
1500 | if (s->socket_fd >= 0) { |
1501 | fds = &s->socket_fd; | |
1502 | n_fds = 1; | |
6cf6bbc2 LP |
1503 | } else { |
1504 | if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0) | |
1505 | goto fail; | |
1506 | ||
1507 | fds = fdsbuf; | |
1508 | } | |
4f2d528d | 1509 | } |
44d8db9e | 1510 | |
e558336f | 1511 | if (timeout && s->timeout_usec) { |
acbb0225 | 1512 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
034c6ed7 LP |
1513 | goto fail; |
1514 | } else | |
acbb0225 | 1515 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 | 1516 | |
9e2f7c11 LP |
1517 | if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) { |
1518 | r = -ENOMEM; | |
1519 | goto fail; | |
1520 | } | |
1521 | ||
2105e76a LP |
1522 | if (!(our_env = new0(char*, 3))) { |
1523 | r = -ENOMEM; | |
1524 | goto fail; | |
1525 | } | |
c952c6ec | 1526 | |
2105e76a LP |
1527 | if (set_notify_socket) |
1528 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) { | |
c952c6ec LP |
1529 | r = -ENOMEM; |
1530 | goto fail; | |
1531 | } | |
1532 | ||
2105e76a LP |
1533 | if (s->main_pid > 0) |
1534 | if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) { | |
c952c6ec LP |
1535 | r = -ENOMEM; |
1536 | goto fail; | |
1537 | } | |
2105e76a LP |
1538 | |
1539 | if (!(final_env = strv_env_merge(2, | |
1540 | s->meta.manager->environment, | |
1541 | our_env, | |
1542 | NULL))) { | |
1543 | r = -ENOMEM; | |
1544 | goto fail; | |
1545 | } | |
c952c6ec | 1546 | |
9e2f7c11 LP |
1547 | r = exec_spawn(c, |
1548 | argv, | |
1549 | &s->exec_context, | |
1550 | fds, n_fds, | |
2105e76a | 1551 | final_env, |
9e2f7c11 LP |
1552 | apply_permissions, |
1553 | apply_chroot, | |
1e3ad081 | 1554 | apply_tty_stdin, |
4cd1fbcc LP |
1555 | s->meta.manager->confirm_spawn, |
1556 | s->meta.cgroup_bondings, | |
9e2f7c11 LP |
1557 | &pid); |
1558 | ||
9e2f7c11 | 1559 | if (r < 0) |
034c6ed7 LP |
1560 | goto fail; |
1561 | ||
4f2d528d | 1562 | |
87f0e418 | 1563 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
034c6ed7 LP |
1564 | /* FIXME: we need to do something here */ |
1565 | goto fail; | |
1566 | ||
2105e76a LP |
1567 | free(fdsbuf); |
1568 | strv_free(argv); | |
1569 | strv_free(our_env); | |
1570 | strv_free(final_env); | |
1571 | ||
034c6ed7 LP |
1572 | *_pid = pid; |
1573 | ||
5cb5a6ff | 1574 | return 0; |
034c6ed7 LP |
1575 | |
1576 | fail: | |
2105e76a | 1577 | free(fdsbuf); |
c952c6ec | 1578 | strv_free(argv); |
2105e76a LP |
1579 | strv_free(our_env); |
1580 | strv_free(final_env); | |
c952c6ec | 1581 | |
034c6ed7 | 1582 | if (timeout) |
acbb0225 | 1583 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 LP |
1584 | |
1585 | return r; | |
1586 | } | |
1587 | ||
80876c20 LP |
1588 | static int main_pid_good(Service *s) { |
1589 | assert(s); | |
1590 | ||
1591 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
1592 | * don't know */ | |
1593 | ||
1594 | /* If we know the pid file, then lets just check if it is | |
1595 | * still valid */ | |
1596 | if (s->main_pid_known) | |
1597 | return s->main_pid > 0; | |
1598 | ||
1599 | /* We don't know the pid */ | |
1600 | return -EAGAIN; | |
1601 | } | |
1602 | ||
1603 | static int control_pid_good(Service *s) { | |
1604 | assert(s); | |
1605 | ||
1606 | return s->control_pid > 0; | |
1607 | } | |
1608 | ||
1609 | static int cgroup_good(Service *s) { | |
1610 | int r; | |
1611 | ||
1612 | assert(s); | |
1613 | ||
4cd1fbcc | 1614 | if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0) |
80876c20 LP |
1615 | return r; |
1616 | ||
1617 | return !r; | |
1618 | } | |
1619 | ||
034c6ed7 LP |
1620 | static void service_enter_dead(Service *s, bool success, bool allow_restart) { |
1621 | int r; | |
1622 | assert(s); | |
1623 | ||
1624 | if (!success) | |
1625 | s->failure = true; | |
1626 | ||
1627 | if (allow_restart && | |
47342320 | 1628 | !s->forbid_restart && |
034c6ed7 | 1629 | (s->restart == SERVICE_RESTART_ALWAYS || |
50caaedb LP |
1630 | (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure) || |
1631 | (s->restart == SERVICE_RESTART_ON_FAILURE && s->failure) || | |
1632 | (s->restart == SERVICE_RESTART_ON_ABORT && s->failure && | |
1633 | (s->main_exec_status.code == CLD_KILLED || | |
1634 | s->main_exec_status.code == CLD_DUMPED)))) { | |
034c6ed7 | 1635 | |
acbb0225 | 1636 | if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0) |
034c6ed7 LP |
1637 | goto fail; |
1638 | ||
1639 | service_set_state(s, SERVICE_AUTO_RESTART); | |
1640 | } else | |
fdf20a31 | 1641 | service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD); |
034c6ed7 | 1642 | |
47342320 LP |
1643 | s->forbid_restart = false; |
1644 | ||
034c6ed7 LP |
1645 | return; |
1646 | ||
1647 | fail: | |
4cd1fbcc | 1648 | log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1649 | service_enter_dead(s, false, false); |
1650 | } | |
1651 | ||
1652 | static void service_enter_signal(Service *s, ServiceState state, bool success); | |
1653 | ||
1654 | static void service_enter_stop_post(Service *s, bool success) { | |
1655 | int r; | |
1656 | assert(s); | |
1657 | ||
1658 | if (!success) | |
1659 | s->failure = true; | |
1660 | ||
5e94833f LP |
1661 | service_unwatch_control_pid(s); |
1662 | ||
a16e1123 | 1663 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
80876c20 | 1664 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) { |
81a2b7ce LP |
1665 | if ((r = service_spawn(s, |
1666 | s->control_command, | |
1667 | true, | |
1668 | false, | |
1669 | !s->permissions_start_only, | |
1670 | !s->root_directory_start_only, | |
1e3ad081 | 1671 | true, |
c952c6ec | 1672 | false, |
81a2b7ce | 1673 | &s->control_pid)) < 0) |
034c6ed7 LP |
1674 | goto fail; |
1675 | ||
d6ea93e3 | 1676 | |
80876c20 LP |
1677 | service_set_state(s, SERVICE_STOP_POST); |
1678 | } else | |
1679 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, true); | |
034c6ed7 LP |
1680 | |
1681 | return; | |
1682 | ||
1683 | fail: | |
4cd1fbcc | 1684 | log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1685 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
1686 | } | |
1687 | ||
1688 | static void service_enter_signal(Service *s, ServiceState state, bool success) { | |
1689 | int r; | |
ca949c9d LP |
1690 | Set *pid_set = NULL; |
1691 | bool wait_for_exit = false; | |
034c6ed7 LP |
1692 | |
1693 | assert(s); | |
1694 | ||
1695 | if (!success) | |
1696 | s->failure = true; | |
1697 | ||
2e22afe9 LP |
1698 | if (s->exec_context.kill_mode != KILL_NONE) { |
1699 | int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL; | |
034c6ed7 | 1700 | |
ca949c9d LP |
1701 | if (s->main_pid > 0) { |
1702 | if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ? | |
1703 | -s->main_pid : | |
1704 | s->main_pid, sig) < 0 && errno != ESRCH) | |
034c6ed7 | 1705 | |
ca949c9d LP |
1706 | log_warning("Failed to kill main process %li: %m", (long) s->main_pid); |
1707 | else | |
1708 | wait_for_exit = true; | |
034c6ed7 LP |
1709 | } |
1710 | ||
ca949c9d LP |
1711 | if (s->control_pid > 0) { |
1712 | if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ? | |
1713 | -s->control_pid : | |
1714 | s->control_pid, sig) < 0 && errno != ESRCH) | |
80876c20 | 1715 | |
ca949c9d LP |
1716 | log_warning("Failed to kill control process %li: %m", (long) s->control_pid); |
1717 | else | |
1718 | wait_for_exit = true; | |
1719 | } | |
50159e6a | 1720 | |
ca949c9d | 1721 | if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) { |
50159e6a | 1722 | |
ca949c9d LP |
1723 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) { |
1724 | r = -ENOMEM; | |
50159e6a | 1725 | goto fail; |
ca949c9d LP |
1726 | } |
1727 | ||
1728 | /* Exclude the main/control pids from being killed via the cgroup */ | |
1729 | if (s->main_pid > 0) | |
1730 | if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) | |
1731 | goto fail; | |
1732 | ||
1733 | if (s->control_pid > 0) | |
1734 | if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) | |
1735 | goto fail; | |
1736 | ||
1737 | if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) { | |
1738 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
1739 | log_warning("Failed to kill control group: %s", strerror(-r)); | |
f5a50114 | 1740 | } else if (r > 0) |
ca949c9d LP |
1741 | wait_for_exit = true; |
1742 | ||
1743 | set_free(pid_set); | |
50159e6a | 1744 | } |
d6ea93e3 | 1745 | } |
034c6ed7 | 1746 | |
ca949c9d | 1747 | if (wait_for_exit) { |
e558336f LP |
1748 | if (s->timeout_usec > 0) |
1749 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
1750 | goto fail; | |
d6ea93e3 | 1751 | |
80876c20 LP |
1752 | service_set_state(s, state); |
1753 | } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) | |
1754 | service_enter_stop_post(s, true); | |
1755 | else | |
034c6ed7 LP |
1756 | service_enter_dead(s, true, true); |
1757 | ||
1758 | return; | |
1759 | ||
1760 | fail: | |
4cd1fbcc | 1761 | log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r)); |
034c6ed7 | 1762 | |
80876c20 | 1763 | if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) |
034c6ed7 LP |
1764 | service_enter_stop_post(s, false); |
1765 | else | |
1766 | service_enter_dead(s, false, true); | |
ca949c9d LP |
1767 | |
1768 | if (pid_set) | |
1769 | set_free(pid_set); | |
034c6ed7 LP |
1770 | } |
1771 | ||
1772 | static void service_enter_stop(Service *s, bool success) { | |
1773 | int r; | |
5925dd3c | 1774 | |
034c6ed7 LP |
1775 | assert(s); |
1776 | ||
1777 | if (!success) | |
1778 | s->failure = true; | |
1779 | ||
5e94833f LP |
1780 | service_unwatch_control_pid(s); |
1781 | ||
a16e1123 | 1782 | s->control_command_id = SERVICE_EXEC_STOP; |
80876c20 | 1783 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) { |
81a2b7ce LP |
1784 | if ((r = service_spawn(s, |
1785 | s->control_command, | |
1786 | true, | |
1787 | false, | |
1788 | !s->permissions_start_only, | |
1789 | !s->root_directory_start_only, | |
c952c6ec | 1790 | false, |
1e3ad081 | 1791 | false, |
e55224ca | 1792 | &s->control_pid)) < 0) |
034c6ed7 LP |
1793 | goto fail; |
1794 | ||
80876c20 LP |
1795 | service_set_state(s, SERVICE_STOP); |
1796 | } else | |
034c6ed7 LP |
1797 | service_enter_signal(s, SERVICE_STOP_SIGTERM, true); |
1798 | ||
1799 | return; | |
1800 | ||
1801 | fail: | |
4cd1fbcc | 1802 | log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1803 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); |
1804 | } | |
1805 | ||
80876c20 | 1806 | static void service_enter_running(Service *s, bool success) { |
4eab639f | 1807 | int main_pid_ok, cgroup_ok; |
80876c20 LP |
1808 | assert(s); |
1809 | ||
1810 | if (!success) | |
1811 | s->failure = true; | |
1812 | ||
4eab639f LP |
1813 | main_pid_ok = main_pid_good(s); |
1814 | cgroup_ok = cgroup_good(s); | |
1815 | ||
1816 | if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) && | |
05e343b7 | 1817 | (s->bus_name_good || s->type != SERVICE_DBUS)) |
80876c20 | 1818 | service_set_state(s, SERVICE_RUNNING); |
02ee865a | 1819 | else if (s->remain_after_exit) |
80876c20 LP |
1820 | service_set_state(s, SERVICE_EXITED); |
1821 | else | |
1822 | service_enter_stop(s, true); | |
1823 | } | |
1824 | ||
034c6ed7 LP |
1825 | static void service_enter_start_post(Service *s) { |
1826 | int r; | |
1827 | assert(s); | |
1828 | ||
5e94833f LP |
1829 | service_unwatch_control_pid(s); |
1830 | ||
a16e1123 | 1831 | s->control_command_id = SERVICE_EXEC_START_POST; |
80876c20 | 1832 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) { |
81a2b7ce LP |
1833 | if ((r = service_spawn(s, |
1834 | s->control_command, | |
1835 | true, | |
1836 | false, | |
1837 | !s->permissions_start_only, | |
1838 | !s->root_directory_start_only, | |
c952c6ec | 1839 | false, |
1e3ad081 | 1840 | false, |
e55224ca | 1841 | &s->control_pid)) < 0) |
034c6ed7 LP |
1842 | goto fail; |
1843 | ||
80876c20 LP |
1844 | service_set_state(s, SERVICE_START_POST); |
1845 | } else | |
1846 | service_enter_running(s, true); | |
034c6ed7 LP |
1847 | |
1848 | return; | |
1849 | ||
1850 | fail: | |
4cd1fbcc | 1851 | log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1852 | service_enter_stop(s, false); |
1853 | } | |
1854 | ||
1855 | static void service_enter_start(Service *s) { | |
1856 | pid_t pid; | |
1857 | int r; | |
1858 | ||
1859 | assert(s); | |
1860 | ||
1861 | assert(s->exec_command[SERVICE_EXEC_START]); | |
34e9ba66 | 1862 | assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT); |
034c6ed7 | 1863 | |
80876c20 LP |
1864 | if (s->type == SERVICE_FORKING) |
1865 | service_unwatch_control_pid(s); | |
1866 | else | |
1867 | service_unwatch_main_pid(s); | |
1868 | ||
34e9ba66 LP |
1869 | s->control_command_id = SERVICE_EXEC_START; |
1870 | s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
1871 | ||
81a2b7ce | 1872 | if ((r = service_spawn(s, |
34e9ba66 | 1873 | s->control_command, |
8c47c732 | 1874 | s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY, |
81a2b7ce LP |
1875 | true, |
1876 | true, | |
1877 | true, | |
1e3ad081 | 1878 | true, |
c952c6ec | 1879 | s->notify_access != NOTIFY_NONE, |
81a2b7ce | 1880 | &pid)) < 0) |
034c6ed7 LP |
1881 | goto fail; |
1882 | ||
1883 | if (s->type == SERVICE_SIMPLE) { | |
1884 | /* For simple services we immediately start | |
1885 | * the START_POST binaries. */ | |
1886 | ||
5925dd3c | 1887 | service_set_main_pid(s, pid); |
034c6ed7 LP |
1888 | service_enter_start_post(s); |
1889 | ||
1890 | } else if (s->type == SERVICE_FORKING) { | |
1891 | ||
1892 | /* For forking services we wait until the start | |
1893 | * process exited. */ | |
1894 | ||
e55224ca | 1895 | s->control_pid = pid; |
80876c20 LP |
1896 | service_set_state(s, SERVICE_START); |
1897 | ||
34e9ba66 | 1898 | } else if (s->type == SERVICE_ONESHOT || |
8c47c732 LP |
1899 | s->type == SERVICE_DBUS || |
1900 | s->type == SERVICE_NOTIFY) { | |
7d55e835 | 1901 | |
34e9ba66 | 1902 | /* For oneshot services we wait until the start |
7d55e835 LP |
1903 | * process exited, too, but it is our main process. */ |
1904 | ||
05e343b7 | 1905 | /* For D-Bus services we know the main pid right away, |
8c47c732 LP |
1906 | * but wait for the bus name to appear on the |
1907 | * bus. Notify services are similar. */ | |
05e343b7 | 1908 | |
5925dd3c | 1909 | service_set_main_pid(s, pid); |
80876c20 | 1910 | service_set_state(s, SERVICE_START); |
034c6ed7 LP |
1911 | } else |
1912 | assert_not_reached("Unknown service type"); | |
1913 | ||
1914 | return; | |
1915 | ||
1916 | fail: | |
4cd1fbcc | 1917 | log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r)); |
80876c20 | 1918 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
034c6ed7 LP |
1919 | } |
1920 | ||
1921 | static void service_enter_start_pre(Service *s) { | |
1922 | int r; | |
1923 | ||
1924 | assert(s); | |
1925 | ||
5e94833f LP |
1926 | service_unwatch_control_pid(s); |
1927 | ||
a16e1123 | 1928 | s->control_command_id = SERVICE_EXEC_START_PRE; |
80876c20 | 1929 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) { |
81a2b7ce LP |
1930 | if ((r = service_spawn(s, |
1931 | s->control_command, | |
1932 | true, | |
1933 | false, | |
1934 | !s->permissions_start_only, | |
1935 | !s->root_directory_start_only, | |
1e3ad081 | 1936 | true, |
c952c6ec | 1937 | false, |
e55224ca | 1938 | &s->control_pid)) < 0) |
034c6ed7 LP |
1939 | goto fail; |
1940 | ||
80876c20 LP |
1941 | service_set_state(s, SERVICE_START_PRE); |
1942 | } else | |
034c6ed7 LP |
1943 | service_enter_start(s); |
1944 | ||
1945 | return; | |
1946 | ||
1947 | fail: | |
4cd1fbcc | 1948 | log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
1949 | service_enter_dead(s, false, true); |
1950 | } | |
1951 | ||
1952 | static void service_enter_restart(Service *s) { | |
1953 | int r; | |
398ef8ba LP |
1954 | DBusError error; |
1955 | ||
034c6ed7 | 1956 | assert(s); |
398ef8ba | 1957 | dbus_error_init(&error); |
034c6ed7 | 1958 | |
9ea9a0c8 LP |
1959 | service_enter_dead(s, true, false); |
1960 | ||
7b2603e6 | 1961 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0) |
034c6ed7 LP |
1962 | goto fail; |
1963 | ||
4cd1fbcc | 1964 | log_debug("%s scheduled restart job.", s->meta.id); |
034c6ed7 LP |
1965 | return; |
1966 | ||
1967 | fail: | |
398ef8ba | 1968 | log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r)); |
034c6ed7 | 1969 | service_enter_dead(s, false, false); |
398ef8ba LP |
1970 | |
1971 | dbus_error_free(&error); | |
034c6ed7 LP |
1972 | } |
1973 | ||
1974 | static void service_enter_reload(Service *s) { | |
1975 | int r; | |
1976 | ||
1977 | assert(s); | |
1978 | ||
5e94833f LP |
1979 | service_unwatch_control_pid(s); |
1980 | ||
a16e1123 | 1981 | s->control_command_id = SERVICE_EXEC_RELOAD; |
80876c20 | 1982 | if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) { |
81a2b7ce LP |
1983 | if ((r = service_spawn(s, |
1984 | s->control_command, | |
1985 | true, | |
1986 | false, | |
1987 | !s->permissions_start_only, | |
1988 | !s->root_directory_start_only, | |
c952c6ec | 1989 | false, |
1e3ad081 | 1990 | false, |
e55224ca | 1991 | &s->control_pid)) < 0) |
034c6ed7 LP |
1992 | goto fail; |
1993 | ||
80876c20 LP |
1994 | service_set_state(s, SERVICE_RELOAD); |
1995 | } else | |
1996 | service_enter_running(s, true); | |
034c6ed7 LP |
1997 | |
1998 | return; | |
1999 | ||
2000 | fail: | |
4cd1fbcc | 2001 | log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r)); |
034c6ed7 LP |
2002 | service_enter_stop(s, false); |
2003 | } | |
2004 | ||
34e9ba66 | 2005 | static void service_run_next_control(Service *s, bool success) { |
034c6ed7 LP |
2006 | int r; |
2007 | ||
2008 | assert(s); | |
2009 | assert(s->control_command); | |
2010 | assert(s->control_command->command_next); | |
2011 | ||
2012 | if (!success) | |
2013 | s->failure = true; | |
2014 | ||
34e9ba66 | 2015 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 2016 | |
34e9ba66 | 2017 | s->control_command = s->control_command->command_next; |
5e94833f LP |
2018 | service_unwatch_control_pid(s); |
2019 | ||
81a2b7ce LP |
2020 | if ((r = service_spawn(s, |
2021 | s->control_command, | |
2022 | true, | |
2023 | false, | |
2024 | !s->permissions_start_only, | |
2025 | !s->root_directory_start_only, | |
5830833f LP |
2026 | s->control_command_id == SERVICE_EXEC_START_PRE || |
2027 | s->control_command_id == SERVICE_EXEC_STOP_POST, | |
1e3ad081 | 2028 | false, |
e55224ca | 2029 | &s->control_pid)) < 0) |
034c6ed7 LP |
2030 | goto fail; |
2031 | ||
2032 | return; | |
2033 | ||
2034 | fail: | |
34e9ba66 | 2035 | log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r)); |
034c6ed7 | 2036 | |
80876c20 LP |
2037 | if (s->state == SERVICE_START_PRE) |
2038 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2039 | else if (s->state == SERVICE_STOP) | |
2040 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); | |
034c6ed7 LP |
2041 | else if (s->state == SERVICE_STOP_POST) |
2042 | service_enter_dead(s, false, true); | |
2043 | else | |
2044 | service_enter_stop(s, false); | |
5cb5a6ff LP |
2045 | } |
2046 | ||
34e9ba66 LP |
2047 | static void service_run_next_main(Service *s, bool success) { |
2048 | pid_t pid; | |
2049 | int r; | |
2050 | ||
2051 | assert(s); | |
2052 | assert(s->control_command); | |
2053 | assert(s->control_command->command_next); | |
2054 | ||
2055 | if (!success) | |
2056 | s->failure = true; | |
2057 | ||
2058 | assert(s->control_command_id == SERVICE_EXEC_START); | |
2059 | assert(s->type == SERVICE_ONESHOT); | |
2060 | ||
2061 | s->control_command = s->control_command->command_next; | |
2062 | service_unwatch_main_pid(s); | |
2063 | ||
2064 | if ((r = service_spawn(s, | |
2065 | s->control_command, | |
2066 | false, | |
2067 | true, | |
2068 | true, | |
2069 | true, | |
2070 | true, | |
2071 | s->notify_access != NOTIFY_NONE, | |
2072 | &pid)) < 0) | |
2073 | goto fail; | |
2074 | ||
2075 | service_set_main_pid(s, pid); | |
2076 | ||
2077 | return; | |
2078 | ||
2079 | fail: | |
2080 | log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r)); | |
2081 | service_enter_stop(s, false); | |
2082 | } | |
2083 | ||
87f0e418 LP |
2084 | static int service_start(Unit *u) { |
2085 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
2086 | |
2087 | assert(s); | |
2088 | ||
034c6ed7 LP |
2089 | /* We cannot fulfill this request right now, try again later |
2090 | * please! */ | |
2091 | if (s->state == SERVICE_STOP || | |
2092 | s->state == SERVICE_STOP_SIGTERM || | |
2093 | s->state == SERVICE_STOP_SIGKILL || | |
2094 | s->state == SERVICE_STOP_POST || | |
2095 | s->state == SERVICE_FINAL_SIGTERM || | |
2096 | s->state == SERVICE_FINAL_SIGKILL) | |
5cb5a6ff LP |
2097 | return -EAGAIN; |
2098 | ||
034c6ed7 LP |
2099 | /* Already on it! */ |
2100 | if (s->state == SERVICE_START_PRE || | |
2101 | s->state == SERVICE_START || | |
2102 | s->state == SERVICE_START_POST) | |
2103 | return 0; | |
2104 | ||
fdf20a31 | 2105 | assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART); |
5cb5a6ff | 2106 | |
1e2e8133 LP |
2107 | /* Make sure we don't enter a busy loop of some kind. */ |
2108 | if (!ratelimit_test(&s->ratelimit)) { | |
9e2f7c11 | 2109 | log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id); |
d5159713 | 2110 | return -ECANCELED; |
1e2e8133 LP |
2111 | } |
2112 | ||
034c6ed7 LP |
2113 | s->failure = false; |
2114 | s->main_pid_known = false; | |
47342320 | 2115 | s->forbid_restart = false; |
034c6ed7 LP |
2116 | |
2117 | service_enter_start_pre(s); | |
2118 | return 0; | |
5cb5a6ff LP |
2119 | } |
2120 | ||
87f0e418 LP |
2121 | static int service_stop(Unit *u) { |
2122 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
2123 | |
2124 | assert(s); | |
2125 | ||
3f6c78dc LP |
2126 | /* This is a user request, so don't do restarts on this |
2127 | * shutdown. */ | |
47342320 | 2128 | s->forbid_restart = true; |
034c6ed7 | 2129 | |
e537352b LP |
2130 | /* Already on it */ |
2131 | if (s->state == SERVICE_STOP || | |
2132 | s->state == SERVICE_STOP_SIGTERM || | |
2133 | s->state == SERVICE_STOP_SIGKILL || | |
2134 | s->state == SERVICE_STOP_POST || | |
2135 | s->state == SERVICE_FINAL_SIGTERM || | |
2136 | s->state == SERVICE_FINAL_SIGKILL) | |
2137 | return 0; | |
2138 | ||
3f6c78dc | 2139 | /* Don't allow a restart */ |
034c6ed7 LP |
2140 | if (s->state == SERVICE_AUTO_RESTART) { |
2141 | service_set_state(s, SERVICE_DEAD); | |
2142 | return 0; | |
2143 | } | |
2144 | ||
3f6c78dc LP |
2145 | /* If there's already something running we go directly into |
2146 | * kill mode. */ | |
2147 | if (s->state == SERVICE_START_PRE || | |
2148 | s->state == SERVICE_START || | |
2149 | s->state == SERVICE_START_POST || | |
2150 | s->state == SERVICE_RELOAD) { | |
2151 | service_enter_signal(s, SERVICE_STOP_SIGTERM, true); | |
2152 | return 0; | |
2153 | } | |
5cb5a6ff | 2154 | |
3f6c78dc LP |
2155 | assert(s->state == SERVICE_RUNNING || |
2156 | s->state == SERVICE_EXITED); | |
3a762661 | 2157 | |
034c6ed7 | 2158 | service_enter_stop(s, true); |
5cb5a6ff LP |
2159 | return 0; |
2160 | } | |
2161 | ||
87f0e418 LP |
2162 | static int service_reload(Unit *u) { |
2163 | Service *s = SERVICE(u); | |
034c6ed7 LP |
2164 | |
2165 | assert(s); | |
2166 | ||
80876c20 | 2167 | assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED); |
034c6ed7 LP |
2168 | |
2169 | service_enter_reload(s); | |
5cb5a6ff LP |
2170 | return 0; |
2171 | } | |
2172 | ||
87f0e418 LP |
2173 | static bool service_can_reload(Unit *u) { |
2174 | Service *s = SERVICE(u); | |
034c6ed7 LP |
2175 | |
2176 | assert(s); | |
2177 | ||
2178 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
2179 | } | |
2180 | ||
a16e1123 LP |
2181 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
2182 | Service *s = SERVICE(u); | |
2183 | ||
2184 | assert(u); | |
2185 | assert(f); | |
2186 | assert(fds); | |
2187 | ||
2188 | unit_serialize_item(u, f, "state", service_state_to_string(s->state)); | |
2189 | unit_serialize_item(u, f, "failure", yes_no(s->failure)); | |
2190 | ||
2191 | if (s->control_pid > 0) | |
5925dd3c | 2192 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); |
a16e1123 | 2193 | |
5925dd3c LP |
2194 | if (s->main_pid_known && s->main_pid > 0) |
2195 | unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid); | |
a16e1123 LP |
2196 | |
2197 | unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known)); | |
2198 | ||
3a2776bc LP |
2199 | if (s->status_text) |
2200 | unit_serialize_item(u, f, "status-text", s->status_text); | |
2201 | ||
a16e1123 LP |
2202 | /* There's a minor uncleanliness here: if there are multiple |
2203 | * commands attached here, we will start from the first one | |
2204 | * again */ | |
2205 | if (s->control_command_id >= 0) | |
825636e5 | 2206 | unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id)); |
a16e1123 LP |
2207 | |
2208 | if (s->socket_fd >= 0) { | |
2209 | int copy; | |
2210 | ||
2211 | if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0) | |
2212 | return copy; | |
2213 | ||
2214 | unit_serialize_item_format(u, f, "socket-fd", "%i", copy); | |
2215 | } | |
2216 | ||
ecdbca40 LP |
2217 | if (s->main_exec_status.pid > 0) { |
2218 | unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid); | |
2219 | ||
2220 | if (s->main_exec_status.start_timestamp.realtime > 0) { | |
2221 | unit_serialize_item_format(u, f, "main-exec-status-start-realtime", | |
2222 | "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime); | |
2223 | ||
2224 | unit_serialize_item_format(u, f, "main-exec-status-start-monotonic", | |
2225 | "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic); | |
2226 | } | |
2227 | ||
2228 | if (s->main_exec_status.exit_timestamp.realtime > 0) { | |
2229 | unit_serialize_item_format(u, f, "main-exec-status-exit-realtime", | |
2230 | "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime); | |
2231 | unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic", | |
2232 | "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic); | |
2233 | ||
2234 | unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code); | |
2235 | unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
2236 | } | |
2237 | } | |
2238 | ||
a16e1123 LP |
2239 | return 0; |
2240 | } | |
2241 | ||
2242 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
2243 | Service *s = SERVICE(u); | |
a16e1123 LP |
2244 | |
2245 | assert(u); | |
2246 | assert(key); | |
2247 | assert(value); | |
2248 | assert(fds); | |
2249 | ||
2250 | if (streq(key, "state")) { | |
2251 | ServiceState state; | |
2252 | ||
2253 | if ((state = service_state_from_string(value)) < 0) | |
2254 | log_debug("Failed to parse state value %s", value); | |
2255 | else | |
2256 | s->deserialized_state = state; | |
2257 | } else if (streq(key, "failure")) { | |
2258 | int b; | |
2259 | ||
2260 | if ((b = parse_boolean(value)) < 0) | |
2261 | log_debug("Failed to parse failure value %s", value); | |
2262 | else | |
2263 | s->failure = b || s->failure; | |
2264 | } else if (streq(key, "control-pid")) { | |
5925dd3c | 2265 | pid_t pid; |
a16e1123 | 2266 | |
e364ad06 | 2267 | if (parse_pid(value, &pid) < 0) |
a16e1123 LP |
2268 | log_debug("Failed to parse control-pid value %s", value); |
2269 | else | |
e55224ca | 2270 | s->control_pid = pid; |
a16e1123 | 2271 | } else if (streq(key, "main-pid")) { |
5925dd3c | 2272 | pid_t pid; |
a16e1123 | 2273 | |
e364ad06 | 2274 | if (parse_pid(value, &pid) < 0) |
a16e1123 LP |
2275 | log_debug("Failed to parse main-pid value %s", value); |
2276 | else | |
5925dd3c | 2277 | service_set_main_pid(s, (pid_t) pid); |
a16e1123 LP |
2278 | } else if (streq(key, "main-pid-known")) { |
2279 | int b; | |
2280 | ||
2281 | if ((b = parse_boolean(value)) < 0) | |
2282 | log_debug("Failed to parse main-pid-known value %s", value); | |
2283 | else | |
2284 | s->main_pid_known = b; | |
3a2776bc LP |
2285 | } else if (streq(key, "status-text")) { |
2286 | char *t; | |
2287 | ||
2288 | if ((t = strdup(value))) { | |
2289 | free(s->status_text); | |
2290 | s->status_text = t; | |
2291 | } | |
2292 | ||
a16e1123 LP |
2293 | } else if (streq(key, "control-command")) { |
2294 | ServiceExecCommand id; | |
2295 | ||
2296 | if ((id = service_exec_command_from_string(value)) < 0) | |
2297 | log_debug("Failed to parse exec-command value %s", value); | |
2298 | else { | |
2299 | s->control_command_id = id; | |
2300 | s->control_command = s->exec_command[id]; | |
2301 | } | |
2302 | } else if (streq(key, "socket-fd")) { | |
2303 | int fd; | |
2304 | ||
2305 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2306 | log_debug("Failed to parse socket-fd value %s", value); | |
2307 | else { | |
2308 | ||
2309 | if (s->socket_fd >= 0) | |
2310 | close_nointr_nofail(s->socket_fd); | |
2311 | s->socket_fd = fdset_remove(fds, fd); | |
2312 | } | |
ecdbca40 LP |
2313 | } else if (streq(key, "main-exec-status-pid")) { |
2314 | pid_t pid; | |
2315 | ||
e364ad06 | 2316 | if (parse_pid(value, &pid) < 0) |
ecdbca40 LP |
2317 | log_debug("Failed to parse main-exec-status-pid value %s", value); |
2318 | else | |
2319 | s->main_exec_status.pid = pid; | |
2320 | } else if (streq(key, "main-exec-status-code")) { | |
2321 | int i; | |
2322 | ||
e364ad06 | 2323 | if (safe_atoi(value, &i) < 0) |
ecdbca40 LP |
2324 | log_debug("Failed to parse main-exec-status-code value %s", value); |
2325 | else | |
2326 | s->main_exec_status.code = i; | |
2327 | } else if (streq(key, "main-exec-status-status")) { | |
2328 | int i; | |
2329 | ||
e364ad06 | 2330 | if (safe_atoi(value, &i) < 0) |
ecdbca40 LP |
2331 | log_debug("Failed to parse main-exec-status-status value %s", value); |
2332 | else | |
2333 | s->main_exec_status.status = i; | |
2334 | } else if (streq(key, "main-exec-status-start-realtime")) { | |
2335 | uint64_t k; | |
2336 | ||
e364ad06 | 2337 | if (safe_atou64(value, &k) < 0) |
ecdbca40 LP |
2338 | log_debug("Failed to parse main-exec-status-start-realtime value %s", value); |
2339 | else | |
2340 | s->main_exec_status.start_timestamp.realtime = (usec_t) k; | |
2341 | } else if (streq(key, "main-exec-status-start-monotonic")) { | |
2342 | uint64_t k; | |
2343 | ||
e364ad06 | 2344 | if (safe_atou64(value, &k) < 0) |
ecdbca40 LP |
2345 | log_debug("Failed to parse main-exec-status-start-monotonic value %s", value); |
2346 | else | |
2347 | s->main_exec_status.start_timestamp.monotonic = (usec_t) k; | |
2348 | } else if (streq(key, "main-exec-status-exit-realtime")) { | |
2349 | uint64_t k; | |
2350 | ||
e364ad06 | 2351 | if (safe_atou64(value, &k) < 0) |
ecdbca40 LP |
2352 | log_debug("Failed to parse main-exec-status-exit-realtime value %s", value); |
2353 | else | |
2354 | s->main_exec_status.exit_timestamp.realtime = (usec_t) k; | |
2355 | } else if (streq(key, "main-exec-status-exit-monotonic")) { | |
2356 | uint64_t k; | |
2357 | ||
e364ad06 | 2358 | if (safe_atou64(value, &k) < 0) |
ecdbca40 LP |
2359 | log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value); |
2360 | else | |
2361 | s->main_exec_status.exit_timestamp.monotonic = (usec_t) k; | |
a16e1123 LP |
2362 | } else |
2363 | log_debug("Unknown serialization key '%s'", key); | |
2364 | ||
2365 | return 0; | |
2366 | } | |
2367 | ||
87f0e418 LP |
2368 | static UnitActiveState service_active_state(Unit *u) { |
2369 | assert(u); | |
5cb5a6ff | 2370 | |
acbb0225 | 2371 | return state_translation_table[SERVICE(u)->state]; |
034c6ed7 LP |
2372 | } |
2373 | ||
10a94420 LP |
2374 | static const char *service_sub_state_to_string(Unit *u) { |
2375 | assert(u); | |
2376 | ||
2377 | return service_state_to_string(SERVICE(u)->state); | |
2378 | } | |
2379 | ||
07459bb6 | 2380 | #ifdef HAVE_SYSV_COMPAT |
701cc384 LP |
2381 | static bool service_check_gc(Unit *u) { |
2382 | Service *s = SERVICE(u); | |
2383 | ||
2384 | assert(s); | |
2385 | ||
2386 | return !!s->sysv_path; | |
2387 | } | |
07459bb6 | 2388 | #endif |
701cc384 LP |
2389 | |
2390 | static bool service_check_snapshot(Unit *u) { | |
2391 | Service *s = SERVICE(u); | |
2392 | ||
2393 | assert(s); | |
2394 | ||
2395 | return !s->got_socket_fd; | |
2396 | } | |
2397 | ||
87f0e418 LP |
2398 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
2399 | Service *s = SERVICE(u); | |
034c6ed7 | 2400 | bool success; |
5cb5a6ff LP |
2401 | |
2402 | assert(s); | |
034c6ed7 LP |
2403 | assert(pid >= 0); |
2404 | ||
f21781d5 | 2405 | if (!s->meta.fragment_path) |
d06dacd0 LP |
2406 | success = is_clean_exit_lsb(code, status); |
2407 | else | |
2408 | success = is_clean_exit(code, status); | |
034c6ed7 LP |
2409 | |
2410 | if (s->main_pid == pid) { | |
2411 | ||
034c6ed7 | 2412 | s->main_pid = 0; |
169c1bda | 2413 | exec_status_exit(&s->main_exec_status, pid, code, status, s->exec_context.utmp_id); |
034c6ed7 | 2414 | |
34e9ba66 LP |
2415 | if (s->type != SERVICE_FORKING && s->control_command) { |
2416 | s->control_command->exec_status = s->main_exec_status; | |
b708e7ce | 2417 | |
34e9ba66 | 2418 | if (s->control_command->ignore) |
b708e7ce | 2419 | success = true; |
034c6ed7 LP |
2420 | } |
2421 | ||
92abbefb LP |
2422 | log_full(success ? LOG_DEBUG : LOG_NOTICE, |
2423 | "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status); | |
b708e7ce | 2424 | s->failure = s->failure || !success; |
034c6ed7 | 2425 | |
34e9ba66 LP |
2426 | if (s->control_command && |
2427 | s->control_command->command_next && | |
2428 | success) { | |
034c6ed7 | 2429 | |
34e9ba66 LP |
2430 | /* There is another command to * |
2431 | * execute, so let's do that. */ | |
034c6ed7 | 2432 | |
34e9ba66 LP |
2433 | log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state)); |
2434 | service_run_next_main(s, success); | |
034c6ed7 | 2435 | |
34e9ba66 LP |
2436 | } else { |
2437 | ||
2438 | /* The service exited, so the service is officially | |
2439 | * gone. */ | |
2440 | ||
2441 | s->control_command = NULL; | |
2442 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2443 | ||
2444 | switch (s->state) { | |
2445 | ||
2446 | case SERVICE_START_POST: | |
2447 | case SERVICE_RELOAD: | |
2448 | case SERVICE_STOP: | |
2449 | /* Need to wait until the operation is | |
2450 | * done */ | |
c4653a4d | 2451 | break; |
7d55e835 | 2452 | |
34e9ba66 LP |
2453 | case SERVICE_START: |
2454 | if (s->type == SERVICE_ONESHOT) { | |
2455 | /* This was our main goal, so let's go on */ | |
2456 | if (success) | |
2457 | service_enter_start_post(s); | |
2458 | else | |
2459 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2460 | break; | |
2461 | } else { | |
2462 | assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY); | |
7d55e835 | 2463 | |
34e9ba66 LP |
2464 | /* Fall through */ |
2465 | } | |
034c6ed7 | 2466 | |
34e9ba66 LP |
2467 | case SERVICE_RUNNING: |
2468 | service_enter_running(s, success); | |
2469 | break; | |
034c6ed7 | 2470 | |
34e9ba66 LP |
2471 | case SERVICE_STOP_SIGTERM: |
2472 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 2473 | |
34e9ba66 LP |
2474 | if (!control_pid_good(s)) |
2475 | service_enter_stop_post(s, success); | |
5cb5a6ff | 2476 | |
34e9ba66 LP |
2477 | /* If there is still a control process, wait for that first */ |
2478 | break; | |
2479 | ||
2480 | default: | |
2481 | assert_not_reached("Uh, main process died at wrong time."); | |
2482 | } | |
034c6ed7 | 2483 | } |
5cb5a6ff | 2484 | |
034c6ed7 | 2485 | } else if (s->control_pid == pid) { |
034c6ed7 | 2486 | |
34e9ba66 LP |
2487 | s->control_pid = 0; |
2488 | ||
b708e7ce | 2489 | if (s->control_command) { |
169c1bda | 2490 | exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id); |
a16e1123 | 2491 | |
b708e7ce LP |
2492 | if (s->control_command->ignore) |
2493 | success = true; | |
2494 | } | |
2495 | ||
34e9ba66 | 2496 | log_full(success ? LOG_DEBUG : LOG_NOTICE, |
92abbefb | 2497 | "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); |
b708e7ce | 2498 | s->failure = s->failure || !success; |
034c6ed7 | 2499 | |
34e9ba66 LP |
2500 | if (s->control_command && |
2501 | s->control_command->command_next && | |
2502 | success) { | |
034c6ed7 LP |
2503 | |
2504 | /* There is another command to * | |
2505 | * execute, so let's do that. */ | |
2506 | ||
34e9ba66 LP |
2507 | log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state)); |
2508 | service_run_next_control(s, success); | |
034c6ed7 | 2509 | |
80876c20 | 2510 | } else { |
034c6ed7 LP |
2511 | /* No further commands for this step, so let's |
2512 | * figure out what to do next */ | |
2513 | ||
a16e1123 LP |
2514 | s->control_command = NULL; |
2515 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2516 | ||
9e2f7c11 | 2517 | log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state)); |
bd982a8b | 2518 | |
034c6ed7 LP |
2519 | switch (s->state) { |
2520 | ||
2521 | case SERVICE_START_PRE: | |
2522 | if (success) | |
2523 | service_enter_start(s); | |
2524 | else | |
80876c20 | 2525 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
034c6ed7 LP |
2526 | break; |
2527 | ||
2528 | case SERVICE_START: | |
2529 | assert(s->type == SERVICE_FORKING); | |
2530 | ||
2531 | /* Let's try to load the pid | |
2532 | * file here if we can. We | |
2533 | * ignore the return value, | |
2534 | * since the PID file might | |
2535 | * actually be created by a | |
2536 | * START_POST script */ | |
2537 | ||
2538 | if (success) { | |
2539 | if (s->pid_file) | |
2540 | service_load_pid_file(s); | |
2541 | ||
2542 | service_enter_start_post(s); | |
2543 | } else | |
80876c20 | 2544 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
034c6ed7 LP |
2545 | |
2546 | break; | |
2547 | ||
2548 | case SERVICE_START_POST: | |
2549 | if (success && s->pid_file && !s->main_pid_known) { | |
2550 | int r; | |
2551 | ||
2552 | /* Hmm, let's see if we can | |
2553 | * load the pid now after the | |
2554 | * start-post scripts got | |
2555 | * executed. */ | |
2556 | ||
2557 | if ((r = service_load_pid_file(s)) < 0) | |
4cd1fbcc | 2558 | log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r)); |
034c6ed7 LP |
2559 | } |
2560 | ||
2561 | /* Fall through */ | |
2562 | ||
2563 | case SERVICE_RELOAD: | |
80876c20 LP |
2564 | if (success) |
2565 | service_enter_running(s, true); | |
2566 | else | |
034c6ed7 LP |
2567 | service_enter_stop(s, false); |
2568 | ||
2569 | break; | |
2570 | ||
2571 | case SERVICE_STOP: | |
80876c20 | 2572 | service_enter_signal(s, SERVICE_STOP_SIGTERM, success); |
034c6ed7 LP |
2573 | break; |
2574 | ||
2575 | case SERVICE_STOP_SIGTERM: | |
2576 | case SERVICE_STOP_SIGKILL: | |
2577 | if (main_pid_good(s) <= 0) | |
2578 | service_enter_stop_post(s, success); | |
2579 | ||
2580 | /* If there is still a service | |
2581 | * process around, wait until | |
2582 | * that one quit, too */ | |
2583 | break; | |
2584 | ||
2585 | case SERVICE_STOP_POST: | |
2586 | case SERVICE_FINAL_SIGTERM: | |
2587 | case SERVICE_FINAL_SIGKILL: | |
2588 | service_enter_dead(s, success, true); | |
2589 | break; | |
2590 | ||
2591 | default: | |
2592 | assert_not_reached("Uh, control process died at wrong time."); | |
2593 | } | |
2594 | } | |
8c47c732 | 2595 | } |
c4e2ceae LP |
2596 | |
2597 | /* Notify clients about changed exit status */ | |
2598 | unit_add_to_dbus_queue(u); | |
034c6ed7 LP |
2599 | } |
2600 | ||
acbb0225 | 2601 | static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) { |
87f0e418 | 2602 | Service *s = SERVICE(u); |
034c6ed7 LP |
2603 | |
2604 | assert(s); | |
2605 | assert(elapsed == 1); | |
2606 | ||
acbb0225 | 2607 | assert(w == &s->timer_watch); |
034c6ed7 LP |
2608 | |
2609 | switch (s->state) { | |
2610 | ||
2611 | case SERVICE_START_PRE: | |
2612 | case SERVICE_START: | |
9e2f7c11 | 2613 | log_warning("%s operation timed out. Terminating.", u->meta.id); |
80876c20 LP |
2614 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
2615 | break; | |
2616 | ||
034c6ed7 LP |
2617 | case SERVICE_START_POST: |
2618 | case SERVICE_RELOAD: | |
9e2f7c11 | 2619 | log_warning("%s operation timed out. Stopping.", u->meta.id); |
034c6ed7 LP |
2620 | service_enter_stop(s, false); |
2621 | break; | |
2622 | ||
2623 | case SERVICE_STOP: | |
9e2f7c11 | 2624 | log_warning("%s stopping timed out. Terminating.", u->meta.id); |
034c6ed7 LP |
2625 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); |
2626 | break; | |
2627 | ||
2628 | case SERVICE_STOP_SIGTERM: | |
9e2f7c11 | 2629 | log_warning("%s stopping timed out. Killing.", u->meta.id); |
034c6ed7 LP |
2630 | service_enter_signal(s, SERVICE_STOP_SIGKILL, false); |
2631 | break; | |
2632 | ||
2633 | case SERVICE_STOP_SIGKILL: | |
2634 | /* Uh, wie sent a SIGKILL and it is still not gone? | |
2635 | * Must be something we cannot kill, so let's just be | |
2636 | * weirded out and continue */ | |
2637 | ||
9e2f7c11 | 2638 | log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id); |
034c6ed7 LP |
2639 | service_enter_stop_post(s, false); |
2640 | break; | |
2641 | ||
2642 | case SERVICE_STOP_POST: | |
9e2f7c11 | 2643 | log_warning("%s stopping timed out (2). Terminating.", u->meta.id); |
034c6ed7 LP |
2644 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
2645 | break; | |
2646 | ||
2647 | case SERVICE_FINAL_SIGTERM: | |
9e2f7c11 | 2648 | log_warning("%s stopping timed out (2). Killing.", u->meta.id); |
034c6ed7 LP |
2649 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, false); |
2650 | break; | |
2651 | ||
2652 | case SERVICE_FINAL_SIGKILL: | |
fdf20a31 | 2653 | log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id); |
034c6ed7 LP |
2654 | service_enter_dead(s, false, true); |
2655 | break; | |
2656 | ||
2657 | case SERVICE_AUTO_RESTART: | |
54165a39 | 2658 | log_info("%s holdoff time over, scheduling restart.", u->meta.id); |
034c6ed7 LP |
2659 | service_enter_restart(s); |
2660 | break; | |
2661 | ||
2662 | default: | |
2663 | assert_not_reached("Timeout at wrong time."); | |
2664 | } | |
5cb5a6ff LP |
2665 | } |
2666 | ||
8e274523 LP |
2667 | static void service_cgroup_notify_event(Unit *u) { |
2668 | Service *s = SERVICE(u); | |
2669 | ||
2670 | assert(u); | |
2671 | ||
9e2f7c11 | 2672 | log_debug("%s: cgroup is empty", u->meta.id); |
8e274523 LP |
2673 | |
2674 | switch (s->state) { | |
2675 | ||
2676 | /* Waiting for SIGCHLD is usually more interesting, | |
2677 | * because it includes return codes/signals. Which is | |
2678 | * why we ignore the cgroup events for most cases, | |
2679 | * except when we don't know pid which to expect the | |
2680 | * SIGCHLD for. */ | |
2681 | ||
2682 | case SERVICE_RUNNING: | |
80876c20 | 2683 | service_enter_running(s, true); |
8e274523 LP |
2684 | break; |
2685 | ||
28708d8a LP |
2686 | case SERVICE_STOP_SIGTERM: |
2687 | case SERVICE_STOP_SIGKILL: | |
2688 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2689 | service_enter_stop_post(s, true); | |
2690 | ||
2691 | break; | |
2692 | ||
7f97f0fe LP |
2693 | case SERVICE_FINAL_SIGTERM: |
2694 | case SERVICE_FINAL_SIGKILL: | |
2695 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2696 | service_enter_dead(s, true, true); | |
2697 | ||
2698 | break; | |
2699 | ||
8e274523 LP |
2700 | default: |
2701 | ; | |
2702 | } | |
2703 | } | |
2704 | ||
c952c6ec | 2705 | static void service_notify_message(Unit *u, pid_t pid, char **tags) { |
8c47c732 LP |
2706 | Service *s = SERVICE(u); |
2707 | const char *e; | |
2708 | ||
2709 | assert(u); | |
2710 | ||
c952c6ec LP |
2711 | if (s->notify_access == NOTIFY_NONE) { |
2712 | log_warning("%s: Got notification message from PID %lu, but reception is disabled.", | |
2713 | u->meta.id, (unsigned long) pid); | |
2714 | return; | |
2715 | } | |
2716 | ||
2717 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { | |
2718 | log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu", | |
2719 | u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid); | |
2720 | return; | |
2721 | } | |
2722 | ||
8c47c732 LP |
2723 | log_debug("%s: Got message", u->meta.id); |
2724 | ||
2725 | /* Interpret MAINPID= */ | |
2726 | if ((e = strv_find_prefix(tags, "MAINPID=")) && | |
2727 | (s->state == SERVICE_START || | |
2728 | s->state == SERVICE_START_POST || | |
2729 | s->state == SERVICE_RUNNING || | |
2730 | s->state == SERVICE_RELOAD)) { | |
8c47c732 | 2731 | |
5925dd3c | 2732 | if (parse_pid(e + 8, &pid) < 0) |
92abbefb | 2733 | log_warning("Failed to parse notification message %s", e); |
8c47c732 LP |
2734 | else { |
2735 | log_debug("%s: got %s", u->meta.id, e); | |
5925dd3c | 2736 | service_set_main_pid(s, pid); |
8c47c732 LP |
2737 | } |
2738 | } | |
2739 | ||
2740 | /* Interpret READY= */ | |
2741 | if (s->type == SERVICE_NOTIFY && | |
2742 | s->state == SERVICE_START && | |
2743 | strv_find(tags, "READY=1")) { | |
2744 | log_debug("%s: got READY=1", u->meta.id); | |
2745 | ||
2746 | service_enter_start_post(s); | |
2747 | } | |
2748 | ||
2749 | /* Interpret STATUS= */ | |
2750 | if ((e = strv_find_prefix(tags, "STATUS="))) { | |
2751 | char *t; | |
2752 | ||
3a2776bc LP |
2753 | if (e[7]) { |
2754 | if (!(t = strdup(e+7))) { | |
2755 | log_error("Failed to allocate string."); | |
2756 | return; | |
2757 | } | |
2758 | ||
2759 | log_debug("%s: got %s", u->meta.id, e); | |
8c47c732 | 2760 | |
3a2776bc LP |
2761 | free(s->status_text); |
2762 | s->status_text = t; | |
2763 | } else { | |
2764 | free(s->status_text); | |
2765 | s->status_text = NULL; | |
2766 | } | |
8c47c732 | 2767 | |
8c47c732 | 2768 | } |
c4e2ceae LP |
2769 | |
2770 | /* Notify clients about changed status or main pid */ | |
2771 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
2772 | } |
2773 | ||
07459bb6 | 2774 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 | 2775 | static int service_enumerate(Manager *m) { |
2c4104f0 LP |
2776 | char **p; |
2777 | unsigned i; | |
2778 | DIR *d = NULL; | |
2779 | char *path = NULL, *fpath = NULL, *name = NULL; | |
c68364b7 LP |
2780 | Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL; |
2781 | Unit *service; | |
2782 | Iterator j; | |
2c4104f0 | 2783 | int r; |
980900c1 TG |
2784 | #ifdef TARGET_ARCH |
2785 | Unit *previous = NULL; | |
2786 | char *arch_daemons = NULL; | |
2787 | char *arch_daemons_stripped = NULL; | |
2788 | char **arch_daemons_split = NULL; | |
2789 | #endif | |
2c4104f0 LP |
2790 | |
2791 | assert(m); | |
2792 | ||
980900c1 TG |
2793 | #ifdef TARGET_ARCH |
2794 | if ((r = parse_env_file("/etc/rc.conf", NEWLINE, | |
2795 | "DAEMONS", &arch_daemons, | |
2796 | NULL)) < 0) { | |
2797 | ||
2798 | if (r != -ENOENT) | |
2799 | log_warning("Failed to read /etc/rc.conf: %s", strerror(-r)); | |
2800 | ||
2801 | } else if (arch_daemons) { | |
2802 | if (!(arch_daemons_stripped = strchr(arch_daemons, '('))) | |
2803 | arch_daemons_stripped = arch_daemons; | |
2804 | else | |
2805 | arch_daemons_stripped++; /* strip start paren */ | |
2806 | ||
2807 | arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */ | |
2808 | ||
2809 | if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) { | |
2810 | r = -ENOMEM; | |
2811 | goto finish; | |
2812 | } | |
2813 | ||
2814 | STRV_FOREACH(p, arch_daemons_split) { | |
2815 | ||
2816 | free(name); | |
409db002 | 2817 | name = NULL; |
980900c1 TG |
2818 | |
2819 | if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */ | |
2820 | continue; | |
2821 | ||
2822 | if (!(name = sysv_translate_name(*p))) { | |
2823 | r = -ENOMEM; | |
2824 | goto finish; | |
2825 | } | |
2826 | ||
2827 | if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) { | |
2828 | log_warning("Failed to prepare unit %s: %s", name, strerror(-r)); | |
2829 | continue; | |
2830 | } | |
2831 | ||
2832 | if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0) | |
2833 | goto finish; | |
2834 | ||
2835 | if (previous) | |
2836 | if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0) | |
2837 | goto finish; | |
2838 | ||
2839 | if (**p != '@') /* daemons prefixed with @ can be started in the background */ | |
2840 | previous = service; | |
2841 | } | |
2842 | } | |
2843 | #endif | |
2844 | ||
c68364b7 LP |
2845 | zero(runlevel_services); |
2846 | ||
84e3543e | 2847 | STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path) |
09cd1ab1 | 2848 | for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) { |
2c4104f0 LP |
2849 | struct dirent *de; |
2850 | ||
2851 | free(path); | |
2852 | path = NULL; | |
09cd1ab1 | 2853 | if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) { |
2c4104f0 LP |
2854 | r = -ENOMEM; |
2855 | goto finish; | |
2856 | } | |
2857 | ||
2858 | if (d) | |
2859 | closedir(d); | |
2860 | ||
2861 | if (!(d = opendir(path))) { | |
2862 | if (errno != ENOENT) | |
2863 | log_warning("opendir() failed on %s: %s", path, strerror(errno)); | |
2864 | ||
2865 | continue; | |
2866 | } | |
2867 | ||
2868 | while ((de = readdir(d))) { | |
db06e3b6 | 2869 | int a, b; |
2c4104f0 LP |
2870 | |
2871 | if (ignore_file(de->d_name)) | |
2872 | continue; | |
2873 | ||
2874 | if (de->d_name[0] != 'S' && de->d_name[0] != 'K') | |
2875 | continue; | |
2876 | ||
2877 | if (strlen(de->d_name) < 4) | |
2878 | continue; | |
2879 | ||
db06e3b6 LP |
2880 | a = undecchar(de->d_name[1]); |
2881 | b = undecchar(de->d_name[2]); | |
2882 | ||
2883 | if (a < 0 || b < 0) | |
2884 | continue; | |
2885 | ||
2c4104f0 LP |
2886 | free(fpath); |
2887 | fpath = NULL; | |
09cd1ab1 | 2888 | if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) { |
2c4104f0 LP |
2889 | r = -ENOMEM; |
2890 | goto finish; | |
2891 | } | |
2892 | ||
2893 | if (access(fpath, X_OK) < 0) { | |
2894 | ||
2895 | if (errno != ENOENT) | |
2896 | log_warning("access() failed on %s: %s", fpath, strerror(errno)); | |
2897 | ||
2898 | continue; | |
2899 | } | |
2900 | ||
2901 | free(name); | |
b7ccee3c | 2902 | if (!(name = sysv_translate_name(de->d_name + 3))) { |
2c4104f0 LP |
2903 | r = -ENOMEM; |
2904 | goto finish; | |
2905 | } | |
2906 | ||
398ef8ba | 2907 | if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) { |
fbe9f3a9 LP |
2908 | log_warning("Failed to prepare unit %s: %s", name, strerror(-r)); |
2909 | continue; | |
2910 | } | |
2c4104f0 | 2911 | |
c68364b7 LP |
2912 | if (de->d_name[0] == 'S') { |
2913 | ||
f73d93a4 LP |
2914 | if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) { |
2915 | SERVICE(service)->sysv_start_priority = | |
2916 | MAX(a*10 + b, SERVICE(service)->sysv_start_priority); | |
db06e3b6 | 2917 | |
c68364b7 | 2918 | SERVICE(service)->sysv_enabled = true; |
f73d93a4 | 2919 | } |
db06e3b6 | 2920 | |
c68364b7 LP |
2921 | if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0) |
2922 | goto finish; | |
2c4104f0 | 2923 | |
c68364b7 | 2924 | if ((r = set_put(runlevel_services[i], service)) < 0) |
2c4104f0 | 2925 | goto finish; |
23a177ef | 2926 | |
fc5df99e LP |
2927 | } else if (de->d_name[0] == 'K' && |
2928 | (rcnd_table[i].type == RUNLEVEL_DOWN || | |
2929 | rcnd_table[i].type == RUNLEVEL_SYSINIT)) { | |
6542952f | 2930 | |
c68364b7 LP |
2931 | if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0) |
2932 | goto finish; | |
2933 | ||
2934 | if ((r = set_put(shutdown_services, service)) < 0) | |
2c4104f0 LP |
2935 | goto finish; |
2936 | } | |
2937 | } | |
2938 | } | |
2939 | ||
c68364b7 LP |
2940 | /* Now we loaded all stubs and are aware of the lowest |
2941 | start-up priority for all services, not let's actually load | |
2942 | the services, this will also tell us which services are | |
2943 | actually native now */ | |
2944 | manager_dispatch_load_queue(m); | |
2945 | ||
2946 | /* If this is a native service, rely on native ways to pull in | |
2947 | * a service, don't pull it in via sysv rcN.d links. */ | |
2948 | for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) | |
2949 | SET_FOREACH(service, runlevel_services[i], j) { | |
2950 | service = unit_follow_merge(service); | |
2951 | ||
2952 | if (service->meta.fragment_path) | |
2953 | continue; | |
2954 | ||
2955 | if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0) | |
2956 | goto finish; | |
2957 | } | |
2958 | ||
2959 | /* We honour K links only for halt/reboot. For the normal | |
2960 | * runlevels we assume the stop jobs will be implicitly added | |
2961 | * by the core logic. Also, we don't really distuingish here | |
2962 | * between the runlevels 0 and 6 and just add them to the | |
2963 | * special shutdown target. On SUSE the boot.d/ runlevel is | |
2964 | * also used for shutdown, so we add links for that too to the | |
2965 | * shutdown target.*/ | |
2966 | SET_FOREACH(service, shutdown_services, j) { | |
2967 | service = unit_follow_merge(service); | |
2968 | ||
2969 | if (service->meta.fragment_path) | |
2970 | continue; | |
2971 | ||
2972 | if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0) | |
2973 | goto finish; | |
2974 | } | |
2975 | ||
2c4104f0 LP |
2976 | r = 0; |
2977 | ||
2978 | finish: | |
2979 | free(path); | |
2980 | free(fpath); | |
2981 | free(name); | |
980900c1 TG |
2982 | #ifdef TARGET_ARCH |
2983 | free(arch_daemons); | |
2984 | free(arch_daemons_split); | |
2985 | #endif | |
fbe9f3a9 | 2986 | |
c68364b7 LP |
2987 | for (i = 0; i < ELEMENTSOF(rcnd_table); i++) |
2988 | set_free(runlevel_services[i]); | |
2989 | set_free(shutdown_services); | |
2990 | ||
fbe9f3a9 LP |
2991 | if (d) |
2992 | closedir(d); | |
2c4104f0 LP |
2993 | |
2994 | return r; | |
2995 | } | |
07459bb6 | 2996 | #endif |
2c4104f0 | 2997 | |
05e343b7 LP |
2998 | static void service_bus_name_owner_change( |
2999 | Unit *u, | |
3000 | const char *name, | |
3001 | const char *old_owner, | |
3002 | const char *new_owner) { | |
3003 | ||
3004 | Service *s = SERVICE(u); | |
3005 | ||
3006 | assert(s); | |
3007 | assert(name); | |
3008 | ||
3009 | assert(streq(s->bus_name, name)); | |
3010 | assert(old_owner || new_owner); | |
3011 | ||
3012 | if (old_owner && new_owner) | |
3013 | log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner); | |
3014 | else if (old_owner) | |
3015 | log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner); | |
3016 | else | |
3017 | log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner); | |
3018 | ||
3019 | s->bus_name_good = !!new_owner; | |
3020 | ||
3021 | if (s->type == SERVICE_DBUS) { | |
3022 | ||
3023 | /* service_enter_running() will figure out what to | |
3024 | * do */ | |
3025 | if (s->state == SERVICE_RUNNING) | |
3026 | service_enter_running(s, true); | |
3027 | else if (s->state == SERVICE_START && new_owner) | |
3028 | service_enter_start_post(s); | |
3029 | ||
3030 | } else if (new_owner && | |
3031 | s->main_pid <= 0 && | |
3032 | (s->state == SERVICE_START || | |
3033 | s->state == SERVICE_START_POST || | |
3034 | s->state == SERVICE_RUNNING || | |
3035 | s->state == SERVICE_RELOAD)) { | |
3036 | ||
3037 | /* Try to acquire PID from bus service */ | |
3038 | log_debug("Trying to acquire PID from D-Bus name..."); | |
3039 | ||
3040 | bus_query_pid(u->meta.manager, name); | |
3041 | } | |
3042 | } | |
3043 | ||
3044 | static void service_bus_query_pid_done( | |
3045 | Unit *u, | |
3046 | const char *name, | |
3047 | pid_t pid) { | |
3048 | ||
3049 | Service *s = SERVICE(u); | |
3050 | ||
3051 | assert(s); | |
3052 | assert(name); | |
3053 | ||
3054 | log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid); | |
3055 | ||
3056 | if (s->main_pid <= 0 && | |
3057 | (s->state == SERVICE_START || | |
3058 | s->state == SERVICE_START_POST || | |
3059 | s->state == SERVICE_RUNNING || | |
3060 | s->state == SERVICE_RELOAD)) | |
5925dd3c | 3061 | service_set_main_pid(s, pid); |
05e343b7 LP |
3062 | } |
3063 | ||
6cf6bbc2 | 3064 | int service_set_socket_fd(Service *s, int fd, Socket *sock) { |
4f2d528d LP |
3065 | assert(s); |
3066 | assert(fd >= 0); | |
3067 | ||
3068 | /* This is called by the socket code when instantiating a new | |
3069 | * service for a stream socket and the socket needs to be | |
3070 | * configured. */ | |
3071 | ||
4cd1fbcc | 3072 | if (s->meta.load_state != UNIT_LOADED) |
4f2d528d LP |
3073 | return -EINVAL; |
3074 | ||
3075 | if (s->socket_fd >= 0) | |
3076 | return -EBUSY; | |
3077 | ||
3078 | if (s->state != SERVICE_DEAD) | |
3079 | return -EAGAIN; | |
3080 | ||
3081 | s->socket_fd = fd; | |
701cc384 | 3082 | s->got_socket_fd = true; |
f976f3f6 | 3083 | s->accept_socket = sock; |
6cf6bbc2 | 3084 | |
4f2d528d LP |
3085 | return 0; |
3086 | } | |
3087 | ||
fdf20a31 | 3088 | static void service_reset_failed(Unit *u) { |
5632e374 LP |
3089 | Service *s = SERVICE(u); |
3090 | ||
3091 | assert(s); | |
3092 | ||
fdf20a31 | 3093 | if (s->state == SERVICE_FAILED) |
5632e374 LP |
3094 | service_set_state(s, SERVICE_DEAD); |
3095 | ||
3096 | s->failure = false; | |
3097 | } | |
3098 | ||
94f04347 LP |
3099 | static const char* const service_state_table[_SERVICE_STATE_MAX] = { |
3100 | [SERVICE_DEAD] = "dead", | |
3101 | [SERVICE_START_PRE] = "start-pre", | |
3102 | [SERVICE_START] = "start", | |
3103 | [SERVICE_START_POST] = "start-post", | |
3104 | [SERVICE_RUNNING] = "running", | |
80876c20 | 3105 | [SERVICE_EXITED] = "exited", |
94f04347 LP |
3106 | [SERVICE_RELOAD] = "reload", |
3107 | [SERVICE_STOP] = "stop", | |
3108 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", | |
3109 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
3110 | [SERVICE_STOP_POST] = "stop-post", | |
3111 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
3112 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 3113 | [SERVICE_FAILED] = "failed", |
94f04347 LP |
3114 | [SERVICE_AUTO_RESTART] = "auto-restart", |
3115 | }; | |
3116 | ||
3117 | DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); | |
3118 | ||
3119 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { | |
525ee6f4 LP |
3120 | [SERVICE_RESTART_NO] = "no", |
3121 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
50caaedb LP |
3122 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", |
3123 | [SERVICE_RESTART_ON_ABORT] = "on-abort", | |
3124 | [SERVICE_RESTART_ALWAYS] = "always" | |
94f04347 LP |
3125 | }; |
3126 | ||
3127 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
3128 | ||
3129 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
94f04347 | 3130 | [SERVICE_SIMPLE] = "simple", |
0d624a78 | 3131 | [SERVICE_FORKING] = "forking", |
34e9ba66 | 3132 | [SERVICE_ONESHOT] = "oneshot", |
8c47c732 LP |
3133 | [SERVICE_DBUS] = "dbus", |
3134 | [SERVICE_NOTIFY] = "notify" | |
94f04347 LP |
3135 | }; |
3136 | ||
3137 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
3138 | ||
e537352b | 3139 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
94f04347 LP |
3140 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", |
3141 | [SERVICE_EXEC_START] = "ExecStart", | |
3142 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
3143 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
3144 | [SERVICE_EXEC_STOP] = "ExecStop", | |
3145 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
3146 | }; | |
3147 | ||
3148 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
3149 | ||
c952c6ec LP |
3150 | static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = { |
3151 | [NOTIFY_NONE] = "none", | |
3152 | [NOTIFY_MAIN] = "main", | |
3153 | [NOTIFY_ALL] = "all" | |
3154 | }; | |
3155 | ||
3156 | DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess); | |
3157 | ||
87f0e418 | 3158 | const UnitVTable service_vtable = { |
5cb5a6ff | 3159 | .suffix = ".service", |
9e58ff9c | 3160 | .show_status = true, |
5cb5a6ff | 3161 | |
034c6ed7 LP |
3162 | .init = service_init, |
3163 | .done = service_done, | |
a16e1123 LP |
3164 | .load = service_load, |
3165 | ||
3166 | .coldplug = service_coldplug, | |
034c6ed7 | 3167 | |
5cb5a6ff LP |
3168 | .dump = service_dump, |
3169 | ||
3170 | .start = service_start, | |
3171 | .stop = service_stop, | |
3172 | .reload = service_reload, | |
3173 | ||
034c6ed7 LP |
3174 | .can_reload = service_can_reload, |
3175 | ||
a16e1123 LP |
3176 | .serialize = service_serialize, |
3177 | .deserialize_item = service_deserialize_item, | |
3178 | ||
5cb5a6ff | 3179 | .active_state = service_active_state, |
10a94420 | 3180 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 3181 | |
07459bb6 | 3182 | #ifdef HAVE_SYSV_COMPAT |
701cc384 | 3183 | .check_gc = service_check_gc, |
07459bb6 | 3184 | #endif |
701cc384 LP |
3185 | .check_snapshot = service_check_snapshot, |
3186 | ||
034c6ed7 LP |
3187 | .sigchld_event = service_sigchld_event, |
3188 | .timer_event = service_timer_event, | |
2c4104f0 | 3189 | |
fdf20a31 | 3190 | .reset_failed = service_reset_failed, |
5632e374 | 3191 | |
8e274523 | 3192 | .cgroup_notify_empty = service_cgroup_notify_event, |
8c47c732 | 3193 | .notify_message = service_notify_message, |
8e274523 | 3194 | |
05e343b7 LP |
3195 | .bus_name_owner_change = service_bus_name_owner_change, |
3196 | .bus_query_pid_done = service_bus_query_pid_done, | |
3197 | ||
c4e2ceae | 3198 | .bus_interface = "org.freedesktop.systemd1.Service", |
4139c1b2 | 3199 | .bus_message_handler = bus_service_message_handler, |
c4e2ceae | 3200 | .bus_invalidating_properties = bus_service_invalidating_properties, |
4139c1b2 | 3201 | |
07459bb6 | 3202 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 | 3203 | .enumerate = service_enumerate |
07459bb6 | 3204 | #endif |
5cb5a6ff | 3205 | }; |