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