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 #include <stdbool.h>
32 
33 
uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc)34 int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc) {
35   uv__stream_init(loop, (uv_stream_t*)handle, UV_NAMED_PIPE);
36   handle->shutdown_req = NULL;
37   handle->connect_req = NULL;
38   handle->pipe_fname = NULL;
39   handle->ipc = ipc;
40   return 0;
41 }
42 
43 
uv_pipe_bind(uv_pipe_t* handle, const char* name)44 int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
45   struct sockaddr_un saddr;
46   const char* pipe_fname;
47   int sockfd;
48   int err;
49 
50   pipe_fname = NULL;
51 
52   /* Already bound? */
53   if (uv__stream_fd(handle) >= 0)
54     return UV_EINVAL;
55   if (uv__is_closing(handle)) {
56     return UV_EINVAL;
57   }
58   /* Make a copy of the file name, it outlives this function's scope. */
59   bool use_abstract = (name != NULL) && (*name == '\0');
60   if (use_abstract) {
61     size_t len = strlen(name + 1) + 2;
62     char* m = uv__malloc(len);
63     if (m == NULL) {
64       return UV_ENOMEM;
65     }
66     pipe_fname = memcpy(m, name, len);
67   } else {
68     pipe_fname = uv__strdup(name);
69   }
70   if (pipe_fname == NULL)
71     return UV_ENOMEM;
72 
73   /* We've got a copy, don't touch the original any more. */
74   name = NULL;
75 
76   err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
77   if (err < 0)
78     goto err_socket;
79   sockfd = err;
80 
81   memset(&saddr, 0, sizeof saddr);
82   if (use_abstract) {
83     saddr.sun_path[0] = '\0';
84     uv__strscpy(saddr.sun_path + 1, pipe_fname + 1, sizeof(saddr.sun_path));
85   } else {
86     uv__strscpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
87   }
88   saddr.sun_family = AF_UNIX;
89 
90   if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr)) {
91     err = UV__ERR(errno);
92     /* Convert ENOENT to EACCES for compatibility with Windows. */
93     if (err == UV_ENOENT)
94       err = UV_EACCES;
95 
96     uv__close(sockfd);
97     goto err_socket;
98   }
99 
100   /* Success. */
101   handle->flags |= UV_HANDLE_BOUND;
102   handle->pipe_fname = pipe_fname; /* Is a strdup'ed copy. */
103   handle->io_watcher.fd = sockfd;
104   return 0;
105 
106 err_socket:
107   uv__free((void*)pipe_fname);
108   return err;
109 }
110 
111 
uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb)112 int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
113   if (uv__stream_fd(handle) == -1)
114     return UV_EINVAL;
115 
116   if (handle->ipc)
117     return UV_EINVAL;
118 
119 #if defined(__MVS__) || defined(__PASE__)
120   /* On zOS, backlog=0 has undefined behaviour */
121   /* On IBMi PASE, backlog=0 leads to "Connection refused" error */
122   if (backlog == 0)
123     backlog = 1;
124   else if (backlog < 0)
125     backlog = SOMAXCONN;
126 #endif
127 
128   if (listen(uv__stream_fd(handle), backlog))
129     return UV__ERR(errno);
130 
131   handle->connection_cb = cb;
132   handle->io_watcher.cb = uv__server_io;
133   uv__io_start(handle->loop, &handle->io_watcher, POLLIN);
134   return 0;
135 }
136 
137 
uv__pipe_close(uv_pipe_t* handle)138 void uv__pipe_close(uv_pipe_t* handle) {
139   if (handle->pipe_fname) {
140     /*
141      * Unlink the file system entity before closing the file descriptor.
142      * Doing it the other way around introduces a race where our process
143      * unlinks a socket with the same name that's just been created by
144      * another thread or process.
145      */
146     unlink(handle->pipe_fname);
147     uv__free((void*)handle->pipe_fname);
148     handle->pipe_fname = NULL;
149   }
150 
151   uv__stream_close((uv_stream_t*)handle);
152 }
153 
154 
uv_pipe_open(uv_pipe_t* handle, uv_file fd)155 int uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
156   int flags;
157   int mode;
158   int err;
159   flags = 0;
160 
161   if (uv__fd_exists(handle->loop, fd))
162     return UV_EEXIST;
163 
164   do
165     mode = fcntl(fd, F_GETFL);
166   while (mode == -1 && errno == EINTR);
167 
168   if (mode == -1)
169     return UV__ERR(errno); /* according to docs, must be EBADF */
170 
171   err = uv__nonblock(fd, 1);
172   if (err)
173     return err;
174 
175 #if defined(__APPLE__)
176   err = uv__stream_try_select((uv_stream_t*) handle, &fd);
177   if (err)
178     return err;
179 #endif /* defined(__APPLE__) */
180 
181   mode &= O_ACCMODE;
182   if (mode != O_WRONLY)
183     flags |= UV_HANDLE_READABLE;
184   if (mode != O_RDONLY)
185     flags |= UV_HANDLE_WRITABLE;
186 
187   return uv__stream_open((uv_stream_t*)handle, fd, flags);
188 }
189 
190 
uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle, const char* name, uv_connect_cb cb)191 void uv_pipe_connect(uv_connect_t* req,
192                     uv_pipe_t* handle,
193                     const char* name,
194                     uv_connect_cb cb) {
195   struct sockaddr_un saddr;
196   int new_sock;
197   int err;
198   int r;
199 
200   new_sock = (uv__stream_fd(handle) == -1);
201 
202   if (new_sock) {
203     err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
204     if (err < 0)
205       goto out;
206     handle->io_watcher.fd = err;
207   }
208 
209   memset(&saddr, 0, sizeof saddr);
210   uv__strscpy(saddr.sun_path, name, sizeof(saddr.sun_path));
211   saddr.sun_family = AF_UNIX;
212 
213   do {
214     r = connect(uv__stream_fd(handle),
215                 (struct sockaddr*)&saddr, sizeof saddr);
216   }
217   while (r == -1 && errno == EINTR);
218 
219   if (r == -1 && errno != EINPROGRESS) {
220     err = UV__ERR(errno);
221 #if defined(__CYGWIN__) || defined(__MSYS__)
222     /* EBADF is supposed to mean that the socket fd is bad, but
223        Cygwin reports EBADF instead of ENOTSOCK when the file is
224        not a socket.  We do not expect to see a bad fd here
225        (e.g. due to new_sock), so translate the error.  */
226     if (err == UV_EBADF)
227       err = UV_ENOTSOCK;
228 #endif
229     goto out;
230   }
231 
232   err = 0;
233   if (new_sock) {
234     err = uv__stream_open((uv_stream_t*)handle,
235                           uv__stream_fd(handle),
236                           UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
237   }
238 
239   if (err == 0)
240     uv__io_start(handle->loop, &handle->io_watcher, POLLOUT);
241 
242 out:
243   handle->delayed_error = err;
244   handle->connect_req = req;
245 
246   uv__req_init(handle->loop, req, UV_CONNECT);
247   req->handle = (uv_stream_t*)handle;
248   req->cb = cb;
249   QUEUE_INIT(&req->queue);
250 
251   /* Force callback to run on next tick in case of error. */
252   if (err)
253     uv__io_feed(handle->loop, &handle->io_watcher);
254 
255 }
256 
257 
uv__pipe_getsockpeername(const uv_pipe_t* handle, uv__peersockfunc func, char* buffer, size_t* size)258 static int uv__pipe_getsockpeername(const uv_pipe_t* handle,
259                                     uv__peersockfunc func,
260                                     char* buffer,
261                                     size_t* size) {
262   struct sockaddr_un sa;
263   socklen_t addrlen;
264   int err;
265 
266   addrlen = sizeof(sa);
267   memset(&sa, 0, addrlen);
268   err = uv__getsockpeername((const uv_handle_t*) handle,
269                             func,
270                             (struct sockaddr*) &sa,
271                             (int*) &addrlen);
272   if (err < 0) {
273     *size = 0;
274     return err;
275   }
276 
277 #if defined(__linux__)
278   if (sa.sun_path[0] == 0)
279     /* Linux abstract namespace */
280     addrlen -= offsetof(struct sockaddr_un, sun_path);
281   else
282 #endif
283     addrlen = strlen(sa.sun_path);
284 
285 
286   if ((size_t)addrlen >= *size) {
287     *size = addrlen + 1;
288     return UV_ENOBUFS;
289   }
290 
291   memcpy(buffer, sa.sun_path, addrlen);
292   *size = addrlen;
293 
294   /* only null-terminate if it's not an abstract socket */
295   if (buffer[0] != '\0')
296     buffer[addrlen] = '\0';
297 
298   return 0;
299 }
300 
301 
uv_pipe_getsockname(const uv_pipe_t* handle, char* buffer, size_t* size)302 int uv_pipe_getsockname(const uv_pipe_t* handle, char* buffer, size_t* size) {
303   return uv__pipe_getsockpeername(handle, getsockname, buffer, size);
304 }
305 
306 
uv_pipe_getpeername(const uv_pipe_t* handle, char* buffer, size_t* size)307 int uv_pipe_getpeername(const uv_pipe_t* handle, char* buffer, size_t* size) {
308   return uv__pipe_getsockpeername(handle, getpeername, buffer, size);
309 }
310 
311 
uv_pipe_pending_instances(uv_pipe_t* handle, int count)312 void uv_pipe_pending_instances(uv_pipe_t* handle, int count) {
313 }
314 
315 
uv_pipe_pending_count(uv_pipe_t* handle)316 int uv_pipe_pending_count(uv_pipe_t* handle) {
317   uv__stream_queued_fds_t* queued_fds;
318 
319   if (!handle->ipc)
320     return 0;
321 
322   if (handle->accepted_fd == -1)
323     return 0;
324 
325   if (handle->queued_fds == NULL)
326     return 1;
327 
328   queued_fds = handle->queued_fds;
329   return queued_fds->offset + 1;
330 }
331 
332 
uv_pipe_pending_type(uv_pipe_t* handle)333 uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle) {
334   if (!handle->ipc)
335     return UV_UNKNOWN_HANDLE;
336 
337   if (handle->accepted_fd == -1)
338     return UV_UNKNOWN_HANDLE;
339   else
340     return uv_guess_handle(handle->accepted_fd);
341 }
342 
343 
uv_pipe_chmod(uv_pipe_t* handle, int mode)344 int uv_pipe_chmod(uv_pipe_t* handle, int mode) {
345   unsigned desired_mode;
346   struct stat pipe_stat;
347   char* name_buffer;
348   size_t name_len;
349   int r;
350 
351   if (handle == NULL || uv__stream_fd(handle) == -1)
352     return UV_EBADF;
353 
354   if (mode != UV_READABLE &&
355       mode != UV_WRITABLE &&
356       mode != (UV_WRITABLE | UV_READABLE))
357     return UV_EINVAL;
358 
359   /* Unfortunately fchmod does not work on all platforms, we will use chmod. */
360   name_len = 0;
361   r = uv_pipe_getsockname(handle, NULL, &name_len);
362   if (r != UV_ENOBUFS)
363     return r;
364 
365   name_buffer = uv__malloc(name_len);
366   if (name_buffer == NULL)
367     return UV_ENOMEM;
368 
369   r = uv_pipe_getsockname(handle, name_buffer, &name_len);
370   if (r != 0) {
371     uv__free(name_buffer);
372     return r;
373   }
374 
375   /* stat must be used as fstat has a bug on Darwin */
376   if (stat(name_buffer, &pipe_stat) == -1) {
377     uv__free(name_buffer);
378     return -errno;
379   }
380 
381   desired_mode = 0;
382   if (mode & UV_READABLE)
383     desired_mode |= S_IRUSR | S_IRGRP | S_IROTH;
384   if (mode & UV_WRITABLE)
385     desired_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
386 
387   /* Exit early if pipe already has desired mode. */
388   if ((pipe_stat.st_mode & desired_mode) == desired_mode) {
389     uv__free(name_buffer);
390     return 0;
391   }
392 
393   pipe_stat.st_mode |= desired_mode;
394 
395   r = chmod(name_buffer, pipe_stat.st_mode);
396   uv__free(name_buffer);
397 
398   return r != -1 ? 0 : UV__ERR(errno);
399 }
400 
401 
uv_pipe(uv_os_fd_t fds[2], int read_flags, int write_flags)402 int uv_pipe(uv_os_fd_t fds[2], int read_flags, int write_flags) {
403   uv_os_fd_t temp[2];
404   int err;
405 #if defined(__FreeBSD__) || defined(__linux__)
406   int flags = O_CLOEXEC;
407 
408   if ((read_flags & UV_NONBLOCK_PIPE) && (write_flags & UV_NONBLOCK_PIPE))
409     flags |= UV_FS_O_NONBLOCK;
410 
411   if (pipe2(temp, flags))
412     return UV__ERR(errno);
413 
414   if (flags & UV_FS_O_NONBLOCK) {
415     fds[0] = temp[0];
416     fds[1] = temp[1];
417     return 0;
418   }
419 #else
420   if (pipe(temp))
421     return UV__ERR(errno);
422 
423   if ((err = uv__cloexec(temp[0], 1)))
424     goto fail;
425 
426   if ((err = uv__cloexec(temp[1], 1)))
427     goto fail;
428 #endif
429 
430   if (read_flags & UV_NONBLOCK_PIPE)
431     if ((err = uv__nonblock(temp[0], 1)))
432       goto fail;
433 
434   if (write_flags & UV_NONBLOCK_PIPE)
435     if ((err = uv__nonblock(temp[1], 1)))
436       goto fail;
437 
438   fds[0] = temp[0];
439   fds[1] = temp[1];
440   return 0;
441 
442 fail:
443   uv__close(temp[0]);
444   uv__close(temp[1]);
445   return err;
446 }
447 
448 
uv__make_pipe(int fds[2], int flags)449 int uv__make_pipe(int fds[2], int flags) {
450   return uv_pipe(fds,
451                  flags & UV_NONBLOCK_PIPE,
452                  flags & UV_NONBLOCK_PIPE);
453 }
454