OSDN Git Service

Merge branch 'master' of git://github.com/monaka/binutils
[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) __attribute__((noreturn));
77 void setup(void);
78
79 const char *TCID= "wait402()";
80 int TST_TOTAL = 1;
81 extern int Tst_count;
82
83 int exp_enos[] = {10, 0};
84
85 int
86 main(int ac, char **av)
87 {
88         int lc;                         /* loop counter */
89         const char *msg;                /* message returned from parse_opts */
90         pid_t pid;
91         pid_t epid = PID_MAX + 1;
92         int status = 1;
93         struct rusage *rusage=NULL;
94
95         /* parse standard options */
96         if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *) NULL) {
97                 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
98         }
99
100         setup();                        /* global setup */
101
102         /* The following loop checks looping state if -i option given */
103
104         for (lc = 0; TEST_LOOPING(lc); lc++) {
105                 /* reset Tst_count in case we are looping */
106                 Tst_count = 0;
107
108
109                 /*
110                  * Allocate some space for the rusage structure.
111                  */
112         
113                 if ((rusage = (struct rusage *)malloc(sizeof(struct rusage)))
114                                 == NULL) {
115                         tst_brkm(TBROK, cleanup, "malloc() failed");
116                 }
117         
118                 pid = fork();
119         
120                 if (pid == -1) {
121                         tst_brkm(TBROK, cleanup, "fork() failed");
122                 }
123         
124                 if (pid == 0) {         /* this is the child */
125                         /*
126                          * sleep for a moment to let us do the test
127                          */
128                         sleep(1);
129                         exit(0);
130                 } else {                /* this is the parent */
131                         /*
132                          * call wait4 with the TEST() macro.  epid is set
133                          * to an illegal positive value.  This should give
134                          * an ECHILD error.
135                          */
136                         TEST(wait4(epid, &status, 0, rusage));
137                 }
138         
139                 if (TEST_RETURN == 0) {
140                         tst_brkm(TFAIL, cleanup, "call failed to produce expected error - errno = %d - %s", TEST_ERRNO, strerror(TEST_ERRNO));
141                 }
142
143                 TEST_ERROR_LOG(TEST_ERRNO);
144         
145                 switch (TEST_ERRNO) {
146                 case ECHILD:
147                         tst_resm(TPASS, "received expected failure - errno = %d - %s",
148                                 TEST_ERRNO, strerror(TEST_ERRNO));
149                         break;
150                 default:
151                         tst_brkm(TFAIL, cleanup, "call failed to produce expected "
152                                 "error - errno = %d - %s", TEST_ERRNO,
153                                 strerror(TEST_ERRNO));
154                 }
155
156                 /*
157                  * Clean up things in case we are looping.
158                  */
159                 if (pid > 0) {
160                         wait4(pid, &status, 0, rusage);
161                 }
162                 free(rusage);
163                 rusage = NULL;
164         }
165
166         cleanup();
167
168         /*NOTREACHED*/
169 }
170
171 /*
172  * setup() - performs all the ONE TIME setup for this test.
173  */
174 void
175 setup(void)
176 {
177         /* capture signals */
178         tst_sig(FORK, DEF_HANDLER, cleanup);
179
180         /* Set up the expected error numbers for -e option */
181         TEST_EXP_ENOS(exp_enos);
182
183         /* Pause if that option was specified */
184         TEST_PAUSE;
185 }
186
187 /*
188  * cleanup() - performs all the ONE TIME cleanup for this test at completion
189  *             or premature exit.
190  */
191 void
192 cleanup(void)
193 {
194         /*
195          * print timing stats if that option was specified.
196          * print errno log if that option was specified.
197          */
198         TEST_CLEANUP;
199
200         /* exit with return code appropriate for results */
201         tst_exit();
202 }
203