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);
127 if (errno != EEXIST) {
128 err("error changing net interface name %s to %s: %s",
129 ifr.ifr_name, ifr.ifr_newname, strerror(errno));
132 dbg("wait for netif '%s' to become free, loop=%i", udev->name, (30 * 20) - loop);
133 usleep(1000 * 1000 / 20);
143 int udev_device_event(struct udev_rules *rules, struct udevice *udev)
147 /* add device node */
148 if (major(udev->devt) != 0 &&
149 (strcmp(udev->action, "add") == 0 || strcmp(udev->action, "change") == 0)) {
150 struct udevice *udev_old;
152 dbg("device node add '%s'", udev->dev->devpath);
154 udev_rules_get_name(rules, udev);
155 if (udev->ignore_device) {
156 info("device event will be ignored");
159 if (udev->name[0] == '\0') {
160 info("device node creation supressed");
164 /* read current database entry, we may want to cleanup symlinks */
165 udev_old = udev_device_init();
166 if (udev_old != NULL) {
167 if (udev_db_get_device(udev_old, udev->dev->devpath) != 0) {
168 udev_device_cleanup(udev_old);
171 info("device '%s' already in database, validate currently present symlinks",
175 /* create node and symlinks */
176 retval = udev_node_add(udev, udev_old);
178 /* store record in database */
179 udev_db_add_device(udev);
181 /* remove possibly left-over symlinks */
182 if (udev_old != NULL) {
183 struct name_entry *link_loop;
184 struct name_entry *link_old_loop;
185 struct name_entry *link_old_tmp_loop;
187 /* remove still valid symlinks from old list */
188 list_for_each_entry_safe(link_old_loop, link_old_tmp_loop, &udev_old->symlink_list, node)
189 list_for_each_entry(link_loop, &udev->symlink_list, node)
190 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
191 dbg("symlink '%s' still valid, keep it", link_old_loop->name);
192 list_del(&link_old_loop->node);
195 udev_node_remove_symlinks(udev_old);
196 udev_device_cleanup(udev_old);
203 if (strcmp(udev->dev->subsystem, "net") == 0 && strcmp(udev->action, "add") == 0) {
204 dbg("netif add '%s'", udev->dev->devpath);
205 udev_rules_get_name(rules, udev);
206 if (udev->ignore_device) {
207 info("device event will be ignored");
211 /* look if we want to change the name of the netif */
212 if (strcmp(udev->name, udev->dev->kernel) != 0) {
215 retval = rename_netif(udev);
218 info("renamed netif to '%s'", udev->name);
220 /* export old name */
221 setenv("INTERFACE_OLD", udev->dev->kernel, 1);
223 /* now fake the devpath, because the kernel name changed silently */
224 pos = strrchr(udev->dev->devpath, '/');
227 strlcat(udev->dev->devpath, udev->name, sizeof(udev->dev->devpath));
228 strlcpy(udev->dev->kernel, udev->name, sizeof(udev->dev->kernel));
229 setenv("DEVPATH", udev->dev->devpath, 1);
230 setenv("INTERFACE", udev->name, 1);
236 /* remove device node */
237 if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
238 struct name_entry *name_loop;
240 /* import and delete database entry */
241 if (udev_db_get_device(udev, udev->dev->devpath) == 0) {
242 udev_db_delete_device(udev);
243 if (udev->ignore_remove) {
244 dbg("remove event for '%s' requested to be ignored by rule", udev->name);
247 /* restore stored persistent data */
248 list_for_each_entry(name_loop, &udev->env_list, node)
249 putenv(name_loop->name);
251 dbg("'%s' not found in database, using kernel name '%s'", udev->dev->devpath, udev->dev->kernel);
252 strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
255 udev_rules_get_run(rules, udev);
256 if (udev->ignore_device) {
257 info("device event will be ignored");
261 retval = udev_node_remove(udev);
265 /* default devices */
266 udev_rules_get_run(rules, udev);
267 if (udev->ignore_device)
268 info("device event will be ignored");