aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/jail.c12
-rw-r--r--src/jail.h4
-rw-r--r--src/main.c2
-rw-r--r--src/utils.c52
-rw-r--r--src/utils.h6
5 files changed, 63 insertions, 13 deletions
diff --git a/src/jail.c b/src/jail.c
index cf6dfc0..a086e22 100644
--- a/src/jail.c
+++ b/src/jail.c
@@ -2,9 +2,11 @@
#include <stdlib.h>
#include <sched.h>
#include <signal.h>
+#include <sys/epoll.h>
#include <assert.h>
#include "jail.h"
+#include "utils.h"
#include "log.h"
static int jail_daemonfn(jail_ctx *ctx);
@@ -17,7 +19,7 @@ void jail_init(jail_ctx **ctx, size_t stacksize)
if (stacksize > BUFSIZ)
stacksize = BUFSIZ;
if (!*ctx)
- *ctx = calloc(1, sizeof(**ctx));
+ *ctx = (jail_ctx *) calloc(1, sizeof(**ctx));
assert(*ctx);
(*ctx)->stacksize = stacksize;
@@ -57,9 +59,17 @@ static int jail_daemonfn(jail_ctx *ctx)
{
int clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|
CLONE_NEWNS|CLONE_NEWNET;
+ int fd;
assert(ctx);
+ set_procname("[potd] jaild");
+ assert( set_child_sighandler() == 0 );
+ fd = epoll_create1(0);
+ if (fd < 0) {
+ E_STRERR("Jail epoll create");
+ exit(EXIT_FAILURE);
+ }
while (1) {
ctx->jail_pid = clone(jail_childfn, ctx->stack_beg,
SIGCHLD|clone_flags, ctx);
diff --git a/src/jail.h b/src/jail.h
index 1b4e476..9759ad3 100644
--- a/src/jail.h
+++ b/src/jail.h
@@ -4,10 +4,14 @@
#include <sys/types.h>
#include <unistd.h>
+#include "socket.h"
+
#define MAX_STACKSIZE BUFSIZ
typedef struct jail_ctx {
pid_t jail_pid;
+ psocket sock;
+ char host_buf[NI_MAXHOST], service_buf[NI_MAXSERV];
size_t stacksize;
void *stack_ptr;
void *stack_beg;
diff --git a/src/main.c b/src/main.c
index 9d0de70..ec7b698 100644
--- a/src/main.c
+++ b/src/main.c
@@ -24,6 +24,7 @@ int main(int argc, char *argv[])
(void) argc;
(void) argv;
+ arg0 = argv[0];
LOG_SET_FUNCS_VA(LOG_COLORED_FUNCS);
N("%s (C) 2018 Toni Uhlig (%s)", PACKAGE_STRING, PACKAGE_BUGREPORT);
@@ -33,6 +34,7 @@ int main(int argc, char *argv[])
ABORT_ON_FATAL( daemon_pid > 0, "Forking" );
if (daemon_pid == 0) {
D("Daemon: main child pid: %d", daemon_pid);
+ set_procname("[potd] main");
} else {
E("Forking failed: %d", daemon_pid);
E_STRERR("Daemonize");
diff --git a/src/utils.c b/src/utils.c
index 37d901c..10ec2af 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -6,17 +6,42 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
-#include <syslog.h>
+#include <sys/prctl.h>
+#include <assert.h>
#include "utils.h"
+#include "log.h"
#define _POSIX_PATH_MAX 256
+char *arg0 = NULL;
-void set_procname(char *arg0, const char *newname)
+
+static void sighandler_child(int signo)
+{
+ switch (signo) {
+ case SIGHUP:
+ if (getppid() == 1) {
+ N("Master process %d died, exiting", getpgrp());
+ exit(EXIT_SUCCESS);
+ }
+ break;
+ }
+}
+
+int set_child_sighandler(void)
+{
+ /* not portable */
+ if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0)
+ return 1;
+ return signal(SIGHUP, sighandler_child) == SIG_ERR;
+}
+
+void set_procname(const char *new_arg0)
{
+ assert(arg0);
memset(arg0, 0, _POSIX_PATH_MAX);
- strncpy(arg0, newname, _POSIX_PATH_MAX);
+ strncpy(arg0, new_arg0, _POSIX_PATH_MAX);
}
pid_t daemonize(int stay_foreground)
@@ -40,16 +65,18 @@ pid_t daemonize(int stay_foreground)
}
/* On success: The child process becomes session leader */
- if (!stay_foreground && setsid() < 0)
+ if (!stay_foreground && setsid() < 0) {
+ E_STRERR("setsid");
exit(EXIT_FAILURE);
+ }
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
//signal(SIGCHLD, SIG_IGN);
//signal(SIGHUP, SIG_IGN);
+ /* Fork off for the second time*/
if (!stay_foreground) {
- /* Fork off for the second time*/
pid = fork();
/* An error occurred */
@@ -57,9 +84,13 @@ pid_t daemonize(int stay_foreground)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
- if (pid > 0) {
+ if (pid > 0)
exit(EXIT_SUCCESS);
- }
+ }
+
+ if (!stay_foreground && setpgrp()) {
+ E_STRERR("setpgrp");
+ exit(EXIT_FAILURE);
}
/* Set new file permissions */
@@ -70,13 +101,12 @@ pid_t daemonize(int stay_foreground)
/* Change the working directory to the root directory */
/* or another appropriated directory */
- chdir("/");
+// chdir("/");
/* Close all open file descriptors */
int x;
- for (x = sysconf(_SC_OPEN_MAX); x>=0; x--)
- {
- close (x);
+ for (x = sysconf(_SC_OPEN_MAX); x>=0; x--) {
+// close (x);
}
return pid;
diff --git a/src/utils.h b/src/utils.h
index dc89311..65ff5fb 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -5,8 +5,12 @@
#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
#endif
+extern char *arg0;
-void set_procname(char *arg0, const char *newname);
+
+int set_child_sighandler(void);
+
+void set_procname(const char *new_arg0);
pid_t daemonize(int stay_foreground);