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