robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2003. |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 3 | * Copyright (c) 2012 Wanlong Gao <gaowanlong@cn.fujitsu.com> |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 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 along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 16 | * |
| 17 | */ |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 18 | /* |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 19 | * This is a test for a glibc bug for the clone(2) system call. |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 20 | */ |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 21 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 22 | #if defined UCLINUX && !__THROW |
| 23 | /* workaround for libc bug */ |
| 24 | #define __THROW |
| 25 | #endif |
| 26 | |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <sched.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include "test.h" |
robbiew | f41288e | 2003-10-10 15:32:03 +0000 | [diff] [blame] | 31 | #include "clone_platform.h" |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 32 | |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 33 | #define TRUE 1 |
| 34 | #define FALSE 0 |
| 35 | |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 36 | static void setup(); |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 37 | static int do_child(); |
| 38 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 39 | char *TCID = "clone07"; |
| 40 | int TST_TOTAL = 1; |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 41 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 42 | static void sigsegv_handler(int); |
| 43 | static void sigusr2_handler(int); |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 44 | static int child_pid; |
| 45 | static int fail = FALSE; |
| 46 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 47 | int main(int ac, char **av) |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 48 | { |
| 49 | |
Jan Stancek | 443d16e | 2012-12-03 13:19:57 +0100 | [diff] [blame] | 50 | int lc, status; |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 51 | void *child_stack; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 52 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 53 | tst_parse_opts(ac, av, NULL, NULL); |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 54 | |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 55 | setup(); |
| 56 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 57 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 58 | tst_count = 0; |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 59 | child_stack = malloc(CHILD_STACK_SIZE); |
| 60 | if (child_stack == NULL) |
Han Pingtian | 0f89b02 | 2016-04-14 16:46:33 +0800 | [diff] [blame] | 61 | tst_brkm(TBROK, NULL, |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 62 | "Cannot allocate stack for child"); |
subrata_modak | 9d71839 | 2009-04-13 14:35:12 +0000 | [diff] [blame] | 63 | |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 64 | child_pid = ltp_clone(SIGCHLD, do_child, NULL, |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 65 | CHILD_STACK_SIZE, child_stack); |
Han Pingtian | 0f89b02 | 2016-04-14 16:46:33 +0800 | [diff] [blame] | 66 | |
| 67 | if (child_pid < 0) |
| 68 | tst_brkm(TBROK | TERRNO, NULL, "clone failed"); |
| 69 | |
Jan Stancek | 443d16e | 2012-12-03 13:19:57 +0100 | [diff] [blame] | 70 | if ((wait(&status)) == -1) |
Han Pingtian | 0f89b02 | 2016-04-14 16:46:33 +0800 | [diff] [blame] | 71 | tst_brkm(TBROK | TERRNO, NULL, |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 72 | "wait failed, status: %d", status); |
| 73 | |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 74 | free(child_stack); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 75 | } |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 76 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | if (fail == FALSE) |
| 78 | tst_resm(TPASS, |
| 79 | "Use of return() in child did not cause SIGSEGV"); |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 80 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | tst_resm(TFAIL, "Use of return() in child caused SIGSEGV"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | |
Garrett Cooper | fbb2014 | 2010-12-17 01:57:48 -0800 | [diff] [blame] | 83 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 84 | } |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 85 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 86 | static void setup(void) |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 87 | { |
| 88 | struct sigaction def_act; |
| 89 | struct sigaction act; |
| 90 | |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 91 | TEST_PAUSE; |
| 92 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 93 | act.sa_handler = sigsegv_handler; |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 94 | act.sa_flags = SA_RESTART; |
Jan Stancek | cbec02d | 2013-04-02 12:16:58 +0200 | [diff] [blame] | 95 | sigemptyset(&act.sa_mask); |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 96 | if ((sigaction(SIGSEGV, &act, NULL)) == -1) |
| 97 | tst_resm(TWARN | TERRNO, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | "sigaction() for SIGSEGV failed in test_setup()"); |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 99 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | /* Setup signal handler for SIGUSR2 */ |
| 101 | def_act.sa_handler = sigusr2_handler; |
| 102 | def_act.sa_flags = SA_RESTART | SA_RESETHAND; |
Jan Stancek | cbec02d | 2013-04-02 12:16:58 +0200 | [diff] [blame] | 103 | sigemptyset(&def_act.sa_mask); |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 104 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 105 | if ((sigaction(SIGUSR2, &def_act, NULL)) == -1) |
| 106 | tst_resm(TWARN | TERRNO, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | "sigaction() for SIGUSR2 failed in test_setup()"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 108 | } |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 109 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 110 | static int do_child(void) |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 111 | { |
| 112 | return 0; |
| 113 | } |
| 114 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 115 | static void sigsegv_handler(int sig) |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 116 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | if (child_pid == 0) { |
| 118 | kill(getppid(), SIGUSR2); |
| 119 | _exit(42); |
| 120 | } |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* sig_default_handler() - Default handler for parent */ |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 124 | static void sigusr2_handler(int sig) |
robbiew | c72e43f | 2003-08-01 15:46:23 +0000 | [diff] [blame] | 125 | { |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 126 | if (child_pid != 0) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | fail = TRUE; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 128 | } |