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