ncdudevmonitor_test.c 4.4 KB

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