pipe.c 9.4 KB

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