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
33int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc) {
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
43int 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
88err_socket:
89 uv__free((void*)pipe_fname);
90 return err;
91}
92
93
94int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
95 if (uv__stream_fd(handle) == -1)
96 return UV_EINVAL;
97
98#if defined(__MVS__)
99 /* On zOS, backlog=0 has undefined behaviour */
100 if (backlog == 0)
101 backlog = 1;
102 else if (backlog < 0)
103 backlog = SOMAXCONN;
104#endif
105
106 if (listen(uv__stream_fd(handle), backlog))
107 return UV__ERR(errno);
108
109 handle->connection_cb = cb;
110 handle->io_watcher.cb = uv__server_io;
111 uv__io_start(handle->loop, &handle->io_watcher, POLLIN);
112 return 0;
113}
114
115
116void uv__pipe_close(uv_pipe_t* handle) {
117 if (handle->pipe_fname) {
118 /*
119 * Unlink the file system entity before closing the file descriptor.
120 * Doing it the other way around introduces a race where our process
121 * unlinks a socket with the same name that's just been created by
122 * another thread or process.
123 */
124 unlink(handle->pipe_fname);
125 uv__free((void*)handle->pipe_fname);
126 handle->pipe_fname = NULL;
127 }
128
129 uv__stream_close((uv_stream_t*)handle);
130}
131
132
133int uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
134 int flags;
135 int mode;
136 int err;
137 flags = 0;
138
139 if (uv__fd_exists(handle->loop, fd))
140 return UV_EEXIST;
141
142 do
143 mode = fcntl(fd, F_GETFL);
144 while (mode == -1 && errno == EINTR);
145
146 if (mode == -1)
147 return UV__ERR(errno); /* according to docs, must be EBADF */
148
149 err = uv__nonblock(fd, 1);
150 if (err)
151 return err;
152
153#if defined(__APPLE__)
154 err = uv__stream_try_select((uv_stream_t*) handle, &fd);
155 if (err)
156 return err;
157#endif /* defined(__APPLE__) */
158
159 mode &= O_ACCMODE;
160 if (mode != O_WRONLY)
161 flags |= UV_HANDLE_READABLE;
162 if (mode != O_RDONLY)
163 flags |= UV_HANDLE_WRITABLE;
164
165 return uv__stream_open((uv_stream_t*)handle, fd, flags);
166}
167
168
169void uv_pipe_connect(uv_connect_t* req,
170 uv_pipe_t* handle,
171 const char* name,
172 uv_connect_cb cb) {
173 struct sockaddr_un saddr;
174 int new_sock;
175 int err;
176 int r;
177
178 new_sock = (uv__stream_fd(handle) == -1);
179
180 if (new_sock) {
181 err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
182 if (err < 0)
183 goto out;
184 handle->io_watcher.fd = err;
185 }
186
187 memset(&saddr, 0, sizeof saddr);
188 uv__strscpy(saddr.sun_path, name, sizeof(saddr.sun_path));
189 saddr.sun_family = AF_UNIX;
190
191 do {
192 r = connect(uv__stream_fd(handle),
193 (struct sockaddr*)&saddr, sizeof saddr);
194 }
195 while (r == -1 && errno == EINTR);
196
197 if (r == -1 && errno != EINPROGRESS) {
198 err = UV__ERR(errno);
199#if defined(__CYGWIN__) || defined(__MSYS__)
200 /* EBADF is supposed to mean that the socket fd is bad, but
201 Cygwin reports EBADF instead of ENOTSOCK when the file is
202 not a socket. We do not expect to see a bad fd here
203 (e.g. due to new_sock), so translate the error. */
204 if (err == UV_EBADF)
205 err = UV_ENOTSOCK;
206#endif
207 goto out;
208 }
209
210 err = 0;
211 if (new_sock) {
212 err = uv__stream_open((uv_stream_t*)handle,
213 uv__stream_fd(handle),
214 UV_HANDLE_READABLE | UV_HANDLE_WRITABLE);
215 }
216
217 if (err == 0)
218 uv__io_start(handle->loop, &handle->io_watcher, POLLOUT);
219
220out:
221 handle->delayed_error = err;
222 handle->connect_req = req;
223
224 uv__req_init(handle->loop, req, UV_CONNECT);
225 req->handle = (uv_stream_t*)handle;
226 req->cb = cb;
227 QUEUE_INIT(&req->queue);
228
229 /* Force callback to run on next tick in case of error. */
230 if (err)
231 uv__io_feed(handle->loop, &handle->io_watcher);
232
233}
234
235
236static int uv__pipe_getsockpeername(const uv_pipe_t* handle,
237 uv__peersockfunc func,
238 char* buffer,
239 size_t* size) {
240 struct sockaddr_un sa;
241 socklen_t addrlen;
242 int err;
243
244 addrlen = sizeof(sa);
245 memset(&sa, 0, addrlen);
246 err = uv__getsockpeername((const uv_handle_t*) handle,
247 func,
248 (struct sockaddr*) &sa,
249 (int*) &addrlen);
250 if (err < 0) {
251 *size = 0;
252 return err;
253 }
254
255#if defined(__linux__)
256 if (sa.sun_path[0] == 0)
257 /* Linux abstract namespace */
258 addrlen -= offsetof(struct sockaddr_un, sun_path);
259 else
260#endif
261 addrlen = strlen(sa.sun_path);
262
263
264 if (addrlen >= *size) {
265 *size = addrlen + 1;
266 return UV_ENOBUFS;
267 }
268
269 memcpy(buffer, sa.sun_path, addrlen);
270 *size = addrlen;
271
272 /* only null-terminate if it's not an abstract socket */
273 if (buffer[0] != '\0')
274 buffer[addrlen] = '\0';
275
276 return 0;
277}
278
279
280int uv_pipe_getsockname(const uv_pipe_t* handle, char* buffer, size_t* size) {
281 return uv__pipe_getsockpeername(handle, getsockname, buffer, size);
282}
283
284
285int uv_pipe_getpeername(const uv_pipe_t* handle, char* buffer, size_t* size) {
286 return uv__pipe_getsockpeername(handle, getpeername, buffer, size);
287}
288
289
290void uv_pipe_pending_instances(uv_pipe_t* handle, int count) {
291}
292
293
294int uv_pipe_pending_count(uv_pipe_t* handle) {
295 uv__stream_queued_fds_t* queued_fds;
296
297 if (!handle->ipc)
298 return 0;
299
300 if (handle->accepted_fd == -1)
301 return 0;
302
303 if (handle->queued_fds == NULL)
304 return 1;
305
306 queued_fds = handle->queued_fds;
307 return queued_fds->offset + 1;
308}
309
310
311uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle) {
312 if (!handle->ipc)
313 return UV_UNKNOWN_HANDLE;
314
315 if (handle->accepted_fd == -1)
316 return UV_UNKNOWN_HANDLE;
317 else
318 return uv__handle_type(handle->accepted_fd);
319}
320
321
322int uv_pipe_chmod(uv_pipe_t* handle, int mode) {
323 unsigned desired_mode;
324 struct stat pipe_stat;
325 char* name_buffer;
326 size_t name_len;
327 int r;
328
329 if (handle == NULL || uv__stream_fd(handle) == -1)
330 return UV_EBADF;
331
332 if (mode != UV_READABLE &&
333 mode != UV_WRITABLE &&
334 mode != (UV_WRITABLE | UV_READABLE))
335 return UV_EINVAL;
336
337 /* Unfortunately fchmod does not work on all platforms, we will use chmod. */
338 name_len = 0;
339 r = uv_pipe_getsockname(handle, NULL, &name_len);
340 if (r != UV_ENOBUFS)
341 return r;
342
343 name_buffer = uv__malloc(name_len);
344 if (name_buffer == NULL)
345 return UV_ENOMEM;
346
347 r = uv_pipe_getsockname(handle, name_buffer, &name_len);
348 if (r != 0) {
349 uv__free(name_buffer);
350 return r;
351 }
352
353 /* stat must be used as fstat has a bug on Darwin */
354 if (stat(name_buffer, &pipe_stat) == -1) {
355 uv__free(name_buffer);
356 return -errno;
357 }
358
359 desired_mode = 0;
360 if (mode & UV_READABLE)
361 desired_mode |= S_IRUSR | S_IRGRP | S_IROTH;
362 if (mode & UV_WRITABLE)
363 desired_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
364
365 /* Exit early if pipe already has desired mode. */
366 if ((pipe_stat.st_mode & desired_mode) == desired_mode) {
367 uv__free(name_buffer);
368 return 0;
369 }
370
371 pipe_stat.st_mode |= desired_mode;
372
373 r = chmod(name_buffer, pipe_stat.st_mode);
374 uv__free(name_buffer);
375
376 return r != -1 ? 0 : UV__ERR(errno);
377}
378