X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=socket.c;h=6b1da016dd4cdb13695f9a914a38ee8b6c04bcb4;hp=68a4d812d6f6956b989a762ca36c9b17071084b5;hb=a9f5d45466c923442ceb31ab2a4206736133d9d4;hpb=fa06836725dd6ec9f8f1c4b76608f7147008ecd9 diff --git a/socket.c b/socket.c index 68a4d812d..6b1da016d 100644 --- a/socket.c +++ b/socket.c @@ -1,11 +1,30 @@ /*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + #include #include #include #include #include -#include +#include #include #include "unit.h" @@ -88,8 +107,11 @@ static int socket_init(Unit *u) { s->timeout_usec = DEFAULT_TIMEOUT_USEC; exec_context_init(&s->exec_context); - if ((r = unit_load_fragment_and_dropin(u)) < 0) + if ((r = unit_load_fragment_and_dropin(u)) <= 0) { + if (r == 0) + r = -ENOENT; goto fail; + } if (!(t = unit_name_change_suffix(unit_id(u), ".service"))) { r = -ENOMEM; @@ -137,14 +159,14 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) { SocketExecCommand c; Socket *s = SOCKET(u); SocketPort *p; - char *prefix2; + const char *prefix2; + char *p2; assert(s); assert(f); - prefix2 = strappend(prefix, "\t"); - if (!prefix2) - prefix2 = ""; + p2 = strappend(prefix, "\t"); + prefix2 = p2 ? p2 : prefix; fprintf(f, "%sSocket State: %s\n" @@ -189,7 +211,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) { exec_command_dump_list(s->exec_command[c], f, prefix2); } - free(prefix2); + free(p2); } static void socket_close_fds(Socket *s) { @@ -282,7 +304,7 @@ static int socket_watch_fds(Socket *s) { if (p->fd < 0) continue; - if ((r = unit_watch_fd(UNIT(s), p->fd, POLLIN, &p->fd_watch)) < 0) + if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0) goto fail; } @@ -340,7 +362,7 @@ static void socket_set_state(Socket *s, SocketState state) { if (state != SOCKET_LISTENING) socket_unwatch_fds(s); - log_debug("%s changing %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]); + log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]); unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); } @@ -393,13 +415,13 @@ static void socket_enter_stop_post(Socket *s, bool success) { if (!success) s->failure = true; - if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) { - + if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) goto fail; - socket_set_state(s, SOCKET_STOP_POST); - } else + socket_set_state(s, SOCKET_STOP_POST); + + if (!s->control_command) socket_enter_dead(s, true); return; @@ -426,9 +448,11 @@ static void socket_enter_signal(Socket *s, SocketState state, bool success) { r = -errno; goto fail; } + } - socket_set_state(s, state); - } else + socket_set_state(s, state); + + if (s->control_pid <= 0) socket_enter_dead(s, true); return; @@ -449,13 +473,13 @@ static void socket_enter_stop_pre(Socket *s, bool success) { if (!success) s->failure = true; - if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) { - + if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) goto fail; - socket_set_state(s, SOCKET_STOP_PRE); - } else + socket_set_state(s, SOCKET_STOP_PRE); + + if (!s->control_command) socket_enter_stop_post(s, true); return; @@ -490,15 +514,15 @@ static void socket_enter_start_post(Socket *s) { goto fail; } - if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) { - + 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", unit_id(UNIT(s)), strerror(-r)); goto fail; } - socket_set_state(s, SOCKET_START_POST); - } else + socket_set_state(s, SOCKET_START_POST); + + if (!s->control_command) socket_enter_listening(s); return; @@ -511,13 +535,13 @@ static void socket_enter_start_pre(Socket *s) { int r; assert(s); - if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) { - + if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) goto fail; - socket_set_state(s, SOCKET_START_PRE); - } else + socket_set_state(s, SOCKET_START_PRE); + + if (!s->control_command) socket_enter_start_post(s); return; @@ -629,7 +653,7 @@ static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { log_debug("Incoming traffic on %s", unit_id(u)); - if (events != POLLIN) + if (events != EPOLLIN) socket_enter_stop_pre(s, false); socket_enter_running(s); @@ -651,7 +675,7 @@ static void socket_sigchld_event(Unit *u, 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", unit_id(u), sigchld_code(code), status); + log_debug("%s control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status); if (s->control_command->command_next && (success || (s->state == SOCKET_EXEC_STOP_PRE || s->state == SOCKET_EXEC_STOP_POST))) {