proto-shell: add support for appending configuration data over multiple update_link...
[project/netifd.git] / interface-event.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 #include <libubox/uloop.h>
7
8 #include "netifd.h"
9 #include "interface.h"
10 #include "ubus.h"
11
12 char *hotplug_cmd_path = DEFAULT_HOTPLUG_PATH;
13 static struct interface *current;
14 static enum interface_event current_ev;
15 static struct list_head pending = LIST_HEAD_INIT(pending);
16
17 static void task_complete(struct uloop_process *proc, int ret);
18 static struct uloop_process task = {
19 .cb = task_complete,
20 };
21
22 static void
23 run_cmd(const char *ifname, const char *device, bool up)
24 {
25 char *argv[3];
26 int pid;
27
28 pid = fork();
29 if (pid < 0)
30 return task_complete(NULL, -1);
31
32 if (pid > 0) {
33 task.pid = pid;
34 uloop_process_add(&task);
35 return;
36 }
37
38 setenv("ACTION", up ? "ifup" : "ifdown", 1);
39 setenv("INTERFACE", ifname, 1);
40 if (device)
41 setenv("DEVICE", device, 1);
42 argv[0] = hotplug_cmd_path;
43 argv[1] = "iface";
44 argv[2] = NULL;
45 execvp(argv[0], argv);
46 exit(127);
47 }
48
49 static void
50 call_hotplug(void)
51 {
52 const char *device = NULL;
53 if (list_empty(&pending))
54 return;
55
56 current = list_first_entry(&pending, struct interface, hotplug_list);
57 current_ev = current->hotplug_ev;
58 list_del_init(&current->hotplug_list);
59
60 if (current_ev == IFEV_UP && current->l3_dev.dev)
61 device = current->l3_dev.dev->ifname;
62
63 D(SYSTEM, "Call hotplug handler for interface '%s' (%s)\n", current->name, device ? device : "none");
64 run_cmd(current->name, device, current_ev == IFEV_UP);
65 }
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 /*
77 * Queue an interface for an up/down event.
78 * An interface can only have one event in the queue and one
79 * event waiting for completion.
80 * When queueing an event that is the same as the one waiting for
81 * completion, remove the interface from the queue
82 */
83 void
84 interface_queue_event(struct interface *iface, enum interface_event ev)
85 {
86 enum interface_event last_ev;
87
88 D(SYSTEM, "Queue hotplug handler for interface '%s'\n", iface->name);
89 netifd_ubus_interface_event(iface, ev == IFEV_UP);
90 if (current == iface)
91 last_ev = current_ev;
92 else
93 last_ev = iface->hotplug_ev;
94
95 iface->hotplug_ev = ev;
96 if (last_ev == ev && !list_empty(&iface->hotplug_list))
97 list_del(&iface->hotplug_list);
98 else if (last_ev != ev && list_empty(&iface->hotplug_list))
99 list_add(&iface->hotplug_list, &pending);
100
101 if (!task.pending && !current)
102 call_hotplug();
103 }
104
105 void
106 interface_dequeue_event(struct interface *iface)
107 {
108 if (iface == current)
109 current = NULL;
110
111 if (!list_empty(&iface->hotplug_list))
112 list_del_init(&iface->hotplug_list);
113 }