BProcess.h 5.3 KB

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