test-pipe-close-stdout-read-stdin.c
Go to the documentation of this file.
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #ifndef _WIN32
23 
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/wait.h>
27 #include <sys/types.h>
28 
29 #include "uv.h"
30 #include "task.h"
31 
32 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t* buf)
33 {
34  static char buffer[1024];
35 
36  buf->base = buffer;
37  buf->len = sizeof(buffer);
38 }
39 
41 {
42  if (nread < 0) {
43  uv_close((uv_handle_t*)stream, NULL);
44  return;
45  }
46 }
47 
48 /*
49  * This test is a reproduction of joyent/libuv#1419 .
50  */
51 TEST_IMPL(pipe_close_stdout_read_stdin) {
52  int r = -1;
53  int pid;
54  int fd[2];
55  int status;
56  char buf;
58 
59  r = pipe(fd);
60  ASSERT(r == 0);
61 
62  if ((pid = fork()) == 0) {
63  /*
64  * Make the read side of the pipe our stdin.
65  * The write side will be closed by the parent process.
66  */
67  close(fd[1]);
68  /* block until write end of pipe is closed */
69  r = read(fd[0], &buf, 1);
70  ASSERT(-1 <= r && r <= 1);
71  close(0);
72  r = dup(fd[0]);
73  ASSERT(r != -1);
74 
75  /* Create a stream that reads from the pipe. */
77  ASSERT(r == 0);
78 
80  ASSERT(r == 0);
81 
83  ASSERT(r == 0);
84 
85  /*
86  * Because the other end of the pipe was closed, there should
87  * be no event left to process after one run of the event loop.
88  * Otherwise, it means that events were not processed correctly.
89  */
91  } else {
92  /*
93  * Close both ends of the pipe so that the child
94  * get a POLLHUP event when it tries to read from
95  * the other end.
96  */
97  close(fd[1]);
98  close(fd[0]);
99 
100  waitpid(pid, &status, 0);
101  ASSERT(WIFEXITED(status) && WEXITSTATUS(status) == 0);
102  }
103 
105  return 0;
106 }
107 
108 #else
109 
110 typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
111 
112 #endif /* ifndef _WIN32 */
task.h
stdin_pipe
uv_pipe_t stdin_pipe
Definition: libuv/docs/code/uvtee/main.c:15
uv_pipe_init
UV_EXTERN int uv_pipe_init(uv_loop_t *, uv_pipe_t *handle, int ipc)
Definition: unix/pipe.c:33
UV_RUN_NOWAIT
@ UV_RUN_NOWAIT
Definition: uv.h:256
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ASSERT
#define ASSERT(expr)
Definition: task.h:102
TEST_IMPL
TEST_IMPL(pipe_close_stdout_read_stdin)
Definition: test-pipe-close-stdout-read-stdin.c:51
status
absl::Status status
Definition: rls.cc:251
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
ssize_t
intptr_t ssize_t
Definition: win.h:27
alloc_buffer
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: test-pipe-close-stdout-read-stdin.c:32
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
close
#define close
Definition: test-fs.c:48
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_buf_t
Definition: unix.h:121
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
file_has_no_tests
int file_has_no_tests
Definition: test-fs-fd-hash.c:131
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_pipe_s
Definition: uv.h:757
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
read_stdin
void read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
Definition: test-pipe-close-stdout-read-stdin.c:40
uv_pipe_open
UV_EXTERN int uv_pipe_open(uv_pipe_t *, uv_file file)
Definition: unix/pipe.c:137
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26