#include "strv.h"
#include "macro.h"
#include "util.h"
+#include "log.h"
static int close_fds(int except[], unsigned n_except) {
DIR *d;
assert(ret);
assert(fds || n_fds <= 0);
+ log_debug("About to execute %s", command->path);
+
if ((pid = fork()) < 0)
return -errno;
- verify fragment data after loading: refuse cycles on yourself, service units contradicting, more than one Start executable, ...
- rate limit startups
+
+- automatically delete stale unix sockets
return 0;
}
+int config_parse_bindtodevice(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ Socket *s = data;
+ char *n;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ if (rvalue[0] && !streq(rvalue, "*")) {
+ if (!(n = strdup(rvalue)))
+ return -ENOMEM;
+ } else
+ n = NULL;
+
+ free(s->bind_to_device);
+ s->bind_to_device = n;
+
+ return 0;
+}
+
#define FOLLOW_MAX 8
static char *build_path(const char *path, const char *filename) {
{ "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
{ "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
{ "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
+ { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
{ "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
{ "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
{ "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
printf("→ By jobs:\n");
manager_dump_jobs(m, stdout, "\t");
- /* manager_loop(m); */
+ if ((r = manager_loop(m)) < 0) {
+ log_error("Failed to run mainloop: %s", strerror(-r));
+ goto finish;
+ }
retval = 0;
if (!(m = new0(Manager, 1)))
return NULL;
- m->signal_fd = m->epoll_fd = -1;
+ m->signal_watch.fd = m->epoll_fd = -1;
if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
goto fail;
assert_se(sigaddset(&mask, SIGCHLD) == 0);
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
- if ((m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
+ m->signal_watch.type = WATCH_SIGNAL_FD;
+ if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
goto fail;
zero(ev);
ev.events = EPOLLIN;
- ev.data.fd = m->signal_fd;
+ ev.data.ptr = &m->signal_watch;
- if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_fd, &ev) < 0)
+ if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
goto fail;
return m;
if (m->epoll_fd >= 0)
close_nointr(m->epoll_fd);
- if (m->signal_fd >= 0)
- close_nointr(m->signal_fd);
+ if (m->signal_watch.fd >= 0)
+ close_nointr(m->signal_watch.fd);
free(m);
}
assert(!j->transaction_next);
assert(!j->transaction_prev);
+ job_schedule_run(j);
}
/* As last step, kill all remaining job dependencies. */
static int manager_dispatch_sigchld(Manager *m) {
assert(m);
+ log_debug("dispatching SIGCHLD");
+
for (;;) {
siginfo_t si;
Unit *u;
zero(si);
- if (waitid(P_ALL, 0, &si, WNOHANG) < 0)
+ if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG) < 0) {
+
+ if (errno == ECHILD)
+ break;
+
return -errno;
+ }
if (si.si_pid == 0)
break;
if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
continue;
+ log_debug("child %llu died (code=%s, status=%i)", (long long unsigned) si.si_pid, sigchld_code(si.si_code), si.si_status);
+
if (!(u = hashmap_remove(m->watch_pids, UINT32_TO_PTR(si.si_pid))))
continue;
assert(m);
for (;;) {
- if ((n = read(m->signal_fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
+ if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
if (n >= 0)
return -EIO;
if (errno == EAGAIN)
- return 0;
+ break;
return -errno;
}
static int process_event(Manager *m, struct epoll_event *ev) {
int r;
+ Watch *w;
assert(m);
assert(ev);
- switch (ev->data.u32) {
+ assert(w = ev->data.ptr);
- case MANAGER_SIGNAL:
- assert(ev->data.fd == m->signal_fd);
+ switch (w->type) {
- /* An incoming signal? */
- if (ev->events != POLLIN)
- return -EINVAL;
+ case WATCH_SIGNAL_FD:
- if ((r = manager_process_signal_fd(m)) < 0)
- return -r;
+ /* An incoming signal? */
+ if (ev->events != POLLIN)
+ return -EINVAL;
- break;
+ if ((r = manager_process_signal_fd(m)) < 0)
+ return r;
- case MANAGER_FD: {
- Unit *u;
+ break;
- /* Some fd event, to be dispatched to the units */
- assert_se(u = ev->data.ptr);
- UNIT_VTABLE(u)->fd_event(u, ev->data.fd, ev->events);
- break;
- }
+ case WATCH_FD:
- case MANAGER_TIMER: {
- Unit *u;
- uint64_t v;
- ssize_t k;
+ /* Some fd event, to be dispatched to the units */
+ UNIT_VTABLE(w->unit)->fd_event(w->unit, w->fd, ev->events, w);
+ break;
- /* Some timer event, to be dispatched to the units */
- if ((k = read(ev->data.fd, &v, sizeof(v))) != sizeof(v)) {
+ case WATCH_TIMER: {
+ uint64_t v;
+ ssize_t k;
- if (k < 0 && (errno == EINTR || errno == EAGAIN))
- break;
+ /* Some timer event, to be dispatched to the units */
+ if ((k = read(ev->data.fd, &v, sizeof(v))) != sizeof(v)) {
- return k < 0 ? -errno : -EIO;
- }
+ if (k < 0 && (errno == EINTR || errno == EAGAIN))
+ break;
- assert_se(u = ev->data.ptr);
- UNIT_VTABLE(u)->timer_event(u, ev->data.fd, v);
- break;
+ return k < 0 ? -errno : -EIO;
}
- default:
- assert_not_reached("Unknown epoll event type.");
+ UNIT_VTABLE(w->unit)->timer_event(w->unit, v, w);
+ break;
+ }
+
+ default:
+ assert_not_reached("Unknown epoll event type.");
}
return 0;
#include <stdio.h>
typedef struct Manager Manager;
-typedef enum ManagerEventType ManagerEventType;
+typedef enum WatchType WatchType;
+typedef struct Watch Watch;
+
+enum WatchType {
+ WATCH_INVALID,
+ WATCH_SIGNAL_FD,
+ WATCH_FD,
+ WATCH_TIMER
+};
+
+struct Watch {
+ int fd;
+ WatchType type;
+ union Unit *unit;
+};
#include "unit.h"
#include "job.h"
#include "list.h"
#include "set.h"
-enum ManagerEventType {
- MANAGER_SIGNAL,
- MANAGER_FD,
- MANAGER_TIMER
-};
-
struct Manager {
uint32_t current_job_id;
Hashmap *watch_pids; /* pid => Unit object n:1 */
int epoll_fd;
- int signal_fd;
+
+ Watch signal_watch;
};
Manager* manager_new(void);
#include "load-dropin.h"
#include "log.h"
-static const UnitActiveState state_table[_SERVICE_STATE_MAX] = {
+static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
[SERVICE_DEAD] = UNIT_INACTIVE,
[SERVICE_START_PRE] = UNIT_ACTIVATING,
[SERVICE_START] = UNIT_ACTIVATING,
[SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
};
+static const char* const state_string_table[_SERVICE_STATE_MAX] = {
+ [SERVICE_DEAD] = "dead",
+ [SERVICE_START_PRE] = "start-pre",
+ [SERVICE_START] = "start",
+ [SERVICE_START_POST] = "post",
+ [SERVICE_RUNNING] = "running",
+ [SERVICE_RELOAD] = "reload",
+ [SERVICE_STOP] = "stop",
+ [SERVICE_STOP_SIGTERM] = "stop-sigterm",
+ [SERVICE_STOP_SIGKILL] = "stop-sigkill",
+ [SERVICE_STOP_POST] = "stop-post",
+ [SERVICE_FINAL_SIGTERM] = "final-sigterm",
+ [SERVICE_FINAL_SIGKILL] = "final-sigkill",
+ [SERVICE_MAINTAINANCE] = "maintainance",
+ [SERVICE_AUTO_RESTART] = "auto-restart",
+};
+
static void service_done(Unit *u) {
Service *s = SERVICE(u);
s->control_pid = 0;
}
- unit_unwatch_timer(u, &s->timer_id);
+ unit_unwatch_timer(u, &s->timer_watch);
}
static int service_load_sysv(Service *s) {
exec_context_init(&s->exec_context);
- s->timer_id = -1;
+ s->timer_watch.type = WATCH_INVALID;
s->state = SERVICE_DEAD;
static void service_dump(Unit *u, FILE *f, const char *prefix) {
- static const char* const state_table[_SERVICE_STATE_MAX] = {
- [SERVICE_DEAD] = "dead",
- [SERVICE_START_PRE] = "start-pre",
- [SERVICE_START] = "start",
- [SERVICE_START_POST] = "post",
- [SERVICE_RUNNING] = "running",
- [SERVICE_RELOAD] = "reload",
- [SERVICE_STOP] = "stop",
- [SERVICE_STOP_SIGTERM] = "stop-sigterm",
- [SERVICE_STOP_SIGKILL] = "stop-sigkill",
- [SERVICE_STOP_POST] = "stop-post",
- [SERVICE_FINAL_SIGTERM] = "final-sigterm",
- [SERVICE_FINAL_SIGKILL] = "final-sigkill",
- [SERVICE_MAINTAINANCE] = "maintainance",
- [SERVICE_AUTO_RESTART] = "auto-restart",
- };
-
static const char* const command_table[_SERVICE_EXEC_MAX] = {
[SERVICE_EXEC_START_PRE] = "ExecStartPre",
[SERVICE_EXEC_START] = "ExecStart",
fprintf(f,
"%sService State: %s\n",
- prefix, state_table[s->state]);
+ prefix, state_string_table[s->state]);
if (s->pid_file)
fprintf(f,
state != SERVICE_FINAL_SIGTERM &&
state != SERVICE_FINAL_SIGKILL &&
state != SERVICE_AUTO_RESTART)
- unit_unwatch_timer(UNIT(s), &s->timer_id);
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
if (state != SERVICE_START_POST &&
state != SERVICE_RUNNING &&
state != SERVICE_STOP &&
state != SERVICE_STOP_SIGTERM &&
state != SERVICE_STOP_SIGKILL)
- if (s->main_pid >= 0) {
+ if (s->main_pid > 0) {
unit_unwatch_pid(UNIT(s), s->main_pid);
s->main_pid = 0;
}
state != SERVICE_STOP_POST &&
state != SERVICE_FINAL_SIGTERM &&
state != SERVICE_FINAL_SIGKILL)
- if (s->control_pid >= 0) {
+ if (s->control_pid > 0) {
unit_unwatch_pid(UNIT(s), s->control_pid);
s->control_pid = 0;
}
state != SERVICE_STOP_POST)
s->control_command = NULL;
- unit_notify(UNIT(s), state_table[old_state], state_table[s->state]);
+ log_debug("%s changing %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 service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
goto fail;
if (timeout) {
- if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_id)) < 0)
+ if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
goto fail;
} else
- unit_unwatch_timer(UNIT(s), &s->timer_id);
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
goto fail;
free(fds);
if (timeout)
- unit_unwatch_timer(UNIT(s), &s->timer_id);
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
return r;
}
(s->restart == SERVICE_RESTART_ALWAYS ||
(s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
- if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_id)) < 0)
+ if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
goto fail;
service_set_state(s, SERVICE_AUTO_RESTART);
static UnitActiveState service_active_state(Unit *u) {
assert(u);
- return state_table[SERVICE(u)->state];
+ return state_translation_table[SERVICE(u)->state];
}
static int main_pid_good(Service *s) {
assert_not_reached("Got SIGCHLD for unkown PID");
}
-static void service_timer_event(Unit *u, int id, uint64_t elapsed) {
+static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
Service *s = SERVICE(u);
assert(s);
assert(elapsed == 1);
- assert(s->timer_id == id);
+ assert(w == &s->timer_watch);
switch (s->state) {
bool main_pid_known:1;
bool failure:1; /* if we shut down, remember why */
- int timer_id;
+ Watch timer_watch;
};
const UnitVTable service_vtable;
} else {
unsigned idx;
+ if (strlen(n) > IF_NAMESIZE-1) {
+ free(n);
+ return -EINVAL;
+ }
+
/* Uh, our last resort, an interface name */
idx = if_nametoindex(n);
free(n);
a->sockaddr.in6.sin6_scope_id = idx;
a->sockaddr.in6.sin6_addr = in6addr_any;
a->size = sizeof(struct sockaddr_in6);
+
}
} else {
}
}
-int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, int *ret) {
- int r, fd;
+int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, int *ret) {
+ int r, fd, one;
assert(a);
assert(ret);
if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
- if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0) {
- close_nointr(fd);
- return -errno;
- }
+ if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
+ goto fail;
}
- if (bind(fd, &a->sockaddr.sa, a->size) < 0) {
- close_nointr(fd);
- return -errno;
- }
+ if (bind_to_device)
+ if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
+ goto fail;
+
+ one = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
+ goto fail;
+
+ if (bind(fd, &a->sockaddr.sa, a->size) < 0)
+ goto fail;
if (a->type == SOCK_STREAM)
- if (listen(fd, backlog) < 0) {
- close_nointr(fd);
- return -errno;
- }
+ if (listen(fd, backlog) < 0)
+ goto fail;
*ret = fd;
return 0;
+
+fail:
+ r = -errno;
+ close_nointr(fd);
+ return r;
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
+#include <net/if.h>
#include "macro.h"
#include "util.h"
int socket_address_parse(SocketAddress *a, const char *s);
int socket_address_print(const SocketAddress *a, char **p);
int socket_address_verify(const SocketAddress *a);
-int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, int *ret);
+int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, int *ret);
#endif
#include "socket.h"
#include "log.h"
-static const UnitActiveState state_table[_SOCKET_STATE_MAX] = {
+static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
[SOCKET_DEAD] = UNIT_INACTIVE,
[SOCKET_START_PRE] = UNIT_ACTIVATING,
[SOCKET_START_POST] = UNIT_ACTIVATING,
[SOCKET_MAINTAINANCE] = 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) {
Socket *s = SOCKET(u);
SocketPort *p;
s->service = NULL;
- unit_unwatch_timer(u, &s->timer_id);
+ free(s->bind_to_device);
+
+ unit_unwatch_timer(u, &s->timer_watch);
}
static int socket_init(Unit *u) {
* reload */
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;
static void socket_dump(Unit *u, 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 const char* const command_table[_SOCKET_EXEC_MAX] = {
[SOCKET_EXEC_START_PRE] = "StartPre",
[SOCKET_EXEC_START_POST] = "StartPost",
"%sSocket State: %s\n"
"%sBindIPv6Only: %s\n"
"%sBacklog: %u\n",
- prefix, state_table[s->state],
+ prefix, state_string_table[s->state],
prefix, yes_no(s->bind_ipv6_only),
prefix, s->backlog);
+ if (s->bind_to_device)
+ fprintf(f,
+ "%sBindToDevice: %s\n",
+ prefix, s->bind_to_device);
+
LIST_FOREACH(port, p, s->ports) {
if (p->type == SOCKET_SOCKET) {
if (p->fd < 0)
continue;
- unit_unwatch_fd(UNIT(s), p->fd);
+ unit_unwatch_fd(UNIT(s), &p->fd_watch);
assert_se(close_nointr(p->fd) >= 0);
p->fd = -1;
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, &p->fd)) < 0)
goto rollback;
} else {
if (p->fd < 0)
continue;
- unit_unwatch_fd(UNIT(s), p->fd);
+ unit_unwatch_fd(UNIT(s), &p->fd_watch);
}
}
if (p->fd < 0)
continue;
- if ((r = unit_watch_fd(UNIT(s), p->fd, POLLIN)) < 0)
+ if ((r = unit_watch_fd(UNIT(s), p->fd, POLLIN, &p->fd_watch)) < 0)
goto fail;
}
state != SOCKET_STOP_POST &&
state != SOCKET_STOP_POST_SIGTERM &&
state != SOCKET_STOP_POST_SIGKILL)
- unit_unwatch_timer(UNIT(s), &s->timer_id);
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
if (state != SOCKET_START_PRE &&
state != SOCKET_START_POST &&
state != SOCKET_STOP_POST &&
state != SOCKET_STOP_POST_SIGTERM &&
state != SOCKET_STOP_POST_SIGKILL)
- if (s->control_pid >= 0) {
+ if (s->control_pid > 0) {
unit_unwatch_pid(UNIT(s), s->control_pid);
s->control_pid = 0;
}
if (state != SOCKET_LISTENING)
socket_unwatch_fds(s);
- unit_notify(UNIT(s), state_table[old_state], state_table[s->state]);
+ log_debug("%s changing %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) {
assert(_pid);
if (timeout) {
- if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_id)) < 0)
+ if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
goto fail;
} else
- unit_unwatch_timer(UNIT(s), &s->timer_id);
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0)
goto fail;
fail:
if (timeout)
- unit_unwatch_timer(UNIT(s), &s->timer_id);
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
return r;
}
static UnitActiveState socket_active_state(Unit *u) {
assert(u);
- return state_table[SOCKET(u)->state];
+ return state_translation_table[SOCKET(u)->state];
}
-static void socket_fd_event(Unit *u, int fd, uint32_t events) {
+static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
Socket *s = SOCKET(u);
assert(s);
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(code), status);
if (s->control_command->command_next &&
- (success || (s->state == SOCKET_EXEC_STOP_PRE || s->state == SOCKET_EXEC_STOP_POST)))
+ (success || (s->state == SOCKET_EXEC_STOP_PRE || s->state == SOCKET_EXEC_STOP_POST))) {
+ log_debug("%s running next command for the 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 finished with 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);
break;
}
}
-static void socket_timer_event(Unit *u, int id, uint64_t elapsed) {
+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) {
typedef struct Socket Socket;
+#include "manager.h"
#include "unit.h"
#include "socket-util.h"
char *path;
int fd;
+ Watch fd_watch;
LIST_FIELDS(SocketPort, port);
};
ExecCommand* control_command;
pid_t control_pid;
+ char *bind_to_device;
+
bool failure;
- int timer_id;
+ Watch timer_watch;
};
/* Called from the service code when collecting fds */
ExecStopPre=/bin/echo "About to delete sockets..."
ExecStopPost=/bin/echo "Deleted sockets..."
ExecStopPost=/bin/echo "Deleted sockets... 2"
+#BindToDevice=eth0
[Socket]
ListenDatagram=/tmp/systemd-syslog-socket
ListenStream=eth0:3456
+ExecStartPre=/bin/rm -f /tmp/systemd-syslog-socket
+ExecStopPost=/bin/rm -f /tmp/systemd-syslog-socket
retroactively_stop_dependencies(u);
}
-int unit_watch_fd(Unit *u, int fd, uint32_t events) {
+int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
struct epoll_event ev;
assert(u);
assert(fd >= 0);
+ assert(w);
+ assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->unit == u));
zero(ev);
- ev.data.fd = fd;
- ev.data.ptr = u;
- ev.data.u32 = MANAGER_FD;
+ ev.data.ptr = w;
ev.events = events;
- if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) >= 0)
- return 0;
+ if (epoll_ctl(u->meta.manager->epoll_fd,
+ w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
+ fd,
+ &ev) < 0)
+ return -errno;
- if (errno == EEXIST)
- if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_MOD, fd, &ev) >= 0)
- return 0;
+ w->fd = fd;
+ w->type = WATCH_FD;
+ w->unit = u;
- return -errno;
+ return 0;
}
-void unit_unwatch_fd(Unit *u, int fd) {
+void unit_unwatch_fd(Unit *u, Watch *w) {
assert(u);
- assert(fd >= 0);
+ assert(w);
- assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, fd, NULL) >= 0 || errno == ENOENT);
+ if (w->type == WATCH_INVALID)
+ return;
+
+ assert(w->type == WATCH_FD && w->unit == u);
+ assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
+
+ w->fd = -1;
+ w->type = WATCH_INVALID;
+ w->unit = NULL;
}
int unit_watch_pid(Unit *u, pid_t pid) {
hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
}
-int unit_watch_timer(Unit *u, usec_t delay, int *id) {
- struct epoll_event ev;
- int fd;
+int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
struct itimerspec its;
- int flags;
+ int flags, fd;
bool ours;
assert(u);
- assert(id);
+ assert(w);
+ assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->unit == u));
/* This will try to reuse the old timer if there is one */
- if (*id >= 0) {
+ if (w->type == WATCH_TIMER) {
ours = false;
- fd = *id;
-
+ fd = w->fd;
} else {
ours = true;
-
if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
return -errno;
}
if (timerfd_settime(fd, flags, &its, NULL) < 0)
goto fail;
- zero(ev);
- ev.data.fd = fd;
- ev.data.ptr = u;
- ev.data.u32 = MANAGER_TIMER;
- ev.events = POLLIN;
+ if (w->type == WATCH_INVALID) {
+ struct epoll_event ev;
- if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
- goto fail;
+ zero(ev);
+ ev.data.ptr = w;
+ ev.events = POLLIN;
+
+ if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
+ goto fail;
+ }
+
+ w->fd = fd;
+ w->type = WATCH_TIMER;
+ w->unit = u;
- *id = fd;
return 0;
fail:
return -errno;
}
-void unit_unwatch_timer(Unit *u, int *id) {
+void unit_unwatch_timer(Unit *u, Watch *w) {
assert(u);
- assert(id);
+ assert(w);
- if (*id < 0)
+ if (w->type == WATCH_INVALID)
return;
- assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, *id, NULL) >= 0);
- assert_se(close_nointr(*id) == 0);
- *id = -1;
+ assert(w->type == WATCH_TIMER && w->unit == u);
+
+ assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
+ assert_se(close_nointr(w->fd) == 0);
+
+ w->fd = -1;
+ w->type = WATCH_INVALID;
+ w->unit = NULL;
}
bool unit_job_is_applicable(Unit *u, JobType j) {
* a simpler one that the engine can understand */
UnitActiveState (*active_state)(Unit *u);
- void (*fd_event)(Unit *u, int fd, uint32_t events);
+ void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
- void (*timer_event)(Unit *u, int id, uint64_t n_elapsed);
+ void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
void (*retry)(Unit *u);
};
void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
-int unit_watch_fd(Unit *u, int fd, uint32_t events);
-void unit_unwatch_fd(Unit *u, int fd);
+int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
+void unit_unwatch_fd(Unit *u, Watch *w);
int unit_watch_pid(Unit *u, pid_t pid);
void unit_unwatch_pid(Unit *u, pid_t pid);
-int unit_watch_timer(Unit *u, usec_t delay, int *id);
-void unit_unwatch_timer(Unit *u, int *id);
+int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
+void unit_unwatch_timer(Unit *u, Watch *w);
bool unit_job_is_applicable(Unit *u, JobType j);