]>
Commit | Line | Data |
---|---|---|
d46f37fd KS |
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 <string.h> | |
28 | #include <sys/ioctl.h> | |
29 | #include <sys/socket.h> | |
30 | #include <net/if.h> | |
31 | #include <linux/sockios.h> | |
32 | ||
33 | #include "udev.h" | |
34 | #include "udev_rules.h" | |
35 | ||
36 | ||
37 | static void kernel_log(struct ifreq ifr) | |
38 | { | |
39 | int klog; | |
40 | FILE *f; | |
41 | ||
42 | klog = open("/dev/kmsg", O_WRONLY); | |
43 | if (klog < 0) | |
44 | return; | |
45 | ||
46 | f = fdopen(klog, "w"); | |
47 | if (f == NULL) { | |
48 | close(klog); | |
49 | return; | |
50 | } | |
51 | ||
52 | fprintf(f, "<6>udev: renamed network interface %s to %s\n", | |
53 | ifr.ifr_name, ifr.ifr_newname); | |
54 | fclose(f); | |
55 | } | |
56 | ||
57 | static int rename_netif(struct udevice *udev) | |
58 | { | |
59 | int sk; | |
60 | struct ifreq ifr; | |
61 | int retval; | |
62 | ||
63 | info("changing net interface name from '%s' to '%s'\n", udev->dev->kernel, udev->name); | |
64 | if (udev->test_run) | |
65 | return 0; | |
66 | ||
67 | sk = socket(PF_INET, SOCK_DGRAM, 0); | |
68 | if (sk < 0) { | |
69 | err("error opening socket: %s\n", strerror(errno)); | |
70 | return -1; | |
71 | } | |
72 | ||
73 | memset(&ifr, 0x00, sizeof(struct ifreq)); | |
74 | strlcpy(ifr.ifr_name, udev->dev->kernel, IFNAMSIZ); | |
75 | strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ); | |
76 | retval = ioctl(sk, SIOCSIFNAME, &ifr); | |
77 | if (retval == 0) | |
78 | kernel_log(ifr); | |
79 | else { | |
80 | int loop; | |
81 | ||
82 | /* see if the destination interface name already exists */ | |
83 | if (errno != EEXIST) { | |
84 | err("error changing netif name %s to %s: %s\n", ifr.ifr_name, ifr.ifr_newname, strerror(errno)); | |
85 | goto exit; | |
86 | } | |
87 | ||
88 | /* free our own name, another process may wait for us */ | |
89 | strlcpy(ifr.ifr_newname, udev->dev->kernel, IFNAMSIZ); | |
90 | strlcat(ifr.ifr_newname, "_rename", IFNAMSIZ); | |
91 | retval = ioctl(sk, SIOCSIFNAME, &ifr); | |
92 | if (retval != 0) { | |
93 | err("error changing netif name %s to %s: %s\n", ifr.ifr_name, ifr.ifr_newname, strerror(errno)); | |
94 | goto exit; | |
95 | } | |
96 | ||
97 | /* wait 30 seconds for our target to become available */ | |
98 | strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ); | |
99 | strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ); | |
100 | loop = 30 * 20; | |
101 | while (loop--) { | |
102 | retval = ioctl(sk, SIOCSIFNAME, &ifr); | |
103 | if (retval == 0) { | |
104 | kernel_log(ifr); | |
105 | break; | |
106 | } | |
107 | ||
108 | if (errno != EEXIST) { | |
109 | err("error changing net interface name %s to %s: %s\n", | |
110 | ifr.ifr_name, ifr.ifr_newname, strerror(errno)); | |
111 | break; | |
112 | } | |
113 | dbg("wait for netif '%s' to become free, loop=%i\n", udev->name, (30 * 20) - loop); | |
114 | usleep(1000 * 1000 / 20); | |
115 | } | |
116 | } | |
117 | ||
118 | exit: | |
119 | close(sk); | |
120 | return retval; | |
121 | } | |
122 | ||
123 | int udev_device_event(struct udev_rules *rules, struct udevice *udev) | |
124 | { | |
125 | int retval = 0; | |
126 | ||
127 | if (udev->devpath_old != NULL) | |
128 | if (udev_db_rename(udev->devpath_old, udev->dev->devpath) == 0) | |
129 | info("moved database from '%s' to '%s'\n", udev->devpath_old, udev->dev->devpath); | |
130 | ||
131 | /* add device node */ | |
132 | if (major(udev->devt) != 0 && | |
133 | (strcmp(udev->action, "add") == 0 || strcmp(udev->action, "change") == 0)) { | |
134 | struct udevice *udev_old; | |
135 | ||
136 | dbg("device node add '%s'\n", udev->dev->devpath); | |
137 | ||
138 | udev_rules_get_name(rules, udev); | |
139 | if (udev->ignore_device) { | |
140 | info("device event will be ignored\n"); | |
141 | goto exit; | |
142 | } | |
143 | if (udev->name[0] == '\0') { | |
144 | info("device node creation supressed\n"); | |
145 | goto exit; | |
146 | } | |
147 | ||
148 | /* read current database entry; cleanup, if it is known device */ | |
149 | udev_old = udev_device_init(NULL); | |
150 | if (udev_old != NULL) { | |
151 | udev_old->test_run = udev->test_run; | |
152 | if (udev_db_get_device(udev_old, udev->dev->devpath) == 0) { | |
153 | info("device '%s' already in database, cleanup\n", udev->dev->devpath); | |
154 | udev_db_delete_device(udev_old); | |
155 | } else { | |
156 | udev_device_cleanup(udev_old); | |
157 | udev_old = NULL; | |
158 | } | |
159 | } | |
160 | ||
161 | /* create node */ | |
162 | retval = udev_node_add(udev); | |
163 | if (retval != 0) | |
164 | goto exit; | |
165 | ||
166 | /* store in database */ | |
167 | udev_db_add_device(udev); | |
168 | ||
169 | /* create, replace, delete symlinks according to priority */ | |
170 | udev_node_update_symlinks(udev, udev_old); | |
171 | ||
172 | if (udev_old != NULL) | |
173 | udev_device_cleanup(udev_old); | |
174 | goto exit; | |
175 | } | |
176 | ||
177 | /* add netif */ | |
178 | if (strcmp(udev->dev->subsystem, "net") == 0 && strcmp(udev->action, "add") == 0) { | |
179 | dbg("netif add '%s'\n", udev->dev->devpath); | |
180 | udev_rules_get_name(rules, udev); | |
181 | if (udev->ignore_device) { | |
182 | info("device event will be ignored\n"); | |
183 | goto exit; | |
184 | } | |
185 | if (udev->name[0] == '\0') { | |
186 | info("device renaming supressed\n"); | |
187 | goto exit; | |
188 | } | |
189 | ||
190 | /* look if we want to change the name of the netif */ | |
191 | if (strcmp(udev->name, udev->dev->kernel) != 0) { | |
192 | char devpath[PATH_MAX]; | |
193 | char *pos; | |
194 | ||
195 | retval = rename_netif(udev); | |
196 | if (retval != 0) | |
197 | goto exit; | |
198 | info("renamed netif to '%s'\n", udev->name); | |
199 | ||
200 | /* export old name */ | |
201 | setenv("INTERFACE_OLD", udev->dev->kernel, 1); | |
202 | ||
203 | /* now change the devpath, because the kernel device name has changed */ | |
204 | strlcpy(devpath, udev->dev->devpath, sizeof(devpath)); | |
205 | pos = strrchr(devpath, '/'); | |
206 | if (pos != NULL) { | |
207 | pos[1] = '\0'; | |
208 | strlcat(devpath, udev->name, sizeof(devpath)); | |
209 | sysfs_device_set_values(udev->dev, devpath, NULL, NULL); | |
210 | setenv("DEVPATH", udev->dev->devpath, 1); | |
211 | setenv("INTERFACE", udev->name, 1); | |
212 | info("changed devpath to '%s'\n", udev->dev->devpath); | |
213 | } | |
214 | } | |
215 | goto exit; | |
216 | } | |
217 | ||
218 | /* remove device node */ | |
219 | if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) { | |
220 | struct name_entry *name_loop; | |
221 | ||
222 | /* import database entry, and delete it */ | |
223 | if (udev_db_get_device(udev, udev->dev->devpath) == 0) { | |
224 | udev_db_delete_device(udev); | |
225 | /* restore stored persistent data */ | |
226 | list_for_each_entry(name_loop, &udev->env_list, node) | |
227 | putenv(name_loop->name); | |
228 | } else { | |
229 | dbg("'%s' not found in database, using kernel name '%s'\n", | |
230 | udev->dev->devpath, udev->dev->kernel); | |
231 | strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name)); | |
232 | } | |
233 | ||
234 | udev_rules_get_run(rules, udev); | |
235 | if (udev->ignore_device) { | |
236 | info("device event will be ignored\n"); | |
237 | goto exit; | |
238 | } | |
239 | ||
240 | if (udev->ignore_remove) { | |
241 | info("ignore_remove for '%s'\n", udev->name); | |
242 | goto exit; | |
243 | } | |
244 | /* remove the node */ | |
245 | retval = udev_node_remove(udev); | |
246 | ||
247 | /* delete or restore symlinks according to priority */ | |
248 | udev_node_update_symlinks(udev, NULL); | |
249 | goto exit; | |
250 | } | |
251 | ||
252 | /* default devices */ | |
253 | udev_rules_get_run(rules, udev); | |
254 | if (udev->ignore_device) | |
255 | info("device event will be ignored\n"); | |
256 | ||
257 | exit: | |
258 | return retval; | |
259 | } |