chiark / gitweb /
fix tests and remove no longer useful stuff
[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                                         /* create node, store in db */
78                                         retval = udev_add_device(udev, class_dev);
79                                 } else {
80                                         info("device node creation supressed");
81                                 }
82                         } else {
83                                 dbg("no dev-file found");
84                                 udev_rules_get_run(rules, udev, class_dev, NULL);
85                                 if (udev->ignore_device) {
86                                         info("device event will be ignored");
87                                         sysfs_close_class_device(class_dev);
88                                         return -1;
89                                 }
90                         }
91                         sysfs_close_class_device(class_dev);
92                 } else if (strcmp(udev->action, "remove") == 0) {
93                         struct name_entry *name_loop;
94
95                         /* get data from db, remove db-entry, delete node */
96                         dbg("node remove");
97                         retval = udev_remove_device(udev);
98
99                         /* restore stored persistent data */
100                         list_for_each_entry(name_loop, &udev->env_list, node)
101                                 putenv(name_loop->name);
102
103                         udev_rules_get_run(rules, udev, NULL, NULL);
104                         if (udev->ignore_device) {
105                                 dbg("device event will be ignored");
106                                 return -1;
107                         }
108                 }
109
110                 /* export name of device node or netif */
111                 if (udev->devname[0] != '\0')
112                         setenv("DEVNAME", udev->devname, 1);
113         } else if (udev->type == DEV_DEVICE && strcmp(udev->action, "add") == 0) {
114                 struct sysfs_device *devices_dev;
115
116                 /* wait for sysfs of /sys/devices/ */
117                 dbg("devices add");
118                 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
119                 path[sizeof(path)-1] = '\0';
120                 devices_dev = wait_devices_device_open(path);
121                 if (!devices_dev) {
122                         dbg("devices device unavailable (probably remove has beaten us)");
123                         return 0;
124                 }
125                 dbg("devices device opened '%s'", path);
126                 wait_for_devices_device(devices_dev, &error);
127                 udev_rules_get_run(rules, udev, NULL, devices_dev);
128                 sysfs_close_device(devices_dev);
129                 if (udev->ignore_device) {
130                         info("device event will be ignored");
131                         return -1;
132                 }
133         } else {
134                 dbg("default handling");
135                 udev_rules_get_run(rules, udev, NULL, NULL);
136                 if (udev->ignore_device) {
137                         info("device event will be ignored");
138                         return -1;
139                 }
140         }
141         return 0;
142 }