chiark / gitweb /
increase netif renaming timeout from 30 to 90 seconds
[elogind.git] / udev / udev-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
33 struct udev_event *udev_event_new(struct udev_device *dev)
34 {
35         struct udev_event *event;
36
37         event = calloc(1, sizeof(struct udev_event));
38         if (event == NULL)
39                 return NULL;
40         event->dev = dev;
41         event->udev = udev_device_get_udev(dev);
42         udev_list_init(&event->run_list);
43         event->mode = 0660;
44         dbg(event->udev, "allocated event %p\n", event);
45         return event;
46 }
47
48 void udev_event_unref(struct udev_event *event)
49 {
50         if (event == NULL)
51                 return;
52         udev_list_cleanup_entries(event->udev, &event->run_list);
53         free(event->tmp_node);
54         free(event->program_result);
55         free(event->name);
56         dbg(event->udev, "free event %p\n", event);
57         free(event);
58 }
59
60 /* extract possible {attr} and move str behind it */
61 static char *get_format_attribute(struct udev *udev, char **str)
62 {
63         char *pos;
64         char *attr = NULL;
65
66         if (*str[0] == '{') {
67                 pos = strchr(*str, '}');
68                 if (pos == NULL) {
69                         err(udev, "missing closing brace for format\n");
70                         return NULL;
71                 }
72                 pos[0] = '\0';
73                 attr = *str+1;
74                 *str = pos+1;
75                 dbg(udev, "attribute='%s', str='%s'\n", attr, *str);
76         }
77         return attr;
78 }
79
80 /* extract possible format length and move str behind it*/
81 static int get_format_len(struct udev *udev, char **str)
82 {
83         int num;
84         char *tail;
85
86         if (isdigit(*str[0])) {
87                 num = (int) strtoul(*str, &tail, 10);
88                 if (num > 0) {
89                         *str = tail;
90                         dbg(udev, "format length=%i\n", num);
91                         return num;
92                 } else {
93                         err(udev, "format parsing error '%s'\n", *str);
94                 }
95         }
96         return -1;
97 }
98
99 void udev_event_apply_format(struct udev_event *event, char *string, size_t maxsize)
100 {
101         struct udev_device *dev = event->dev;
102         char temp[UTIL_PATH_SIZE];
103         char temp2[UTIL_PATH_SIZE];
104         char *head, *tail, *cpos, *attr, *rest;
105         int len;
106         int i;
107         int count;
108         enum subst_type {
109                 SUBST_UNKNOWN,
110                 SUBST_DEVPATH,
111                 SUBST_KERNEL,
112                 SUBST_KERNEL_NUMBER,
113                 SUBST_ID,
114                 SUBST_DRIVER,
115                 SUBST_MAJOR,
116                 SUBST_MINOR,
117                 SUBST_RESULT,
118                 SUBST_ATTR,
119                 SUBST_PARENT,
120                 SUBST_TEMP_NODE,
121                 SUBST_NAME,
122                 SUBST_LINKS,
123                 SUBST_ROOT,
124                 SUBST_SYS,
125                 SUBST_ENV,
126         };
127         static const struct subst_map {
128                 char *name;
129                 char fmt;
130                 enum subst_type type;
131         } map[] = {
132                 { .name = "devpath",    .fmt = 'p',     .type = SUBST_DEVPATH },
133                 { .name = "number",     .fmt = 'n',     .type = SUBST_KERNEL_NUMBER },
134                 { .name = "kernel",     .fmt = 'k',     .type = SUBST_KERNEL },
135                 { .name = "id",         .fmt = 'b',     .type = SUBST_ID },
136                 { .name = "driver",     .fmt = 'd',     .type = SUBST_DRIVER },
137                 { .name = "major",      .fmt = 'M',     .type = SUBST_MAJOR },
138                 { .name = "minor",      .fmt = 'm',     .type = SUBST_MINOR },
139                 { .name = "result",     .fmt = 'c',     .type = SUBST_RESULT },
140                 { .name = "attr",       .fmt = 's',     .type = SUBST_ATTR },
141                 { .name = "sysfs",      .fmt = 's',     .type = SUBST_ATTR },
142                 { .name = "parent",     .fmt = 'P',     .type = SUBST_PARENT },
143                 { .name = "tempnode",   .fmt = 'N',     .type = SUBST_TEMP_NODE },
144                 { .name = "name",       .fmt = 'D',     .type = SUBST_NAME },
145                 { .name = "links",      .fmt = 'L',     .type = SUBST_LINKS },
146                 { .name = "root",       .fmt = 'r',     .type = SUBST_ROOT },
147                 { .name = "sys",        .fmt = 'S',     .type = SUBST_SYS },
148                 { .name = "env",        .fmt = 'E',     .type = SUBST_ENV },
149                 { NULL, '\0', 0 }
150         };
151         enum subst_type type;
152         const struct subst_map *subst;
153
154         head = string;
155         while (1) {
156                 len = -1;
157                 while (head[0] != '\0') {
158                         if (head[0] == '$') {
159                                 /* substitute named variable */
160                                 if (head[1] == '\0')
161                                         break;
162                                 if (head[1] == '$') {
163                                         util_strlcpy(temp, head+2, sizeof(temp));
164                                         util_strlcpy(head+1, temp, maxsize);
165                                         head++;
166                                         continue;
167                                 }
168                                 head[0] = '\0';
169                                 for (subst = map; subst->name; subst++) {
170                                         if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
171                                                 type = subst->type;
172                                                 tail = head + strlen(subst->name)+1;
173                                                 dbg(event->udev, "will substitute format name '%s'\n", subst->name);
174                                                 goto found;
175                                         }
176                                 }
177                                 head[0] = '$';
178                                 err(event->udev, "unknown format variable '%s'\n", head);
179                         } else if (head[0] == '%') {
180                                 /* substitute format char */
181                                 if (head[1] == '\0')
182                                         break;
183                                 if (head[1] == '%') {
184                                         util_strlcpy(temp, head+2, sizeof(temp));
185                                         util_strlcpy(head+1, temp, maxsize);
186                                         head++;
187                                         continue;
188                                 }
189                                 head[0] = '\0';
190                                 tail = head+1;
191                                 len = get_format_len(event->udev, &tail);
192                                 for (subst = map; subst->name; subst++) {
193                                         if (tail[0] == subst->fmt) {
194                                                 type = subst->type;
195                                                 tail++;
196                                                 dbg(event->udev, "will substitute format char '%c'\n", subst->fmt);
197                                                 goto found;
198                                         }
199                                 }
200                                 head[0] = '%';
201                                 err(event->udev, "unknown format char '%c'\n", tail[0]);
202                         }
203                         head++;
204                 }
205                 break;
206 found:
207                 attr = get_format_attribute(event->udev, &tail);
208                 util_strlcpy(temp, tail, sizeof(temp));
209                 dbg(event->udev, "format=%i, string='%s', tail='%s'\n", type ,string, tail);
210
211                 switch (type) {
212                 case SUBST_DEVPATH:
213                         util_strlcat(string, udev_device_get_devpath(dev), maxsize);
214                         dbg(event->udev, "substitute devpath '%s'\n", udev_device_get_devpath(dev));
215                         break;
216                 case SUBST_KERNEL:
217                         util_strlcat(string, udev_device_get_sysname(dev), maxsize);
218                         dbg(event->udev, "substitute kernel name '%s'\n", udev_device_get_sysname(dev));
219                         break;
220                 case SUBST_KERNEL_NUMBER:
221                         if (udev_device_get_sysnum(dev) == NULL)
222                                 break;
223                         util_strlcat(string, udev_device_get_sysnum(dev), maxsize);
224                         dbg(event->udev, "substitute kernel number '%s'\n", udev_device_get_sysnum(dev));
225                         break;
226                 case SUBST_ID:
227                         if (event->dev_parent != NULL) {
228                                 util_strlcat(string, udev_device_get_sysname(event->dev_parent), maxsize);
229                                 dbg(event->udev, "substitute id '%s'\n", udev_device_get_sysname(event->dev_parent));
230                         }
231                         break;
232                 case SUBST_DRIVER:
233                         if (event->dev_parent != NULL) {
234                                 const char *driver;
235
236                                 driver = udev_device_get_driver(event->dev_parent);
237                                 if (driver == NULL)
238                                         break;
239                                 util_strlcat(string, driver, maxsize);
240                                 dbg(event->udev, "substitute driver '%s'\n", driver);
241                         }
242                         break;
243                 case SUBST_MAJOR:
244                         sprintf(temp2, "%d", major(udev_device_get_devnum(dev)));
245                         util_strlcat(string, temp2, maxsize);
246                         dbg(event->udev, "substitute major number '%s'\n", temp2);
247                         break;
248                 case SUBST_MINOR:
249                         sprintf(temp2, "%d", minor(udev_device_get_devnum(dev)));
250                         util_strlcat(string, temp2, maxsize);
251                         dbg(event->udev, "substitute minor number '%s'\n", temp2);
252                         break;
253                 case SUBST_RESULT:
254                         if (event->program_result == NULL)
255                                 break;
256                         /* get part part of the result string */
257                         i = 0;
258                         if (attr != NULL)
259                                 i = strtoul(attr, &rest, 10);
260                         if (i > 0) {
261                                 char result[UTIL_PATH_SIZE];
262
263                                 dbg(event->udev, "request part #%d of result string\n", i);
264                                 util_strlcpy(result, event->program_result, sizeof(result));
265                                 cpos = result;
266                                 while (--i) {
267                                         while (cpos[0] != '\0' && !isspace(cpos[0]))
268                                                 cpos++;
269                                         while (isspace(cpos[0]))
270                                                 cpos++;
271                                 }
272                                 if (i > 0) {
273                                         err(event->udev, "requested part of result string not found\n");
274                                         break;
275                                 }
276                                 util_strlcpy(temp2, cpos, sizeof(temp2));
277                                 /* %{2+}c copies the whole string from the second part on */
278                                 if (rest[0] != '+') {
279                                         cpos = strchr(temp2, ' ');
280                                         if (cpos)
281                                                 cpos[0] = '\0';
282                                 }
283                                 util_strlcat(string, temp2, maxsize);
284                                 dbg(event->udev, "substitute part of result string '%s'\n", temp2);
285                         } else {
286                                 util_strlcat(string, event->program_result, maxsize);
287                                 dbg(event->udev, "substitute result string '%s'\n", event->program_result);
288                         }
289                         break;
290                 case SUBST_ATTR:
291                         if (attr == NULL)
292                                 err(event->udev, "missing file parameter for attr\n");
293                         else {
294                                 const char *val;
295                                 char value[UTIL_NAME_SIZE];
296                                 size_t size;
297
298                                 value[0] = '\0';
299                                 /* read the value specified by [usb/]*/
300                                 util_resolve_subsys_kernel(event->udev, attr, value, sizeof(value), 1);
301
302                                 /* try to read attribute of the current device */
303                                 if (value[0] == '\0') {
304                                         val = udev_device_get_sysattr_value(event->dev, attr);
305                                         if (val != NULL)
306                                                 util_strlcpy(value, val, sizeof(value));
307                                 }
308
309                                 /* try to read the attribute of the parent device, other matches have selected */
310                                 if (value[0] == '\0' && event->dev_parent != NULL && event->dev_parent != event->dev) {
311                                         val = udev_device_get_sysattr_value(event->dev_parent, attr);
312                                         if (val != NULL)
313                                                 util_strlcpy(value, val, sizeof(value));
314                                 }
315
316                                 if (value[0]=='\0')
317                                         break;
318
319                                 /* strip trailing whitespace, and replace unwanted characters */
320                                 size = strlen(value);
321                                 while (size > 0 && isspace(value[--size]))
322                                         value[size] = '\0';
323                                 count = udev_util_replace_chars(value, UDEV_ALLOWED_CHARS_INPUT);
324                                 if (count > 0)
325                                         info(event->udev, "%i character(s) replaced\n" , count);
326                                 util_strlcat(string, value, maxsize);
327                                 dbg(event->udev, "substitute sysfs value '%s'\n", value);
328                         }
329                         break;
330                 case SUBST_PARENT:
331                         {
332                                 struct udev_device *dev_parent;
333                                 const char *devnode;
334
335                                 dev_parent = udev_device_get_parent(event->dev);
336                                 if (dev_parent == NULL)
337                                         break;
338                                 devnode = udev_device_get_devnode(dev_parent);
339                                 if (devnode != NULL) {
340                                         size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
341
342                                         util_strlcat(string, &devnode[devlen], maxsize);
343                                         dbg(event->udev, "found parent '%s', got node name '%s'\n",
344                                             udev_device_get_syspath(dev_parent), &devnode[devlen]);
345                                 }
346                         }
347                         break;
348                 case SUBST_TEMP_NODE:
349                         {
350                                 dev_t devnum;
351                                 struct stat statbuf;
352                                 char filename[UTIL_PATH_SIZE];
353                                 const char *devtype;
354
355                                 if (event->tmp_node != NULL) {
356                                         util_strlcat(string, event->tmp_node, maxsize);
357                                         dbg(event->udev, "return existing temporary node\n");
358                                         break;
359                                 }
360                                 devnum = udev_device_get_devnum(dev);
361                                 if (major(udev_device_get_devnum(dev) == 0))
362                                         break;
363                                 if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
364                                         devtype = "block";
365                                 else
366                                         devtype = "char";
367                                 snprintf(filename, sizeof(filename), "%s/%s/%u:%u",
368                                          udev_get_dev_path(event->udev), devtype,
369                                          major(udev_device_get_devnum(dev)),
370                                          minor(udev_device_get_devnum(dev)));
371                                 if (stat(filename, &statbuf) == 0 && statbuf.st_rdev == devnum) {
372                                         util_strlcat(string, filename, maxsize);
373                                         dbg(event->udev, "return existing temporary node\n");
374                                         break;
375                                 }
376                                 dbg(event->udev, "create temporary node\n");
377                                 asprintf(&event->tmp_node, "%s/.tmp-%s-%u:%u",
378                                          udev_get_dev_path(event->udev), devtype,
379                                          major(udev_device_get_devnum(dev)),
380                                          minor(udev_device_get_devnum(dev)));
381                                 if (event->tmp_node == NULL)
382                                         break;
383                                 udev_node_mknod(dev, event->tmp_node, makedev(0, 0), 0600, 0, 0);
384                                 util_strlcat(string, event->tmp_node, maxsize);
385                         }
386                         break;
387                 case SUBST_NAME:
388                         if (event->name != NULL) {
389                                 util_strlcat(string, event->name, maxsize);
390                                 dbg(event->udev, "substitute name '%s'\n", event->name);
391                         } else {
392                                 util_strlcat(string, udev_device_get_sysname(dev), maxsize);
393                                 dbg(event->udev, "substitute sysname '%s'\n", udev_device_get_sysname(dev));
394                         }
395                         break;
396                 case SUBST_LINKS:
397                         {
398                                 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
399                                 struct udev_list_entry *list_entry;
400
401                                 list_entry = udev_device_get_devlinks_list_entry(dev);
402                                 if (list_entry == NULL)
403                                         break;
404                                 util_strlcat(string, &udev_list_entry_get_name(list_entry)[devlen], maxsize);
405                                 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
406                                         util_strlcat(string, " ", maxsize);
407                                         util_strlcat(string, &udev_list_entry_get_name(list_entry)[devlen], maxsize);
408                                 }
409                         }
410                         break;
411                 case SUBST_ROOT:
412                         util_strlcat(string, udev_get_dev_path(event->udev), maxsize);
413                         dbg(event->udev, "substitute udev_root '%s'\n", udev_get_dev_path(event->udev));
414                         break;
415                 case SUBST_SYS:
416                         util_strlcat(string, udev_get_sys_path(event->udev), maxsize);
417                         dbg(event->udev, "substitute sys_path '%s'\n", udev_get_sys_path(event->udev));
418                         break;
419                 case SUBST_ENV:
420                         if (attr == NULL) {
421                                 dbg(event->udev, "missing attribute\n");
422                                 break;
423                         } else {
424                                 const char *value;
425
426                                 value = udev_device_get_property_value(event->dev, attr);
427                                 if (value == NULL)
428                                         break;
429                                 dbg(event->udev, "substitute env '%s=%s'\n", attr, value);
430                                 util_strlcat(string, value, maxsize);
431                                 break;
432                         }
433                 default:
434                         err(event->udev, "unknown substitution type=%i\n", type);
435                         break;
436                 }
437                 /* possibly truncate to format-char specified length */
438                 if (len >= 0 && len < (int)strlen(head)) {
439                         head[len] = '\0';
440                         dbg(event->udev, "truncate to %i chars, subtitution string becomes '%s'\n", len, head);
441                 }
442                 util_strlcat(string, temp, maxsize);
443         }
444 }
445
446 static void rename_netif_kernel_log(struct ifreq ifr)
447 {
448         int klog;
449         FILE *f;
450
451         klog = open("/dev/kmsg", O_WRONLY);
452         if (klog < 0)
453                 return;
454
455         f = fdopen(klog, "w");
456         if (f == NULL) {
457                 close(klog);
458                 return;
459         }
460
461         fprintf(f, "<6>udev: renamed network interface %s to %s\n",
462                 ifr.ifr_name, ifr.ifr_newname);
463         fclose(f);
464 }
465
466 static int rename_netif(struct udev_event *event)
467 {
468         struct udev_device *dev = event->dev;
469         int sk;
470         struct ifreq ifr;
471         int err;
472
473         info(event->udev, "changing net interface name from '%s' to '%s'\n",
474              udev_device_get_sysname(dev), event->name);
475
476         sk = socket(PF_INET, SOCK_DGRAM, 0);
477         if (sk < 0) {
478                 err(event->udev, "error opening socket: %m\n");
479                 return -1;
480         }
481
482         memset(&ifr, 0x00, sizeof(struct ifreq));
483         util_strlcpy(ifr.ifr_name, udev_device_get_sysname(dev), IFNAMSIZ);
484         util_strlcpy(ifr.ifr_newname, event->name, IFNAMSIZ);
485         err = ioctl(sk, SIOCSIFNAME, &ifr);
486         if (err == 0)
487                 rename_netif_kernel_log(ifr);
488         else {
489                 int loop;
490
491                 /* see if the destination interface name already exists */
492                 if (errno != EEXIST) {
493                         err(event->udev, "error changing netif name %s to %s: %m\n",
494                             ifr.ifr_name, ifr.ifr_newname);
495                         goto exit;
496                 }
497
498                 /* free our own name, another process may wait for us */
499                 util_strlcpy(ifr.ifr_newname, udev_device_get_sysname(dev), IFNAMSIZ);
500                 util_strlcat(ifr.ifr_newname, "_rename", IFNAMSIZ);
501                 err = ioctl(sk, SIOCSIFNAME, &ifr);
502                 if (err != 0) {
503                         err(event->udev, "error changing netif name %s to %s: %m\n",
504                             ifr.ifr_name, ifr.ifr_newname);
505                         goto exit;
506                 }
507
508                 /* wait 90 seconds for our target to become available */
509                 util_strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ);
510                 util_strlcpy(ifr.ifr_newname, event->name, IFNAMSIZ);
511                 loop = 90 * 20;
512                 while (loop--) {
513                         err = ioctl(sk, SIOCSIFNAME, &ifr);
514                         if (err == 0) {
515                                 rename_netif_kernel_log(ifr);
516                                 break;
517                         }
518
519                         if (errno != EEXIST) {
520                                 err(event->udev, "error changing net interface name %s to %s: %m\n",
521                                     ifr.ifr_name, ifr.ifr_newname);
522                                 break;
523                         }
524                         dbg(event->udev, "wait for netif '%s' to become free, loop=%i\n",
525                             event->name, (90 * 20) - loop);
526                         usleep(1000 * 1000 / 20);
527                 }
528         }
529 exit:
530         close(sk);
531         return err;
532 }
533
534 int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
535 {
536         struct udev_device *dev = event->dev;
537         int err = 0;
538
539         if (udev_device_get_devpath_old(dev) != NULL) {
540                 if (udev_device_rename_db(dev, udev_device_get_devpath(dev)) == 0)
541                         info(event->udev, "moved database from '%s' to '%s'\n",
542                              udev_device_get_devpath_old(dev), udev_device_get_devpath(dev));
543         }
544
545         /* add device node */
546         if (major(udev_device_get_devnum(dev)) != 0 &&
547             (strcmp(udev_device_get_action(dev), "add") == 0 || strcmp(udev_device_get_action(dev), "change") == 0)) {
548                 char filename[UTIL_PATH_SIZE];
549                 struct udev_device *dev_old;
550
551                 dbg(event->udev, "device node add '%s'\n", udev_device_get_devpath(dev));
552
553                 udev_rules_apply_to_event(rules, event);
554                 if (event->tmp_node != NULL) {
555                         dbg(event->udev, "removing temporary device node\n");
556                         util_unlink_secure(event->udev, event->tmp_node);
557                         free(event->tmp_node);
558                         event->tmp_node = NULL;
559                 }
560
561                 if (event->ignore_device) {
562                         info(event->udev, "device event will be ignored\n");
563                         goto exit;
564                 }
565
566                 if (event->name != NULL && event->name[0] == '\0') {
567                         info(event->udev, "device node creation supressed\n");
568                         goto exit;
569                 }
570
571                 if (event->name == NULL) {
572                         info(event->udev, "no node name set, will use kernel name '%s'\n",
573                              udev_device_get_sysname(event->dev));
574                         event->name = strdup(udev_device_get_sysname(event->dev));
575                         if (event->name == NULL)
576                                 goto exit;
577                 }
578
579                 /* set device node name */
580                 util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
581                 util_strlcat(filename, "/", sizeof(filename));
582                 util_strlcat(filename, event->name, sizeof(filename));
583                 udev_device_set_devnode(dev, filename);
584
585                 /* read old database entry */
586                 dev_old = udev_device_new_from_syspath(event->udev, udev_device_get_syspath(dev));
587                 if (dev_old != NULL) {
588                         udev_device_read_db(dev_old);
589                         udev_device_set_info_loaded(dev_old);
590                 }
591
592                 /* write current database entry */
593                 udev_device_update_db(dev);
594
595                 /* remove/update possible left-over symlinks from old database entry */
596                 if (dev_old != NULL) {
597                         udev_node_update_old_links(dev, dev_old);
598                         udev_device_unref(dev_old);
599                 }
600
601                 /* create new node and symlinks */
602                 err = udev_node_add(dev, event->mode, event->uid, event->gid);
603                 goto exit;
604         }
605
606         /* add netif */
607         if (strcmp(udev_device_get_subsystem(dev), "net") == 0 && strcmp(udev_device_get_action(dev), "add") == 0) {
608                 dbg(event->udev, "netif add '%s'\n", udev_device_get_devpath(dev));
609                 udev_device_delete_db(dev);
610
611                 udev_rules_apply_to_event(rules, event);
612                 if (event->ignore_device) {
613                         info(event->udev, "device event will be ignored\n");
614                         goto exit;
615                 }
616                 if (event->name == NULL)
617                         goto exit;
618
619                 /* look if we want to change the name of the netif */
620                 if (strcmp(event->name, udev_device_get_sysname(dev)) != 0) {
621                         char syspath[UTIL_PATH_SIZE];
622                         char *pos;
623
624                         err = rename_netif(event);
625                         if (err != 0)
626                                 goto exit;
627                         info(event->udev, "renamed netif to '%s'\n", event->name);
628
629                         /* remember old name */
630                         udev_device_add_property(dev, "INTERFACE_OLD", udev_device_get_sysname(dev));
631
632                         /* now change the devpath, because the kernel device name has changed */
633                         util_strlcpy(syspath, udev_device_get_syspath(dev), sizeof(syspath));
634                         pos = strrchr(syspath, '/');
635                         if (pos != NULL) {
636                                 pos[1] = '\0';
637                                 util_strlcat(syspath, event->name, sizeof(syspath));
638                                 udev_device_set_syspath(event->dev, syspath);
639                                 udev_device_add_property(dev, "INTERFACE", udev_device_get_sysname(dev));
640                                 info(event->udev, "changed devpath to '%s'\n", udev_device_get_devpath(dev));
641                         }
642                 }
643                 udev_device_update_db(dev);
644                 goto exit;
645         }
646
647         /* remove device node */
648         if (major(udev_device_get_devnum(dev)) != 0 && strcmp(udev_device_get_action(dev), "remove") == 0) {
649                 /* import database entry and delete it */
650                 udev_device_read_db(dev);
651                 udev_device_set_info_loaded(dev);
652                 udev_device_delete_db(dev);
653
654                 if (udev_device_get_devnode(dev) == NULL) {
655                         char devnode[UTIL_PATH_SIZE];
656
657                         info(event->udev, "'%s' not found in database, using kernel name '%s'\n",
658                              udev_device_get_syspath(dev), udev_device_get_sysname(dev));
659                         util_strlcpy(devnode, udev_get_dev_path(event->udev), sizeof(devnode));
660                         util_strlcat(devnode, "/", sizeof(devnode));
661                         util_strlcat(devnode, udev_device_get_sysname(dev), sizeof(devnode));
662                         udev_device_set_devnode(dev, devnode);
663                 }
664
665                 udev_rules_apply_to_event(rules, event);
666                 if (event->ignore_device) {
667                         info(event->udev, "device event will be ignored\n");
668                         goto exit;
669                 }
670
671                 if (udev_device_get_ignore_remove(dev)) {
672                         info(event->udev, "ignore_remove for '%s'\n", udev_device_get_devnode(dev));
673                         goto exit;
674                 }
675
676                 err = udev_node_remove(dev);
677                 goto exit;
678         }
679
680         /* default devices */
681         udev_rules_apply_to_event(rules, event);
682         if (event->ignore_device)
683                 info(event->udev, "device event will be ignored\n");
684
685         if (strcmp(udev_device_get_action(dev), "remove") != 0)
686                 udev_device_update_db(dev);
687         else
688                 udev_device_delete_db(dev);
689 exit:
690         return err;
691 }
692
693 int udev_event_execute_run(struct udev_event *event)
694 {
695         struct udev_list_entry *list_entry;
696         int err = 0;
697
698         dbg(event->udev, "executing run list\n");
699         udev_list_entry_foreach(list_entry, udev_list_get_entry(&event->run_list)) {
700                 const char *cmd = udev_list_entry_get_name(list_entry);
701
702                 if (strncmp(cmd, "socket:", strlen("socket:")) == 0) {
703                         struct udev_monitor *monitor;
704
705                         monitor = udev_monitor_new_from_socket(event->udev, &cmd[strlen("socket:")]);
706                         if (monitor == NULL)
707                                 continue;
708                         udev_monitor_send_device(monitor, event->dev);
709                         udev_monitor_unref(monitor);
710                 } else {
711                         char program[UTIL_PATH_SIZE];
712                         char **envp;
713
714                         util_strlcpy(program, cmd, sizeof(program));
715                         udev_event_apply_format(event, program, sizeof(program));
716                         if (event->trace)
717                                 fprintf(stderr, "run  %s (%llu) '%s'\n",
718                                        udev_device_get_syspath(event->dev),
719                                        udev_device_get_seqnum(event->dev),
720                                        program);
721                         envp = udev_device_get_properties_envp(event->dev);
722                         if (util_run_program(event->udev, program, envp, NULL, 0, NULL) != 0) {
723                                 if (!udev_list_entry_get_flag(list_entry))
724                                         err = -1;
725                         }
726                 }
727         }
728         return err;
729 }