interface-event.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <libubox/uloop.h>
  19. #include "netifd.h"
  20. #include "interface.h"
  21. #include "ubus.h"
  22. char *hotplug_cmd_path = DEFAULT_HOTPLUG_PATH;
  23. static struct interface *current;
  24. static enum interface_event current_ev;
  25. static struct list_head pending = LIST_HEAD_INIT(pending);
  26. static void task_complete(struct uloop_process *proc, int ret);
  27. static struct uloop_process task = {
  28. .cb = task_complete,
  29. };
  30. static void
  31. run_cmd(const char *ifname, const char *device, bool up)
  32. {
  33. char *argv[3];
  34. int pid;
  35. pid = fork();
  36. if (pid < 0)
  37. return task_complete(NULL, -1);
  38. if (pid > 0) {
  39. task.pid = pid;
  40. uloop_process_add(&task);
  41. return;
  42. }
  43. setenv("ACTION", up ? "ifup" : "ifdown", 1);
  44. setenv("INTERFACE", ifname, 1);
  45. if (device)
  46. setenv("DEVICE", device, 1);
  47. argv[0] = hotplug_cmd_path;
  48. argv[1] = "iface";
  49. argv[2] = NULL;
  50. execvp(argv[0], argv);
  51. exit(127);
  52. }
  53. static void
  54. call_hotplug(void)
  55. {
  56. const char *device = NULL;
  57. if (list_empty(&pending))
  58. return;
  59. current = list_first_entry(&pending, struct interface, hotplug_list);
  60. current_ev = current->hotplug_ev;
  61. list_del_init(&current->hotplug_list);
  62. if (current_ev == IFEV_UP && current->l3_dev.dev)
  63. device = current->l3_dev.dev->ifname;
  64. D(SYSTEM, "Call hotplug handler for interface '%s' (%s)\n", current->name, device ? device : "none");
  65. run_cmd(current->name, device, current_ev == IFEV_UP);
  66. }
  67. static void
  68. task_complete(struct uloop_process *proc, int ret)
  69. {
  70. if (current)
  71. D(SYSTEM, "Complete hotplug handler for interface '%s'\n", current->name);
  72. current = NULL;
  73. call_hotplug();
  74. }
  75. /*
  76. * Queue an interface for an up/down event.
  77. * An interface can only have one event in the queue and one
  78. * event waiting for completion.
  79. * When queueing an event that is the same as the one waiting for
  80. * completion, remove the interface from the queue
  81. */
  82. static void
  83. interface_queue_event(struct interface *iface, enum interface_event ev)
  84. {
  85. enum interface_event last_ev;
  86. D(SYSTEM, "Queue hotplug handler for interface '%s'\n", iface->name);
  87. netifd_ubus_interface_event(iface, ev == IFEV_UP);
  88. if (current == iface)
  89. last_ev = current_ev;
  90. else
  91. last_ev = iface->hotplug_ev;
  92. iface->hotplug_ev = ev;
  93. if (last_ev == ev && !list_empty(&iface->hotplug_list))
  94. list_del_init(&iface->hotplug_list);
  95. else if (last_ev != ev && list_empty(&iface->hotplug_list))
  96. list_add(&iface->hotplug_list, &pending);
  97. if (!task.pending && !current)
  98. call_hotplug();
  99. }
  100. static void
  101. interface_dequeue_event(struct interface *iface)
  102. {
  103. if (iface == current)
  104. current = NULL;
  105. if (!list_empty(&iface->hotplug_list))
  106. list_del_init(&iface->hotplug_list);
  107. }
  108. static void interface_event_cb(struct interface_user *dep, struct interface *iface,
  109. enum interface_event ev)
  110. {
  111. switch (ev) {
  112. case IFEV_UP:
  113. case IFEV_DOWN:
  114. interface_queue_event(iface, ev);
  115. break;
  116. case IFEV_FREE:
  117. case IFEV_RELOAD:
  118. interface_dequeue_event(iface);
  119. break;
  120. }
  121. }
  122. static struct interface_user event_user = {
  123. .cb = interface_event_cb
  124. };
  125. static void __init interface_event_init(void)
  126. {
  127. interface_add_user(&event_user, NULL);
  128. }