fix error checking of asprintf
[project/netifd.git] / alias.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 <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include "netifd.h"
19 #include "device.h"
20 #include "interface.h"
21
22 static struct avl_tree aliases;
23
24 struct alias_device {
25         struct avl_node avl;
26         struct device dev;
27         struct device_user dep;
28         bool cleanup;
29         char name[];
30 };
31
32 static const struct device_type alias_device_type;
33
34 static int
35 alias_device_set_state(struct device *dev, bool state)
36 {
37         struct alias_device *alias;
38
39         alias = container_of(dev, struct alias_device, dev);
40         if (!alias->dep.dev)
41                 return -1;
42
43         if (state)
44                 return device_claim(&alias->dep);
45
46         device_release(&alias->dep);
47         if (alias->cleanup)
48                 device_remove_user(&alias->dep);
49         return 0;
50 }
51
52 static void alias_device_cb(struct device_user *dep, enum device_event ev)
53 {
54         struct alias_device *alias;
55         bool present = false;
56
57         alias = container_of(dep, struct alias_device, dep);
58         switch (ev) {
59         case DEV_EVENT_ADD:
60                 present = true;
61         case DEV_EVENT_REMOVE:
62                 device_set_present(&alias->dev, present);
63                 break;
64         default:
65                 device_broadcast_event(&alias->dev, ev);
66                 break;
67         }
68 }
69
70 static struct device *
71 alias_device_create(const char *name, struct blob_attr *attr)
72 {
73         struct alias_device *alias;
74
75         alias = calloc(1, sizeof(*alias) + strlen(name) + 1);
76         strcpy(alias->name, name);
77         alias->dev.set_state = alias_device_set_state;
78         alias->dev.hidden = true;
79         device_init_virtual(&alias->dev, &alias_device_type, NULL);
80         alias->avl.key = alias->name;
81         avl_insert(&aliases, &alias->avl);
82         alias->dep.alias = true;
83         alias->dep.cb = alias_device_cb;
84         device_check_state(&alias->dev);
85
86         return &alias->dev;
87 }
88
89 static void alias_device_free(struct device *dev)
90 {
91         struct alias_device *alias;
92
93         alias = container_of(dev, struct alias_device, dev);
94         avl_delete(&aliases, &alias->avl);
95         free(alias);
96 }
97
98 static void __alias_notify_device(struct alias_device *alias, struct device *dev)
99 {
100         alias->cleanup = !dev;
101         if (dev) {
102                 if (dev != alias->dep.dev) {
103                         device_remove_user(&alias->dep);
104                         strcpy(alias->dev.ifname, dev->ifname);
105                         device_add_user(&alias->dep, dev);
106                         alias->dev.hidden = false;
107                         device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
108                 }
109         }
110
111         if (!dev && alias->dep.dev && !alias->dep.dev->active) {
112                 device_remove_user(&alias->dep);
113                 alias->dev.hidden = true;
114                 alias->dev.ifname[0] = 0;
115                 device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
116         }
117 }
118
119 static int alias_check_state(struct device *dev)
120 {
121         struct alias_device *alias;
122         struct interface *iface;
123         struct device *ndev = NULL;
124
125         alias = container_of(dev, struct alias_device, dev);
126
127         iface = vlist_find(&interfaces, alias->name, iface, node);
128         if (iface && iface->state == IFS_UP)
129                 ndev = iface->main_dev.dev;
130
131         __alias_notify_device(alias, ndev);
132
133         return 0;
134 }
135
136 static const struct device_type alias_device_type = {
137         .name = "Network alias",
138         .create = alias_device_create,
139         .free = alias_device_free,
140         .check_state = alias_check_state,
141 };
142
143 void
144 alias_notify_device(const char *name, struct device *dev)
145 {
146         struct alias_device *alias;
147
148         device_lock();
149
150         alias = avl_find_element(&aliases, name, alias, avl);
151         if (alias)
152                 __alias_notify_device(alias, dev);
153
154         device_unlock();
155 }
156
157 struct device *
158 device_alias_get(const char *name)
159 {
160         struct alias_device *alias;
161
162         alias = avl_find_element(&aliases, name, alias, avl);
163         if (alias)
164                 return &alias->dev;
165
166         return alias_device_create(name, NULL);
167 }
168
169 static void __init alias_init(void)
170 {
171         avl_init(&aliases, avl_strcmp, false, NULL);
172 }