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