utils.c: add missing include
[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 static const char * const eventnames[] = {"ifdown", "ifup", "ifupdate"};
34
35 static void
36 run_cmd(const char *ifname, const char *device, enum interface_event event,
37                 enum interface_update_flags updated)
38 {
39         char *argv[3];
40         int pid;
41
42         pid = fork();
43         if (pid < 0)
44                 return task_complete(NULL, -1);
45
46         if (pid > 0) {
47                 task.pid = pid;
48                 uloop_process_add(&task);
49                 return;
50         }
51
52         setenv("ACTION", eventnames[event], 1);
53         setenv("INTERFACE", ifname, 1);
54         if (device)
55                 setenv("DEVICE", device, 1);
56
57         if (event == IFEV_UPDATE) {
58                 if (updated & IUF_ADDRESS)
59                         setenv("IFUPDATE_ADDRESSES", "1", 1);
60                 if (updated & IUF_ROUTE)
61                         setenv("IFUPDATE_ROUTES", "1", 1);
62                 if (updated & IUF_PREFIX)
63                         setenv("IFUPDATE_PREFIXES", "1", 1);
64                 if (updated & IUF_DATA)
65                         setenv("IFUPDATE_DATA", "1", 1);
66         }
67
68         argv[0] = hotplug_cmd_path;
69         argv[1] = "iface";
70         argv[2] = NULL;
71         execvp(argv[0], argv);
72         exit(127);
73 }
74
75 static void
76 call_hotplug(void)
77 {
78         const char *device = NULL;
79         if (list_empty(&pending))
80                 return;
81
82         current = list_first_entry(&pending, struct interface, hotplug_list);
83         current_ev = current->hotplug_ev;
84         list_del_init(&current->hotplug_list);
85
86         if ((current_ev == IFEV_UP || current_ev == IFEV_UPDATE) && current->l3_dev.dev)
87                 device = current->l3_dev.dev->ifname;
88
89         D(SYSTEM, "Call hotplug handler for interface '%s', event '%s' (%s)\n",
90         current->name, eventnames[current_ev], device ? device : "none");
91         run_cmd(current->name, device, current_ev, current->updated);
92 }
93
94 static void
95 task_complete(struct uloop_process *proc, int ret)
96 {
97         if (current)
98                 D(SYSTEM, "Complete hotplug handler for interface '%s'\n", current->name);
99         current = NULL;
100         call_hotplug();
101 }
102
103 /*
104  * Queue an interface for an up/down event.
105  * An interface can only have one event in the queue and one
106  * event waiting for completion.
107  * When queueing an event that is the same as the one waiting for
108  * completion, remove the interface from the queue
109  */
110 static void
111 interface_queue_event(struct interface *iface, enum interface_event ev)
112 {
113         D(SYSTEM, "Queue hotplug handler for interface '%s', event '%s'\n",
114                         iface->name, eventnames[ev]);
115         if (ev == IFEV_UP || ev == IFEV_DOWN)
116                 netifd_ubus_interface_event(iface, ev == IFEV_UP);
117
118         netifd_ubus_interface_notify(iface, ev != IFEV_DOWN);
119
120         if (current == iface) {
121                 /* an event for iface is being processed */
122                 if (!list_empty(&iface->hotplug_list)) {
123                         /* an additional event for iface is pending */
124                         if ((ev != current_ev || ev == IFEV_UPDATE) &&
125                         !(iface->hotplug_ev == IFEV_UP && ev == IFEV_UPDATE)) {
126                                 /* if incoming event is different from the one
127                                  * being handled or if it is an update,
128                                  * overwrite pending event, but never
129                                  * overwrite an ifup with an ifupdate */
130                                 iface->hotplug_ev = ev;
131                         }
132                         else if (ev == current_ev && ev != IFEV_UPDATE) {
133                                 /* if incoming event is not an ifupdate
134                                  * and is the same as the one that is
135                                  * being handled, remove it from the
136                                  * pending list */
137                                 list_del_init(&iface->hotplug_list);
138                         }
139                 }
140                 else {
141                         /* no additional event for iface is pending */
142                         if (ev != current_ev || ev == IFEV_UPDATE) {
143                                 /* only add the interface to the pending list if
144                                  * the event is different from the one being
145                                  * handled or if it is an update */
146                                 iface->hotplug_ev = ev;
147                                 /* Handle hotplug calls FIFO */
148                                 list_add_tail(&iface->hotplug_list, &pending);
149                         }
150                 }
151         }
152         else {
153                 /* currently not handling an event or handling an event
154                  * for another interface */
155                 if (!list_empty(&iface->hotplug_list)) {
156                         /* an event for iface is pending */
157                         if (!(iface->hotplug_ev == IFEV_UP &&
158                                 ev == IFEV_UPDATE)) {
159                                 /* overwrite pending event, unless the incoming
160                                  * event is an ifupdate while the pending one
161                                  * is an ifup */
162                                 iface->hotplug_ev = ev;
163                         }
164                 }
165                 else {
166                         /* an event for the interface is not yet pending,
167                          * queue it */
168                         iface->hotplug_ev = ev;
169                         /* Handle hotplug calls FIFO */
170                         list_add_tail(&iface->hotplug_list, &pending);
171                 }
172         }
173
174         if (!task.pending && !current)
175                 call_hotplug();
176 }
177
178 static void
179 interface_dequeue_event(struct interface *iface)
180 {
181         if (iface == current)
182                 current = NULL;
183
184         if (!list_empty(&iface->hotplug_list))
185                 list_del_init(&iface->hotplug_list);
186 }
187
188 static void interface_event_cb(struct interface_user *dep, struct interface *iface,
189                                enum interface_event ev)
190 {
191         switch (ev) {
192                 case IFEV_UP:
193                 case IFEV_UPDATE:
194                 case IFEV_DOWN:
195                         interface_queue_event(iface, ev);
196                         break;
197                 case IFEV_FREE:
198                 case IFEV_RELOAD:
199                         interface_dequeue_event(iface);
200                         break;
201         }
202 }
203
204 static struct interface_user event_user = {
205         .cb = interface_event_cb
206 };
207
208 static void __init interface_event_init(void)
209 {
210         interface_add_user(&event_user, NULL);
211 }