Debian Codegraph @

nsprpub/pr/tests/pipepong2.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 * File: pipepong2.c
8 *
9 * Description:
10 * This test runs in conjunction with the pipeping2 test.
11 * The pipeping2 test creates two pipes and passes two
12 * pipe fd's to this test. Then the pipeping2 test writes
13 * "ping" to this test and this test writes "pong" back.
14 * To run this pair of tests, just invoke pipeping2.
15 *
16 * Tested areas: process creation, pipes, file descriptor
17 * inheritance.
18 */
19
20 #include "prerror.h"
21 #include "prio.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #define NUM_ITERATIONS 10
28
29 int main(int argc, char** argv) {
30 PRFileDesc *pipe_read, *pipe_write;
31 PRStatus status;
32 char buf[1024];
33 PRInt32 nBytes;
34 int idx;
35
36 pipe_read = PR_GetInheritedFD("PIPE_READ");
37 if (pipe_read == NULL) {
38 fprintf(stderr, "PR_GetInheritedFD failed\n");
39 exit(1);
40 }
41 status = PR_SetFDInheritable(pipe_read, PR_FALSE);
42 if (status == PR_FAILURE) {
43 fprintf(stderr, "PR_SetFDInheritable failed\n");
44 exit(1);
45 }
46 pipe_write = PR_GetInheritedFD("PIPE_WRITE");
47 if (pipe_write == NULL) {
48 fprintf(stderr, "PR_GetInheritedFD failed\n");
49 exit(1);
50 }
51 status = PR_SetFDInheritable(pipe_write, PR_FALSE);
52 if (status == PR_FAILURE) {
53 fprintf(stderr, "PR_SetFDInheritable failed\n");
54 exit(1);
55 }
56
57 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
58 memset(buf, 0, sizeof(buf));
59 nBytes = PR_Read(pipe_read, buf, sizeof(buf));
60 if (nBytes == -1) {
61 fprintf(stderr, "PR_Read failed: (%d, %d)\n", PR_GetError(),
62 PR_GetOSError());
63 exit(1);
64 }
65 printf("pong process: received \"%s\"\n", buf);
66 if (nBytes != 5) {
67 fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
68 nBytes);
69 exit(1);
70 }
71 if (strcmp(buf, "ping") != 0) {
72 fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n", buf);
73 exit(1);
74 }
75
76 strcpy(buf, "pong");
77 printf("pong process: sending \"%s\"\n", buf);
78 nBytes = PR_Write(pipe_write, buf, 5);
79 if (nBytes == -1) {
80 fprintf(stderr, "PR_Write failed\n");
81 exit(1);
82 }
83 }
84
85 status = PR_Close(pipe_read);
86 if (status == PR_FAILURE) {
87 fprintf(stderr, "PR_Close failed\n");
88 exit(1);
89 }
90 status = PR_Close(pipe_write);
91 if (status == PR_FAILURE) {
92 fprintf(stderr, "PR_Close failed\n");
93 exit(1);
94 }
95 return 0;
96 }