#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
dprintf(log_fd, "%s", logLevels[ll].prefix);
}
if (logLevels[ll].print_funcline) {
- dprintf(log_fd, "[%s][%s][%d] %s():%d ", timestr, logLevels[ll].descr, getpid(), fn, ln);
+ dprintf(log_fd, "[%s][%s][%ld] %s():%d ", timestr, logLevels[ll].descr, syscall(__NR_getpid), fn, ln);
}
va_list args;
{
LOG_I("Server stops due to fatal signal (%d) caught. Exiting", sig);
}
+
+void logNewLogFD(int fd)
+{
+ log_fd = fd;
+}
+
+void logDirectly(const char *msg)
+{
+ dprintf(log_fd, "%s", msg);
+}
#include <arpa/inet.h>
#include <errno.h>
+#include <fcntl.h>
#include <netinet/in.h>
#include <sched.h>
#include <signal.h>
#include "net.h"
#include "sandbox.h"
-static int subprocNewProc(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_err)
+static int subprocNewProc(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_err, int pipefd)
{
if (containPrepareEnv(nsjconf) == false) {
exit(1);
}
- if (containSetupFD(nsjconf, fd_in, fd_out, fd_err) == false) {
+ if (containSetupFD(nsjconf, fd_in, fd_out, fd_err, pipefd) == false) {
exit(1);
}
if (containMountFS(nsjconf) == false) {
LOG_D(" Arg[%d]: '%s'", i, nsjconf->argv[i]);
}
execve(nsjconf->argv[0], &nsjconf->argv[0], env);
- PLOG_F("execve('%s')", nsjconf->argv[0]);
- exit(1);
+
+ PLOG_E("execve('%s') failed", nsjconf->argv[0]);
+
+ _exit(1);
}
static void subprocAdd(struct nsjconf_t *nsjconf, pid_t pid, int sock)
struct pids_t *p;
LIST_FOREACH(p, &nsjconf->pids, pointers) {
time_t diff = now - p->start;
- LOG_I("PID: %d, Remote host: %s, Run time: %ld sec.", p->pid, p->remote_txt, (long)diff);
+ time_t left = nsjconf->tlimit ? nsjconf->tlimit - diff : 0;
+ LOG_I("PID: %d, Remote host: %s, Run time: %ld sec. (time left: %ld sec.)", p->pid, p->remote_txt,
+ (long)diff, (long)left);
}
}
LOG_D("Creating new process with clone flags: %#x", flags);
+ int pipefd[2];
+ if (pipe2(pipefd, O_CLOEXEC) == -1) {
+ PLOG_E("pipe2(pipefd, O_CLOEXEC) failed");
+ return;
+ }
+
pid_t pid = syscall(__NR_clone, flags, NULL, NULL, NULL, 0);
if (pid == 0) {
- subprocNewProc(nsjconf, fd_in, fd_out, fd_err);
+ subprocNewProc(nsjconf, fd_in, fd_out, fd_err, pipefd[1]);
}
if (pid == -1) {
PLOG_E("clone(flags=%#x) failed. You probably need root privileges if your system "
return;
}
+ char log_buf[4096];
+
+ ssize_t sz;
+ while ((sz = read(pipefd[0], log_buf, sizeof(log_buf) - 1)) > 0) {
+ log_buf[sz] = '\0';
+ logDirectly(log_buf);
+ }
+ close(pipefd[0]);
+ close(pipefd[1]);
+
subprocAdd(nsjconf, pid, fd_in);
char cs_addr[64];