Introduce util.c
authorJagger <robert@swiecki.net>
Sun, 17 Jan 2016 03:14:09 +0000 (04:14 +0100)
committerJagger <robert@swiecki.net>
Sun, 17 Jan 2016 03:14:09 +0000 (04:14 +0100)
Makefile
cmdline.c
subproc.c

index 63d78f0f439d17c07f10f8454bdd80a52ba3b432..1b3ccbc2af0e4ac13559acb49ab429e435d2adfb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,7 @@ CFLAGS += -O2 -g -ggdb -c -std=c11 \
 LD = gcc
 LDFLAGS += -Wl,-z,now -Wl,-z,relro -pie
 
-SRCS = nsjail.c cmdline.c contain.c log.c net.c subproc.c sandbox.c seccomp/bpf-helper.c
+SRCS = nsjail.c cmdline.c contain.c log.c net.c subproc.c sandbox.c util.c seccomp/bpf-helper.c
 OBJS = $(SRCS:.c=.o)
 BIN = nsjail
 
@@ -50,10 +50,11 @@ indent:
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
 nsjail.o: nsjail.h cmdline.h common.h log.h net.h subproc.h
-cmdline.o: cmdline.h common.h log.h
+cmdline.o: cmdline.h common.h log.h util.h
 contain.o: contain.h common.h log.h
 log.o: log.h common.h
 net.o: net.h common.h log.h
-subproc.o: subproc.h common.h contain.h log.h net.h sandbox.h
+subproc.o: subproc.h common.h contain.h log.h net.h sandbox.h util.h
 sandbox.o: sandbox.h common.h log.h seccomp/bpf-helper.h
+util.o: util.h log.h common.h
 seccomp/bpf-helper.o: seccomp/bpf-helper.h
index 14dff34d7fbe21aa249a42af8e8de6b8403d1cdd..c73f06b1ed11786b6befe2fcdc0c2f61edcf7f14 100644 (file)
--- a/cmdline.c
+++ b/cmdline.c
@@ -40,6 +40,7 @@
 
 #include "common.h"
 #include "log.h"
+#include "util.h"
 
 struct custom_option {
        struct option opt;
@@ -404,10 +405,7 @@ bool cmdlineParse(int argc, char *argv[], struct nsjconf_t * nsjconf)
                        break;
                case 'R':
                        {
-                               struct mounts_t *p = malloc(sizeof(struct mounts_t));
-                               if (p == NULL) {
-                                       PLOG_F("malloc(%zu)", sizeof(struct mounts_t));
-                               }
+                               struct mounts_t *p = util_malloc(sizeof(struct mounts_t));
                                p->src = optarg;
                                p->dst = cmdlineMountParam(optarg);
                                p->flags = MS_BIND | MS_REC | MS_PRIVATE | MS_RDONLY;
@@ -418,10 +416,7 @@ bool cmdlineParse(int argc, char *argv[], struct nsjconf_t * nsjconf)
                        break;
                case 'B':
                        {
-                               struct mounts_t *p = malloc(sizeof(struct mounts_t));
-                               if (p == NULL) {
-                                       PLOG_F("malloc(%zu)", sizeof(struct mounts_t));
-                               }
+                               struct mounts_t *p = util_malloc(sizeof(struct mounts_t));
                                p->src = optarg;
                                p->dst = cmdlineMountParam(optarg);
                                p->flags = MS_BIND | MS_REC | MS_PRIVATE;
@@ -432,10 +427,7 @@ bool cmdlineParse(int argc, char *argv[], struct nsjconf_t * nsjconf)
                        break;
                case 'T':
                        {
-                               struct mounts_t *p = malloc(sizeof(struct mounts_t));
-                               if (p == NULL) {
-                                       PLOG_F("malloc(%zu)", sizeof(struct mounts_t));
-                               }
+                               struct mounts_t *p = util_malloc(sizeof(struct mounts_t));
                                p->src = "none";
                                p->dst = optarg;
                                p->flags = 0;
@@ -462,6 +454,7 @@ bool cmdlineParse(int argc, char *argv[], struct nsjconf_t * nsjconf)
                                LOG_E("Modes supported: -M l - MODE_LISTEN_TCP (default)");
                                LOG_E("                 -M o - MODE_STANDALONE_ONCE");
                                LOG_E("                 -M r - MODE_STANDALONE_RERUN");
+                               LOG_E("                 -M e - MODE_STANDALONE_EXECVE");
                                cmdlineUsage(argv[0], custom_opts);
                                return false;
                                break;
@@ -478,10 +471,7 @@ bool cmdlineParse(int argc, char *argv[], struct nsjconf_t * nsjconf)
        }
 
        if (nsjconf->mount_proc == true) {
-               struct mounts_t *p = malloc(sizeof(struct mounts_t));
-               if (p == NULL) {
-                       PLOG_F("malloc(%zu)", sizeof(struct mounts_t));
-               }
+               struct mounts_t *p = util_malloc(sizeof(struct mounts_t));
                p->src = "none";
                p->dst = "/proc";
                p->flags = 0;
@@ -490,10 +480,7 @@ bool cmdlineParse(int argc, char *argv[], struct nsjconf_t * nsjconf)
                TAILQ_INSERT_HEAD(&nsjconf->mountpts, p, pointers);
        }
        if (strlen(nsjconf->chroot) > 0) {
-               struct mounts_t *p = malloc(sizeof(struct mounts_t));
-               if (p == NULL) {
-                       PLOG_F(" malloc(%zu) ", sizeof(struct mounts_t));
-               }
+               struct mounts_t *p = util_malloc(sizeof(struct mounts_t));
                p->src = nsjconf->chroot;
                p->dst = "/";
                p->flags = MS_BIND | MS_REC | MS_PRIVATE;
index bf5eef02c368989e166dfe728f48d56857a6c3c2..c48bc93346268c087f614178e5e1c1806256278a 100644 (file)
--- a/subproc.c
+++ b/subproc.c
@@ -44,6 +44,7 @@
 #include "log.h"
 #include "net.h"
 #include "sandbox.h"
+#include "util.h"
 
 static int subprocNewProc(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_err, int pipefd)
 {
@@ -93,12 +94,7 @@ static int subprocNewProc(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int
 
 static void subprocAdd(struct nsjconf_t *nsjconf, pid_t pid, int sock)
 {
-       struct pids_t *p = malloc(sizeof(struct pids_t));
-       if (p == NULL) {
-               PLOG_E("malloc");
-               return;
-       }
-
+       struct pids_t *p = util_malloc(sizeof(struct pids_t));
        p->pid = pid;
        p->start = time(NULL);
        netConnToText(sock, true /* remote */ , p->remote_txt, sizeof(p->remote_txt),