BInputProcess.c 5.0 KB

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