net_backend_waitdevice.c 5.4 KB

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