Rizin
unix-like reverse engineering framework and cli tools
stream.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 #include <assert.h>
23 
24 #include "uv.h"
25 #include "internal.h"
26 #include "handle-inl.h"
27 #include "req-inl.h"
28 
29 
31  int err;
32 
33  err = ERROR_INVALID_PARAMETER;
34  switch (stream->type) {
35  case UV_TCP:
36  err = uv_tcp_listen((uv_tcp_t*)stream, backlog, cb);
37  break;
38  case UV_NAMED_PIPE:
39  err = uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
40  break;
41  default:
42  assert(0);
43  }
44 
46 }
47 
48 
49 int uv_accept(uv_stream_t* server, uv_stream_t* client) {
50  int err;
51 
52  err = ERROR_INVALID_PARAMETER;
53  switch (server->type) {
54  case UV_TCP:
55  err = uv_tcp_accept((uv_tcp_t*)server, (uv_tcp_t*)client);
56  break;
57  case UV_NAMED_PIPE:
58  err = uv_pipe_accept((uv_pipe_t*)server, client);
59  break;
60  default:
61  assert(0);
62  }
63 
65 }
66 
67 
69  uv_read_cb read_cb) {
70  int err;
71 
72  if (handle->flags & UV_HANDLE_READING) {
73  return UV_EALREADY;
74  }
75 
76  if (!(handle->flags & UV_HANDLE_READABLE)) {
77  return UV_ENOTCONN;
78  }
79 
80  err = ERROR_INVALID_PARAMETER;
81  switch (handle->type) {
82  case UV_TCP:
83  err = uv_tcp_read_start((uv_tcp_t*)handle, alloc_cb, read_cb);
84  break;
85  case UV_NAMED_PIPE:
86  err = uv_pipe_read_start((uv_pipe_t*)handle, alloc_cb, read_cb);
87  break;
88  case UV_TTY:
89  err = uv_tty_read_start((uv_tty_t*) handle, alloc_cb, read_cb);
90  break;
91  default:
92  assert(0);
93  }
94 
96 }
97 
98 
100  int err;
101 
102  if (!(handle->flags & UV_HANDLE_READING))
103  return 0;
104 
105  err = 0;
106  if (handle->type == UV_TTY) {
108  } else if (handle->type == UV_NAMED_PIPE) {
110  } else {
111  handle->flags &= ~UV_HANDLE_READING;
113  }
114 
115  return uv_translate_sys_error(err);
116 }
117 
118 
121  const uv_buf_t bufs[],
122  unsigned int nbufs,
123  uv_write_cb cb) {
124  uv_loop_t* loop = handle->loop;
125  int err;
126 
127  if (!(handle->flags & UV_HANDLE_WRITABLE)) {
128  return UV_EPIPE;
129  }
130 
131  err = ERROR_INVALID_PARAMETER;
132  switch (handle->type) {
133  case UV_TCP:
134  err = uv_tcp_write(loop, req, (uv_tcp_t*) handle, bufs, nbufs, cb);
135  break;
136  case UV_NAMED_PIPE:
138  loop, req, (uv_pipe_t*) handle, bufs, nbufs, NULL, cb);
139  break;
140  case UV_TTY:
141  err = uv_tty_write(loop, req, (uv_tty_t*) handle, bufs, nbufs, cb);
142  break;
143  default:
144  assert(0);
145  }
146 
147  return uv_translate_sys_error(err);
148 }
149 
150 
153  const uv_buf_t bufs[],
154  unsigned int nbufs,
155  uv_stream_t* send_handle,
156  uv_write_cb cb) {
157  uv_loop_t* loop = handle->loop;
158  int err;
159 
160  if (send_handle == NULL) {
161  return uv_write(req, handle, bufs, nbufs, cb);
162  }
163 
164  if (handle->type != UV_NAMED_PIPE || !((uv_pipe_t*) handle)->ipc) {
165  return UV_EINVAL;
166  } else if (!(handle->flags & UV_HANDLE_WRITABLE)) {
167  return UV_EPIPE;
168  }
169 
171  loop, req, (uv_pipe_t*) handle, bufs, nbufs, send_handle, cb);
172  return uv_translate_sys_error(err);
173 }
174 
175 
177  const uv_buf_t bufs[],
178  unsigned int nbufs) {
179  if (stream->flags & UV_HANDLE_CLOSING)
180  return UV_EBADF;
181  if (!(stream->flags & UV_HANDLE_WRITABLE))
182  return UV_EPIPE;
183 
184  switch (stream->type) {
185  case UV_TCP:
186  return uv__tcp_try_write((uv_tcp_t*) stream, bufs, nbufs);
187  case UV_TTY:
188  return uv__tty_try_write((uv_tty_t*) stream, bufs, nbufs);
189  case UV_NAMED_PIPE:
190  return UV_EAGAIN;
191  default:
192  assert(0);
193  return UV_ENOSYS;
194  }
195 }
196 
197 
199  uv_loop_t* loop = handle->loop;
200 
201  if (!(handle->flags & UV_HANDLE_WRITABLE) ||
202  handle->flags & UV_HANDLE_SHUTTING ||
204  return UV_ENOTCONN;
205  }
206 
207  UV_REQ_INIT(req, UV_SHUTDOWN);
208  req->handle = handle;
209  req->cb = cb;
210 
211  handle->flags &= ~UV_HANDLE_WRITABLE;
212  handle->flags |= UV_HANDLE_SHUTTING;
213  handle->stream.conn.shutdown_req = req;
214  handle->reqs_pending++;
216 
218 
219  return 0;
220 }
221 
222 
224  return !!(handle->flags & UV_HANDLE_READABLE);
225 }
226 
227 
229  return !!(handle->flags & UV_HANDLE_WRITABLE);
230 }
231 
232 
234  if (handle->type != UV_NAMED_PIPE)
235  return UV_EINVAL;
236 
237  if (blocking != 0)
239  else
241 
242  return 0;
243 }
static bool err
Definition: armass.c:435
static mcore_handle handle
Definition: asm_mcore.c:8
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec req
Definition: sflib.h:128
#define DECREASE_ACTIVE_COUNT(loop, handle)
Definition: handle-inl.h:32
static INLINE void uv_want_endgame(uv_loop_t *loop, uv_handle_t *handle)
Definition: handle-inl.h:88
voidpf stream
Definition: ioapi.h:138
assert(limit<=UINT32_MAX/2)
#define REGISTER_HANDLE_REQ(loop, handle, req)
Definition: req-inl.h:56
Definition: unix.h:123
Definition: uv.h:1780
Definition: uv.h:767
Definition: uv.h:547
Definition: uv.h:714
Definition: uv.h:525
uv_loop_t * loop
Definition: main.c:7
int uv_pipe_listen(uv_pipe_t *handle, int backlog, uv_connection_cb cb)
Definition: pipe.c:94
int uv_tcp_listen(uv_tcp_t *tcp, int backlog, uv_connection_cb cb)
Definition: tcp.c:328
int uv__pipe_write(uv_loop_t *loop, uv_write_t *req, uv_pipe_t *handle, const uv_buf_t bufs[], size_t nbufs, uv_stream_t *send_handle, uv_write_cb cb)
Definition: pipe.c:1578
int uv_tty_write(uv_loop_t *loop, uv_write_t *req, uv_tty_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: tty.c:2182
void uv__pipe_read_stop(uv_pipe_t *handle)
Definition: pipe.c:775
int uv__tcp_try_write(uv_tcp_t *handle, const uv_buf_t bufs[], unsigned int nbufs)
Definition: tcp.c:968
int uv_tty_read_stop(uv_tty_t *handle)
Definition: tty.c:1054
int uv_tcp_read_start(uv_tcp_t *handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: tcp.c:726
int uv__tty_try_write(uv_tty_t *handle, const uv_buf_t bufs[], unsigned int nbufs)
Definition: tty.c:2212
int uv_pipe_read_start(uv_pipe_t *handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: pipe.c:1177
int uv_tcp_accept(uv_tcp_t *server, uv_tcp_t *client)
Definition: tcp.c:657
int uv_pipe_accept(uv_pipe_t *server, uv_stream_t *client)
Definition: pipe.c:876
int uv_tcp_write(uv_loop_t *loop, uv_write_t *req, uv_tcp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: tcp.c:901
int uv_tty_read_start(uv_tty_t *handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: tty.c:1018
static char bufs[4][128]
Buffers for uint64_to_str() and uint64_to_nicestr()
Definition: util.c:18
int uv_stream_set_blocking(uv_stream_t *handle, int blocking)
Definition: stream.c:1688
int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: stream.c:656
int uv_write2(uv_write_t *req, uv_stream_t *stream, const uv_buf_t bufs[], unsigned int nbufs, uv_stream_t *send_handle, uv_write_cb cb)
Definition: stream.c:1393
int uv_accept(uv_stream_t *server, uv_stream_t *client)
Definition: stream.c:591
int uv_is_readable(const uv_stream_t *stream)
Definition: stream.c:1606
int uv_try_write(uv_stream_t *stream, const uv_buf_t bufs[], unsigned int nbufs)
Definition: stream.c:1507
int uv_shutdown(uv_shutdown_t *req, uv_stream_t *stream, uv_shutdown_cb cb)
Definition: stream.c:1259
int uv_read_start(uv_stream_t *stream, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: stream.c:1555
int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: stream.c:1492
int uv_read_stop(uv_stream_t *stream)
Definition: stream.c:1590
int uv_is_writable(const uv_stream_t *stream)
Definition: stream.c:1611
@ UV_HANDLE_BLOCKING_WRITES
Definition: uv-common.h:98
@ UV_HANDLE_READING
Definition: uv-common.h:90
@ UV_HANDLE_CLOSING
Definition: uv-common.h:74
@ UV_HANDLE_SHUTTING
Definition: uv-common.h:84
@ UV_HANDLE_WRITABLE
Definition: uv-common.h:93
@ UV_HANDLE_READABLE
Definition: uv-common.h:92
#define uv__is_closing(h)
Definition: uv-common.h:255
#define UV_REQ_INIT(req, typ)
Definition: uv-common.h:322
void(* uv_write_cb)(uv_write_t *req, int status)
Definition: uv.h:315
void(* uv_connection_cb)(uv_stream_t *server, int status)
Definition: uv.h:318
UV_EXTERN int uv_translate_sys_error(int sys_errno)
Definition: core.c:1249
void(* uv_read_cb)(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
Definition: uv.h:312
void(* uv_alloc_cb)(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: uv.h:309
void(* uv_shutdown_cb)(uv_shutdown_t *req, int status)
Definition: uv.h:317
static const char * cb[]
Definition: z80_tab.h:176