OSDN Git Service

* libltp/lib/tst_sig.c: Pass SIGSEGV to application to consider
[pf3gnuchains/pf3gnuchains3x.git] / winsup / testsuite / winsup.api / ltp / wait402.c
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 /*
21  * NAME
22  *      wait402.c
23  *
24  * DESCRIPTION
25  *      wait402 - check for ECHILD errno when using an illegal pid value
26  *
27  * ALGORITHM
28  *      loop if that option was specified
29  *      issue the system call with an illegal pid value
30  *      check the errno value
31  *        issue a PASS message if we get ECHILD
32  *      otherwise, the tests fails
33  *        issue a FAIL message
34  *        break any remaining tests
35  *        call cleanup
36  *
37  * USAGE:  <for command-line>
38  *  wait402 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
39  *      where,  -c n : Run n copies concurrently.
40  *              -e   : Turn on errno logging.
41  *              -i n : Execute test n times.
42  *              -I x : Execute test for x seconds.
43  *              -P x : Pause for x seconds between iterations.
44  *              -t   : Turn on syscall timing.
45  *
46  * History
47  *      07/2001 John George
48  *              -Ported
49  *
50  * Restrictions
51  *      none
52  */
53
54 #include "test.h"
55 #include "usctest.h"
56
57 #include <errno.h>
58 #define _USE_BSD
59 #include <sys/types.h>
60 #include <sys/resource.h>
61 #include <sys/wait.h>
62
63 /*
64  * See the Makefile for comments about the following preprocessor code.
65  */
66 #if defined (__CYGWIN__)
67 #define PID_MAX 0xfffffffd
68 #else
69 #ifndef _LTP_TASKS_H
70 #include <linux/threads.h>      /* for PID_MAX value - new */
71 #else
72 #include <linux/tasks.h>        /* for PID_MAX value - old */
73 #endif
74 #endif
75
76 void cleanup(void);
77 void setup(void);
78
79 char *TCID= "wait402()";
80 int TST_TOTAL = 1;
81 extern int Tst_count;
82
83 int exp_enos[] = {10, 0};
84
85 main(int ac, char **av)
86 {
87         int lc;                         /* loop counter */
88         const char *msg;                /* message returned from parse_opts */
89         pid_t pid;
90         pid_t epid = PID_MAX + 1;
91         int status = 1;
92         struct rusage *rusage=NULL;
93
94         /* parse standard options */
95         if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *) NULL) {
96                 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
97         }
98
99         setup();                        /* global setup */
100
101         /* The following loop checks looping state if -i option given */
102
103         for (lc = 0; TEST_LOOPING(lc); lc++) {
104                 /* reset Tst_count in case we are looping */
105                 Tst_count = 0;
106
107
108                 /*
109                  * Allocate some space for the rusage structure.
110                  */
111         
112                 if ((rusage = (struct rusage *)malloc(sizeof(struct rusage)))
113                                 == NULL) {
114                         tst_brkm(TBROK, cleanup, "malloc() failed");
115                 }
116         
117                 pid = fork();
118         
119                 if (pid == -1) {
120                         tst_brkm(TBROK, cleanup, "fork() failed");
121                 }
122         
123                 if (pid == 0) {         /* this is the child */
124                         /*
125                          * sleep for a moment to let us do the test
126                          */
127                         sleep(1);
128                         exit(0);
129                 } else {                /* this is the parent */
130                         /*
131                          * call wait4 with the TEST() macro.  epid is set
132                          * to an illegal positive value.  This should give
133                          * an ECHILD error.
134                          */
135                         TEST(wait4(epid, &status, 0, rusage));
136                 }
137         
138                 if (TEST_RETURN == 0) {
139                         tst_brkm(TFAIL, cleanup, "call failed to produce expected error - errno = %d - %s", TEST_ERRNO, strerror(TEST_ERRNO));
140                 }
141
142                 TEST_ERROR_LOG(TEST_ERRNO);
143         
144                 switch (TEST_ERRNO) {
145                 case ECHILD:
146                         tst_resm(TPASS, "received expected failure - errno = %d - %s",
147                                 TEST_ERRNO, strerror(TEST_ERRNO));
148                         break;
149                 default:
150                         tst_brkm(TFAIL, cleanup, "call failed to produce expected "
151                                 "error - errno = %d - %s", TEST_ERRNO,
152                                 strerror(TEST_ERRNO));
153                 }
154
155                 /*
156                  * Clean up things in case we are looping.
157                  */
158                 if (pid > 0) {
159                         wait4(pid, &status, 0, rusage);
160                 }
161                 free(rusage);
162                 rusage = NULL;
163         }
164
165         cleanup();
166
167         /*NOTREACHED*/
168 }
169
170 /*
171  * setup() - performs all the ONE TIME setup for this test.
172  */
173 void
174 setup(void)
175 {
176         /* capture signals */
177         tst_sig(FORK, DEF_HANDLER, cleanup);
178
179         /* Set up the expected error numbers for -e option */
180         TEST_EXP_ENOS(exp_enos);
181
182         /* Pause if that option was specified */
183         TEST_PAUSE;
184 }
185
186 /*
187  * cleanup() - performs all the ONE TIME cleanup for this test at completion
188  *             or premature exit.
189  */
190 void
191 cleanup(void)
192 {
193         /*
194          * print timing stats if that option was specified.
195          * print errno log if that option was specified.
196          */
197         TEST_CLEANUP;
198
199         /* exit with return code appropriate for results */
200         tst_exit();
201 }
202