Pipe.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/events/libuv/UvWrapper.h"
  16. #include "exception/Er.h"
  17. #include "memory/Allocator.h"
  18. #include "util/events/Pipe.h"
  19. #include "util/events/libuv/Pipe_pvt.h"
  20. #include "util/events/libuv/EventBase_pvt.h"
  21. #include "util/log/Log.h"
  22. #include "util/Identity.h"
  23. #include "util/CString.h"
  24. #include "wire/Message.h"
  25. #include "wire/Error.h"
  26. #include "benc/String.h"
  27. #include <inttypes.h>
  28. #include <libgen.h>
  29. #include <stdio.h>
  30. #include <sys/stat.h>
  31. #include <string.h>
  32. struct Pipe_WriteRequest_pvt;
  33. struct Pipe_pvt
  34. {
  35. struct Pipe pub;
  36. uv_pipe_t peer;
  37. /** Job to close the handles when the allocator is freed */
  38. struct Allocator_OnFreeJob* closeHandlesOnFree;
  39. /** Job which blocks the freeing until the callback completes */
  40. struct Allocator_OnFreeJob* blockFreeInsideCallback;
  41. // true if we can pass file descriptors through this pipe
  42. bool ipc;
  43. /** 1 when the pipe becomes active. */
  44. int isActive;
  45. int queueLen;
  46. /** Used by blockFreeInsideCallback */
  47. int isInCallback;
  48. /** only non-null before the connection is setup. */
  49. struct Pipe_WriteRequest_pvt* bufferedRequest;
  50. struct Allocator* alloc;
  51. struct Log* log;
  52. Identity
  53. };
  54. struct Pipe_WriteRequest_pvt {
  55. uv_write_t uvReq;
  56. struct Pipe_pvt* pipe;
  57. struct Message* msg;
  58. struct Allocator* alloc;
  59. Identity
  60. };
  61. static void sendMessageCallback(uv_write_t* uvReq, int error)
  62. {
  63. struct Pipe_WriteRequest_pvt* req = Identity_check((struct Pipe_WriteRequest_pvt*) uvReq);
  64. if (error) {
  65. Log_info(req->pipe->log, "Failed to write to pipe [%s] [%s]",
  66. req->pipe->pub.fullName, uv_strerror(error) );
  67. }
  68. req->pipe->queueLen -= Message_getLength(req->msg);
  69. Assert_ifParanoid(req->pipe->queueLen >= 0);
  70. Allocator_free(req->alloc);
  71. }
  72. static void sendMessage2(struct Pipe_WriteRequest_pvt* req)
  73. {
  74. struct Pipe_pvt* pipe = req->pipe;
  75. struct Message* m = req->msg;
  76. uv_buf_t buffers[] = {
  77. { .base = (char*)m->msgbytes, .len = Message_getLength(m) }
  78. };
  79. int ret = -1;
  80. int fd = Message_getAssociatedFd(m);
  81. if (pipe->ipc && fd > -1 && !Defined(win32)) {
  82. uv_stream_t* fake_handle = Allocator_calloc(req->alloc, sizeof(uv_stream_t), 1);
  83. #ifndef win32
  84. fake_handle->io_watcher.fd = fd;
  85. #endif
  86. fake_handle->type = UV_TCP;
  87. ret = uv_write2(
  88. &req->uvReq,
  89. (uv_stream_t*) &pipe->peer,
  90. buffers,
  91. 1,
  92. fake_handle,
  93. sendMessageCallback);
  94. Log_debug(pipe->log, "Sending message with fd [%d]", fd);
  95. } else {
  96. ret = uv_write(&req->uvReq, (uv_stream_t*) &pipe->peer, buffers, 1, sendMessageCallback);
  97. }
  98. if (ret) {
  99. Log_info(pipe->log, "Failed writing to pipe [%s] [%s]",
  100. pipe->pub.fullName, uv_strerror(ret) );
  101. Allocator_free(req->alloc);
  102. return;
  103. }
  104. pipe->queueLen += Message_getLength(m);
  105. return;
  106. }
  107. static Iface_DEFUN sendMessage(struct Message* m, struct Iface* iface)
  108. {
  109. struct Pipe_pvt* pipe = Identity_check((struct Pipe_pvt*) iface);
  110. if (pipe->queueLen > 50000) {
  111. return Error(m, "OVERFLOW");
  112. }
  113. // This allocator will hold the message allocator in existance after it is freed.
  114. struct Allocator* reqAlloc = Allocator_child(pipe->alloc);
  115. Allocator_adopt(reqAlloc, Message_getAlloc(m));
  116. struct Pipe_WriteRequest_pvt* req =
  117. Allocator_clone(reqAlloc, (&(struct Pipe_WriteRequest_pvt) {
  118. .pipe = pipe,
  119. .msg = m,
  120. .alloc = reqAlloc
  121. }));
  122. Identity_set(req);
  123. if (pipe->isActive) {
  124. sendMessage2(req);
  125. } else {
  126. if (!pipe->bufferedRequest) {
  127. Log_debug(pipe->log, "Buffering a message");
  128. pipe->bufferedRequest = req;
  129. } else {
  130. Log_debug(pipe->log, "Appending to the buffered message");
  131. struct Message* m2 = Message_new(0,
  132. Message_getLength(m) + Message_getLength(pipe->bufferedRequest->msg), reqAlloc);
  133. Er_assert(Message_epush(m2, m->msgbytes, Message_getLength(m)));
  134. Er_assert(Message_epush(m2,
  135. pipe->bufferedRequest->msg->msgbytes,
  136. Message_getLength(pipe->bufferedRequest->msg)));
  137. req->msg = m2;
  138. Allocator_free(pipe->bufferedRequest->alloc);
  139. pipe->bufferedRequest = req;
  140. }
  141. }
  142. return NULL;
  143. }
  144. /** Asynchronous allocator freeing. */
  145. static void onClose(uv_handle_t* handle)
  146. {
  147. struct Pipe_pvt* pipe = Identity_check((struct Pipe_pvt*)handle->data);
  148. handle->data = NULL;
  149. if (pipe->closeHandlesOnFree && !pipe->peer.data) {
  150. Allocator_onFreeComplete((struct Allocator_OnFreeJob*) pipe->closeHandlesOnFree);
  151. }
  152. }
  153. #if Pipe_PADDING_AMOUNT < 8
  154. #error
  155. #endif
  156. #define ALLOC(buff) (((struct Message**) &(buff[-(8 + (((uintptr_t)buff) % 8))]))[0])
  157. static void incoming(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
  158. {
  159. struct Pipe_pvt* pipe = Identity_check((struct Pipe_pvt*) stream->data);
  160. // Grab out the msg which was placed there by allocate()
  161. struct Message* msg = buf->base ? ALLOC(buf->base) : NULL;
  162. pipe->isInCallback = 1;
  163. Assert_true(!msg || Message_getAlloc(msg)->fileName == pipe->alloc->fileName);
  164. if (nread < 0) {
  165. if (pipe->pub.onClose) {
  166. pipe->pub.onClose(&pipe->pub, 0);
  167. }
  168. } else if (nread == 0) {
  169. // This is common.
  170. //Log_debug(pipe->log, "Pipe 0 length read [%s]", pipe->pub.fullName);
  171. } else {
  172. Assert_true(msg);
  173. Er_assert(Message_truncate(msg, nread));
  174. if (pipe->ipc) {
  175. #ifndef win32
  176. Message_setAssociatedFd(msg, stream->accepted_fd);
  177. #endif
  178. }
  179. Iface_send(&pipe->pub.iface, msg);
  180. }
  181. if (msg) {
  182. Allocator_free(Message_getAlloc(msg));
  183. }
  184. pipe->isInCallback = 0;
  185. if (pipe->blockFreeInsideCallback) {
  186. Allocator_onFreeComplete((struct Allocator_OnFreeJob*) pipe->blockFreeInsideCallback);
  187. }
  188. }
  189. static void incoming2(uv_pipe_t* stream, ssize_t nread, const uv_buf_t* buf, uv_handle_type _)
  190. {
  191. incoming((uv_stream_t*)stream, nread, buf);
  192. }
  193. static void allocate(uv_handle_t* handle, size_t size, uv_buf_t* buf)
  194. {
  195. struct Pipe_pvt* pipe = Identity_check((struct Pipe_pvt*) handle->data);
  196. size = Pipe_BUFFER_CAP;
  197. struct Allocator* child = Allocator_child(pipe->alloc);
  198. struct Message* msg = Message_new(size, Pipe_PADDING_AMOUNT, child);
  199. ALLOC(msg->msgbytes) = msg;
  200. buf->base = msg->msgbytes;
  201. buf->len = size;
  202. }
  203. static int startPipe(struct Pipe_pvt* pipe)
  204. {
  205. if (pipe->ipc) {
  206. return uv_read2_start((uv_stream_t*)&pipe->peer, allocate, incoming2);
  207. } else {
  208. return uv_read_start((uv_stream_t*)&pipe->peer, allocate, incoming);
  209. }
  210. }
  211. static void connected(uv_connect_t* req, int status)
  212. {
  213. uv_stream_t* link = req->handle;
  214. struct Pipe_pvt* pipe = Identity_check((struct Pipe_pvt*) link->data);
  215. Log_debug(pipe->log, "Pipe [%s] established connection", pipe->pub.fullName);
  216. int ret;
  217. if (status) {
  218. Log_info(pipe->log, "uv_pipe_connect() failed for pipe [%s] [%s]",
  219. pipe->pub.fullName, uv_strerror(status) );
  220. uv_close((uv_handle_t*) &pipe->peer, onClose);
  221. } else if ((ret = startPipe(pipe))) {
  222. Log_info(pipe->log, "uv_read_start() failed for pipe [%s] [%s]",
  223. pipe->pub.fullName, uv_strerror(ret));
  224. uv_close((uv_handle_t*) &pipe->peer, onClose);
  225. } else {
  226. pipe->isActive = 1;
  227. if (pipe->pub.onConnection) {
  228. pipe->pub.onConnection(&pipe->pub, status);
  229. }
  230. // If there's anything buffered then send it.
  231. if (pipe->bufferedRequest) {
  232. Log_debug(pipe->log, "Sending buffered message");
  233. sendMessage2(pipe->bufferedRequest);
  234. pipe->bufferedRequest = NULL;
  235. }
  236. }
  237. }
  238. static int blockFreeInsideCallback(struct Allocator_OnFreeJob* job)
  239. {
  240. struct Pipe_pvt* pipe = Identity_check((struct Pipe_pvt*)job->userData);
  241. if (!pipe->isInCallback) {
  242. return 0;
  243. }
  244. pipe->blockFreeInsideCallback = job;
  245. return Allocator_ONFREE_ASYNC;
  246. }
  247. static int closeHandlesOnFree(struct Allocator_OnFreeJob* job)
  248. {
  249. struct Pipe_pvt* pipe = Identity_check((struct Pipe_pvt*)job->userData);
  250. pipe->closeHandlesOnFree = job;
  251. if (pipe->peer.data) {
  252. uv_close((uv_handle_t*) &pipe->peer, onClose);
  253. return Allocator_ONFREE_ASYNC;
  254. }
  255. return 0;
  256. }
  257. static Er_DEFUN(struct Pipe_pvt* newPipeAny(struct EventBase* eb,
  258. const char* fullPath,
  259. bool ipc,
  260. struct Log* log,
  261. struct Allocator* userAlloc))
  262. {
  263. struct EventBase_pvt* ctx = EventBase_privatize(eb);
  264. struct Allocator* alloc = Allocator_child(userAlloc);
  265. struct Pipe_pvt* out = Allocator_clone(alloc, (&(struct Pipe_pvt) {
  266. .pub = {
  267. .iface = {
  268. .send = sendMessage
  269. },
  270. .fullName = (fullPath) ? CString_strdup(fullPath, alloc) : NULL,
  271. },
  272. .alloc = alloc,
  273. .log = log,
  274. .ipc = ipc,
  275. }));
  276. int ret = uv_pipe_init(ctx->loop, &out->peer, ipc);
  277. if (ret) {
  278. Er_raise(alloc, "uv_pipe_init() failed [%s]", uv_strerror(ret));
  279. }
  280. Allocator_onFree(alloc, closeHandlesOnFree, out);
  281. Allocator_onFree(alloc, blockFreeInsideCallback, out);
  282. out->peer.data = out;
  283. Identity_set(out);
  284. Er_ret(out);
  285. }
  286. Er_DEFUN(struct Pipe* Pipe_forFd(int fd,
  287. bool ipc,
  288. struct EventBase* eb,
  289. struct Log* log,
  290. struct Allocator* userAlloc))
  291. {
  292. char buff[32] = {0};
  293. snprintf(buff, 31, "forFd(%d)", fd);
  294. struct Pipe_pvt* out = Er(newPipeAny(eb, buff, ipc, log, userAlloc));
  295. int ret = uv_pipe_open(&out->peer, fd);
  296. if (ret) {
  297. Er_raise(out->alloc, "uv_pipe_open(inFd) failed [%s]", uv_strerror(ret));
  298. }
  299. uv_connect_t req = { .handle = (uv_stream_t*) &out->peer };
  300. connected(&req, 0);
  301. Er_ret(&out->pub);
  302. }
  303. Er_DEFUN(struct Pipe* Pipe_named(const char* fullPath,
  304. struct EventBase* eb,
  305. struct Log* log,
  306. struct Allocator* userAlloc))
  307. {
  308. struct Pipe_pvt* out = Er(newPipeAny(eb, fullPath, true, log, userAlloc));
  309. uv_connect_t* req = Allocator_malloc(out->alloc, sizeof(uv_connect_t));
  310. req->data = out;
  311. uv_pipe_connect(req, &out->peer, out->pub.fullName, connected);
  312. int err = 0;
  313. // We get the error back synchronously but windows doesn't support that
  314. // TODO(cjd): Find a better way
  315. #ifndef win32
  316. err = (&out->peer)->delayed_error;
  317. #endif
  318. if (err != 0) {
  319. Er_raise(out->alloc, "uv_pipe_connect() failed [%s] for pipe [%s]",
  320. uv_strerror(err), out->pub.fullName);
  321. }
  322. Er_ret(&out->pub);
  323. }
  324. Er_DEFUN(struct Pipe* Pipe_serverAccept(uv_pipe_t* server,
  325. const char* pipeName,
  326. struct EventBase* eb,
  327. struct Log* log,
  328. struct Allocator* userAlloc))
  329. {
  330. struct Pipe_pvt* out = Er(newPipeAny(eb, NULL, true, log, userAlloc));
  331. int ret = uv_accept((uv_stream_t*) server, (uv_stream_t*) &out->peer);
  332. if (ret) {
  333. uv_close((uv_handle_t*) &out->peer, onClose);
  334. Er_raise(out->alloc, "uv_accept() failed: pipe [%s] [%s]",
  335. pipeName, uv_strerror(ret));
  336. } else {
  337. uv_connect_t req = { .handle = (uv_stream_t*) &out->peer };
  338. connected(&req, 0);
  339. }
  340. Er_ret(&out->pub);
  341. }
  342. Er_DEFUN(bool Pipe_exists(const char* path, struct Allocator* errAlloc))
  343. {
  344. struct stat st;
  345. if (stat(path, &st)) {
  346. if (errno == ENOENT) { Er_ret(false); }
  347. Er_raise(errAlloc, "Failed stat(%s) [%s]", path, strerror(errno));
  348. } else {
  349. int flag = 0;
  350. #ifdef win32
  351. flag = S_IFIFO;
  352. #elif defined(S_IFSOCK)
  353. flag = S_IFSOCK;
  354. #else
  355. #error "missing S_IFSOCK"
  356. #endif
  357. Er_ret(((int)(st.st_mode & S_IFMT)) == flag);
  358. }
  359. }