1/*
2 * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3 * Copyright (c) 2012 Wanlong Gao <gaowanlong@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program;  if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 */
18 /*
19  *     Verify that,
20  *      clone(2) returns -1 and sets errno to EINVAL if
21  *     child stack is set to a zero value(NULL)
22  */
23
24#if defined UCLINUX && !__THROW
25/* workaround for libc bug */
26#define __THROW
27#endif
28
29#include <sched.h>
30#include <errno.h>
31#include <sys/wait.h>
32#include "test.h"
33#include "clone_platform.h"
34
35static void cleanup(void);
36static void setup(void);
37static int child_fn();
38
39static void *child_stack;
40
41static struct test_case_t {
42	int (*child_fn) ();
43	void **child_stack;
44	int exp_errno;
45	char err_desc[10];
46} test_cases[] = {
47	{
48child_fn, NULL, EINVAL, "EINVAL"},};
49
50char *TCID = "clone04";
51int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
52
53int main(int ac, char **av)
54{
55	int lc, ind;
56	void *test_stack;
57
58	tst_parse_opts(ac, av, NULL, NULL);
59
60	setup();
61
62	for (lc = 0; TEST_LOOPING(lc); lc++) {
63		tst_count = 0;
64
65		for (ind = 0; ind < TST_TOTAL; ind++) {
66			if (test_cases[ind].child_stack == NULL) {
67				test_stack = NULL;
68			} else if (*test_cases[ind].child_stack == NULL) {
69				tst_resm(TWARN, "Can not allocate stack for"
70					 "child, skipping test case");
71				continue;
72			} else {
73				test_stack = child_stack;
74			}
75
76			TEST(ltp_clone(0, test_cases[ind].child_fn, NULL,
77				       CHILD_STACK_SIZE, test_stack));
78
79			if ((TEST_RETURN == -1) &&
80			    (TEST_ERRNO == test_cases[ind].exp_errno)) {
81				tst_resm(TPASS, "expected failure; Got %s",
82					 test_cases[ind].err_desc);
83			} else {
84				tst_resm(TFAIL | TTERRNO,
85					 "Call failed to produce expected error; "
86					 "expected errno %d and result -1; got result %ld",
87					 test_cases[ind].exp_errno,
88					 TEST_RETURN);
89			}
90		}
91	}
92
93	cleanup();
94	tst_exit();
95}
96
97static void setup(void)
98{
99	tst_sig(NOFORK, DEF_HANDLER, cleanup);
100	TEST_PAUSE;
101
102	child_stack = malloc(CHILD_STACK_SIZE);
103}
104
105static void cleanup(void)
106{
107	free(child_stack);
108}
109
110static int child_fn(void)
111{
112	exit(1);
113}
114