ncdudevmanager_test.c 3.6 KB

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