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