net_backend_waitlink.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @file net_backend_waitlink.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 link on a network interface.
  32. *
  33. * Synopsis: net.backend.waitlink(string ifname)
  34. */
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <misc/get_iface_info.h>
  38. #include <ncd/NCDModule.h>
  39. #include <ncd/NCDIfConfig.h>
  40. #include <ncd/NCDInterfaceMonitor.h>
  41. #include <generated/blog_channel_ncd_net_backend_waitlink.h>
  42. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  43. struct instance {
  44. NCDModuleInst *i;
  45. NCDInterfaceMonitor monitor;
  46. int up;
  47. };
  48. static void instance_free (struct instance *o);
  49. static void monitor_handler (struct instance *o, struct NCDInterfaceMonitor_event event)
  50. {
  51. ASSERT(event.event == NCDIFMONITOR_EVENT_LINK_UP || event.event == NCDIFMONITOR_EVENT_LINK_DOWN)
  52. int was_up = o->up;
  53. o->up = (event.event == NCDIFMONITOR_EVENT_LINK_UP);
  54. if (o->up && !was_up) {
  55. NCDModuleInst_Backend_Up(o->i);
  56. }
  57. else if (!o->up && was_up) {
  58. NCDModuleInst_Backend_Down(o->i);
  59. }
  60. }
  61. static void monitor_handler_error (struct instance *o)
  62. {
  63. ModuleLog(o->i, BLOG_ERROR, "monitor error");
  64. NCDModuleInst_Backend_SetError(o->i);
  65. instance_free(o);
  66. }
  67. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  68. {
  69. struct instance *o = vo;
  70. o->i = i;
  71. // check arguments
  72. NCDValRef arg;
  73. if (!NCDVal_ListRead(params->args, 1, &arg)) {
  74. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  75. goto fail0;
  76. }
  77. if (!NCDVal_IsStringNoNulls(arg)) {
  78. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  79. goto fail0;
  80. }
  81. const char *ifname = NCDVal_StringValue(arg);
  82. // get interface index
  83. int ifindex;
  84. if (!get_iface_info(ifname, NULL, NULL, &ifindex)) {
  85. ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
  86. goto fail0;
  87. }
  88. // init monitor
  89. if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_LINK, i->params->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) {
  90. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  91. goto fail0;
  92. }
  93. // set not up
  94. o->up = 0;
  95. return;
  96. fail0:
  97. NCDModuleInst_Backend_SetError(i);
  98. NCDModuleInst_Backend_Dead(i);
  99. }
  100. static void instance_free (struct instance *o)
  101. {
  102. // free monitor
  103. NCDInterfaceMonitor_Free(&o->monitor);
  104. NCDModuleInst_Backend_Dead(o->i);
  105. }
  106. static void func_die (void *vo)
  107. {
  108. struct instance *o = vo;
  109. instance_free(o);
  110. }
  111. static const struct NCDModule modules[] = {
  112. {
  113. .type = "net.backend.waitlink",
  114. .func_new2 = func_new,
  115. .func_die = func_die,
  116. .alloc_size = sizeof(struct instance)
  117. }, {
  118. .type = NULL
  119. }
  120. };
  121. struct NCDModuleGroup ncdmodule_net_backend_waitlink = {
  122. .modules = modules
  123. };