add support for calls to a hotplug script on interface state changes
[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         run_cmd(current->name, current_ev == IFEV_UP);
56 }
57
58 static void
59 task_complete(struct uloop_process *proc, int ret)
60 {
61         current = NULL;
62         call_hotplug();
63 }
64
65 /*
66  * Queue an interface for an up/down event.
67  * An interface can only have one event in the queue and one
68  * event waiting for completion.
69  * When queueing an event that is the same as the one waiting for
70  * completion, remove the interface from the queue
71  */
72 void
73 interface_queue_event(struct interface *iface, enum interface_event ev)
74 {
75         enum interface_event last_ev;
76
77         D(SYSTEM, "Queue hotplug handler for interface '%s'\n", iface->name);
78         if (current == iface)
79                 last_ev = current_ev;
80         else
81                 last_ev = iface->hotplug_ev;
82
83         iface->hotplug_ev = ev;
84         if (last_ev == ev && !list_empty(&iface->hotplug_list))
85                 list_del(&iface->hotplug_list);
86         else if (last_ev != ev && list_empty(&iface->hotplug_list))
87                 list_add(&iface->hotplug_list, &pending);
88
89         if (!task.pending && !current)
90                 call_hotplug();
91 }
92
93 void
94 interface_dequeue_event(struct interface *iface)
95 {
96         if (iface == current)
97                 current = NULL;
98
99         if (!list_empty(&iface->hotplug_list))
100                 list_del_init(&iface->hotplug_list);
101 }