blob: 4b2e04ee7a5672ad6207b135d7766441035712c6 [file] [log] [blame]
robbiewc72e43f2003-08-01 15:46:23 +00001/*
2 * Copyright (c) International Business Machines Corp., 2003.
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +08003 * Copyright (c) 2012 Wanlong Gao <gaowanlong@cn.fujitsu.com>
robbiewc72e43f2003-08-01 15:46:23 +00004 *
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 along
Wanlong Gaofed96412012-10-24 10:10:29 +080014 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiewc72e43f2003-08-01 15:46:23 +000016 *
17 */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080018/*
robbiewc72e43f2003-08-01 15:46:23 +000019 * This is a test for a glibc bug for the clone(2) system call.
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080020 */
robbiewc72e43f2003-08-01 15:46:23 +000021
robbiewd34d5812005-07-11 22:28:09 +000022#if defined UCLINUX && !__THROW
23/* workaround for libc bug */
24#define __THROW
25#endif
26
robbiewc72e43f2003-08-01 15:46:23 +000027#include <errno.h>
28#include <sched.h>
29#include <sys/wait.h>
30#include "test.h"
robbiewf41288e2003-10-10 15:32:03 +000031#include "clone_platform.h"
robbiewc72e43f2003-08-01 15:46:23 +000032
robbiewc72e43f2003-08-01 15:46:23 +000033#define TRUE 1
34#define FALSE 0
35
robbiewc72e43f2003-08-01 15:46:23 +000036static void setup();
robbiewc72e43f2003-08-01 15:46:23 +000037static int do_child();
38
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080039char *TCID = "clone07";
40int TST_TOTAL = 1;
robbiewc72e43f2003-08-01 15:46:23 +000041
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080042static void sigsegv_handler(int);
43static void sigusr2_handler(int);
robbiewc72e43f2003-08-01 15:46:23 +000044static int child_pid;
45static int fail = FALSE;
46
subrata_modak56207ce2009-03-23 13:35:39 +000047int main(int ac, char **av)
robbiewc72e43f2003-08-01 15:46:23 +000048{
49
Jan Stancek443d16e2012-12-03 13:19:57 +010050 int lc, status;
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080051 void *child_stack;
subrata_modak56207ce2009-03-23 13:35:39 +000052
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010053 tst_parse_opts(ac, av, NULL, NULL);
robbiewc72e43f2003-08-01 15:46:23 +000054
robbiewc72e43f2003-08-01 15:46:23 +000055 setup();
56
subrata_modak56207ce2009-03-23 13:35:39 +000057 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080058 tst_count = 0;
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080059 child_stack = malloc(CHILD_STACK_SIZE);
60 if (child_stack == NULL)
Han Pingtian0f89b022016-04-14 16:46:33 +080061 tst_brkm(TBROK, NULL,
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080062 "Cannot allocate stack for child");
subrata_modak9d718392009-04-13 14:35:12 +000063
vapierf6d7f092009-11-03 20:07:35 +000064 child_pid = ltp_clone(SIGCHLD, do_child, NULL,
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080065 CHILD_STACK_SIZE, child_stack);
Han Pingtian0f89b022016-04-14 16:46:33 +080066
67 if (child_pid < 0)
68 tst_brkm(TBROK | TERRNO, NULL, "clone failed");
69
Jan Stancek443d16e2012-12-03 13:19:57 +010070 if ((wait(&status)) == -1)
Han Pingtian0f89b022016-04-14 16:46:33 +080071 tst_brkm(TBROK | TERRNO, NULL,
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080072 "wait failed, status: %d", status);
73
robbiewc72e43f2003-08-01 15:46:23 +000074 free(child_stack);
Garrett Cooper2c282152010-12-16 00:55:50 -080075 }
robbiewc72e43f2003-08-01 15:46:23 +000076
subrata_modak56207ce2009-03-23 13:35:39 +000077 if (fail == FALSE)
78 tst_resm(TPASS,
79 "Use of return() in child did not cause SIGSEGV");
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080080 else
subrata_modak56207ce2009-03-23 13:35:39 +000081 tst_resm(TFAIL, "Use of return() in child caused SIGSEGV");
subrata_modak56207ce2009-03-23 13:35:39 +000082
Garrett Cooperfbb20142010-12-17 01:57:48 -080083 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080084}
robbiewc72e43f2003-08-01 15:46:23 +000085
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080086static void setup(void)
robbiewc72e43f2003-08-01 15:46:23 +000087{
88 struct sigaction def_act;
89 struct sigaction act;
90
robbiewc72e43f2003-08-01 15:46:23 +000091 TEST_PAUSE;
92
subrata_modakbdbaec52009-02-26 12:14:51 +000093 act.sa_handler = sigsegv_handler;
robbiewc72e43f2003-08-01 15:46:23 +000094 act.sa_flags = SA_RESTART;
Jan Stancekcbec02d2013-04-02 12:16:58 +020095 sigemptyset(&act.sa_mask);
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080096 if ((sigaction(SIGSEGV, &act, NULL)) == -1)
97 tst_resm(TWARN | TERRNO,
subrata_modak56207ce2009-03-23 13:35:39 +000098 "sigaction() for SIGSEGV failed in test_setup()");
robbiewc72e43f2003-08-01 15:46:23 +000099
subrata_modak56207ce2009-03-23 13:35:39 +0000100 /* Setup signal handler for SIGUSR2 */
101 def_act.sa_handler = sigusr2_handler;
102 def_act.sa_flags = SA_RESTART | SA_RESETHAND;
Jan Stancekcbec02d2013-04-02 12:16:58 +0200103 sigemptyset(&def_act.sa_mask);
robbiewc72e43f2003-08-01 15:46:23 +0000104
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800105 if ((sigaction(SIGUSR2, &def_act, NULL)) == -1)
106 tst_resm(TWARN | TERRNO,
subrata_modak56207ce2009-03-23 13:35:39 +0000107 "sigaction() for SIGUSR2 failed in test_setup()");
Garrett Cooper2c282152010-12-16 00:55:50 -0800108}
robbiewc72e43f2003-08-01 15:46:23 +0000109
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800110static int do_child(void)
robbiewc72e43f2003-08-01 15:46:23 +0000111{
112 return 0;
113}
114
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800115static void sigsegv_handler(int sig)
robbiewc72e43f2003-08-01 15:46:23 +0000116{
subrata_modak56207ce2009-03-23 13:35:39 +0000117 if (child_pid == 0) {
118 kill(getppid(), SIGUSR2);
119 _exit(42);
120 }
robbiewc72e43f2003-08-01 15:46:23 +0000121}
122
123/* sig_default_handler() - Default handler for parent */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800124static void sigusr2_handler(int sig)
robbiewc72e43f2003-08-01 15:46:23 +0000125{
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800126 if (child_pid != 0)
subrata_modak56207ce2009-03-23 13:35:39 +0000127 fail = TRUE;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700128}