Add indicator-flags to ubus and hotplug update-events
[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
34 static void
35 run_cmd(const char *ifname, const char *device, enum interface_event event,
36                 enum interface_update_flags updated)
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
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->l3_dev.dev)
87                 device = current->l3_dev.dev->ifname;
88
89         D(SYSTEM, "Call hotplug handler for interface '%s' (%s)\n", current->name, device ? device : "none");
90         run_cmd(current->name, device, current_ev, current->updated);
91 }
92
93 static void
94 task_complete(struct uloop_process *proc, int ret)
95 {
96         if (current)
97                 D(SYSTEM, "Complete hotplug handler for interface '%s'\n", current->name);
98         current = NULL;
99         call_hotplug();
100 }
101
102 /*
103  * Queue an interface for an up/down event.
104  * An interface can only have one event in the queue and one
105  * event waiting for completion.
106  * When queueing an event that is the same as the one waiting for
107  * completion, remove the interface from the queue
108  */
109 static void
110 interface_queue_event(struct interface *iface, enum interface_event ev)
111 {
112         enum interface_event last_ev;
113
114         D(SYSTEM, "Queue hotplug handler for interface '%s'\n", iface->name);
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                 last_ev = current_ev;
122         else
123                 last_ev = iface->hotplug_ev;
124
125         iface->hotplug_ev = ev;
126         if ((last_ev == ev && ev != IFEV_UPDATE) && !list_empty(&iface->hotplug_list))
127                 list_del_init(&iface->hotplug_list);
128         else if ((last_ev != ev || ev == IFEV_UPDATE) && list_empty(&iface->hotplug_list))
129                 list_add(&iface->hotplug_list, &pending);
130
131         if (!task.pending && !current)
132                 call_hotplug();
133 }
134
135 static void
136 interface_dequeue_event(struct interface *iface)
137 {
138         if (iface == current)
139                 current = NULL;
140
141         if (!list_empty(&iface->hotplug_list))
142                 list_del_init(&iface->hotplug_list);
143 }
144
145 static void interface_event_cb(struct interface_user *dep, struct interface *iface,
146                                enum interface_event ev)
147 {
148         switch (ev) {
149                 case IFEV_UP:
150                 case IFEV_UPDATE:
151                 case IFEV_DOWN:
152                         interface_queue_event(iface, ev);
153                         break;
154                 case IFEV_FREE:
155                 case IFEV_RELOAD:
156                         interface_dequeue_event(iface);
157                         break;
158         }
159 }
160
161 static struct interface_user event_user = {
162         .cb = interface_event_cb
163 };
164
165 static void __init interface_event_init(void)
166 {
167         interface_add_user(&event_user, NULL);
168 }