diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2018-04-24 00:23:55 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2018-04-24 00:23:55 +0200 |
commit | b14059ea3a1f2e0a41d90ababf27473c026042d0 (patch) | |
tree | b36e82bf6ff28805ffab4870d2405dbb62e1fe8e /src/jail.c | |
parent | 8be43bc691b38b582aca2f8c3bbf232fbb4d495e (diff) |
POTD skeleton #26.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/jail.c')
-rw-r--r-- | src/jail.c | 65 |
1 files changed, 59 insertions, 6 deletions
@@ -3,9 +3,11 @@ #include <sched.h> #include <signal.h> #include <sys/epoll.h> +#include <sys/prctl.h> #include <assert.h> #include "jail.h" +#include "socket.h" #include "utils.h" #include "log.h" @@ -13,7 +15,7 @@ static int jail_daemonfn(jail_ctx *ctx); static int jail_childfn(void *arg); -void jail_init(jail_ctx **ctx, size_t stacksize) +void jail_init_ctx(jail_ctx **ctx, size_t stacksize) { assert(ctx); if (stacksize > BUFSIZ) @@ -27,6 +29,43 @@ void jail_init(jail_ctx **ctx, size_t stacksize) (*ctx)->stack_beg = (unsigned char *) (*ctx)->stack_ptr + (*ctx)->stacksize; + assert( (*ctx)->stack_ptr ); +} + +int jail_setup(jail_ctx *ctx, + const char *listen_addr, const char *listen_port) +{ + int s; + struct addrinfo *srv_addr = NULL; + + assert(ctx); + assert(listen_addr || listen_port); + + D2("Try to listen on %s:%s", + (listen_addr ? listen_addr : "*"), listen_port); + s = socket_init_in(listen_addr, listen_port, &srv_addr); + if (s) { + E_GAIERR(s, "Could not initialise server socket"); + return 1; + } + if (socket_bind_in(&ctx->sock, &srv_addr)) { + E_STRERR("Could not bind server socket"); + return 1; + } + if (socket_listen_in(&ctx->sock)) { + E_STRERR("Could not listen on server socket"); + return 1; + } + + return 0; +} + +int jail_validate_ctx(const jail_ctx *ctx) +{ + assert(ctx); + assert(ctx->sock.addr_len > 0); + + return 0; } void jail_free(jail_ctx **ctx) @@ -38,7 +77,15 @@ void jail_free(jail_ctx **ctx) int jail_daemonize(jail_ctx *ctx) { + int s; + assert(ctx); + s = socket_addrtostr_in(&ctx->sock, + ctx->host_buf, ctx->service_buf); + if (s) { + E_GAIERR(s, "Could not initialise jail daemon socket"); + return 1; + } ctx->jail_pid = fork(); switch (ctx->jail_pid) { @@ -64,6 +111,7 @@ static int jail_daemonfn(jail_ctx *ctx) assert(ctx); set_procname("[potd] jaild"); assert( set_child_sighandler() == 0 ); + assert( signal(SIGCHLD, SIG_IGN) != SIG_ERR ); fd = epoll_create1(0); if (fd < 0) { @@ -72,7 +120,7 @@ static int jail_daemonfn(jail_ctx *ctx) } while (1) { ctx->jail_pid = clone(jail_childfn, ctx->stack_beg, - SIGCHLD|clone_flags, ctx); + SIGCHLD|clone_flags, NULL); sleep(1); printf("---\n"); } @@ -82,9 +130,14 @@ static int jail_daemonfn(jail_ctx *ctx) static int jail_childfn(void *arg) { + (void) arg; + + if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0) { + E_STRERR("Jail child prctl"); + return 1; + } printf("----> CHILD FN <----\n"); - FILE *log = fopen("./test.log", "wb"); - fprintf(log, "---> CHILD FN <----\n"); - sleep(200); - return 0; + sleep(10); + + exit(EXIT_SUCCESS); } |