net_backend_waitdevice.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. size_t ifname_len;
  50. NCDUdevClient client;
  51. regex_t reg;
  52. char *devpath;
  53. uintmax_t ifindex;
  54. };
  55. static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
  56. {
  57. if (o->devpath && !strcmp(devpath, o->devpath) && !NCDUdevManager_Query(o->i->params->iparams->umanager, o->devpath)) {
  58. // free devpath
  59. free(o->devpath);
  60. // set no devpath
  61. o->devpath = NULL;
  62. // signal down
  63. NCDModuleInst_Backend_Down(o->i);
  64. } else {
  65. const BStringMap *cache_map = NCDUdevManager_Query(o->i->params->iparams->umanager, devpath);
  66. if (!cache_map) {
  67. goto out;
  68. }
  69. int match_res = regexec(&o->reg, devpath, 0, NULL, 0);
  70. const char *interface = BStringMap_Get(cache_map, "INTERFACE");
  71. const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
  72. uintmax_t ifindex;
  73. if (!(!match_res && interface && strlen(interface) == o->ifname_len && !memcmp(interface, o->ifname, o->ifname_len) && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
  74. goto out;
  75. }
  76. if (o->devpath && (strcmp(o->devpath, devpath) || o->ifindex != ifindex)) {
  77. // free devpath
  78. free(o->devpath);
  79. // set no devpath
  80. o->devpath = NULL;
  81. // signal down
  82. NCDModuleInst_Backend_Down(o->i);
  83. }
  84. if (!o->devpath) {
  85. // grab devpath
  86. o->devpath = devpath;
  87. devpath = NULL;
  88. // remember ifindex
  89. o->ifindex = ifindex;
  90. // signal up
  91. NCDModuleInst_Backend_Up(o->i);
  92. }
  93. }
  94. out:
  95. free(devpath);
  96. if (have_map) {
  97. BStringMap_Free(&map);
  98. }
  99. }
  100. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  101. {
  102. struct instance *o = vo;
  103. o->i = i;
  104. // check arguments
  105. NCDValRef arg;
  106. if (!NCDVal_ListRead(params->args, 1, &arg)) {
  107. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  108. goto fail0;
  109. }
  110. if (!NCDVal_IsStringNoNulls(arg)) {
  111. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  112. goto fail0;
  113. }
  114. o->ifname = NCDVal_StringData(arg);
  115. o->ifname_len = NCDVal_StringLength(arg);
  116. // init client
  117. NCDUdevClient_Init(&o->client, o->i->params->iparams->umanager, o, (NCDUdevClient_handler)client_handler);
  118. // compile regex
  119. if (regcomp(&o->reg, DEVPATH_REGEX, REG_EXTENDED)) {
  120. ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
  121. goto fail1;
  122. }
  123. // set no devpath
  124. o->devpath = NULL;
  125. return;
  126. fail1:
  127. NCDUdevClient_Free(&o->client);
  128. fail0:
  129. NCDModuleInst_Backend_DeadError(i);
  130. }
  131. static void func_die (void *vo)
  132. {
  133. struct instance *o = vo;
  134. // free devpath
  135. if (o->devpath) {
  136. free(o->devpath);
  137. }
  138. // free regex
  139. regfree(&o->reg);
  140. // free client
  141. NCDUdevClient_Free(&o->client);
  142. NCDModuleInst_Backend_Dead(o->i);
  143. }
  144. static struct NCDModule modules[] = {
  145. {
  146. .type = "net.backend.waitdevice",
  147. .func_new2 = func_new,
  148. .func_die = func_die,
  149. .alloc_size = sizeof(struct instance)
  150. }, {
  151. .type = NULL
  152. }
  153. };
  154. const struct NCDModuleGroup ncdmodule_net_backend_waitdevice = {
  155. .modules = modules
  156. };