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