chiark / gitweb /
add firmware_helper to load firmware
[elogind.git] / udev.c
1 /*
2  * udev.c
3  *
4  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <unistd.h>
31
32 #include "libsysfs/sysfs/libsysfs.h"
33 #include "udev_libc_wrapper.h"
34 #include "udev.h"
35 #include "udev_utils.h"
36 #include "udev_sysfs.h"
37 #include "udev_version.h"
38 #include "udev_rules.h"
39 #include "logging.h"
40
41 #ifdef USE_LOG
42 void log_message(int priority, const char *format, ...)
43 {
44         va_list args;
45
46         if (priority > udev_log_priority)
47                 return;
48
49         va_start(args, format);
50         vsyslog(priority, format, args);
51         va_end(args);
52 }
53 #endif
54
55 static void asmlinkage sig_handler(int signum)
56 {
57         switch (signum) {
58                 case SIGALRM:
59                         exit(1);
60                 case SIGINT:
61                 case SIGTERM:
62                         exit(20 + signum);
63         }
64 }
65
66 int main(int argc, char *argv[], char *envp[])
67 {
68         struct udevice udev;
69         struct udev_rules rules;
70         const char *action;
71         const char *devpath;
72         const char *subsystem;
73         struct sigaction act;
74         int retval = -EINVAL;
75
76         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
77                 printf("%s\n", UDEV_VERSION);
78                 exit(0);
79         }
80
81         logging_init("udev");
82         udev_init_config();
83         dbg("version %s", UDEV_VERSION);
84
85         /* set signal handlers */
86         memset(&act, 0x00, sizeof(act));
87         act.sa_handler = (void (*)(int)) sig_handler;
88         sigemptyset (&act.sa_mask);
89         act.sa_flags = 0;
90         sigaction(SIGALRM, &act, NULL);
91         sigaction(SIGINT, &act, NULL);
92         sigaction(SIGTERM, &act, NULL);
93
94         /* trigger timeout to prevent hanging processes */
95         alarm(UDEV_ALARM_TIMEOUT);
96
97         action = getenv("ACTION");
98         devpath = getenv("DEVPATH");
99         subsystem = getenv("SUBSYSTEM");
100         /* older kernels passed the SUBSYSTEM only as argument */
101         if (!subsystem && argc == 2)
102                 subsystem = argv[1];
103
104         if (!action || !subsystem || !devpath) {
105                 err("action, subsystem or devpath missing");
106                 goto exit;
107         }
108
109         /* export log_priority , as called programs may want to do the same as udev */
110         if (udev_log_priority) {
111                 char priority[32];
112
113                 sprintf(priority, "%i", udev_log_priority);
114                 setenv("UDEV_LOG", priority, 1);
115         }
116
117         udev_init_device(&udev, devpath, subsystem, action);
118         udev_rules_init(&rules, 1, 0);
119
120         retval = udev_process_event(&rules, &udev);
121
122         if (!retval && udev_run && !list_empty(&udev.run_list)) {
123                 struct name_entry *name_loop;
124
125                 dbg("executing run list");
126                 list_for_each_entry(name_loop, &udev.run_list, node) {
127                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
128                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, action);
129                         else
130                                 execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
131                 }
132         }
133
134         udev_rules_close(&rules);
135         udev_cleanup_device(&udev);
136
137 exit:
138         logging_close();
139         return retval;
140 }