• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt