Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
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 | * File: pipepong2.c |
michael@0 | 8 | * |
michael@0 | 9 | * Description: |
michael@0 | 10 | * This test runs in conjunction with the pipeping2 test. |
michael@0 | 11 | * The pipeping2 test creates two pipes and passes two |
michael@0 | 12 | * pipe fd's to this test. Then the pipeping2 test writes |
michael@0 | 13 | * "ping" to this test and this test writes "pong" back. |
michael@0 | 14 | * To run this pair of tests, just invoke pipeping2. |
michael@0 | 15 | * |
michael@0 | 16 | * Tested areas: process creation, pipes, file descriptor |
michael@0 | 17 | * inheritance. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | #include "prerror.h" |
michael@0 | 21 | #include "prio.h" |
michael@0 | 22 | |
michael@0 | 23 | #include <stdio.h> |
michael@0 | 24 | #include <string.h> |
michael@0 | 25 | #include <stdlib.h> |
michael@0 | 26 | |
michael@0 | 27 | #define NUM_ITERATIONS 10 |
michael@0 | 28 | |
michael@0 | 29 | int main(int argc, char **argv) |
michael@0 | 30 | { |
michael@0 | 31 | PRFileDesc *pipe_read, *pipe_write; |
michael@0 | 32 | PRStatus status; |
michael@0 | 33 | char buf[1024]; |
michael@0 | 34 | PRInt32 nBytes; |
michael@0 | 35 | int idx; |
michael@0 | 36 | |
michael@0 | 37 | pipe_read = PR_GetInheritedFD("PIPE_READ"); |
michael@0 | 38 | if (pipe_read == NULL) { |
michael@0 | 39 | fprintf(stderr, "PR_GetInheritedFD failed\n"); |
michael@0 | 40 | exit(1); |
michael@0 | 41 | } |
michael@0 | 42 | status = PR_SetFDInheritable(pipe_read, PR_FALSE); |
michael@0 | 43 | if (status == PR_FAILURE) { |
michael@0 | 44 | fprintf(stderr, "PR_SetFDInheritable failed\n"); |
michael@0 | 45 | exit(1); |
michael@0 | 46 | } |
michael@0 | 47 | pipe_write = PR_GetInheritedFD("PIPE_WRITE"); |
michael@0 | 48 | if (pipe_write == NULL) { |
michael@0 | 49 | fprintf(stderr, "PR_GetInheritedFD failed\n"); |
michael@0 | 50 | exit(1); |
michael@0 | 51 | } |
michael@0 | 52 | status = PR_SetFDInheritable(pipe_write, PR_FALSE); |
michael@0 | 53 | if (status == PR_FAILURE) { |
michael@0 | 54 | fprintf(stderr, "PR_SetFDInheritable failed\n"); |
michael@0 | 55 | exit(1); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | for (idx = 0; idx < NUM_ITERATIONS; idx++) { |
michael@0 | 59 | memset(buf, 0, sizeof(buf)); |
michael@0 | 60 | nBytes = PR_Read(pipe_read, buf, sizeof(buf)); |
michael@0 | 61 | if (nBytes == -1) { |
michael@0 | 62 | fprintf(stderr, "PR_Read failed: (%d, %d)\n", |
michael@0 | 63 | PR_GetError(), PR_GetOSError()); |
michael@0 | 64 | exit(1); |
michael@0 | 65 | } |
michael@0 | 66 | printf("pong process: received \"%s\"\n", buf); |
michael@0 | 67 | if (nBytes != 5) { |
michael@0 | 68 | fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n", |
michael@0 | 69 | nBytes); |
michael@0 | 70 | exit(1); |
michael@0 | 71 | } |
michael@0 | 72 | if (strcmp(buf, "ping") != 0) { |
michael@0 | 73 | fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n", |
michael@0 | 74 | buf); |
michael@0 | 75 | exit(1); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | strcpy(buf, "pong"); |
michael@0 | 79 | printf("pong process: sending \"%s\"\n", buf); |
michael@0 | 80 | nBytes = PR_Write(pipe_write, buf, 5); |
michael@0 | 81 | if (nBytes == -1) { |
michael@0 | 82 | fprintf(stderr, "PR_Write failed\n"); |
michael@0 | 83 | exit(1); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | status = PR_Close(pipe_read); |
michael@0 | 88 | if (status == PR_FAILURE) { |
michael@0 | 89 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 90 | exit(1); |
michael@0 | 91 | } |
michael@0 | 92 | status = PR_Close(pipe_write); |
michael@0 | 93 | if (status == PR_FAILURE) { |
michael@0 | 94 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 95 | exit(1); |
michael@0 | 96 | } |
michael@0 | 97 | return 0; |
michael@0 | 98 | } |