net_backend_waitdevice.c 5.4 KB

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