bprocess_example.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <system/DebugObject.h>
  26. #include <system/BLog.h>
  27. #include <system/BReactor.h>
  28. #include <system/BSignal.h>
  29. #include <system/BTime.h>
  30. #include <system/BProcess.h>
  31. BReactor reactor;
  32. BProcessManager manager;
  33. BProcess process;
  34. static void terminate (int ret);
  35. static void signal_handler (void *user);
  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. if (argc < 2) {
  43. printf("Usage: %s <program> [argument ...]\n", argv[0]);
  44. goto fail0;
  45. }
  46. char *program = argv[1];
  47. BTime_Init();
  48. BLog_InitStdout();
  49. if (!BReactor_Init(&reactor)) {
  50. DEBUG("BReactor_Init failed");
  51. goto fail1;
  52. }
  53. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  54. DEBUG("BSignal_Init failed");
  55. goto fail2;
  56. }
  57. if (!BProcessManager_Init(&manager, &reactor)) {
  58. DEBUG("BProcessManager_Init failed");
  59. goto fail3;
  60. }
  61. char **p_argv = argv + 1;
  62. if (!BProcess_Init(&process, &manager, process_handler, NULL, program, p_argv)) {
  63. DEBUG("BProcess_Init failed");
  64. goto fail4;
  65. }
  66. int ret = BReactor_Exec(&reactor);
  67. BReactor_Free(&reactor);
  68. BLog_Free();
  69. DebugObjectGlobal_Finish();
  70. return ret;
  71. fail4:
  72. BProcessManager_Free(&manager);
  73. fail3:
  74. BSignal_Finish();
  75. fail2:
  76. BReactor_Free(&reactor);
  77. fail1:
  78. BLog_Free();
  79. fail0:
  80. DebugObjectGlobal_Finish();
  81. return 1;
  82. }
  83. void terminate (int ret)
  84. {
  85. BProcess_Free(&process);
  86. BProcessManager_Free(&manager);
  87. BSignal_Finish();
  88. BReactor_Quit(&reactor, ret);
  89. }
  90. void signal_handler (void *user)
  91. {
  92. DEBUG("termination requested, passing to child");
  93. BProcess_Terminate(&process);
  94. }
  95. void process_handler (void *user, int normally, uint8_t normally_exit_status)
  96. {
  97. DEBUG("process terminated");
  98. int ret = (normally ? normally_exit_status : 1);
  99. terminate(ret);
  100. }