BInputProcess.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @file BInputProcess.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <unistd.h>
  30. #include <base/BLog.h>
  31. #include <system/BNetwork.h>
  32. #include "BInputProcess.h"
  33. #include <generated/blog_channel_BInputProcess.h>
  34. static void connection_handler (BInputProcess *o, int event);
  35. static void process_handler (BInputProcess *o, int normally, uint8_t normally_exit_status);
  36. void connection_handler (BInputProcess *o, int event)
  37. {
  38. DebugObject_Access(&o->d_obj);
  39. ASSERT(o->pipe_fd >= 0)
  40. if (event == BCONNECTION_EVENT_RECVCLOSED) {
  41. BLog(BLOG_INFO, "pipe closed");
  42. } else {
  43. BLog(BLOG_ERROR, "pipe error");
  44. }
  45. // free pipe connection read interface
  46. BConnection_RecvAsync_Free(&o->pipe_con);
  47. // free pipe connection
  48. BConnection_Free(&o->pipe_con);
  49. // close pipe read end
  50. ASSERT_FORCE(close(o->pipe_fd) == 0)
  51. // forget pipe
  52. o->pipe_fd = -1;
  53. // call closed handler
  54. o->handler_closed(o->user, (event != BCONNECTION_EVENT_RECVCLOSED));
  55. return;
  56. }
  57. void process_handler (BInputProcess *o, int normally, uint8_t normally_exit_status)
  58. {
  59. DebugObject_Access(&o->d_obj);
  60. ASSERT(o->started)
  61. ASSERT(o->have_process)
  62. // free process
  63. BProcess_Free(&o->process);
  64. // set not have process
  65. o->have_process = 0;
  66. // call terminated handler
  67. o->handler_terminated(o->user, normally, normally_exit_status);
  68. return;
  69. }
  70. int BInputProcess_Init (BInputProcess *o, BReactor *reactor, BProcessManager *manager, void *user,
  71. BInputProcess_handler_terminated handler_terminated,
  72. BInputProcess_handler_closed handler_closed)
  73. {
  74. BNetwork_Assert();
  75. // init arguments
  76. o->reactor = reactor;
  77. o->manager = manager;
  78. o->user = user;
  79. o->handler_terminated = handler_terminated;
  80. o->handler_closed = handler_closed;
  81. // create pipe
  82. int pipefds[2];
  83. if (pipe(pipefds) < 0) {
  84. BLog(BLOG_ERROR, "pipe failed");
  85. goto fail0;
  86. }
  87. // init pipe connection
  88. if (!BConnection_Init(&o->pipe_con, BCONNECTION_SOURCE_PIPE(pipefds[0]), o->reactor, o, (BConnection_handler)connection_handler)) {
  89. BLog(BLOG_ERROR, "BConnection_Init failed");
  90. goto fail1;
  91. }
  92. // init pipe connection read interface
  93. BConnection_RecvAsync_Init(&o->pipe_con);
  94. // remember pipe fds
  95. o->pipe_fd = pipefds[0];
  96. o->pipe_write_fd = pipefds[1];
  97. // set not started
  98. o->started = 0;
  99. DebugObject_Init(&o->d_obj);
  100. return 1;
  101. fail1:
  102. ASSERT_FORCE(close(pipefds[0]) == 0)
  103. ASSERT_FORCE(close(pipefds[1]) == 0)
  104. fail0:
  105. return 0;
  106. }
  107. void BInputProcess_Free (BInputProcess *o)
  108. {
  109. DebugObject_Free(&o->d_obj);
  110. if (!o->started) {
  111. // close pipe write end
  112. ASSERT_FORCE(close(o->pipe_write_fd) == 0)
  113. } else {
  114. // free process
  115. if (o->have_process) {
  116. BProcess_Free(&o->process);
  117. }
  118. }
  119. if (o->pipe_fd >= 0) {
  120. // free pipe connection read interface
  121. BConnection_RecvAsync_Free(&o->pipe_con);
  122. // free pipe connection
  123. BConnection_Free(&o->pipe_con);
  124. // close pipe read end
  125. ASSERT_FORCE(close(o->pipe_fd) == 0)
  126. }
  127. }
  128. int BInputProcess_Start (BInputProcess *o, const char *file, char *const argv[], const char *username)
  129. {
  130. DebugObject_Access(&o->d_obj);
  131. ASSERT(!o->started)
  132. // start process
  133. int fds[] = { o->pipe_write_fd, -1 };
  134. int fds_map[] = { 1 };
  135. if (!BProcess_InitWithFds(&o->process, o->manager, (BProcess_handler)process_handler, o, file, argv, username, fds, fds_map)) {
  136. BLog(BLOG_ERROR, "BProcess_Init failed");
  137. goto fail0;
  138. }
  139. // close pipe write end
  140. ASSERT_FORCE(close(o->pipe_write_fd) == 0)
  141. // set started
  142. o->started = 1;
  143. // set have process
  144. o->have_process = 1;
  145. return 1;
  146. fail0:
  147. return 0;
  148. }
  149. int BInputProcess_Terminate (BInputProcess *o)
  150. {
  151. DebugObject_Access(&o->d_obj);
  152. ASSERT(o->started)
  153. ASSERT(o->have_process)
  154. return BProcess_Terminate(&o->process);
  155. }
  156. int BInputProcess_Kill (BInputProcess *o)
  157. {
  158. DebugObject_Access(&o->d_obj);
  159. ASSERT(o->started)
  160. ASSERT(o->have_process)
  161. return BProcess_Kill(&o->process);
  162. }
  163. StreamRecvInterface * BInputProcess_GetInput (BInputProcess *o)
  164. {
  165. DebugObject_Access(&o->d_obj);
  166. ASSERT(o->pipe_fd >= 0)
  167. return BConnection_RecvAsync_GetIf(&o->pipe_con);
  168. }