michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * File: pipepong2.c michael@0: * michael@0: * Description: michael@0: * This test runs in conjunction with the pipeping2 test. michael@0: * The pipeping2 test creates two pipes and passes two michael@0: * pipe fd's to this test. Then the pipeping2 test writes michael@0: * "ping" to this test and this test writes "pong" back. michael@0: * To run this pair of tests, just invoke pipeping2. michael@0: * michael@0: * Tested areas: process creation, pipes, file descriptor michael@0: * inheritance. michael@0: */ michael@0: michael@0: #include "prerror.h" michael@0: #include "prio.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define NUM_ITERATIONS 10 michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRFileDesc *pipe_read, *pipe_write; michael@0: PRStatus status; michael@0: char buf[1024]; michael@0: PRInt32 nBytes; michael@0: int idx; michael@0: michael@0: pipe_read = PR_GetInheritedFD("PIPE_READ"); michael@0: if (pipe_read == NULL) { michael@0: fprintf(stderr, "PR_GetInheritedFD failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_SetFDInheritable(pipe_read, PR_FALSE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed\n"); michael@0: exit(1); michael@0: } michael@0: pipe_write = PR_GetInheritedFD("PIPE_WRITE"); michael@0: if (pipe_write == NULL) { michael@0: fprintf(stderr, "PR_GetInheritedFD failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_SetFDInheritable(pipe_write, PR_FALSE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: for (idx = 0; idx < NUM_ITERATIONS; idx++) { michael@0: memset(buf, 0, sizeof(buf)); michael@0: nBytes = PR_Read(pipe_read, buf, sizeof(buf)); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Read failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: printf("pong process: received \"%s\"\n", buf); michael@0: if (nBytes != 5) { michael@0: fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: if (strcmp(buf, "ping") != 0) { michael@0: fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n", michael@0: buf); michael@0: exit(1); michael@0: } michael@0: michael@0: strcpy(buf, "pong"); michael@0: printf("pong process: sending \"%s\"\n", buf); michael@0: nBytes = PR_Write(pipe_write, buf, 5); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Write failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: status = PR_Close(pipe_read); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(pipe_write); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: return 0; michael@0: }