]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_device.c
let "ignore_device" always return the event successfully
[thirdparty/systemd.git] / udev_device.c
1 /*
2 * udev_utils.c - generic stuff used by udev
3 *
4 * Copyright (C) 2004, 2005 Kay Sievers <kay.sievers@vrfy.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29
30 #include "udev.h"
31 #include "udev_rules.h"
32
33
34 struct udevice *udev_device_init(void)
35 {
36 struct udevice *udev;
37
38 udev = malloc(sizeof(struct udevice));
39 if (udev == NULL)
40 return NULL;
41 memset(udev, 0x00, sizeof(struct udevice));
42
43 INIT_LIST_HEAD(&udev->symlink_list);
44 INIT_LIST_HEAD(&udev->run_list);
45 INIT_LIST_HEAD(&udev->env_list);
46
47 /* set sysfs device to local storage, can be overridden if needed */
48 udev->dev = &udev->dev_local;
49
50 /* default node permissions */
51 udev->mode = 0660;
52 strcpy(udev->owner, "root");
53 strcpy(udev->group, "root");
54
55 return udev;
56 }
57
58 void udev_device_cleanup(struct udevice *udev)
59 {
60 name_list_cleanup(&udev->symlink_list);
61 name_list_cleanup(&udev->run_list);
62 name_list_cleanup(&udev->env_list);
63 free(udev);
64 }
65
66 dev_t udev_device_get_devt(struct udevice *udev)
67 {
68 const char *attr;
69 unsigned int major, minor;
70
71 /* read it from sysfs */
72 attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
73 if (attr != NULL) {
74 if (sscanf(attr, "%u:%u", &major, &minor) == 2)
75 return makedev(major, minor);
76 }
77 return makedev(0, 0);
78 }
79
80 int udev_device_event(struct udev_rules *rules, struct udevice *udev)
81 {
82 int retval = 0;
83
84 /* device node or netif */
85 if ((major(udev->devt) != 0 || strcmp(udev->dev->subsystem, "net") == 0) &&
86 strcmp(udev->action, "add") == 0) {
87 dbg("device node or netif add '%s'", udev->dev->devpath);
88 udev_rules_get_name(rules, udev);
89 if (udev->ignore_device) {
90 info("device event will be ignored");
91 return 0;
92 }
93 /* create node, store in db */
94 if (udev->name[0] != '\0')
95 retval = udev_add_device(udev);
96 else
97 info("device node creation supressed");
98 return retval;
99 }
100
101 if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
102 struct name_entry *name_loop;
103
104 udev_rules_get_run(rules, udev);
105 if (udev->ignore_device) {
106 info("device event will be ignored");
107 return 0;
108 }
109 /* get data from db, remove db-entry, delete node */
110 retval = udev_remove_device(udev);
111
112 /* restore stored persistent data */
113 list_for_each_entry(name_loop, &udev->env_list, node)
114 putenv(name_loop->name);
115 return retval;
116 }
117
118 /* default devices without a node */
119 udev_rules_get_run(rules, udev);
120 if (udev->ignore_device)
121 info("device event will be ignored");
122
123 return retval;
124 }