X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fcore%2Fsocket.c;h=7c18a2b75c17898676df6b6e7c79b33fe5bbabb6;hp=eae9465788058b5b726b530386dc9bfc4a6f4c53;hb=2f20a8ebdb5aed3146f366360762d8963efe8d82;hpb=718db96199eb307751264e4163555662c9a389fa diff --git a/src/core/socket.c b/src/core/socket.c index eae946578..7c18a2b75 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -34,8 +34,6 @@ #endif #include "sd-event.h" -#include "unit.h" -#include "socket.h" #include "log.h" #include "load-dropin.h" #include "load-fragment.h" @@ -44,7 +42,6 @@ #include "path-util.h" #include "unit-name.h" #include "unit-printf.h" -#include "dbus-socket.h" #include "missing.h" #include "special.h" #include "label.h" @@ -53,6 +50,9 @@ #include "smack-util.h" #include "bus-util.h" #include "bus-error.h" +#include "dbus-socket.h" +#include "unit.h" +#include "socket.h" static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { [SOCKET_DEAD] = UNIT_INACTIVE, @@ -90,11 +90,8 @@ static void socket_init(Unit *u) { s->ip_ttl = -1; s->mark = -1; - exec_context_init(&s->exec_context); s->exec_context.std_output = u->manager->default_std_output; s->exec_context.std_error = u->manager->default_std_error; - kill_context_init(&s->kill_context); - cgroup_context_init(&s->cgroup_context); s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; } @@ -119,9 +116,7 @@ void socket_free_ports(Socket *s) { sd_event_source_unref(p->event_source); - if (p->fd >= 0) - close_nointr_nofail(p->fd); - + safe_close(p->fd); free(p->path); free(p); } @@ -134,9 +129,7 @@ static void socket_done(Unit *u) { socket_free_ports(s); - exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager)); - cgroup_context_init(&s->cgroup_context); - + s->exec_runtime = exec_runtime_unref(s->exec_runtime); exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); s->control_command = NULL; @@ -175,11 +168,17 @@ static int socket_arm_timer(Socket *s) { return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); } - return sd_event_add_monotonic(UNIT(s)->manager->event, now(CLOCK_MONOTONIC) + s->timeout_usec, 0, socket_dispatch_timer, s, &s->timer_event_source); + return sd_event_add_time( + UNIT(s)->manager->event, + &s->timer_event_source, + CLOCK_MONOTONIC, + now(CLOCK_MONOTONIC) + s->timeout_usec, 0, + socket_dispatch_timer, s); } static int socket_instantiate_service(Socket *s) { - char *prefix, *name; + _cleanup_free_ char *prefix = NULL; + _cleanup_free_ char *name = NULL; int r; Unit *u; @@ -195,18 +194,14 @@ static int socket_instantiate_service(Socket *s) { assert(s->accept); - if (!(prefix = unit_name_to_prefix(UNIT(s)->id))) + prefix = unit_name_to_prefix(UNIT(s)->id); + if (!prefix) return -ENOMEM; - r = asprintf(&name, "%s@%u.service", prefix, s->n_accepted); - free(prefix); - - if (r < 0) + if (asprintf(&name, "%s@%u.service", prefix, s->n_accepted) < 0) return -ENOMEM; r = manager_load_unit(UNIT(s)->manager, name, NULL, NULL, &u); - free(name); - if (r < 0) return r; @@ -243,48 +238,6 @@ static bool have_non_accept_socket(Socket *s) { return false; } -static int socket_verify(Socket *s) { - assert(s); - - if (UNIT(s)->load_state != UNIT_LOADED) - return 0; - - if (!s->ports) { - log_error_unit(UNIT(s)->id, - "%s lacks Listen setting. Refusing.", UNIT(s)->id); - return -EINVAL; - } - - if (s->accept && have_non_accept_socket(s)) { - log_error_unit(UNIT(s)->id, - "%s configured for accepting sockets, but sockets are non-accepting. Refusing.", - UNIT(s)->id); - return -EINVAL; - } - - if (s->accept && s->max_connections <= 0) { - log_error_unit(UNIT(s)->id, - "%s's MaxConnection setting too small. Refusing.", UNIT(s)->id); - return -EINVAL; - } - - if (s->accept && UNIT_DEREF(s->service)) { - log_error_unit(UNIT(s)->id, - "Explicit service configuration for accepting sockets not supported on %s. Refusing.", - UNIT(s)->id); - return -EINVAL; - } - - if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) { - log_error_unit(UNIT(s)->id, - "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", - UNIT(s)->id); - return -EINVAL; - } - - return 0; -} - static int socket_add_mount_links(Socket *s) { SocketPort *p; int r; @@ -312,20 +265,14 @@ static int socket_add_mount_links(Socket *s) { static int socket_add_device_link(Socket *s) { char *t; - int r; assert(s); if (!s->bind_to_device || streq(s->bind_to_device, "lo")) return 0; - if (asprintf(&t, "/sys/subsystem/net/devices/%s", s->bind_to_device) < 0) - return -ENOMEM; - - r = unit_add_node_link(UNIT(s), t, false); - free(t); - - return r; + t = strappenda("/sys/subsystem/net/devices/", s->bind_to_device); + return unit_add_node_link(UNIT(s), t, false); } static int socket_add_default_dependencies(Socket *s) { @@ -356,55 +303,109 @@ _pure_ static bool socket_has_exec(Socket *s) { return false; } -static int socket_load(Unit *u) { - Socket *s = SOCKET(u); +static int socket_add_extras(Socket *s) { + Unit *u = UNIT(s); int r; - assert(u); - assert(u->load_state == UNIT_STUB); - - if ((r = unit_load_fragment_and_dropin(u)) < 0) - return r; - - /* This is a new unit? Then let's add in some extras */ - if (u->load_state == UNIT_LOADED) { - - if (have_non_accept_socket(s)) { - - if (!UNIT_DEREF(s->service)) { - Unit *x; + assert(s); - r = unit_load_related_unit(u, ".service", &x); - if (r < 0) - return r; + if (have_non_accept_socket(s)) { - unit_ref_set(&s->service, x); - } + if (!UNIT_DEREF(s->service)) { + Unit *x; - r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true); + r = unit_load_related_unit(u, ".service", &x); if (r < 0) return r; + + unit_ref_set(&s->service, x); } - if ((r = socket_add_mount_links(s)) < 0) + r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true); + if (r < 0) return r; + } - if ((r = socket_add_device_link(s)) < 0) + r = socket_add_mount_links(s); + if (r < 0) + return r; + + r = socket_add_device_link(s); + if (r < 0) + return r; + + r = unit_patch_contexts(u); + if (r < 0) + return r; + + if (socket_has_exec(s)) { + r = unit_add_exec_dependencies(u, &s->exec_context); + if (r < 0) return r; - if (socket_has_exec(s)) - if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) - return r; + r = unit_add_default_slice(u, &s->cgroup_context); + if (r < 0) + return r; + } - r = unit_add_default_slice(u); + if (u->default_dependencies) { + r = socket_add_default_dependencies(s); if (r < 0) return r; + } - if (UNIT(s)->default_dependencies) - if ((r = socket_add_default_dependencies(s)) < 0) - return r; + return 0; +} + +static int socket_verify(Socket *s) { + assert(s); + + if (UNIT(s)->load_state != UNIT_LOADED) + return 0; + + if (!s->ports) { + log_error_unit(UNIT(s)->id, "%s lacks Listen setting. Refusing.", UNIT(s)->id); + return -EINVAL; + } + + if (s->accept && have_non_accept_socket(s)) { + log_error_unit(UNIT(s)->id, "%s configured for accepting sockets, but sockets are non-accepting. Refusing.", + UNIT(s)->id); + return -EINVAL; + } + + if (s->accept && s->max_connections <= 0) { + log_error_unit(UNIT(s)->id, "%s's MaxConnection setting too small. Refusing.", UNIT(s)->id); + return -EINVAL; + } - r = unit_exec_context_defaults(u, &s->exec_context); + if (s->accept && UNIT_DEREF(s->service)) { + log_error_unit(UNIT(s)->id, "Explicit service configuration for accepting sockets not supported on %s. Refusing.", UNIT(s)->id); + return -EINVAL; + } + + if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) { + log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id); + return -EINVAL; + } + + return 0; +} + +static int socket_load(Unit *u) { + Socket *s = SOCKET(u); + int r; + + assert(u); + assert(u->load_state == UNIT_STUB); + + r = unit_load_fragment_and_dropin(u); + if (r < 0) + return r; + + if (u->load_state == UNIT_LOADED) { + /* This is a new unit? Then let's add in some extras */ + r = socket_add_extras(s); if (r < 0) return r; } @@ -429,18 +430,15 @@ _const_ static const char* listen_lookup(int family, int type) { } static void socket_dump(Unit *u, FILE *f, const char *prefix) { - SocketExecCommand c; Socket *s = SOCKET(u); SocketPort *p; const char *prefix2; - char *p2; assert(s); assert(f); - p2 = strappend(prefix, "\t"); - prefix2 = p2 ? p2 : prefix; + prefix2 = strappenda(prefix, "\t"); fprintf(f, "%sSocket State: %s\n" @@ -588,20 +586,12 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) { exec_command_dump_list(s->exec_command[c], f, prefix2); } - - free(p2); } static int instance_from_socket(int fd, unsigned nr, char **instance) { socklen_t l; char *r; - union { - struct sockaddr sa; - struct sockaddr_un un; - struct sockaddr_in in; - struct sockaddr_in6 in6; - struct sockaddr_storage storage; - } local, remote; + union sockaddr_union local, remote; assert(fd >= 0); assert(instance); @@ -670,10 +660,11 @@ static int instance_from_socket(int fd, unsigned nr, char **instance) { case AF_UNIX: { struct ucred ucred; + int k; - l = sizeof(ucred); - if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) - return -errno; + k = getpeercred(fd, &ucred); + if (k < 0) + return k; if (asprintf(&r, "%u-%lu-%lu", @@ -705,7 +696,7 @@ static void socket_close_fds(Socket *s) { if (p->fd < 0) continue; - close_nointr_nofail(p->fd); + p->fd = safe_close(p->fd); /* One little note: we should never delete any sockets * in the file system here! After all some other @@ -714,8 +705,6 @@ static void socket_close_fds(Socket *s) { * we delete sockets in the file system before we * create a new one, not after we stopped using * one! */ - - p->fd = -1; } } @@ -885,9 +874,7 @@ static int fifo_address_create( fail: label_context_clear(); - - if (fd >= 0) - close_nointr_nofail(fd); + safe_close(fd); return r; } @@ -922,8 +909,7 @@ static int special_address_create( return 0; fail: - if (fd >= 0) - close_nointr_nofail(fd); + safe_close(fd); return r; } @@ -982,9 +968,7 @@ static int mq_address_create( return 0; fail: - if (fd >= 0) - close_nointr_nofail(fd); - + safe_close(fd); return r; } @@ -1111,7 +1095,7 @@ static int socket_watch_fds(Socket *s) { if (p->event_source) r = sd_event_source_set_enabled(p->event_source, SD_EVENT_ON); else - r = sd_event_add_io(UNIT(s)->manager->event, p->fd, EPOLLIN, socket_dispatch_io, p, &p->event_source); + r = sd_event_add_io(UNIT(s)->manager->event, &p->event_source, p->fd, EPOLLIN, socket_dispatch_io, p); if (r < 0) { log_warning_unit(UNIT(s)->id, "Failed to watch listening fds: %s", strerror(-r)); @@ -1160,10 +1144,8 @@ static void socket_set_state(Socket *s, SocketState state) { socket_close_fds(s); if (state != old_state) - log_debug_unit(UNIT(s)->id, - "%s changed %s -> %s", UNIT(s)->id, - socket_state_to_string(old_state), - socket_state_to_string(state)); + log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", + UNIT(s)->id, socket_state_to_string(old_state), socket_state_to_string(state)); unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true); } @@ -1175,49 +1157,48 @@ static int socket_coldplug(Unit *u) { assert(s); assert(s->state == SOCKET_DEAD); - if (s->deserialized_state != s->state) { + if (s->deserialized_state == s->state) + return 0; - if (s->deserialized_state == SOCKET_START_PRE || - s->deserialized_state == SOCKET_START_POST || - s->deserialized_state == SOCKET_STOP_PRE || - s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || - s->deserialized_state == SOCKET_STOP_PRE_SIGKILL || - s->deserialized_state == SOCKET_STOP_POST || - s->deserialized_state == SOCKET_FINAL_SIGTERM || - s->deserialized_state == SOCKET_FINAL_SIGKILL) { + if (s->deserialized_state == SOCKET_START_PRE || + s->deserialized_state == SOCKET_START_POST || + s->deserialized_state == SOCKET_STOP_PRE || + s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || + s->deserialized_state == SOCKET_STOP_PRE_SIGKILL || + s->deserialized_state == SOCKET_STOP_POST || + s->deserialized_state == SOCKET_FINAL_SIGTERM || + s->deserialized_state == SOCKET_FINAL_SIGKILL) { - if (s->control_pid <= 0) - return -EBADMSG; + if (s->control_pid <= 0) + return -EBADMSG; - r = unit_watch_pid(UNIT(s), s->control_pid); - if (r < 0) - return r; - - r = socket_arm_timer(s); - if (r < 0) - return r; - } + r = unit_watch_pid(UNIT(s), s->control_pid); + if (r < 0) + return r; - if (s->deserialized_state == SOCKET_START_POST || - s->deserialized_state == SOCKET_LISTENING || - s->deserialized_state == SOCKET_RUNNING || - s->deserialized_state == SOCKET_STOP_PRE || - s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || - s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) { - r = socket_open_fds(s); - if (r < 0) - return r; - } + r = socket_arm_timer(s); + if (r < 0) + return r; + } - if (s->deserialized_state == SOCKET_LISTENING) { - r = socket_watch_fds(s); - if (r < 0) - return r; - } + if (s->deserialized_state == SOCKET_START_POST || + s->deserialized_state == SOCKET_LISTENING || + s->deserialized_state == SOCKET_RUNNING || + s->deserialized_state == SOCKET_STOP_PRE || + s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || + s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) { + r = socket_open_fds(s); + if (r < 0) + return r; + } - socket_set_state(s, s->deserialized_state); + if (s->deserialized_state == SOCKET_LISTENING) { + r = socket_watch_fds(s); + if (r < 0) + return r; } + socket_set_state(s, s->deserialized_state); return 0; } @@ -1232,6 +1213,10 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { unit_realize_cgroup(UNIT(s)); + r = unit_setup_exec_runtime(UNIT(s)); + if (r < 0) + goto fail; + r = socket_arm_timer(s); if (r < 0) goto fail; @@ -1251,8 +1236,11 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { UNIT(s)->manager->confirm_spawn, UNIT(s)->manager->cgroup_supported, UNIT(s)->cgroup_path, + manager_get_runtime_prefix(UNIT(s)->manager), UNIT(s)->id, + 0, NULL, + s->exec_runtime, &pid); strv_free(argv); @@ -1280,7 +1268,11 @@ static void socket_enter_dead(Socket *s, SocketResult f) { if (f != SOCKET_SUCCESS) s->result = f; - exec_context_tmp_dirs_done(&s->exec_context); + exec_runtime_destroy(s->exec_runtime); + s->exec_runtime = exec_runtime_unref(s->exec_runtime); + + exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager)); + socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD); } @@ -1338,17 +1330,19 @@ static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) { goto fail; socket_set_state(s, state); - } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) + } else if (state == SOCKET_STOP_PRE_SIGTERM) + socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_SUCCESS); + else if (state == SOCKET_STOP_PRE_SIGKILL) socket_enter_stop_post(s, SOCKET_SUCCESS); + else if (state == SOCKET_FINAL_SIGTERM) + socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS); else socket_enter_dead(s, SOCKET_SUCCESS); return; fail: - log_warning_unit(UNIT(s)->id, - "%s failed to kill processes: %s", - UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r)); if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); @@ -1378,9 +1372,7 @@ static void socket_enter_stop_pre(Socket *s, SocketResult f) { return; fail: - log_warning_unit(UNIT(s)->id, - "%s failed to run 'stop-pre' task: %s", - UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to run 'stop-pre' task: %s", UNIT(s)->id, strerror(-r)); socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); } @@ -1390,9 +1382,7 @@ static void socket_enter_listening(Socket *s) { r = socket_watch_fds(s); if (r < 0) { - log_warning_unit(UNIT(s)->id, - "%s failed to watch sockets: %s", - UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to watch sockets: %s", UNIT(s)->id, strerror(-r)); goto fail; } @@ -1409,9 +1399,7 @@ static void socket_enter_start_post(Socket *s) { r = socket_open_fds(s); if (r < 0) { - log_warning_unit(UNIT(s)->id, - "%s failed to listen on sockets: %s", - UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to listen on sockets: %s", UNIT(s)->id, strerror(-r)); goto fail; } @@ -1422,9 +1410,7 @@ static void socket_enter_start_post(Socket *s) { if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { r = socket_spawn(s, s->control_command, &s->control_pid); if (r < 0) { - log_warning_unit(UNIT(s)->id, - "%s failed to run 'start-post' task: %s", - UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r)); goto fail; } @@ -1447,7 +1433,8 @@ static void socket_enter_start_pre(Socket *s) { s->control_command_id = SOCKET_EXEC_START_PRE; if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) { - if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) + r = socket_spawn(s, s->control_command, &s->control_pid); + if (r < 0) goto fail; socket_set_state(s, SOCKET_START_PRE); @@ -1457,9 +1444,7 @@ static void socket_enter_start_pre(Socket *s) { return; fail: - log_warning_unit(UNIT(s)->id, - "%s failed to run 'start-pre' task: %s", - UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r)); socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); } @@ -1472,21 +1457,25 @@ static void socket_enter_running(Socket *s, int cfd) { /* We don't take connections anymore if we are supposed to * shut down anyway */ if (unit_stop_pending(UNIT(s))) { - log_debug_unit(UNIT(s)->id, - "Suppressing connection request on %s since unit stop is scheduled.", - UNIT(s)->id); + + log_debug_unit(UNIT(s)->id, "Suppressing connection request on %s since unit stop is scheduled.", UNIT(s)->id); if (cfd >= 0) - close_nointr_nofail(cfd); + safe_close(cfd); else { /* Flush all sockets by closing and reopening them */ socket_close_fds(s); + r = socket_open_fds(s); + if (r < 0) { + log_warning_unit(UNIT(s)->id, "%s failed to listen on sockets: %s", UNIT(s)->id, strerror(-r)); + socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); + return; + } + r = socket_watch_fds(s); if (r < 0) { - log_warning_unit(UNIT(s)->id, - "%s failed to watch sockets: %s", - UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to watch sockets: %s", UNIT(s)->id, strerror(-r)); socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); } } @@ -1496,13 +1485,13 @@ static void socket_enter_running(Socket *s, int cfd) { if (cfd < 0) { Iterator i; - Unit *u; + Unit *other; bool pending = false; /* If there's already a start pending don't bother to * do anything */ - SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERS], i) - if (unit_active_or_pending(u)) { + SET_FOREACH(other, UNIT(s)->dependencies[UNIT_TRIGGERS], i) + if (unit_active_or_pending(other)) { pending = true; break; } @@ -1519,10 +1508,8 @@ static void socket_enter_running(Socket *s, int cfd) { Service *service; if (s->n_connections >= s->max_connections) { - log_warning_unit(UNIT(s)->id, - "%s: Too many incoming connections (%u)", - UNIT(s)->id, s->n_connections); - close_nointr_nofail(cfd); + log_warning_unit(UNIT(s)->id, "%s: Too many incoming connections (%u)", UNIT(s)->id, s->n_connections); + safe_close(cfd); return; } @@ -1537,7 +1524,7 @@ static void socket_enter_running(Socket *s, int cfd) { /* ENOTCONN is legitimate if TCP RST was received. * This connection is over, but the socket unit lives on. */ - close_nointr_nofail(cfd); + safe_close(cfd); return; } @@ -1548,7 +1535,6 @@ static void socket_enter_running(Socket *s, int cfd) { } name = unit_name_build(prefix, instance, ".service"); - if (!name) { r = -ENOMEM; goto fail; @@ -1584,15 +1570,12 @@ static void socket_enter_running(Socket *s, int cfd) { return; fail: - log_warning_unit(UNIT(s)->id, - "%s failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s", - UNIT(s)->id, - cfd >= 0 ? "template" : "non-template", + log_warning_unit(UNIT(s)->id, "%s failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s", + UNIT(s)->id, cfd >= 0 ? "template" : "non-template", bus_error_message(&error, r)); - socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); - if (cfd >= 0) - close_nointr_nofail(cfd); + socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); + safe_close(cfd); } static void socket_run_next(Socket *s) { @@ -1606,15 +1589,14 @@ static void socket_run_next(Socket *s) { s->control_command = s->control_command->command_next; - if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) + r = socket_spawn(s, s->control_command, &s->control_pid); + if (r < 0) goto fail; return; fail: - log_warning_unit(UNIT(s)->id, - "%s failed to run next task: %s", - UNIT(s)->id, strerror(-r)); + log_warning_unit(UNIT(s)->id, "%s failed to run next task: %s", UNIT(s)->id, strerror(-r)); if (s->state == SOCKET_START_POST) socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); @@ -1650,9 +1632,7 @@ static int socket_start(Unit *u) { service = SERVICE(UNIT_DEREF(s->service)); if (UNIT(service)->load_state != UNIT_LOADED) { - log_error_unit(u->id, - "Socket service %s not loaded, refusing.", - UNIT(service)->id); + log_error_unit(u->id, "Socket service %s not loaded, refusing.", UNIT(service)->id); return -ENOENT; } @@ -1661,9 +1641,7 @@ static int socket_start(Unit *u) { if (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED && service->state != SERVICE_AUTO_RESTART) { - log_error_unit(u->id, - "Socket service %s already active, refusing.", - UNIT(service)->id); + log_error_unit(u->id, "Socket service %s already active, refusing.", UNIT(service)->id); return -EBUSY; } @@ -1680,6 +1658,7 @@ static int socket_start(Unit *u) { s->result = SOCKET_SUCCESS; socket_enter_start_pre(s); + return 0; } @@ -1736,11 +1715,12 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { if (p->fd < 0) continue; - if ((copy = fdset_put_dup(fds, p->fd)) < 0) + copy = fdset_put_dup(fds, p->fd); + if (copy < 0) return copy; if (p->type == SOCKET_SOCKET) { - char *t; + _cleanup_free_ char *t = NULL; r = socket_address_print(&p->address, &t); if (r < 0) @@ -1750,7 +1730,7 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { unit_serialize_item_format(u, f, "netlink", "%i %s", copy, t); else unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t); - free(t); + } else if (p->type == SOCKET_SPECIAL) unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path); else if (p->type == SOCKET_MQUEUE) @@ -1761,8 +1741,6 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { } } - exec_context_serialize(&s->exec_context, UNIT(s), f); - return 0; } @@ -1772,15 +1750,13 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, assert(u); assert(key); assert(value); - assert(fds); if (streq(key, "state")) { SocketState state; state = socket_state_from_string(value); if (state < 0) - log_debug_unit(u->id, - "Failed to parse state value %s", value); + log_debug_unit(u->id, "Failed to parse state value %s", value); else s->deserialized_state = state; } else if (streq(key, "result")) { @@ -1788,8 +1764,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, f = socket_result_from_string(value); if (f < 0) - log_debug_unit(u->id, - "Failed to parse result value %s", value); + log_debug_unit(u->id, "Failed to parse result value %s", value); else if (f != SOCKET_SUCCESS) s->result = f; @@ -1797,16 +1772,14 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, unsigned k; if (safe_atou(value, &k) < 0) - log_debug_unit(u->id, - "Failed to parse n-accepted value %s", value); + log_debug_unit(u->id, "Failed to parse n-accepted value %s", value); else s->n_accepted += k; } else if (streq(key, "control-pid")) { pid_t pid; if (parse_pid(value, &pid) < 0) - log_debug_unit(u->id, - "Failed to parse control-pid value %s", value); + log_debug_unit(u->id, "Failed to parse control-pid value %s", value); else s->control_pid = pid; } else if (streq(key, "control-command")) { @@ -1814,8 +1787,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, id = socket_exec_command_from_string(value); if (id < 0) - log_debug_unit(u->id, - "Failed to parse exec-command value %s", value); + log_debug_unit(u->id, "Failed to parse exec-command value %s", value); else { s->control_command_id = id; s->control_command = s->exec_command[id]; @@ -1825,8 +1797,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, SocketPort *p; if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) - log_debug_unit(u->id, - "Failed to parse fifo value %s", value); + log_debug_unit(u->id, "Failed to parse fifo value %s", value); else { LIST_FOREACH(port, p, s->ports) @@ -1835,8 +1806,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, break; if (p) { - if (p->fd >= 0) - close_nointr_nofail(p->fd); + safe_close(p->fd); p->fd = fdset_remove(fds, fd); } } @@ -1846,8 +1816,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, SocketPort *p; if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) - log_debug_unit(u->id, - "Failed to parse special value %s", value); + log_debug_unit(u->id, "Failed to parse special value %s", value); else { LIST_FOREACH(port, p, s->ports) @@ -1856,8 +1825,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, break; if (p) { - if (p->fd >= 0) - close_nointr_nofail(p->fd); + safe_close(p->fd); p->fd = fdset_remove(fds, fd); } } @@ -1867,8 +1835,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, SocketPort *p; if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) - log_debug_unit(u->id, - "Failed to parse mqueue value %s", value); + log_debug_unit(u->id, "Failed to parse mqueue value %s", value); else { LIST_FOREACH(port, p, s->ports) @@ -1877,8 +1844,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, break; if (p) { - if (p->fd >= 0) - close_nointr_nofail(p->fd); + safe_close(p->fd); p->fd = fdset_remove(fds, fd); } } @@ -1888,8 +1854,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, SocketPort *p; if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd)) - log_debug_unit(u->id, - "Failed to parse socket value %s", value); + log_debug_unit(u->id, "Failed to parse socket value %s", value); else { LIST_FOREACH(port, p, s->ports) @@ -1897,8 +1862,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, break; if (p) { - if (p->fd >= 0) - close_nointr_nofail(p->fd); + safe_close(p->fd); p->fd = fdset_remove(fds, fd); } } @@ -1908,8 +1872,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, SocketPort *p; if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) - log_debug_unit(u->id, - "Failed to parse socket value %s", value); + log_debug_unit(u->id, "Failed to parse socket value %s", value); else { LIST_FOREACH(port, p, s->ports) @@ -1917,30 +1880,12 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, break; if (p) { - if (p->fd >= 0) - close_nointr_nofail(p->fd); + safe_close(p->fd); p->fd = fdset_remove(fds, fd); } } - } else if (streq(key, "tmp-dir")) { - char *t; - - t = strdup(value); - if (!t) - return log_oom(); - - s->exec_context.tmp_dir = t; - } else if (streq(key, "var-tmp-dir")) { - char *t; - - t = strdup(value); - if (!t) - return log_oom(); - - s->exec_context.var_tmp_dir = t; } else - log_debug_unit(UNIT(s)->id, - "Unknown serialization key '%s'", key); + log_debug_unit(UNIT(s)->id, "Unknown serialization key '%s'", key); return 0; } @@ -2049,12 +1994,10 @@ static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, if (revents != EPOLLIN) { if (revents & EPOLLHUP) - log_error_unit(UNIT(p->socket)->id, - "%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", + log_error_unit(UNIT(p->socket)->id, "%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", UNIT(p->socket)->id); else - log_error_unit(UNIT(p->socket)->id, - "%s: Got unexpected poll event (0x%x) on socket.", + log_error_unit(UNIT(p->socket)->id, "%s: Got unexpected poll event (0x%x) on socket.", UNIT(p->socket)->id, revents); goto fail; @@ -2301,6 +2244,17 @@ int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { return 0; } +static void socket_reset_failed(Unit *u) { + Socket *s = SOCKET(u); + + assert(s); + + if (s->state == SOCKET_FAILED) + socket_set_state(s, SOCKET_DEAD); + + s->result = SOCKET_SUCCESS; +} + static void socket_notify_service_dead(Socket *s, bool failed_permanent) { assert(s); @@ -2310,9 +2264,7 @@ static void socket_notify_service_dead(Socket *s, bool failed_permanent) { * services. */ if (s->state == SOCKET_RUNNING) { - log_debug_unit(UNIT(s)->id, - "%s got notified about service death (failed permanently: %s)", - UNIT(s)->id, yes_no(failed_permanent)); + log_debug_unit(UNIT(s)->id, "%s got notified about service death (failed permanently: %s)", UNIT(s)->id, yes_no(failed_permanent)); if (failed_permanent) socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_FAILED_PERMANENT); else @@ -2331,24 +2283,12 @@ void socket_connection_unref(Socket *s) { assert(s->n_connections > 0); s->n_connections--; - log_debug_unit(UNIT(s)->id, - "%s: One connection closed, %u left.", UNIT(s)->id, s->n_connections); -} - -static void socket_reset_failed(Unit *u) { - Socket *s = SOCKET(u); - - assert(s); - - if (s->state == SOCKET_FAILED) - socket_set_state(s, SOCKET_DEAD); - - s->result = SOCKET_SUCCESS; + log_debug_unit(UNIT(s)->id, "%s: One connection closed, %u left.", UNIT(s)->id, s->n_connections); } static void socket_trigger_notify(Unit *u, Unit *other) { Socket *s = SOCKET(u); - Service *se = SERVICE(other); + Service *se; assert(u); assert(other); @@ -2364,6 +2304,8 @@ static void socket_trigger_notify(Unit *u, Unit *other) { other->type != UNIT_SERVICE) return; + se = SERVICE(other); + if (se->state == SERVICE_FAILED) socket_notify_service_dead(s, se->result == SERVICE_FAILURE_START_LIMIT); @@ -2385,6 +2327,20 @@ static int socket_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { return unit_kill_common(u, who, signo, -1, SOCKET(u)->control_pid, error); } +static int socket_get_timeout(Unit *u, uint64_t *timeout) { + Socket *s = SOCKET(u); + int r; + + if (!s->timer_event_source) + return 0; + + r = sd_event_source_get_time(s->timer_event_source, timeout); + if (r < 0) + return r; + + return 1; +} + static const char* const socket_state_table[_SOCKET_STATE_MAX] = { [SOCKET_DEAD] = "dead", [SOCKET_START_PRE] = "start-pre", @@ -2428,6 +2384,7 @@ const UnitVTable socket_vtable = { .exec_context_offset = offsetof(Socket, exec_context), .cgroup_context_offset = offsetof(Socket, cgroup_context), .kill_context_offset = offsetof(Socket, kill_context), + .exec_runtime_offset = offsetof(Socket, exec_runtime), .sections = "Unit\0" @@ -2448,6 +2405,8 @@ const UnitVTable socket_vtable = { .kill = socket_kill, + .get_timeout = socket_get_timeout, + .serialize = socket_serialize, .deserialize_item = socket_deserialize_item, .distribute_fds = socket_distribute_fds, @@ -2465,7 +2424,6 @@ const UnitVTable socket_vtable = { .bus_interface = "org.freedesktop.systemd1.Socket", .bus_vtable = bus_socket_vtable, - .bus_changing_properties = bus_socket_changing_properties, .bus_set_property = bus_socket_set_property, .bus_commit_properties = bus_socket_commit_properties,