BInputProcess.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <base/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 pipe_source_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)pipe_source_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 pipe_source_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 closed");
  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. ASSERT(o->started)
  75. ASSERT(o->have_process)
  76. // free process
  77. BProcess_Free(&o->process);
  78. // set not have process
  79. o->have_process = 0;
  80. // call terminated handler
  81. o->handler_terminated(o->user, normally, normally_exit_status);
  82. return;
  83. }
  84. int BInputProcess_Init (BInputProcess *o, BReactor *reactor, BProcessManager *manager, void *user,
  85. BInputProcess_handler_terminated handler_terminated,
  86. BInputProcess_handler_closed handler_closed)
  87. {
  88. // init arguments
  89. o->reactor = reactor;
  90. o->manager = manager;
  91. o->user = user;
  92. o->handler_terminated = handler_terminated;
  93. o->handler_closed = handler_closed;
  94. // create pipe
  95. int pipefds[2];
  96. if (pipe(pipefds) < 0) {
  97. BLog(BLOG_ERROR, "pipe failed");
  98. goto fail0;
  99. }
  100. // init pipe reading
  101. if (!init_pipe(o, pipefds[0])) {
  102. goto fail1;
  103. }
  104. // remember pipe fds
  105. o->pipe_fd = pipefds[0];
  106. o->pipe_write_fd = pipefds[1];
  107. // set not started
  108. o->started = 0;
  109. DebugObject_Init(&o->d_obj);
  110. return 1;
  111. fail2:
  112. free_pipe(o);
  113. fail1:
  114. ASSERT_FORCE(close(pipefds[0]) == 0)
  115. ASSERT_FORCE(close(pipefds[1]) == 0)
  116. fail0:
  117. return 0;
  118. }
  119. void BInputProcess_Free (BInputProcess *o)
  120. {
  121. DebugObject_Free(&o->d_obj);
  122. if (!o->started) {
  123. // close pipe write end
  124. ASSERT_FORCE(close(o->pipe_write_fd) == 0)
  125. } else {
  126. // free process
  127. if (o->have_process) {
  128. BProcess_Free(&o->process);
  129. }
  130. }
  131. if (o->pipe_fd >= 0) {
  132. // free pipe reading
  133. free_pipe(o);
  134. // close pipe read end
  135. ASSERT_FORCE(close(o->pipe_fd) == 0)
  136. }
  137. }
  138. int BInputProcess_Start (BInputProcess *o, const char *file, char *const argv[], const char *username)
  139. {
  140. DebugObject_Access(&o->d_obj);
  141. ASSERT(!o->started)
  142. // start process
  143. int fds[] = { o->pipe_write_fd, -1 };
  144. int fds_map[] = { 1 };
  145. if (!BProcess_InitWithFds(&o->process, o->manager, (BProcess_handler)process_handler, o, file, argv, username, fds, fds_map)) {
  146. BLog(BLOG_ERROR, "BProcess_Init failed");
  147. goto fail0;
  148. }
  149. // close pipe write end
  150. ASSERT_FORCE(close(o->pipe_write_fd) == 0)
  151. // set started
  152. o->started = 1;
  153. // set have process
  154. o->have_process = 1;
  155. return 1;
  156. fail0:
  157. return 0;
  158. }
  159. int BInputProcess_Terminate (BInputProcess *o)
  160. {
  161. DebugObject_Access(&o->d_obj);
  162. ASSERT(o->started)
  163. ASSERT(o->have_process)
  164. return BProcess_Terminate(&o->process);
  165. }
  166. int BInputProcess_Kill (BInputProcess *o)
  167. {
  168. DebugObject_Access(&o->d_obj);
  169. ASSERT(o->started)
  170. ASSERT(o->have_process)
  171. return BProcess_Kill(&o->process);
  172. }
  173. StreamRecvInterface * BInputProcess_GetInput (BInputProcess *o)
  174. {
  175. DebugObject_Access(&o->d_obj);
  176. ASSERT(o->pipe_fd >= 0)
  177. return StreamSocketSource_GetOutput(&o->pipe_source);
  178. }