bprocess_example.c 4.0 KB

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