X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fcore%2Fservice.c;h=8097e26b0034bac306813b92d4e26815ad8e7d6d;hp=3b3f9563808a43b78035e53568cdc7c07b852975;hb=79a98c609da042c60260e8e0236464014d480dcb;hpb=c4ef331782b4c0c615a258d3a4e6c71d8ccc67e1 diff --git a/src/core/service.c b/src/core/service.c index 3b3f95638..8097e26b0 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1254,8 +1254,7 @@ static int service_load(Unit *u) { s->notify_access = NOTIFY_MAIN; if (s->type == SERVICE_DBUS || s->bus_name) { - r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, - SPECIAL_DBUS_SOCKET, NULL, true); + r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true); if (r < 0) return r; } @@ -1489,7 +1488,8 @@ static void service_set_state(Service *s, ServiceState state) { if (!IN_SET(state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD, - SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, + SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, + SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, SERVICE_AUTO_RESTART)) s->timer_event_source = sd_event_source_unref(s->timer_event_source); @@ -1497,7 +1497,9 @@ static void service_set_state(Service *s, ServiceState state) { if (!IN_SET(state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD, - SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) { + SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, + SERVICE_STOP_POST, + SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { service_unwatch_main_pid(s); s->main_command = NULL; } @@ -1505,7 +1507,8 @@ static void service_set_state(Service *s, ServiceState state) { if (!IN_SET(state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD, - SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, + SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, + SERVICE_STOP_POST, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { service_unwatch_control_pid(s); s->control_command = NULL; @@ -1562,22 +1565,16 @@ static int service_coldplug(Unit *u) { if (s->deserialized_state != s->state) { - if (s->deserialized_state == SERVICE_START_PRE || - s->deserialized_state == SERVICE_START || - s->deserialized_state == SERVICE_START_POST || - s->deserialized_state == SERVICE_RELOAD || - s->deserialized_state == SERVICE_STOP || - s->deserialized_state == SERVICE_STOP_SIGTERM || - s->deserialized_state == SERVICE_STOP_SIGKILL || - s->deserialized_state == SERVICE_STOP_POST || - s->deserialized_state == SERVICE_FINAL_SIGTERM || - s->deserialized_state == SERVICE_FINAL_SIGKILL) { + if (IN_SET(s->deserialized_state, + SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, + SERVICE_RELOAD, + SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, + SERVICE_STOP_POST, + SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { usec_t k; - k = s->deserialized_state == SERVICE_START_PRE || s->deserialized_state == SERVICE_START || - s->deserialized_state == SERVICE_START_POST || s->deserialized_state == SERVICE_RELOAD ? - s->timeout_start_usec : s->timeout_stop_usec; + k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec; /* For the start/stop timeouts 0 means off */ if (k > 0) { @@ -1595,38 +1592,30 @@ static int service_coldplug(Unit *u) { return r; } - if ((s->deserialized_state == SERVICE_START && - (s->type == SERVICE_FORKING || - s->type == SERVICE_DBUS || - s->type == SERVICE_ONESHOT || - s->type == SERVICE_NOTIFY)) || - s->deserialized_state == SERVICE_START_POST || - s->deserialized_state == SERVICE_RUNNING || - s->deserialized_state == SERVICE_RELOAD || - s->deserialized_state == SERVICE_STOP || - s->deserialized_state == SERVICE_STOP_SIGTERM || - s->deserialized_state == SERVICE_STOP_SIGKILL) - if (s->main_pid > 0) { - r = unit_watch_pid(UNIT(s), s->main_pid); - if (r < 0) - return r; - } + if (pid_valid(s->main_pid) && + ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) || + IN_SET(s->deserialized_state, + SERVICE_START, SERVICE_START_POST, + SERVICE_RUNNING, SERVICE_RELOAD, + SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, + SERVICE_STOP_POST, + SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) { + r = unit_watch_pid(UNIT(s), s->main_pid); + if (r < 0) + return r; + } - if (s->deserialized_state == SERVICE_START_PRE || - s->deserialized_state == SERVICE_START || - s->deserialized_state == SERVICE_START_POST || - s->deserialized_state == SERVICE_RELOAD || - s->deserialized_state == SERVICE_STOP || - s->deserialized_state == SERVICE_STOP_SIGTERM || - s->deserialized_state == SERVICE_STOP_SIGKILL || - s->deserialized_state == SERVICE_STOP_POST || - s->deserialized_state == SERVICE_FINAL_SIGTERM || - s->deserialized_state == SERVICE_FINAL_SIGKILL) - if (s->control_pid > 0) { - r = unit_watch_pid(UNIT(s), s->control_pid); - if (r < 0) - return r; - } + if (pid_valid(s->control_pid) && + IN_SET(s->deserialized_state, + SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, + SERVICE_RELOAD, + SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, + SERVICE_STOP_POST, + SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { + r = unit_watch_pid(UNIT(s), s->control_pid); + if (r < 0) + return r; + } if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) service_start_watchdog(s); @@ -1761,7 +1750,7 @@ static int service_spawn( if (r < 0) goto fail; - our_env = new0(char*, 5); + our_env = new0(char*, 4); if (!our_env) { r = -ENOMEM; goto fail; @@ -1779,12 +1768,6 @@ static int service_spawn( goto fail; } - if (s->watchdog_usec > 0) - if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) { - r = -ENOMEM; - goto fail; - } - if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM) if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) { r = -ENOMEM; @@ -1815,6 +1798,7 @@ static int service_spawn( UNIT(s)->manager->cgroup_supported, path, UNIT(s)->id, + s->watchdog_usec, s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL, s->exec_runtime, &pid); @@ -3023,6 +3007,14 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { /* If there is still a control process, wait for that first */ break; + case SERVICE_STOP_POST: + case SERVICE_FINAL_SIGTERM: + case SERVICE_FINAL_SIGKILL: + + if (!control_pid_good(s)) + service_enter_dead(s, f, true); + break; + default: assert_not_reached("Uh, main process died at wrong time."); } @@ -3164,7 +3156,8 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { case SERVICE_STOP_POST: case SERVICE_FINAL_SIGTERM: case SERVICE_FINAL_SIGKILL: - service_enter_dead(s, f, true); + if (main_pid_good(s) <= 0) + service_enter_dead(s, f, true); break; default: @@ -3328,6 +3321,7 @@ static void service_notify_cgroup_empty_event(Unit *u) { break; + case SERVICE_STOP_POST: case SERVICE_FINAL_SIGTERM: case SERVICE_FINAL_SIGKILL: if (main_pid_good(s) <= 0 && !control_pid_good(s)) @@ -3343,6 +3337,7 @@ static void service_notify_cgroup_empty_event(Unit *u) { static void service_notify_message(Unit *u, pid_t pid, char **tags) { Service *s = SERVICE(u); const char *e; + bool notify_dbus = false; assert(u); @@ -3360,8 +3355,7 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) { return; } - log_debug_unit(u->id, - "%s: Got message", u->id); + log_debug_unit(u->id, "%s: Got message", u->id); /* Interpret MAINPID= */ if ((e = strv_find_prefix(tags, "MAINPID=")) && @@ -3371,24 +3365,20 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) { s->state == SERVICE_RELOAD)) { if (parse_pid(e + 8, &pid) < 0) - log_warning_unit(u->id, - "Failed to parse notification message %s", e); + log_warning_unit(u->id, "Failed to parse notification message %s", e); else { - log_debug_unit(u->id, - "%s: got %s", u->id, e); + log_debug_unit(u->id, "%s: got %s", u->id, e); service_set_main_pid(s, pid); unit_watch_pid(UNIT(s), pid); + notify_dbus = true; } } /* Interpret READY= */ - if (s->type == SERVICE_NOTIFY && - s->state == SERVICE_START && - strv_find(tags, "READY=1")) { - log_debug_unit(u->id, - "%s: got READY=1", u->id); - + if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) { + log_debug_unit(u->id, "%s: got READY=1", u->id); service_enter_start_post(s); + notify_dbus = true; } /* Interpret STATUS= */ @@ -3397,39 +3387,39 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags) { char *t; if (e[7]) { - if (!utf8_is_valid(e+7)) { - log_warning_unit(u->id, - "Status message in notification is not UTF-8 clean."); + log_warning_unit(u->id, "Status message in notification is not UTF-8 clean."); return; } + log_debug_unit(u->id, "%s: got %s", u->id, e); + t = strdup(e+7); if (!t) { - log_error_unit(u->id, - "Failed to allocate string."); + log_oom(); return; } - log_debug_unit(u->id, - "%s: got %s", u->id, e); + } else + t = NULL; + if (!streq_ptr(s->status_text, t)) { free(s->status_text); s->status_text = t; - } else { - free(s->status_text); - s->status_text = NULL; - } - + notify_dbus = true; + } else + free(t); } + /* Interpet WATCHDOG= */ if (strv_find(tags, "WATCHDOG=1")) { log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id); service_reset_watchdog(s); } /* Notify clients about changed status or main pid */ - unit_add_to_dbus_queue(u); + if (notify_dbus) + unit_add_to_dbus_queue(u); } #ifdef HAVE_SYSV_COMPAT @@ -3666,6 +3656,8 @@ static void service_bus_name_owner_change( } int service_set_socket_fd(Service *s, int fd, Socket *sock) { + _cleanup_free_ char *peer = NULL; + int r; assert(s); assert(fd >= 0); @@ -3683,6 +3675,23 @@ int service_set_socket_fd(Service *s, int fd, Socket *sock) { if (s->state != SERVICE_DEAD) return -EAGAIN; + if (getpeername_pretty(fd, &peer) >= 0) { + + if (UNIT(s)->description) { + _cleanup_free_ char *a; + + a = strjoin(UNIT(s)->description, " (", peer, ")", NULL); + if (!a) + return -ENOMEM; + + r = unit_set_description(UNIT(s), a); + } else + r = unit_set_description(UNIT(s), peer); + + if (r < 0) + return r; + } + s->socket_fd = fd; unit_ref_set(&s->accept_socket, UNIT(sock)); @@ -3841,7 +3850,6 @@ const UnitVTable service_vtable = { .bus_interface = "org.freedesktop.systemd1.Service", .bus_vtable = bus_service_vtable, - .bus_changing_properties = bus_service_changing_properties, .bus_set_property = bus_service_set_property, .bus_commit_properties = bus_service_commit_properties,