net_backend_waitdevice.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @file net_backend_waitdevice.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. * @section DESCRIPTION
  23. *
  24. * Module which waits for the presence of a network interface.
  25. *
  26. * Synopsis: net.backend.waitdevice(string ifname)
  27. * Description: statement is UP when a network interface named ifname
  28. * exists, and DOWN when it does not.
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <misc/parse_number.h>
  33. #include <ncd/NCDModule.h>
  34. #include <ncd/NCDIfConfig.h>
  35. #include <generated/blog_channel_ncd_net_backend_waitdevice.h>
  36. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  37. struct instance {
  38. NCDModuleInst *i;
  39. const char *ifname;
  40. NCDUdevClient client;
  41. char *devpath;
  42. uintmax_t ifindex;
  43. };
  44. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  45. {
  46. if (o->devpath && !strcmp(devpath, o->devpath) && !NCDUdevManager_Query(o->i->umanager, o->devpath)) {
  47. // free devpath
  48. free(o->devpath);
  49. // set no devpath
  50. o->devpath = NULL;
  51. // signal down
  52. NCDModuleInst_Backend_Down(o->i);
  53. } else {
  54. const BStringMap *cache_map = NCDUdevManager_Query(o->i->umanager, devpath);
  55. if (!cache_map) {
  56. goto out;
  57. }
  58. const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
  59. const char *interface = BStringMap_Get(cache_map, "INTERFACE");
  60. const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
  61. uintmax_t ifindex;
  62. if (!(subsystem && !strcmp(subsystem, "net") && interface && !strcmp(interface, o->ifname) && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
  63. goto out;
  64. }
  65. if (o->devpath && (strcmp(o->devpath, devpath) || o->ifindex != ifindex)) {
  66. // free devpath
  67. free(o->devpath);
  68. // set no devpath
  69. o->devpath = NULL;
  70. // signal down
  71. NCDModuleInst_Backend_Down(o->i);
  72. }
  73. if (!o->devpath) {
  74. // grab devpath
  75. o->devpath = devpath;
  76. devpath = NULL;
  77. // remember ifindex
  78. o->ifindex = ifindex;
  79. // signal up
  80. NCDModuleInst_Backend_Up(o->i);
  81. }
  82. }
  83. out:
  84. free(devpath);
  85. if (have_map) {
  86. BStringMap_Free(&map);
  87. }
  88. }
  89. static void func_new (NCDModuleInst *i)
  90. {
  91. // allocate instance
  92. struct instance *o = malloc(sizeof(*o));
  93. if (!o) {
  94. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  95. goto fail0;
  96. }
  97. NCDModuleInst_Backend_SetUser(i, o);
  98. // init arguments
  99. o->i = i;
  100. // check arguments
  101. NCDValue *arg;
  102. if (!NCDValue_ListRead(i->args, 1, &arg)) {
  103. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  104. goto fail1;
  105. }
  106. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  107. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  108. goto fail1;
  109. }
  110. o->ifname = NCDValue_StringValue(arg);
  111. // init client
  112. NCDUdevClient_Init(&o->client, o->i->umanager, o, (NCDUdevClient_handler)client_handler);
  113. // set no devpath
  114. o->devpath = NULL;
  115. return;
  116. fail1:
  117. free(o);
  118. fail0:
  119. NCDModuleInst_Backend_SetError(i);
  120. NCDModuleInst_Backend_Dead(i);
  121. }
  122. static void func_die (void *vo)
  123. {
  124. struct instance *o = vo;
  125. NCDModuleInst *i = o->i;
  126. // free devpath
  127. if (o->devpath) {
  128. free(o->devpath);
  129. }
  130. // free client
  131. NCDUdevClient_Free(&o->client);
  132. // free instance
  133. free(o);
  134. NCDModuleInst_Backend_Dead(i);
  135. }
  136. static const struct NCDModule modules[] = {
  137. {
  138. .type = "net.backend.waitdevice",
  139. .func_new = func_new,
  140. .func_die = func_die
  141. }, {
  142. .type = NULL
  143. }
  144. };
  145. const struct NCDModuleGroup ncdmodule_net_backend_waitdevice = {
  146. .modules = modules
  147. };