chiark / gitweb /
consistent key naming to match only the event device or include all parent devices
[elogind.git] / udev_device.c
1 /*
2  * udev_device.c - main udev data object
3  *
4  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <net/if.h>
32 #include <linux/sockios.h>
33
34 #include "udev.h"
35 #include "udev_rules.h"
36
37
38 struct udevice *udev_device_init(void)
39 {
40         struct udevice *udev;
41
42         udev = malloc(sizeof(struct udevice));
43         if (udev == NULL)
44                 return NULL;
45         memset(udev, 0x00, sizeof(struct udevice));
46
47         INIT_LIST_HEAD(&udev->symlink_list);
48         INIT_LIST_HEAD(&udev->run_list);
49         INIT_LIST_HEAD(&udev->env_list);
50
51         /* set sysfs device to local storage, can be overridden if needed */
52         udev->dev = &udev->dev_local;
53
54         /* default node permissions */
55         udev->mode = 0660;
56         strcpy(udev->owner, "root");
57         strcpy(udev->group, "root");
58
59         return udev;
60 }
61
62 void udev_device_cleanup(struct udevice *udev)
63 {
64         name_list_cleanup(&udev->symlink_list);
65         name_list_cleanup(&udev->run_list);
66         name_list_cleanup(&udev->env_list);
67         free(udev);
68 }
69
70 dev_t udev_device_get_devt(struct udevice *udev)
71 {
72         const char *attr;
73         unsigned int major, minor;
74
75         /* read it from sysfs  */
76         attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
77         if (attr != NULL) {
78                 if (sscanf(attr, "%u:%u", &major, &minor) == 2)
79                         return makedev(major, minor);
80         }
81         return makedev(0, 0);
82 }
83
84 static int rename_netif(struct udevice *udev)
85 {
86         int sk;
87         struct ifreq ifr;
88         int retval;
89
90         info("changing net interface name from '%s' to '%s'", udev->dev->kernel, udev->name);
91         if (udev->test_run)
92                 return 0;
93
94         sk = socket(PF_INET, SOCK_DGRAM, 0);
95         if (sk < 0) {
96                 err("error opening socket: %s", strerror(errno));
97                 return -1;
98         }
99
100         memset(&ifr, 0x00, sizeof(struct ifreq));
101         strlcpy(ifr.ifr_name, udev->dev->kernel, IFNAMSIZ);
102         strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
103         retval = ioctl(sk, SIOCSIFNAME, &ifr);
104         if (retval != 0) {
105                 int loop;
106
107                 /* see if the destination interface name already exists */
108                 if (errno != EEXIST) {
109                         err("error changing netif name %s to %s: %s", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
110                         goto exit;
111                 }
112
113                 /* free our own name, another process may wait for us */
114                 strlcpy(ifr.ifr_newname, udev->dev->kernel, IFNAMSIZ);
115                 strlcat(ifr.ifr_newname, "_rename", IFNAMSIZ);
116                 retval = ioctl(sk, SIOCSIFNAME, &ifr);
117                 if (retval != 0) {
118                         err("error changing netif name %s to %s: %s", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
119                         goto exit;
120                 }
121
122                 /* wait 30 seconds for our target to become available */
123                 strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ);
124                 strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
125                 loop = 30 * 20;
126                 while (loop--) {
127                         retval = ioctl(sk, SIOCSIFNAME, &ifr);  
128                         if (retval != 0) {
129                                 if (errno != EEXIST) {
130                                         err("error changing net interface name %s to %s: %s",
131                                             ifr.ifr_name, ifr.ifr_newname, strerror(errno));
132                                         break;
133                                 }
134                                 dbg("wait for netif '%s' to become free, loop=%i", udev->name, (30 * 20) - loop);
135                                 usleep(1000 * 1000 / 20);
136                         }
137                 }
138         }
139
140 exit:
141         close(sk);
142         return retval;
143 }
144
145 int udev_device_event(struct udev_rules *rules, struct udevice *udev)
146 {
147         int retval = 0;
148
149         /* add device node */
150         if (major(udev->devt) != 0 &&
151             (strcmp(udev->action, "add") == 0 || strcmp(udev->action, "change") == 0)) {
152                 struct udevice *udev_old;
153
154                 dbg("device node add '%s'", udev->dev->devpath);
155
156                 udev_rules_get_name(rules, udev);
157                 if (udev->ignore_device) {
158                         info("device event will be ignored");
159                         goto exit;
160                 }
161                 if (udev->name[0] == '\0') {
162                         info("device node creation supressed");
163                         goto exit;
164                 }
165
166                 /* read current database entry, we may want to cleanup symlinks */
167                 udev_old = udev_device_init();
168                 if (udev_old != NULL) {
169                         if (udev_db_get_device(udev_old, udev->dev->devpath) != 0) {
170                                 udev_device_cleanup(udev_old);
171                                 udev_old = NULL;
172                         } else
173                                 info("device '%s' already in database, validate currently present symlinks",
174                                      udev->dev->devpath);
175                 }
176
177                 /* create node and symlinks */
178                 retval = udev_node_add(udev, udev_old);
179                 if (retval == 0) {
180                         /* store record in database */
181                         udev_db_add_device(udev);
182
183                         /* remove possibly left-over symlinks */
184                         if (udev_old != NULL) {
185                                 struct name_entry *link_loop;
186                                 struct name_entry *link_old_loop;
187                                 struct name_entry *link_old_tmp_loop;
188
189                                 /* remove still valid symlinks from old list */
190                                 list_for_each_entry_safe(link_old_loop, link_old_tmp_loop, &udev_old->symlink_list, node)
191                                         list_for_each_entry(link_loop, &udev->symlink_list, node)
192                                                 if (strcmp(link_old_loop->name, link_loop->name) == 0) {
193                                                         dbg("symlink '%s' still valid, keep it", link_old_loop->name);
194                                                         list_del(&link_old_loop->node);
195                                                         free(link_old_loop);
196                                                 }
197                                 udev_node_remove_symlinks(udev_old);
198                                 udev_device_cleanup(udev_old);
199                         }
200                 }
201                 goto exit;
202         }
203
204         /* add netif */
205         if (strcmp(udev->dev->subsystem, "net") == 0 && strcmp(udev->action, "add") == 0) {
206                 dbg("netif add '%s'", udev->dev->devpath);
207                 udev_rules_get_name(rules, udev);
208                 if (udev->ignore_device) {
209                         info("device event will be ignored");
210                         goto exit;
211                 }
212
213                 /* look if we want to change the name of the netif */
214                 if (strcmp(udev->name, udev->dev->kernel) != 0) {
215                         char *pos;
216
217                         retval = rename_netif(udev);
218                         if (retval != 0)
219                                 goto exit;
220                         info("renamed netif to '%s'", udev->name);
221
222                         /* export old name */
223                         setenv("INTERFACE_OLD", udev->dev->kernel, 1);
224
225                         /* now fake the devpath, because the kernel name changed silently */
226                         pos = strrchr(udev->dev->devpath, '/');
227                         if (pos != NULL) {
228                                 pos[1] = '\0';
229                                 strlcat(udev->dev->devpath, udev->name, sizeof(udev->dev->devpath));
230                                 strlcpy(udev->dev->kernel, udev->name, sizeof(udev->dev->kernel));
231                                 setenv("DEVPATH", udev->dev->devpath, 1);
232                                 setenv("INTERFACE", udev->name, 1);
233                         }
234                 }
235                 goto exit;
236         }
237
238         /* remove device node */
239         if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
240                 struct name_entry *name_loop;
241
242                 /* import and delete database entry */
243                 if (udev_db_get_device(udev, udev->dev->devpath) == 0) {
244                         udev_db_delete_device(udev);
245                         if (udev->ignore_remove) {
246                                 dbg("remove event for '%s' requested to be ignored by rule", udev->name);
247                                 return 0;
248                         }
249                         /* restore stored persistent data */
250                         list_for_each_entry(name_loop, &udev->env_list, node)
251                                 putenv(name_loop->name);
252                 } else {
253                         dbg("'%s' not found in database, using kernel name '%s'", udev->dev->devpath, udev->dev->kernel);
254                         strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
255                 }
256
257                 udev_rules_get_run(rules, udev);
258                 if (udev->ignore_device) {
259                         info("device event will be ignored");
260                         goto exit;
261                 }
262
263                 retval = udev_node_remove(udev);
264                 goto exit;
265         }
266
267         /* default devices */
268         udev_rules_get_run(rules, udev);
269         if (udev->ignore_device)
270                 info("device event will be ignored");
271
272 exit:
273         return retval;
274 }