X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fsocket.c;h=7a8624c848fc962495eeb90371aab6863137c077;hb=6398bea5d22a1d53f2fdabbf6fa4dc6f4ac1b6f4;hp=71f1672027d6905557f48f5f6b97da483a91dd98;hpb=0213c3f8102bdc934c629d11a44ca0b408762287;p=elogind.git diff --git a/src/socket.c b/src/socket.c index 71f167202..7a8624c84 100644 --- a/src/socket.c +++ b/src/socket.c @@ -36,7 +36,6 @@ #include "strv.h" #include "unit-name.h" #include "dbus-socket.h" -#include "tcpwrap.h" static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { [SOCKET_DEAD] = UNIT_INACTIVE, @@ -64,6 +63,8 @@ static void socket_init(Unit *u) { s->directory_mode = 0755; s->socket_mode = 0666; + s->max_connections = 64; + exec_context_init(&s->exec_context); s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; @@ -82,6 +83,7 @@ static void socket_unwatch_control_pid(Socket *s) { static void socket_done(Unit *u) { Socket *s = SOCKET(u); SocketPort *p; + Meta *i; assert(s); @@ -108,10 +110,15 @@ static void socket_done(Unit *u) { free(s->bind_to_device); s->bind_to_device = NULL; - free(s->tcpwrap_name); - s->tcpwrap_name = NULL; - unit_unwatch_timer(u, &s->timer_watch); + + /* Make sure no service instance refers to us anymore. */ + LIST_FOREACH(units_per_type, i, u->meta.manager->units_per_type[UNIT_SERVICE]) { + Service *service = (Service *) i; + + if (service->socket == s) + service->socket = NULL; + } } static bool have_non_accept_socket(Socket *s) { @@ -137,11 +144,21 @@ static bool have_non_accept_socket(Socket *s) { static int socket_verify(Socket *s) { assert(s); - if (UNIT(s)->meta.load_state != UNIT_LOADED) + if (s->meta.load_state != UNIT_LOADED) return 0; if (!s->ports) { - log_error("%s lacks Listen setting. Refusing.", UNIT(s)->meta.id); + log_error("%s lacks Listen setting. Refusing.", s->meta.id); + return -EINVAL; + } + + if (s->accept && s->max_connections <= 0) { + log_error("%s's MaxConnection setting too small. Refusing.", s->meta.id); + return -EINVAL; + } + + if (s->exec_context.pam_name && s->kill_mode != KILL_CONTROL_GROUP) { + log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id); return -EINVAL; } @@ -301,23 +318,22 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) { if (s->control_pid > 0) fprintf(f, - "%sControl PID: %llu\n", - prefix, (unsigned long long) s->control_pid); + "%sControl PID: %lu\n", + prefix, (unsigned long) s->control_pid); if (s->bind_to_device) fprintf(f, "%sBindToDevice: %s\n", prefix, s->bind_to_device); - if (s->tcpwrap_name) - fprintf(f, - "%sTCPWrapName: %s\n", - prefix, s->tcpwrap_name); - if (s->accept) fprintf(f, - "%sAccepted: %u\n", - prefix, s->n_accepted); + "%sAccepted: %u\n" + "%sNConnections: %u\n" + "%sMaxConnections: %u\n", + prefix, s->n_accepted, + prefix, s->n_connections, + prefix, s->max_connections); LIST_FOREACH(port, p, s->ports) { @@ -436,10 +452,10 @@ static int instance_from_socket(int fd, unsigned nr, char **instance) { return -errno; if (asprintf(&r, - "%u-%llu-%llu", + "%u-%lu-%lu", nr, - (unsigned long long) ucred.pid, - (unsigned long long) ucred.uid) < 0) + (unsigned long) ucred.pid, + (unsigned long) ucred.uid) < 0) return -ENOMEM; break; @@ -688,8 +704,8 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { s->meta.manager->environment, true, true, - UNIT(s)->meta.manager->confirm_spawn, - UNIT(s)->meta.cgroup_bondings, + s->meta.manager->confirm_spawn, + s->meta.cgroup_bondings, &pid); strv_free(argv); @@ -743,7 +759,7 @@ static void socket_enter_stop_post(Socket *s, bool success) { return; fail: - log_warning("%s failed to run stop-post executable: %s", s->meta.id, strerror(-r)); + log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r)); socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false); } @@ -761,7 +777,7 @@ static void socket_enter_signal(Socket *s, SocketState state, bool success) { if (s->kill_mode == KILL_CONTROL_GROUP) { - if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) { + if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig)) < 0) { if (r != -EAGAIN && r != -ESRCH) goto fail; } else @@ -818,7 +834,7 @@ static void socket_enter_stop_pre(Socket *s, bool success) { return; fail: - log_warning("%s failed to run stop-pre executable: %s", s->meta.id, strerror(-r)); + log_warning("%s failed to run 'stop-pre' task: %s", s->meta.id, strerror(-r)); socket_enter_stop_post(s, false); } @@ -853,7 +869,7 @@ static void socket_enter_start_post(Socket *s) { if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) { - log_warning("%s failed to run start-post executable: %s", s->meta.id, strerror(-r)); + log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r)); goto fail; } @@ -886,7 +902,7 @@ static void socket_enter_start_pre(Socket *s) { return; fail: - log_warning("%s failed to run start-pre exectuable: %s", s->meta.id, strerror(-r)); + log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r)); socket_enter_dead(s, false); } @@ -896,7 +912,7 @@ static void socket_enter_running(Socket *s, int cfd) { assert(s); if (cfd < 0) { - if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, NULL)) < 0) + if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, NULL)) < 0) goto fail; socket_set_state(s, SOCKET_RUNNING); @@ -904,10 +920,16 @@ static void socket_enter_running(Socket *s, int cfd) { Unit *u; char *prefix, *instance, *name; + if (s->n_connections >= s->max_connections) { + log_warning("Too many incoming connections (%u)", s->n_connections); + close_nointr_nofail(cfd); + return; + } + if ((r = instance_from_socket(cfd, s->n_accepted++, &instance)) < 0) goto fail; - if (!(prefix = unit_name_to_prefix(UNIT(s)->meta.id))) { + if (!(prefix = unit_name_to_prefix(s->meta.id))) { free(instance); r = -ENOMEM; goto fail; @@ -922,17 +944,19 @@ static void socket_enter_running(Socket *s, int cfd) { goto fail; } - r = manager_load_unit(UNIT(s)->meta.manager, name, NULL, &u); + r = manager_load_unit(s->meta.manager, name, NULL, &u); free(name); if (r < 0) goto fail; - if ((r = service_set_socket_fd(SERVICE(u), cfd)) < 0) + if ((r = service_set_socket_fd(SERVICE(u), cfd, s)) < 0) goto fail; cfd = -1; + s->n_connections ++; + if ((r = manager_add_job(u->meta.manager, JOB_START, u, JOB_REPLACE, true, NULL)) < 0) goto fail; } @@ -967,7 +991,7 @@ static void socket_run_next(Socket *s, bool success) { return; fail: - log_warning("%s failed to run spawn next executable: %s", s->meta.id, strerror(-r)); + log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r)); if (s->state == SOCKET_START_POST) socket_enter_stop_pre(s, false); @@ -1056,7 +1080,7 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted); if (s->control_pid > 0) - unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) s->control_pid); + unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); if (s->control_command_id >= 0) unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id)); @@ -1119,12 +1143,12 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value, else s->n_accepted += k; } else if (streq(key, "control-pid")) { - unsigned pid; + pid_t pid; - if ((r = safe_atou(value, &pid)) < 0 || pid <= 0) + if ((r = parse_pid(value, &pid)) < 0) log_debug("Failed to parse control-pid value %s", value); else - s->control_pid = (pid_t) pid; + s->control_pid = pid; } else if (streq(key, "control-command")) { SocketExecCommand id; @@ -1190,6 +1214,14 @@ static const char *socket_sub_state_to_string(Unit *u) { return socket_state_to_string(SOCKET(u)->state); } +static bool socket_check_gc(Unit *u) { + Socket *s = SOCKET(u); + + assert(u); + + return s->n_connections > 0; +} + static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { Socket *s = SOCKET(u); int cfd = -1; @@ -1221,12 +1253,6 @@ static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { break; } - - if (s->tcpwrap_name) - if (!socket_tcpwrap(cfd, s->tcpwrap_name)) { - close_nointr_nofail(cfd); - return; - } } socket_enter_running(s, cfd); @@ -1390,7 +1416,10 @@ int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { void socket_notify_service_dead(Socket *s) { assert(s); - /* The service is dead. Dang. */ + /* The service is dead. Dang! + * + * This is strictly for one-instance-for-all-connections + * services. */ if (s->state == SOCKET_RUNNING) { log_debug("%s got notified about service death.", s->meta.id); @@ -1398,6 +1427,20 @@ void socket_notify_service_dead(Socket *s) { } } +void socket_connection_unref(Socket *s) { + assert(s); + + /* The service is dead. Yay! + * + * This is strictly for one-onstance-per-connection + * services. */ + + assert(s->n_connections > 0); + s->n_connections--; + + log_debug("%s: One connection closed, %u left.", s->meta.id, s->n_connections); +} + static const char* const socket_state_table[_SOCKET_STATE_MAX] = { [SOCKET_DEAD] = "dead", [SOCKET_START_PRE] = "start-pre", @@ -1444,6 +1487,8 @@ const UnitVTable socket_vtable = { .active_state = socket_active_state, .sub_state_to_string = socket_sub_state_to_string, + .check_gc = socket_check_gc, + .fd_event = socket_fd_event, .sigchld_event = socket_sigchld_event, .timer_event = socket_timer_event,