X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=socket.c;h=1b23483881549caa3de4ce14882a8360e5e1bdfb;hp=a8a1914249f115e03db339f17317b5eb9bde5940;hb=c43d20a0af915a2641e9645ba7f47f576f9075e3;hpb=034c6ed7da5e44bfdde5a5d0da75f7b7a59953b8 diff --git a/socket.c b/socket.c index a8a191424..1b2348388 100644 --- a/socket.c +++ b/socket.c @@ -8,61 +8,27 @@ #include #include -#include "name.h" +#include "unit.h" #include "socket.h" #include "log.h" -static const NameActiveState state_table[_SOCKET_STATE_MAX] = { - [SOCKET_DEAD] = NAME_INACTIVE, - [SOCKET_START_PRE] = NAME_ACTIVATING, - [SOCKET_START_POST] = NAME_ACTIVATING, - [SOCKET_LISTENING] = NAME_ACTIVE, - [SOCKET_RUNNING] = NAME_ACTIVE, - [SOCKET_STOP_PRE] = NAME_DEACTIVATING, - [SOCKET_STOP_PRE_SIGTERM] = NAME_DEACTIVATING, - [SOCKET_STOP_PRE_SIGKILL] = NAME_DEACTIVATING, - [SOCKET_STOP_POST] = NAME_DEACTIVATING, - [SOCKET_STOP_POST_SIGTERM] = NAME_DEACTIVATING, - [SOCKET_STOP_POST_SIGKILL] = NAME_DEACTIVATING, - [SOCKET_MAINTAINANCE] = NAME_INACTIVE, +static const UnitActiveState state_table[_SOCKET_STATE_MAX] = { + [SOCKET_DEAD] = UNIT_INACTIVE, + [SOCKET_START_PRE] = UNIT_ACTIVATING, + [SOCKET_START_POST] = UNIT_ACTIVATING, + [SOCKET_LISTENING] = UNIT_ACTIVE, + [SOCKET_RUNNING] = UNIT_ACTIVE, + [SOCKET_STOP_PRE] = UNIT_DEACTIVATING, + [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING, + [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING, + [SOCKET_STOP_POST] = UNIT_DEACTIVATING, + [SOCKET_STOP_POST_SIGTERM] = UNIT_DEACTIVATING, + [SOCKET_STOP_POST_SIGKILL] = UNIT_DEACTIVATING, + [SOCKET_MAINTAINANCE] = UNIT_INACTIVE, }; -static int socket_init(Name *n) { - Socket *s = SOCKET(n); - char *t; - int r; - - /* First, reset everything to the defaults, in case this is a - * reload */ - - s->bind_ipv6_only = false; - s->backlog = SOMAXCONN; - s->timeout_usec = DEFAULT_TIMEOUT_USEC; - exec_context_init(&s->exec_context); - - if ((r = name_load_fragment_and_dropin(n)) < 0) - return r; - - if (!(t = name_change_suffix(name_id(n), ".service"))) - return -ENOMEM; - - r = manager_load_name(n->meta.manager, t, (Name**) &s->service); - free(t); - - if (r < 0) - return r; - - if ((r = set_ensure_allocated(n->meta.dependencies + NAME_BEFORE, trivial_hash_func, trivial_compare_func)) < 0) - return r; - - if ((r = set_put(n->meta.dependencies[NAME_BEFORE], s->service)) < 0) - return r; - - return 0; -} - -static void socket_done(Name *n) { - Socket *s = SOCKET(n); +static void socket_done(Unit *u) { + Socket *s = SOCKET(u); SocketPort *p; assert(s); @@ -81,13 +47,52 @@ static void socket_done(Name *n) { s->control_command = NULL; if (s->control_pid > 0) { - name_unwatch_pid(n, s->control_pid); + unit_unwatch_pid(u, s->control_pid); s->control_pid = 0; } s->service = NULL; - name_unwatch_timer(n, &s->timer_id); + unit_unwatch_timer(u, &s->timer_id); +} + +static int socket_init(Unit *u) { + Socket *s = SOCKET(u); + char *t; + int r; + + /* First, reset everything to the defaults, in case this is a + * reload */ + + s->state = 0; + s->timer_id = -1; + s->bind_ipv6_only = false; + s->backlog = SOMAXCONN; + s->timeout_usec = DEFAULT_TIMEOUT_USEC; + exec_context_init(&s->exec_context); + + if ((r = unit_load_fragment_and_dropin(u)) < 0) + goto fail; + + if (!(t = unit_name_change_suffix(unit_id(u), ".service"))) { + r = -ENOMEM; + goto fail; + } + + r = manager_load_unit(u->meta.manager, t, (Unit**) &s->service); + free(t); + + if (r < 0) + goto fail; + + if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0) + goto fail; + + return 0; + +fail: + socket_done(u); + return r; } static const char* listen_lookup(int type) { @@ -103,7 +108,7 @@ static const char* listen_lookup(int type) { return NULL; } -static void socket_dump(Name *n, FILE *f, const char *prefix) { +static void socket_dump(Unit *u, FILE *f, const char *prefix) { static const char* const state_table[_SOCKET_STATE_MAX] = { [SOCKET_DEAD] = "dead", @@ -128,11 +133,16 @@ static void socket_dump(Name *n, FILE *f, const char *prefix) { }; SocketExecCommand c; - Socket *s = SOCKET(n); + Socket *s = SOCKET(u); SocketPort *p; + char *prefix2; assert(s); + prefix2 = strappend(prefix, "\t"); + if (!prefix2) + prefix2 = ""; + fprintf(f, "%sSocket State: %s\n" "%sBindIPv6Only: %s\n" @@ -162,11 +172,16 @@ static void socket_dump(Name *n, FILE *f, const char *prefix) { exec_context_dump(&s->exec_context, f, prefix); for (c = 0; c < _SOCKET_EXEC_MAX; c++) { - ExecCommand *i; + if (!s->exec_command[c]) + continue; - LIST_FOREACH(command, i, s->exec_command[c]) - fprintf(f, "%s%s: %s\n", prefix, command_table[c], i->path); + fprintf(f, "%s→ %s:\n", + prefix, command_table[c]); + + exec_command_dump_list(s->exec_command[c], f, prefix2); } + + free(prefix2); } static void socket_close_fds(Socket *s) { @@ -178,7 +193,7 @@ static void socket_close_fds(Socket *s) { if (p->fd < 0) continue; - name_unwatch_fd(NAME(s), p->fd); + unit_unwatch_fd(UNIT(s), p->fd); assert_se(close_nointr(p->fd) >= 0); p->fd = -1; @@ -245,7 +260,7 @@ static void socket_unwatch_fds(Socket *s) { if (p->fd < 0) continue; - name_unwatch_fd(NAME(s), p->fd); + unit_unwatch_fd(UNIT(s), p->fd); } } @@ -259,7 +274,7 @@ static int socket_watch_fds(Socket *s) { if (p->fd < 0) continue; - if ((r = name_watch_fd(NAME(s), p->fd, POLLIN)) < 0) + if ((r = unit_watch_fd(UNIT(s), p->fd, POLLIN)) < 0) goto fail; } @@ -285,7 +300,7 @@ static void socket_set_state(Socket *s, SocketState state) { state != SOCKET_STOP_POST && state != SOCKET_STOP_POST_SIGTERM && state != SOCKET_STOP_POST_SIGKILL) - name_unwatch_timer(NAME(s), &s->timer_id); + unit_unwatch_timer(UNIT(s), &s->timer_id); if (state != SOCKET_START_PRE && state != SOCKET_START_POST && @@ -296,7 +311,7 @@ static void socket_set_state(Socket *s, SocketState state) { state != SOCKET_STOP_POST_SIGTERM && state != SOCKET_STOP_POST_SIGKILL) if (s->control_pid >= 0) { - name_unwatch_pid(NAME(s), s->control_pid); + unit_unwatch_pid(UNIT(s), s->control_pid); s->control_pid = 0; } @@ -317,7 +332,7 @@ static void socket_set_state(Socket *s, SocketState state) { if (state != SOCKET_LISTENING) socket_unwatch_fds(s); - name_notify(NAME(s), state_table[old_state], state_table[s->state]); + unit_notify(UNIT(s), state_table[old_state], state_table[s->state]); } static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) { @@ -329,15 +344,15 @@ static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) { assert(_pid); if (timeout) { - if ((r = name_watch_timer(NAME(s), s->timeout_usec, &s->timer_id)) < 0) + if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_id)) < 0) goto fail; } else - name_unwatch_timer(NAME(s), &s->timer_id); + unit_unwatch_timer(UNIT(s), &s->timer_id); if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0) goto fail; - if ((r = name_watch_pid(NAME(s), pid)) < 0) + if ((r = unit_watch_pid(UNIT(s), pid)) < 0) /* FIXME: we need to do something here */ goto fail; @@ -347,7 +362,7 @@ static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) { fail: if (timeout) - name_unwatch_timer(NAME(s), &s->timer_id); + unit_unwatch_timer(UNIT(s), &s->timer_id); return r; } @@ -380,7 +395,7 @@ static void socket_enter_stop_post(Socket *s, bool success) { return; fail: - log_warning("%s failed to run stop-post executable: %s", name_id(NAME(s)), strerror(-r)); + log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r)); socket_enter_dead(s, false); } @@ -409,7 +424,7 @@ static void socket_enter_signal(Socket *s, SocketState state, bool success) { return; fail: - log_warning("%s failed to kill processes: %s", name_id(NAME(s)), strerror(-r)); + log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r)); if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL) socket_enter_stop_post(s, false); @@ -436,7 +451,7 @@ static void socket_enter_stop_pre(Socket *s, bool success) { return; fail: - log_warning("%s failed to run stop-pre executable: %s", name_id(NAME(s)), strerror(-r)); + log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s)), strerror(-r)); socket_enter_stop_post(s, false); } @@ -446,14 +461,14 @@ static void socket_enter_start_post(Socket *s) { if ((r = socket_open_fds(s)) < 0 || (r = socket_watch_fds(s)) < 0) { - log_warning("%s failed to listen on sockets: %s", name_id(NAME(s)), strerror(-r)); + log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s)), strerror(-r)); goto fail; } if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) { - log_warning("%s failed to run start-post executable: %s", name_id(NAME(s)), strerror(-r)); + log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r)); goto fail; } @@ -483,7 +498,7 @@ static void socket_enter_start_pre(Socket *s) { return; fail: - log_warning("%s failed to run start-pre exectuable: %s", name_id(NAME(s)), strerror(-r)); + log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s)), strerror(-r)); socket_enter_dead(s, false); } @@ -492,14 +507,14 @@ static void socket_enter_running(Socket *s) { assert(s); - if ((r = manager_add_job(NAME(s)->meta.manager, JOB_START, NAME(s->service), JOB_REPLACE, true, NULL)) < 0) + if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, NULL)) < 0) goto fail; socket_set_state(s, SOCKET_RUNNING); return; fail: - log_warning("%s failed to queue socket startup job: %s", name_id(NAME(s)), strerror(-r)); + log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s)), strerror(-r)); socket_enter_dead(s, false); } @@ -529,8 +544,8 @@ fail: socket_enter_stop_pre(s, false); } -static int socket_start(Name *n) { - Socket *s = SOCKET(n); +static int socket_start(Unit *u) { + Socket *s = SOCKET(u); assert(s); @@ -549,7 +564,7 @@ static int socket_start(Name *n) { return 0; /* Cannot run this without the service being around */ - if (s->service->meta.load_state != NAME_LOADED) + if (s->service->meta.load_state != UNIT_LOADED) return -ENOENT; assert(s->state == SOCKET_DEAD || s->state == SOCKET_MAINTAINANCE); @@ -559,8 +574,8 @@ static int socket_start(Name *n) { return 0; } -static int socket_stop(Name *n) { - Socket *s = SOCKET(n); +static int socket_stop(Unit *u) { + Socket *s = SOCKET(u); assert(s); @@ -576,18 +591,18 @@ static int socket_stop(Name *n) { return 0; } -static NameActiveState socket_active_state(Name *n) { - assert(n); +static UnitActiveState socket_active_state(Unit *u) { + assert(u); - return state_table[SOCKET(n)->state]; + return state_table[SOCKET(u)->state]; } -static void socket_fd_event(Name *n, int fd, uint32_t events) { - Socket *s = SOCKET(n); +static void socket_fd_event(Unit *u, int fd, uint32_t events) { + Socket *s = SOCKET(u); assert(s); - log_info("Incoming traffic on %s", name_id(n)); + log_info("Incoming traffic on %s", unit_id(u)); if (events != POLLIN) socket_enter_stop_pre(s, false); @@ -595,8 +610,8 @@ static void socket_fd_event(Name *n, int fd, uint32_t events) { socket_enter_running(s); } -static void socket_sigchld_event(Name *n, pid_t pid, int code, int status) { - Socket *s = SOCKET(n); +static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { + Socket *s = SOCKET(u); bool success; assert(s); @@ -611,7 +626,7 @@ static void socket_sigchld_event(Name *n, pid_t pid, int code, int status) { exec_status_fill(&s->control_command->exec_status, pid, code, status); s->control_pid = 0; - log_debug("%s: control process exited, code=%s status=%i", name_id(n), sigchld_code(code), status); + log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status); if (s->control_command->command_next && (success || (s->state == SOCKET_EXEC_STOP_PRE || s->state == SOCKET_EXEC_STOP_POST))) @@ -654,8 +669,8 @@ static void socket_sigchld_event(Name *n, pid_t pid, int code, int status) { } } -static void socket_timer_event(Name *n, int id, uint64_t elapsed) { - Socket *s = SOCKET(n); +static void socket_timer_event(Unit *u, int id, uint64_t elapsed) { + Socket *s = SOCKET(u); assert(s); assert(elapsed == 1); @@ -666,37 +681,37 @@ static void socket_timer_event(Name *n, int id, uint64_t elapsed) { case SOCKET_START_PRE: case SOCKET_START_POST: - log_warning("%s operation timed out. Stopping.", name_id(n)); + log_warning("%s operation timed out. Stopping.", unit_id(u)); socket_enter_stop_pre(s, false); break; case SOCKET_STOP_PRE: - log_warning("%s stopping timed out. Terminating.", name_id(n)); + log_warning("%s stopping timed out. Terminating.", unit_id(u)); socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false); break; case SOCKET_STOP_PRE_SIGTERM: - log_warning("%s stopping timed out. Killing.", name_id(n)); + log_warning("%s stopping timed out. Killing.", unit_id(u)); socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false); break; case SOCKET_STOP_PRE_SIGKILL: - log_warning("%s still around after SIGKILL. Ignoring.", name_id(n)); + log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u)); socket_enter_stop_post(s, false); break; case SOCKET_STOP_POST: - log_warning("%s stopping timed out (2). Terminating.", name_id(n)); + log_warning("%s stopping timed out (2). Terminating.", unit_id(u)); socket_enter_signal(s, SOCKET_STOP_POST_SIGTERM, false); break; case SOCKET_STOP_POST_SIGTERM: - log_warning("%s stopping timed out (2). Killing.", name_id(n)); + log_warning("%s stopping timed out (2). Killing.", unit_id(u)); socket_enter_signal(s, SOCKET_STOP_POST_SIGKILL, false); break; case SOCKET_STOP_POST_SIGKILL: - log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", name_id(n)); + log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u)); socket_enter_dead(s, false); break; @@ -705,7 +720,39 @@ static void socket_timer_event(Name *n, int id, uint64_t elapsed) { } } -const NameVTable socket_vtable = { +int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { + int *rfds; + unsigned rn_fds, k; + SocketPort *p; + + assert(s); + assert(fds); + assert(n_fds); + + /* Called from the service code for requesting our fds */ + + rn_fds = 0; + LIST_FOREACH(port, p, s->ports) + if (p->fd >= 0) + rn_fds++; + + if (!(rfds = new(int, rn_fds)) < 0) + return -ENOMEM; + + k = 0; + LIST_FOREACH(port, p, s->ports) + if (p->fd >= 0) + rfds[k++] = p->fd; + + assert(k == rn_fds); + + *fds = rfds; + *n_fds = rn_fds; + + return 0; +} + +const UnitVTable socket_vtable = { .suffix = ".socket", .init = socket_init,