]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
namespace: add lxc_raw_clone_cb()
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 15 Dec 2017 16:35:43 +0000 (17:35 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 21 Dec 2017 22:01:04 +0000 (23:01 +0100)
This is a copy-on-write (no stack passed) variant of lxc_clone().

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/namespace.c
src/lxc/namespace.h

index e9c0ddd0f09dc7a9fa57a76b7e148cc715a0f6b4..6f5ea674b9c17eb023562c52a730bac6f5f90863 100644 (file)
@@ -127,6 +127,23 @@ pid_t lxc_raw_clone(unsigned long flags)
 #endif
 }
 
+pid_t lxc_raw_clone_cb(int (*fn)(void *), void *args, unsigned long flags)
+{
+       pid_t pid;
+
+       pid = lxc_raw_clone(flags);
+       if (pid < 0)
+               return -1;
+
+       /* exit() is not thread-safe and might mess with the parent's signal
+        * handlers and other stuff when exec() fails.
+        */
+       if (pid == 0)
+               _exit(fn(args));
+
+       return pid;
+}
+
 /* 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
index 781e29970cc9e15d4d15b695f4cbbe85426aa4d7..4bfe9c4f5a047ea2660cd86c46cac541ef658f03 100644 (file)
@@ -130,7 +130,7 @@ int clone(int (*fn)(void *), void *child_stack,
  *   corresponding libc wrapper. glibc currently does not run pthread_atfork()
  *   handlers but does not guarantee that they are not. Other libcs might or
  *   might not run pthread_atfork() handlers. If you require guarantees please
- *   refer to the lxc_raw_clone() function below.
+ *   refer to the lxc_raw_clone*() functions below.
  *
  * - should call lxc_raw_getpid():
  *   The child should use lxc_raw_getpid() to retrieve its pid.
@@ -165,6 +165,19 @@ extern pid_t lxc_clone(int (*fn)(void *), void *arg, int flags);
  *   The child must use lxc_raw_getpid() to retrieve its pid.
  */
 extern pid_t lxc_raw_clone(unsigned long flags);
+/**
+ * lxc_raw_clone_cb() - create a new process
+ *
+ * - non-fork() behavior:
+ *   Function does return pid of the child or -1 on error. Pass in a callback
+ *   function via the "fn" argument that gets executed in the child process. The
+ *   "args" argument is passed to "fn".
+ *
+ * All other comments that apply to lxc_raw_clone() apply to lxc_raw_clone_cb()
+ * as well.
+ */
+extern pid_t lxc_raw_clone_cb(int (*fn)(void *), void *args,
+                             unsigned long flags);
 
 extern int lxc_namespace_2_cloneflag(const char *namespace);
 extern int lxc_namespace_2_ns_idx(const char *namespace);