]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/namespace.c
lxc_clone: pass non-stack allocated stack to clone
[mirror_lxc.git] / src / lxc / namespace.c
index e9c0ddd0f09dc7a9fa57a76b7e148cc715a0f6b4..bc14fb52c91aaba4c03c6fb4396fca301fcf2a17 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <alloca.h>
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#endif
 #include <errno.h>
 #include <fcntl.h>
 #include <sched.h>
 #include <signal.h>
-#include <unistd.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
+#include <unistd.h>
 
+#include "config.h"
 #include "log.h"
+#include "memory_utils.h"
 #include "namespace.h"
 #include "utils.h"
 
-lxc_log_define(lxc_namespace, lxc);
+lxc_log_define(namespace, lxc);
 
 struct clone_arg {
        int (*fn)(void *);
@@ -49,84 +53,33 @@ static int do_clone(void *arg)
        return clone_arg->fn(clone_arg->arg);
 }
 
-pid_t lxc_clone(int (*fn)(void *), void *arg, int flags)
+#define __LXC_STACK_SIZE 4096
+pid_t lxc_clone(int (*fn)(void *), void *arg, int flags, int *pidfd)
 {
+       pid_t ret;
        struct clone_arg clone_arg = {
-               .fn = fn,
-               .arg = arg,
+           .fn = fn,
+           .arg = arg,
        };
+       void *stack;
 
-       size_t stack_size = lxc_getpagesize();
-       void *stack = alloca(stack_size);
-       pid_t ret;
+       stack = malloc(__LXC_STACK_SIZE);
+       if (!stack) {
+               SYSERROR("Failed to allocate clone stack");
+               return -ENOMEM;
+       }
 
 #ifdef __ia64__
-       ret = __clone2(do_clone, stack, stack_size, flags | SIGCHLD, &clone_arg);
+       ret = __clone2(fn, stack, __LXC_STACK_SIZE, flags | SIGCHLD, &clone_arg, pidfd);
 #else
-       ret = clone(do_clone, stack  + stack_size, flags | SIGCHLD, &clone_arg);
+       ret = clone(fn, stack + __LXC_STACK_SIZE, flags | SIGCHLD, &clone_arg, pidfd);
 #endif
        if (ret < 0)
-               ERROR("Failed to clone (%#x): %s.", flags, strerror(errno));
+               SYSERROR("Failed to clone (%#x)", flags);
 
        return ret;
 }
 
-/**
- * This is based on raw_clone in systemd but adapted to our needs. This uses
- * copy on write semantics and doesn't pass a stack. CLONE_VM is tricky and
- * doesn't really matter to us so disallow it.
- *
- * The nice thing about this is that we get fork() behavior. That is
- * lxc_raw_clone() returns 0 in the child and the child pid in the parent.
- */
-pid_t lxc_raw_clone(unsigned long flags)
-{
-
-       /* These flags don't interest at all so we don't jump through any hoopes
-        * of retrieving them and passing them to the kernel.
-        */
-       errno = EINVAL;
-       if ((flags & (CLONE_VM | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
-                     CLONE_CHILD_CLEARTID | CLONE_SETTLS)))
-               return -EINVAL;
-
-#if defined(__s390x__) || defined(__s390__) || defined(__CRIS__)
-       /* On s390/s390x and cris the order of the first and second arguments
-        * of the system call is reversed.
-        */
-       return (int)syscall(__NR_clone, NULL, flags | SIGCHLD);
-#elif defined(__sparc__) && defined(__arch64__)
-       {
-               /**
-                * sparc64 always returns the other process id in %o0, and
-                * a boolean flag whether this is the child or the parent in
-                * %o1. Inline assembly is needed to get the flag returned
-                * in %o1.
-                */
-               int in_child;
-               int child_pid;
-               asm volatile("mov %2, %%g1\n\t"
-                            "mov %3, %%o0\n\t"
-                            "mov 0 , %%o1\n\t"
-                            "t 0x6d\n\t"
-                            "mov %%o1, %0\n\t"
-                            "mov %%o0, %1"
-                            : "=r"(in_child), "=r"(child_pid)
-                            : "i"(__NR_clone), "r"(flags | SIGCHLD)
-                            : "%o1", "%o0", "%g1");
-               if (in_child)
-                       return 0;
-               else
-                       return child_pid;
-       }
-#elif defined(__ia64__)
-       /* On ia64 the stack and stack size are passed as separate arguments. */
-       return (int)syscall(__NR_clone, flags | SIGCHLD, NULL, 0);
-#else
-       return (int)syscall(__NR_clone, flags | SIGCHLD, NULL);
-#endif
-}
-
 /* Leave the user namespace at the first position in the array of structs so
  * that we always attach to it first when iterating over the struct and using
  * setns() to switch namespaces. This especially affects lxc_attach(): Suppose
@@ -156,6 +109,7 @@ const struct ns_info ns_info[LXC_NS_MAX] = {
 int lxc_namespace_2_cloneflag(const char *namespace)
 {
        int i;
+
        for (i = 0; i < LXC_NS_MAX; i++)
                if (!strcasecmp(ns_info[i].proc_name, namespace))
                        return ns_info[i].clone_flag;
@@ -167,6 +121,7 @@ int lxc_namespace_2_cloneflag(const char *namespace)
 int lxc_namespace_2_ns_idx(const char *namespace)
 {
        int i;
+
        for (i = 0; i < LXC_NS_MAX; i++)
                if (!strcmp(ns_info[i].proc_name, namespace))
                        return i;
@@ -175,9 +130,43 @@ int lxc_namespace_2_ns_idx(const char *namespace)
        return -EINVAL;
 }
 
+extern int lxc_namespace_2_std_identifiers(char *namespaces)
+{
+       char **it;
+       char *del;
+
+       /* The identifiers for namespaces used with lxc-attach and lxc-unshare
+        * as given on the manpage do not align with the standard identifiers.
+        * This affects network, mount, and uts namespaces. The standard identifiers
+        * are: "mnt", "uts", and "net" whereas lxc-attach and lxc-unshare uses
+        * "MOUNT", "UTSNAME", and "NETWORK". So let's use some cheap memmove()s
+        * to replace them by their standard identifiers.
+        * Let's illustrate this with an example:
+        * Assume the string:
+        *
+        *      "IPC|MOUNT|PID"
+        *
+        * then we memmove()
+        *
+        *      dest: del + 1 == OUNT|PID
+        *      src:  del + 3 == NT|PID
+        */
+       if (!namespaces)
+               return -1;
+
+       while ((del = strstr(namespaces, "MOUNT")))
+               memmove(del + 1, del + 3, strlen(del) - 2);
+
+       for (it = (char *[]){"NETWORK", "UTSNAME", NULL}; it && *it; it++)
+               while ((del = strstr(namespaces, *it)))
+                       memmove(del + 3, del + 7, strlen(del) - 6);
+
+       return 0;
+}
+
 int lxc_fill_namespace_flags(char *flaglist, int *flags)
 {
-       char *token, *saveptr = NULL;
+       char *token;
        int aflag;
 
        if (!flaglist) {
@@ -185,16 +174,13 @@ int lxc_fill_namespace_flags(char *flaglist, int *flags)
                return -1;
        }
 
-       token = strtok_r(flaglist, "|", &saveptr);
-       while (token) {
-
+       lxc_iterate_parts(token, flaglist, "|") {
                aflag = lxc_namespace_2_cloneflag(token);
                if (aflag < 0)
                        return -1;
 
                *flags |= aflag;
-
-               token = strtok_r(NULL, "|", &saveptr);
        }
+
        return 0;
 }