BProcess.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @file BProcess.h
  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. #ifndef BADVPN_BPROCESS_H
  30. #define BADVPN_BPROCESS_H
  31. #include <stdint.h>
  32. #include <unistd.h>
  33. #include <misc/debug.h>
  34. #include <misc/debugerror.h>
  35. #include <structure/LinkedList1.h>
  36. #include <base/DebugObject.h>
  37. #include <system/BUnixSignal.h>
  38. #include <base/BPending.h>
  39. /**
  40. * Manages child processes.
  41. * There may be at most one process manager at any given time. This restriction is not
  42. * enforced, however.
  43. */
  44. typedef struct {
  45. BReactor *reactor;
  46. BUnixSignal signal;
  47. LinkedList1 processes;
  48. BPending wait_job;
  49. DebugObject d_obj;
  50. } BProcessManager;
  51. /**
  52. * Handler called when the process terminates.
  53. * The process object must be freed from the job context of this handler.
  54. * {@link BProcess_Terminate} or {@link BProcess_Kill} must not be called
  55. * after this handler is called.
  56. *
  57. * @param user as in {@link BProcess_InitWithFds} or {@link BProcess_Init}
  58. * @param normally whether the child process terminated normally (0 or 1)
  59. * @param normally_exit_status if the child process terminated normally, its exit
  60. * status; otherwise undefined
  61. */
  62. typedef void (*BProcess_handler) (void *user, int normally, uint8_t normally_exit_status);
  63. /**
  64. * Represents a child process.
  65. */
  66. typedef struct {
  67. BProcessManager *m;
  68. BProcess_handler handler;
  69. void *user;
  70. pid_t pid;
  71. LinkedList1Node list_node; // node in BProcessManager.processes
  72. DebugObject d_obj;
  73. DebugError d_err;
  74. } BProcess;
  75. /**
  76. * Initializes the process manager.
  77. * There may be at most one process manager at any given time. This restriction is not
  78. * enforced, however.
  79. *
  80. * @param o the object
  81. * @param reactor reactor we live in
  82. * @return 1 on success, 0 on failure
  83. */
  84. int BProcessManager_Init (BProcessManager *o, BReactor *reactor) WARN_UNUSED;
  85. /**
  86. * Frees the process manager.
  87. * There must be no {@link BProcess} objects using this process manager.
  88. *
  89. * @param o the object
  90. */
  91. void BProcessManager_Free (BProcessManager *o);
  92. struct BProcess_params {
  93. const char *username;
  94. const int *fds;
  95. const int *fds_map;
  96. int do_setsid;
  97. };
  98. /**
  99. * Initializes the process.
  100. * 'file', 'argv', 'username', 'fds' and 'fds_map' arguments are only used during this
  101. * function call.
  102. * If no file descriptor is mapped to a standard stream (file descriptors 0, 1, 2),
  103. * then /dev/null will be opened in the child for that standard stream.
  104. *
  105. * @param o the object
  106. * @param m process manager
  107. * @param handler handler called when the process terminates
  108. * @param user argument to handler
  109. * @param file path to executable file
  110. * @param argv arguments array, including the zeroth argument, terminated with a NULL pointer
  111. * @param params.username user account to run the program as, or NULL to not switch user
  112. * @param params.fds array of file descriptors in the parent to map to file descriptors in the child,
  113. * terminated with -1
  114. * @param params.fds_map array of file descriptors in the child that file descriptors in 'fds' will
  115. * be mapped to, in the same order. Must contain the same number of file descriptors
  116. * as the 'fds' argument, and does not have to be terminated with -1.
  117. * @param params.do_setsid if set to non-zero, will make the child call setsid() before exec'ing.
  118. * Failure of setsid() will be ignored.
  119. * @return 1 on success, 0 on failure
  120. */
  121. int BProcess_Init2 (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], struct BProcess_params params) WARN_UNUSED;
  122. /**
  123. * Initializes the process.
  124. * 'file', 'argv', 'username', 'fds' and 'fds_map' arguments are only used during this
  125. * function call.
  126. * If no file descriptor is mapped to a standard stream (file descriptors 0, 1, 2),
  127. * then /dev/null will be opened in the child for that standard stream.
  128. *
  129. * @param o the object
  130. * @param m process manager
  131. * @param handler handler called when the process terminates
  132. * @param user argument to handler
  133. * @param file path to executable file
  134. * @param argv arguments array, including the zeroth argument, terminated with a NULL pointer
  135. * @param username user account to run the program as, or NULL to not switch user
  136. * @param fds array of file descriptors in the parent to map to file descriptors in the child,
  137. * terminated with -1
  138. * @param fds_map array of file descriptors in the child that file descriptors in 'fds' will
  139. * be mapped to, in the same order. Must contain the same number of file descriptors
  140. * as the 'fds' argument, and does not have to be terminated with -1.
  141. * @return 1 on success, 0 on failure
  142. */
  143. int BProcess_InitWithFds (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username, const int *fds, const int *fds_map) WARN_UNUSED;
  144. /**
  145. * Initializes the process.
  146. * Like {@link BProcess_InitWithFds}, but without file descriptor mapping.
  147. * 'file', 'argv' and 'username' arguments are only used during this function call.
  148. *
  149. * @param o the object
  150. * @param m process manager
  151. * @param handler handler called when the process terminates
  152. * @param user argument to handler
  153. * @param file path to executable file
  154. * @param argv arguments array, including the zeroth argument, terminated with a NULL pointer
  155. * @param username user account to run the program as, or NULL to not switch user
  156. * @return 1 on success, 0 on failure
  157. */
  158. int BProcess_Init (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username) WARN_UNUSED;
  159. /**
  160. * Frees the process.
  161. * This does not do anything with the actual child process; it only prevents the user to wait
  162. * for its termination. If the process terminates while a process manager is running, it will still
  163. * be waited for (and will not become a zombie).
  164. *
  165. * @param o the object
  166. */
  167. void BProcess_Free (BProcess *o);
  168. /**
  169. * Sends the process the SIGTERM signal.
  170. * Success of this action does NOT mean that the child has terminated.
  171. *
  172. * @param o the object
  173. * @return 1 on success, 0 on failure
  174. */
  175. int BProcess_Terminate (BProcess *o);
  176. /**
  177. * Sends the process the SIGKILL signal.
  178. * Success of this action does NOT mean that the child has terminated.
  179. *
  180. * @param o the object
  181. * @return 1 on success, 0 on failure
  182. */
  183. int BProcess_Kill (BProcess *o);
  184. #endif