2 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
30 #include <linux/sockios.h>
33 #include "udev_rules.h"
36 struct udevice *udev_device_init(void)
40 udev = malloc(sizeof(struct udevice));
43 memset(udev, 0x00, sizeof(struct udevice));
45 INIT_LIST_HEAD(&udev->symlink_list);
46 INIT_LIST_HEAD(&udev->run_list);
47 INIT_LIST_HEAD(&udev->env_list);
49 /* set sysfs device to local storage, can be overridden if needed */
50 udev->dev = &udev->dev_local;
52 /* default node permissions */
54 strcpy(udev->owner, "root");
55 strcpy(udev->group, "root");
60 void udev_device_cleanup(struct udevice *udev)
62 name_list_cleanup(&udev->symlink_list);
63 name_list_cleanup(&udev->run_list);
64 name_list_cleanup(&udev->env_list);
68 dev_t udev_device_get_devt(struct udevice *udev)
71 unsigned int maj, min;
73 /* read it from sysfs */
74 attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
76 if (sscanf(attr, "%u:%u", &maj, &min) == 2)
77 return makedev(maj, min);
82 static int rename_netif(struct udevice *udev)
88 info("changing net interface name from '%s' to '%s'", udev->dev->kernel, udev->name);
92 sk = socket(PF_INET, SOCK_DGRAM, 0);
94 err("error opening socket: %s", strerror(errno));
98 memset(&ifr, 0x00, sizeof(struct ifreq));
99 strlcpy(ifr.ifr_name, udev->dev->kernel, IFNAMSIZ);
100 strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
101 retval = ioctl(sk, SIOCSIFNAME, &ifr);
105 /* see if the destination interface name already exists */
106 if (errno != EEXIST) {
107 err("error changing netif name %s to %s: %s", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
111 /* free our own name, another process may wait for us */
112 strlcpy(ifr.ifr_newname, udev->dev->kernel, IFNAMSIZ);
113 strlcat(ifr.ifr_newname, "_rename", IFNAMSIZ);
114 retval = ioctl(sk, SIOCSIFNAME, &ifr);
116 err("error changing netif name %s to %s: %s", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
120 /* wait 30 seconds for our target to become available */
121 strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ);
122 strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
125 retval = ioctl(sk, SIOCSIFNAME, &ifr);
129 if (errno != EEXIST) {
130 err("error changing net interface name %s to %s: %s",
131 ifr.ifr_name, ifr.ifr_newname, strerror(errno));
134 dbg("wait for netif '%s' to become free, loop=%i", udev->name, (30 * 20) - loop);
135 usleep(1000 * 1000 / 20);
144 int udev_device_event(struct udev_rules *rules, struct udevice *udev)
148 /* add device node */
149 if (major(udev->devt) != 0 &&
150 (strcmp(udev->action, "add") == 0 || strcmp(udev->action, "change") == 0)) {
151 struct udevice *udev_old;
153 dbg("device node add '%s'", udev->dev->devpath);
155 udev_rules_get_name(rules, udev);
156 if (udev->ignore_device) {
157 info("device event will be ignored");
160 if (udev->name[0] == '\0') {
161 info("device node creation supressed");
165 /* read current database entry, we may want to cleanup symlinks */
166 udev_old = udev_device_init();
167 if (udev_old != NULL) {
168 if (udev_db_get_device(udev_old, udev->dev->devpath) != 0) {
169 udev_device_cleanup(udev_old);
172 info("device '%s' already in database, validate currently present symlinks",
176 /* create node and symlinks */
177 retval = udev_node_add(udev, udev_old);
179 /* store record in database */
180 udev_db_add_device(udev);
182 /* remove possibly left-over symlinks */
183 if (udev_old != NULL) {
184 struct name_entry *link_loop;
185 struct name_entry *link_old_loop;
186 struct name_entry *link_old_tmp_loop;
188 /* remove still valid symlinks from old list */
189 list_for_each_entry_safe(link_old_loop, link_old_tmp_loop, &udev_old->symlink_list, node)
190 list_for_each_entry(link_loop, &udev->symlink_list, node)
191 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
192 dbg("symlink '%s' still valid, keep it", link_old_loop->name);
193 list_del(&link_old_loop->node);
196 udev_node_remove_symlinks(udev_old);
197 udev_device_cleanup(udev_old);
204 if (strcmp(udev->dev->subsystem, "net") == 0 && strcmp(udev->action, "add") == 0) {
205 dbg("netif add '%s'", udev->dev->devpath);
206 udev_rules_get_name(rules, udev);
207 if (udev->ignore_device) {
208 info("device event will be ignored");
212 /* look if we want to change the name of the netif */
213 if (strcmp(udev->name, udev->dev->kernel) != 0) {
216 retval = rename_netif(udev);
219 info("renamed netif to '%s'", udev->name);
221 /* export old name */
222 setenv("INTERFACE_OLD", udev->dev->kernel, 1);
224 /* now fake the devpath, because the kernel name changed silently */
225 pos = strrchr(udev->dev->devpath, '/');
228 strlcat(udev->dev->devpath, udev->name, sizeof(udev->dev->devpath));
229 strlcpy(udev->dev->kernel, udev->name, sizeof(udev->dev->kernel));
230 setenv("DEVPATH", udev->dev->devpath, 1);
231 setenv("INTERFACE", udev->name, 1);
237 /* remove device node */
238 if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
239 struct name_entry *name_loop;
241 /* import and delete database entry */
242 if (udev_db_get_device(udev, udev->dev->devpath) == 0) {
243 udev_db_delete_device(udev);
244 if (udev->ignore_remove) {
245 dbg("remove event for '%s' requested to be ignored by rule", udev->name);
248 /* restore stored persistent data */
249 list_for_each_entry(name_loop, &udev->env_list, node)
250 putenv(name_loop->name);
252 dbg("'%s' not found in database, using kernel name '%s'", udev->dev->devpath, udev->dev->kernel);
253 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
256 udev_rules_get_run(rules, udev);
257 if (udev->ignore_device) {
258 info("device event will be ignored");
262 retval = udev_node_remove(udev);
266 /* default devices */
267 udev_rules_get_run(rules, udev);
268 if (udev->ignore_device)
269 info("device event will be ignored");