vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2009 |
| 3 | * Some wrappers for clone functionality. Thrown together by Serge Hallyn |
| 4 | * <serue@us.ibm.com> based on existing clone usage in ltp. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
yaberauneya | 8680b28 | 2009-11-14 10:27:29 +0000 | [diff] [blame] | 20 | #ifndef _GNU_SOURCE |
Zeng Linggang | 1db6266 | 2014-01-09 20:10:11 +0800 | [diff] [blame] | 21 | # define _GNU_SOURCE |
yaberauneya | 8680b28 | 2009-11-14 10:27:29 +0000 | [diff] [blame] | 22 | #endif |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <errno.h> |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 26 | #include <unistd.h> |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 27 | #include <string.h> |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <sched.h> |
zenglg.jy | 5f5cb63 | 2013-12-18 17:29:51 +0800 | [diff] [blame] | 30 | #include <stdarg.h> |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 31 | #include "test.h" |
Zeng Linggang | 1db6266 | 2014-01-09 20:10:11 +0800 | [diff] [blame] | 32 | #include "config.h" |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 33 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 34 | #undef clone /* we want to use clone() */ |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 35 | |
Zeng Linggang | 1db6266 | 2014-01-09 20:10:11 +0800 | [diff] [blame] | 36 | /* |
| 37 | * The ia64 port has never included a prototype for __clone2(). It was updated |
| 38 | * to take eight parameters in glibc commit: |
| 39 | * |
| 40 | * commit 625f22fc7f8e0d61e3e6cff2c65468b91dbad426 |
| 41 | * Author: Ulrich Drepper <drepper@redhat.com> |
| 42 | * Date: Mon Mar 3 19:53:27 2003 +0000 |
| 43 | * |
| 44 | * The first release that contained this commit was glibc-2.3.3 which is old |
| 45 | * enough to assume that __clone2() takes eight parameters. |
| 46 | */ |
Cyril Hrubis | 9317d9c | 2014-01-09 14:42:23 +0100 | [diff] [blame] | 47 | #if defined(__ia64__) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 48 | extern int __clone2(int (*fn) (void *arg), void *child_stack_base, |
Zeng Linggang | 1db6266 | 2014-01-09 20:10:11 +0800 | [diff] [blame] | 49 | size_t child_stack_size, int flags, void *arg, |
| 50 | pid_t *parent_tid, void *tls, pid_t *child_tid); |
| 51 | #endif |
| 52 | |
| 53 | #ifndef CLONE_SUPPORTS_7_ARGS |
| 54 | # define clone(fn, stack, flags, arg, ptid, tls, ctid) \ |
| 55 | clone(fn, stack, flags, arg) |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 58 | /* |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 59 | * ltp_clone: wrapper for clone to hide the architecture dependencies. |
| 60 | * 1. hppa takes bottom of stack and no stacksize (stack grows up) |
| 61 | * 2. __ia64__ takes bottom of stack and uses clone2 |
| 62 | * 3. all others take top of stack (stack grows down) |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 63 | */ |
Zeng Linggang | 1db6266 | 2014-01-09 20:10:11 +0800 | [diff] [blame] | 64 | static int |
| 65 | ltp_clone_(unsigned long flags, int (*fn)(void *arg), void *arg, |
| 66 | size_t stack_size, void *stack, pid_t *ptid, void *tls, pid_t *ctid) |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 67 | { |
| 68 | int ret; |
| 69 | |
Mike Frysinger | 93c6031 | 2014-01-09 09:35:08 -0500 | [diff] [blame] | 70 | #if defined(__ia64__) |
Zeng Linggang | 1db6266 | 2014-01-09 20:10:11 +0800 | [diff] [blame] | 71 | ret = __clone2(fn, stack, stack_size, flags, arg, ptid, tls, ctid); |
Garrett Cooper | 9185e83 | 2010-11-20 19:32:18 -0800 | [diff] [blame] | 72 | #else |
Mike Frysinger | 93c6031 | 2014-01-09 09:35:08 -0500 | [diff] [blame] | 73 | # if defined(__hppa__) || defined(__metag__) |
| 74 | /* |
| 75 | * These arches grow their stack up, so don't need to adjust the base. |
| 76 | * XXX: This should be made into a runtime test. |
| 77 | */ |
| 78 | # else |
Garrett Cooper | 0056e39 | 2010-07-22 08:45:53 -0700 | [diff] [blame] | 79 | /* |
Garrett Cooper | 9185e83 | 2010-11-20 19:32:18 -0800 | [diff] [blame] | 80 | * For archs where stack grows downwards, stack points to the topmost |
| 81 | * address of the memory space set up for the child stack. |
Garrett Cooper | 0056e39 | 2010-07-22 08:45:53 -0700 | [diff] [blame] | 82 | */ |
Mike Frysinger | 93c6031 | 2014-01-09 09:35:08 -0500 | [diff] [blame] | 83 | if (stack) |
| 84 | stack += stack_size; |
| 85 | # endif |
| 86 | |
Zeng Linggang | 1db6266 | 2014-01-09 20:10:11 +0800 | [diff] [blame] | 87 | ret = clone(fn, stack, flags, arg, ptid, tls, ctid); |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 88 | #endif |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
Zeng Linggang | 1db6266 | 2014-01-09 20:10:11 +0800 | [diff] [blame] | 93 | int ltp_clone(unsigned long flags, int (*fn)(void *arg), void *arg, |
| 94 | size_t stack_size, void *stack) |
| 95 | { |
| 96 | return ltp_clone_(flags, fn, arg, stack_size, stack, NULL, NULL, NULL); |
| 97 | } |
| 98 | |
| 99 | int ltp_clone7(unsigned long flags, int (*fn)(void *arg), void *arg, |
| 100 | size_t stack_size, void *stack, ...) |
| 101 | { |
| 102 | pid_t *ptid, *ctid; |
| 103 | void *tls; |
| 104 | va_list arg_clone; |
| 105 | |
| 106 | va_start(arg_clone, stack); |
| 107 | ptid = va_arg(arg_clone, pid_t *); |
| 108 | tls = va_arg(arg_clone, void *); |
| 109 | ctid = va_arg(arg_clone, pid_t *); |
| 110 | va_end(arg_clone); |
| 111 | |
| 112 | #ifdef CLONE_SUPPORTS_7_ARGS |
| 113 | return ltp_clone_(flags, fn, arg, stack_size, stack, ptid, tls, ctid); |
| 114 | #else |
| 115 | errno = ENOSYS; |
| 116 | return -1; |
| 117 | #endif |
| 118 | } |
| 119 | |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 120 | /* |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 121 | * ltp_clone_malloc: also does the memory allocation for clone with a |
| 122 | * caller-specified size. |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 123 | */ |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 124 | int |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | ltp_clone_malloc(unsigned long clone_flags, int (*fn) (void *arg), void *arg, |
| 126 | size_t stack_size) |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 127 | { |
Garrett Cooper | 619e6a7 | 2010-12-16 05:15:14 -0800 | [diff] [blame] | 128 | void *stack; |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 129 | int ret; |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 130 | int saved_errno; |
| 131 | |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 132 | stack = malloc(stack_size); |
| 133 | if (stack == NULL) |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 134 | return -1; |
| 135 | |
| 136 | ret = ltp_clone(clone_flags, fn, arg, stack_size, stack); |
| 137 | |
| 138 | if (ret == -1) { |
| 139 | saved_errno = errno; |
| 140 | free(stack); |
| 141 | errno = saved_errno; |
| 142 | } |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 147 | /* |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 148 | * ltp_clone_quick: calls ltp_clone_malloc with predetermined stack size. |
| 149 | * Experience thus far suggests that one page is often insufficient, |
Cyril Hrubis | e97ba6f | 2012-02-23 17:44:51 +0100 | [diff] [blame] | 150 | * while 6*getpagesize() seems adequate. |
Wanlong Gao | 2b9f501 | 2012-12-25 16:42:54 +0800 | [diff] [blame] | 151 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | int ltp_clone_quick(unsigned long clone_flags, int (*fn) (void *arg), void *arg) |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 153 | { |
Cyril Hrubis | e97ba6f | 2012-02-23 17:44:51 +0100 | [diff] [blame] | 154 | size_t stack_size = getpagesize() * 6; |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 155 | |
| 156 | return ltp_clone_malloc(clone_flags, fn, arg, stack_size); |
Cyril Hrubis | e97ba6f | 2012-02-23 17:44:51 +0100 | [diff] [blame] | 157 | } |