chiark / gitweb /
core: implement serialization/deserialization of fd store elements
[elogind.git] / src / core / service.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <signal.h>
24 #include <dirent.h>
25 #include <unistd.h>
26
27 #include "async.h"
28 #include "manager.h"
29 #include "unit.h"
30 #include "service.h"
31 #include "load-fragment.h"
32 #include "load-dropin.h"
33 #include "log.h"
34 #include "strv.h"
35 #include "unit-name.h"
36 #include "unit-printf.h"
37 #include "dbus-service.h"
38 #include "special.h"
39 #include "exit-status.h"
40 #include "def.h"
41 #include "path-util.h"
42 #include "util.h"
43 #include "utf8.h"
44 #include "env-util.h"
45 #include "fileio.h"
46 #include "bus-error.h"
47 #include "bus-util.h"
48 #include "bus-kernel.h"
49
50 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
51         [SERVICE_DEAD] = UNIT_INACTIVE,
52         [SERVICE_START_PRE] = UNIT_ACTIVATING,
53         [SERVICE_START] = UNIT_ACTIVATING,
54         [SERVICE_START_POST] = UNIT_ACTIVATING,
55         [SERVICE_RUNNING] = UNIT_ACTIVE,
56         [SERVICE_EXITED] = UNIT_ACTIVE,
57         [SERVICE_RELOAD] = UNIT_RELOADING,
58         [SERVICE_STOP] = UNIT_DEACTIVATING,
59         [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING,
60         [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
61         [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
62         [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
63         [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
64         [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
65         [SERVICE_FAILED] = UNIT_FAILED,
66         [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
67 };
68
69 /* For Type=idle we never want to delay any other jobs, hence we
70  * consider idle jobs active as soon as we start working on them */
71 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
72         [SERVICE_DEAD] = UNIT_INACTIVE,
73         [SERVICE_START_PRE] = UNIT_ACTIVE,
74         [SERVICE_START] = UNIT_ACTIVE,
75         [SERVICE_START_POST] = UNIT_ACTIVE,
76         [SERVICE_RUNNING] = UNIT_ACTIVE,
77         [SERVICE_EXITED] = UNIT_ACTIVE,
78         [SERVICE_RELOAD] = UNIT_RELOADING,
79         [SERVICE_STOP] = UNIT_DEACTIVATING,
80         [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING,
81         [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
82         [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
83         [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
84         [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
85         [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
86         [SERVICE_FAILED] = UNIT_FAILED,
87         [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
88 };
89
90 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
91 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
92 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
93
94 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
95 static void service_enter_reload_by_notify(Service *s);
96
97 static void service_init(Unit *u) {
98         Service *s = SERVICE(u);
99
100         assert(u);
101         assert(u->load_state == UNIT_STUB);
102
103         s->timeout_start_usec = u->manager->default_timeout_start_usec;
104         s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
105         s->restart_usec = u->manager->default_restart_usec;
106         s->type = _SERVICE_TYPE_INVALID;
107         s->socket_fd = -1;
108         s->bus_endpoint_fd = -1;
109         s->guess_main_pid = true;
110
111         RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
112
113         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
114 }
115
116 static void service_unwatch_control_pid(Service *s) {
117         assert(s);
118
119         if (s->control_pid <= 0)
120                 return;
121
122         unit_unwatch_pid(UNIT(s), s->control_pid);
123         s->control_pid = 0;
124 }
125
126 static void service_unwatch_main_pid(Service *s) {
127         assert(s);
128
129         if (s->main_pid <= 0)
130                 return;
131
132         unit_unwatch_pid(UNIT(s), s->main_pid);
133         s->main_pid = 0;
134 }
135
136 static void service_unwatch_pid_file(Service *s) {
137         if (!s->pid_file_pathspec)
138                 return;
139
140         log_unit_debug(UNIT(s)->id, "Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
141         path_spec_unwatch(s->pid_file_pathspec);
142         path_spec_done(s->pid_file_pathspec);
143         free(s->pid_file_pathspec);
144         s->pid_file_pathspec = NULL;
145 }
146
147 static int service_set_main_pid(Service *s, pid_t pid) {
148         pid_t ppid;
149
150         assert(s);
151
152         if (pid <= 1)
153                 return -EINVAL;
154
155         if (pid == getpid())
156                 return -EINVAL;
157
158         if (s->main_pid == pid && s->main_pid_known)
159                 return 0;
160
161         if (s->main_pid != pid) {
162                 service_unwatch_main_pid(s);
163                 exec_status_start(&s->main_exec_status, pid);
164         }
165
166         s->main_pid = pid;
167         s->main_pid_known = true;
168
169         if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
170                 log_unit_warning(UNIT(s)->id, "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", UNIT(s)->id, pid);
171                 s->main_pid_alien = true;
172         } else
173                 s->main_pid_alien = false;
174
175         return 0;
176 }
177
178 static void service_close_socket_fd(Service *s) {
179         assert(s);
180
181         s->socket_fd = asynchronous_close(s->socket_fd);
182 }
183
184 static void service_connection_unref(Service *s) {
185         assert(s);
186
187         if (!UNIT_ISSET(s->accept_socket))
188                 return;
189
190         socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
191         unit_ref_unset(&s->accept_socket);
192 }
193
194 static void service_stop_watchdog(Service *s) {
195         assert(s);
196
197         s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
198         s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
199 }
200
201 static void service_start_watchdog(Service *s) {
202         int r;
203
204         assert(s);
205
206         if (s->watchdog_usec <= 0)
207                 return;
208
209         if (s->watchdog_event_source) {
210                 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
211                 if (r < 0) {
212                         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to reset watchdog timer: %m", UNIT(s)->id);
213                         return;
214                 }
215
216                 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
217         } else {
218                 r = sd_event_add_time(
219                                 UNIT(s)->manager->event,
220                                 &s->watchdog_event_source,
221                                 CLOCK_MONOTONIC,
222                                 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
223                                 service_dispatch_watchdog, s);
224                 if (r < 0) {
225                         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to add watchdog timer: %m", UNIT(s)->id);
226                         return;
227                 }
228
229                 /* Let's process everything else which might be a sign
230                  * of living before we consider a service died. */
231                 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
232         }
233
234         if (r < 0)
235                 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to install watchdog timer: %m", UNIT(s)->id);
236 }
237
238 static void service_reset_watchdog(Service *s) {
239         assert(s);
240
241         dual_timestamp_get(&s->watchdog_timestamp);
242         service_start_watchdog(s);
243 }
244
245 static void service_fd_store_unlink(ServiceFDStore *fs) {
246
247         if (!fs)
248                 return;
249
250         if (fs->service) {
251                 assert(fs->service->n_fd_store > 0);
252                 LIST_REMOVE(fd_store, fs->service->fd_store, fs);
253                 fs->service->n_fd_store--;
254         }
255
256         if (fs->event_source) {
257                 sd_event_source_set_enabled(fs->event_source, SD_EVENT_OFF);
258                 sd_event_source_unref(fs->event_source);
259         }
260
261         safe_close(fs->fd);
262         free(fs);
263 }
264
265 static void service_release_resources(Unit *u) {
266         Service *s = SERVICE(u);
267
268         assert(s);
269
270         if (!s->fd_store)
271                 return;
272
273         log_debug("Releasing all resources for %s", u->id);
274
275         while (s->fd_store)
276                 service_fd_store_unlink(s->fd_store);
277
278         assert(s->n_fd_store == 0);
279 }
280
281 static void service_done(Unit *u) {
282         Service *s = SERVICE(u);
283
284         assert(s);
285
286         free(s->pid_file);
287         s->pid_file = NULL;
288
289         free(s->status_text);
290         s->status_text = NULL;
291
292         free(s->reboot_arg);
293         s->reboot_arg = NULL;
294
295         s->exec_runtime = exec_runtime_unref(s->exec_runtime);
296         exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
297         s->control_command = NULL;
298         s->main_command = NULL;
299
300         exit_status_set_free(&s->restart_prevent_status);
301         exit_status_set_free(&s->restart_force_status);
302         exit_status_set_free(&s->success_status);
303
304         /* This will leak a process, but at least no memory or any of
305          * our resources */
306         service_unwatch_main_pid(s);
307         service_unwatch_control_pid(s);
308         service_unwatch_pid_file(s);
309
310         if (s->bus_name)  {
311                 unit_unwatch_bus_name(u, s->bus_name);
312                 free(s->bus_name);
313                 s->bus_name = NULL;
314         }
315
316         s->bus_endpoint_fd = safe_close(s->bus_endpoint_fd);
317         service_close_socket_fd(s);
318         service_connection_unref(s);
319
320         unit_ref_unset(&s->accept_socket);
321
322         service_stop_watchdog(s);
323
324         s->timer_event_source = sd_event_source_unref(s->timer_event_source);
325
326         service_release_resources(u);
327 }
328
329 static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
330         ServiceFDStore *fs = userdata;
331
332         assert(e);
333         assert(fs);
334
335         /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
336         service_fd_store_unlink(fs);
337         return 0;
338 }
339
340 static int service_add_fd_store(Service *s, int fd) {
341         ServiceFDStore *fs;
342         int r;
343
344         assert(s);
345         assert(fd >= 0);
346
347         if (s->n_fd_store >= s->n_fd_store_max)
348                 return 0;
349
350         LIST_FOREACH(fd_store, fs, s->fd_store) {
351                 r = same_fd(fs->fd, fd);
352                 if (r < 0)
353                         return r;
354                 if (r > 0) {
355                         /* Already included */
356                         safe_close(fd);
357                         return 1;
358                 }
359         }
360
361         fs = new0(ServiceFDStore, 1);
362         if (!fs)
363                 return -ENOMEM;
364
365         fs->fd = fd;
366         fs->service = s;
367
368         r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fd, 0, on_fd_store_io, fs);
369         if (r < 0) {
370                 free(fs);
371                 return r;
372         }
373
374         LIST_PREPEND(fd_store, s->fd_store, fs);
375         s->n_fd_store++;
376
377         return 1;
378 }
379
380 static int service_add_fd_store_set(Service *s, FDSet *fds) {
381         int r;
382
383         assert(s);
384
385         if (fdset_size(fds) <= 0)
386                 return 0;
387
388         while (s->n_fd_store < s->n_fd_store_max) {
389                 _cleanup_close_ int fd = -1;
390
391                 fd = fdset_steal_first(fds);
392                 if (fd < 0)
393                         break;
394
395                 r = service_add_fd_store(s, fd);
396                 if (r < 0)
397                         return log_unit_error_errno(UNIT(s)->id, r, "%s: Couldn't add fd to fd store: %m", UNIT(s)->id);
398
399                 if (r > 0) {
400                         log_unit_debug(UNIT(s)->id, "%s: added fd to fd store.", UNIT(s)->id);
401                         fd = -1;
402                 }
403         }
404
405         if (fdset_size(fds) > 0)
406                 log_unit_warning(UNIT(s)->id, "%s: tried to store more fds than FDStoreMax=%u allows, closing remaining.", UNIT(s)->id, s->n_fd_store_max);
407
408         return 0;
409 }
410
411 static int service_arm_timer(Service *s, usec_t usec) {
412         int r;
413
414         assert(s);
415
416         if (s->timer_event_source) {
417                 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
418                 if (r < 0)
419                         return r;
420
421                 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
422         }
423
424         return sd_event_add_time(
425                         UNIT(s)->manager->event,
426                         &s->timer_event_source,
427                         CLOCK_MONOTONIC,
428                         now(CLOCK_MONOTONIC) + usec, 0,
429                         service_dispatch_timer, s);
430 }
431
432 static int service_verify(Service *s) {
433         assert(s);
434
435         if (UNIT(s)->load_state != UNIT_LOADED)
436                 return 0;
437
438         if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]) {
439                 log_unit_error(UNIT(s)->id, "%s lacks both ExecStart= and ExecStop= setting. Refusing.", UNIT(s)->id);
440                 return -EINVAL;
441         }
442
443         if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) {
444                 log_unit_error(UNIT(s)->id, "%s has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
445                 return -EINVAL;
446         }
447
448         if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START]) {
449                 log_unit_error(UNIT(s)->id, "%s has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.", UNIT(s)->id);
450                 return -EINVAL;
451         }
452
453         if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) {
454                 log_unit_error(UNIT(s)->id, "%s has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
455                 return -EINVAL;
456         }
457
458         if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
459                 log_unit_error(UNIT(s)->id, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
460                 return -EINVAL;
461         }
462
463         if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) {
464                 log_unit_error(UNIT(s)->id, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
465                 return -EINVAL;
466         }
467
468         if (s->type == SERVICE_DBUS && !s->bus_name) {
469                 log_unit_error(UNIT(s)->id, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
470                 return -EINVAL;
471         }
472
473         if (s->bus_name && s->type != SERVICE_DBUS)
474                 log_unit_warning(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
475
476         if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
477                 log_unit_error(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id);
478                 return -EINVAL;
479         }
480
481         return 0;
482 }
483
484 static int service_add_default_dependencies(Service *s) {
485         int r;
486
487         assert(s);
488
489         /* Add a number of automatic dependencies useful for the
490          * majority of services. */
491
492         /* First, pull in base system */
493         r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true);
494         if (r < 0)
495                 return r;
496
497         /* Second, activate normal shutdown */
498         r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
499         return r;
500 }
501
502 static void service_fix_output(Service *s) {
503         assert(s);
504
505         /* If nothing has been explicitly configured, patch default
506          * output in. If input is socket/tty we avoid this however,
507          * since in that case we want output to default to the same
508          * place as we read input from. */
509
510         if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
511             s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
512             s->exec_context.std_input == EXEC_INPUT_NULL)
513                 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
514
515         if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
516             s->exec_context.std_input == EXEC_INPUT_NULL)
517                 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
518 }
519
520 static int service_load(Unit *u) {
521         Service *s = SERVICE(u);
522         int r;
523
524         assert(s);
525
526         /* Load a .service file */
527         r = unit_load_fragment(u);
528         if (r < 0)
529                 return r;
530
531         /* Still nothing found? Then let's give up */
532         if (u->load_state == UNIT_STUB)
533                 return -ENOENT;
534
535         /* This is a new unit? Then let's add in some extras */
536         if (u->load_state == UNIT_LOADED) {
537
538                 /* We were able to load something, then let's add in
539                  * the dropin directories. */
540                 r = unit_load_dropin(u);
541                 if (r < 0)
542                         return r;
543
544                 if (s->type == _SERVICE_TYPE_INVALID) {
545                         /* Figure out a type automatically */
546                         if (s->bus_name)
547                                 s->type = SERVICE_DBUS;
548                         else if (s->exec_command[SERVICE_EXEC_START])
549                                 s->type = SERVICE_SIMPLE;
550                         else
551                                 s->type = SERVICE_ONESHOT;
552                 }
553
554                 /* Oneshot services have disabled start timeout by default */
555                 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
556                         s->timeout_start_usec = 0;
557
558                 service_fix_output(s);
559
560                 r = unit_patch_contexts(u);
561                 if (r < 0)
562                         return r;
563
564                 r = unit_add_exec_dependencies(u, &s->exec_context);
565                 if (r < 0)
566                         return r;
567
568                 r = unit_add_default_slice(u, &s->cgroup_context);
569                 if (r < 0)
570                         return r;
571
572                 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
573                         s->notify_access = NOTIFY_MAIN;
574
575                 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
576                         s->notify_access = NOTIFY_MAIN;
577
578                 if (s->bus_name) {
579                         r = unit_watch_bus_name(u, s->bus_name);
580                         if (r < 0)
581                                 return r;
582                 }
583
584                 if (u->default_dependencies) {
585                         r = service_add_default_dependencies(s);
586                         if (r < 0)
587
588                                 return r;
589                 }
590         }
591
592         return service_verify(s);
593 }
594
595 static void service_dump(Unit *u, FILE *f, const char *prefix) {
596         ServiceExecCommand c;
597         Service *s = SERVICE(u);
598         const char *prefix2;
599
600         assert(s);
601
602         prefix = strempty(prefix);
603         prefix2 = strappenda(prefix, "\t");
604
605         fprintf(f,
606                 "%sService State: %s\n"
607                 "%sResult: %s\n"
608                 "%sReload Result: %s\n"
609                 "%sPermissionsStartOnly: %s\n"
610                 "%sRootDirectoryStartOnly: %s\n"
611                 "%sRemainAfterExit: %s\n"
612                 "%sGuessMainPID: %s\n"
613                 "%sType: %s\n"
614                 "%sRestart: %s\n"
615                 "%sNotifyAccess: %s\n"
616                 "%sNotifyState: %s\n",
617                 prefix, service_state_to_string(s->state),
618                 prefix, service_result_to_string(s->result),
619                 prefix, service_result_to_string(s->reload_result),
620                 prefix, yes_no(s->permissions_start_only),
621                 prefix, yes_no(s->root_directory_start_only),
622                 prefix, yes_no(s->remain_after_exit),
623                 prefix, yes_no(s->guess_main_pid),
624                 prefix, service_type_to_string(s->type),
625                 prefix, service_restart_to_string(s->restart),
626                 prefix, notify_access_to_string(s->notify_access),
627                 prefix, notify_state_to_string(s->notify_state));
628
629         if (s->control_pid > 0)
630                 fprintf(f,
631                         "%sControl PID: "PID_FMT"\n",
632                         prefix, s->control_pid);
633
634         if (s->main_pid > 0)
635                 fprintf(f,
636                         "%sMain PID: "PID_FMT"\n"
637                         "%sMain PID Known: %s\n"
638                         "%sMain PID Alien: %s\n",
639                         prefix, s->main_pid,
640                         prefix, yes_no(s->main_pid_known),
641                         prefix, yes_no(s->main_pid_alien));
642
643         if (s->pid_file)
644                 fprintf(f,
645                         "%sPIDFile: %s\n",
646                         prefix, s->pid_file);
647
648         if (s->bus_name)
649                 fprintf(f,
650                         "%sBusName: %s\n"
651                         "%sBus Name Good: %s\n",
652                         prefix, s->bus_name,
653                         prefix, yes_no(s->bus_name_good));
654
655         kill_context_dump(&s->kill_context, f, prefix);
656         exec_context_dump(&s->exec_context, f, prefix);
657
658         for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
659
660                 if (!s->exec_command[c])
661                         continue;
662
663                 fprintf(f, "%s-> %s:\n",
664                         prefix, service_exec_command_to_string(c));
665
666                 exec_command_dump_list(s->exec_command[c], f, prefix2);
667         }
668
669         if (s->status_text)
670                 fprintf(f, "%sStatus Text: %s\n",
671                         prefix, s->status_text);
672
673         if (s->n_fd_store_max > 0) {
674                 fprintf(f,
675                         "%sFile Descriptor Store Max: %u\n"
676                         "%sFile Descriptor Store Current: %u\n",
677                         prefix, s->n_fd_store_max,
678                         prefix, s->n_fd_store);
679         }
680 }
681
682 static int service_load_pid_file(Service *s, bool may_warn) {
683         _cleanup_free_ char *k = NULL;
684         int r;
685         pid_t pid;
686
687         assert(s);
688
689         if (!s->pid_file)
690                 return -ENOENT;
691
692         r = read_one_line_file(s->pid_file, &k);
693         if (r < 0) {
694                 if (may_warn)
695                         log_unit_info(UNIT(s)->id, "PID file %s not readable (yet?) after %s.", s->pid_file, service_state_to_string(s->state));
696                 return r;
697         }
698
699         r = parse_pid(k, &pid);
700         if (r < 0) {
701                 if (may_warn)
702                         log_unit_info_errno(UNIT(s)->id, r, "Failed to read PID from file %s: %m", s->pid_file);
703                 return r;
704         }
705
706         if (!pid_is_alive(pid)) {
707                 if (may_warn)
708                         log_unit_info(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
709                 return -ESRCH;
710         }
711
712         if (s->main_pid_known) {
713                 if (pid == s->main_pid)
714                         return 0;
715
716                 log_unit_debug(UNIT(s)->id, "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid);
717
718                 service_unwatch_main_pid(s);
719                 s->main_pid_known = false;
720         } else
721                 log_unit_debug(UNIT(s)->id, "Main PID loaded: "PID_FMT, pid);
722
723         r = service_set_main_pid(s, pid);
724         if (r < 0)
725                 return r;
726
727         r = unit_watch_pid(UNIT(s), pid);
728         if (r < 0) {
729                 /* FIXME: we need to do something here */
730                 log_unit_warning(UNIT(s)->id, "Failed to watch PID "PID_FMT" from service %s", pid, UNIT(s)->id);
731                 return r;
732         }
733
734         return 0;
735 }
736
737 static int service_search_main_pid(Service *s) {
738         pid_t pid;
739         int r;
740
741         assert(s);
742
743         /* If we know it anyway, don't ever fallback to unreliable
744          * heuristics */
745         if (s->main_pid_known)
746                 return 0;
747
748         if (!s->guess_main_pid)
749                 return 0;
750
751         assert(s->main_pid <= 0);
752
753         pid = unit_search_main_pid(UNIT(s));
754         if (pid <= 0)
755                 return -ENOENT;
756
757         log_unit_debug(UNIT(s)->id, "Main PID guessed: "PID_FMT, pid);
758         r = service_set_main_pid(s, pid);
759         if (r < 0)
760                 return r;
761
762         r = unit_watch_pid(UNIT(s), pid);
763         if (r < 0) {
764                 /* FIXME: we need to do something here */
765                 log_unit_warning(UNIT(s)->id, "Failed to watch PID "PID_FMT" from service %s", pid, UNIT(s)->id);
766                 return r;
767         }
768
769         return 0;
770 }
771
772 static void service_set_state(Service *s, ServiceState state) {
773         ServiceState old_state;
774         const UnitActiveState *table;
775
776         assert(s);
777
778         table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
779
780         old_state = s->state;
781         s->state = state;
782
783         service_unwatch_pid_file(s);
784
785         if (!IN_SET(state,
786                     SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
787                     SERVICE_RELOAD,
788                     SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
789                     SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
790                     SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
791                     SERVICE_AUTO_RESTART))
792                 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
793
794         if (!IN_SET(state,
795                     SERVICE_START, SERVICE_START_POST,
796                     SERVICE_RUNNING, SERVICE_RELOAD,
797                     SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
798                     SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
799                     SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
800                 service_unwatch_main_pid(s);
801                 s->main_command = NULL;
802         }
803
804         if (!IN_SET(state,
805                     SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
806                     SERVICE_RELOAD,
807                     SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
808                     SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
809                     SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
810                 service_unwatch_control_pid(s);
811                 s->control_command = NULL;
812                 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
813         }
814
815         if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
816                 unit_unwatch_all_pids(UNIT(s));
817
818         if (!IN_SET(state,
819                     SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
820                     SERVICE_RUNNING, SERVICE_RELOAD,
821                     SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
822                     SERVICE_STOP_SIGABRT, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
823             !(state == SERVICE_DEAD && UNIT(s)->job)) {
824                 service_close_socket_fd(s);
825                 service_connection_unref(s);
826         }
827
828         if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
829                 service_stop_watchdog(s);
830
831         /* For the inactive states unit_notify() will trim the cgroup,
832          * but for exit we have to do that ourselves... */
833         if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
834                 unit_destroy_cgroup_if_empty(UNIT(s));
835
836         /* For remain_after_exit services, let's see if we can "release" the
837          * hold on the console, since unit_notify() only does that in case of
838          * change of state */
839         if (state == SERVICE_EXITED &&
840             s->remain_after_exit &&
841             UNIT(s)->manager->n_on_console > 0) {
842
843                 ExecContext *ec;
844
845                 ec = unit_get_exec_context(UNIT(s));
846                 if (ec && exec_context_may_touch_console(ec)) {
847                         Manager *m = UNIT(s)->manager;
848
849                         m->n_on_console --;
850                         if (m->n_on_console == 0)
851                                 /* unset no_console_output flag, since the console is free */
852                                 m->no_console_output = false;
853                 }
854         }
855
856         if (old_state != state)
857                 log_unit_debug(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
858
859         unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
860         s->reload_result = SERVICE_SUCCESS;
861 }
862
863 static int service_coldplug(Unit *u) {
864         Service *s = SERVICE(u);
865         int r;
866
867         assert(s);
868         assert(s->state == SERVICE_DEAD);
869
870         if (s->deserialized_state != s->state) {
871
872                 if (IN_SET(s->deserialized_state,
873                            SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
874                            SERVICE_RELOAD,
875                            SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
876                            SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
877                            SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
878
879                         usec_t k;
880
881                         k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
882
883                         /* For the start/stop timeouts 0 means off */
884                         if (k > 0) {
885                                 r = service_arm_timer(s, k);
886                                 if (r < 0)
887                                         return r;
888                         }
889                 }
890
891                 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
892
893                         /* The restart timeouts 0 means immediately */
894                         r = service_arm_timer(s, s->restart_usec);
895                         if (r < 0)
896                                 return r;
897                 }
898
899                 if (pid_is_unwaited(s->main_pid) &&
900                     ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
901                      IN_SET(s->deserialized_state,
902                             SERVICE_START, SERVICE_START_POST,
903                             SERVICE_RUNNING, SERVICE_RELOAD,
904                             SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
905                             SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
906                             SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
907                         r = unit_watch_pid(UNIT(s), s->main_pid);
908                         if (r < 0)
909                                 return r;
910                 }
911
912                 if (pid_is_unwaited(s->control_pid) &&
913                     IN_SET(s->deserialized_state,
914                            SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
915                            SERVICE_RELOAD,
916                            SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
917                            SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
918                            SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
919                         r = unit_watch_pid(UNIT(s), s->control_pid);
920                         if (r < 0)
921                                 return r;
922                 }
923
924                 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
925                         unit_watch_all_pids(UNIT(s));
926
927                 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
928                         service_start_watchdog(s);
929
930                 service_set_state(s, s->deserialized_state);
931         }
932
933         return 0;
934 }
935
936 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
937         _cleanup_free_ int *rfds = NULL;
938         unsigned rn_fds = 0;
939         Iterator i;
940         int r;
941         Unit *u;
942
943         assert(s);
944         assert(fds);
945         assert(n_fds);
946
947         if (s->socket_fd >= 0)
948                 return 0;
949
950         SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
951                 int *cfds;
952                 unsigned cn_fds;
953                 Socket *sock;
954
955                 if (u->type != UNIT_SOCKET)
956                         continue;
957
958                 sock = SOCKET(u);
959
960                 r = socket_collect_fds(sock, &cfds, &cn_fds);
961                 if (r < 0)
962                         return r;
963
964                 if (cn_fds <= 0) {
965                         free(cfds);
966                         continue;
967                 }
968
969                 if (!rfds) {
970                         rfds = cfds;
971                         rn_fds = cn_fds;
972                 } else {
973                         int *t;
974
975                         t = realloc(rfds, (rn_fds + cn_fds) * sizeof(int));
976                         if (!t) {
977                                 free(cfds);
978                                 return -ENOMEM;
979                         }
980
981                         memcpy(t + rn_fds, cfds, cn_fds * sizeof(int));
982                         rfds = t;
983                         rn_fds += cn_fds;
984
985                         free(cfds);
986
987                 }
988         }
989
990         if (s->n_fd_store > 0) {
991                 ServiceFDStore *fs;
992                 int *t;
993
994                 t = realloc(rfds, (rn_fds + s->n_fd_store) * sizeof(int));
995                 if (!t)
996                         return -ENOMEM;
997
998                 rfds = t;
999                 LIST_FOREACH(fd_store, fs, s->fd_store)
1000                         rfds[rn_fds++] = fs->fd;
1001         }
1002
1003         *fds = rfds;
1004         *n_fds = rn_fds;
1005
1006         rfds = NULL;
1007         return 0;
1008 }
1009
1010 static int service_spawn(
1011                 Service *s,
1012                 ExecCommand *c,
1013                 usec_t timeout,
1014                 bool pass_fds,
1015                 bool apply_permissions,
1016                 bool apply_chroot,
1017                 bool apply_tty_stdin,
1018                 bool is_control,
1019                 pid_t *_pid) {
1020
1021         pid_t pid;
1022         int r;
1023         int *fds = NULL;
1024         _cleanup_free_ int *fdsbuf = NULL;
1025         unsigned n_fds = 0, n_env = 0;
1026         _cleanup_free_ char *bus_endpoint_path = NULL;
1027         _cleanup_strv_free_ char
1028                 **argv = NULL, **final_env = NULL, **our_env = NULL;
1029         const char *path;
1030         ExecParameters exec_params = {
1031                 .apply_permissions = apply_permissions,
1032                 .apply_chroot      = apply_chroot,
1033                 .apply_tty_stdin   = apply_tty_stdin,
1034                 .bus_endpoint_fd   = -1,
1035                 .selinux_context_net = s->socket_fd_selinux_context_net
1036         };
1037
1038         assert(s);
1039         assert(c);
1040         assert(_pid);
1041
1042         unit_realize_cgroup(UNIT(s));
1043
1044         r = unit_setup_exec_runtime(UNIT(s));
1045         if (r < 0)
1046                 goto fail;
1047
1048         if (pass_fds ||
1049             s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1050             s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1051             s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1052
1053                 if (s->socket_fd >= 0) {
1054                         fds = &s->socket_fd;
1055                         n_fds = 1;
1056                 } else {
1057                         r = service_collect_fds(s, &fdsbuf, &n_fds);
1058                         if (r < 0)
1059                                 goto fail;
1060
1061                         fds = fdsbuf;
1062                 }
1063         }
1064
1065         if (timeout > 0) {
1066                 r = service_arm_timer(s, timeout);
1067                 if (r < 0)
1068                         goto fail;
1069         } else
1070                 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1071
1072         r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1073         if (r < 0)
1074                 goto fail;
1075
1076         our_env = new0(char*, 4);
1077         if (!our_env) {
1078                 r = -ENOMEM;
1079                 goto fail;
1080         }
1081
1082         if (is_control ? s->notify_access == NOTIFY_ALL : s->notify_access != NOTIFY_NONE)
1083                 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
1084                         r = -ENOMEM;
1085                         goto fail;
1086                 }
1087
1088         if (s->main_pid > 0)
1089                 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
1090                         r = -ENOMEM;
1091                         goto fail;
1092                 }
1093
1094         if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
1095                 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
1096                         r = -ENOMEM;
1097                         goto fail;
1098                 }
1099
1100         final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1101         if (!final_env) {
1102                 r = -ENOMEM;
1103                 goto fail;
1104         }
1105
1106         if (is_control && UNIT(s)->cgroup_path) {
1107                 path = strappenda(UNIT(s)->cgroup_path, "/control");
1108                 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1109         } else
1110                 path = UNIT(s)->cgroup_path;
1111
1112 #ifdef ENABLE_KDBUS
1113         if (s->exec_context.bus_endpoint) {
1114                 r = bus_kernel_create_endpoint(UNIT(s)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user",
1115                                                UNIT(s)->id, &bus_endpoint_path);
1116                 if (r < 0)
1117                         goto fail;
1118
1119                 /* Pass the fd to the exec_params so that the child process can upload the policy.
1120                  * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1121                  * as the service is running. */
1122                 exec_params.bus_endpoint_fd = s->bus_endpoint_fd = r;
1123         }
1124 #endif
1125
1126         exec_params.argv = argv;
1127         exec_params.fds = fds;
1128         exec_params.n_fds = n_fds;
1129         exec_params.environment = final_env;
1130         exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
1131         exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
1132         exec_params.cgroup_path = path;
1133         exec_params.cgroup_delegate = s->cgroup_context.delegate;
1134         exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
1135         exec_params.unit_id = UNIT(s)->id;
1136         exec_params.watchdog_usec = s->watchdog_usec;
1137         exec_params.bus_endpoint_path = bus_endpoint_path;
1138         if (s->type == SERVICE_IDLE)
1139                 exec_params.idle_pipe = UNIT(s)->manager->idle_pipe;
1140
1141         r = exec_spawn(c,
1142                        &s->exec_context,
1143                        &exec_params,
1144                        s->exec_runtime,
1145                        &pid);
1146         if (r < 0)
1147                 goto fail;
1148
1149         r = unit_watch_pid(UNIT(s), pid);
1150         if (r < 0)
1151                 /* FIXME: we need to do something here */
1152                 goto fail;
1153
1154         *_pid = pid;
1155
1156         return 0;
1157
1158 fail:
1159         if (timeout)
1160                 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1161
1162         return r;
1163 }
1164
1165 static int main_pid_good(Service *s) {
1166         assert(s);
1167
1168         /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1169          * don't know */
1170
1171         /* If we know the pid file, then lets just check if it is
1172          * still valid */
1173         if (s->main_pid_known) {
1174
1175                 /* If it's an alien child let's check if it is still
1176                  * alive ... */
1177                 if (s->main_pid_alien && s->main_pid > 0)
1178                         return pid_is_alive(s->main_pid);
1179
1180                 /* .. otherwise assume we'll get a SIGCHLD for it,
1181                  * which we really should wait for to collect exit
1182                  * status and code */
1183                 return s->main_pid > 0;
1184         }
1185
1186         /* We don't know the pid */
1187         return -EAGAIN;
1188 }
1189
1190 _pure_ static int control_pid_good(Service *s) {
1191         assert(s);
1192
1193         return s->control_pid > 0;
1194 }
1195
1196 static int cgroup_good(Service *s) {
1197         int r;
1198
1199         assert(s);
1200
1201         if (!UNIT(s)->cgroup_path)
1202                 return 0;
1203
1204         r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
1205         if (r < 0)
1206                 return r;
1207
1208         return !r;
1209 }
1210
1211 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1212         int r;
1213         assert(s);
1214
1215         if (f != SERVICE_SUCCESS)
1216                 s->result = f;
1217
1218         service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1219
1220         if (s->result != SERVICE_SUCCESS) {
1221                 log_unit_warning(UNIT(s)->id, "%s failed.", UNIT(s)->id);
1222                 failure_action(UNIT(s)->manager, s->failure_action, s->reboot_arg);
1223         }
1224
1225         if (allow_restart &&
1226             !s->forbid_restart &&
1227             (s->restart == SERVICE_RESTART_ALWAYS ||
1228              (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1229              (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1230              (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) ||
1231              (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
1232              (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) ||
1233              (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) ||
1234              (IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) && set_contains(s->restart_force_status.signal, INT_TO_PTR(s->main_exec_status.status)))) &&
1235             (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) &&
1236             (!IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) || !set_contains(s->restart_prevent_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
1237
1238                 r = service_arm_timer(s, s->restart_usec);
1239                 if (r < 0)
1240                         goto fail;
1241
1242                 service_set_state(s, SERVICE_AUTO_RESTART);
1243         }
1244
1245         s->forbid_restart = false;
1246
1247         /* We want fresh tmpdirs in case service is started again immediately */
1248         exec_runtime_destroy(s->exec_runtime);
1249         s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1250
1251         /* Also, remove the runtime directory in */
1252         exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1253
1254         /* Try to delete the pid file. At this point it will be
1255          * out-of-date, and some software might be confused by it, so
1256          * let's remove it. */
1257         if (s->pid_file)
1258                 unlink_noerrno(s->pid_file);
1259
1260         return;
1261
1262 fail:
1263         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run install restart timer: %m", UNIT(s)->id);
1264         service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1265 }
1266
1267 static void service_enter_stop_post(Service *s, ServiceResult f) {
1268         int r;
1269         assert(s);
1270
1271         if (f != SERVICE_SUCCESS)
1272                 s->result = f;
1273
1274         service_unwatch_control_pid(s);
1275         unit_watch_all_pids(UNIT(s));
1276
1277         s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1278         if (s->control_command) {
1279                 s->control_command_id = SERVICE_EXEC_STOP_POST;
1280
1281                 r = service_spawn(s,
1282                                   s->control_command,
1283                                   s->timeout_stop_usec,
1284                                   false,
1285                                   !s->permissions_start_only,
1286                                   !s->root_directory_start_only,
1287                                   true,
1288                                   true,
1289                                   &s->control_pid);
1290                 if (r < 0)
1291                         goto fail;
1292
1293                 service_set_state(s, SERVICE_STOP_POST);
1294         } else
1295                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1296
1297         return;
1298
1299 fail:
1300         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'stop-post' task: %m", UNIT(s)->id);
1301         service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1302 }
1303
1304 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1305         int r;
1306
1307         assert(s);
1308
1309         if (f != SERVICE_SUCCESS)
1310                 s->result = f;
1311
1312         unit_watch_all_pids(UNIT(s));
1313
1314         r = unit_kill_context(
1315                         UNIT(s),
1316                         &s->kill_context,
1317                         (state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM && state != SERVICE_STOP_SIGABRT) ?
1318                         KILL_KILL : (state == SERVICE_STOP_SIGABRT ? KILL_ABORT : KILL_TERMINATE),
1319                         s->main_pid,
1320                         s->control_pid,
1321                         s->main_pid_alien);
1322
1323         if (r < 0)
1324                 goto fail;
1325
1326         if (r > 0) {
1327                 if (s->timeout_stop_usec > 0) {
1328                         r = service_arm_timer(s, s->timeout_stop_usec);
1329                         if (r < 0)
1330                                 goto fail;
1331                 }
1332
1333                 service_set_state(s, state);
1334         } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGABRT)
1335                 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1336         else if (state == SERVICE_STOP_SIGKILL)
1337                 service_enter_stop_post(s, SERVICE_SUCCESS);
1338         else if (state == SERVICE_FINAL_SIGTERM)
1339                 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1340         else
1341                 service_enter_dead(s, SERVICE_SUCCESS, true);
1342
1343         return;
1344
1345 fail:
1346         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to kill processes: %m", UNIT(s)->id);
1347
1348         if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL ||
1349             state == SERVICE_STOP_SIGABRT)
1350                 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1351         else
1352                 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1353 }
1354
1355 static void service_enter_stop_by_notify(Service *s) {
1356         assert(s);
1357
1358         unit_watch_all_pids(UNIT(s));
1359
1360         if (s->timeout_stop_usec > 0)
1361                 service_arm_timer(s, s->timeout_stop_usec);
1362
1363         /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1364         service_set_state(s, SERVICE_STOP_SIGTERM);
1365 }
1366
1367 static void service_enter_stop(Service *s, ServiceResult f) {
1368         int r;
1369
1370         assert(s);
1371
1372         if (f != SERVICE_SUCCESS)
1373                 s->result = f;
1374
1375         service_unwatch_control_pid(s);
1376         unit_watch_all_pids(UNIT(s));
1377
1378         s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1379         if (s->control_command) {
1380                 s->control_command_id = SERVICE_EXEC_STOP;
1381
1382                 r = service_spawn(s,
1383                                   s->control_command,
1384                                   s->timeout_stop_usec,
1385                                   false,
1386                                   !s->permissions_start_only,
1387                                   !s->root_directory_start_only,
1388                                   false,
1389                                   true,
1390                                   &s->control_pid);
1391                 if (r < 0)
1392                         goto fail;
1393
1394                 service_set_state(s, SERVICE_STOP);
1395         } else
1396                 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1397
1398         return;
1399
1400 fail:
1401         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'stop' task: %m", UNIT(s)->id);
1402         service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1403 }
1404
1405 static void service_enter_running(Service *s, ServiceResult f) {
1406         int main_pid_ok, cgroup_ok;
1407         assert(s);
1408
1409         if (f != SERVICE_SUCCESS)
1410                 s->result = f;
1411
1412         main_pid_ok = main_pid_good(s);
1413         cgroup_ok = cgroup_good(s);
1414
1415         if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1416             (s->bus_name_good || s->type != SERVICE_DBUS)) {
1417
1418                 /* If there are any queued up sd_notify()
1419                  * notifications, process them now */
1420                 if (s->notify_state == NOTIFY_RELOADING)
1421                         service_enter_reload_by_notify(s);
1422                 else if (s->notify_state == NOTIFY_STOPPING)
1423                         service_enter_stop_by_notify(s);
1424                 else
1425                         service_set_state(s, SERVICE_RUNNING);
1426
1427         } else if (s->remain_after_exit)
1428                 service_set_state(s, SERVICE_EXITED);
1429         else
1430                 service_enter_stop(s, SERVICE_SUCCESS);
1431 }
1432
1433 static void service_enter_start_post(Service *s) {
1434         int r;
1435         assert(s);
1436
1437         service_unwatch_control_pid(s);
1438         service_reset_watchdog(s);
1439
1440         s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1441         if (s->control_command) {
1442                 s->control_command_id = SERVICE_EXEC_START_POST;
1443
1444                 r = service_spawn(s,
1445                                   s->control_command,
1446                                   s->timeout_start_usec,
1447                                   false,
1448                                   !s->permissions_start_only,
1449                                   !s->root_directory_start_only,
1450                                   false,
1451                                   true,
1452                                   &s->control_pid);
1453                 if (r < 0)
1454                         goto fail;
1455
1456                 service_set_state(s, SERVICE_START_POST);
1457         } else
1458                 service_enter_running(s, SERVICE_SUCCESS);
1459
1460         return;
1461
1462 fail:
1463         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start-post' task: %m", UNIT(s)->id);
1464         service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1465 }
1466
1467 static void service_kill_control_processes(Service *s) {
1468         char *p;
1469
1470         if (!UNIT(s)->cgroup_path)
1471                 return;
1472
1473         p = strappenda(UNIT(s)->cgroup_path, "/control");
1474         cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1475 }
1476
1477 static void service_enter_start(Service *s) {
1478         ExecCommand *c;
1479         pid_t pid;
1480         int r;
1481
1482         assert(s);
1483
1484         service_unwatch_control_pid(s);
1485         service_unwatch_main_pid(s);
1486
1487         /* We want to ensure that nobody leaks processes from
1488          * START_PRE here, so let's go on a killing spree, People
1489          * should not spawn long running processes from START_PRE. */
1490         service_kill_control_processes(s);
1491
1492         if (s->type == SERVICE_FORKING) {
1493                 s->control_command_id = SERVICE_EXEC_START;
1494                 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1495
1496                 s->main_command = NULL;
1497         } else {
1498                 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1499                 s->control_command = NULL;
1500
1501                 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1502         }
1503
1504         if (!c) {
1505                 assert(s->type == SERVICE_ONESHOT);
1506                 service_enter_start_post(s);
1507                 return;
1508         }
1509
1510         r = service_spawn(s,
1511                           c,
1512                           IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_ONESHOT) ? s->timeout_start_usec : 0,
1513                           true,
1514                           true,
1515                           true,
1516                           true,
1517                           false,
1518                           &pid);
1519         if (r < 0)
1520                 goto fail;
1521
1522         if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
1523                 /* For simple services we immediately start
1524                  * the START_POST binaries. */
1525
1526                 service_set_main_pid(s, pid);
1527                 service_enter_start_post(s);
1528
1529         } else  if (s->type == SERVICE_FORKING) {
1530
1531                 /* For forking services we wait until the start
1532                  * process exited. */
1533
1534                 s->control_pid = pid;
1535                 service_set_state(s, SERVICE_START);
1536
1537         } else if (s->type == SERVICE_ONESHOT ||
1538                    s->type == SERVICE_DBUS ||
1539                    s->type == SERVICE_NOTIFY) {
1540
1541                 /* For oneshot services we wait until the start
1542                  * process exited, too, but it is our main process. */
1543
1544                 /* For D-Bus services we know the main pid right away,
1545                  * but wait for the bus name to appear on the
1546                  * bus. Notify services are similar. */
1547
1548                 service_set_main_pid(s, pid);
1549                 service_set_state(s, SERVICE_START);
1550         } else
1551                 assert_not_reached("Unknown service type");
1552
1553         return;
1554
1555 fail:
1556         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start' task: %m", UNIT(s)->id);
1557         service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1558 }
1559
1560 static void service_enter_start_pre(Service *s) {
1561         int r;
1562
1563         assert(s);
1564
1565         service_unwatch_control_pid(s);
1566
1567         s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1568         if (s->control_command) {
1569                 /* Before we start anything, let's clear up what might
1570                  * be left from previous runs. */
1571                 service_kill_control_processes(s);
1572
1573                 s->control_command_id = SERVICE_EXEC_START_PRE;
1574
1575                 r = service_spawn(s,
1576                                   s->control_command,
1577                                   s->timeout_start_usec,
1578                                   false,
1579                                   !s->permissions_start_only,
1580                                   !s->root_directory_start_only,
1581                                   true,
1582                                   true,
1583                                   &s->control_pid);
1584                 if (r < 0)
1585                         goto fail;
1586
1587                 service_set_state(s, SERVICE_START_PRE);
1588         } else
1589                 service_enter_start(s);
1590
1591         return;
1592
1593 fail:
1594         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start-pre' task: %m", UNIT(s)->id);
1595         service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1596 }
1597
1598 static void service_enter_restart(Service *s) {
1599         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1600         int r;
1601
1602         assert(s);
1603
1604         if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1605                 /* Don't restart things if we are going down anyway */
1606                 log_unit_info(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart.");
1607
1608                 r = service_arm_timer(s, s->restart_usec);
1609                 if (r < 0)
1610                         goto fail;
1611
1612                 return;
1613         }
1614
1615         /* Any units that are bound to this service must also be
1616          * restarted. We use JOB_RESTART (instead of the more obvious
1617          * JOB_START) here so that those dependency jobs will be added
1618          * as well. */
1619         r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
1620         if (r < 0)
1621                 goto fail;
1622
1623         /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1624          * it will be canceled as part of the service_stop() call that
1625          * is executed as part of JOB_RESTART. */
1626
1627         log_unit_debug(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id);
1628         return;
1629
1630 fail:
1631         log_unit_warning(UNIT(s)->id, "%s failed to schedule restart job: %s", UNIT(s)->id, bus_error_message(&error, -r));
1632         service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1633 }
1634
1635 static void service_enter_reload_by_notify(Service *s) {
1636         assert(s);
1637
1638         if (s->timeout_start_usec > 0)
1639                 service_arm_timer(s, s->timeout_start_usec);
1640
1641         service_set_state(s, SERVICE_RELOAD);
1642 }
1643
1644 static void service_enter_reload(Service *s) {
1645         int r;
1646
1647         assert(s);
1648
1649         service_unwatch_control_pid(s);
1650
1651         s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1652         if (s->control_command) {
1653                 s->control_command_id = SERVICE_EXEC_RELOAD;
1654
1655                 r = service_spawn(s,
1656                                   s->control_command,
1657                                   s->timeout_start_usec,
1658                                   false,
1659                                   !s->permissions_start_only,
1660                                   !s->root_directory_start_only,
1661                                   false,
1662                                   true,
1663                                   &s->control_pid);
1664                 if (r < 0)
1665                         goto fail;
1666
1667                 service_set_state(s, SERVICE_RELOAD);
1668         } else
1669                 service_enter_running(s, SERVICE_SUCCESS);
1670
1671         return;
1672
1673 fail:
1674         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'reload' task: %m", UNIT(s)->id);
1675         s->reload_result = SERVICE_FAILURE_RESOURCES;
1676         service_enter_running(s, SERVICE_SUCCESS);
1677 }
1678
1679 static void service_run_next_control(Service *s) {
1680         int r;
1681
1682         assert(s);
1683         assert(s->control_command);
1684         assert(s->control_command->command_next);
1685
1686         assert(s->control_command_id != SERVICE_EXEC_START);
1687
1688         s->control_command = s->control_command->command_next;
1689         service_unwatch_control_pid(s);
1690
1691         r = service_spawn(s,
1692                           s->control_command,
1693                           IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec,
1694                           false,
1695                           !s->permissions_start_only,
1696                           !s->root_directory_start_only,
1697                           s->control_command_id == SERVICE_EXEC_START_PRE ||
1698                           s->control_command_id == SERVICE_EXEC_STOP_POST,
1699                           true,
1700                           &s->control_pid);
1701         if (r < 0)
1702                 goto fail;
1703
1704         return;
1705
1706 fail:
1707         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run next control task: %m", UNIT(s)->id);
1708
1709         if (s->state == SERVICE_START_PRE)
1710                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1711         else if (s->state == SERVICE_STOP)
1712                 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1713         else if (s->state == SERVICE_STOP_POST)
1714                 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1715         else if (s->state == SERVICE_RELOAD) {
1716                 s->reload_result = SERVICE_FAILURE_RESOURCES;
1717                 service_enter_running(s, SERVICE_SUCCESS);
1718         } else
1719                 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1720 }
1721
1722 static void service_run_next_main(Service *s) {
1723         pid_t pid;
1724         int r;
1725
1726         assert(s);
1727         assert(s->main_command);
1728         assert(s->main_command->command_next);
1729         assert(s->type == SERVICE_ONESHOT);
1730
1731         s->main_command = s->main_command->command_next;
1732         service_unwatch_main_pid(s);
1733
1734         r = service_spawn(s,
1735                           s->main_command,
1736                           s->timeout_start_usec,
1737                           true,
1738                           true,
1739                           true,
1740                           true,
1741                           false,
1742                           &pid);
1743         if (r < 0)
1744                 goto fail;
1745
1746         service_set_main_pid(s, pid);
1747
1748         return;
1749
1750 fail:
1751         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run next main task: %m", UNIT(s)->id);
1752         service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1753 }
1754
1755 static int service_start_limit_test(Service *s) {
1756         assert(s);
1757
1758         if (ratelimit_test(&s->start_limit))
1759                 return 0;
1760
1761         log_unit_warning(UNIT(s)->id, "start request repeated too quickly for %s", UNIT(s)->id);
1762
1763         return failure_action(UNIT(s)->manager, s->start_limit_action, s->reboot_arg);
1764 }
1765
1766 static int service_start(Unit *u) {
1767         Service *s = SERVICE(u);
1768         int r;
1769
1770         assert(s);
1771
1772         /* We cannot fulfill this request right now, try again later
1773          * please! */
1774         if (s->state == SERVICE_STOP ||
1775             s->state == SERVICE_STOP_SIGABRT ||
1776             s->state == SERVICE_STOP_SIGTERM ||
1777             s->state == SERVICE_STOP_SIGKILL ||
1778             s->state == SERVICE_STOP_POST ||
1779             s->state == SERVICE_FINAL_SIGTERM ||
1780             s->state == SERVICE_FINAL_SIGKILL)
1781                 return -EAGAIN;
1782
1783         /* Already on it! */
1784         if (s->state == SERVICE_START_PRE ||
1785             s->state == SERVICE_START ||
1786             s->state == SERVICE_START_POST)
1787                 return 0;
1788
1789         /* A service that will be restarted must be stopped first to
1790          * trigger BindsTo and/or OnFailure dependencies. If a user
1791          * does not want to wait for the holdoff time to elapse, the
1792          * service should be manually restarted, not started. We
1793          * simply return EAGAIN here, so that any start jobs stay
1794          * queued, and assume that the auto restart timer will
1795          * eventually trigger the restart. */
1796         if (s->state == SERVICE_AUTO_RESTART)
1797                 return -EAGAIN;
1798
1799         assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
1800
1801         /* Make sure we don't enter a busy loop of some kind. */
1802         r = service_start_limit_test(s);
1803         if (r < 0) {
1804                 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
1805                 return r;
1806         }
1807
1808         s->result = SERVICE_SUCCESS;
1809         s->reload_result = SERVICE_SUCCESS;
1810         s->main_pid_known = false;
1811         s->main_pid_alien = false;
1812         s->forbid_restart = false;
1813
1814         free(s->status_text);
1815         s->status_text = NULL;
1816         s->status_errno = 0;
1817
1818         s->notify_state = NOTIFY_UNKNOWN;
1819
1820         service_enter_start_pre(s);
1821         return 0;
1822 }
1823
1824 static int service_stop(Unit *u) {
1825         Service *s = SERVICE(u);
1826
1827         assert(s);
1828
1829         /* Don't create restart jobs from here. */
1830         s->forbid_restart = true;
1831
1832         /* Already on it */
1833         if (s->state == SERVICE_STOP ||
1834             s->state == SERVICE_STOP_SIGABRT ||
1835             s->state == SERVICE_STOP_SIGTERM ||
1836             s->state == SERVICE_STOP_SIGKILL ||
1837             s->state == SERVICE_STOP_POST ||
1838             s->state == SERVICE_FINAL_SIGTERM ||
1839             s->state == SERVICE_FINAL_SIGKILL)
1840                 return 0;
1841
1842         /* A restart will be scheduled or is in progress. */
1843         if (s->state == SERVICE_AUTO_RESTART) {
1844                 service_set_state(s, SERVICE_DEAD);
1845                 return 0;
1846         }
1847
1848         /* If there's already something running we go directly into
1849          * kill mode. */
1850         if (s->state == SERVICE_START_PRE ||
1851             s->state == SERVICE_START ||
1852             s->state == SERVICE_START_POST ||
1853             s->state == SERVICE_RELOAD) {
1854                 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1855                 return 0;
1856         }
1857
1858         assert(s->state == SERVICE_RUNNING ||
1859                s->state == SERVICE_EXITED);
1860
1861         service_enter_stop(s, SERVICE_SUCCESS);
1862         return 0;
1863 }
1864
1865 static int service_reload(Unit *u) {
1866         Service *s = SERVICE(u);
1867
1868         assert(s);
1869
1870         assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1871
1872         service_enter_reload(s);
1873         return 0;
1874 }
1875
1876 _pure_ static bool service_can_reload(Unit *u) {
1877         Service *s = SERVICE(u);
1878
1879         assert(s);
1880
1881         return !!s->exec_command[SERVICE_EXEC_RELOAD];
1882 }
1883
1884 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1885         Service *s = SERVICE(u);
1886         ServiceFDStore *fs;
1887
1888         assert(u);
1889         assert(f);
1890         assert(fds);
1891
1892         unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1893         unit_serialize_item(u, f, "result", service_result_to_string(s->result));
1894         unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
1895
1896         if (s->control_pid > 0)
1897                 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
1898                                            s->control_pid);
1899
1900         if (s->main_pid_known && s->main_pid > 0)
1901                 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
1902
1903         unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1904
1905         if (s->status_text)
1906                 unit_serialize_item(u, f, "status-text", s->status_text);
1907
1908         /* FIXME: There's a minor uncleanliness here: if there are
1909          * multiple commands attached here, we will start from the
1910          * first one again */
1911         if (s->control_command_id >= 0)
1912                 unit_serialize_item(u, f, "control-command",
1913                                     service_exec_command_to_string(s->control_command_id));
1914
1915         if (s->socket_fd >= 0) {
1916                 int copy;
1917
1918                 copy = fdset_put_dup(fds, s->socket_fd);
1919                 if (copy < 0)
1920                         return copy;
1921
1922                 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1923         }
1924
1925         if (s->bus_endpoint_fd >= 0) {
1926                 int copy;
1927
1928                 copy = fdset_put_dup(fds, s->bus_endpoint_fd);
1929                 if (copy < 0)
1930                         return copy;
1931
1932                 unit_serialize_item_format(u, f, "endpoint-fd", "%i", copy);
1933         }
1934
1935         LIST_FOREACH(fd_store, fs, s->fd_store) {
1936                 int copy;
1937
1938                 copy = fdset_put_dup(fds, fs->fd);
1939                 if (copy < 0)
1940                         return copy;
1941
1942                 unit_serialize_item_format(u, f, "fd-store-fd", "%i", copy);
1943         }
1944
1945         if (s->main_exec_status.pid > 0) {
1946                 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
1947                                            s->main_exec_status.pid);
1948                 dual_timestamp_serialize(f, "main-exec-status-start",
1949                                          &s->main_exec_status.start_timestamp);
1950                 dual_timestamp_serialize(f, "main-exec-status-exit",
1951                                          &s->main_exec_status.exit_timestamp);
1952
1953                 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
1954                         unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
1955                                                    s->main_exec_status.code);
1956                         unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
1957                                                    s->main_exec_status.status);
1958                 }
1959         }
1960         if (dual_timestamp_is_set(&s->watchdog_timestamp))
1961                 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
1962
1963         if (s->forbid_restart)
1964                 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
1965
1966         return 0;
1967 }
1968
1969 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1970         Service *s = SERVICE(u);
1971         int r;
1972
1973         assert(u);
1974         assert(key);
1975         assert(value);
1976         assert(fds);
1977
1978         if (streq(key, "state")) {
1979                 ServiceState state;
1980
1981                 state = service_state_from_string(value);
1982                 if (state < 0)
1983                         log_unit_debug(u->id, "Failed to parse state value %s", value);
1984                 else
1985                         s->deserialized_state = state;
1986         } else if (streq(key, "result")) {
1987                 ServiceResult f;
1988
1989                 f = service_result_from_string(value);
1990                 if (f < 0)
1991                         log_unit_debug(u->id, "Failed to parse result value %s", value);
1992                 else if (f != SERVICE_SUCCESS)
1993                         s->result = f;
1994
1995         } else if (streq(key, "reload-result")) {
1996                 ServiceResult f;
1997
1998                 f = service_result_from_string(value);
1999                 if (f < 0)
2000                         log_unit_debug(u->id, "Failed to parse reload result value %s", value);
2001                 else if (f != SERVICE_SUCCESS)
2002                         s->reload_result = f;
2003
2004         } else if (streq(key, "control-pid")) {
2005                 pid_t pid;
2006
2007                 if (parse_pid(value, &pid) < 0)
2008                         log_unit_debug(u->id, "Failed to parse control-pid value %s", value);
2009                 else
2010                         s->control_pid = pid;
2011         } else if (streq(key, "main-pid")) {
2012                 pid_t pid;
2013
2014                 if (parse_pid(value, &pid) < 0)
2015                         log_unit_debug(u->id, "Failed to parse main-pid value %s", value);
2016                 else {
2017                         service_set_main_pid(s, pid);
2018                         unit_watch_pid(UNIT(s), pid);
2019                 }
2020         } else if (streq(key, "main-pid-known")) {
2021                 int b;
2022
2023                 b = parse_boolean(value);
2024                 if (b < 0)
2025                         log_unit_debug(u->id, "Failed to parse main-pid-known value %s", value);
2026                 else
2027                         s->main_pid_known = b;
2028         } else if (streq(key, "status-text")) {
2029                 char *t;
2030
2031                 t = strdup(value);
2032                 if (!t)
2033                         log_oom();
2034                 else {
2035                         free(s->status_text);
2036                         s->status_text = t;
2037                 }
2038
2039         } else if (streq(key, "control-command")) {
2040                 ServiceExecCommand id;
2041
2042                 id = service_exec_command_from_string(value);
2043                 if (id < 0)
2044                         log_unit_debug(u->id, "Failed to parse exec-command value %s", value);
2045                 else {
2046                         s->control_command_id = id;
2047                         s->control_command = s->exec_command[id];
2048                 }
2049         } else if (streq(key, "socket-fd")) {
2050                 int fd;
2051
2052                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2053                         log_unit_debug(u->id, "Failed to parse socket-fd value %s", value);
2054                 else {
2055                         asynchronous_close(s->socket_fd);
2056                         s->socket_fd = fdset_remove(fds, fd);
2057                 }
2058         } else if (streq(key, "endpoint-fd")) {
2059                 int fd;
2060
2061                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2062                         log_unit_debug(u->id, "Failed to parse endpoint-fd value %s", value);
2063                 else {
2064                         safe_close(s->bus_endpoint_fd);
2065                         s->bus_endpoint_fd = fdset_remove(fds, fd);
2066                 }
2067         } else if (streq(key, "fd-store-fd")) {
2068                 int fd;
2069
2070                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2071                         log_unit_debug(u->id, "Failed to parse fd-store-fd value %s", value);
2072                 else {
2073                         r = service_add_fd_store(s, fd);
2074                         if (r < 0)
2075                                 log_unit_error_errno(u->id, r, "Failed to add fd to store: %m");
2076                         else if (r > 0)
2077                                 fdset_remove(fds, fd);
2078                 }
2079
2080         } else if (streq(key, "main-exec-status-pid")) {
2081                 pid_t pid;
2082
2083                 if (parse_pid(value, &pid) < 0)
2084                         log_unit_debug(u->id, "Failed to parse main-exec-status-pid value %s", value);
2085                 else
2086                         s->main_exec_status.pid = pid;
2087         } else if (streq(key, "main-exec-status-code")) {
2088                 int i;
2089
2090                 if (safe_atoi(value, &i) < 0)
2091                         log_unit_debug(u->id, "Failed to parse main-exec-status-code value %s", value);
2092                 else
2093                         s->main_exec_status.code = i;
2094         } else if (streq(key, "main-exec-status-status")) {
2095                 int i;
2096
2097                 if (safe_atoi(value, &i) < 0)
2098                         log_unit_debug(u->id, "Failed to parse main-exec-status-status value %s", value);
2099                 else
2100                         s->main_exec_status.status = i;
2101         } else if (streq(key, "main-exec-status-start"))
2102                 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2103         else if (streq(key, "main-exec-status-exit"))
2104                 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2105         else if (streq(key, "watchdog-timestamp"))
2106                 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2107         else if (streq(key, "forbid-restart")) {
2108                 int b;
2109
2110                 b = parse_boolean(value);
2111                 if (b < 0)
2112                         log_unit_debug(u->id, "Failed to parse forbid-restart value %s", value);
2113                 else
2114                         s->forbid_restart = b;
2115         } else
2116                 log_unit_debug(u->id, "Unknown serialization key '%s'", key);
2117
2118         return 0;
2119 }
2120
2121 _pure_ static UnitActiveState service_active_state(Unit *u) {
2122         const UnitActiveState *table;
2123
2124         assert(u);
2125
2126         table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2127
2128         return table[SERVICE(u)->state];
2129 }
2130
2131 static const char *service_sub_state_to_string(Unit *u) {
2132         assert(u);
2133
2134         return service_state_to_string(SERVICE(u)->state);
2135 }
2136
2137 static bool service_check_gc(Unit *u) {
2138         Service *s = SERVICE(u);
2139
2140         assert(s);
2141
2142         /* Never clean up services that still have a process around,
2143          * even if the service is formally dead. */
2144         if (cgroup_good(s) > 0 ||
2145             main_pid_good(s) > 0 ||
2146             control_pid_good(s) > 0)
2147                 return true;
2148
2149         return false;
2150 }
2151
2152 _pure_ static bool service_check_snapshot(Unit *u) {
2153         Service *s = SERVICE(u);
2154
2155         assert(s);
2156
2157         return s->socket_fd < 0;
2158 }
2159
2160 static int service_retry_pid_file(Service *s) {
2161         int r;
2162
2163         assert(s->pid_file);
2164         assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2165
2166         r = service_load_pid_file(s, false);
2167         if (r < 0)
2168                 return r;
2169
2170         service_unwatch_pid_file(s);
2171
2172         service_enter_running(s, SERVICE_SUCCESS);
2173         return 0;
2174 }
2175
2176 static int service_watch_pid_file(Service *s) {
2177         int r;
2178
2179         log_unit_debug(UNIT(s)->id, "Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
2180
2181         r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2182         if (r < 0)
2183                 goto fail;
2184
2185         /* the pidfile might have appeared just before we set the watch */
2186         log_unit_debug(UNIT(s)->id, "Trying to read %s's PID file %s in case it changed", UNIT(s)->id, s->pid_file_pathspec->path);
2187         service_retry_pid_file(s);
2188
2189         return 0;
2190 fail:
2191         log_unit_error_errno(UNIT(s)->id, r, "Failed to set a watch for %s's PID file %s: %m", UNIT(s)->id, s->pid_file_pathspec->path);
2192         service_unwatch_pid_file(s);
2193         return r;
2194 }
2195
2196 static int service_demand_pid_file(Service *s) {
2197         PathSpec *ps;
2198
2199         assert(s->pid_file);
2200         assert(!s->pid_file_pathspec);
2201
2202         ps = new0(PathSpec, 1);
2203         if (!ps)
2204                 return -ENOMEM;
2205
2206         ps->unit = UNIT(s);
2207         ps->path = strdup(s->pid_file);
2208         if (!ps->path) {
2209                 free(ps);
2210                 return -ENOMEM;
2211         }
2212
2213         path_kill_slashes(ps->path);
2214
2215         /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2216          * keep their PID file open all the time. */
2217         ps->type = PATH_MODIFIED;
2218         ps->inotify_fd = -1;
2219
2220         s->pid_file_pathspec = ps;
2221
2222         return service_watch_pid_file(s);
2223 }
2224
2225 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2226         PathSpec *p = userdata;
2227         Service *s;
2228
2229         assert(p);
2230
2231         s = SERVICE(p->unit);
2232
2233         assert(s);
2234         assert(fd >= 0);
2235         assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2236         assert(s->pid_file_pathspec);
2237         assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2238
2239         log_unit_debug(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
2240
2241         if (path_spec_fd_event(p, events) < 0)
2242                 goto fail;
2243
2244         if (service_retry_pid_file(s) == 0)
2245                 return 0;
2246
2247         if (service_watch_pid_file(s) < 0)
2248                 goto fail;
2249
2250         return 0;
2251
2252 fail:
2253         service_unwatch_pid_file(s);
2254         service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2255         return 0;
2256 }
2257
2258 static void service_notify_cgroup_empty_event(Unit *u) {
2259         Service *s = SERVICE(u);
2260
2261         assert(u);
2262
2263         log_unit_debug(u->id, "%s: cgroup is empty", u->id);
2264
2265         switch (s->state) {
2266
2267                 /* Waiting for SIGCHLD is usually more interesting,
2268                  * because it includes return codes/signals. Which is
2269                  * why we ignore the cgroup events for most cases,
2270                  * except when we don't know pid which to expect the
2271                  * SIGCHLD for. */
2272
2273         case SERVICE_START:
2274         case SERVICE_START_POST:
2275                 /* If we were hoping for the daemon to write its PID file,
2276                  * we can give up now. */
2277                 if (s->pid_file_pathspec) {
2278                         log_unit_warning(u->id, "%s never wrote its PID file. Failing.", UNIT(s)->id);
2279
2280                         service_unwatch_pid_file(s);
2281                         if (s->state == SERVICE_START)
2282                                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2283                         else
2284                                 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2285                 }
2286                 break;
2287
2288         case SERVICE_RUNNING:
2289                 /* service_enter_running() will figure out what to do */
2290                 service_enter_running(s, SERVICE_SUCCESS);
2291                 break;
2292
2293         case SERVICE_STOP_SIGABRT:
2294         case SERVICE_STOP_SIGTERM:
2295         case SERVICE_STOP_SIGKILL:
2296
2297                 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2298                         service_enter_stop_post(s, SERVICE_SUCCESS);
2299
2300                 break;
2301
2302         case SERVICE_STOP_POST:
2303         case SERVICE_FINAL_SIGTERM:
2304         case SERVICE_FINAL_SIGKILL:
2305                 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2306                         service_enter_dead(s, SERVICE_SUCCESS, true);
2307
2308                 break;
2309
2310         default:
2311                 ;
2312         }
2313 }
2314
2315 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2316         Service *s = SERVICE(u);
2317         ServiceResult f;
2318
2319         assert(s);
2320         assert(pid >= 0);
2321
2322         if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2323                                      is_clean_exit_lsb(code, status, &s->success_status))
2324                 f = SERVICE_SUCCESS;
2325         else if (code == CLD_EXITED)
2326                 f = SERVICE_FAILURE_EXIT_CODE;
2327         else if (code == CLD_KILLED)
2328                 f = SERVICE_FAILURE_SIGNAL;
2329         else if (code == CLD_DUMPED)
2330                 f = SERVICE_FAILURE_CORE_DUMP;
2331         else
2332                 assert_not_reached("Unknown code");
2333
2334         if (s->main_pid == pid) {
2335                 /* Forking services may occasionally move to a new PID.
2336                  * As long as they update the PID file before exiting the old
2337                  * PID, they're fine. */
2338                 if (service_load_pid_file(s, false) == 0)
2339                         return;
2340
2341                 s->main_pid = 0;
2342                 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2343
2344                 if (s->main_command) {
2345                         /* If this is not a forking service than the
2346                          * main process got started and hence we copy
2347                          * the exit status so that it is recorded both
2348                          * as main and as control process exit
2349                          * status */
2350
2351                         s->main_command->exec_status = s->main_exec_status;
2352
2353                         if (s->main_command->ignore)
2354                                 f = SERVICE_SUCCESS;
2355                 } else if (s->exec_command[SERVICE_EXEC_START]) {
2356
2357                         /* If this is a forked process, then we should
2358                          * ignore the return value if this was
2359                          * configured for the starter process */
2360
2361                         if (s->exec_command[SERVICE_EXEC_START]->ignore)
2362                                 f = SERVICE_SUCCESS;
2363                 }
2364
2365                 log_unit_struct(u->id,
2366                                 f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2367                                 LOG_MESSAGE("%s: main process exited, code=%s, status=%i/%s",
2368                                             u->id, sigchld_code_to_string(code), status,
2369                                             strna(code == CLD_EXITED
2370                                                   ? exit_status_to_string(status, EXIT_STATUS_FULL)
2371                                                   : signal_to_string(status))),
2372                                 "EXIT_CODE=%s", sigchld_code_to_string(code),
2373                                 "EXIT_STATUS=%i", status,
2374                                 NULL);
2375
2376                 if (f != SERVICE_SUCCESS)
2377                         s->result = f;
2378
2379                 if (s->main_command &&
2380                     s->main_command->command_next &&
2381                     f == SERVICE_SUCCESS) {
2382
2383                         /* There is another command to *
2384                          * execute, so let's do that. */
2385
2386                         log_unit_debug(u->id, "%s running next main command for state %s", u->id, service_state_to_string(s->state));
2387                         service_run_next_main(s);
2388
2389                 } else {
2390
2391                         /* The service exited, so the service is officially
2392                          * gone. */
2393                         s->main_command = NULL;
2394
2395                         switch (s->state) {
2396
2397                         case SERVICE_START_POST:
2398                         case SERVICE_RELOAD:
2399                         case SERVICE_STOP:
2400                                 /* Need to wait until the operation is
2401                                  * done */
2402                                 break;
2403
2404                         case SERVICE_START:
2405                                 if (s->type == SERVICE_ONESHOT) {
2406                                         /* This was our main goal, so let's go on */
2407                                         if (f == SERVICE_SUCCESS)
2408                                                 service_enter_start_post(s);
2409                                         else
2410                                                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2411                                         break;
2412                                 }
2413
2414                                 /* Fall through */
2415
2416                         case SERVICE_RUNNING:
2417                                 service_enter_running(s, f);
2418                                 break;
2419
2420                         case SERVICE_STOP_SIGABRT:
2421                         case SERVICE_STOP_SIGTERM:
2422                         case SERVICE_STOP_SIGKILL:
2423
2424                                 if (!control_pid_good(s))
2425                                         service_enter_stop_post(s, f);
2426
2427                                 /* If there is still a control process, wait for that first */
2428                                 break;
2429
2430                         case SERVICE_STOP_POST:
2431                         case SERVICE_FINAL_SIGTERM:
2432                         case SERVICE_FINAL_SIGKILL:
2433
2434                                 if (!control_pid_good(s))
2435                                         service_enter_dead(s, f, true);
2436                                 break;
2437
2438                         default:
2439                                 assert_not_reached("Uh, main process died at wrong time.");
2440                         }
2441                 }
2442
2443         } else if (s->control_pid == pid) {
2444                 s->control_pid = 0;
2445
2446                 if (s->control_command) {
2447                         exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2448
2449                         if (s->control_command->ignore)
2450                                 f = SERVICE_SUCCESS;
2451                 }
2452
2453                 log_unit_full(u->id,
2454                               f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2455                               "%s: control process exited, code=%s status=%i",
2456                               u->id, sigchld_code_to_string(code), status);
2457
2458                 if (f != SERVICE_SUCCESS)
2459                         s->result = f;
2460
2461                 /* Immediately get rid of the cgroup, so that the
2462                  * kernel doesn't delay the cgroup empty messages for
2463                  * the service cgroup any longer than necessary */
2464                 service_kill_control_processes(s);
2465
2466                 if (s->control_command &&
2467                     s->control_command->command_next &&
2468                     f == SERVICE_SUCCESS) {
2469
2470                         /* There is another command to *
2471                          * execute, so let's do that. */
2472
2473                         log_unit_debug(u->id, "%s running next control command for state %s", u->id, service_state_to_string(s->state));
2474                         service_run_next_control(s);
2475
2476                 } else {
2477                         /* No further commands for this step, so let's
2478                          * figure out what to do next */
2479
2480                         s->control_command = NULL;
2481                         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2482
2483                         log_unit_debug(u->id, "%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
2484
2485                         switch (s->state) {
2486
2487                         case SERVICE_START_PRE:
2488                                 if (f == SERVICE_SUCCESS)
2489                                         service_enter_start(s);
2490                                 else
2491                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2492                                 break;
2493
2494                         case SERVICE_START:
2495                                 if (s->type != SERVICE_FORKING)
2496                                         /* Maybe spurious event due to a reload that changed the type? */
2497                                         break;
2498
2499                                 if (f != SERVICE_SUCCESS) {
2500                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2501                                         break;
2502                                 }
2503
2504                                 if (s->pid_file) {
2505                                         bool has_start_post;
2506                                         int r;
2507
2508                                         /* Let's try to load the pid file here if we can.
2509                                          * The PID file might actually be created by a START_POST
2510                                          * script. In that case don't worry if the loading fails. */
2511
2512                                         has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2513                                         r = service_load_pid_file(s, !has_start_post);
2514                                         if (!has_start_post && r < 0) {
2515                                                 r = service_demand_pid_file(s);
2516                                                 if (r < 0 || !cgroup_good(s))
2517                                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2518                                                 break;
2519                                         }
2520                                 } else
2521                                         service_search_main_pid(s);
2522
2523                                 service_enter_start_post(s);
2524                                 break;
2525
2526                         case SERVICE_START_POST:
2527                                 if (f != SERVICE_SUCCESS) {
2528                                         service_enter_stop(s, f);
2529                                         break;
2530                                 }
2531
2532                                 if (s->pid_file) {
2533                                         int r;
2534
2535                                         r = service_load_pid_file(s, true);
2536                                         if (r < 0) {
2537                                                 r = service_demand_pid_file(s);
2538                                                 if (r < 0 || !cgroup_good(s))
2539                                                         service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2540                                                 break;
2541                                         }
2542                                 } else
2543                                         service_search_main_pid(s);
2544
2545                                 service_enter_running(s, SERVICE_SUCCESS);
2546                                 break;
2547
2548                         case SERVICE_RELOAD:
2549                                 if (f == SERVICE_SUCCESS) {
2550                                         service_load_pid_file(s, true);
2551                                         service_search_main_pid(s);
2552                                 }
2553
2554                                 s->reload_result = f;
2555                                 service_enter_running(s, SERVICE_SUCCESS);
2556                                 break;
2557
2558                         case SERVICE_STOP:
2559                                 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2560                                 break;
2561
2562                         case SERVICE_STOP_SIGABRT:
2563                         case SERVICE_STOP_SIGTERM:
2564                         case SERVICE_STOP_SIGKILL:
2565                                 if (main_pid_good(s) <= 0)
2566                                         service_enter_stop_post(s, f);
2567
2568                                 /* If there is still a service
2569                                  * process around, wait until
2570                                  * that one quit, too */
2571                                 break;
2572
2573                         case SERVICE_STOP_POST:
2574                         case SERVICE_FINAL_SIGTERM:
2575                         case SERVICE_FINAL_SIGKILL:
2576                                 if (main_pid_good(s) <= 0)
2577                                         service_enter_dead(s, f, true);
2578                                 break;
2579
2580                         default:
2581                                 assert_not_reached("Uh, control process died at wrong time.");
2582                         }
2583                 }
2584         }
2585
2586         /* Notify clients about changed exit status */
2587         unit_add_to_dbus_queue(u);
2588
2589         /* We got one SIGCHLD for the service, let's watch all
2590          * processes that are now running of the service, and watch
2591          * that. Among the PIDs we then watch will be children
2592          * reassigned to us, which hopefully allows us to identify
2593          * when all children are gone */
2594         unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2595         unit_watch_all_pids(u);
2596
2597         /* If the PID set is empty now, then let's finish this off */
2598         if (set_isempty(u->pids))
2599                 service_notify_cgroup_empty_event(u);
2600 }
2601
2602 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2603         Service *s = SERVICE(userdata);
2604
2605         assert(s);
2606         assert(source == s->timer_event_source);
2607
2608         switch (s->state) {
2609
2610         case SERVICE_START_PRE:
2611         case SERVICE_START:
2612                 log_unit_warning(UNIT(s)->id, "%s %s operation timed out. Terminating.", UNIT(s)->id, s->state == SERVICE_START ? "start" : "start-pre");
2613                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2614                 break;
2615
2616         case SERVICE_START_POST:
2617                 log_unit_warning(UNIT(s)->id, "%s start-post operation timed out. Stopping.", UNIT(s)->id);
2618                 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2619                 break;
2620
2621         case SERVICE_RELOAD:
2622                 log_unit_warning(UNIT(s)->id, "%s reload operation timed out. Stopping.", UNIT(s)->id);
2623                 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2624                 service_enter_running(s, SERVICE_SUCCESS);
2625                 break;
2626
2627         case SERVICE_STOP:
2628                 log_unit_warning(UNIT(s)->id, "%s stopping timed out. Terminating.", UNIT(s)->id);
2629                 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2630                 break;
2631
2632         case SERVICE_STOP_SIGABRT:
2633                 log_unit_warning(UNIT(s)->id,
2634                                  "%s stop-sigabrt timed out. Terminating.", UNIT(s)->id);
2635                 service_enter_signal(s, SERVICE_STOP_SIGTERM, s->result);
2636                 break;
2637
2638         case SERVICE_STOP_SIGTERM:
2639                 if (s->kill_context.send_sigkill) {
2640                         log_unit_warning(UNIT(s)->id, "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
2641                         service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2642                 } else {
2643                         log_unit_warning(UNIT(s)->id, "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
2644                         service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2645                 }
2646
2647                 break;
2648
2649         case SERVICE_STOP_SIGKILL:
2650                 /* Uh, we sent a SIGKILL and it is still not gone?
2651                  * Must be something we cannot kill, so let's just be
2652                  * weirded out and continue */
2653
2654                 log_unit_warning(UNIT(s)->id, "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
2655                 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2656                 break;
2657
2658         case SERVICE_STOP_POST:
2659                 log_unit_warning(UNIT(s)->id, "%s stop-post timed out. Terminating.", UNIT(s)->id);
2660                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2661                 break;
2662
2663         case SERVICE_FINAL_SIGTERM:
2664                 if (s->kill_context.send_sigkill) {
2665                         log_unit_warning(UNIT(s)->id, "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
2666                         service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2667                 } else {
2668                         log_unit_warning(UNIT(s)->id, "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", UNIT(s)->id);
2669                         service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
2670                 }
2671
2672                 break;
2673
2674         case SERVICE_FINAL_SIGKILL:
2675                 log_unit_warning(UNIT(s)->id, "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
2676                 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
2677                 break;
2678
2679         case SERVICE_AUTO_RESTART:
2680                 log_unit_info(UNIT(s)->id,
2681                               s->restart_usec > 0 ?
2682                               "%s holdoff time over, scheduling restart." :
2683                               "%s has no holdoff time, scheduling restart.",
2684                               UNIT(s)->id);
2685                 service_enter_restart(s);
2686                 break;
2687
2688         default:
2689                 assert_not_reached("Timeout at wrong time.");
2690         }
2691
2692         return 0;
2693 }
2694
2695 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2696         Service *s = SERVICE(userdata);
2697         char t[FORMAT_TIMESPAN_MAX];
2698
2699         assert(s);
2700         assert(source == s->watchdog_event_source);
2701
2702         log_unit_error(UNIT(s)->id, "%s watchdog timeout (limit %s)!", UNIT(s)->id,
2703                        format_timespan(t, sizeof(t), s->watchdog_usec, 1));
2704
2705         service_enter_signal(s, SERVICE_STOP_SIGABRT, SERVICE_FAILURE_WATCHDOG);
2706
2707         return 0;
2708 }
2709
2710 static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds) {
2711         Service *s = SERVICE(u);
2712         _cleanup_free_ char *cc = NULL;
2713         bool notify_dbus = false;
2714         const char *e;
2715
2716         assert(u);
2717
2718         cc = strv_join(tags, ", ");
2719         log_unit_debug(u->id, "%s: Got notification message from PID "PID_FMT" (%s)",
2720                        u->id, pid, isempty(cc) ? "n/a" : cc);
2721
2722         if (s->notify_access == NOTIFY_NONE) {
2723                 log_unit_warning(u->id, "%s: Got notification message from PID "PID_FMT", but reception is disabled.", u->id, pid);
2724                 return;
2725         }
2726
2727         if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2728                 if (s->main_pid != 0)
2729                         log_unit_warning(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, u->id, pid, s->main_pid);
2730                 else
2731                         log_unit_debug(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", u->id, pid);
2732                 return;
2733         }
2734
2735         /* Interpret MAINPID= */
2736         e = strv_find_startswith(tags, "MAINPID=");
2737         if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
2738                 if (parse_pid(e, &pid) < 0)
2739                         log_unit_warning(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
2740                 else {
2741                         log_unit_debug(u->id, "%s: got MAINPID=%s", u->id, e);
2742
2743                         service_set_main_pid(s, pid);
2744                         unit_watch_pid(UNIT(s), pid);
2745                         notify_dbus = true;
2746                 }
2747         }
2748
2749         /* Interpret RELOADING= */
2750         if (strv_find(tags, "RELOADING=1")) {
2751
2752                 log_unit_debug(u->id, "%s: got RELOADING=1", u->id);
2753                 s->notify_state = NOTIFY_RELOADING;
2754
2755                 if (s->state == SERVICE_RUNNING)
2756                         service_enter_reload_by_notify(s);
2757
2758                 notify_dbus = true;
2759         }
2760
2761         /* Interpret READY= */
2762         if (strv_find(tags, "READY=1")) {
2763
2764                 log_unit_debug(u->id, "%s: got READY=1", u->id);
2765                 s->notify_state = NOTIFY_READY;
2766
2767                 /* Type=notify services inform us about completed
2768                  * initialization with READY=1 */
2769                 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START)
2770                         service_enter_start_post(s);
2771
2772                 /* Sending READY=1 while we are reloading informs us
2773                  * that the reloading is complete */
2774                 if (s->state == SERVICE_RELOAD && s->control_pid == 0)
2775                         service_enter_running(s, SERVICE_SUCCESS);
2776
2777                 notify_dbus = true;
2778         }
2779
2780         /* Interpret STOPPING= */
2781         if (strv_find(tags, "STOPPING=1")) {
2782
2783                 log_unit_debug(u->id, "%s: got STOPPING=1", u->id);
2784                 s->notify_state = NOTIFY_STOPPING;
2785
2786                 if (s->state == SERVICE_RUNNING)
2787                         service_enter_stop_by_notify(s);
2788
2789                 notify_dbus = true;
2790         }
2791
2792         /* Interpret STATUS= */
2793         e = strv_find_startswith(tags, "STATUS=");
2794         if (e) {
2795                 _cleanup_free_ char *t = NULL;
2796
2797                 if (!isempty(e)) {
2798                         if (!utf8_is_valid(e))
2799                                 log_unit_warning(u->id, "Status message in notification is not UTF-8 clean.");
2800                         else {
2801                                 log_unit_debug(u->id, "%s: got STATUS=%s", u->id, e);
2802
2803                                 t = strdup(e);
2804                                 if (!t)
2805                                         log_oom();
2806                         }
2807                 }
2808
2809                 if (!streq_ptr(s->status_text, t)) {
2810
2811                         free(s->status_text);
2812                         s->status_text = t;
2813                         t = NULL;
2814
2815                         notify_dbus = true;
2816                 }
2817         }
2818
2819         /* Interpret ERRNO= */
2820         e = strv_find_startswith(tags, "ERRNO=");
2821         if (e) {
2822                 int status_errno;
2823
2824                 if (safe_atoi(e, &status_errno) < 0 || status_errno < 0)
2825                         log_unit_warning(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
2826                 else {
2827                         log_unit_debug(u->id, "%s: got ERRNO=%s", u->id, e);
2828
2829                         if (s->status_errno != status_errno) {
2830                                 s->status_errno = status_errno;
2831                                 notify_dbus = true;
2832                         }
2833                 }
2834         }
2835
2836         /* Interpret WATCHDOG= */
2837         if (strv_find(tags, "WATCHDOG=1")) {
2838                 log_unit_debug(u->id, "%s: got WATCHDOG=1", u->id);
2839                 service_reset_watchdog(s);
2840         }
2841
2842         /* Add the passed fds to the fd store */
2843         if (strv_find(tags, "FDSTORE=1")) {
2844                 log_unit_debug(u->id, "%s: got FDSTORE=1", u->id);
2845                 service_add_fd_store_set(s, fds);
2846         }
2847
2848         /* Notify clients about changed status or main pid */
2849         if (notify_dbus)
2850                 unit_add_to_dbus_queue(u);
2851 }
2852
2853 static int service_get_timeout(Unit *u, uint64_t *timeout) {
2854         Service *s = SERVICE(u);
2855         int r;
2856
2857         if (!s->timer_event_source)
2858                 return 0;
2859
2860         r = sd_event_source_get_time(s->timer_event_source, timeout);
2861         if (r < 0)
2862                 return r;
2863
2864         return 1;
2865 }
2866
2867 static void service_bus_name_owner_change(
2868                 Unit *u,
2869                 const char *name,
2870                 const char *old_owner,
2871                 const char *new_owner) {
2872
2873         Service *s = SERVICE(u);
2874         int r;
2875
2876         assert(s);
2877         assert(name);
2878
2879         assert(streq(s->bus_name, name));
2880         assert(old_owner || new_owner);
2881
2882         if (old_owner && new_owner)
2883                 log_unit_debug(u->id, "%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
2884         else if (old_owner)
2885                 log_unit_debug(u->id, "%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
2886         else
2887                 log_unit_debug(u->id, "%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
2888
2889         s->bus_name_good = !!new_owner;
2890
2891         if (s->type == SERVICE_DBUS) {
2892
2893                 /* service_enter_running() will figure out what to
2894                  * do */
2895                 if (s->state == SERVICE_RUNNING)
2896                         service_enter_running(s, SERVICE_SUCCESS);
2897                 else if (s->state == SERVICE_START && new_owner)
2898                         service_enter_start_post(s);
2899
2900         } else if (new_owner &&
2901                    s->main_pid <= 0 &&
2902                    (s->state == SERVICE_START ||
2903                     s->state == SERVICE_START_POST ||
2904                     s->state == SERVICE_RUNNING ||
2905                     s->state == SERVICE_RELOAD)) {
2906
2907                 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
2908                 pid_t pid;
2909
2910                 /* Try to acquire PID from bus service */
2911
2912                 r = sd_bus_get_name_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
2913                 if (r >= 0)
2914                         r = sd_bus_creds_get_pid(creds, &pid);
2915                 if (r >= 0) {
2916                         log_unit_debug(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
2917
2918                         service_set_main_pid(s, pid);
2919                         unit_watch_pid(UNIT(s), pid);
2920                 }
2921         }
2922 }
2923
2924 int service_set_socket_fd(Service *s, int fd, Socket *sock, bool selinux_context_net) {
2925         _cleanup_free_ char *peer = NULL;
2926         int r;
2927
2928         assert(s);
2929         assert(fd >= 0);
2930
2931         /* This is called by the socket code when instantiating a new
2932          * service for a stream socket and the socket needs to be
2933          * configured. */
2934
2935         if (UNIT(s)->load_state != UNIT_LOADED)
2936                 return -EINVAL;
2937
2938         if (s->socket_fd >= 0)
2939                 return -EBUSY;
2940
2941         if (s->state != SERVICE_DEAD)
2942                 return -EAGAIN;
2943
2944         if (getpeername_pretty(fd, &peer) >= 0) {
2945
2946                 if (UNIT(s)->description) {
2947                         _cleanup_free_ char *a;
2948
2949                         a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
2950                         if (!a)
2951                                 return -ENOMEM;
2952
2953                         r = unit_set_description(UNIT(s), a);
2954                 }  else
2955                         r = unit_set_description(UNIT(s), peer);
2956
2957                 if (r < 0)
2958                         return r;
2959         }
2960
2961         s->socket_fd = fd;
2962         s->socket_fd_selinux_context_net = selinux_context_net;
2963
2964         unit_ref_set(&s->accept_socket, UNIT(sock));
2965
2966         return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
2967 }
2968
2969 static void service_reset_failed(Unit *u) {
2970         Service *s = SERVICE(u);
2971
2972         assert(s);
2973
2974         if (s->state == SERVICE_FAILED)
2975                 service_set_state(s, SERVICE_DEAD);
2976
2977         s->result = SERVICE_SUCCESS;
2978         s->reload_result = SERVICE_SUCCESS;
2979
2980         RATELIMIT_RESET(s->start_limit);
2981 }
2982
2983 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
2984         Service *s = SERVICE(u);
2985
2986         return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
2987 }
2988
2989 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2990         [SERVICE_DEAD] = "dead",
2991         [SERVICE_START_PRE] = "start-pre",
2992         [SERVICE_START] = "start",
2993         [SERVICE_START_POST] = "start-post",
2994         [SERVICE_RUNNING] = "running",
2995         [SERVICE_EXITED] = "exited",
2996         [SERVICE_RELOAD] = "reload",
2997         [SERVICE_STOP] = "stop",
2998         [SERVICE_STOP_SIGABRT] = "stop-sigabrt",
2999         [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3000         [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3001         [SERVICE_STOP_POST] = "stop-post",
3002         [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3003         [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3004         [SERVICE_FAILED] = "failed",
3005         [SERVICE_AUTO_RESTART] = "auto-restart",
3006 };
3007
3008 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3009
3010 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3011         [SERVICE_RESTART_NO] = "no",
3012         [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3013         [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3014         [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
3015         [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3016         [SERVICE_RESTART_ON_ABORT] = "on-abort",
3017         [SERVICE_RESTART_ALWAYS] = "always",
3018 };
3019
3020 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3021
3022 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3023         [SERVICE_SIMPLE] = "simple",
3024         [SERVICE_FORKING] = "forking",
3025         [SERVICE_ONESHOT] = "oneshot",
3026         [SERVICE_DBUS] = "dbus",
3027         [SERVICE_NOTIFY] = "notify",
3028         [SERVICE_IDLE] = "idle"
3029 };
3030
3031 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3032
3033 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3034         [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3035         [SERVICE_EXEC_START] = "ExecStart",
3036         [SERVICE_EXEC_START_POST] = "ExecStartPost",
3037         [SERVICE_EXEC_RELOAD] = "ExecReload",
3038         [SERVICE_EXEC_STOP] = "ExecStop",
3039         [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3040 };
3041
3042 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3043
3044 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3045         [NOTIFY_NONE] = "none",
3046         [NOTIFY_MAIN] = "main",
3047         [NOTIFY_ALL] = "all"
3048 };
3049
3050 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3051
3052 static const char* const notify_state_table[_NOTIFY_STATE_MAX] = {
3053         [NOTIFY_UNKNOWN] = "unknown",
3054         [NOTIFY_READY] = "ready",
3055         [NOTIFY_RELOADING] = "reloading",
3056         [NOTIFY_STOPPING] = "stopping",
3057 };
3058
3059 DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState);
3060
3061 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3062         [SERVICE_SUCCESS] = "success",
3063         [SERVICE_FAILURE_RESOURCES] = "resources",
3064         [SERVICE_FAILURE_TIMEOUT] = "timeout",
3065         [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3066         [SERVICE_FAILURE_SIGNAL] = "signal",
3067         [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3068         [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3069         [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3070 };
3071
3072 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3073
3074 const UnitVTable service_vtable = {
3075         .object_size = sizeof(Service),
3076         .exec_context_offset = offsetof(Service, exec_context),
3077         .cgroup_context_offset = offsetof(Service, cgroup_context),
3078         .kill_context_offset = offsetof(Service, kill_context),
3079         .exec_runtime_offset = offsetof(Service, exec_runtime),
3080
3081         .sections =
3082                 "Unit\0"
3083                 "Service\0"
3084                 "Install\0",
3085         .private_section = "Service",
3086
3087         .init = service_init,
3088         .done = service_done,
3089         .load = service_load,
3090         .release_resources = service_release_resources,
3091
3092         .coldplug = service_coldplug,
3093
3094         .dump = service_dump,
3095
3096         .start = service_start,
3097         .stop = service_stop,
3098         .reload = service_reload,
3099
3100         .can_reload = service_can_reload,
3101
3102         .kill = service_kill,
3103
3104         .serialize = service_serialize,
3105         .deserialize_item = service_deserialize_item,
3106
3107         .active_state = service_active_state,
3108         .sub_state_to_string = service_sub_state_to_string,
3109
3110         .check_gc = service_check_gc,
3111         .check_snapshot = service_check_snapshot,
3112
3113         .sigchld_event = service_sigchld_event,
3114
3115         .reset_failed = service_reset_failed,
3116
3117         .notify_cgroup_empty = service_notify_cgroup_empty_event,
3118         .notify_message = service_notify_message,
3119
3120         .bus_name_owner_change = service_bus_name_owner_change,
3121
3122         .bus_interface = "org.freedesktop.systemd1.Service",
3123         .bus_vtable = bus_service_vtable,
3124         .bus_set_property = bus_service_set_property,
3125         .bus_commit_properties = bus_service_commit_properties,
3126
3127         .get_timeout = service_get_timeout,
3128         .can_transient = true,
3129
3130         .status_message_formats = {
3131                 .starting_stopping = {
3132                         [0] = "Starting %s...",
3133                         [1] = "Stopping %s...",
3134                 },
3135                 .finished_start_job = {
3136                         [JOB_DONE]       = "Started %s.",
3137                         [JOB_FAILED]     = "Failed to start %s.",
3138                         [JOB_DEPENDENCY] = "Dependency failed for %s.",
3139                         [JOB_TIMEOUT]    = "Timed out starting %s.",
3140                 },
3141                 .finished_stop_job = {
3142                         [JOB_DONE]       = "Stopped %s.",
3143                         [JOB_FAILED]     = "Stopped (with error) %s.",
3144                         [JOB_TIMEOUT]    = "Timed out stopping %s.",
3145                 },
3146         },
3147 };