Make it compile (maybe) under uClibc
authorRobert Swiecki <robert@swiecki.net>
Fri, 29 Sep 2017 11:07:42 +0000 (13:07 +0200)
committerRobert Swiecki <robert@swiecki.net>
Fri, 29 Sep 2017 11:07:42 +0000 (13:07 +0200)
contain.c
mount.c
util.c

index 0c3df8a4eb21bb4379c489b2baf827afcbc01ce0..93dc2a45e0f02007ddbbb343996d3250c0151eb4 100644 (file)
--- a/contain.c
+++ b/contain.c
@@ -126,42 +126,48 @@ static bool containCPU(struct nsjconf_t *nsjconf)
        return cpuInit(nsjconf);
 }
 
+/* uClibc doesn't provide prlimit64 natively */
+static int containSetLimit64(int resource, struct rlimit64 *rlim)
+{
+       return syscall(__NR_prlimit64, (uintptr_t) 0, (uintptr_t) resource, rlim, NULL);
+}
+
 static bool containSetLimits(struct nsjconf_t *nsjconf)
 {
        struct rlimit64 rl;
        rl.rlim_cur = rl.rlim_max = nsjconf->rl_as;
-       if (prlimit64(0, RLIMIT_AS, &rl, NULL) == -1) {
-               PLOG_E("prlimit64(0, RLIMIT_AS, %" PRIu64 ")", nsjconf->rl_as);
+       if (containSetLimit64(RLIMIT_AS, &rl) == -1) {
+               PLOG_E("containSetLimit64(0, RLIMIT_AS, %" PRIu64 ")", nsjconf->rl_as);
                return false;
        }
        rl.rlim_cur = rl.rlim_max = nsjconf->rl_core;
-       if (prlimit64(0, RLIMIT_CORE, &rl, NULL) == -1) {
-               PLOG_E("prlimit64(0, RLIMIT_CORE, %" PRIu64 ")", nsjconf->rl_core);
+       if (containSetLimit64(RLIMIT_CORE, &rl) == -1) {
+               PLOG_E("containSetLimit64(0, RLIMIT_CORE, %" PRIu64 ")", nsjconf->rl_core);
                return false;
        }
        rl.rlim_cur = rl.rlim_max = nsjconf->rl_cpu;
-       if (prlimit64(0, RLIMIT_CPU, &rl, NULL) == -1) {
-               PLOG_E("prlimit64(0, RLIMIT_CPU, %" PRIu64 ")", nsjconf->rl_cpu);
+       if (containSetLimit64(RLIMIT_CPU, &rl) == -1) {
+               PLOG_E("containSetLimit64(0, RLIMIT_CPU, %" PRIu64 ")", nsjconf->rl_cpu);
                return false;
        }
        rl.rlim_cur = rl.rlim_max = nsjconf->rl_fsize;
-       if (prlimit64(0, RLIMIT_FSIZE, &rl, NULL) == -1) {
-               PLOG_E("prlimit64(0, RLIMIT_FSIZE, %" PRIu64 ")", nsjconf->rl_fsize);
+       if (containSetLimit64(RLIMIT_FSIZE, &rl) == -1) {
+               PLOG_E("containSetLimit64(0, RLIMIT_FSIZE, %" PRIu64 ")", nsjconf->rl_fsize);
                return false;
        }
        rl.rlim_cur = rl.rlim_max = nsjconf->rl_nofile;
-       if (prlimit64(0, RLIMIT_NOFILE, &rl, NULL) == -1) {
-               PLOG_E("prlimit64(0, RLIMIT_NOFILE, %" PRIu64 ")", nsjconf->rl_nofile);
+       if (containSetLimit64(RLIMIT_NOFILE, &rl) == -1) {
+               PLOG_E("containSetLimit64(0, RLIMIT_NOFILE, %" PRIu64 ")", nsjconf->rl_nofile);
                return false;
        }
        rl.rlim_cur = rl.rlim_max = nsjconf->rl_nproc;
-       if (prlimit64(0, RLIMIT_NPROC, &rl, NULL) == -1) {
-               PLOG_E("prlimit64(0, RLIMIT_NPROC, %" PRIu64 ")", nsjconf->rl_nproc);
+       if (containSetLimit64(RLIMIT_NPROC, &rl) == -1) {
+               PLOG_E("containSetLimit64(0, RLIMIT_NPROC, %" PRIu64 ")", nsjconf->rl_nproc);
                return false;
        }
        rl.rlim_cur = rl.rlim_max = nsjconf->rl_stack;
-       if (prlimit64(0, RLIMIT_STACK, &rl, NULL) == -1) {
-               PLOG_E("prlimit64(0, RLIMIT_STACK, %" PRIu64 ")", nsjconf->rl_stack);
+       if (containSetLimit64(RLIMIT_STACK, &rl) == -1) {
+               PLOG_E("containSetLimit64(0, RLIMIT_STACK, %" PRIu64 ")", nsjconf->rl_stack);
                return false;
        }
        return true;
diff --git a/mount.c b/mount.c
index 195c65d2de3c79fa5206595272bb80495cbea91f..12b88f6f3881dc469ad250de19792fc969d94cd7 100644 (file)
--- a/mount.c
+++ b/mount.c
@@ -175,11 +175,14 @@ static bool mountMount(struct mounts_t *mpt, const char *newroot, const char *tm
        }
 
        if (mpt->src_content) {
-               snprintf(srcpath, sizeof(srcpath), "%s/file.XXXXXX", tmpdir);
-               int fd = mkostemp(srcpath, O_CLOEXEC);
+               static uint64_t df_counter = 0;
+               snprintf(srcpath, sizeof(srcpath), "%s/dynamic_file.%" PRIu64, tmpdir,
+                        ++df_counter);
+               int fd =
+                   TEMP_FAILURE_RETRY(open
+                                      (srcpath, O_CREAT | O_EXCL | O_CLOEXEC | O_WRONLY, 0644));
                if (fd < 0) {
-                       PLOG_W("mkostemp('%s')", srcpath);
-                       close(fd);
+                       PLOG_W("open(srcpath, O_CREAT|O_EXCL|O_CLOEXEC|O_WRONLY, 0644) failed");
                        return false;
                }
                if (utilWriteToFd(fd, mpt->src_content, mpt->src_content_len) == false) {
diff --git a/util.c b/util.c
index 1ddd6914d95355bf5f884b43f93664b3ce0cca82..097e1336ff22fe96409551491f7659bcdc9f19fa 100644 (file)
--- a/util.c
+++ b/util.c
@@ -255,19 +255,64 @@ uint64_t utilRnd64(void)
        return rndX;
 }
 
-extern const char *sys_sigabbrev[];
-static __thread char sigstr[32];
+#define VALSTR_STRUCT(x)       \
+       {                                               \
+               x, #x                           \
+       }
 const char *utilSigName(int signo)
 {
-       if (signo < 0 || signo > _NSIG) {
-               snprintf(sigstr, sizeof(sigstr), "UNKNOWN-%d", signo);
-               return sigstr;
+       static __thread char sigstr[32];
+       sigstr[0] = '\0';
+
+       /*  *INDENT-OFF* */
+       static struct {
+               const int signo;
+               const char* const name;
+       } const sigNames[] = {
+               VALSTR_STRUCT(SIGINT),
+               VALSTR_STRUCT(SIGILL),
+               VALSTR_STRUCT(SIGABRT),
+               VALSTR_STRUCT(SIGFPE),
+               VALSTR_STRUCT(SIGSEGV),
+               VALSTR_STRUCT(SIGTERM),
+               VALSTR_STRUCT(SIGHUP),
+               VALSTR_STRUCT(SIGQUIT),
+               VALSTR_STRUCT(SIGTRAP),
+               VALSTR_STRUCT(SIGKILL),
+               VALSTR_STRUCT(SIGBUS),
+               VALSTR_STRUCT(SIGSYS),
+               VALSTR_STRUCT(SIGPIPE),
+               VALSTR_STRUCT(SIGALRM),
+               VALSTR_STRUCT(SIGURG),
+               VALSTR_STRUCT(SIGSTOP),
+               VALSTR_STRUCT(SIGTSTP),
+               VALSTR_STRUCT(SIGCONT),
+               VALSTR_STRUCT(SIGCHLD),
+               VALSTR_STRUCT(SIGTTIN),
+               VALSTR_STRUCT(SIGTTOU),
+               VALSTR_STRUCT(SIGPOLL),
+               VALSTR_STRUCT(SIGXCPU),
+               VALSTR_STRUCT(SIGXFSZ),
+               VALSTR_STRUCT(SIGVTALRM),
+               VALSTR_STRUCT(SIGPROF),
+               VALSTR_STRUCT(SIGUSR1),
+               VALSTR_STRUCT(SIGUSR2),
+               VALSTR_STRUCT(SIGWINCH),
+       };
+       /*  *INDENT-ON* */
+
+       for (size_t i = 0; i < ARRAYSIZE(sigNames); i++) {
+               if (signo == sigNames[i].signo) {
+                       snprintf(sigstr, sizeof(sigstr), "%s", sigNames[i].name);
+                       return sigstr;
+               }
        }
+
        if (signo > __SIGRTMIN) {
                snprintf(sigstr, sizeof(sigstr), "SIG%d-RTMIN+%d", signo, signo - __SIGRTMIN);
                return sigstr;
        }
-       snprintf(sigstr, sizeof(sigstr), "SIG%s", sys_sigabbrev[signo]);
+       snprintf(sigstr, sizeof(sigstr), "UNKNOWN-%d", signo);
        return sigstr;
 }