ncdudevmonitor_test.c 3.2 KB

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