chiark / gitweb /
add EXTRAS documentation to the README file.
[elogind.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
32 #include "udev_libc_wrapper.h"
33 #include "udev.h"
34 #include "logging.h"
35 #include "udev_rules.h"
36 #include "udev_utils.h"
37 #include "udev_sysfs.h"
38 #include "list.h"
39
40
41 int udev_process_event(struct udev_rules *rules, struct udevice *udev)
42 {
43         int retval;
44         char path[PATH_SIZE];
45         const char *error;
46
47         if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS || udev->type == DEV_NET) {
48                 /* handle device node */
49                 if (strcmp(udev->action, "add") == 0) {
50                         struct sysfs_class_device *class_dev;
51
52                         /* wait for sysfs of /sys/class /sys/block */
53                         dbg("node add");
54                         snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
55                         path[sizeof(path)-1] = '\0';
56                         class_dev = wait_class_device_open(path);
57                         if (class_dev == NULL) {
58                                 dbg("open class device failed");
59                                 return 0;
60                         }
61                         dbg("opened class_dev->name='%s'", class_dev->name);
62                         wait_for_class_device(class_dev, &error);
63
64                         /* get major/minor */
65                         if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS)
66                                 udev->devt = get_devt(class_dev);
67
68                         if (udev->type == DEV_NET || udev->devt) {
69                                 /* name device */
70                                 udev_rules_get_name(rules, udev, class_dev);
71                                 if (udev->ignore_device) {
72                                         info("device event will be ignored");
73                                         sysfs_close_class_device(class_dev);
74                                         return -1;
75                                 }
76                                 if (udev->name[0] == '\0') {
77                                         info("device node creation supressed");
78                                         sysfs_close_class_device(class_dev);
79                                         return -1;
80                                 }
81                                 /* create node, store in db */
82                                 retval = udev_add_device(udev, class_dev);
83                         } else {
84                                 dbg("no dev-file found");
85                                 udev_rules_get_run(rules, udev, class_dev, NULL);
86                                 if (udev->ignore_device) {
87                                         info("device event will be ignored");
88                                         sysfs_close_class_device(class_dev);
89                                         return -1;
90                                 }
91                         }
92                         sysfs_close_class_device(class_dev);
93                 } else if (strcmp(udev->action, "remove") == 0) {
94                         dbg("node remove");
95                         udev_rules_get_run(rules, udev, NULL, NULL);
96                         if (udev->ignore_device) {
97                                 dbg("device event will be ignored");
98                                 return -1;
99                         }
100
101                         /* get name from db, remove db-entry, delete node */
102                         retval = udev_remove_device(udev);
103                 }
104
105                 /* export name of device node or netif */
106                 if (udev->devname[0] != '\0')
107                         setenv("DEVNAME", udev->devname, 1);
108         } else if (udev->type == DEV_DEVICE && strcmp(udev->action, "add") == 0) {
109                 struct sysfs_device *devices_dev;
110
111                 /* wait for sysfs of /sys/devices/ */
112                 dbg("devices add");
113                 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
114                 path[sizeof(path)-1] = '\0';
115                 devices_dev = wait_devices_device_open(path);
116                 if (!devices_dev) {
117                         dbg("devices device unavailable (probably remove has beaten us)");
118                         return 0;
119                 }
120                 dbg("devices device opened '%s'", path);
121                 wait_for_devices_device(devices_dev, &error);
122                 udev_rules_get_run(rules, udev, NULL, devices_dev);
123                 sysfs_close_device(devices_dev);
124                 if (udev->ignore_device) {
125                         info("device event will be ignored");
126                         return -1;
127                 }
128         } else {
129                 dbg("default handling");
130                 udev_rules_get_run(rules, udev, NULL, NULL);
131                 if (udev->ignore_device) {
132                         info("device event will be ignored");
133                         return -1;
134                 }
135         }
136         return 0;
137 }