bprocess_example.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @file bprocess_example.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 <stddef.h>
  23. #include <unistd.h>
  24. #include <misc/debug.h>
  25. #include <base/DebugObject.h>
  26. #include <base/BLog.h>
  27. #include <system/BReactor.h>
  28. #include <system/BUnixSignal.h>
  29. #include <system/BTime.h>
  30. #include <system/BProcess.h>
  31. BReactor reactor;
  32. BUnixSignal unixsignal;
  33. BProcessManager manager;
  34. BProcess process;
  35. static void unixsignal_handler (void *user, int signo);
  36. static void process_handler (void *user, int normally, uint8_t normally_exit_status);
  37. int main (int argc, char **argv)
  38. {
  39. if (argc <= 0) {
  40. return 1;
  41. }
  42. int ret = 1;
  43. if (argc < 2) {
  44. printf("Usage: %s <program> [argument ...]\n", argv[0]);
  45. goto fail0;
  46. }
  47. char *program = argv[1];
  48. // init time
  49. BTime_Init();
  50. // init logger
  51. BLog_InitStdout();
  52. // init reactor (event loop)
  53. if (!BReactor_Init(&reactor)) {
  54. DEBUG("BReactor_Init failed");
  55. goto fail1;
  56. }
  57. // choose signals to catch
  58. sigset_t set;
  59. sigemptyset(&set);
  60. sigaddset(&set, SIGINT);
  61. sigaddset(&set, SIGTERM);
  62. // init BUnixSignal for catching signals
  63. if (!BUnixSignal_Init(&unixsignal, &reactor, set, unixsignal_handler, NULL)) {
  64. DEBUG("BUnixSignal_Init failed");
  65. goto fail2;
  66. }
  67. // init process manager
  68. if (!BProcessManager_Init(&manager, &reactor)) {
  69. DEBUG("BProcessManager_Init failed");
  70. goto fail3;
  71. }
  72. char **p_argv = argv + 1;
  73. // map fds 0, 1, 2 in child to fds 0, 1, 2 in parent
  74. int fds[] = { 0, 1, 2, -1 };
  75. int fds_map[] = { 0, 1, 2 };
  76. // start child process
  77. if (!BProcess_InitWithFds(&process, &manager, process_handler, NULL, program, p_argv, NULL, fds, fds_map)) {
  78. DEBUG("BProcess_Init failed");
  79. goto fail4;
  80. }
  81. // enter event loop
  82. ret = BReactor_Exec(&reactor);
  83. BProcess_Free(&process);
  84. fail4:
  85. BProcessManager_Free(&manager);
  86. fail3:
  87. BUnixSignal_Free(&unixsignal, 0);
  88. fail2:
  89. BReactor_Free(&reactor);
  90. fail1:
  91. BLog_Free();
  92. fail0:
  93. DebugObjectGlobal_Finish();
  94. return ret;
  95. }
  96. void unixsignal_handler (void *user, int signo)
  97. {
  98. DEBUG("received %s, terminating child", (signo == SIGINT ? "SIGINT" : "SIGTERM"));
  99. // send SIGTERM to child
  100. BProcess_Terminate(&process);
  101. }
  102. void process_handler (void *user, int normally, uint8_t normally_exit_status)
  103. {
  104. DEBUG("process terminated");
  105. int ret = (normally ? normally_exit_status : 1);
  106. // return from event loop
  107. BReactor_Quit(&reactor, ret);
  108. }