ncdudevmanager_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @file ncdudevmanager_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 <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <misc/debug.h>
  33. #include <system/BTime.h>
  34. #include <base/BLog.h>
  35. #include <system/BReactor.h>
  36. #include <system/BUnixSignal.h>
  37. #include <system/BProcess.h>
  38. #include <system/BNetwork.h>
  39. #include <udevmonitor/NCDUdevManager.h>
  40. BReactor reactor;
  41. BUnixSignal usignal;
  42. BProcessManager manager;
  43. NCDUdevManager umanager;
  44. NCDUdevClient client;
  45. static void signal_handler (void *user, int signo);
  46. static void client_handler (void *unused, char *devpath, int have_map, BStringMap map);
  47. int main (int argc, char **argv)
  48. {
  49. if (!(argc == 1 || (argc == 2 && !strcmp(argv[1], "--no-udev")))) {
  50. fprintf(stderr, "Usage: %s [--no-udev]\n", (argc > 0 ? argv[0] : NULL));
  51. goto fail0;
  52. }
  53. int no_udev = (argc == 2);
  54. if (!BNetwork_GlobalInit()) {
  55. DEBUG("BNetwork_GlobalInit failed");
  56. goto fail0;
  57. }
  58. BTime_Init();
  59. BLog_InitStdout();
  60. if (!BReactor_Init(&reactor)) {
  61. DEBUG("BReactor_Init failed");
  62. goto fail1;
  63. }
  64. sigset_t set;
  65. sigemptyset(&set);
  66. sigaddset(&set, SIGINT);
  67. sigaddset(&set, SIGTERM);
  68. sigaddset(&set, SIGHUP);
  69. if (!BUnixSignal_Init(&usignal, &reactor, set, signal_handler, NULL)) {
  70. fprintf(stderr, "BUnixSignal_Init failed\n");
  71. goto fail2;
  72. }
  73. if (!BProcessManager_Init(&manager, &reactor)) {
  74. DEBUG("BProcessManager_Init failed");
  75. goto fail3;
  76. }
  77. NCDUdevManager_Init(&umanager, no_udev, &reactor, &manager);
  78. NCDUdevClient_Init(&client, &umanager, NULL, client_handler);
  79. BReactor_Exec(&reactor);
  80. NCDUdevClient_Free(&client);
  81. NCDUdevManager_Free(&umanager);
  82. BProcessManager_Free(&manager);
  83. fail3:
  84. BUnixSignal_Free(&usignal, 0);
  85. fail2:
  86. BReactor_Free(&reactor);
  87. fail1:
  88. BLog_Free();
  89. fail0:
  90. DebugObjectGlobal_Finish();
  91. return 1;
  92. }
  93. static void signal_handler (void *user, int signo)
  94. {
  95. if (signo == SIGHUP) {
  96. fprintf(stderr, "received SIGHUP, restarting client\n");
  97. NCDUdevClient_Free(&client);
  98. NCDUdevClient_Init(&client, &umanager, NULL, client_handler);
  99. } else {
  100. fprintf(stderr, "received %s, exiting\n", (signo == SIGINT ? "SIGINT" : "SIGTERM"));
  101. // exit event loop
  102. BReactor_Quit(&reactor, 1);
  103. }
  104. }
  105. void client_handler (void *unused, char *devpath, int have_map, BStringMap map)
  106. {
  107. printf("event %s\n", devpath);
  108. if (!have_map) {
  109. printf(" no map\n");
  110. } else {
  111. printf(" map:\n");
  112. const char *name = BStringMap_First(&map);
  113. while (name) {
  114. printf(" %s=%s\n", name, BStringMap_Get(&map, name));
  115. name = BStringMap_Next(&map, name);
  116. }
  117. }
  118. const BStringMap *cache_map = NCDUdevManager_Query(&umanager, devpath);
  119. if (!cache_map) {
  120. printf(" no cache\n");
  121. } else {
  122. printf(" cache:\n");
  123. const char *name = BStringMap_First(cache_map);
  124. while (name) {
  125. printf(" %s=%s\n", name, BStringMap_Get(cache_map, name));
  126. name = BStringMap_Next(cache_map, name);
  127. }
  128. }
  129. if (have_map) {
  130. BStringMap_Free(&map);
  131. }
  132. free(devpath);
  133. }