BInputProcess.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "BInputProcess.h"
  25. #include <generated/blog_channel_BInputProcess.h>
  26. static void connection_handler (BInputProcess *o, int event);
  27. static void process_handler (BInputProcess *o, int normally, uint8_t normally_exit_status);
  28. void connection_handler (BInputProcess *o, int event)
  29. {
  30. DebugObject_Access(&o->d_obj);
  31. ASSERT(o->pipe_fd >= 0)
  32. if (event == BCONNECTION_EVENT_RECVCLOSED) {
  33. BLog(BLOG_INFO, "pipe closed");
  34. } else {
  35. BLog(BLOG_ERROR, "pipe error");
  36. }
  37. // free pipe connection read interface
  38. BConnection_RecvAsync_Free(&o->pipe_con);
  39. // free pipe connection
  40. BConnection_Free(&o->pipe_con);
  41. // close pipe read end
  42. ASSERT_FORCE(close(o->pipe_fd) == 0)
  43. // forget pipe
  44. o->pipe_fd = -1;
  45. // call closed handler
  46. o->handler_closed(o->user, (event != BCONNECTION_EVENT_RECVCLOSED));
  47. return;
  48. }
  49. void process_handler (BInputProcess *o, int normally, uint8_t normally_exit_status)
  50. {
  51. DebugObject_Access(&o->d_obj);
  52. ASSERT(o->started)
  53. ASSERT(o->have_process)
  54. // free process
  55. BProcess_Free(&o->process);
  56. // set not have process
  57. o->have_process = 0;
  58. // call terminated handler
  59. o->handler_terminated(o->user, normally, normally_exit_status);
  60. return;
  61. }
  62. int BInputProcess_Init (BInputProcess *o, BReactor *reactor, BProcessManager *manager, void *user,
  63. BInputProcess_handler_terminated handler_terminated,
  64. BInputProcess_handler_closed handler_closed)
  65. {
  66. // init arguments
  67. o->reactor = reactor;
  68. o->manager = manager;
  69. o->user = user;
  70. o->handler_terminated = handler_terminated;
  71. o->handler_closed = handler_closed;
  72. // create pipe
  73. int pipefds[2];
  74. if (pipe(pipefds) < 0) {
  75. BLog(BLOG_ERROR, "pipe failed");
  76. goto fail0;
  77. }
  78. // init pipe connection
  79. if (!BConnection_Init(&o->pipe_con, BCONNECTION_SOURCE_PIPE(pipefds[0]), o->reactor, o, (BConnection_handler)connection_handler)) {
  80. BLog(BLOG_ERROR, "BConnection_Init failed");
  81. goto fail1;
  82. }
  83. // init pipe connection read interface
  84. BConnection_RecvAsync_Init(&o->pipe_con);
  85. // remember pipe fds
  86. o->pipe_fd = pipefds[0];
  87. o->pipe_write_fd = pipefds[1];
  88. // set not started
  89. o->started = 0;
  90. DebugObject_Init(&o->d_obj);
  91. return 1;
  92. fail1:
  93. ASSERT_FORCE(close(pipefds[0]) == 0)
  94. ASSERT_FORCE(close(pipefds[1]) == 0)
  95. fail0:
  96. return 0;
  97. }
  98. void BInputProcess_Free (BInputProcess *o)
  99. {
  100. DebugObject_Free(&o->d_obj);
  101. if (!o->started) {
  102. // close pipe write end
  103. ASSERT_FORCE(close(o->pipe_write_fd) == 0)
  104. } else {
  105. // free process
  106. if (o->have_process) {
  107. BProcess_Free(&o->process);
  108. }
  109. }
  110. if (o->pipe_fd >= 0) {
  111. // free pipe connection read interface
  112. BConnection_RecvAsync_Free(&o->pipe_con);
  113. // free pipe connection
  114. BConnection_Free(&o->pipe_con);
  115. // close pipe read end
  116. ASSERT_FORCE(close(o->pipe_fd) == 0)
  117. }
  118. }
  119. int BInputProcess_Start (BInputProcess *o, const char *file, char *const argv[], const char *username)
  120. {
  121. DebugObject_Access(&o->d_obj);
  122. ASSERT(!o->started)
  123. // start process
  124. int fds[] = { o->pipe_write_fd, -1 };
  125. int fds_map[] = { 1 };
  126. if (!BProcess_InitWithFds(&o->process, o->manager, (BProcess_handler)process_handler, o, file, argv, username, fds, fds_map)) {
  127. BLog(BLOG_ERROR, "BProcess_Init failed");
  128. goto fail0;
  129. }
  130. // close pipe write end
  131. ASSERT_FORCE(close(o->pipe_write_fd) == 0)
  132. // set started
  133. o->started = 1;
  134. // set have process
  135. o->have_process = 1;
  136. return 1;
  137. fail0:
  138. return 0;
  139. }
  140. int BInputProcess_Terminate (BInputProcess *o)
  141. {
  142. DebugObject_Access(&o->d_obj);
  143. ASSERT(o->started)
  144. ASSERT(o->have_process)
  145. return BProcess_Terminate(&o->process);
  146. }
  147. int BInputProcess_Kill (BInputProcess *o)
  148. {
  149. DebugObject_Access(&o->d_obj);
  150. ASSERT(o->started)
  151. ASSERT(o->have_process)
  152. return BProcess_Kill(&o->process);
  153. }
  154. StreamRecvInterface * BInputProcess_GetInput (BInputProcess *o)
  155. {
  156. DebugObject_Access(&o->d_obj);
  157. ASSERT(o->pipe_fd >= 0)
  158. return BConnection_RecvAsync_GetIf(&o->pipe_con);
  159. }