]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_device.c
encode db-file names, instead of just replacing '/'
[thirdparty/systemd.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 break;
128
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 exit:
140 close(sk);
141 return retval;
142 }
143
144 int udev_device_event(struct udev_rules *rules, struct udevice *udev)
145 {
146 int retval = 0;
147
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;
152
153 dbg("device node add '%s'", udev->dev->devpath);
154
155 udev_rules_get_name(rules, udev);
156 if (udev->ignore_device) {
157 info("device event will be ignored");
158 goto exit;
159 }
160 if (udev->name[0] == '\0') {
161 info("device node creation supressed");
162 goto exit;
163 }
164
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);
170 udev_old = NULL;
171 } else
172 info("device '%s' already in database, validate currently present symlinks",
173 udev->dev->devpath);
174 }
175
176 /* create node and symlinks */
177 retval = udev_node_add(udev, udev_old);
178 if (retval == 0) {
179 /* store record in database */
180 udev_db_add_device(udev);
181
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;
187
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);
194 free(link_old_loop);
195 }
196 udev_node_remove_symlinks(udev_old);
197 udev_device_cleanup(udev_old);
198 }
199 }
200 goto exit;
201 }
202
203 /* add netif */
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");
209 goto exit;
210 }
211
212 /* look if we want to change the name of the netif */
213 if (strcmp(udev->name, udev->dev->kernel) != 0) {
214 char *pos;
215
216 retval = rename_netif(udev);
217 if (retval != 0)
218 goto exit;
219 info("renamed netif to '%s'", udev->name);
220
221 /* export old name */
222 setenv("INTERFACE_OLD", udev->dev->kernel, 1);
223
224 /* now fake the devpath, because the kernel name changed silently */
225 pos = strrchr(udev->dev->devpath, '/');
226 if (pos != NULL) {
227 pos[1] = '\0';
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);
232 }
233 }
234 goto exit;
235 }
236
237 /* remove device node */
238 if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
239 struct name_entry *name_loop;
240
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);
246 return 0;
247 }
248 /* restore stored persistent data */
249 list_for_each_entry(name_loop, &udev->env_list, node)
250 putenv(name_loop->name);
251 } else {
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));
254 }
255
256 udev_rules_get_run(rules, udev);
257 if (udev->ignore_device) {
258 info("device event will be ignored");
259 goto exit;
260 }
261
262 retval = udev_node_remove(udev);
263 goto exit;
264 }
265
266 /* default devices */
267 udev_rules_get_run(rules, udev);
268 if (udev->ignore_device)
269 info("device event will be ignored");
270
271 exit:
272 return retval;
273 }