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