chiark / gitweb /
udevadm: settle - use libudev queue
[elogind.git] / udev / udev_device_event.c
1 /*
2  * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <net/if.h>
29 #include <linux/sockios.h>
30
31 #include "udev.h"
32 #include "udev_rules.h"
33
34 static void kernel_log(struct ifreq ifr)
35 {
36         int klog;
37         FILE *f;
38
39         klog = open("/dev/kmsg", O_WRONLY);
40         if (klog < 0)
41                 return;
42
43         f = fdopen(klog, "w");
44         if (f == NULL) {
45                 close(klog);
46                 return;
47         }
48
49         fprintf(f, "<6>udev: renamed network interface %s to %s\n",
50                 ifr.ifr_name, ifr.ifr_newname);
51         fclose(f);
52 }
53
54 static int rename_netif(struct udevice *udevice)
55 {
56         int sk;
57         struct ifreq ifr;
58         int retval;
59
60         info(udevice->udev, "changing net interface name from '%s' to '%s'\n", udevice->dev->kernel, udevice->name);
61         if (udevice->test_run)
62                 return 0;
63
64         sk = socket(PF_INET, SOCK_DGRAM, 0);
65         if (sk < 0) {
66                 err(udevice->udev, "error opening socket: %m\n");
67                 return -1;
68         }
69
70         memset(&ifr, 0x00, sizeof(struct ifreq));
71         util_strlcpy(ifr.ifr_name, udevice->dev->kernel, IFNAMSIZ);
72         util_strlcpy(ifr.ifr_newname, udevice->name, IFNAMSIZ);
73         retval = ioctl(sk, SIOCSIFNAME, &ifr);
74         if (retval == 0)
75                 kernel_log(ifr);
76         else {
77                 int loop;
78
79                 /* see if the destination interface name already exists */
80                 if (errno != EEXIST) {
81                         err(udevice->udev, "error changing netif name %s to %s: %m\n",
82                             ifr.ifr_name, ifr.ifr_newname);
83                         goto exit;
84                 }
85
86                 /* free our own name, another process may wait for us */
87                 util_strlcpy(ifr.ifr_newname, udevice->dev->kernel, IFNAMSIZ);
88                 util_strlcat(ifr.ifr_newname, "_rename", IFNAMSIZ);
89                 retval = ioctl(sk, SIOCSIFNAME, &ifr);
90                 if (retval != 0) {
91                         err(udevice->udev, "error changing netif name %s to %s: %m\n",
92                             ifr.ifr_name, ifr.ifr_newname);
93                         goto exit;
94                 }
95
96                 /* wait 30 seconds for our target to become available */
97                 util_strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ);
98                 util_strlcpy(ifr.ifr_newname, udevice->name, IFNAMSIZ);
99                 loop = 30 * 20;
100                 while (loop--) {
101                         retval = ioctl(sk, SIOCSIFNAME, &ifr);
102                         if (retval == 0) {
103                                 kernel_log(ifr);
104                                 break;
105                         }
106
107                         if (errno != EEXIST) {
108                                 err(udevice->udev, "error changing net interface name %s to %s: %m\n",
109                                     ifr.ifr_name, ifr.ifr_newname);
110                                 break;
111                         }
112                         dbg(udevice->udev, "wait for netif '%s' to become free, loop=%i\n",
113                             udevice->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 *udevice)
124 {
125         int retval = 0;
126
127         if (udevice->devpath_old != NULL)
128                 if (udev_db_rename(udevice->udev, udevice->devpath_old, udevice->dev->devpath) == 0)
129                         info(udevice->udev, "moved database from '%s' to '%s'\n", udevice->devpath_old, udevice->dev->devpath);
130
131         /* add device node */
132         if (major(udevice->devt) != 0 &&
133             (strcmp(udevice->action, "add") == 0 || strcmp(udevice->action, "change") == 0)) {
134                 struct udevice *udevice_old;
135
136                 dbg(udevice->udev, "device node add '%s'\n", udevice->dev->devpath);
137
138                 udev_rules_get_name(rules, udevice);
139                 if (udevice->ignore_device) {
140                         info(udevice->udev, "device event will be ignored\n");
141                         goto exit;
142                 }
143                 if (udevice->name[0] == '\0') {
144                         info(udevice->udev, "device node creation supressed\n");
145                         goto exit;
146                 }
147
148                 /* read current database entry; cleanup, if it is known device */
149                 udevice_old = udev_device_init(udevice->udev);
150                 if (udevice_old != NULL) {
151                         udevice_old->test_run = udevice->test_run;
152                         if (udev_db_get_device(udevice_old, udevice->dev->devpath) == 0) {
153                                 info(udevice->udev, "device '%s' already in database, cleanup\n", udevice->dev->devpath);
154                                 udev_db_delete_device(udevice_old);
155                         } else {
156                                 udev_device_cleanup(udevice_old);
157                                 udevice_old = NULL;
158                         }
159                 }
160
161                 /* create node */
162                 retval = udev_node_add(udevice);
163                 if (retval != 0)
164                         goto exit;
165
166                 /* store in database */
167                 udev_db_add_device(udevice);
168
169                 /* create, replace, delete symlinks according to priority */
170                 udev_node_update_symlinks(udevice, udevice_old);
171
172                 if (udevice_old != NULL)
173                         udev_device_cleanup(udevice_old);
174                 goto exit;
175         }
176
177         /* add netif */
178         if (strcmp(udevice->dev->subsystem, "net") == 0 && strcmp(udevice->action, "add") == 0) {
179                 dbg(udevice->udev, "netif add '%s'\n", udevice->dev->devpath);
180                 udev_rules_get_name(rules, udevice);
181                 if (udevice->ignore_device) {
182                         info(udevice->udev, "device event will be ignored\n");
183                         goto exit;
184                 }
185                 if (udevice->name[0] == '\0') {
186                         info(udevice->udev, "device renaming supressed\n");
187                         goto exit;
188                 }
189
190                 /* look if we want to change the name of the netif */
191                 if (strcmp(udevice->name, udevice->dev->kernel) != 0) {
192                         char devpath[PATH_MAX];
193                         char *pos;
194
195                         retval = rename_netif(udevice);
196                         if (retval != 0)
197                                 goto exit;
198                         info(udevice->udev, "renamed netif to '%s'\n", udevice->name);
199
200                         /* export old name */
201                         setenv("INTERFACE_OLD", udevice->dev->kernel, 1);
202
203                         /* now change the devpath, because the kernel device name has changed */
204                         util_strlcpy(devpath, udevice->dev->devpath, sizeof(devpath));
205                         pos = strrchr(devpath, '/');
206                         if (pos != NULL) {
207                                 pos[1] = '\0';
208                                 util_strlcat(devpath, udevice->name, sizeof(devpath));
209                                 sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL);
210                                 setenv("DEVPATH", udevice->dev->devpath, 1);
211                                 setenv("INTERFACE", udevice->name, 1);
212                                 info(udevice->udev, "changed devpath to '%s'\n", udevice->dev->devpath);
213                         }
214                 }
215                 goto exit;
216         }
217
218         /* remove device node */
219         if (major(udevice->devt) != 0 && strcmp(udevice->action, "remove") == 0) {
220                 struct name_entry *name_loop;
221
222                 /* import database entry, and delete it */
223                 if (udev_db_get_device(udevice, udevice->dev->devpath) == 0) {
224                         udev_db_delete_device(udevice);
225                         /* restore stored persistent data */
226                         list_for_each_entry(name_loop, &udevice->env_list, node)
227                                 putenv(name_loop->name);
228                 } else {
229                         dbg(udevice->udev, "'%s' not found in database, using kernel name '%s'\n",
230                             udevice->dev->devpath, udevice->dev->kernel);
231                         util_strlcpy(udevice->name, udevice->dev->kernel, sizeof(udevice->name));
232                 }
233
234                 udev_rules_get_run(rules, udevice);
235                 if (udevice->ignore_device) {
236                         info(udevice->udev, "device event will be ignored\n");
237                         goto exit;
238                 }
239
240                 if (udevice->ignore_remove) {
241                         info(udevice->udev, "ignore_remove for '%s'\n", udevice->name);
242                         goto exit;
243                 }
244                 /* remove the node */
245                 retval = udev_node_remove(udevice);
246
247                 /* delete or restore symlinks according to priority */
248                 udev_node_update_symlinks(udevice, NULL);
249                 goto exit;
250         }
251
252         /* default devices */
253         udev_rules_get_run(rules, udevice);
254         if (udevice->ignore_device)
255                 info(udevice->udev, "device event will be ignored\n");
256
257 exit:
258         return retval;
259 }