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