chiark / gitweb /
volume_id: use udev-provided log-level
[elogind.git] / udev.c
1 /*
2  * udev.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <unistd.h>
33
34 #include "libsysfs/sysfs/libsysfs.h"
35 #include "udev_libc_wrapper.h"
36 #include "udev.h"
37 #include "udev_utils.h"
38 #include "udev_sysfs.h"
39 #include "udev_version.h"
40 #include "udev_rules.h"
41 #include "logging.h"
42
43 #ifdef USE_LOG
44 void log_message(int priority, const char *format, ...)
45 {
46         va_list args;
47
48         if (priority > udev_log_priority)
49                 return;
50
51         va_start(args, format);
52         vsyslog(priority, format, args);
53         va_end(args);
54 }
55 #endif
56
57 /* Decide if we should manage the whole uevent, including multiplexing
58  * of the hotplug directories.
59  * For now look if the kernel calls udevsend instead of /sbin/hotplug,
60  * or the uevent-helper in /proc/sys/kernel/hotplug is empty.
61  */
62 static int manage_hotplug_event(void) {
63         char helper[256];
64         int fd;
65         int len;
66
67         /* don't handle hotplug.d if we are called directly */
68         if (!getenv("UDEVD_EVENT"))
69                 return 0;
70
71         fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
72         if (fd < 0)
73                 return 0;
74
75         len = read(fd, helper, sizeof(helper)-1);
76         close(fd);
77
78         if (len < 0)
79                 return 0;
80         helper[len] = '\0';
81
82         if (helper[0] == '\0' || helper[0] == '\n')
83                 return 1;
84         if (strstr(helper, "udevsend"))
85                 return 1;
86
87         return 0;
88 }
89
90 static void asmlinkage sig_handler(int signum)
91 {
92         switch (signum) {
93                 case SIGALRM:
94                         exit(1);
95                 case SIGINT:
96                 case SIGTERM:
97                         exit(20 + signum);
98         }
99 }
100
101 int main(int argc, char *argv[], char *envp[])
102 {
103         struct sysfs_class_device *class_dev;
104         struct sysfs_device *devices_dev;
105         struct udevice udev;
106         char path[PATH_SIZE];
107         const char *error;
108         const char *action;
109         const char *devpath;
110         const char *subsystem;
111         int managed_event;
112         struct sigaction act;
113         int retval = -EINVAL;
114
115         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
116                 printf("%s\n", UDEV_VERSION);
117                 exit(0);
118         }
119
120         logging_init("udev");
121         udev_init_config();
122         dbg("version %s", UDEV_VERSION);
123
124         /* set signal handlers */
125         memset(&act, 0x00, sizeof(act));
126         act.sa_handler = (void (*)(int)) sig_handler;
127         sigemptyset (&act.sa_mask);
128         act.sa_flags = 0;
129         sigaction(SIGALRM, &act, NULL);
130         sigaction(SIGINT, &act, NULL);
131         sigaction(SIGTERM, &act, NULL);
132
133         /* trigger timeout to prevent hanging processes */
134         alarm(ALARM_TIMEOUT);
135
136         /* let the executed programs know if we handle the whole hotplug event */
137         managed_event = manage_hotplug_event();
138         if (managed_event)
139                 setenv("MANAGED_EVENT", "1", 1);
140
141         action = getenv("ACTION");
142         devpath = getenv("DEVPATH");
143         subsystem = getenv("SUBSYSTEM");
144         /* older kernels passed the SUBSYSTEM only as argument */
145         if (!subsystem && argc == 2)
146                 subsystem = argv[1];
147
148         udev_init_device(&udev, devpath, subsystem, action);
149
150         if (!action || !subsystem || !devpath) {
151                 err("action, subsystem or devpath missing");
152                 goto hotplug;
153         }
154
155         /* export logging flag, as called programs may want to do the same as udev */
156         if (udev_log_priority) {
157                 char priority[32];
158
159                 sprintf(priority, "%i", udev_log_priority);
160                 setenv("UDEV_LOG", priority, 1);
161         }
162
163         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS || udev.type == DEV_NET) {
164                 udev_rules_init();
165
166                 if (strcmp(action, "add") == 0) {
167                         /* wait for sysfs and possibly add node */
168                         dbg("udev add");
169
170                         /* skip subsystems without "dev", but handle net devices */
171                         if (udev.type != DEV_NET && subsystem_expect_no_dev(udev.subsystem)) {
172                                 dbg("don't care about '%s' devices", udev.subsystem);
173                                 goto hotplug;
174                         }
175
176                         snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
177                         path[sizeof(path)-1] = '\0';
178                         class_dev = wait_class_device_open(path);
179                         if (class_dev == NULL) {
180                                 dbg("open class device failed");
181                                 goto hotplug;
182                         }
183                         dbg("opened class_dev->name='%s'", class_dev->name);
184
185                         wait_for_class_device(class_dev, &error);
186
187                         /* name, create node, store in db */
188                         retval = udev_add_device(&udev, class_dev);
189
190                         sysfs_close_class_device(class_dev);
191                 } else if (strcmp(action, "remove") == 0) {
192                         /* possibly remove a node */
193                         dbg("udev remove");
194
195                         /* skip subsystems without "dev" */
196                         if (subsystem_expect_no_dev(udev.subsystem)) {
197                                 dbg("don't care about '%s' devices", udev.subsystem);
198                                 goto hotplug;
199                         }
200
201                         udev_rules_get_run(&udev);
202                         if (udev.ignore_device) {
203                                 dbg("device event will be ignored");
204                                 goto hotplug;
205                         }
206
207                         /* get node from db, remove db-entry, delete created node */
208                         retval = udev_remove_device(&udev);
209                 }
210
211                 if (udev.devname[0] != '\0')
212                         setenv("DEVNAME", udev.devname, 1);
213
214                 if (udev_run && !list_empty(&udev.run_list)) {
215                         struct name_entry *name_loop;
216
217                         dbg("executing run list");
218                         list_for_each_entry(name_loop, &udev.run_list, node)
219                                 execute_command(name_loop->name, udev.subsystem);
220                 }
221
222         } else if (udev.type == DEV_DEVICE) {
223                 if (strcmp(action, "add") == 0) {
224                         /* wait for sysfs */
225                         dbg("devices add");
226
227                         snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
228                         path[sizeof(path)-1] = '\0';
229                         devices_dev = wait_devices_device_open(path);
230                         if (!devices_dev) {
231                                 dbg("devices device unavailable (probably remove has beaten us)");
232                                 goto hotplug;
233                         }
234                         dbg("devices device opened '%s'", path);
235
236                         wait_for_devices_device(devices_dev, &error);
237
238                         sysfs_close_device(devices_dev);
239                 } else if (strcmp(action, "remove") == 0) {
240                         dbg("devices remove");
241                 }
242         } else {
243                 dbg("unhandled");
244         }
245
246 hotplug:
247         if (udev_hotplug_d && managed_event)
248                 udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);
249
250         udev_cleanup_device(&udev);
251
252         logging_close();
253         return retval;
254 }