summaryrefslogtreecommitdiff
path: root/ltp_framework/lib/cloner.c
blob: bf9eed15728f80baaea63fbb901d079a26f258f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (c) International Business Machines Corp., 2009
 * Some wrappers for clone functionality.  Thrown together by Serge Hallyn
 * <serue@us.ibm.com> based on existing clone usage in ltp.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <errno.h>
#include <unistd.h> /* fork, getpid, sleep */
#include <string.h>
#include <stdlib.h> /* exit */
#include <sched.h> /* clone */
#include "test.h"

#undef clone /* we want to use clone() */

/* copied from several other files under ltp */
#if defined (__s390__) || (__s390x__)
#define clone __clone
extern int __clone(int(void*),void*,int,void*);
#elif defined(__ia64__)
#define clone2 __clone2
/* Prototype provided by David Mosberger				*/
extern int  __clone2(int (*fn) (void *arg), void *child_stack_base,
		size_t child_stack_size, int flags, void *arg,
		pid_t *parent_tid, void *tls, pid_t *child_tid);
#endif

/***********************************************************************
 * ltp_clone: wrapper for clone to hide the architecture dependencies.
 *   1. hppa takes bottom of stack and no stacksize (stack grows up)
 *   2. __ia64__ takes bottom of stack and uses clone2
 *   3. all others take top of stack (stack grows down)
 ***********************************************************************/
int
ltp_clone(unsigned long clone_flags, int (*fn)(void *arg), void *arg,
		size_t stack_size, void *stack)
{
	int ret;

#if defined(__hppa__)
	ret = clone(fn, stack, clone_flags, arg);
#elif defined(__ia64__)
	ret = clone2(fn, stack, stack_size, clone_flags, arg, NULL, NULL, NULL);
#else
	/*
	 * For archs where stack grows downwards, stack points to the topmost
	 * address of the memory space set up for the child stack.
	 */
	ret = clone(fn, (stack ? stack + stack_size : NULL),
			clone_flags, arg);
#endif

	return ret;
}

/***********************************************************************
 * ltp_clone_malloc: also does the memory allocation for clone with a
 * caller-specified size.
 ***********************************************************************/
int
ltp_clone_malloc(unsigned long clone_flags, int (*fn)(void *arg), void *arg,
		size_t stack_size)
{
	void *stack;
	int ret;
	int saved_errno;

	if ((stack = malloc(stack_size)) == NULL)
		return -1;

	ret = ltp_clone(clone_flags, fn, arg, stack_size, stack);

	if (ret == -1) {
		saved_errno = errno;
		free(stack);
		errno = saved_errno;
	}

	return ret;
}

/***********************************************************************
 * ltp_clone_quick: calls ltp_clone_malloc with predetermined stack size.
 * Experience thus far suggests that one page is often insufficient,
 * while 4*getpagesize() seems adequate.
 ***********************************************************************/
int
ltp_clone_quick(unsigned long clone_flags, int (*fn)(void *arg), void *arg)
{
	size_t stack_size = getpagesize() * 4;

	return ltp_clone_malloc(clone_flags, fn, arg, stack_size);
}