chiark / gitweb /
[PATCH] remove untrusted chars read from sysfs-values or returned by PROGRAM
[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 hotplug event
58  * for now look if the kernel calls udevsend instead of /sbin/hotplug
59  */
60 static int manage_hotplug_event(void) {
61         char helper[256];
62         int fd;
63         int len;
64
65         /* don't handle hotplug.d if we are called directly */
66         if (!getenv("UDEVD_EVENT"))
67                 return 0;
68
69         fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
70         if (fd < 0)
71                 return 0;
72
73         len = read(fd, helper, 256);
74         close(fd);
75
76         if (len < 0)
77                 return 0;
78         helper[len] = '\0';
79
80         if (strstr(helper, "udevsend"))
81                 return 1;
82
83         return 0;
84 }
85
86 static void asmlinkage sig_handler(int signum)
87 {
88         switch (signum) {
89                 case SIGALRM:
90                         exit(1);
91                 case SIGINT:
92                 case SIGTERM:
93                         exit(20 + signum);
94         }
95 }
96
97 int main(int argc, char *argv[], char *envp[])
98 {
99         struct sysfs_class_device *class_dev;
100         struct sysfs_device *devices_dev;
101         struct udevice udev;
102         char path[PATH_SIZE];
103         const char *error;
104         const char *action;
105         const char *devpath;
106         const char *subsystem;
107         int managed_event;
108         struct sigaction act;
109         int retval = -EINVAL;
110
111         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
112                 printf("%s\n", UDEV_VERSION);
113                 exit(0);
114         }
115
116         logging_init("udev");
117         udev_init_config();
118         dbg("version %s", UDEV_VERSION);
119
120         /* set signal handlers */
121         memset(&act, 0x00, sizeof(act));
122         act.sa_handler = (void (*)(int)) sig_handler;
123         sigemptyset (&act.sa_mask);
124         act.sa_flags = 0;
125         sigaction(SIGALRM, &act, NULL);
126         sigaction(SIGINT, &act, NULL);
127         sigaction(SIGTERM, &act, NULL);
128
129         /* trigger timeout to prevent hanging processes */
130         alarm(ALARM_TIMEOUT);
131
132         /* let the executed programs know if we handle the whole hotplug event */
133         managed_event = manage_hotplug_event();
134         if (managed_event)
135                 setenv("MANAGED_EVENT", "1", 1);
136
137         action = getenv("ACTION");
138         devpath = getenv("DEVPATH");
139         subsystem = getenv("SUBSYSTEM");
140         /* older kernels passed the SUBSYSTEM only as argument */
141         if (!subsystem && argc == 2)
142                 subsystem = argv[1];
143
144         udev_init_device(&udev, devpath, subsystem);
145
146         if (!action || !subsystem || !devpath) {
147                 err("action, subsystem or devpath missing");
148                 goto hotplug;
149         }
150
151         /* export logging flag, as called scripts may want to do the same as udev */
152         if (udev_log_priority) {
153                 char priority[32];
154
155                 sprintf(priority, "%i", udev_log_priority);
156                 setenv("UDEV_LOG", priority, 1);
157         }
158
159         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS || udev.type == DEV_NET) {
160                 if (strcmp(action, "add") == 0) {
161                         /* wait for sysfs and possibly add node */
162                         dbg("udev add");
163
164                         /* skip subsystems without "dev", but handle net devices */
165                         if (udev.type != DEV_NET && subsystem_expect_no_dev(udev.subsystem)) {
166                                 dbg("don't care about '%s' devices", udev.subsystem);
167                                 goto hotplug;
168                         }
169
170                         snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
171                         path[sizeof(path)-1] = '\0';
172                         class_dev = wait_class_device_open(path);
173                         if (class_dev == NULL) {
174                                 dbg("open class device failed");
175                                 goto hotplug;
176                         }
177                         dbg("opened class_dev->name='%s'", class_dev->name);
178
179                         wait_for_class_device(class_dev, &error);
180
181                         /* init rules */
182                         udev_rules_init();
183
184                         /* name, create node, store in db */
185                         retval = udev_add_device(&udev, class_dev);
186
187                         sysfs_close_class_device(class_dev);
188                 } else if (strcmp(action, "remove") == 0) {
189                         /* possibly remove a node */
190                         dbg("udev remove");
191
192                         /* skip subsystems without "dev" */
193                         if (subsystem_expect_no_dev(udev.subsystem)) {
194                                 dbg("don't care about '%s' devices", udev.subsystem);
195                                 goto hotplug;
196                         }
197
198                         /* get node from db, remove db-entry, delete created node */
199                         retval = udev_remove_device(&udev);
200                 }
201
202                 /* run dev.d/ scripts if we created/deleted a node or changed a netif name */
203                 if (udev.devname[0] != '\0') {
204                         setenv("DEVNAME", udev.devname, 1);
205                         if (udev_dev_d)
206                                 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
207                 }
208         } else if (udev.type == DEV_DEVICE) {
209                 if (strcmp(action, "add") == 0) {
210                         /* wait for sysfs */
211                         dbg("devices add");
212
213                         snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
214                         path[sizeof(path)-1] = '\0';
215                         devices_dev = wait_devices_device_open(path);
216                         if (!devices_dev) {
217                                 dbg("devices device unavailable (probably remove has beaten us)");
218                                 goto hotplug;
219                         }
220                         dbg("devices device opened '%s'", path);
221
222                         wait_for_devices_device(devices_dev, &error);
223
224                         sysfs_close_device(devices_dev);
225                 } else if (strcmp(action, "remove") == 0) {
226                         dbg("devices remove");
227                 }
228         } else {
229                 dbg("unhandled");
230         }
231
232 hotplug:
233         udev_cleanup_device(&udev);
234         if (udev_hotplug_d && managed_event)
235                 udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);
236
237         logging_close();
238         return retval;
239 }