chiark / gitweb /
volume_id: provide a custom debug function
[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 "udev.h"
34 #include "udev_rules.h"
35
36 #ifdef USE_LOG
37 void log_message(int priority, const char *format, ...)
38 {
39         va_list args;
40
41         if (priority > udev_log_priority)
42                 return;
43
44         va_start(args, format);
45         vsyslog(priority, format, args);
46         va_end(args);
47 }
48 #endif
49
50 static void asmlinkage sig_handler(int signum)
51 {
52         switch (signum) {
53                 case SIGALRM:
54                         exit(1);
55                 case SIGINT:
56                 case SIGTERM:
57                         exit(20 + signum);
58         }
59 }
60
61 int main(int argc, char *argv[], char *envp[])
62 {
63         struct sysfs_device *dev;
64         struct udevice *udev;
65         const char *maj, *min;
66         struct udev_rules rules;
67         const char *action;
68         const char *devpath;
69         const char *subsystem;
70         struct sigaction act;
71         int devnull;
72         int retval = -EINVAL;
73
74         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
75                 printf("%s\n", UDEV_VERSION);
76                 exit(0);
77         }
78
79         /* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
80         devnull = open("/dev/null", O_RDWR);
81         if (devnull >= 0)  {
82                 if (devnull != STDIN_FILENO)
83                         dup2(devnull, STDIN_FILENO);
84                 if (devnull != STDOUT_FILENO)
85                         dup2(devnull, STDOUT_FILENO);
86                 if (devnull != STDERR_FILENO)
87                         dup2(devnull, STDERR_FILENO);
88                 if (devnull > STDERR_FILENO)
89                         close(devnull);
90         }
91
92         logging_init("udev");
93         if (devnull < 0)
94                 err("fatal, could not open /dev/null: %s", strerror(errno));
95         udev_config_init();
96         dbg("version %s", UDEV_VERSION);
97
98         /* set signal handlers */
99         memset(&act, 0x00, sizeof(act));
100         act.sa_handler = (void (*)(int)) sig_handler;
101         sigemptyset (&act.sa_mask);
102         act.sa_flags = 0;
103         sigaction(SIGALRM, &act, NULL);
104         sigaction(SIGINT, &act, NULL);
105         sigaction(SIGTERM, &act, NULL);
106
107         /* trigger timeout to prevent hanging processes */
108         alarm(UDEV_ALARM_TIMEOUT);
109
110         action = getenv("ACTION");
111         devpath = getenv("DEVPATH");
112         subsystem = getenv("SUBSYSTEM");
113         /* older kernels passed the SUBSYSTEM only as argument */
114         if (subsystem == NULL && argc == 2)
115                 subsystem = argv[1];
116
117         if (action == NULL || subsystem == NULL || devpath == NULL) {
118                 err("action, subsystem or devpath missing");
119                 goto exit;
120         }
121
122         /* export log_priority , as called programs may want to do the same as udev */
123         if (udev_log_priority) {
124                 char priority[32];
125
126                 sprintf(priority, "%i", udev_log_priority);
127                 setenv("UDEV_LOG", priority, 1);
128         }
129
130         sysfs_init();
131         udev_rules_init(&rules, 0);
132
133         dev = sysfs_device_get(devpath);
134         if (dev == NULL) {
135                 info("unable to open '%s'", devpath);
136                 goto fail;
137         }
138
139         udev = udev_device_init();
140         if (udev == NULL)
141                 goto fail;
142
143         /* override built-in sysfs device */
144         udev->dev = dev;
145         strlcpy(udev->action, action, sizeof(udev->action));
146
147         /* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
148         maj = getenv("MAJOR");
149         min = getenv("MINOR");
150         if (maj != NULL && min != NULL)
151                 udev->devt = makedev(atoi(maj), atoi(min));
152         else
153                 udev->devt = udev_device_get_devt(udev);
154
155         retval = udev_device_event(&rules, udev);
156
157         if (retval == 0 && !udev->ignore_device && udev_run) {
158                 struct name_entry *name_loop;
159
160                 dbg("executing run list");
161                 list_for_each_entry(name_loop, &udev->run_list, node) {
162                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
163                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, action);
164                         else {
165                                 char program[PATH_SIZE];
166
167                                 strlcpy(program, name_loop->name, sizeof(program));
168                                 udev_rules_apply_format(udev, program, sizeof(program));
169                                 run_program(program, udev->dev->subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
170                         }
171                 }
172         }
173
174         udev_device_cleanup(udev);
175 fail:
176         udev_rules_cleanup(&rules);
177         sysfs_cleanup();
178
179 exit:
180         logging_close();
181         if (retval != 0)
182                 return 1;
183         return 0;
184 }