chiark / gitweb /
dasd_id: add s390 disk-label prober
[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 static void asmlinkage sig_handler(int signum)
58 {
59         switch (signum) {
60                 case SIGALRM:
61                         exit(1);
62                 case SIGINT:
63                 case SIGTERM:
64                         exit(20 + signum);
65         }
66 }
67
68 int main(int argc, char *argv[], char *envp[])
69 {
70         struct udevice udev;
71         struct udev_rules rules;
72         char path[PATH_SIZE];
73         const char *error;
74         const char *action;
75         const char *devpath;
76         const char *subsystem;
77         struct sigaction act;
78         int retval = -EINVAL;
79
80         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
81                 printf("%s\n", UDEV_VERSION);
82                 exit(0);
83         }
84
85         logging_init("udev");
86         udev_init_config();
87         dbg("version %s", UDEV_VERSION);
88
89         /* set signal handlers */
90         memset(&act, 0x00, sizeof(act));
91         act.sa_handler = (void (*)(int)) sig_handler;
92         sigemptyset (&act.sa_mask);
93         act.sa_flags = 0;
94         sigaction(SIGALRM, &act, NULL);
95         sigaction(SIGINT, &act, NULL);
96         sigaction(SIGTERM, &act, NULL);
97
98         /* trigger timeout to prevent hanging processes */
99         alarm(UDEV_ALARM_TIMEOUT);
100
101         action = getenv("ACTION");
102         devpath = getenv("DEVPATH");
103         subsystem = getenv("SUBSYSTEM");
104         /* older kernels passed the SUBSYSTEM only as argument */
105         if (!subsystem && argc == 2)
106                 subsystem = argv[1];
107
108         if (!action || !subsystem || !devpath) {
109                 err("action, subsystem or devpath missing");
110                 goto exit;
111         }
112
113         /* export log_priority , as called programs may want to do the same as udev */
114         if (udev_log_priority) {
115                 char priority[32];
116
117                 sprintf(priority, "%i", udev_log_priority);
118                 setenv("UDEV_LOG", priority, 1);
119         }
120
121         udev_init_device(&udev, devpath, subsystem, action);
122         udev_rules_init(&rules, 0);
123
124         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS || udev.type == DEV_NET) {
125                 /* handle device node */
126                 if (strcmp(action, "add") == 0) {
127                         struct sysfs_class_device *class_dev;
128
129                         /* wait for sysfs of /sys/class /sys/block */
130                         dbg("node add");
131                         snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
132                         path[sizeof(path)-1] = '\0';
133                         class_dev = wait_class_device_open(path);
134                         if (class_dev == NULL) {
135                                 dbg("open class device failed");
136                                 goto run;
137                         }
138                         dbg("opened class_dev->name='%s'", class_dev->name);
139                         wait_for_class_device(class_dev, &error);
140
141                         /* get major/minor */
142                         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS)
143                                 udev.devt = get_devt(class_dev);
144
145                         if (udev.type == DEV_NET || udev.devt) {
146                                 /* name device */
147                                 udev_rules_get_name(&rules, &udev, class_dev);
148                                 if (udev.ignore_device) {
149                                         info("device event will be ignored");
150                                         goto cleanup;
151                                 }
152                                 if (udev.name[0] == '\0') {
153                                         info("device node creation supressed");
154                                         goto cleanup;
155                                 }
156                                 
157                                 /* create node, store in db */
158                                 retval = udev_add_device(&udev, class_dev);
159                         } else {
160                                 dbg("no dev-file found");
161                                 udev_rules_get_run(&rules, &udev, NULL);
162                                 if (udev.ignore_device) {
163                                         info("device event will be ignored");
164                                         goto cleanup;
165                                 }
166                         }
167                         sysfs_close_class_device(class_dev);
168                 } else if (strcmp(action, "remove") == 0) {
169                         dbg("node remove");
170                         udev_rules_get_run(&rules, &udev, NULL);
171                         if (udev.ignore_device) {
172                                 dbg("device event will be ignored");
173                                 goto cleanup;
174                         }
175
176                         /* get name from db, remove db-entry, delete node */
177                         retval = udev_remove_device(&udev);
178                 }
179
180                 /* export name of device node or netif */
181                 if (udev.devname[0] != '\0')
182                         setenv("DEVNAME", udev.devname, 1);
183         } else if (udev.type == DEV_DEVICE && strcmp(action, "add") == 0) {
184                 struct sysfs_device *devices_dev;
185
186                 /* wait for sysfs of /sys/devices/ */
187                 dbg("devices add");
188                 snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
189                 path[sizeof(path)-1] = '\0';
190                 devices_dev = wait_devices_device_open(path);
191                 if (!devices_dev) {
192                         dbg("devices device unavailable (probably remove has beaten us)");
193                         goto run;
194                 }
195                 dbg("devices device opened '%s'", path);
196                 wait_for_devices_device(devices_dev, &error);
197                 udev_rules_get_run(&rules, &udev, devices_dev);
198                 sysfs_close_device(devices_dev);
199                 if (udev.ignore_device) {
200                         info("device event will be ignored");
201                         goto cleanup;
202                 }
203         } else {
204                 dbg("default handling");
205                 udev_rules_get_run(&rules, &udev, NULL);
206                 if (udev.ignore_device) {
207                         info("device event will be ignored");
208                         goto cleanup;
209                 }
210         }
211
212 run:
213         if (udev_run && !list_empty(&udev.run_list)) {
214                 struct name_entry *name_loop;
215
216                 dbg("executing run list");
217                 list_for_each_entry(name_loop, &udev.run_list, node)
218                         execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
219         }
220
221 cleanup:
222         udev_cleanup_device(&udev);
223
224 exit:
225         logging_close();
226         return retval;
227 }