apply device settings to simple devices with config specified from the interface...
[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, 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         argv[0] = hotplug_cmd_path;
41         argv[1] = "network";
42         argv[2] = NULL;
43         execvp(argv[0], argv);
44         exit(127);
45 }
46
47 static void
48 call_hotplug(void)
49 {
50         if (list_empty(&pending))
51                 return;
52
53         current = list_first_entry(&pending, struct interface, hotplug_list);
54         current_ev = current->hotplug_ev;
55         list_del_init(&current->hotplug_list);
56
57         D(SYSTEM, "Call hotplug handler for interface '%s'\n", current->name);
58         run_cmd(current->name, current_ev == IFEV_UP);
59 }
60
61 static void
62 task_complete(struct uloop_process *proc, int ret)
63 {
64         if (current)
65                 D(SYSTEM, "Complete hotplug handler for interface '%s'\n", current->name);
66         current = NULL;
67         call_hotplug();
68 }
69
70 /*
71  * Queue an interface for an up/down event.
72  * An interface can only have one event in the queue and one
73  * event waiting for completion.
74  * When queueing an event that is the same as the one waiting for
75  * completion, remove the interface from the queue
76  */
77 void
78 interface_queue_event(struct interface *iface, enum interface_event ev)
79 {
80         enum interface_event last_ev;
81
82         D(SYSTEM, "Queue hotplug handler for interface '%s'\n", iface->name);
83         netifd_ubus_interface_event(iface, ev == IFEV_UP);
84         if (current == iface)
85                 last_ev = current_ev;
86         else
87                 last_ev = iface->hotplug_ev;
88
89         iface->hotplug_ev = ev;
90         if (last_ev == ev && !list_empty(&iface->hotplug_list))
91                 list_del(&iface->hotplug_list);
92         else if (last_ev != ev && list_empty(&iface->hotplug_list))
93                 list_add(&iface->hotplug_list, &pending);
94
95         if (!task.pending && !current)
96                 call_hotplug();
97 }
98
99 void
100 interface_dequeue_event(struct interface *iface)
101 {
102         if (iface == current)
103                 current = NULL;
104
105         if (!list_empty(&iface->hotplug_list))
106                 list_del_init(&iface->hotplug_list);
107 }