chiark / gitweb /
rename config "filename" to "dir"
[elogind.git] / udev_device.c
1 /*
2  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
3  *
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.
7  * 
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.
12  * 
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.
16  *
17  */
18
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <net/if.h>
30 #include <linux/sockios.h>
31
32 #include "udev.h"
33 #include "udev_rules.h"
34
35
36 struct udevice *udev_device_init(void)
37 {
38         struct udevice *udev;
39
40         udev = malloc(sizeof(struct udevice));
41         if (udev == NULL)
42                 return NULL;
43         memset(udev, 0x00, sizeof(struct udevice));
44
45         INIT_LIST_HEAD(&udev->symlink_list);
46         INIT_LIST_HEAD(&udev->run_list);
47         INIT_LIST_HEAD(&udev->env_list);
48
49         /* set sysfs device to local storage, can be overridden if needed */
50         udev->dev = &udev->dev_local;
51
52         /* default node permissions */
53         udev->mode = 0660;
54         strcpy(udev->owner, "root");
55         strcpy(udev->group, "root");
56
57         return udev;
58 }
59
60 void udev_device_cleanup(struct udevice *udev)
61 {
62         name_list_cleanup(&udev->symlink_list);
63         name_list_cleanup(&udev->run_list);
64         name_list_cleanup(&udev->env_list);
65         free(udev);
66 }
67
68 dev_t udev_device_get_devt(struct udevice *udev)
69 {
70         const char *attr;
71         unsigned int maj, min;
72
73         /* read it from sysfs  */
74         attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
75         if (attr != NULL) {
76                 if (sscanf(attr, "%u:%u", &maj, &min) == 2)
77                         return makedev(maj, min);
78         }
79         return makedev(0, 0);
80 }
81
82 static int rename_netif(struct udevice *udev)
83 {
84         int sk;
85         struct ifreq ifr;
86         int retval;
87
88         info("changing net interface name from '%s' to '%s'", udev->dev->kernel, udev->name);
89         if (udev->test_run)
90                 return 0;
91
92         sk = socket(PF_INET, SOCK_DGRAM, 0);
93         if (sk < 0) {
94                 err("error opening socket: %s", strerror(errno));
95                 return -1;
96         }
97
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);
102         if (retval != 0) {
103                 int loop;
104
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));
108                         goto exit;
109                 }
110
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);
115                 if (retval != 0) {
116                         err("error changing netif name %s to %s: %s", ifr.ifr_name, ifr.ifr_newname, strerror(errno));
117                         goto exit;
118                 }
119
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);
123                 loop = 30 * 20;
124                 while (loop--) {
125                         retval = ioctl(sk, SIOCSIFNAME, &ifr);  
126                         if (retval != 0) {
127                                 if (errno != EEXIST) {
128                                         err("error changing net interface name %s to %s: %s",
129                                             ifr.ifr_name, ifr.ifr_newname, strerror(errno));
130                                         break;
131                                 }
132                                 dbg("wait for netif '%s' to become free, loop=%i", udev->name, (30 * 20) - loop);
133                                 usleep(1000 * 1000 / 20);
134                         }
135                 }
136         }
137
138 exit:
139         close(sk);
140         return retval;
141 }
142
143 int udev_device_event(struct udev_rules *rules, struct udevice *udev)
144 {
145         int retval = 0;
146
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;
151
152                 dbg("device node add '%s'", udev->dev->devpath);
153
154                 udev_rules_get_name(rules, udev);
155                 if (udev->ignore_device) {
156                         info("device event will be ignored");
157                         goto exit;
158                 }
159                 if (udev->name[0] == '\0') {
160                         info("device node creation supressed");
161                         goto exit;
162                 }
163
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);
169                                 udev_old = NULL;
170                         } else
171                                 info("device '%s' already in database, validate currently present symlinks",
172                                      udev->dev->devpath);
173                 }
174
175                 /* create node and symlinks */
176                 retval = udev_node_add(udev, udev_old);
177                 if (retval == 0) {
178                         /* store record in database */
179                         udev_db_add_device(udev);
180
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;
186
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);
193                                                         free(link_old_loop);
194                                                 }
195                                 udev_node_remove_symlinks(udev_old);
196                                 udev_device_cleanup(udev_old);
197                         }
198                 }
199                 goto exit;
200         }
201
202         /* add netif */
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");
208                         goto exit;
209                 }
210
211                 /* look if we want to change the name of the netif */
212                 if (strcmp(udev->name, udev->dev->kernel) != 0) {
213                         char *pos;
214
215                         retval = rename_netif(udev);
216                         if (retval != 0)
217                                 goto exit;
218                         info("renamed netif to '%s'", udev->name);
219
220                         /* export old name */
221                         setenv("INTERFACE_OLD", udev->dev->kernel, 1);
222
223                         /* now fake the devpath, because the kernel name changed silently */
224                         pos = strrchr(udev->dev->devpath, '/');
225                         if (pos != NULL) {
226                                 pos[1] = '\0';
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);
231                         }
232                 }
233                 goto exit;
234         }
235
236         /* remove device node */
237         if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
238                 struct name_entry *name_loop;
239
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);
245                                 return 0;
246                         }
247                         /* restore stored persistent data */
248                         list_for_each_entry(name_loop, &udev->env_list, node)
249                                 putenv(name_loop->name);
250                 } else {
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));
253                 }
254
255                 udev_rules_get_run(rules, udev);
256                 if (udev->ignore_device) {
257                         info("device event will be ignored");
258                         goto exit;
259                 }
260
261                 retval = udev_node_remove(udev);
262                 goto exit;
263         }
264
265         /* default devices */
266         udev_rules_get_run(rules, udev);
267         if (udev->ignore_device)
268                 info("device event will be ignored");
269
270 exit:
271         return retval;
272 }