net_backend_waitdevice.c 5.7 KB

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