ncdudevmanager_test.c 3.7 KB

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