net_backend_waitdevice.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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->params->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->params->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 (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  100. {
  101. struct instance *o = vo;
  102. o->i = i;
  103. // check arguments
  104. NCDValRef arg;
  105. if (!NCDVal_ListRead(params->args, 1, &arg)) {
  106. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  107. goto fail0;
  108. }
  109. if (!NCDVal_IsStringNoNulls(arg)) {
  110. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  111. goto fail0;
  112. }
  113. o->ifname = NCDVal_StringValue(arg);
  114. // init client
  115. NCDUdevClient_Init(&o->client, o->i->params->iparams->umanager, o, (NCDUdevClient_handler)client_handler);
  116. // compile regex
  117. if (regcomp(&o->reg, DEVPATH_REGEX, REG_EXTENDED)) {
  118. ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
  119. goto fail1;
  120. }
  121. // set no devpath
  122. o->devpath = NULL;
  123. return;
  124. fail1:
  125. NCDUdevClient_Free(&o->client);
  126. fail0:
  127. NCDModuleInst_Backend_SetError(i);
  128. NCDModuleInst_Backend_Dead(i);
  129. }
  130. static void func_die (void *vo)
  131. {
  132. struct instance *o = vo;
  133. // free devpath
  134. if (o->devpath) {
  135. free(o->devpath);
  136. }
  137. // free regex
  138. regfree(&o->reg);
  139. // free client
  140. NCDUdevClient_Free(&o->client);
  141. NCDModuleInst_Backend_Dead(o->i);
  142. }
  143. static struct NCDModule modules[] = {
  144. {
  145. .type = "net.backend.waitdevice",
  146. .func_new2 = func_new,
  147. .func_die = func_die,
  148. .alloc_size = sizeof(struct instance)
  149. }, {
  150. .type = NULL
  151. }
  152. };
  153. const struct NCDModuleGroup ncdmodule_net_backend_waitdevice = {
  154. .modules = modules
  155. };