nsprpub/pr/tests/forktest.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /***********************************************************************
michael@0 7 **
michael@0 8 ** Name: forktest.c
michael@0 9 **
michael@0 10 ** Description: UNIX test for fork functions.
michael@0 11 **
michael@0 12 ** Modification History:
michael@0 13 ** 15-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
michael@0 14 ** The debug mode will print all of the printfs associated with this test.
michael@0 15 ** The regress mode will be the default mode. Since the regress tool limits
michael@0 16 ** the output to a one line status:PASS or FAIL,all of the printf statements
michael@0 17 ** have been handled with an if (debug_mode) statement.
michael@0 18 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
michael@0 19 ** recognize the return code from tha main program.
michael@0 20 ** 12-June-97 AGarcic - Revert to return code 0 and 1, remove debug option (obsolete).
michael@0 21 ***********************************************************************/
michael@0 22
michael@0 23 /***********************************************************************
michael@0 24 ** Includes
michael@0 25 ***********************************************************************/
michael@0 26 /* Used to get the command line option */
michael@0 27 #include "plgetopt.h"
michael@0 28
michael@0 29 #include "nspr.h"
michael@0 30 #include <string.h>
michael@0 31 #include <stdio.h>
michael@0 32 #include <stdlib.h>
michael@0 33
michael@0 34 PRIntn failed_already=0;
michael@0 35
michael@0 36 #ifdef XP_UNIX
michael@0 37
michael@0 38 #include <sys/types.h>
michael@0 39 #include <sys/wait.h>
michael@0 40 #include <unistd.h>
michael@0 41 #include <errno.h>
michael@0 42
michael@0 43 static char *message = "Hello world!";
michael@0 44
michael@0 45 static void
michael@0 46 ClientThreadFunc(void *arg)
michael@0 47 {
michael@0 48 PRNetAddr addr;
michael@0 49 PRFileDesc *sock = NULL;
michael@0 50 PRInt32 tmp = (PRInt32)arg;
michael@0 51
michael@0 52 /*
michael@0 53 * Make sure the PR_Accept call will block
michael@0 54 */
michael@0 55
michael@0 56 printf("Wait one second before connect\n");
michael@0 57 fflush(stdout);
michael@0 58 PR_Sleep(PR_SecondsToInterval(1));
michael@0 59
michael@0 60 addr.inet.family = AF_INET;
michael@0 61 addr.inet.ip = PR_htonl(INADDR_ANY);
michael@0 62 addr.inet.port = 0;
michael@0 63 if ((sock = PR_NewTCPSocket()) == NULL) {
michael@0 64 fprintf(stderr, "failed to create TCP socket: error code %d\n",
michael@0 65 PR_GetError());
michael@0 66 failed_already = 1;
michael@0 67 goto finish;
michael@0 68 }
michael@0 69 if (PR_Bind(sock, &addr) != PR_SUCCESS) {
michael@0 70 fprintf(stderr, "PR_Bind failed: error code %d\n",
michael@0 71 PR_GetError());
michael@0 72 failed_already = 1;
michael@0 73 goto finish;
michael@0 74 }
michael@0 75 addr.inet.ip = PR_htonl(INADDR_LOOPBACK);
michael@0 76 addr.inet.port = PR_htons((PRInt16)tmp);
michael@0 77 printf("Connecting to port %hu\n", PR_ntohs(addr.inet.port));
michael@0 78 fflush(stdout);
michael@0 79 if (PR_Connect(sock, &addr, PR_SecondsToInterval(5)) !=
michael@0 80 PR_SUCCESS) {
michael@0 81 fprintf(stderr, "PR_Connect failed: error code %d\n",
michael@0 82 PR_GetError());
michael@0 83 failed_already = 1;
michael@0 84 goto finish;
michael@0 85 }
michael@0 86 printf("Writing message \"%s\"\n", message);
michael@0 87 fflush(stdout);
michael@0 88 if (PR_Send(sock, message, strlen(message) + 1, 0, PR_INTERVAL_NO_TIMEOUT) ==
michael@0 89 -1) {
michael@0 90 fprintf(stderr, "PR_Send failed: error code %d\n",
michael@0 91 PR_GetError());
michael@0 92 failed_already = 1;
michael@0 93 goto finish;
michael@0 94 }
michael@0 95 finish:
michael@0 96 if (sock) {
michael@0 97 PR_Close(sock);
michael@0 98 }
michael@0 99 return;
michael@0 100 }
michael@0 101
michael@0 102 /*
michael@0 103 * DoIO --
michael@0 104 * This function creates a thread that acts as a client and itself.
michael@0 105 * acts as a server. Then it joins the client thread.
michael@0 106 */
michael@0 107 static void
michael@0 108 DoIO(void)
michael@0 109 {
michael@0 110 PRThread *clientThread;
michael@0 111 PRFileDesc *listenSock = NULL;
michael@0 112 PRFileDesc *sock = NULL;
michael@0 113 PRNetAddr addr;
michael@0 114 PRInt32 nBytes;
michael@0 115 char buf[128];
michael@0 116
michael@0 117 listenSock = PR_NewTCPSocket();
michael@0 118 if (!listenSock) {
michael@0 119 fprintf(stderr, "failed to create a TCP socket: error code %d\n",
michael@0 120 PR_GetError());
michael@0 121 failed_already = 1;
michael@0 122 goto finish;
michael@0 123 }
michael@0 124 addr.inet.family = AF_INET;
michael@0 125 addr.inet.ip = PR_htonl(INADDR_ANY);
michael@0 126 addr.inet.port = 0;
michael@0 127 if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
michael@0 128 fprintf(stderr, "failed to bind socket: error code %d\n",
michael@0 129 PR_GetError());
michael@0 130 failed_already = 1;
michael@0 131 goto finish;
michael@0 132 }
michael@0 133 if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
michael@0 134 fprintf(stderr, "failed to get socket port number: error code %d\n",
michael@0 135 PR_GetError());
michael@0 136 failed_already = 1;
michael@0 137 goto finish;
michael@0 138 }
michael@0 139 if (PR_Listen(listenSock, 5) == PR_FAILURE) {
michael@0 140 fprintf(stderr, "PR_Listen failed: error code %d\n",
michael@0 141 PR_GetError());
michael@0 142 failed_already = 1;
michael@0 143 goto finish;
michael@0 144 }
michael@0 145 clientThread = PR_CreateThread( PR_USER_THREAD, ClientThreadFunc,
michael@0 146 (void *) PR_ntohs(addr.inet.port), PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
michael@0 147 PR_JOINABLE_THREAD, 0);
michael@0 148 if (clientThread == NULL) {
michael@0 149 fprintf(stderr, "Cannot create client thread: (%d, %d)\n",
michael@0 150 PR_GetError(), PR_GetOSError());
michael@0 151 failed_already = 1;
michael@0 152 goto finish;
michael@0 153 }
michael@0 154 printf("Accepting connection at port %hu\n", PR_ntohs(addr.inet.port));
michael@0 155 fflush(stdout);
michael@0 156 sock = PR_Accept(listenSock, &addr, PR_SecondsToInterval(5));
michael@0 157 if (!sock) {
michael@0 158 fprintf(stderr, "PR_Accept failed: error code %d\n",
michael@0 159 PR_GetError());
michael@0 160 failed_already = 1;
michael@0 161 goto finish;
michael@0 162 }
michael@0 163 nBytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
michael@0 164 if (nBytes == -1) {
michael@0 165 fprintf(stderr, "PR_Recv failed: error code %d\n",
michael@0 166 PR_GetError());
michael@0 167 failed_already = 1;
michael@0 168 goto finish;
michael@0 169 }
michael@0 170
michael@0 171 /*
michael@0 172 * Make sure it has proper null byte to mark end of string
michael@0 173 */
michael@0 174
michael@0 175 buf[sizeof(buf) - 1] = '\0';
michael@0 176 printf("Received \"%s\" from the client\n", buf);
michael@0 177 fflush(stdout);
michael@0 178 if (!strcmp(buf, message)) {
michael@0 179 PR_JoinThread(clientThread);
michael@0 180
michael@0 181 printf("The message is received correctly\n");
michael@0 182 fflush(stdout);
michael@0 183 } else {
michael@0 184 fprintf(stderr, "The message should be \"%s\"\n",
michael@0 185 message);
michael@0 186 failed_already = 1;
michael@0 187 }
michael@0 188
michael@0 189 finish:
michael@0 190 if (listenSock) {
michael@0 191 PR_Close(listenSock);
michael@0 192 }
michael@0 193 if (sock) {
michael@0 194 PR_Close(sock);
michael@0 195 }
michael@0 196 return;
michael@0 197 }
michael@0 198
michael@0 199 #ifdef _PR_DCETHREADS
michael@0 200
michael@0 201 #include <syscall.h>
michael@0 202
michael@0 203 pid_t PR_UnixFork1(void)
michael@0 204 {
michael@0 205 pid_t parent = getpid();
michael@0 206 int rv = syscall(SYS_fork);
michael@0 207
michael@0 208 if (rv == -1) {
michael@0 209 return (pid_t) -1;
michael@0 210 } else {
michael@0 211 /* For each process, rv is the pid of the other process */
michael@0 212 if (rv == parent) {
michael@0 213 /* the child */
michael@0 214 return 0;
michael@0 215 } else {
michael@0 216 /* the parent */
michael@0 217 return rv;
michael@0 218 }
michael@0 219 }
michael@0 220 }
michael@0 221
michael@0 222 #elif defined(SOLARIS)
michael@0 223
michael@0 224 /*
michael@0 225 * It seems like that in Solaris 2.4 one must call fork1() if the
michael@0 226 * the child process is going to use thread functions. Solaris 2.5
michael@0 227 * doesn't have this problem. Calling fork() also works.
michael@0 228 */
michael@0 229
michael@0 230 pid_t PR_UnixFork1(void)
michael@0 231 {
michael@0 232 return fork1();
michael@0 233 }
michael@0 234
michael@0 235 #else
michael@0 236
michael@0 237 pid_t PR_UnixFork1(void)
michael@0 238 {
michael@0 239 return fork();
michael@0 240 }
michael@0 241
michael@0 242 #endif /* PR_DCETHREADS */
michael@0 243
michael@0 244 int main(int argc, char **argv)
michael@0 245 {
michael@0 246 pid_t pid;
michael@0 247 int rv;
michael@0 248
michael@0 249 /* main test program */
michael@0 250
michael@0 251 DoIO();
michael@0 252
michael@0 253 pid = PR_UnixFork1();
michael@0 254
michael@0 255 if (pid == (pid_t) -1) {
michael@0 256 fprintf(stderr, "Fork failed: errno %d\n", errno);
michael@0 257 failed_already=1;
michael@0 258 return 1;
michael@0 259 } else if (pid > 0) {
michael@0 260 int childStatus;
michael@0 261
michael@0 262 printf("Fork succeeded. Parent process continues.\n");
michael@0 263 DoIO();
michael@0 264 if ((rv = waitpid(pid, &childStatus, 0)) != pid) {
michael@0 265 #if defined(IRIX) && !defined(_PR_PTHREADS)
michael@0 266 /*
michael@0 267 * nspr may handle SIGCLD signal
michael@0 268 */
michael@0 269 if ((rv < 0) && (errno == ECHILD)) {
michael@0 270 } else
michael@0 271 #endif
michael@0 272 {
michael@0 273 fprintf(stderr, "waitpid failed: %d\n", errno);
michael@0 274 failed_already = 1;
michael@0 275 }
michael@0 276 } else if (!WIFEXITED(childStatus)
michael@0 277 || WEXITSTATUS(childStatus) != 0) {
michael@0 278 failed_already = 1;
michael@0 279 }
michael@0 280 printf("Parent process exits.\n");
michael@0 281 if (!failed_already) {
michael@0 282 printf("PASSED\n");
michael@0 283 } else {
michael@0 284 printf("FAILED\n");
michael@0 285 }
michael@0 286 return failed_already;
michael@0 287 } else {
michael@0 288 #if defined(IRIX) && !defined(_PR_PTHREADS)
michael@0 289 extern void _PR_IRIX_CHILD_PROCESS(void);
michael@0 290 _PR_IRIX_CHILD_PROCESS();
michael@0 291 #endif
michael@0 292 printf("Fork succeeded. Child process continues.\n");
michael@0 293 DoIO();
michael@0 294 printf("Child process exits.\n");
michael@0 295 return failed_already;
michael@0 296 }
michael@0 297 }
michael@0 298
michael@0 299 #else /* XP_UNIX */
michael@0 300
michael@0 301 int main( int argc,
michael@0 302 char *argv[]
michael@0 303 )
michael@0 304 {
michael@0 305
michael@0 306 printf("The fork test is applicable to Unix only.\n");
michael@0 307 return 0;
michael@0 308
michael@0 309 }
michael@0 310
michael@0 311 #endif /* XP_UNIX */

mercurial