pipe.c 9.3 KB

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