wireless: make interfaces in status an array instead of an object
[project/netifd.git] / interface-event.c
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
18 #include <libubox/uloop.h>
19
20 #include "netifd.h"
21 #include "interface.h"
22 #include "ubus.h"
23
24 char *hotplug_cmd_path = DEFAULT_HOTPLUG_PATH;
25 static struct interface *current;
26 static enum interface_event current_ev;
27 static struct list_head pending = LIST_HEAD_INIT(pending);
28
29 static void task_complete(struct uloop_process *proc, int ret);
30 static struct uloop_process task = {
31         .cb = task_complete,
32 };
33
34 static void
35 run_cmd(const char *ifname, const char *device, enum interface_event event)
36 {
37         char *argv[3];
38         int pid;
39
40         pid = fork();
41         if (pid < 0)
42                 return task_complete(NULL, -1);
43
44         if (pid > 0) {
45                 task.pid = pid;
46                 uloop_process_add(&task);
47                 return;
48         }
49
50         char *eventnames[] = {"ifdown", "ifup", "ifupdate"};
51         setenv("ACTION", eventnames[event], 1);
52         setenv("INTERFACE", ifname, 1);
53         if (device)
54                 setenv("DEVICE", device, 1);
55         argv[0] = hotplug_cmd_path;
56         argv[1] = "iface";
57         argv[2] = NULL;
58         execvp(argv[0], argv);
59         exit(127);
60 }
61
62 static void
63 call_hotplug(void)
64 {
65         const char *device = NULL;
66         if (list_empty(&pending))
67                 return;
68
69         current = list_first_entry(&pending, struct interface, hotplug_list);
70         current_ev = current->hotplug_ev;
71         list_del_init(&current->hotplug_list);
72
73         if (current_ev == IFEV_UP && current->l3_dev.dev)
74                 device = current->l3_dev.dev->ifname;
75
76         D(SYSTEM, "Call hotplug handler for interface '%s' (%s)\n", current->name, device ? device : "none");
77         run_cmd(current->name, device, current_ev);
78 }
79
80 static void
81 task_complete(struct uloop_process *proc, int ret)
82 {
83         if (current)
84                 D(SYSTEM, "Complete hotplug handler for interface '%s'\n", current->name);
85         current = NULL;
86         call_hotplug();
87 }
88
89 /*
90  * Queue an interface for an up/down event.
91  * An interface can only have one event in the queue and one
92  * event waiting for completion.
93  * When queueing an event that is the same as the one waiting for
94  * completion, remove the interface from the queue
95  */
96 static void
97 interface_queue_event(struct interface *iface, enum interface_event ev)
98 {
99         enum interface_event last_ev;
100
101         D(SYSTEM, "Queue hotplug handler for interface '%s'\n", iface->name);
102         if (ev == IFEV_UP || ev == IFEV_DOWN)
103                 netifd_ubus_interface_event(iface, ev == IFEV_UP);
104
105         netifd_ubus_interface_notify(iface, ev != IFEV_DOWN);
106
107         if (current == iface)
108                 last_ev = current_ev;
109         else
110                 last_ev = iface->hotplug_ev;
111
112         iface->hotplug_ev = ev;
113         if ((last_ev == ev && ev != IFEV_UPDATE) && !list_empty(&iface->hotplug_list))
114                 list_del_init(&iface->hotplug_list);
115         else if ((last_ev != ev || ev == IFEV_UPDATE) && list_empty(&iface->hotplug_list))
116                 list_add(&iface->hotplug_list, &pending);
117
118         if (!task.pending && !current)
119                 call_hotplug();
120 }
121
122 static void
123 interface_dequeue_event(struct interface *iface)
124 {
125         if (iface == current)
126                 current = NULL;
127
128         if (!list_empty(&iface->hotplug_list))
129                 list_del_init(&iface->hotplug_list);
130 }
131
132 static void interface_event_cb(struct interface_user *dep, struct interface *iface,
133                                enum interface_event ev)
134 {
135         switch (ev) {
136                 case IFEV_UP:
137                 case IFEV_UPDATE:
138                 case IFEV_DOWN:
139                         interface_queue_event(iface, ev);
140                         break;
141                 case IFEV_FREE:
142                 case IFEV_RELOAD:
143                         interface_dequeue_event(iface);
144                         break;
145         }
146 }
147
148 static struct interface_user event_user = {
149         .cb = interface_event_cb
150 };
151
152 static void __init interface_event_init(void)
153 {
154         interface_add_user(&event_user, NULL);
155 }