]> git.ipfire.org Git - thirdparty/systemd.git/blame - service.c
fix quoted parser
[thirdparty/systemd.git] / service.c
CommitLineData
5cb5a6ff
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3#include <errno.h>
034c6ed7 4#include <signal.h>
5cb5a6ff 5
87f0e418 6#include "unit.h"
5cb5a6ff
LP
7#include "service.h"
8#include "load-fragment.h"
9#include "load-dropin.h"
034c6ed7
LP
10#include "log.h"
11
87f0e418
LP
12static const UnitActiveState state_table[_SERVICE_STATE_MAX] = {
13 [SERVICE_DEAD] = UNIT_INACTIVE,
14 [SERVICE_START_PRE] = UNIT_ACTIVATING,
15 [SERVICE_START] = UNIT_ACTIVATING,
16 [SERVICE_START_POST] = UNIT_ACTIVATING,
17 [SERVICE_RUNNING] = UNIT_ACTIVE,
18 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
19 [SERVICE_STOP] = UNIT_DEACTIVATING,
20 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
21 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
22 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
23 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
24 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
25 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
26 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
034c6ed7 27};
5cb5a6ff 28
87f0e418
LP
29static void service_done(Unit *u) {
30 Service *s = SERVICE(u);
44d8db9e
LP
31
32 assert(s);
33
34 free(s->pid_file);
35 s->pid_file = NULL;
36
37 exec_context_done(&s->exec_context);
38 exec_command_free_array(s->exec_command, _SERVICE_EXEC_MAX);
39 s->control_command = NULL;
40
41 /* This will leak a process, but at least no memory or any of
42 * our resources */
43 if (s->main_pid > 0) {
87f0e418 44 unit_unwatch_pid(u, s->main_pid);
44d8db9e
LP
45 s->main_pid = 0;
46 }
47
48 if (s->control_pid > 0) {
87f0e418 49 unit_unwatch_pid(u, s->control_pid);
44d8db9e
LP
50 s->control_pid = 0;
51 }
52
87f0e418 53 unit_unwatch_timer(u, &s->timer_id);
44d8db9e
LP
54}
55
5cb5a6ff
LP
56static int service_load_sysv(Service *s) {
57 assert(s);
58
59 /* Load service data from SysV init scripts, preferably with
60 * LSB headers ... */
61
62 return -ENOENT;
63}
64
87f0e418 65static int service_init(Unit *u) {
5cb5a6ff 66 int r;
87f0e418 67 Service *s = SERVICE(u);
5cb5a6ff
LP
68
69 assert(s);
70
034c6ed7
LP
71 /* First, reset everything to the defaults, in case this is a
72 * reload */
73
74 s->type = 0;
75 s->restart = 0;
76
77 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
78 s->restart_usec = DEFAULT_RESTART_USEC;
79
80 exec_context_init(&s->exec_context);
81
82 s->timer_id = -1;
83
84 s->state = SERVICE_DEAD;
5cb5a6ff
LP
85
86 /* Load a .service file */
87f0e418 87 r = unit_load_fragment(u);
5cb5a6ff
LP
88
89 /* Load a classic init script as a fallback */
90 if (r == -ENOENT)
91 r = service_load_sysv(s);
92
44d8db9e 93 if (r < 0) {
87f0e418 94 service_done(u);
5cb5a6ff 95 return r;
44d8db9e 96 }
5cb5a6ff
LP
97
98 /* Load dropin directory data */
87f0e418
LP
99 if ((r = unit_load_dropin(u)) < 0) {
100 service_done(u);
5cb5a6ff 101 return r;
034c6ed7
LP
102 }
103
44d8db9e 104 return 0;
034c6ed7
LP
105}
106
87f0e418 107static void service_dump(Unit *u, FILE *f, const char *prefix) {
5cb5a6ff
LP
108
109 static const char* const state_table[_SERVICE_STATE_MAX] = {
110 [SERVICE_DEAD] = "dead",
111 [SERVICE_START_PRE] = "start-pre",
112 [SERVICE_START] = "start",
113 [SERVICE_START_POST] = "post",
114 [SERVICE_RUNNING] = "running",
5cb5a6ff 115 [SERVICE_RELOAD] = "reload",
5cb5a6ff 116 [SERVICE_STOP] = "stop",
034c6ed7
LP
117 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
118 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
5cb5a6ff 119 [SERVICE_STOP_POST] = "stop-post",
034c6ed7
LP
120 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
121 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
122 [SERVICE_MAINTAINANCE] = "maintainance",
123 [SERVICE_AUTO_RESTART] = "auto-restart",
5cb5a6ff
LP
124 };
125
126 static const char* const command_table[_SERVICE_EXEC_MAX] = {
034c6ed7
LP
127 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
128 [SERVICE_EXEC_START] = "ExecStart",
129 [SERVICE_EXEC_START_POST] = "ExecStartPost",
130 [SERVICE_EXEC_RELOAD] = "ExecReload",
131 [SERVICE_EXEC_STOP] = "ExecStop",
132 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
5cb5a6ff
LP
133 };
134
135 ServiceExecCommand c;
87f0e418 136 Service *s = SERVICE(u);
44d8db9e 137 char *prefix2;
5cb5a6ff
LP
138
139 assert(s);
140
44d8db9e
LP
141 prefix2 = strappend(prefix, "\t");
142 if (!prefix2)
143 prefix2 = "";
144
5cb5a6ff
LP
145 fprintf(f,
146 "%sService State: %s\n",
147 prefix, state_table[s->state]);
148
034c6ed7
LP
149 if (s->pid_file)
150 fprintf(f,
151 "%sPIDFile: %s\n",
152 prefix, s->pid_file);
153
154
5cb5a6ff
LP
155 exec_context_dump(&s->exec_context, f, prefix);
156
157 for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
5cb5a6ff 158
44d8db9e
LP
159 if (!s->exec_command[c])
160 continue;
161
162 fprintf(f, "%s→ %s:\n",
163 prefix, command_table[c]);
164
165 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff 166 }
44d8db9e
LP
167
168 free(prefix2);
5cb5a6ff
LP
169}
170
034c6ed7
LP
171static int service_load_pid_file(Service *s) {
172 char *k;
173 unsigned long p;
174 int r;
175
176 assert(s);
177
178 if (s->main_pid_known)
179 return 0;
180
181 if (!s->pid_file)
182 return -ENOENT;
183
184 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
185 return r;
186
187 if ((r = safe_atolu(k, &p)) < 0) {
188 free(k);
189 return r;
190 }
191
192 if ((unsigned long) (pid_t) p != p)
193 return -ERANGE;
194
195 s->main_pid = p;
196 s->main_pid_known = true;
197
198 return 0;
199}
200
201static void service_set_state(Service *s, ServiceState state) {
202 ServiceState old_state;
5cb5a6ff
LP
203 assert(s);
204
034c6ed7 205 old_state = s->state;
5cb5a6ff 206 s->state = state;
034c6ed7
LP
207
208 if (state != SERVICE_START_PRE &&
209 state != SERVICE_START &&
210 state != SERVICE_START_POST &&
211 state != SERVICE_RELOAD &&
212 state != SERVICE_STOP &&
213 state != SERVICE_STOP_SIGTERM &&
214 state != SERVICE_STOP_SIGKILL &&
215 state != SERVICE_STOP_POST &&
216 state != SERVICE_FINAL_SIGTERM &&
217 state != SERVICE_FINAL_SIGKILL &&
218 state != SERVICE_AUTO_RESTART)
87f0e418 219 unit_unwatch_timer(UNIT(s), &s->timer_id);
034c6ed7
LP
220
221 if (state != SERVICE_START_POST &&
222 state != SERVICE_RUNNING &&
223 state != SERVICE_RELOAD &&
224 state != SERVICE_STOP &&
225 state != SERVICE_STOP_SIGTERM &&
226 state != SERVICE_STOP_SIGKILL)
227 if (s->main_pid >= 0) {
87f0e418 228 unit_unwatch_pid(UNIT(s), s->main_pid);
034c6ed7
LP
229 s->main_pid = 0;
230 }
231
232 if (state != SERVICE_START_PRE &&
233 state != SERVICE_START &&
234 state != SERVICE_START_POST &&
235 state != SERVICE_RELOAD &&
236 state != SERVICE_STOP &&
237 state != SERVICE_STOP_SIGTERM &&
238 state != SERVICE_STOP_SIGKILL &&
239 state != SERVICE_STOP_POST &&
240 state != SERVICE_FINAL_SIGTERM &&
241 state != SERVICE_FINAL_SIGKILL)
242 if (s->control_pid >= 0) {
87f0e418 243 unit_unwatch_pid(UNIT(s), s->control_pid);
034c6ed7
LP
244 s->control_pid = 0;
245 }
246
247 if (state != SERVICE_START_PRE &&
248 state != SERVICE_START &&
249 state != SERVICE_START_POST &&
250 state != SERVICE_RELOAD &&
251 state != SERVICE_STOP &&
252 state != SERVICE_STOP_POST)
253 s->control_command = NULL;
254
87f0e418 255 unit_notify(UNIT(s), state_table[old_state], state_table[s->state]);
034c6ed7
LP
256}
257
44d8db9e
LP
258static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
259 Iterator i;
260 int r;
261 int *rfds = NULL;
262 unsigned rn_fds = 0;
263 char *t;
264
265 assert(s);
266 assert(fds);
267 assert(n_fds);
268
87f0e418 269 SET_FOREACH(t, UNIT(s)->meta.names, i) {
44d8db9e 270 char *k;
87f0e418 271 Unit *p;
44d8db9e
LP
272 int *cfds;
273 unsigned cn_fds;
274
275 /* Look for all socket objects that go by any of our
87f0e418 276 * units and collect their fds */
44d8db9e 277
87f0e418 278 if (!(k = unit_name_change_suffix(t, ".socket"))) {
44d8db9e
LP
279 r = -ENOMEM;
280 goto fail;
281 }
282
87f0e418 283 p = manager_get_unit(UNIT(s)->meta.manager, k);
44d8db9e
LP
284 free(k);
285
286 if ((r = socket_collect_fds(SOCKET(p), &cfds, &cn_fds)) < 0)
287 goto fail;
288
289 if (!cfds)
290 continue;
291
292 if (!rfds) {
293 rfds = cfds;
294 rn_fds = cn_fds;
295 } else {
296 int *t;
297
298 if (!(t = new(int, rn_fds+cn_fds))) {
299 free(cfds);
300 r = -ENOMEM;
301 goto fail;
302 }
303
304 memcpy(t, rfds, rn_fds);
305 memcpy(t+rn_fds, cfds, cn_fds);
306 free(rfds);
307 free(cfds);
308
309 rfds = t;
310 rn_fds = rn_fds+cn_fds;
311 }
312 }
313
314 *fds = rfds;
315 *n_fds = rn_fds;
316 return 0;
317
318fail:
319 free(rfds);
320 return r;
321}
322
323static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
034c6ed7
LP
324 pid_t pid;
325 int r;
44d8db9e
LP
326 int *fds = NULL;
327 unsigned n_fds = 0;
034c6ed7
LP
328
329 assert(s);
330 assert(c);
331 assert(_pid);
332
44d8db9e
LP
333 if (pass_fds)
334 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
335 goto fail;
336
034c6ed7 337 if (timeout) {
87f0e418 338 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_id)) < 0)
034c6ed7
LP
339 goto fail;
340 } else
87f0e418 341 unit_unwatch_timer(UNIT(s), &s->timer_id);
034c6ed7 342
44d8db9e 343 if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
034c6ed7
LP
344 goto fail;
345
87f0e418 346 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
034c6ed7
LP
347 /* FIXME: we need to do something here */
348 goto fail;
349
44d8db9e 350 free(fds);
034c6ed7
LP
351 *_pid = pid;
352
5cb5a6ff 353 return 0;
034c6ed7
LP
354
355fail:
44d8db9e
LP
356 free(fds);
357
034c6ed7 358 if (timeout)
87f0e418 359 unit_unwatch_timer(UNIT(s), &s->timer_id);
034c6ed7
LP
360
361 return r;
362}
363
364static void service_enter_dead(Service *s, bool success, bool allow_restart) {
365 int r;
366 assert(s);
367
368 if (!success)
369 s->failure = true;
370
371 if (allow_restart &&
372 (s->restart == SERVICE_RESTART_ALWAYS ||
373 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
374
87f0e418 375 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_id)) < 0)
034c6ed7
LP
376 goto fail;
377
378 service_set_state(s, SERVICE_AUTO_RESTART);
379 } else
380 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
381
382 return;
383
384fail:
87f0e418 385 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
386 service_enter_dead(s, false, false);
387}
388
389static void service_enter_signal(Service *s, ServiceState state, bool success);
390
391static void service_enter_stop_post(Service *s, bool success) {
392 int r;
393 assert(s);
394
395 if (!success)
396 s->failure = true;
397
398 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
399
44d8db9e 400 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
034c6ed7
LP
401 goto fail;
402
403 service_set_state(s, SERVICE_STOP_POST);
404 } else
405 service_enter_dead(s, true, true);
406
407 return;
408
409fail:
87f0e418 410 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
411 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
412}
413
414static void service_enter_signal(Service *s, ServiceState state, bool success) {
415 int r;
416 bool sent = false;
417
418 assert(s);
419
420 if (!success)
421 s->failure = true;
422
423 if (s->main_pid > 0 || s->control_pid > 0) {
424 int sig;
425
426 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
427
428 r = 0;
429 if (s->main_pid > 0) {
430 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
431 r = -errno;
432 else
433 sent = true;
434 }
435
436 if (s->control_pid > 0) {
437 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
438 r = -errno;
439 else
440 sent = true;
441 }
442
443 if (r < 0)
444 goto fail;
445
446 service_set_state(s, state);
447 } else
448 service_enter_dead(s, true, true);
449
450 return;
451
452fail:
87f0e418 453 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
454
455 if (sent) {
456 s->failure = true;
457 service_set_state(s, state);
458 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
459 service_enter_stop_post(s, false);
460 else
461 service_enter_dead(s, false, true);
462}
463
464static void service_enter_stop(Service *s, bool success) {
465 int r;
466 assert(s);
467
468 if (!success)
469 s->failure = true;
470
471 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
472
44d8db9e 473 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
034c6ed7
LP
474 goto fail;
475
476 service_set_state(s, SERVICE_STOP);
477 } else
478 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
479
480 return;
481
482fail:
87f0e418 483 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
484 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
485}
486
487static void service_enter_start_post(Service *s) {
488 int r;
489 assert(s);
490
491 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
492
44d8db9e 493 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
034c6ed7
LP
494 goto fail;
495
496 service_set_state(s, SERVICE_START_POST);
497 } else
498 service_set_state(s, SERVICE_RUNNING);
499
500 return;
501
502fail:
87f0e418 503 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
504 service_enter_stop(s, false);
505}
506
507static void service_enter_start(Service *s) {
508 pid_t pid;
509 int r;
510
511 assert(s);
512
513 assert(s->exec_command[SERVICE_EXEC_START]);
514 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
515
44d8db9e 516 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
034c6ed7
LP
517 goto fail;
518
519 if (s->type == SERVICE_SIMPLE) {
520 /* For simple services we immediately start
521 * the START_POST binaries. */
522
523 s->main_pid = pid;
524 s->main_pid_known = true;
525 service_enter_start_post(s);
526
527 } else if (s->type == SERVICE_FORKING) {
528
529 /* For forking services we wait until the start
530 * process exited. */
531
532 s->control_pid = pid;
533 s->control_command = s->exec_command[SERVICE_EXEC_START];
534 service_set_state(s, SERVICE_START);
535 } else
536 assert_not_reached("Unknown service type");
537
538 return;
539
540fail:
87f0e418 541 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
542 service_enter_stop(s, false);
543}
544
545static void service_enter_start_pre(Service *s) {
546 int r;
547
548 assert(s);
549
550 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
551
44d8db9e 552 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
034c6ed7
LP
553 goto fail;
554
555 service_set_state(s, SERVICE_START_PRE);
556 } else
557 service_enter_start(s);
558
559 return;
560
561fail:
87f0e418 562 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
563 service_enter_dead(s, false, true);
564}
565
566static void service_enter_restart(Service *s) {
567 int r;
568 assert(s);
569
87f0e418 570 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
034c6ed7
LP
571 goto fail;
572
87f0e418 573 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
034c6ed7
LP
574 service_enter_dead(s, true, false);
575 return;
576
577fail:
578
87f0e418 579 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
580 service_enter_dead(s, false, false);
581}
582
583static void service_enter_reload(Service *s) {
584 int r;
585
586 assert(s);
587
588 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
589
44d8db9e 590 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
034c6ed7
LP
591 goto fail;
592
593 service_set_state(s, SERVICE_RELOAD);
594 } else
595 service_set_state(s, SERVICE_RUNNING);
596
597 return;
598
599fail:
87f0e418 600 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
601 service_enter_stop(s, false);
602}
603
604static void service_run_next(Service *s, bool success) {
605 int r;
606
607 assert(s);
608 assert(s->control_command);
609 assert(s->control_command->command_next);
610
611 if (!success)
612 s->failure = true;
613
614 s->control_command = s->control_command->command_next;
615
44d8db9e 616 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
034c6ed7
LP
617 goto fail;
618
619 return;
620
621fail:
87f0e418 622 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
034c6ed7
LP
623
624 if (s->state == SERVICE_STOP)
625 service_enter_stop_post(s, false);
626 else if (s->state == SERVICE_STOP_POST)
627 service_enter_dead(s, false, true);
628 else
629 service_enter_stop(s, false);
5cb5a6ff
LP
630}
631
87f0e418
LP
632static int service_start(Unit *u) {
633 Service *s = SERVICE(u);
5cb5a6ff
LP
634
635 assert(s);
636
034c6ed7
LP
637 /* We cannot fulfill this request right now, try again later
638 * please! */
639 if (s->state == SERVICE_STOP ||
640 s->state == SERVICE_STOP_SIGTERM ||
641 s->state == SERVICE_STOP_SIGKILL ||
642 s->state == SERVICE_STOP_POST ||
643 s->state == SERVICE_FINAL_SIGTERM ||
644 s->state == SERVICE_FINAL_SIGKILL)
5cb5a6ff
LP
645 return -EAGAIN;
646
034c6ed7
LP
647 /* Already on it! */
648 if (s->state == SERVICE_START_PRE ||
649 s->state == SERVICE_START ||
650 s->state == SERVICE_START_POST)
651 return 0;
652
653 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
5cb5a6ff 654
034c6ed7
LP
655 s->failure = false;
656 s->main_pid_known = false;
657
658 service_enter_start_pre(s);
659 return 0;
5cb5a6ff
LP
660}
661
87f0e418
LP
662static int service_stop(Unit *u) {
663 Service *s = SERVICE(u);
5cb5a6ff
LP
664
665 assert(s);
666
034c6ed7
LP
667 if (s->state == SERVICE_START_PRE ||
668 s->state == SERVICE_START ||
669 s->state == SERVICE_START_POST ||
670 s->state == SERVICE_RELOAD)
671 return -EAGAIN;
672
673 if (s->state == SERVICE_AUTO_RESTART) {
674 service_set_state(s, SERVICE_DEAD);
675 return 0;
676 }
677
678 assert(s->state == SERVICE_RUNNING);
5cb5a6ff 679
034c6ed7 680 service_enter_stop(s, true);
5cb5a6ff
LP
681 return 0;
682}
683
87f0e418
LP
684static int service_reload(Unit *u) {
685 Service *s = SERVICE(u);
034c6ed7
LP
686
687 assert(s);
688
689 assert(s->state == SERVICE_RUNNING);
690
691 service_enter_reload(s);
5cb5a6ff
LP
692 return 0;
693}
694
87f0e418
LP
695static bool service_can_reload(Unit *u) {
696 Service *s = SERVICE(u);
034c6ed7
LP
697
698 assert(s);
699
700 return !!s->exec_command[SERVICE_EXEC_RELOAD];
701}
702
87f0e418
LP
703static UnitActiveState service_active_state(Unit *u) {
704 assert(u);
5cb5a6ff 705
87f0e418 706 return state_table[SERVICE(u)->state];
034c6ed7
LP
707}
708
709static int main_pid_good(Service *s) {
710 assert(s);
711
712 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
713 * don't know */
714
715 /* If we know the pid file, then lets just check if it is
716 * still valid */
717 if (s->main_pid_known)
718 return s->main_pid > 0;
719
720 /* We don't know the pid */
721 return -1;
722}
723
724static bool control_pid_good(Service *s) {
725 assert(s);
5cb5a6ff 726
034c6ed7 727 return s->control_pid > 0;
5cb5a6ff
LP
728}
729
87f0e418
LP
730static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
731 Service *s = SERVICE(u);
034c6ed7 732 bool success;
5cb5a6ff
LP
733
734 assert(s);
034c6ed7
LP
735 assert(pid >= 0);
736
737 success = code == CLD_EXITED || status == 0;
738 s->failure = s->failure || !success;
739
740 if (s->main_pid == pid) {
741
742 exec_status_fill(&s->main_exec_status, pid, code, status);
743 s->main_pid = 0;
744
745 if (s->type == SERVICE_SIMPLE) {
746 assert(s->exec_command[SERVICE_EXEC_START]);
747 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
748 }
749
87f0e418 750 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
034c6ed7
LP
751
752 /* The service exited, so the service is officially
753 * gone. */
754
755 switch (s->state) {
756
757 case SERVICE_START_POST:
758 case SERVICE_RELOAD:
759 case SERVICE_STOP:
760 /* Need to wait until the operation is
761 * done */
762 break;
763
764 case SERVICE_RUNNING:
765 service_enter_stop(s, success);
766 break;
767
768 case SERVICE_STOP_SIGTERM:
769 case SERVICE_STOP_SIGKILL:
770
771 if (!control_pid_good(s))
772 service_enter_stop_post(s, success);
5cb5a6ff 773
034c6ed7
LP
774 /* If there is still a control process, wait for that first */
775 break;
5cb5a6ff 776
034c6ed7
LP
777 default:
778 assert_not_reached("Uh, main process died at wrong time.");
779 }
5cb5a6ff 780
034c6ed7
LP
781 } else if (s->control_pid == pid) {
782 assert(s->control_command);
783
784 exec_status_fill(&s->control_command->exec_status, pid, code, status);
785 s->control_pid = 0;
786
87f0e418 787 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
034c6ed7
LP
788
789 /* If we are shutting things down anyway we
790 * don't care about failing commands. */
791
792 if (s->control_command->command_next &&
793 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
794
795 /* There is another command to *
796 * execute, so let's do that. */
797
798 service_run_next(s, success);
799
800 else {
801 /* No further commands for this step, so let's
802 * figure out what to do next */
803
804 switch (s->state) {
805
806 case SERVICE_START_PRE:
807 if (success)
808 service_enter_start(s);
809 else
810 service_enter_stop(s, false);
811 break;
812
813 case SERVICE_START:
814 assert(s->type == SERVICE_FORKING);
815
816 /* Let's try to load the pid
817 * file here if we can. We
818 * ignore the return value,
819 * since the PID file might
820 * actually be created by a
821 * START_POST script */
822
823 if (success) {
824 if (s->pid_file)
825 service_load_pid_file(s);
826
827 service_enter_start_post(s);
828 } else
829 service_enter_stop(s, false);
830
831 break;
832
833 case SERVICE_START_POST:
834 if (success && s->pid_file && !s->main_pid_known) {
835 int r;
836
837 /* Hmm, let's see if we can
838 * load the pid now after the
839 * start-post scripts got
840 * executed. */
841
842 if ((r = service_load_pid_file(s)) < 0)
87f0e418 843 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
034c6ed7
LP
844 }
845
846 /* Fall through */
847
848 case SERVICE_RELOAD:
849 if (success) {
850 if (main_pid_good(s) != 0)
851 service_set_state(s, SERVICE_RUNNING);
852 else
853 service_enter_stop(s, true);
854 } else
855 service_enter_stop(s, false);
856
857 break;
858
859 case SERVICE_STOP:
860 if (main_pid_good(s) > 0)
861 /* Still not dead and we know the PID? Let's go hunting. */
862 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
863 else
864 service_enter_stop_post(s, success);
865 break;
866
867 case SERVICE_STOP_SIGTERM:
868 case SERVICE_STOP_SIGKILL:
869 if (main_pid_good(s) <= 0)
870 service_enter_stop_post(s, success);
871
872 /* If there is still a service
873 * process around, wait until
874 * that one quit, too */
875 break;
876
877 case SERVICE_STOP_POST:
878 case SERVICE_FINAL_SIGTERM:
879 case SERVICE_FINAL_SIGKILL:
880 service_enter_dead(s, success, true);
881 break;
882
883 default:
884 assert_not_reached("Uh, control process died at wrong time.");
885 }
886 }
887 } else
888 assert_not_reached("Got SIGCHLD for unkown PID");
889}
890
87f0e418
LP
891static void service_timer_event(Unit *u, int id, uint64_t elapsed) {
892 Service *s = SERVICE(u);
034c6ed7
LP
893
894 assert(s);
895 assert(elapsed == 1);
896
897 assert(s->timer_id == id);
898
899 switch (s->state) {
900
901 case SERVICE_START_PRE:
902 case SERVICE_START:
903 case SERVICE_START_POST:
904 case SERVICE_RELOAD:
87f0e418 905 log_warning("%s operation timed out. Stopping.", unit_id(u));
034c6ed7
LP
906 service_enter_stop(s, false);
907 break;
908
909 case SERVICE_STOP:
87f0e418 910 log_warning("%s stopping timed out. Terminating.", unit_id(u));
034c6ed7
LP
911 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
912 break;
913
914 case SERVICE_STOP_SIGTERM:
87f0e418 915 log_warning("%s stopping timed out. Killing.", unit_id(u));
034c6ed7
LP
916 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
917 break;
918
919 case SERVICE_STOP_SIGKILL:
920 /* Uh, wie sent a SIGKILL and it is still not gone?
921 * Must be something we cannot kill, so let's just be
922 * weirded out and continue */
923
87f0e418 924 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
034c6ed7
LP
925 service_enter_stop_post(s, false);
926 break;
927
928 case SERVICE_STOP_POST:
87f0e418 929 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
034c6ed7
LP
930 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
931 break;
932
933 case SERVICE_FINAL_SIGTERM:
87f0e418 934 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
034c6ed7
LP
935 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
936 break;
937
938 case SERVICE_FINAL_SIGKILL:
87f0e418 939 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
034c6ed7
LP
940 service_enter_dead(s, false, true);
941 break;
942
943 case SERVICE_AUTO_RESTART:
87f0e418 944 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
034c6ed7
LP
945 service_enter_restart(s);
946 break;
947
948 default:
949 assert_not_reached("Timeout at wrong time.");
950 }
5cb5a6ff
LP
951}
952
87f0e418 953const UnitVTable service_vtable = {
5cb5a6ff
LP
954 .suffix = ".service",
955
034c6ed7
LP
956 .init = service_init,
957 .done = service_done,
958
5cb5a6ff
LP
959 .dump = service_dump,
960
961 .start = service_start,
962 .stop = service_stop,
963 .reload = service_reload,
964
034c6ed7
LP
965 .can_reload = service_can_reload,
966
5cb5a6ff
LP
967 .active_state = service_active_state,
968
034c6ed7
LP
969 .sigchld_event = service_sigchld_event,
970 .timer_event = service_timer_event,
5cb5a6ff 971};