1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**********************************************************
18  *
19  *    TEST IDENTIFIER	: prctl02
20  *
21  *    EXECUTED BY	: anyone
22  *
23  *    TEST TITLE	: Tests for error conditions
24  *
25  *    TEST CASE TOTAL	: 2
26  *
27  *    AUTHOR		: Saji Kumar.V.R <saji.kumar@wipro.com>
28  *
29  *    SIGNALS
30  * 	Uses SIGUSR1 to pause before test if option set.
31  * 	(See the parse_opts(3) man page).
32  *
33  *    DESCRIPTION
34  *	Verify that
35  *	1) prctl() fails with errno, EINVAL when an invalid value is given for
36  *	   option
37  *	2) prctl() fails with errno, EINVAL when option is PR_SET_PDEATHSIG
38  *	   & arg2 is not zero or a valid signal number
39  *
40  * 	Setup:
41  * 	  Setup signal handling.
42  *	  Pause for SIGUSR1 if option specified.
43  *
44  * 	Test:
45  *	 Loop if the proper options are given.
46  *	 fork a child
47  *
48  *	 CHILD:
49  *		call prctl() with proper arguments
50  *		If call fails with expected errno,
51  *			exit with 0
52  *		else
53  *			exit with 1
54  *	 PARENT:
55  *		wait() for child.
56  *		If child exits with exit value 0,
57  *			Test passed
58  *		else
59  *			Test Failed
60  *
61  * 	Cleanup:
62  * 	  Print errno log and/or timing stats if options given
63  *
64  * USAGE:  <for command-line>
65  *  prctl02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
66  *			where,  -c n : Run n copies concurrently.
67  *				-e   : Turn on errno logging.
68  *				-h   : Show help screen
69  *				-f   : Turn off functional testing
70  *				-i n : Execute test n times.
71  *				-I x : Execute test for x seconds.
72  *				-p   : Pause for SIGUSR1 before starting
73  *				-P x : Pause for x seconds between iterations.
74  *				-t   : Turn on syscall timing.
75  *
76  ****************************************************************/
77 
78 #include <errno.h>
79 #include <signal.h>
80 #include <sys/prctl.h>
81 #include <sys/wait.h>
82 
83 #include "test.h"
84 
85 #define OPTION_INVALID 999
86 #define INVALID_ARG 999
87 
88 static void setup(void);
89 static void cleanup(void);
90 
91 char *TCID = "prctl02";
92 
93 struct test_cases_t {
94 	int option;
95 	unsigned long arg2;
96 	int exp_errno;
97 } test_cases[] = {
98 	{
99 	OPTION_INVALID, 0, EINVAL}, {
100 	PR_SET_PDEATHSIG, INVALID_ARG, EINVAL}
101 };
102 
103 int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
104 
105 int main(int ac, char **av)
106 {
107 
108 	int lc, i;
109 	pid_t child_pid;
110 	int status;
111 
112 	tst_parse_opts(ac, av, NULL, NULL);
113 
114 	setup();
115 
116 	for (lc = 0; TEST_LOOPING(lc); lc++) {
117 
118 		tst_count = 0;
119 
120 		for (i = 0; i < TST_TOTAL; ++i) {
121 
122 			switch (child_pid = FORK_OR_VFORK()) {
123 
124 			case -1:
125 				/* fork() failed */
126 				tst_resm(TFAIL, "fork() failed");
127 				continue;
128 
129 			case 0:
130 				/* Child */
131 
132 				TEST(prctl(test_cases[i].option,
133 					   test_cases[i].arg2));
134 				if ((TEST_RETURN == -1) && (TEST_ERRNO ==
135 							    test_cases
136 							    [i].exp_errno)) {
137 					exit(TEST_ERRNO);
138 				} else {
139 					tst_resm(TWARN | TTERRNO,
140 						 "prctl() returned %ld",
141 						 TEST_RETURN);
142 					exit(TEST_ERRNO);
143 				}
144 
145 			default:
146 				/* Parent */
147 				if ((waitpid(child_pid, &status, 0)) < 0) {
148 					tst_resm(TFAIL, "waitpid() failed");
149 					continue;
150 				}
151 
152 				if ((WIFEXITED(status)) && (WEXITSTATUS(status)
153 							    ==
154 							    test_cases
155 							    [i].exp_errno)) {
156 					tst_resm(TPASS, "Test Passed");
157 				} else {
158 					tst_resm(TFAIL, "Test Failed");
159 				}
160 
161 			}
162 		}
163 	}
164 
165 	/* cleanup and exit */
166 	cleanup();
167 
168 	tst_exit();
169 
170 }
171 
172 /* setup() - performs all ONE TIME setup for this test */
173 void setup(void)
174 {
175 
176 	tst_sig(FORK, DEF_HANDLER, cleanup);
177 
178 	TEST_PAUSE;
179 
180 }
181 
182 /*
183  *cleanup() -  performs all ONE TIME cleanup for this test at
184  *		completion or premature exit.
185  */
186 void cleanup(void)
187 {
188 
189 }
190