ncdudevmonitor_test.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file ncdudevmonitor_test.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 <string.h>
  23. #include <system/BTime.h>
  24. #include <base/BLog.h>
  25. #include <system/BReactor.h>
  26. #include <system/BSignal.h>
  27. #include <system/BProcess.h>
  28. #include <system/BNetwork.h>
  29. #include <udevmonitor/NCDUdevMonitor.h>
  30. BReactor reactor;
  31. BProcessManager manager;
  32. NCDUdevMonitor monitor;
  33. static void signal_handler (void *user);
  34. static void monitor_handler_event (void *unused);
  35. static void monitor_handler_error (void *unused, int is_error);
  36. int main (int argc, char **argv)
  37. {
  38. int ret = 1;
  39. if (argc < 2 || (strcmp(argv[1], "monitor") && strcmp(argv[1], "info"))) {
  40. fprintf(stderr, "Usage: %s <monitor/info>\n", argv[0]);
  41. goto fail0;
  42. }
  43. int is_info_mode = !strcmp(argv[1], "info");
  44. if (!BNetwork_GlobalInit()) {
  45. DEBUG("BNetwork_GlobalInit failed");
  46. goto fail0;
  47. }
  48. BTime_Init();
  49. BLog_InitStdout();
  50. if (!BReactor_Init(&reactor)) {
  51. DEBUG("BReactor_Init failed");
  52. goto fail1;
  53. }
  54. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  55. DEBUG("BSignal_Init failed");
  56. goto fail2;
  57. }
  58. if (!BProcessManager_Init(&manager, &reactor)) {
  59. DEBUG("BProcessManager_Init failed");
  60. goto fail3;
  61. }
  62. if (!NCDUdevMonitor_Init(&monitor, &reactor, &manager, is_info_mode, NULL,
  63. monitor_handler_event,
  64. monitor_handler_error
  65. )) {
  66. DEBUG("NCDUdevMonitor_Init failed");
  67. goto fail4;
  68. }
  69. ret = BReactor_Exec(&reactor);
  70. NCDUdevMonitor_Free(&monitor);
  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 ret;
  82. }
  83. void signal_handler (void *user)
  84. {
  85. DEBUG("termination requested");
  86. BReactor_Quit(&reactor, 1);
  87. }
  88. void monitor_handler_event (void *unused)
  89. {
  90. // accept event
  91. NCDUdevMonitor_Done(&monitor);
  92. if (NCDUdevMonitor_IsReadyEvent(&monitor)) {
  93. printf("ready\n");
  94. return;
  95. }
  96. printf("event\n");
  97. int num_props = NCDUdevMonitor_GetNumProperties(&monitor);
  98. for (int i = 0; i < num_props; i++) {
  99. const char *name;
  100. const char *value;
  101. NCDUdevMonitor_GetProperty(&monitor, i, &name, &value);
  102. printf(" %s=%s\n", name, value);
  103. }
  104. }
  105. void monitor_handler_error (void *unused, int is_error)
  106. {
  107. if (is_error) {
  108. DEBUG("monitor error");
  109. } else {
  110. DEBUG("monitor finished");
  111. }
  112. BReactor_Quit(&reactor, (is_error ? 1 : 0));
  113. }