2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
19 #include <libubox/uloop.h>
22 #include "interface.h"
25 char *hotplug_cmd_path
= DEFAULT_HOTPLUG_PATH
;
26 static struct interface
*current
;
27 static enum interface_event current_ev
;
28 static struct list_head pending
= LIST_HEAD_INIT(pending
);
30 static void task_complete(struct uloop_process
*proc
, int ret
);
31 static struct uloop_process task
= {
36 run_cmd(const char *ifname
, const char *device
, enum interface_event event
)
43 return task_complete(NULL
, -1);
47 uloop_process_add(&task
);
51 char *eventnames
[] = {"ifdown", "ifup", "ifupdate"};
52 setenv("ACTION", eventnames
[event
], 1);
53 setenv("INTERFACE", ifname
, 1);
55 setenv("DEVICE", device
, 1);
56 argv
[0] = hotplug_cmd_path
;
59 execvp(argv
[0], argv
);
66 const char *device
= NULL
;
67 if (list_empty(&pending
))
70 current
= list_first_entry(&pending
, struct interface
, hotplug_list
);
71 current_ev
= current
->hotplug_ev
;
72 list_del_init(¤t
->hotplug_list
);
74 if (current_ev
== IFEV_UP
&& current
->l3_dev
.dev
)
75 device
= current
->l3_dev
.dev
->ifname
;
77 D(SYSTEM
, "Call hotplug handler for interface '%s' (%s)\n", current
->name
, device
? device
: "none");
78 run_cmd(current
->name
, device
, current_ev
);
82 task_complete(struct uloop_process
*proc
, int ret
)
85 D(SYSTEM
, "Complete hotplug handler for interface '%s'\n", current
->name
);
91 * Queue an interface for an up/down event.
92 * An interface can only have one event in the queue and one
93 * event waiting for completion.
94 * When queueing an event that is the same as the one waiting for
95 * completion, remove the interface from the queue
98 interface_queue_event(struct interface
*iface
, enum interface_event ev
)
100 enum interface_event last_ev
;
102 D(SYSTEM
, "Queue hotplug handler for interface '%s'\n", iface
->name
);
103 if (ev
== IFEV_UP
|| ev
== IFEV_DOWN
)
104 netifd_ubus_interface_event(iface
, ev
== IFEV_UP
);
106 netifd_ubus_interface_notify(iface
, ev
!= IFEV_DOWN
);
108 if (current
== iface
)
109 last_ev
= current_ev
;
111 last_ev
= iface
->hotplug_ev
;
113 iface
->hotplug_ev
= ev
;
114 if ((last_ev
== ev
&& ev
!= IFEV_UPDATE
) && !list_empty(&iface
->hotplug_list
))
115 list_del_init(&iface
->hotplug_list
);
116 else if ((last_ev
!= ev
|| ev
== IFEV_UPDATE
) && list_empty(&iface
->hotplug_list
))
117 list_add(&iface
->hotplug_list
, &pending
);
119 if (!task
.pending
&& !current
)
124 interface_dequeue_event(struct interface
*iface
)
126 if (iface
== current
)
129 if (!list_empty(&iface
->hotplug_list
))
130 list_del_init(&iface
->hotplug_list
);
133 static void interface_event_cb(struct interface_user
*dep
, struct interface
*iface
,
134 enum interface_event ev
)
140 interface_queue_event(iface
, ev
);
144 interface_dequeue_event(iface
);
149 static struct interface_user event_user
= {
150 .cb
= interface_event_cb
153 static void __init
interface_event_init(void)
155 interface_add_user(&event_user
, NULL
);