chiark / gitweb /
EXTRAS/Makefile: fix install targets to match main Makefile
[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                         struct name_entry *name_loop;
95
96                         /* get data from db, remove db-entry, delete node */
97                         dbg("node remove");
98                         retval = udev_remove_device(udev);
99
100                         /* restore stored persistent data */
101                         list_for_each_entry(name_loop, &udev->env_list, node)
102                                 putenv(name_loop->name);
103
104                         udev_rules_get_run(rules, udev, NULL, NULL);
105                         if (udev->ignore_device) {
106                                 dbg("device event will be ignored");
107                                 return -1;
108                         }
109                 }
110
111                 /* export name of device node or netif */
112                 if (udev->devname[0] != '\0')
113                         setenv("DEVNAME", udev->devname, 1);
114         } else if (udev->type == DEV_DEVICE && strcmp(udev->action, "add") == 0) {
115                 struct sysfs_device *devices_dev;
116
117                 /* wait for sysfs of /sys/devices/ */
118                 dbg("devices add");
119                 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
120                 path[sizeof(path)-1] = '\0';
121                 devices_dev = wait_devices_device_open(path);
122                 if (!devices_dev) {
123                         dbg("devices device unavailable (probably remove has beaten us)");
124                         return 0;
125                 }
126                 dbg("devices device opened '%s'", path);
127                 wait_for_devices_device(devices_dev, &error);
128                 udev_rules_get_run(rules, udev, NULL, devices_dev);
129                 sysfs_close_device(devices_dev);
130                 if (udev->ignore_device) {
131                         info("device event will be ignored");
132                         return -1;
133                 }
134         } else {
135                 dbg("default handling");
136                 udev_rules_get_run(rules, udev, NULL, NULL);
137                 if (udev->ignore_device) {
138                         info("device event will be ignored");
139                         return -1;
140                 }
141         }
142         return 0;
143 }