[REBASED,2/3] utils: Add sys_clone_unified()

Submitted by Kirill Tkhai on July 6, 2017, 11:13 a.m.

Details

Message ID 149933962893.17355.6358003398217463531.stgit@localhost.localdomain
State Accepted
Series "Create pid_ns helpers as children of criu main task"
Commit 2df6fe57390cdac569ddf35f5d55f5ad7db67a3b
Headers show

Commit Message

Kirill Tkhai July 6, 2017, 11:13 a.m.
Cleanup fork() definition and make a generic function
for all archs. It may be useful, when you want to add
more clone flags to fork(), or if you want to pass more,
than one argument to child function (glibc's clone
alows only one).

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/include/util.h  |    3 +++
 criu/util.c          |   25 +++++++++++++++----------
 test/zdtm/lib/test.c |   23 ++++++++++++++---------
 3 files changed, 32 insertions(+), 19 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/include/util.h b/criu/include/util.h
index ec93fd5c6..3285c30ce 100644
--- a/criu/include/util.h
+++ b/criu/include/util.h
@@ -61,6 +61,9 @@  extern int open_pid_proc(pid_t pid);
 extern int close_pid_proc(void);
 extern int set_proc_fd(int fd);
 
+extern pid_t sys_clone_unified(unsigned long flags, void *child_stack, void *parent_tid,
+			       void *child_tid, unsigned long newtls);
+
 /*
  * Values for pid argument of the proc opening routines below.
  * SELF would open file under /proc/self
diff --git a/criu/util.c b/criu/util.c
index 2adc6139a..7c6dae655 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -1417,20 +1417,25 @@  int getpid()
        return syscall(__NR_getpid);
 }
 
+pid_t sys_clone_unified(unsigned long flags, void *child_stack, void *parent_tid,
+			void *child_tid, unsigned long newtls)
+{
+#ifdef __x86_64__
+	return (pid_t)syscall(__NR_clone, flags, child_stack, parent_tid, child_tid, newtls);
+#elif (__i386__ || __arm__ || __aarch64__ || __powerpc64__)
+	return (pid_t)syscall(__NR_clone, flags, child_stack, parent_tid, newtls, child_tid);
+#elif __s390x__
+	return (pid_t)syscall(__NR_clone, child_stack, flags, parent_tid, child_tid, newtls);
+#else
+#error "Unsupported architecture"
+#endif
+}
+
 /*
  * In glibc 2.24, fork() may fail when parent and child are
  * from different pid namespaces and have the same pid.
  */
 pid_t fork()
 {
-	/*
-	 * Two last arguments are swapped on different archs,
-	 * but we don't care as they are zero anyway.
-	 */
-#ifdef __s390x__
-	/* See kernel/fork.c: CONFIG_CLONE_BACKWARDS2 */
-	return (pid_t)syscall(__NR_clone, 0, SIGCHLD, NULL, 0, NULL);
-#else
-	return (pid_t)syscall(__NR_clone, SIGCHLD, 0, 0, 0, 0);
-#endif
+	return sys_clone_unified(SIGCHLD, 0, NULL, NULL, 0);
 }
diff --git a/test/zdtm/lib/test.c b/test/zdtm/lib/test.c
index 5f1208399..620dcfce6 100644
--- a/test/zdtm/lib/test.c
+++ b/test/zdtm/lib/test.c
@@ -286,20 +286,25 @@  void test_waitsig(void)
 	futex_wait_while(&sig_received, 0);
 }
 
-pid_t fork()
+pid_t sys_clone_unified(unsigned long flags, void *child_stack, void *parent_tid,
+			void *child_tid, unsigned long newtls)
 {
-	/*
-	 * Two last arguments are swapped on different archs,
-	 * but we don't care as they are zero anyway.
-	 */
-#ifdef __s390x__
-	/* See kernel/fork.c: CONFIG_CLONE_BACKWARDS2 */
-	return (pid_t)syscall(__NR_clone, 0, SIGCHLD, NULL, 0, NULL);
+#ifdef __x86_64__
+	return (pid_t)syscall(__NR_clone, flags, child_stack, parent_tid, child_tid, newtls);
+#elif (__i386__ || __arm__ || __aarch64__ ||__powerpc64__)
+	return (pid_t)syscall(__NR_clone, flags, child_stack, parent_tid, newtls, child_tid);
+#elif __s390x__
+	return (pid_t)syscall(__NR_clone, child_stack, flags, parent_tid, child_tid, newtls);
 #else
-	return (pid_t)syscall(__NR_clone, SIGCHLD, 0, 0, 0, 0);
+#error "Unsupported architecture"
 #endif
 }
 
+pid_t fork()
+{
+	return sys_clone_unified(SIGCHLD, NULL, NULL, NULL, 0);
+}
+
 int getpid()
 {
        return syscall(__NR_getpid);