unix/pipe.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 "uv.h"
23 #include "internal.h"
24 
25 #include <assert.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <sys/un.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 
32 
34  uv__stream_init(loop, (uv_stream_t*)handle, UV_NAMED_PIPE);
35  handle->shutdown_req = NULL;
36  handle->connect_req = NULL;
37  handle->pipe_fname = NULL;
38  handle->ipc = ipc;
39  return 0;
40 }
41 
42 
43 int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
44  struct sockaddr_un saddr;
45  const char* pipe_fname;
46  int sockfd;
47  int err;
48 
49  pipe_fname = NULL;
50 
51  /* Already bound? */
52  if (uv__stream_fd(handle) >= 0)
53  return UV_EINVAL;
54 
55  /* Make a copy of the file name, it outlives this function's scope. */
56  pipe_fname = uv__strdup(name);
57  if (pipe_fname == NULL)
58  return UV_ENOMEM;
59 
60  /* We've got a copy, don't touch the original any more. */
61  name = NULL;
62 
63  err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
64  if (err < 0)
65  goto err_socket;
66  sockfd = err;
67 
68  memset(&saddr, 0, sizeof saddr);
69  uv__strscpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
70  saddr.sun_family = AF_UNIX;
71 
72  if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr)) {
73  err = UV__ERR(errno);
74  /* Convert ENOENT to EACCES for compatibility with Windows. */
75  if (err == UV_ENOENT)
76  err = UV_EACCES;
77 
78  uv__close(sockfd);
79  goto err_socket;
80  }
81 
82  /* Success. */
83  handle->flags |= UV_HANDLE_BOUND;
84  handle->pipe_fname = pipe_fname; /* Is a strdup'ed copy. */
85  handle->io_watcher.fd = sockfd;
86  return 0;
87 
88 err_socket:
89  uv__free((void*)pipe_fname);
90  return err;
91 }
92 
93 
95  if (uv__stream_fd(handle) == -1)
96  return UV_EINVAL;
97 
98  if (handle->ipc)
99  return UV_EINVAL;
100 
101 #if defined(__MVS__) || defined(__PASE__)
102  /* On zOS, backlog=0 has undefined behaviour */
103  /* On IBMi PASE, backlog=0 leads to "Connection refused" error */
104  if (backlog == 0)
105  backlog = 1;
106  else if (backlog < 0)
107  backlog = SOMAXCONN;
108 #endif
109 
110  if (listen(uv__stream_fd(handle), backlog))
111  return UV__ERR(errno);
112 
113  handle->connection_cb = cb;
114  handle->io_watcher.cb = uv__server_io;
115  uv__io_start(handle->loop, &handle->io_watcher, POLLIN);
116  return 0;
117 }
118 
119 
121  if (handle->pipe_fname) {
122  /*
123  * Unlink the file system entity before closing the file descriptor.
124  * Doing it the other way around introduces a race where our process
125  * unlinks a socket with the same name that's just been created by
126  * another thread or process.
127  */
128  unlink(handle->pipe_fname);
129  uv__free((void*)handle->pipe_fname);
130  handle->pipe_fname = NULL;
131  }
132 
134 }
135 
136 
138  int flags;
139  int mode;
140  int err;
141  flags = 0;
142 
143  if (uv__fd_exists(handle->loop, fd))
144  return UV_EEXIST;
145 
146  do
147  mode = fcntl(fd, F_GETFL);
148  while (mode == -1 && errno == EINTR);
149 
150  if (mode == -1)
151  return UV__ERR(errno); /* according to docs, must be EBADF */
152 
153  err = uv__nonblock(fd, 1);
154  if (err)
155  return err;
156 
157 #if defined(__APPLE__)
158  err = uv__stream_try_select((uv_stream_t*) handle, &fd);
159  if (err)
160  return err;
161 #endif /* defined(__APPLE__) */
162 
163  mode &= O_ACCMODE;
164  if (mode != O_WRONLY)
166  if (mode != O_RDONLY)
168 
169  return uv__stream_open((uv_stream_t*)handle, fd, flags);
170 }
171 
172 
174  uv_pipe_t* handle,
175  const char* name,
176  uv_connect_cb cb) {
177  struct sockaddr_un saddr;
178  int new_sock;
179  int err;
180  int r;
181 
182  new_sock = (uv__stream_fd(handle) == -1);
183 
184  if (new_sock) {
185  err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
186  if (err < 0)
187  goto out;
188  handle->io_watcher.fd = err;
189  }
190 
191  memset(&saddr, 0, sizeof saddr);
192  uv__strscpy(saddr.sun_path, name, sizeof(saddr.sun_path));
193  saddr.sun_family = AF_UNIX;
194 
195  do {
196  r = connect(uv__stream_fd(handle),
197  (struct sockaddr*)&saddr, sizeof saddr);
198  }
199  while (r == -1 && errno == EINTR);
200 
201  if (r == -1 && errno != EINPROGRESS) {
202  err = UV__ERR(errno);
203 #if defined(__CYGWIN__) || defined(__MSYS__)
204  /* EBADF is supposed to mean that the socket fd is bad, but
205  Cygwin reports EBADF instead of ENOTSOCK when the file is
206  not a socket. We do not expect to see a bad fd here
207  (e.g. due to new_sock), so translate the error. */
208  if (err == UV_EBADF)
209  err = UV_ENOTSOCK;
210 #endif
211  goto out;
212  }
213 
214  err = 0;
215  if (new_sock) {
219  }
220 
221  if (err == 0)
222  uv__io_start(handle->loop, &handle->io_watcher, POLLOUT);
223 
224 out:
225  handle->delayed_error = err;
226  handle->connect_req = req;
227 
228  uv__req_init(handle->loop, req, UV_CONNECT);
230  req->cb = cb;
231  QUEUE_INIT(&req->queue);
232 
233  /* Force callback to run on next tick in case of error. */
234  if (err)
235  uv__io_feed(handle->loop, &handle->io_watcher);
236 
237 }
238 
239 
242  char* buffer,
243  size_t* size) {
244  struct sockaddr_un sa;
245  socklen_t addrlen;
246  int err;
247 
248  addrlen = sizeof(sa);
249  memset(&sa, 0, addrlen);
251  func,
252  (struct sockaddr*) &sa,
253  (int*) &addrlen);
254  if (err < 0) {
255  *size = 0;
256  return err;
257  }
258 
259 #if defined(__linux__)
260  if (sa.sun_path[0] == 0)
261  /* Linux abstract namespace */
262  addrlen -= offsetof(struct sockaddr_un, sun_path);
263  else
264 #endif
265  addrlen = strlen(sa.sun_path);
266 
267 
268  if ((size_t)addrlen >= *size) {
269  *size = addrlen + 1;
270  return UV_ENOBUFS;
271  }
272 
273  memcpy(buffer, sa.sun_path, addrlen);
274  *size = addrlen;
275 
276  /* only null-terminate if it's not an abstract socket */
277  if (buffer[0] != '\0')
278  buffer[addrlen] = '\0';
279 
280  return 0;
281 }
282 
283 
284 int uv_pipe_getsockname(const uv_pipe_t* handle, char* buffer, size_t* size) {
285  return uv__pipe_getsockpeername(handle, getsockname, buffer, size);
286 }
287 
288 
289 int uv_pipe_getpeername(const uv_pipe_t* handle, char* buffer, size_t* size) {
290  return uv__pipe_getsockpeername(handle, getpeername, buffer, size);
291 }
292 
293 
295 }
296 
297 
299  uv__stream_queued_fds_t* queued_fds;
300 
301  if (!handle->ipc)
302  return 0;
303 
304  if (handle->accepted_fd == -1)
305  return 0;
306 
307  if (handle->queued_fds == NULL)
308  return 1;
309 
310  queued_fds = handle->queued_fds;
311  return queued_fds->offset + 1;
312 }
313 
314 
316  if (!handle->ipc)
317  return UV_UNKNOWN_HANDLE;
318 
319  if (handle->accepted_fd == -1)
320  return UV_UNKNOWN_HANDLE;
321  else
322  return uv__handle_type(handle->accepted_fd);
323 }
324 
325 
327  unsigned desired_mode;
328  struct stat pipe_stat;
329  char* name_buffer;
330  size_t name_len;
331  int r;
332 
333  if (handle == NULL || uv__stream_fd(handle) == -1)
334  return UV_EBADF;
335 
336  if (mode != UV_READABLE &&
337  mode != UV_WRITABLE &&
339  return UV_EINVAL;
340 
341  /* Unfortunately fchmod does not work on all platforms, we will use chmod. */
342  name_len = 0;
343  r = uv_pipe_getsockname(handle, NULL, &name_len);
344  if (r != UV_ENOBUFS)
345  return r;
346 
347  name_buffer = uv__malloc(name_len);
348  if (name_buffer == NULL)
349  return UV_ENOMEM;
350 
351  r = uv_pipe_getsockname(handle, name_buffer, &name_len);
352  if (r != 0) {
353  uv__free(name_buffer);
354  return r;
355  }
356 
357  /* stat must be used as fstat has a bug on Darwin */
358  if (stat(name_buffer, &pipe_stat) == -1) {
359  uv__free(name_buffer);
360  return -errno;
361  }
362 
363  desired_mode = 0;
364  if (mode & UV_READABLE)
365  desired_mode |= S_IRUSR | S_IRGRP | S_IROTH;
366  if (mode & UV_WRITABLE)
367  desired_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
368 
369  /* Exit early if pipe already has desired mode. */
370  if ((pipe_stat.st_mode & desired_mode) == desired_mode) {
371  uv__free(name_buffer);
372  return 0;
373  }
374 
375  pipe_stat.st_mode |= desired_mode;
376 
377  r = chmod(name_buffer, pipe_stat.st_mode);
378  uv__free(name_buffer);
379 
380  return r != -1 ? 0 : UV__ERR(errno);
381 }
UV_HANDLE_WRITABLE
@ UV_HANDLE_WRITABLE
Definition: uv-common.h:85
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
uv__stream_close
void uv__stream_close(uv_stream_t *handle)
Definition: unix/stream.c:1633
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
uv__pipe_close
void uv__pipe_close(uv_pipe_t *handle)
Definition: unix/pipe.c:120
UV_HANDLE_BOUND
@ UV_HANDLE_BOUND
Definition: uv-common.h:83
memset
return memset(p, 0, total)
uv__socket
int uv__socket(int domain, int type, int protocol)
Definition: unix/core.c:424
uv_handle_type
uv_handle_type
Definition: uv.h:189
uv_connect_s
Definition: uv.h:580
uv__req_init
#define uv__req_init(loop, req, typ)
Definition: uv-common.h:312
UV_WRITABLE
@ UV_WRITABLE
Definition: uv.h:791
uv__malloc
void * uv__malloc(size_t size)
Definition: uv-common.c:75
string.h
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
error_ref_leak.err
err
Definition: error_ref_leak.py:35
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
setup.name
name
Definition: setup.py:542
uv__pipe_getsockpeername
static int uv__pipe_getsockpeername(const uv_pipe_t *handle, uv__peersockfunc func, char *buffer, size_t *size)
Definition: unix/pipe.c:240
uv_pipe_listen
int uv_pipe_listen(uv_pipe_t *handle, int backlog, uv_connection_cb cb)
Definition: unix/pipe.c:94
uv_connect_s::cb
UV_REQ_FIELDS uv_connect_cb cb
Definition: uv.h:582
uv_stream_s
Definition: uv.h:491
QUEUE_INIT
#define QUEUE_INIT(q)
Definition: queue.h:45
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
uv__server_io
void uv__server_io(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: unix/stream.c:528
uv_pipe_pending_instances
void uv_pipe_pending_instances(uv_pipe_t *handle, int count)
Definition: unix/pipe.c:294
uv__fd_exists
int uv__fd_exists(uv_loop_t *loop, int fd)
Definition: unix/core.c:953
req
static uv_connect_t req
Definition: test-connection-fail.c:30
uv__strdup
char * uv__strdup(const char *s)
Definition: uv-common.c:55
uv__io_start
void uv__io_start(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: unix/core.c:870
uv__getsockpeername
int uv__getsockpeername(const uv_handle_t *handle, uv__peersockfunc func, struct sockaddr *name, int *namelen)
Definition: unix/core.c:1485
uv_pipe_init
int uv_pipe_init(uv_loop_t *loop, uv_pipe_t *handle, int ipc)
Definition: unix/pipe.c:33
uv_pipe_getsockname
int uv_pipe_getsockname(const uv_pipe_t *handle, char *buffer, size_t *size)
Definition: unix/pipe.c:284
uv__free
void uv__free(void *ptr)
Definition: uv-common.c:81
uv_pipe_open
int uv_pipe_open(uv_pipe_t *handle, uv_file fd)
Definition: unix/pipe.c:137
uv__stream_init
void uv__stream_init(uv_loop_t *loop, uv_stream_t *stream, uv_handle_type type)
Definition: unix/stream.c:85
UV__ERR
#define UV__ERR(x)
Definition: errno.h:29
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
UV_READABLE
@ UV_READABLE
Definition: uv.h:790
uv_file
int uv_file
Definition: unix.h:126
uv_pipe_pending_type
uv_handle_type uv_pipe_pending_type(uv_pipe_t *handle)
Definition: unix/pipe.c:315
uv_connect_cb
void(* uv_connect_cb)(uv_connect_t *req, int status)
Definition: uv.h:313
uv_pipe_bind
int uv_pipe_bind(uv_pipe_t *handle, const char *name)
Definition: unix/pipe.c:43
uv.h
uv__nonblock
#define uv__nonblock
Definition: third_party/libuv/src/unix/internal.h:174
uv_pipe_connect
void uv_pipe_connect(uv_connect_t *req, uv_pipe_t *handle, const char *name, uv_connect_cb cb)
Definition: unix/pipe.c:173
internal.h
UV_HANDLE_READABLE
@ UV_HANDLE_READABLE
Definition: uv-common.h:84
uv__strscpy
ssize_t uv__strscpy(char *d, const char *s, size_t n)
Definition: strscpy.c:4
uv__io_feed
void uv__io_feed(uv_loop_t *loop, uv__io_t *w)
Definition: unix/core.c:940
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
uv_pipe_pending_count
int uv_pipe_pending_count(uv_pipe_t *handle)
Definition: unix/pipe.c:298
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
http2_test_server.listen
def listen(endpoint, test_case)
Definition: http2_test_server.py:87
uv__peersockfunc
int(* uv__peersockfunc)(int, struct sockaddr *, socklen_t *)
Definition: third_party/libuv/src/unix/internal.h:324
uv_connection_cb
void(* uv_connection_cb)(uv_stream_t *server, int status)
Definition: uv.h:315
uv__stream_fd
#define uv__stream_fd(handle)
Definition: third_party/libuv/src/unix/internal.h:285
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv__handle_type
uv_handle_type uv__handle_type(int fd)
Definition: unix/stream.c:958
uv_pipe_getpeername
int uv_pipe_getpeername(const uv_pipe_t *handle, char *buffer, size_t *size)
Definition: unix/pipe.c:289
uv_pipe_s
Definition: uv.h:757
unlink
#define unlink
Definition: test-fs-copyfile.c:33
stat
#define stat
Definition: test-fs.c:50
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
uv__stream_queued_fds_s
Definition: third_party/libuv/src/unix/internal.h:155
flags
uint32_t flags
Definition: retry_filter.cc:632
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv__stream_open
int uv__stream_open(uv_stream_t *, int fd, int flags)
Definition: unix/stream.c:406
errno.h
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
uv__stream_queued_fds_s::offset
unsigned int offset
Definition: third_party/libuv/src/unix/internal.h:157
uv_pipe_chmod
int uv_pipe_chmod(uv_pipe_t *handle, int mode)
Definition: unix/pipe.c:326
uv__close
int uv__close(int fd)
Definition: unix/core.c:557
UV_UNKNOWN_HANDLE
@ UV_UNKNOWN_HANDLE
Definition: uv.h:190


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:52