X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=socket.c;h=3b74b5e9f82ab15279b7589331d35f44963023ec;hp=23e347d7f4a708a8311dd80302a7103c8ef4902c;hb=40531a555ae313543c0fbb80137edbae71eee773;hpb=44d8db9e5aa86165c97289f6c78a7e42bac78362
diff --git a/socket.c b/socket.c
index 23e347d7f..3b74b5e9f 100644
--- a/socket.c
+++ b/socket.c
@@ -1,34 +1,80 @@
/*-*- 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 "name.h"
+#include "unit.h"
#include "socket.h"
#include "log.h"
+#include "load-dropin.h"
+#include "load-fragment.h"
+
+static const UnitActiveState state_translation_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_FINAL_SIGTERM] = UNIT_DEACTIVATING,
+ [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING,
+ [SOCKET_MAINTAINANCE] = UNIT_INACTIVE,
+};
-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 char* const state_string_table[_SOCKET_STATE_MAX] = {
+ [SOCKET_DEAD] = "dead",
+ [SOCKET_START_PRE] = "start-pre",
+ [SOCKET_START_POST] = "start-post",
+ [SOCKET_LISTENING] = "listening",
+ [SOCKET_RUNNING] = "running",
+ [SOCKET_STOP_PRE] = "stop-pre",
+ [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
+ [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
+ [SOCKET_STOP_POST] = "stop-post",
+ [SOCKET_FINAL_SIGTERM] = "final-sigterm",
+ [SOCKET_FINAL_SIGKILL] = "final-sigkill",
+ [SOCKET_MAINTAINANCE] = "maintainance"
};
-static void socket_done(Name *n) {
- Socket *s = SOCKET(n);
+static void socket_unwatch_control_pid(Socket *s) {
+ assert(s);
+
+ if (s->control_pid <= 0)
+ return;
+
+ unit_unwatch_pid(UNIT(s), s->control_pid);
+ s->control_pid = 0;
+}
+
+static void socket_done(Unit *u) {
+ Socket *s = SOCKET(u);
SocketPort *p;
assert(s);
@@ -43,56 +89,66 @@ static void socket_done(Name *n) {
}
exec_context_done(&s->exec_context);
- exec_command_free_array(s->exec_command, _SOCKET_EXEC_MAX);
+ exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
s->control_command = NULL;
- if (s->control_pid > 0) {
- name_unwatch_pid(n, s->control_pid);
- s->control_pid = 0;
- }
+ socket_unwatch_control_pid(s);
s->service = NULL;
- name_unwatch_timer(n, &s->timer_id);
+ free(s->bind_to_device);
+ s->bind_to_device = NULL;
+
+ unit_unwatch_timer(u, &s->timer_watch);
}
-static int socket_init(Name *n) {
- Socket *s = SOCKET(n);
- char *t;
- int r;
+static void socket_init(Unit *u) {
+ Socket *s = SOCKET(u);
- /* First, reset everything to the defaults, in case this is a
- * reload */
+ assert(u);
+ assert(u->meta.load_state == UNIT_STUB);
s->state = 0;
- s->timer_id = -1;
+ s->timer_watch.type = WATCH_INVALID;
s->bind_ipv6_only = false;
s->backlog = SOMAXCONN;
s->timeout_usec = DEFAULT_TIMEOUT_USEC;
+ s->directory_mode = 0755;
+ s->socket_mode = 0666;
+ s->kill_mode = 0;
+ s->failure = false;
+ s->control_pid = 0;
+ s->service = NULL;
exec_context_init(&s->exec_context);
+}
- if ((r = name_load_fragment_and_dropin(n)) < 0)
- goto fail;
+static int socket_load(Unit *u) {
+ Socket *s = SOCKET(u);
+ int r;
- if (!(t = name_change_suffix(name_id(n), ".service"))) {
- r = -ENOMEM;
- goto fail;
- }
+ assert(u);
+ assert(u->meta.load_state == UNIT_STUB);
- r = manager_load_name(n->meta.manager, t, (Name**) &s->service);
- free(t);
+ if ((r = unit_load_fragment_and_dropin(u)) < 0)
+ return r;
- if (r < 0)
- goto fail;
+ /* This is a new unit? Then let's add in some extras */
+ if (u->meta.load_state == UNIT_LOADED) {
- if ((r = name_add_dependency(n, NAME_BEFORE, NAME(s->service))) < 0)
- goto fail;
+ if ((r = unit_load_related_unit(u, ".service", (Unit**) &s->service)))
+ return r;
- return 0;
+ if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0)
+ return r;
-fail:
- socket_done(n);
- return r;
+ if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
+ return r;
+
+ if ((r = unit_add_default_cgroup(u)) < 0)
+ return r;
+ }
+
+ return 0;
}
static const char* listen_lookup(int type) {
@@ -108,24 +164,9 @@ static const char* listen_lookup(int type) {
return NULL;
}
-static void socket_dump(Name *n, FILE *f, const char *prefix) {
-
- static const char* const state_table[_SOCKET_STATE_MAX] = {
- [SOCKET_DEAD] = "dead",
- [SOCKET_START_PRE] = "start-pre",
- [SOCKET_START_POST] = "start-post",
- [SOCKET_LISTENING] = "listening",
- [SOCKET_RUNNING] = "running",
- [SOCKET_STOP_PRE] = "stop-pre",
- [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
- [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
- [SOCKET_STOP_POST] = "stop-post",
- [SOCKET_STOP_POST_SIGTERM] = "stop-post-sigterm",
- [SOCKET_STOP_POST_SIGKILL] = "stop-post-sigkill",
- [SOCKET_MAINTAINANCE] = "maintainance"
- };
+static void socket_dump(Unit *u, FILE *f, const char *prefix) {
- static const char* const command_table[_SOCKET_EXEC_MAX] = {
+ static const char* const command_table[_SOCKET_EXEC_COMMAND_MAX] = {
[SOCKET_EXEC_START_PRE] = "StartPre",
[SOCKET_EXEC_START_POST] = "StartPost",
[SOCKET_EXEC_STOP_PRE] = "StopPre",
@@ -133,18 +174,40 @@ static void socket_dump(Name *n, FILE *f, const char *prefix) {
};
SocketExecCommand c;
- Socket *s = SOCKET(n);
+ Socket *s = SOCKET(u);
SocketPort *p;
+ const char *prefix2;
+ char *p2;
assert(s);
+ assert(f);
+
+ p2 = strappend(prefix, "\t");
+ prefix2 = p2 ? p2 : prefix;
fprintf(f,
"%sSocket State: %s\n"
"%sBindIPv6Only: %s\n"
- "%sBacklog: %u\n",
- prefix, state_table[s->state],
+ "%sBacklog: %u\n"
+ "%sKillMode: %s\n"
+ "%sSocketMode: %04o\n"
+ "%sDirectoryMode: %04o\n",
+ prefix, state_string_table[s->state],
prefix, yes_no(s->bind_ipv6_only),
- prefix, s->backlog);
+ prefix, s->backlog,
+ prefix, kill_mode_to_string(s->kill_mode),
+ prefix, s->socket_mode,
+ prefix, s->directory_mode);
+
+ if (s->control_pid > 0)
+ fprintf(f,
+ "%sControl PID: %llu\n",
+ prefix, (unsigned long long) s->control_pid);
+
+ if (s->bind_to_device)
+ fprintf(f,
+ "%sBindToDevice: %s\n",
+ prefix, s->bind_to_device);
LIST_FOREACH(port, p, s->ports) {
@@ -166,12 +229,17 @@ 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;
+ for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) {
+ if (!s->exec_command[c])
+ continue;
+
+ fprintf(f, "%sâ %s:\n",
+ prefix, command_table[c]);
- LIST_FOREACH(command, i, s->exec_command[c])
- fprintf(f, "%s%s: %s\n", prefix, command_table[c], i->path);
+ exec_command_dump_list(s->exec_command[c], f, prefix2);
}
+
+ free(p2);
}
static void socket_close_fds(Socket *s) {
@@ -183,7 +251,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_watch);
assert_se(close_nointr(p->fd) >= 0);
p->fd = -1;
@@ -203,14 +271,23 @@ static int socket_open_fds(Socket *s) {
if (p->type == SOCKET_SOCKET) {
- if ((r = socket_address_listen(&p->address, s->backlog, s->bind_ipv6_only, &p->fd)) < 0)
+ if ((r = socket_address_listen(
+ &p->address,
+ s->backlog,
+ s->bind_ipv6_only,
+ s->bind_to_device,
+ s->directory_mode,
+ s->socket_mode,
+ &p->fd)) < 0)
goto rollback;
} else {
struct stat st;
assert(p->type == SOCKET_FIFO);
- if (mkfifo(p->path, 0666 & ~s->exec_context.umask) < 0 && errno != EEXIST) {
+ mkdir_parents(p->path, s->directory_mode);
+
+ if (mkfifo(p->path, s->socket_mode) < 0 && errno != EEXIST) {
r = -errno;
goto rollback;
}
@@ -250,7 +327,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_watch);
}
}
@@ -264,7 +341,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, EPOLLIN, &p->fd_watch)) < 0)
goto fail;
}
@@ -288,28 +365,12 @@ static void socket_set_state(Socket *s, SocketState state) {
state != SOCKET_STOP_PRE_SIGTERM &&
state != SOCKET_STOP_PRE_SIGKILL &&
state != SOCKET_STOP_POST &&
- state != SOCKET_STOP_POST_SIGTERM &&
- state != SOCKET_STOP_POST_SIGKILL)
- name_unwatch_timer(NAME(s), &s->timer_id);
-
- if (state != SOCKET_START_PRE &&
- state != SOCKET_START_POST &&
- state != SOCKET_STOP_PRE &&
- state != SOCKET_STOP_PRE_SIGTERM &&
- state != SOCKET_STOP_PRE_SIGKILL &&
- state != SOCKET_STOP_POST &&
- state != SOCKET_STOP_POST_SIGTERM &&
- state != SOCKET_STOP_POST_SIGKILL)
- if (s->control_pid >= 0) {
- name_unwatch_pid(NAME(s), s->control_pid);
- s->control_pid = 0;
- }
-
- if (state != SOCKET_START_PRE &&
- state != SOCKET_START_POST &&
- state != SOCKET_STOP_PRE &&
- state != SOCKET_STOP_POST)
+ state != SOCKET_FINAL_SIGTERM &&
+ state != SOCKET_FINAL_SIGKILL) {
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
+ socket_unwatch_control_pid(s);
s->control_command = NULL;
+ }
if (state != SOCKET_START_POST &&
state != SOCKET_LISTENING &&
@@ -322,10 +383,13 @@ 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]);
+ if (state != old_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]);
}
-static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
+static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
pid_t pid;
int r;
@@ -333,16 +397,20 @@ static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
assert(c);
assert(_pid);
- if (timeout) {
- if ((r = name_watch_timer(NAME(s), s->timeout_usec, &s->timer_id)) < 0)
- goto fail;
- } else
- name_unwatch_timer(NAME(s), &s->timer_id);
+ if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
+ goto fail;
- if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0)
+ if ((r = exec_spawn(c,
+ &s->exec_context,
+ NULL, 0,
+ true,
+ true,
+ UNIT(s)->meta.manager->confirm_spawn,
+ UNIT(s)->meta.cgroup_bondings,
+ &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;
@@ -351,8 +419,7 @@ static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
return 0;
fail:
- if (timeout)
- name_unwatch_timer(NAME(s), &s->timer_id);
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
return r;
}
@@ -366,6 +433,8 @@ static void socket_enter_dead(Socket *s, bool success) {
socket_set_state(s, s->failure ? SOCKET_MAINTAINANCE : SOCKET_DEAD);
}
+static void socket_enter_signal(Socket *s, SocketState state, bool success);
+
static void socket_enter_stop_post(Socket *s, bool success) {
int r;
assert(s);
@@ -373,48 +442,65 @@ 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])) {
+ socket_unwatch_control_pid(s);
- if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) {
+ if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
goto fail;
socket_set_state(s, SOCKET_STOP_POST);
} else
- socket_enter_dead(s, true);
+ socket_enter_signal(s, SOCKET_FINAL_SIGTERM, true);
return;
fail:
- log_warning("%s failed to run stop-post executable: %s", name_id(NAME(s)), strerror(-r));
- socket_enter_dead(s, false);
+ log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r));
+ socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
}
static void socket_enter_signal(Socket *s, SocketState state, bool success) {
int r;
+ bool sent = false;
assert(s);
if (!success)
s->failure = true;
- if (s->control_pid > 0) {
- int sig;
+ if (s->kill_mode != KILL_NONE) {
+ int sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
- sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_POST_SIGTERM) ? SIGTERM : SIGKILL;
+ if (s->kill_mode == KILL_CONTROL_GROUP) {
- if (kill(s->control_pid, sig) < 0 && errno != ESRCH) {
- r = -errno;
- goto fail;
+ if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
+ if (r != -EAGAIN && r != -ESRCH)
+ goto fail;
+ } else
+ sent = true;
}
+ if (!sent && s->control_pid > 0)
+ if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH) {
+ r = -errno;
+ goto fail;
+ }
+ }
+
+ if (sent) {
+ if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
+ goto fail;
+
socket_set_state(s, state);
- } else
+ } else if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
+ socket_enter_stop_post(s, true);
+ else
socket_enter_dead(s, true);
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);
@@ -429,9 +515,10 @@ 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])) {
+ socket_unwatch_control_pid(s);
- if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) {
+ if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
goto fail;
socket_set_state(s, SOCKET_STOP_PRE);
@@ -441,30 +528,46 @@ 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);
}
+static void socket_enter_listening(Socket *s) {
+ int r;
+ assert(s);
+
+ if ((r = socket_watch_fds(s)) < 0) {
+ log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s)), strerror(-r));
+ goto fail;
+ }
+
+ socket_set_state(s, SOCKET_LISTENING);
+ return;
+
+fail:
+ socket_enter_stop_pre(s, false);
+}
+
static void socket_enter_start_post(Socket *s) {
int r;
assert(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));
+ if ((r = socket_open_fds(s)) < 0) {
+ 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])) {
+ socket_unwatch_control_pid(s);
- 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));
+ 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", unit_id(UNIT(s)), strerror(-r));
goto fail;
}
socket_set_state(s, SOCKET_START_POST);
} else
- socket_set_state(s, SOCKET_LISTENING);
+ socket_enter_listening(s);
return;
@@ -476,9 +579,10 @@ static void socket_enter_start_pre(Socket *s) {
int r;
assert(s);
- if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
+ socket_unwatch_control_pid(s);
- if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
+ if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
goto fail;
socket_set_state(s, SOCKET_START_PRE);
@@ -488,7 +592,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);
}
@@ -497,15 +601,15 @@ 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));
- socket_enter_dead(s, false);
+ log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s)), strerror(-r));
+ socket_enter_stop_pre(s, false);
}
static void socket_run_next(Socket *s, bool success) {
@@ -518,24 +622,28 @@ static void socket_run_next(Socket *s, bool success) {
if (!success)
s->failure = true;
+ socket_unwatch_control_pid(s);
+
s->control_command = s->control_command->command_next;
- if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
goto fail;
return;
fail:
- if (s->state == SOCKET_STOP_PRE)
- socket_enter_stop_post(s, false);
+ log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
+
+ if (s->state == SOCKET_START_POST)
+ socket_enter_stop_pre(s, false);
else if (s->state == SOCKET_STOP_POST)
socket_enter_dead(s, false);
else
- socket_enter_stop_pre(s, false);
+ socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
}
-static int socket_start(Name *n) {
- Socket *s = SOCKET(n);
+static int socket_start(Unit *u) {
+ Socket *s = SOCKET(u);
assert(s);
@@ -545,8 +653,8 @@ static int socket_start(Name *n) {
s->state == SOCKET_STOP_PRE_SIGKILL ||
s->state == SOCKET_STOP_PRE_SIGTERM ||
s->state == SOCKET_STOP_POST ||
- s->state == SOCKET_STOP_POST_SIGTERM ||
- s->state == SOCKET_STOP_POST_SIGKILL)
+ s->state == SOCKET_FINAL_SIGTERM ||
+ s->state == SOCKET_FINAL_SIGKILL)
return -EAGAIN;
if (s->state == SOCKET_START_PRE ||
@@ -554,9 +662,16 @@ 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;
+ /* If the service is alredy actvie we cannot start the
+ * socket */
+ if (s->service->state != SERVICE_DEAD &&
+ s->service->state != SERVICE_MAINTAINANCE &&
+ s->service->state != SERVICE_AUTO_RESTART)
+ return -EBUSY;
+
assert(s->state == SOCKET_DEAD || s->state == SOCKET_MAINTAINANCE);
s->failure = false;
@@ -564,8 +679,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);
@@ -575,39 +690,48 @@ static int socket_stop(Name *n) {
s->state == SOCKET_START_POST)
return -EAGAIN;
+ /* Already on it */
+ if (s->state == SOCKET_STOP_PRE ||
+ s->state == SOCKET_STOP_PRE_SIGTERM ||
+ s->state == SOCKET_STOP_PRE_SIGKILL ||
+ s->state == SOCKET_STOP_POST ||
+ s->state == SOCKET_FINAL_SIGTERM ||
+ s->state == SOCKET_FINAL_SIGTERM)
+ return 0;
+
assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
socket_enter_stop_pre(s, true);
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_translation_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, Watch *w) {
+ Socket *s = SOCKET(u);
assert(s);
- log_info("Incoming traffic on %s", name_id(n));
+ 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);
}
-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);
assert(pid >= 0);
- success = code == CLD_EXITED || status == 0;
+ success = code == CLD_EXITED && status == 0;
s->failure = s->failure || !success;
assert(s->control_pid == pid);
@@ -616,27 +740,29 @@ 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_to_string(code), status);
- if (s->control_command->command_next &&
- (success || (s->state == SOCKET_EXEC_STOP_PRE || s->state == SOCKET_EXEC_STOP_POST)))
+ if (s->control_command->command_next && success) {
+ log_debug("%s running next command for state %s", unit_id(u), state_string_table[s->state]);
socket_run_next(s, success);
- else {
+ } else {
/* No further commands for this step, so let's figure
* out what to do next */
+ log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
+
switch (s->state) {
case SOCKET_START_PRE:
if (success)
- socket_enter_start_pre(s);
+ socket_enter_start_post(s);
else
- socket_enter_stop_pre(s, false);
+ socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
break;
case SOCKET_START_POST:
if (success)
- socket_set_state(s, SOCKET_LISTENING);
+ socket_enter_listening(s);
else
socket_enter_stop_pre(s, false);
break;
@@ -648,8 +774,8 @@ static void socket_sigchld_event(Name *n, pid_t pid, int code, int status) {
break;
case SOCKET_STOP_POST:
- case SOCKET_STOP_POST_SIGTERM:
- case SOCKET_STOP_POST_SIGKILL:
+ case SOCKET_FINAL_SIGTERM:
+ case SOCKET_FINAL_SIGKILL:
socket_enter_dead(s, success);
break;
@@ -659,49 +785,51 @@ 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, uint64_t elapsed, Watch *w) {
+ Socket *s = SOCKET(u);
assert(s);
assert(elapsed == 1);
-
- assert(s->timer_id == id);
+ assert(w == &s->timer_watch);
switch (s->state) {
case SOCKET_START_PRE:
+ log_warning("%s starting timed out. Terminating.", unit_id(u));
+ socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
+
case SOCKET_START_POST:
- log_warning("%s operation timed out. Stopping.", name_id(n));
+ log_warning("%s starting 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));
- socket_enter_signal(s, SOCKET_STOP_POST_SIGTERM, false);
+ log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
+ socket_enter_signal(s, SOCKET_FINAL_SIGTERM, false);
break;
- case SOCKET_STOP_POST_SIGTERM:
- log_warning("%s stopping timed out (2). Killing.", name_id(n));
- socket_enter_signal(s, SOCKET_STOP_POST_SIGKILL, false);
+ case SOCKET_FINAL_SIGTERM:
+ log_warning("%s stopping timed out (2). Killing.", unit_id(u));
+ socket_enter_signal(s, SOCKET_FINAL_SIGKILL, false);
break;
- case SOCKET_STOP_POST_SIGKILL:
- log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", name_id(n));
+ case SOCKET_FINAL_SIGKILL:
+ log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
socket_enter_dead(s, false);
break;
@@ -742,10 +870,22 @@ int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) {
return 0;
}
-const NameVTable socket_vtable = {
+void socket_notify_service_dead(Socket *s) {
+ assert(s);
+
+ /* The service is dead. Dang. */
+
+ if (s->state == SOCKET_RUNNING) {
+ log_debug("%s got notified about service death.", unit_id(UNIT(s)));
+ socket_enter_listening(s);
+ }
+}
+
+const UnitVTable socket_vtable = {
.suffix = ".socket",
.init = socket_init,
+ .load = socket_load,
.done = socket_done,
.dump = socket_dump,