xref: /libuv/src/unix/pipe.c (revision 1eac3310)
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 
33 /* Does the file path contain embedded nul bytes? */
includes_nul(const char * s,size_t n)34 static int includes_nul(const char *s, size_t n) {
35   if (n == 0)
36     return 0;
37 #ifdef __linux__
38   /* Accept abstract socket namespace path ("\0/virtual/path"). */
39   s++;
40   n--;
41 #endif
42   return NULL != memchr(s, '\0', n);
43 }
44 
45 
uv_pipe_init(uv_loop_t * loop,uv_pipe_t * handle,int ipc)46 int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc) {
47   uv__stream_init(loop, (uv_stream_t*)handle, UV_NAMED_PIPE);
48   handle->shutdown_req = NULL;
49   handle->connect_req = NULL;
50   handle->pipe_fname = NULL;
51   handle->ipc = ipc;
52   return 0;
53 }
54 
55 
uv_pipe_bind(uv_pipe_t * handle,const char * name)56 int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
57   return uv_pipe_bind2(handle, name, strlen(name), 0);
58 }
59 
60 
uv_pipe_bind2(uv_pipe_t * handle,const char * name,size_t namelen,unsigned int flags)61 int uv_pipe_bind2(uv_pipe_t* handle,
62                   const char* name,
63                   size_t namelen,
64                   unsigned int flags) {
65   struct sockaddr_un saddr;
66   char* pipe_fname;
67   int sockfd;
68   int err;
69   socklen_t addrlen;
70 
71   pipe_fname = NULL;
72 
73   if (flags & ~UV_PIPE_NO_TRUNCATE)
74     return UV_EINVAL;
75 
76   if (name == NULL)
77     return UV_EINVAL;
78 
79   /* namelen==0 on Linux means autobind the listen socket in the abstract
80    * socket namespace, see `man 7 unix` for details.
81    */
82 #if !defined(__linux__)
83   if (namelen == 0)
84     return UV_EINVAL;
85 #endif
86 
87   if (includes_nul(name, namelen))
88     return UV_EINVAL;
89 
90   if (flags & UV_PIPE_NO_TRUNCATE)
91     if (namelen > sizeof(saddr.sun_path))
92       return UV_EINVAL;
93 
94   /* Truncate long paths. Documented behavior. */
95   if (namelen > sizeof(saddr.sun_path))
96     namelen = sizeof(saddr.sun_path);
97 
98   /* Already bound? */
99   if (uv__stream_fd(handle) >= 0)
100     return UV_EINVAL;
101 
102   if (uv__is_closing(handle))
103     return UV_EINVAL;
104 
105   /* Make a copy of the file path unless it is an abstract socket.
106    * We unlink the file later but abstract sockets disappear
107    * automatically since they're not real file system entities.
108    */
109   if (*name == '\0') {
110     addrlen = offsetof(struct sockaddr_un, sun_path) + namelen;
111   } else {
112     pipe_fname = uv__malloc(namelen + 1);
113     if (pipe_fname == NULL)
114       return UV_ENOMEM;
115     memcpy(pipe_fname, name, namelen);
116     pipe_fname[namelen] = '\0';
117     addrlen = sizeof saddr;
118   }
119 
120   err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
121   if (err < 0)
122     goto err_socket;
123   sockfd = err;
124 
125   memset(&saddr, 0, sizeof saddr);
126   memcpy(&saddr.sun_path, name, namelen);
127   saddr.sun_family = AF_UNIX;
128 
129   if (bind(sockfd, (struct sockaddr*)&saddr, addrlen)) {
130     err = UV__ERR(errno);
131     /* Convert ENOENT to EACCES for compatibility with Windows. */
132     if (err == UV_ENOENT)
133       err = UV_EACCES;
134 
135     uv__close(sockfd);
136     goto err_socket;
137   }
138 
139   /* Success. */
140   handle->flags |= UV_HANDLE_BOUND;
141   handle->pipe_fname = pipe_fname; /* NULL or a copy of |name| */
142   handle->io_watcher.fd = sockfd;
143   return 0;
144 
145 err_socket:
146   uv__free(pipe_fname);
147   return err;
148 }
149 
150 
uv__pipe_listen(uv_pipe_t * handle,int backlog,uv_connection_cb cb)151 int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
152   if (uv__stream_fd(handle) == -1)
153     return UV_EINVAL;
154 
155   if (handle->ipc)
156     return UV_EINVAL;
157 
158 #if defined(__MVS__) || defined(__PASE__)
159   /* On zOS, backlog=0 has undefined behaviour */
160   /* On IBMi PASE, backlog=0 leads to "Connection refused" error */
161   if (backlog == 0)
162     backlog = 1;
163   else if (backlog < 0)
164     backlog = SOMAXCONN;
165 #endif
166 
167   if (listen(uv__stream_fd(handle), backlog))
168     return UV__ERR(errno);
169 
170   handle->connection_cb = cb;
171   handle->io_watcher.cb = uv__server_io;
172   uv__io_start(handle->loop, &handle->io_watcher, POLLIN);
173   return 0;
174 }
175 
176 
uv__pipe_close(uv_pipe_t * handle)177 void uv__pipe_close(uv_pipe_t* handle) {
178   if (handle->pipe_fname) {
179     /*
180      * Unlink the file system entity before closing the file descriptor.
181      * Doing it the other way around introduces a race where our process
182      * unlinks a socket with the same name that's just been created by
183      * another thread or process.
184      */
185     unlink(handle->pipe_fname);
186     uv__free((void*)handle->pipe_fname);
187     handle->pipe_fname = NULL;
188   }
189 
190   uv__stream_close((uv_stream_t*)handle);
191 }
192 
193 
uv_pipe_open(uv_pipe_t * handle,uv_file fd)194 int uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
195   int flags;
196   int mode;
197   int err;
198   flags = 0;
199 
200   if (uv__fd_exists(handle->loop, fd))
201     return UV_EEXIST;
202 
203   do
204     mode = fcntl(fd, F_GETFL);
205   while (mode == -1 && errno == EINTR);
206 
207   if (mode == -1)
208     return UV__ERR(errno); /* according to docs, must be EBADF */
209 
210   err = uv__nonblock(fd, 1);
211   if (err)
212     return err;
213 
214 #if defined(__APPLE__)
215   err = uv__stream_try_select((uv_stream_t*) handle, &fd);
216   if (err)
217     return err;
218 #endif /* defined(__APPLE__) */
219 
220   mode &= O_ACCMODE;
221   if (mode != O_WRONLY)
222     flags |= UV_HANDLE_READABLE;
223   if (mode != O_RDONLY)
224     flags |= UV_HANDLE_WRITABLE;
225 
226   return uv__stream_open((uv_stream_t*)handle, fd, flags);
227 }
228 
229 
uv_pipe_connect(uv_connect_t * req,uv_pipe_t * handle,const char * name,uv_connect_cb cb)230 void uv_pipe_connect(uv_connect_t* req,
231                     uv_pipe_t* handle,
232                     const char* name,
233                     uv_connect_cb cb) {
234   int err;
235 
236   err = uv_pipe_connect2(req, handle, name, strlen(name), 0, cb);
237 
238   if (err) {
239     handle->delayed_error = err;
240     handle->connect_req = req;
241 
242     uv__req_init(handle->loop, req, UV_CONNECT);
243     req->handle = (uv_stream_t*) handle;
244     req->cb = cb;
245     uv__queue_init(&req->queue);
246 
247     /* Force callback to run on next tick in case of error. */
248     uv__io_feed(handle->loop, &handle->io_watcher);
249   }
250 }
251 
252 
uv_pipe_connect2(uv_connect_t * req,uv_pipe_t * handle,const char * name,size_t namelen,unsigned int flags,uv_connect_cb cb)253 int uv_pipe_connect2(uv_connect_t* req,
254                      uv_pipe_t* handle,
255                      const char* name,
256                      size_t namelen,
257                      unsigned int flags,
258                      uv_connect_cb cb) {
259   struct sockaddr_un saddr;
260   int new_sock;
261   int err;
262   int r;
263   socklen_t addrlen;
264 
265   if (flags & ~UV_PIPE_NO_TRUNCATE)
266     return UV_EINVAL;
267 
268   if (name == NULL)
269     return UV_EINVAL;
270 
271   if (namelen == 0)
272     return UV_EINVAL;
273 
274   if (includes_nul(name, namelen))
275     return UV_EINVAL;
276 
277   if (flags & UV_PIPE_NO_TRUNCATE)
278     if (namelen > sizeof(saddr.sun_path))
279       return UV_EINVAL;
280 
281   /* Truncate long paths. Documented behavior. */
282   if (namelen > sizeof(saddr.sun_path))
283     namelen = sizeof(saddr.sun_path);
284 
285   new_sock = (uv__stream_fd(handle) == -1);
286 
287   if (new_sock) {
288     err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
289     if (err < 0)
290       goto out;
291     handle->io_watcher.fd = err;
292   }
293 
294   memset(&saddr, 0, sizeof saddr);
295   memcpy(&saddr.sun_path, name, namelen);
296   saddr.sun_family = AF_UNIX;
297 
298   if (*name == '\0')
299     addrlen = offsetof(struct sockaddr_un, sun_path) + namelen;
300   else
301     addrlen = sizeof saddr;
302 
303   do {
304     r = connect(uv__stream_fd(handle), (struct sockaddr*)&saddr, addrlen);
305   }
306   while (r == -1 && errno == EINTR);
307 
308   if (r == -1 && errno != EINPROGRESS) {
309     err = UV__ERR(errno);
310 #if defined(__CYGWIN__) || defined(__MSYS__)
311     /* EBADF is supposed to mean that the socket fd is bad, but
312        Cygwin reports EBADF instead of ENOTSOCK when the file is
313        not a socket.  We do not expect to see a bad fd here
314        (e.g. due to new_sock), so translate the error.  */
315     if (err == UV_EBADF)
316       err = UV_ENOTSOCK;
317 #endif
318     goto out;
319   }
320 
321   err = 0;
322   if (new_sock) {
323     err = uv__stream_open((uv_stream_t*)handle,
324                           uv__stream_fd(handle),
325                           UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
326   }
327 
328   if (err == 0)
329     uv__io_start(handle->loop, &handle->io_watcher, POLLOUT);
330 
331 out:
332   handle->delayed_error = err;
333   handle->connect_req = req;
334 
335   uv__req_init(handle->loop, req, UV_CONNECT);
336   req->handle = (uv_stream_t*) handle;
337   req->cb = cb;
338   uv__queue_init(&req->queue);
339 
340   /* Force callback to run on next tick in case of error. */
341   if (err)
342     uv__io_feed(handle->loop, &handle->io_watcher);
343 
344   return 0;
345 }
346 
347 
uv__pipe_getsockpeername(const uv_pipe_t * handle,uv__peersockfunc func,char * buffer,size_t * size)348 static int uv__pipe_getsockpeername(const uv_pipe_t* handle,
349                                     uv__peersockfunc func,
350                                     char* buffer,
351                                     size_t* size) {
352 #if defined(__linux__)
353   static const int is_linux = 1;
354 #else
355   static const int is_linux = 0;
356 #endif
357   struct sockaddr_un sa;
358   socklen_t addrlen;
359   size_t slop;
360   char* p;
361   int err;
362 
363   addrlen = sizeof(sa);
364   memset(&sa, 0, addrlen);
365   err = uv__getsockpeername((const uv_handle_t*) handle,
366                             func,
367                             (struct sockaddr*) &sa,
368                             (int*) &addrlen);
369   if (err < 0) {
370     *size = 0;
371     return err;
372   }
373 
374   slop = 1;
375   if (is_linux && sa.sun_path[0] == '\0') {
376     /* Linux abstract namespace. Not zero-terminated. */
377     slop = 0;
378     addrlen -= offsetof(struct sockaddr_un, sun_path);
379   } else {
380     p = memchr(sa.sun_path, '\0', sizeof(sa.sun_path));
381     if (p == NULL)
382       p = ARRAY_END(sa.sun_path);
383     addrlen = p - sa.sun_path;
384   }
385 
386   if ((size_t)addrlen + slop > *size) {
387     *size = addrlen + slop;
388     return UV_ENOBUFS;
389   }
390 
391   memcpy(buffer, sa.sun_path, addrlen);
392   *size = addrlen;
393 
394   /* only null-terminate if it's not an abstract socket */
395   if (buffer[0] != '\0')
396     buffer[addrlen] = '\0';
397 
398   return 0;
399 }
400 
401 
uv_pipe_getsockname(const uv_pipe_t * handle,char * buffer,size_t * size)402 int uv_pipe_getsockname(const uv_pipe_t* handle, char* buffer, size_t* size) {
403   return uv__pipe_getsockpeername(handle, getsockname, buffer, size);
404 }
405 
406 
uv_pipe_getpeername(const uv_pipe_t * handle,char * buffer,size_t * size)407 int uv_pipe_getpeername(const uv_pipe_t* handle, char* buffer, size_t* size) {
408   return uv__pipe_getsockpeername(handle, getpeername, buffer, size);
409 }
410 
411 
uv_pipe_pending_instances(uv_pipe_t * handle,int count)412 void uv_pipe_pending_instances(uv_pipe_t* handle, int count) {
413 }
414 
415 
uv_pipe_pending_count(uv_pipe_t * handle)416 int uv_pipe_pending_count(uv_pipe_t* handle) {
417   uv__stream_queued_fds_t* queued_fds;
418 
419   if (!handle->ipc)
420     return 0;
421 
422   if (handle->accepted_fd == -1)
423     return 0;
424 
425   if (handle->queued_fds == NULL)
426     return 1;
427 
428   queued_fds = handle->queued_fds;
429   return queued_fds->offset + 1;
430 }
431 
432 
uv_pipe_pending_type(uv_pipe_t * handle)433 uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle) {
434   if (!handle->ipc)
435     return UV_UNKNOWN_HANDLE;
436 
437   if (handle->accepted_fd == -1)
438     return UV_UNKNOWN_HANDLE;
439   else
440     return uv_guess_handle(handle->accepted_fd);
441 }
442 
443 
uv_pipe_chmod(uv_pipe_t * handle,int mode)444 int uv_pipe_chmod(uv_pipe_t* handle, int mode) {
445   unsigned desired_mode;
446   struct stat pipe_stat;
447   char* name_buffer;
448   size_t name_len;
449   int r;
450 
451   if (handle == NULL || uv__stream_fd(handle) == -1)
452     return UV_EBADF;
453 
454   if (mode != UV_READABLE &&
455       mode != UV_WRITABLE &&
456       mode != (UV_WRITABLE | UV_READABLE))
457     return UV_EINVAL;
458 
459   /* Unfortunately fchmod does not work on all platforms, we will use chmod. */
460   name_len = 0;
461   r = uv_pipe_getsockname(handle, NULL, &name_len);
462   if (r != UV_ENOBUFS)
463     return r;
464 
465   name_buffer = uv__malloc(name_len);
466   if (name_buffer == NULL)
467     return UV_ENOMEM;
468 
469   r = uv_pipe_getsockname(handle, name_buffer, &name_len);
470   if (r != 0) {
471     uv__free(name_buffer);
472     return r;
473   }
474 
475   /* stat must be used as fstat has a bug on Darwin */
476   if (uv__stat(name_buffer, &pipe_stat) == -1) {
477     uv__free(name_buffer);
478     return -errno;
479   }
480 
481   desired_mode = 0;
482   if (mode & UV_READABLE)
483     desired_mode |= S_IRUSR | S_IRGRP | S_IROTH;
484   if (mode & UV_WRITABLE)
485     desired_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
486 
487   /* Exit early if pipe already has desired mode. */
488   if ((pipe_stat.st_mode & desired_mode) == desired_mode) {
489     uv__free(name_buffer);
490     return 0;
491   }
492 
493   pipe_stat.st_mode |= desired_mode;
494 
495   r = chmod(name_buffer, pipe_stat.st_mode);
496   uv__free(name_buffer);
497 
498   return r != -1 ? 0 : UV__ERR(errno);
499 }
500 
501 
uv_pipe(uv_os_fd_t fds[2],int read_flags,int write_flags)502 int uv_pipe(uv_os_fd_t fds[2], int read_flags, int write_flags) {
503   uv_os_fd_t temp[2];
504   int err;
505 #if defined(__linux__) || \
506     defined(__FreeBSD__) || \
507     defined(__OpenBSD__) || \
508     defined(__DragonFly__) || \
509     defined(__NetBSD__)
510   int flags = O_CLOEXEC;
511 
512   if ((read_flags & UV_NONBLOCK_PIPE) && (write_flags & UV_NONBLOCK_PIPE))
513     flags |= UV_FS_O_NONBLOCK;
514 
515   if (pipe2(temp, flags))
516     return UV__ERR(errno);
517 
518   if (flags & UV_FS_O_NONBLOCK) {
519     fds[0] = temp[0];
520     fds[1] = temp[1];
521     return 0;
522   }
523 #else
524   if (pipe(temp))
525     return UV__ERR(errno);
526 
527   if ((err = uv__cloexec(temp[0], 1)))
528     goto fail;
529 
530   if ((err = uv__cloexec(temp[1], 1)))
531     goto fail;
532 #endif
533 
534   if (read_flags & UV_NONBLOCK_PIPE)
535     if ((err = uv__nonblock(temp[0], 1)))
536       goto fail;
537 
538   if (write_flags & UV_NONBLOCK_PIPE)
539     if ((err = uv__nonblock(temp[1], 1)))
540       goto fail;
541 
542   fds[0] = temp[0];
543   fds[1] = temp[1];
544   return 0;
545 
546 fail:
547   uv__close(temp[0]);
548   uv__close(temp[1]);
549   return err;
550 }
551 
552 
uv__make_pipe(int fds[2],int flags)553 int uv__make_pipe(int fds[2], int flags) {
554   return uv_pipe(fds,
555                  flags & UV_NONBLOCK_PIPE,
556                  flags & UV_NONBLOCK_PIPE);
557 }
558