aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlns <matzeton@googlemail.com>2018-04-26 17:59:09 +0200
committerlns <matzeton@googlemail.com>2018-04-26 17:59:09 +0200
commita2e40e828261e1d8407c1b5c02f715147e568170 (patch)
treeae3e703c072ec38c7c6cd2930ac03834ecbe81fc
parentdc8d50bd8d382505ca7a34d501097ce7c181803b (diff)
POTD skeleton #31.
Signed-off-by: lns <matzeton@googlemail.com>
-rw-r--r--src/jail.c9
-rw-r--r--src/jail.h2
-rw-r--r--src/main.c10
-rw-r--r--src/server.c38
-rw-r--r--src/server.h2
5 files changed, 51 insertions, 10 deletions
diff --git a/src/jail.c b/src/jail.c
index 22c8ec2..04bfd21 100644
--- a/src/jail.c
+++ b/src/jail.c
@@ -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;
diff --git a/src/jail.h b/src/jail.h
index 1a6ebd7..a7dff6b 100644
--- a/src/jail.h
+++ b/src/jail.h
@@ -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
diff --git a/src/main.c b/src/main.c
index 0f09095..1d65660 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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