xref: /libuv/src/unix/pipe.c (revision 31ea3411)
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   if (buffer == NULL || size == NULL || *size == 0)
364     return UV_EINVAL;
365 
366   addrlen = sizeof(sa);
367   memset(&sa, 0, addrlen);
368   err = uv__getsockpeername((const uv_handle_t*) handle,
369                             func,
370                             (struct sockaddr*) &sa,
371                             (int*) &addrlen);
372   if (err < 0) {
373     *size = 0;
374     return err;
375   }
376 
377   slop = 1;
378   if (is_linux && sa.sun_path[0] == '\0') {
379     /* Linux abstract namespace. Not zero-terminated. */
380     slop = 0;
381     addrlen -= offsetof(struct sockaddr_un, sun_path);
382   } else {
383     p = memchr(sa.sun_path, '\0', sizeof(sa.sun_path));
384     if (p == NULL)
385       p = ARRAY_END(sa.sun_path);
386     addrlen = p - sa.sun_path;
387   }
388 
389   if ((size_t)addrlen + slop > *size) {
390     *size = addrlen + slop;
391     return UV_ENOBUFS;
392   }
393 
394   memcpy(buffer, sa.sun_path, addrlen);
395   *size = addrlen;
396 
397   /* only null-terminate if it's not an abstract socket */
398   if (buffer[0] != '\0')
399     buffer[addrlen] = '\0';
400 
401   return 0;
402 }
403 
404 
uv_pipe_getsockname(const uv_pipe_t * handle,char * buffer,size_t * size)405 int uv_pipe_getsockname(const uv_pipe_t* handle, char* buffer, size_t* size) {
406   return uv__pipe_getsockpeername(handle, getsockname, buffer, size);
407 }
408 
409 
uv_pipe_getpeername(const uv_pipe_t * handle,char * buffer,size_t * size)410 int uv_pipe_getpeername(const uv_pipe_t* handle, char* buffer, size_t* size) {
411   return uv__pipe_getsockpeername(handle, getpeername, buffer, size);
412 }
413 
414 
uv_pipe_pending_instances(uv_pipe_t * handle,int count)415 void uv_pipe_pending_instances(uv_pipe_t* handle, int count) {
416 }
417 
418 
uv_pipe_pending_count(uv_pipe_t * handle)419 int uv_pipe_pending_count(uv_pipe_t* handle) {
420   uv__stream_queued_fds_t* queued_fds;
421 
422   if (!handle->ipc)
423     return 0;
424 
425   if (handle->accepted_fd == -1)
426     return 0;
427 
428   if (handle->queued_fds == NULL)
429     return 1;
430 
431   queued_fds = handle->queued_fds;
432   return queued_fds->offset + 1;
433 }
434 
435 
uv_pipe_pending_type(uv_pipe_t * handle)436 uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle) {
437   if (!handle->ipc)
438     return UV_UNKNOWN_HANDLE;
439 
440   if (handle->accepted_fd == -1)
441     return UV_UNKNOWN_HANDLE;
442   else
443     return uv_guess_handle(handle->accepted_fd);
444 }
445 
446 
uv_pipe_chmod(uv_pipe_t * handle,int mode)447 int uv_pipe_chmod(uv_pipe_t* handle, int mode) {
448   unsigned desired_mode;
449   struct stat pipe_stat;
450   char name_buffer[1 + UV__PATH_MAX];
451   size_t name_len;
452   int r;
453 
454   if (handle == NULL || uv__stream_fd(handle) == -1)
455     return UV_EBADF;
456 
457   if (mode != UV_READABLE &&
458       mode != UV_WRITABLE &&
459       mode != (UV_WRITABLE | UV_READABLE))
460     return UV_EINVAL;
461 
462   /* Unfortunately fchmod does not work on all platforms, we will use chmod. */
463   name_len = sizeof(name_buffer);
464   r = uv_pipe_getsockname(handle, name_buffer, &name_len);
465   if (r != 0)
466     return r;
467 
468   /* stat must be used as fstat has a bug on Darwin */
469   if (uv__stat(name_buffer, &pipe_stat) == -1)
470     return UV__ERR(errno);
471 
472   desired_mode = 0;
473   if (mode & UV_READABLE)
474     desired_mode |= S_IRUSR | S_IRGRP | S_IROTH;
475   if (mode & UV_WRITABLE)
476     desired_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
477 
478   /* Exit early if pipe already has desired mode. */
479   if ((pipe_stat.st_mode & desired_mode) == desired_mode)
480     return 0;
481 
482   pipe_stat.st_mode |= desired_mode;
483 
484   r = chmod(name_buffer, pipe_stat.st_mode);
485 
486   return r != -1 ? 0 : UV__ERR(errno);
487 }
488 
489 
uv_pipe(uv_os_fd_t fds[2],int read_flags,int write_flags)490 int uv_pipe(uv_os_fd_t fds[2], int read_flags, int write_flags) {
491   uv_os_fd_t temp[2];
492   int err;
493 #if defined(__linux__) || \
494     defined(__FreeBSD__) || \
495     defined(__OpenBSD__) || \
496     defined(__DragonFly__) || \
497     defined(__NetBSD__)
498   int flags = O_CLOEXEC;
499 
500   if ((read_flags & UV_NONBLOCK_PIPE) && (write_flags & UV_NONBLOCK_PIPE))
501     flags |= UV_FS_O_NONBLOCK;
502 
503   if (pipe2(temp, flags))
504     return UV__ERR(errno);
505 
506   if (flags & UV_FS_O_NONBLOCK) {
507     fds[0] = temp[0];
508     fds[1] = temp[1];
509     return 0;
510   }
511 #else
512   if (pipe(temp))
513     return UV__ERR(errno);
514 
515   if ((err = uv__cloexec(temp[0], 1)))
516     goto fail;
517 
518   if ((err = uv__cloexec(temp[1], 1)))
519     goto fail;
520 #endif
521 
522   if (read_flags & UV_NONBLOCK_PIPE)
523     if ((err = uv__nonblock(temp[0], 1)))
524       goto fail;
525 
526   if (write_flags & UV_NONBLOCK_PIPE)
527     if ((err = uv__nonblock(temp[1], 1)))
528       goto fail;
529 
530   fds[0] = temp[0];
531   fds[1] = temp[1];
532   return 0;
533 
534 fail:
535   uv__close(temp[0]);
536   uv__close(temp[1]);
537   return err;
538 }
539 
540 
uv__make_pipe(int fds[2],int flags)541 int uv__make_pipe(int fds[2], int flags) {
542   return uv_pipe(fds,
543                  flags & UV_NONBLOCK_PIPE,
544                  flags & UV_NONBLOCK_PIPE);
545 }
546