chiark / gitweb /
add man pages for *_id programs
[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 #include <syslog.h>
32
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev_libc_wrapper.h"
35 #include "udev.h"
36 #include "udev_utils.h"
37 #include "udev_sysfs.h"
38 #include "udev_version.h"
39 #include "udev_rules.h"
40 #include "logging.h"
41
42 #ifdef USE_LOG
43 void log_message(int priority, const char *format, ...)
44 {
45         va_list args;
46
47         if (priority > udev_log_priority)
48                 return;
49
50         va_start(args, format);
51         vsyslog(priority, format, args);
52         va_end(args);
53 }
54 #endif
55
56 static void asmlinkage sig_handler(int signum)
57 {
58         switch (signum) {
59                 case SIGALRM:
60                         exit(1);
61                 case SIGINT:
62                 case SIGTERM:
63                         exit(20 + signum);
64         }
65 }
66
67 int main(int argc, char *argv[], char *envp[])
68 {
69         struct udevice udev;
70         struct udev_rules rules;
71         const char *action;
72         const char *devpath;
73         const char *subsystem;
74         struct sigaction act;
75         int devnull;
76         int retval = -EINVAL;
77
78         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
79                 printf("%s\n", UDEV_VERSION);
80                 exit(0);
81         }
82
83         /* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
84         devnull = open("/dev/null", O_RDWR);
85         if (devnull >= 0)  {
86                 if (devnull != STDIN_FILENO)
87                         dup2(devnull, STDIN_FILENO);
88                 if (devnull != STDOUT_FILENO)
89                         dup2(devnull, STDOUT_FILENO);
90                 if (devnull != STDERR_FILENO)
91                         dup2(devnull, STDERR_FILENO);
92                 if (devnull > STDERR_FILENO)
93                         close(devnull);
94         }
95
96         logging_init("udev");
97         if (devnull < 0)
98                 err("fatal, could not open /dev/null");
99         udev_init_config();
100         dbg("version %s", UDEV_VERSION);
101
102         /* set signal handlers */
103         memset(&act, 0x00, sizeof(act));
104         act.sa_handler = (void (*)(int)) sig_handler;
105         sigemptyset (&act.sa_mask);
106         act.sa_flags = 0;
107         sigaction(SIGALRM, &act, NULL);
108         sigaction(SIGINT, &act, NULL);
109         sigaction(SIGTERM, &act, NULL);
110
111         /* trigger timeout to prevent hanging processes */
112         alarm(UDEV_ALARM_TIMEOUT);
113
114         action = getenv("ACTION");
115         devpath = getenv("DEVPATH");
116         subsystem = getenv("SUBSYSTEM");
117         /* older kernels passed the SUBSYSTEM only as argument */
118         if (!subsystem && argc == 2)
119                 subsystem = argv[1];
120
121         if (!action || !subsystem || !devpath) {
122                 err("action, subsystem or devpath missing");
123                 goto exit;
124         }
125
126         /* export log_priority , as called programs may want to do the same as udev */
127         if (udev_log_priority) {
128                 char priority[32];
129
130                 sprintf(priority, "%i", udev_log_priority);
131                 setenv("UDEV_LOG", priority, 1);
132         }
133
134         udev_init_device(&udev, devpath, subsystem, action);
135         udev_rules_init(&rules, 1, 0);
136
137         retval = udev_process_event(&rules, &udev);
138
139         if (!retval && udev_run && !list_empty(&udev.run_list)) {
140                 struct name_entry *name_loop;
141
142                 dbg("executing run list");
143                 list_for_each_entry(name_loop, &udev.run_list, node) {
144                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
145                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, action);
146                         else
147                                 run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
148                 }
149         }
150
151         udev_rules_close(&rules);
152         udev_cleanup_device(&udev);
153
154 exit:
155         logging_close();
156         return retval;
157 }