socket.c revision 8e27452380193a5f81bfd08a59aab8b07008ba0b
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include "unit.h"
#include "socket.h"
#include "log.h"
[SOCKET_DEAD] = UNIT_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_STOP_POST_SIGTERM] = "stop-post-sigterm",
[SOCKET_STOP_POST_SIGKILL] = "stop-post-sigkill",
[SOCKET_MAINTAINANCE] = "maintainance"
};
static void socket_done(Unit *u) {
SocketPort *p;
assert(s);
while ((p = s->ports)) {
if (p->fd >= 0)
close_nointr(p->fd);
free(p);
}
s->control_command = NULL;
if (s->control_pid > 0) {
unit_unwatch_pid(u, s->control_pid);
s->control_pid = 0;
}
free(s->bind_to_device);
unit_unwatch_timer(u, &s->timer_watch);
}
static int socket_init(Unit *u) {
char *t;
int r;
/* First, reset everything to the defaults, in case this is a
* reload */
s->state = 0;
s->bind_ipv6_only = false;
s->directory_mode = 0755;
s->socket_mode = 0666;
if ((r = unit_load_fragment_and_dropin(u)) <= 0) {
if (r == 0)
r = -ENOENT;
goto fail;
}
r = -ENOMEM;
goto fail;
}
free(t);
if (r < 0)
goto fail;
goto fail;
/* Add default cgroup */
if ((r = unit_add_default_cgroup(u)) < 0)
goto fail;
return 0;
fail:
socket_done(u);
return r;
}
static const char* listen_lookup(int type) {
if (type == SOCK_STREAM)
return "ListenStream";
else if (type == SOCK_DGRAM)
return "ListenDatagram";
else if (type == SOCK_SEQPACKET)
return "ListenSequentialPacket";
assert_not_reached("Unknown socket type");
return NULL;
}
static const char* const command_table[_SOCKET_EXEC_MAX] = {
[SOCKET_EXEC_START_PRE] = "StartPre",
[SOCKET_EXEC_START_POST] = "StartPost",
[SOCKET_EXEC_STOP_PRE] = "StopPre",
[SOCKET_EXEC_STOP_POST] = "StopPost"
};
SocketPort *p;
const char *prefix2;
char *p2;
assert(s);
assert(f);
fprintf(f,
"%sSocket State: %s\n"
"%sBindIPv6Only: %s\n"
"%sBacklog: %u\n"
"%sSocketMode: %04o\n"
"%sDirectoryMode: %04o\n",
prefix, s->socket_mode,
prefix, s->directory_mode);
if (s->bind_to_device)
fprintf(f,
"%sBindToDevice: %s\n",
prefix, s->bind_to_device);
if (p->type == SOCKET_SOCKET) {
const char *t;
int r;
char *k;
if ((r = socket_address_print(&p->address, &k)) < 0)
t = strerror(-r);
else
t = k;
free(k);
} else
}
for (c = 0; c < _SOCKET_EXEC_MAX; c++) {
if (!s->exec_command[c])
continue;
fprintf(f, "%sā %s:\n",
prefix, command_table[c]);
}
}
static void socket_close_fds(Socket *s) {
SocketPort *p;
assert(s);
if (p->fd < 0)
continue;
p->fd = -1;
}
}
static int socket_open_fds(Socket *s) {
SocketPort *p;
int r;
assert(s);
if (p->fd >= 0)
continue;
if (p->type == SOCKET_SOCKET) {
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 {
r = -errno;
goto rollback;
}
r = -errno;
goto rollback;
}
r = -errno;
goto rollback;
}
/* FIXME verify user, access mode */
r = -EEXIST;
goto rollback;
}
}
}
return 0;
socket_close_fds(s);
return r;
}
static void socket_unwatch_fds(Socket *s) {
SocketPort *p;
assert(s);
if (p->fd < 0)
continue;
}
}
static int socket_watch_fds(Socket *s) {
SocketPort *p;
int r;
assert(s);
if (p->fd < 0)
continue;
goto fail;
}
return 0;
fail:
return r;
}
assert(s);
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 &&
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 &&
if (s->control_pid > 0) {
s->control_pid = 0;
}
if (state != SOCKET_START_PRE &&
state != SOCKET_START_POST &&
state != SOCKET_STOP_PRE &&
s->control_command = NULL;
if (state != SOCKET_START_POST &&
state != SOCKET_LISTENING &&
state != SOCKET_RUNNING &&
state != SOCKET_STOP_PRE &&
state != SOCKET_STOP_PRE_SIGTERM &&
socket_close_fds(s);
if (state != SOCKET_LISTENING)
log_debug("%s changed %s ā %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
}
int r;
assert(s);
assert(c);
if (timeout) {
goto fail;
} else
if ((r = exec_spawn(c,
&s->exec_context,
NULL, 0,
true,
true,
&pid)) < 0)
goto fail;
/* FIXME: we need to do something here */
goto fail;
return 0;
fail:
if (timeout)
return r;
}
assert(s);
if (!success)
s->failure = true;
}
int r;
assert(s);
if (!success)
s->failure = true;
goto fail;
if (!s->control_command)
socket_enter_dead(s, true);
return;
fail:
socket_enter_dead(s, false);
}
int r;
assert(s);
if (!success)
s->failure = true;
if (s->control_pid > 0) {
int sig;
r = -errno;
goto fail;
}
}
socket_set_state(s, state);
if (s->control_pid <= 0)
socket_enter_dead(s, true);
return;
fail:
socket_enter_stop_post(s, false);
else
socket_enter_dead(s, false);
}
int r;
assert(s);
if (!success)
s->failure = true;
goto fail;
if (!s->control_command)
socket_enter_stop_post(s, true);
return;
fail:
socket_enter_stop_post(s, false);
}
static void socket_enter_listening(Socket *s) {
int r;
assert(s);
if ((r = socket_watch_fds(s)) < 0) {
goto fail;
}
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) {
goto fail;
}
goto fail;
}
if (!s->control_command)
return;
fail:
socket_enter_stop_pre(s, false);
}
static void socket_enter_start_pre(Socket *s) {
int r;
assert(s);
goto fail;
if (!s->control_command)
return;
fail:
socket_enter_dead(s, false);
}
static void socket_enter_running(Socket *s) {
int r;
assert(s);
if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s->service), JOB_REPLACE, true, NULL)) < 0)
goto fail;
return;
fail:
socket_enter_dead(s, false);
}
int r;
assert(s);
assert(s->control_command);
if (!success)
s->failure = true;
goto fail;
return;
fail:
if (s->state == SOCKET_STOP_PRE)
socket_enter_stop_post(s, false);
else if (s->state == SOCKET_STOP_POST)
socket_enter_dead(s, false);
else
socket_enter_stop_pre(s, false);
}
static int socket_start(Unit *u) {
assert(s);
/* We cannot fulfill this request right now, try again later
* please! */
if (s->state == SOCKET_STOP_PRE ||
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)
return -EAGAIN;
if (s->state == SOCKET_START_PRE ||
s->state == SOCKET_START_POST)
return 0;
/* Cannot run this without the service being around */
return -ENOENT;
s->failure = false;
return 0;
}
static int socket_stop(Unit *u) {
assert(s);
/* We cannot fulfill this request right now, try again later
* please! */
if (s->state == SOCKET_START_PRE ||
s->state == SOCKET_START_POST)
return -EAGAIN;
socket_enter_stop_pre(s, true);
return 0;
}
assert(u);
}
assert(s);
socket_enter_stop_pre(s, false);
}
bool success;
assert(s);
assert(s->control_command);
s->control_pid = 0;
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 &&
socket_run_next(s, success);
} else {
/* No further commands for this step, so let's figure
* out what to do next */
switch (s->state) {
case SOCKET_START_PRE:
if (success)
else
socket_enter_stop_pre(s, false);
break;
case SOCKET_START_POST:
if (success)
else
socket_enter_stop_pre(s, false);
break;
case SOCKET_STOP_PRE:
case SOCKET_STOP_PRE_SIGTERM:
case SOCKET_STOP_PRE_SIGKILL:
break;
case SOCKET_STOP_POST:
case SOCKET_STOP_POST_SIGTERM:
case SOCKET_STOP_POST_SIGKILL:
socket_enter_dead(s, success);
break;
default:
assert_not_reached("Uh, control process died at wrong time.");
}
}
}
assert(s);
assert(w == &s->timer_watch);
switch (s->state) {
case SOCKET_START_PRE:
case SOCKET_START_POST:
socket_enter_stop_pre(s, false);
break;
case SOCKET_STOP_PRE:
socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false);
break;
case SOCKET_STOP_PRE_SIGTERM:
socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false);
break;
case SOCKET_STOP_PRE_SIGKILL:
socket_enter_stop_post(s, false);
break;
case SOCKET_STOP_POST:
socket_enter_signal(s, SOCKET_STOP_POST_SIGTERM, false);
break;
case SOCKET_STOP_POST_SIGTERM:
socket_enter_signal(s, SOCKET_STOP_POST_SIGKILL, false);
break;
case SOCKET_STOP_POST_SIGKILL:
socket_enter_dead(s, false);
break;
default:
assert_not_reached("Timeout at wrong time.");
}
}
int *rfds;
unsigned rn_fds, k;
SocketPort *p;
assert(s);
/* Called from the service code for requesting our fds */
rn_fds = 0;
if (p->fd >= 0)
rn_fds++;
return -ENOMEM;
k = 0;
if (p->fd >= 0)
return 0;
}
void socket_notify_service_dead(Socket *s) {
assert(s);
/* The service is dead. Dang. */
if (s->state == SOCKET_RUNNING) {
}
}
const UnitVTable socket_vtable = {
.suffix = ".socket",
.init = socket_init,
.done = socket_done,
.dump = socket_dump,
.start = socket_start,
.stop = socket_stop,
};