6c0556
commit de8995a2a04163617c1a233b4b81356ef9f9741f
6c0556
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
6c0556
Date:   Wed Mar 10 12:26:30 2021 -0300
6c0556
6c0556
    support: Add xclone
6c0556
    
6c0556
    It is a wrapper for Linux clone syscall, to simplify the call to the
6c0556
    use only the most common arguments and remove architecture specific
6c0556
    handling (such as ia64 different name and signature).
6c0556
6c0556
# Conflicts:
6c0556
#	support/Makefile
6c0556
6c0556
diff --git a/support/Makefile b/support/Makefile
6c0556
index fb95a69ed9158e78..d2b95539403e416c 100644
6c0556
--- a/support/Makefile
6c0556
+++ b/support/Makefile
6c0556
@@ -84,6 +84,7 @@ libsupport-routines = \
6c0556
   xcalloc \
6c0556
   xchdir \
6c0556
   xchroot \
6c0556
+  xclone \
6c0556
   xclose \
6c0556
   xconnect \
6c0556
   xcopy_file_range \
6c0556
diff --git a/support/xclone.c b/support/xclone.c
6c0556
new file mode 100644
6c0556
index 0000000000000000..924d2b875402a819
6c0556
--- /dev/null
6c0556
+++ b/support/xclone.c
6c0556
@@ -0,0 +1,50 @@
6c0556
+/* Auxiliary functions to issue the clone syscall.
6c0556
+   Copyright (C) 2021 Free Software Foundation, Inc.
6c0556
+   This file is part of the GNU C Library.
6c0556
+
6c0556
+   The GNU C Library is free software; you can redistribute it and/or
6c0556
+   modify it under the terms of the GNU Lesser General Public
6c0556
+   License as published by the Free Software Foundation; either
6c0556
+   version 2.1 of the License, or (at your option) any later version.
6c0556
+
6c0556
+   The GNU C Library is distributed in the hope that it will be useful,
6c0556
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6c0556
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6c0556
+   Lesser General Public License for more details.
6c0556
+
6c0556
+   You should have received a copy of the GNU Lesser General Public
6c0556
+   License along with the GNU C Library; if not, see
6c0556
+   <https://www.gnu.org/licenses/>.  */
6c0556
+
6c0556
+#ifdef __linux__
6c0556
+# include <support/check.h>
6c0556
+# include <stackinfo.h>  /* For _STACK_GROWS_{UP,DOWN}.  */
6c0556
+# include <xsched.h>
6c0556
+
6c0556
+pid_t
6c0556
+xclone (int (*fn) (void *arg), void *arg, void *stack, size_t stack_size,
6c0556
+	int flags)
6c0556
+{
6c0556
+  pid_t r = -1;
6c0556
+
6c0556
+# ifdef __ia64__
6c0556
+  extern int __clone2 (int (*fn) (void *arg), void *stack, size_t stack_size,
6c0556
+		       int flags, void *arg, ...);
6c0556
+  r = __clone2 (f, stack, stack_size, flags, arg, /* ptid */ NULL,
6c0556
+		/* tls */ NULL, /* ctid  */ ctid);
6c0556
+# else
6c0556
+#  if _STACK_GROWS_DOWN
6c0556
+  r = clone (fn, stack + stack_size, flags, arg, /* ptid */ NULL,
6c0556
+	     /* tls */ NULL, /* ctid */  NULL);
6c0556
+#  elif _STACK_GROWS_UP
6c0556
+  r = clone (fn, stack, flags, arg, /* ptid */ NULL, /* tls */ NULL,
6c0556
+	     &ctid);
6c0556
+#  endif
6c0556
+# endif
6c0556
+
6c0556
+  if (r < 0)
6c0556
+    FAIL_EXIT1 ("clone: %m");
6c0556
+
6c0556
+  return r;
6c0556
+}
6c0556
+#endif
6c0556
diff --git a/support/xsched.h b/support/xsched.h
6c0556
new file mode 100644
6c0556
index 0000000000000000..eefd731940187b39
6c0556
--- /dev/null
6c0556
+++ b/support/xsched.h
6c0556
@@ -0,0 +1,34 @@
6c0556
+/* Wrapper for sched.h functions.
6c0556
+   Copyright (C) 2021 Free Software Foundation, Inc.
6c0556
+   This file is part of the GNU C Library.
6c0556
+
6c0556
+   The GNU C Library is free software; you can redistribute it and/or
6c0556
+   modify it under the terms of the GNU Lesser General Public
6c0556
+   License as published by the Free Software Foundation; either
6c0556
+   version 2.1 of the License, or (at your option) any later version.
6c0556
+
6c0556
+   The GNU C Library is distributed in the hope that it will be useful,
6c0556
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6c0556
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6c0556
+   Lesser General Public License for more details.
6c0556
+
6c0556
+   You should have received a copy of the GNU Lesser General Public
6c0556
+   License along with the GNU C Library; if not, see
6c0556
+   <https://www.gnu.org/licenses/>.  */
6c0556
+
6c0556
+#ifndef SUPPORT_XSCHED_H
6c0556
+#define SUPPORT_XSCHED_H
6c0556
+
6c0556
+__BEGIN_DECLS
6c0556
+
6c0556
+#include <sched.h>
6c0556
+#include <sys/types.h>
6c0556
+
6c0556
+#ifdef __linux__
6c0556
+pid_t xclone (int (*fn) (void *arg), void *arg, void *stack,
6c0556
+	      size_t stack_size, int flags);
6c0556
+#endif
6c0556
+
6c0556
+__END_DECLS
6c0556
+
6c0556
+#endif