BInputProcess.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @file BInputProcess.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <unistd.h>
  23. #include <system/BLog.h>
  24. #include <inputprocess/BInputProcess.h>
  25. #include <generated/blog_channel_BInputProcess.h>
  26. static int init_pipe (BInputProcess *o, int pipe_fd);
  27. static void free_pipe (BInputProcess *o);
  28. static void process_pipe_handler_error (BInputProcess *o, int component, int code);
  29. static void process_handler (BInputProcess *o, int normally, uint8_t normally_exit_status);
  30. int init_pipe (BInputProcess *o, int pipe_fd)
  31. {
  32. // init socket
  33. if (BSocket_InitPipe(&o->pipe_sock, o->reactor, pipe_fd) < 0) {
  34. BLog(BLOG_ERROR, "BSocket_InitPipe failed");
  35. goto fail0;
  36. }
  37. // init domain
  38. FlowErrorDomain_Init(&o->pipe_domain, (FlowErrorDomain_handler)process_pipe_handler_error, o);
  39. // init source
  40. StreamSocketSource_Init(&o->pipe_source, FlowErrorReporter_Create(&o->pipe_domain, 0), &o->pipe_sock, BReactor_PendingGroup(o->reactor));
  41. return 1;
  42. fail0:
  43. return 0;
  44. }
  45. void free_pipe (BInputProcess *o)
  46. {
  47. // free source
  48. StreamSocketSource_Free(&o->pipe_source);
  49. // free socket
  50. BSocket_Free(&o->pipe_sock);
  51. }
  52. void process_pipe_handler_error (BInputProcess *o, int component, int code)
  53. {
  54. DebugObject_Access(&o->d_obj);
  55. ASSERT(o->pipe_fd >= 0)
  56. if (code == STREAMSOCKETSOURCE_ERROR_CLOSED) {
  57. BLog(BLOG_INFO, "pipe eof");
  58. } else {
  59. BLog(BLOG_ERROR, "pipe error");
  60. }
  61. // free pipe reading
  62. free_pipe(o);
  63. // close pipe read end
  64. ASSERT_FORCE(close(o->pipe_fd) == 0)
  65. // forget pipe
  66. o->pipe_fd = -1;
  67. // call closed handler
  68. o->handler_closed(o->user, (code != STREAMSOCKETSOURCE_ERROR_CLOSED));
  69. return;
  70. }
  71. void process_handler (BInputProcess *o, int normally, uint8_t normally_exit_status)
  72. {
  73. DebugObject_Access(&o->d_obj);
  74. DEBUGERROR(&o->d_err, o->handler_terminated(o->user, normally, normally_exit_status));
  75. }
  76. int BInputProcess_Init (BInputProcess *o, const char *file, char *const argv[], const char *username, BReactor *reactor, BProcessManager *manager, void *user,
  77. BInputProcess_handler_terminated handler_terminated,
  78. BInputProcess_handler_closed handler_closed)
  79. {
  80. // init arguments
  81. o->reactor = reactor;
  82. o->user = user;
  83. o->handler_terminated = handler_terminated;
  84. o->handler_closed = handler_closed;
  85. // create pipe
  86. int pipefds[2];
  87. if (pipe(pipefds) < 0) {
  88. BLog(BLOG_ERROR, "pipe failed");
  89. goto fail0;
  90. }
  91. // init pipe reading
  92. if (!init_pipe(o, pipefds[0])) {
  93. goto fail1;
  94. }
  95. // start process
  96. int fds[] = { pipefds[1], -1 };
  97. int fds_map[] = { 1 };
  98. if (!BProcess_InitWithFds(&o->process, manager, (BProcess_handler)process_handler, o, file, argv, username, fds, fds_map)) {
  99. BLog(BLOG_ERROR, "BProcess_Init failed");
  100. goto fail2;
  101. }
  102. // remember pipe read end
  103. o->pipe_fd = pipefds[0];
  104. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  105. DebugObject_Init(&o->d_obj);
  106. return 1;
  107. fail2:
  108. free_pipe(o);
  109. fail1:
  110. ASSERT_FORCE(close(pipefds[0]) == 0)
  111. ASSERT_FORCE(close(pipefds[1]) == 0)
  112. fail0:
  113. return 0;
  114. }
  115. void BInputProcess_Free (BInputProcess *o)
  116. {
  117. DebugObject_Free(&o->d_obj);
  118. DebugError_Free(&o->d_err);
  119. // free process
  120. BProcess_Free(&o->process);
  121. if (o->pipe_fd >= 0) {
  122. // free pipe reading
  123. free_pipe(o);
  124. // close pipe read end
  125. ASSERT_FORCE(close(o->pipe_fd) == 0)
  126. }
  127. }
  128. int BInputProcess_Terminate (BInputProcess *o)
  129. {
  130. DebugObject_Access(&o->d_obj);
  131. DebugError_AssertNoError(&o->d_err);
  132. return BProcess_Terminate(&o->process);
  133. }
  134. int BInputProcess_Kill (BInputProcess *o)
  135. {
  136. DebugObject_Access(&o->d_obj);
  137. DebugError_AssertNoError(&o->d_err);
  138. return BProcess_Kill(&o->process);
  139. }
  140. StreamRecvInterface * BInputProcess_GetInput (BInputProcess *o)
  141. {
  142. DebugObject_Access(&o->d_obj);
  143. ASSERT(o->pipe_fd >= 0)
  144. return StreamSocketSource_GetOutput(&o->pipe_source);
  145. }