diff options
author | lns <matzeton@googlemail.com> | 2018-04-26 17:59:09 +0200 |
---|---|---|
committer | lns <matzeton@googlemail.com> | 2018-04-26 17:59:09 +0200 |
commit | a2e40e828261e1d8407c1b5c02f715147e568170 (patch) | |
tree | ae3e703c072ec38c7c6cd2930ac03834ecbe81fc | |
parent | dc8d50bd8d382505ca7a34d501097ce7c181803b (diff) |
POTD skeleton #31.
Signed-off-by: lns <matzeton@googlemail.com>
-rw-r--r-- | src/jail.c | 9 | ||||
-rw-r--r-- | src/jail.h | 2 | ||||
-rw-r--r-- | src/main.c | 10 | ||||
-rw-r--r-- | src/server.c | 38 | ||||
-rw-r--r-- | src/server.h | 2 |
5 files changed, 51 insertions, 10 deletions
@@ -18,7 +18,7 @@ typedef struct jail_prisoner_process { char host_buf[NI_MAXHOST], service_buf[NI_MAXSERV]; } jail_prisoner_process; -static int jail_daemonfn_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz); +static int jail_mainloop_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz); static int jail_accept_client(jail_ctx *ctx[], size_t siz, struct epoll_event *event); static int jail_childfn(void *arg); @@ -115,13 +115,14 @@ int jail_setup_epoll(jail_ctx *ctx[], size_t siz) return fd; } -pid_t jail_daemonize_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz) +pid_t jail_daemonize(int epoll_fd, jail_ctx *ctx[], size_t siz) { pid_t p; int s; size_t i; assert(ctx); + assert(siz > 0); for (i = 0; i < siz; ++i) { assert(ctx[i]); @@ -140,7 +141,7 @@ pid_t jail_daemonize_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz) return -1; case 0: N("%s", "Jail daemon mainloop"); - jail_daemonfn_epoll(epoll_fd, ctx, siz); + jail_mainloop_epoll(epoll_fd, ctx, siz); break; } D2("Jail daemon pid: %d", p); @@ -148,7 +149,7 @@ pid_t jail_daemonize_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz) return p; } -static int jail_daemonfn_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz) +static int jail_mainloop_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz) { static struct epoll_event *events = NULL; sigset_t eset; @@ -27,6 +27,6 @@ int jail_validate_ctx(const jail_ctx *ctx); int jail_setup_epoll(jail_ctx *ctx[], size_t siz); -pid_t jail_daemonize_epoll(int epoll_fd, jail_ctx *ctx[], size_t siz); +pid_t jail_daemonize(int epoll_fd, jail_ctx *ctx[], size_t siz); #endif @@ -1,4 +1,6 @@ #include <stdio.h> +#include <sys/types.h> +#include <sys/wait.h> #include "log.h" #include "log_colored.h" @@ -21,7 +23,7 @@ int main(int argc, char *argv[]) server_ctx *srv[srv_siz]; jail_ctx *jail[jail_siz]; forward_ctx *ssh_fwd = NULL; - int epoll_fd; + int epoll_fd, proc_status; pid_t daemon_pid; (void) argc; @@ -60,7 +62,7 @@ int main(int argc, char *argv[]) epoll_fd = jail_setup_epoll( jail, jail_siz ); D2("Jail epoll fd: %d", epoll_fd); ABORT_ON_FATAL( epoll_fd < 0, "Jail daemon epoll setup" ); - ABORT_ON_FATAL( jail_daemonize_epoll(epoll_fd, jail, jail_siz) < 0, + ABORT_ON_FATAL( jail_daemonize(epoll_fd, jail, jail_siz) < 1, "Jail daemon startup" ); { @@ -97,8 +99,10 @@ int main(int argc, char *argv[]) "Server dropping privileges" ); N("%s", "Server epoll mainloop"); - ABORT_ON_FATAL( server_mainloop_epoll( epoll_fd, srv, srv_siz ), + ABORT_ON_FATAL( server_daemonize( epoll_fd, srv, srv_siz ) < 1, "Server epoll mainloop" ); + wait(&proc_status); + return 0; } diff --git a/src/server.c b/src/server.c index 073229c..6627afa 100644 --- a/src/server.c +++ b/src/server.c @@ -29,6 +29,8 @@ static void * client_mainloop_epoll(void *arg); static connection_state client_io_epoll(struct epoll_event *ev, int dest_fd); +static int +server_mainloop_epoll(int epoll_fd, server_ctx *ctx[], size_t siz); void server_init_ctx(server_ctx **ctx, forward_ctx *fwd_ctx) @@ -112,7 +114,41 @@ int server_setup_epoll(server_ctx *ctx[], size_t siz) return fd; } -int server_mainloop_epoll(int epoll_fd, server_ctx *ctx[], size_t siz) +pid_t server_daemonize(int epoll_fd, server_ctx *ctx[], size_t siz) +{ + pid_t p; + int s; + size_t i; + + assert(ctx); + assert(siz > 0); + + for (i = 0; i < siz; ++i) { + assert(ctx[i]); + s = socket_addrtostr_in(&ctx[i]->sock, + ctx[i]->host_buf, ctx[i]->service_buf); + if (s) { + E_GAIERR(s, "Could not initialise server daemon socket"); + return 1; + } + } + + p = fork(); + switch (p) { + case -1: + W_STRERR("Server daemonsize"); + return -1; + case 0: + N("%s", "Server daemon mainloop"); + server_mainloop_epoll(epoll_fd, ctx, siz); + break; + } + D2("Server daemon pid: %d", p); + + return p; +} + +static int server_mainloop_epoll(int epoll_fd, server_ctx *ctx[], size_t siz) { static struct epoll_event *events = NULL; sigset_t eset; diff --git a/src/server.h b/src/server.h index c1a64e0..65e1797 100644 --- a/src/server.h +++ b/src/server.h @@ -23,6 +23,6 @@ int server_validate_ctx(const server_ctx *ctx); int server_setup_epoll(server_ctx *ctx[], size_t siz); -int server_mainloop_epoll(int epoll_fd, server_ctx *ctx[], size_t siz); +pid_t server_daemonize(int epoll_fd, server_ctx *ctx[], size_t siz); #endif |