VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/pipeping2.c@ 32258

Last change on this file since 32258 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39 * File: pipeping2.c
40 *
41 * Description:
42 * This test runs in conjunction with the pipepong2 test.
43 * This test creates two pipes and passes two pipe fd's
44 * to the pipepong2 test. Then this test writes "ping" to
45 * to the pipepong2 test and the pipepong2 test writes "pong"
46 * back. To run this pair of tests, just invoke pipeping2.
47 *
48 * Tested areas: process creation, pipes, file descriptor
49 * inheritance.
50 */
51
52#include "prerror.h"
53#include "prio.h"
54#include "prproces.h"
55
56#include <stdio.h>
57#include <string.h>
58#include <stdlib.h>
59
60#define NUM_ITERATIONS 10
61
62static char *child_argv[] = { "pipepong2", NULL };
63
64int main()
65{
66 PRFileDesc *in_pipe[2];
67 PRFileDesc *out_pipe[2];
68 PRStatus status;
69 PRProcess *process;
70 PRProcessAttr *attr;
71 char buf[1024];
72 PRInt32 nBytes;
73 PRInt32 exitCode;
74 int idx;
75
76 status = PR_CreatePipe(&in_pipe[0], &in_pipe[1]);
77 if (status == PR_FAILURE) {
78 fprintf(stderr, "PR_CreatePipe failed\n");
79 exit(1);
80 }
81 status = PR_CreatePipe(&out_pipe[0], &out_pipe[1]);
82 if (status == PR_FAILURE) {
83 fprintf(stderr, "PR_CreatePipe failed\n");
84 exit(1);
85 }
86
87 status = PR_SetFDInheritable(in_pipe[0], PR_FALSE);
88 if (status == PR_FAILURE) {
89 fprintf(stderr, "PR_SetFDInheritable failed\n");
90 exit(1);
91 }
92 status = PR_SetFDInheritable(in_pipe[1], PR_TRUE);
93 if (status == PR_FAILURE) {
94 fprintf(stderr, "PR_SetFDInheritable failed\n");
95 exit(1);
96 }
97 status = PR_SetFDInheritable(out_pipe[0], PR_TRUE);
98 if (status == PR_FAILURE) {
99 fprintf(stderr, "PR_SetFDInheritable failed\n");
100 exit(1);
101 }
102 status = PR_SetFDInheritable(out_pipe[1], PR_FALSE);
103 if (status == PR_FAILURE) {
104 fprintf(stderr, "PR_SetFDInheritable failed\n");
105 exit(1);
106 }
107
108 attr = PR_NewProcessAttr();
109 if (attr == NULL) {
110 fprintf(stderr, "PR_NewProcessAttr failed\n");
111 exit(1);
112 }
113
114 status = PR_ProcessAttrSetInheritableFD(attr, out_pipe[0], "PIPE_READ");
115 if (status == PR_FAILURE) {
116 fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n");
117 exit(1);
118 }
119 status = PR_ProcessAttrSetInheritableFD(attr, in_pipe[1], "PIPE_WRITE");
120 if (status == PR_FAILURE) {
121 fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n");
122 exit(1);
123 }
124
125 process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr);
126 if (process == NULL) {
127 fprintf(stderr, "PR_CreateProcess failed\n");
128 exit(1);
129 }
130 PR_DestroyProcessAttr(attr);
131 status = PR_Close(out_pipe[0]);
132 if (status == PR_FAILURE) {
133 fprintf(stderr, "PR_Close failed\n");
134 exit(1);
135 }
136 status = PR_Close(in_pipe[1]);
137 if (status == PR_FAILURE) {
138 fprintf(stderr, "PR_Close failed\n");
139 exit(1);
140 }
141
142 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
143 strcpy(buf, "ping");
144 printf("ping process: sending \"%s\"\n", buf);
145 nBytes = PR_Write(out_pipe[1], buf, 5);
146 if (nBytes == -1) {
147 fprintf(stderr, "PR_Write failed: (%d, %d)\n",
148 PR_GetError(), PR_GetOSError());
149 exit(1);
150 }
151 memset(buf, 0, sizeof(buf));
152 nBytes = PR_Read(in_pipe[0], buf, sizeof(buf));
153 if (nBytes == -1) {
154 fprintf(stderr, "PR_Read failed\n");
155 exit(1);
156 }
157 printf("ping process: received \"%s\"\n", buf);
158 if (nBytes != 5) {
159 fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n",
160 nBytes);
161 exit(1);
162 }
163 if (strcmp(buf, "pong") != 0) {
164 fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n",
165 buf);
166 exit(1);
167 }
168 }
169
170 status = PR_Close(in_pipe[0]);
171 if (status == PR_FAILURE) {
172 fprintf(stderr, "PR_Close failed\n");
173 exit(1);
174 }
175 status = PR_Close(out_pipe[1]);
176 if (status == PR_FAILURE) {
177 fprintf(stderr, "PR_Close failed\n");
178 exit(1);
179 }
180 status = PR_WaitProcess(process, &exitCode);
181 if (status == PR_FAILURE) {
182 fprintf(stderr, "PR_WaitProcess failed\n");
183 exit(1);
184 }
185 if (exitCode == 0) {
186 printf("PASS\n");
187 return 0;
188 } else {
189 printf("FAIL\n");
190 return 1;
191 }
192}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette