pipe.c 11 KB

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