ncdudevmanager_test.c 4.5 KB

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