]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_event.c
Makefile: fail, if submake fails
[thirdparty/systemd.git] / udev_event.c
1 /*
2 * udev_event.c - udev event process
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 #include <dirent.h>
30 #include <syslog.h>
31 #include <sys/stat.h>
32
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev_libc_wrapper.h"
35 #include "udev.h"
36 #include "logging.h"
37 #include "udev_rules.h"
38 #include "udev_utils.h"
39 #include "list.h"
40
41
42 dev_t get_devt(struct sysfs_class_device *class_dev)
43 {
44 struct sysfs_attribute *attr = NULL;
45 unsigned int major, minor;
46 char *maj, *min;
47
48 maj = getenv("MAJOR");
49 min = getenv("MINOR");
50
51 if (maj && min) {
52 major = atoi(maj);
53 minor = atoi(min);
54 } else {
55 attr = sysfs_get_classdev_attr(class_dev, "dev");
56 if (attr == NULL)
57 return 0;
58 dbg("dev='%s'", attr->value);
59
60 if (sscanf(attr->value, "%u:%u", &major, &minor) != 2)
61 return 0;
62 }
63
64 dbg("found major=%d, minor=%d", major, minor);
65 return makedev(major, minor);
66 }
67
68 int udev_process_event(struct udev_rules *rules, struct udevice *udev)
69 {
70 int retval;
71 char path[PATH_SIZE];
72
73 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS || udev->type == DEV_NET) {
74 /* handle device node */
75 if (strcmp(udev->action, "add") == 0) {
76 struct sysfs_class_device *class_dev;
77
78 dbg("node add");
79 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
80 path[sizeof(path)-1] = '\0';
81 class_dev = sysfs_open_class_device_path(path);
82 if (class_dev == NULL) {
83 dbg("open class device failed");
84 return 0;
85 }
86 dbg("opened class_dev->name='%s'", class_dev->name);
87
88 /* get major/minor */
89 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS)
90 udev->devt = get_devt(class_dev);
91
92 if (udev->type == DEV_NET || udev->devt) {
93 /* name device */
94 udev_rules_get_name(rules, udev, class_dev);
95 if (udev->ignore_device) {
96 info("device event will be ignored");
97 sysfs_close_class_device(class_dev);
98 return -1;
99 }
100 if (udev->name[0] != '\0') {
101 /* create node, store in db */
102 retval = udev_add_device(udev, class_dev);
103 } else {
104 info("device node creation supressed");
105 }
106 } else {
107 dbg("no dev-file found");
108 udev_rules_get_run(rules, udev, class_dev, NULL);
109 if (udev->ignore_device) {
110 info("device event will be ignored");
111 sysfs_close_class_device(class_dev);
112 return -1;
113 }
114 }
115 sysfs_close_class_device(class_dev);
116 } else if (strcmp(udev->action, "remove") == 0) {
117 struct name_entry *name_loop;
118
119 /* get data from db, remove db-entry, delete node */
120 dbg("node remove");
121 retval = udev_remove_device(udev);
122
123 /* restore stored persistent data */
124 list_for_each_entry(name_loop, &udev->env_list, node)
125 putenv(name_loop->name);
126
127 udev_rules_get_run(rules, udev, NULL, NULL);
128 if (udev->ignore_device) {
129 dbg("device event will be ignored");
130 return -1;
131 }
132 }
133
134 /* export name of device node or netif */
135 if (udev->devname[0] != '\0')
136 setenv("DEVNAME", udev->devname, 1);
137 } else if (udev->type == DEV_DEVICE && strcmp(udev->action, "add") == 0) {
138 struct sysfs_device *devices_dev;
139
140 dbg("devices add");
141 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
142 path[sizeof(path)-1] = '\0';
143 devices_dev = sysfs_open_device_path(path);
144 if (!devices_dev) {
145 dbg("devices device unavailable (probably remove has beaten us)");
146 return 0;
147 }
148
149 dbg("devices device opened '%s'", path);
150 udev_rules_get_run(rules, udev, NULL, devices_dev);
151 sysfs_close_device(devices_dev);
152 if (udev->ignore_device) {
153 info("device event will be ignored");
154 return -1;
155 }
156 } else {
157 dbg("default handling");
158 udev_rules_get_run(rules, udev, NULL, NULL);
159 if (udev->ignore_device) {
160 info("device event will be ignored");
161 return -1;
162 }
163 }
164 return 0;
165 }