ba7405a8410534b3ae4b90e7995f3e8c131e4a83
[project/netifd.git] / interface-hotplug.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
11 char *hotplug_cmd_path = DEFAULT_HOTPLUG_PATH;
12 static struct interface *current;
13 static enum interface_event current_ev;
14 static struct list_head pending = LIST_HEAD_INIT(pending);
15
16 static void task_complete(struct uloop_process *proc, int ret);
17 static struct uloop_process task = {
18         .cb = task_complete,
19 };
20
21 static void
22 run_cmd(const char *ifname, bool up)
23 {
24         char *argv[3];
25         int pid;
26
27         pid = fork();
28         if (pid < 0)
29                 return task_complete(NULL, -1);
30
31         if (pid > 0) {
32                 task.pid = pid;
33                 uloop_process_add(&task);
34                 return;
35         }
36
37         setenv("ACTION", up ? "ifup" : "ifdown", 1);
38         setenv("INTERFACE", ifname, 1);
39         argv[0] = hotplug_cmd_path;
40         argv[1] = "network";
41         argv[2] = NULL;
42         execvp(argv[0], argv);
43         exit(127);
44 }
45
46 static void
47 call_hotplug(void)
48 {
49         if (list_empty(&pending))
50                 return;
51
52         current = list_first_entry(&pending, struct interface, hotplug_list);
53         current_ev = current->hotplug_ev;
54         list_del_init(&current->hotplug_list);
55
56         D(SYSTEM, "Call hotplug handler for interface '%s'\n", current->name);
57         run_cmd(current->name, current_ev == IFEV_UP);
58 }
59
60 static void
61 task_complete(struct uloop_process *proc, int ret)
62 {
63         if (current)
64                 D(SYSTEM, "Complete hotplug handler for interface '%s'\n", current->name);
65         current = NULL;
66         call_hotplug();
67 }
68
69 /*
70  * Queue an interface for an up/down event.
71  * An interface can only have one event in the queue and one
72  * event waiting for completion.
73  * When queueing an event that is the same as the one waiting for
74  * completion, remove the interface from the queue
75  */
76 void
77 interface_queue_event(struct interface *iface, enum interface_event ev)
78 {
79         enum interface_event last_ev;
80
81         D(SYSTEM, "Queue hotplug handler for interface '%s'\n", iface->name);
82         if (current == iface)
83                 last_ev = current_ev;
84         else
85                 last_ev = iface->hotplug_ev;
86
87         iface->hotplug_ev = ev;
88         if (last_ev == ev && !list_empty(&iface->hotplug_list))
89                 list_del(&iface->hotplug_list);
90         else if (last_ev != ev && list_empty(&iface->hotplug_list))
91                 list_add(&iface->hotplug_list, &pending);
92
93         if (!task.pending && !current)
94                 call_hotplug();
95 }
96
97 void
98 interface_dequeue_event(struct interface *iface)
99 {
100         if (iface == current)
101                 current = NULL;
102
103         if (!list_empty(&iface->hotplug_list))
104                 list_del_init(&iface->hotplug_list);
105 }