]>
Commit | Line | Data |
---|---|---|
7fafc032 | 1 | /* |
b3518c16 | 2 | * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org> |
2f6cbd19 | 3 | * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca> |
7fafc032 | 4 | * |
7fafc032 KS |
5 | * This program is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation version 2 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
27b77df4 | 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
7fafc032 KS |
17 | * |
18 | */ | |
19 | ||
a695feae | 20 | #include <stddef.h> |
7fafc032 KS |
21 | #include <signal.h> |
22 | #include <unistd.h> | |
23 | #include <errno.h> | |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
085cce37 KS |
27 | #include <ctype.h> |
28 | #include <dirent.h> | |
29 | #include <fcntl.h> | |
40caaeec | 30 | #include <syslog.h> |
0b3dfb3d | 31 | #include <time.h> |
b52a01ee | 32 | #include <getopt.h> |
138068d6 KS |
33 | #include <sys/select.h> |
34 | #include <sys/wait.h> | |
53921bfa KS |
35 | #include <sys/types.h> |
36 | #include <sys/socket.h> | |
37 | #include <sys/un.h> | |
dc117daa | 38 | #include <sys/stat.h> |
c895fd00 | 39 | #include <sys/ioctl.h> |
62821d0d | 40 | #include <linux/types.h> |
88f4b648 | 41 | #include <linux/netlink.h> |
7fafc032 KS |
42 | |
43 | #include "udev.h" | |
c895fd00 | 44 | #include "udev_rules.h" |
7fafc032 | 45 | #include "udevd.h" |
eef7c9a3 | 46 | #include "udev_selinux.h" |
7fafc032 | 47 | |
c7c00276 | 48 | static int debug_trace; |
9e8fe79b KS |
49 | static int verbose; |
50 | ||
1aa1e248 | 51 | static struct udev_rules rules; |
239cc98b KS |
52 | static int udevd_sock = -1; |
53 | static int uevent_netlink_sock = -1; | |
54 | static int inotify_fd = -1; | |
085cce37 | 55 | static pid_t sid; |
13f24d59 | 56 | |
f1ff8d7b | 57 | static int signal_pipe[2] = {-1, -1}; |
5cab7caa | 58 | static volatile int sigchilds_waiting; |
63cc8f04 | 59 | static volatile int udev_exit; |
c895fd00 | 60 | static volatile int reload_config; |
f27125f9 | 61 | static int run_exec_q; |
3b47c739 | 62 | static int stop_exec_q; |
a15f42c4 KS |
63 | static int max_childs; |
64 | static int max_childs_running; | |
916c5e47 | 65 | static char udev_log[32]; |
40caaeec | 66 | |
40caaeec KS |
67 | static LIST_HEAD(exec_list); |
68 | static LIST_HEAD(running_list); | |
a15f42c4 | 69 | |
7fafc032 | 70 | |
6c18b1fb | 71 | #ifdef USE_LOG |
c895fd00 | 72 | void log_message(int priority, const char *format, ...) |
51a8bb2f | 73 | { |
6b493a20 KS |
74 | va_list args; |
75 | ||
76 | if (priority > udev_log_priority) | |
77 | return; | |
d026a35d | 78 | |
9e8fe79b | 79 | if (verbose) { |
3bf1efa8 | 80 | printf("[%d] ", (int) getpid()); |
9e8fe79b KS |
81 | va_start(args, format); |
82 | vprintf(format, args); | |
83 | va_end(args); | |
84 | printf("\n"); | |
3bf1efa8 KS |
85 | } else { |
86 | va_start(args, format); | |
87 | vsyslog(priority, format, args); | |
88 | va_end(args); | |
9e8fe79b | 89 | } |
51a8bb2f | 90 | } |
b52a01ee | 91 | |
d026a35d | 92 | #endif |
51a8bb2f | 93 | |
c895fd00 KS |
94 | static void asmlinkage udev_event_sig_handler(int signum) |
95 | { | |
96 | if (signum == SIGALRM) | |
97 | exit(1); | |
98 | } | |
99 | ||
b3518c16 | 100 | static int udev_event_process(struct udevd_uevent_msg *msg) |
c895fd00 KS |
101 | { |
102 | struct sigaction act; | |
1aa1e248 | 103 | struct udevice *udev; |
c895fd00 KS |
104 | int i; |
105 | int retval; | |
106 | ||
107 | /* set signal handlers */ | |
108 | memset(&act, 0x00, sizeof(act)); | |
109 | act.sa_handler = (void (*)(int)) udev_event_sig_handler; | |
110 | sigemptyset (&act.sa_mask); | |
111 | act.sa_flags = 0; | |
112 | sigaction(SIGALRM, &act, NULL); | |
113 | ||
3e5e8332 KS |
114 | /* reset to default */ |
115 | act.sa_handler = SIG_DFL; | |
116 | sigaction(SIGINT, &act, NULL); | |
117 | sigaction(SIGTERM, &act, NULL); | |
118 | sigaction(SIGCHLD, &act, NULL); | |
119 | sigaction(SIGHUP, &act, NULL); | |
120 | ||
c895fd00 KS |
121 | /* trigger timeout to prevent hanging processes */ |
122 | alarm(UDEV_ALARM_TIMEOUT); | |
123 | ||
1aa1e248 | 124 | /* reconstruct event environment from message */ |
c895fd00 KS |
125 | for (i = 0; msg->envp[i]; i++) |
126 | putenv(msg->envp[i]); | |
127 | ||
31de3a2b | 128 | udev = udev_device_init(NULL); |
1aa1e248 KS |
129 | if (udev == NULL) |
130 | return -1; | |
131 | strlcpy(udev->action, msg->action, sizeof(udev->action)); | |
254efc14 | 132 | sysfs_device_set_values(udev->dev, msg->devpath, msg->subsystem, msg->driver); |
a2f2270e | 133 | udev->devpath_old = msg->devpath_old; |
1aa1e248 KS |
134 | udev->devt = msg->devt; |
135 | ||
136 | retval = udev_device_event(&rules, udev); | |
c895fd00 KS |
137 | |
138 | /* run programs collected by RUN-key*/ | |
274da2b2 KS |
139 | if (retval == 0 && !udev->ignore_device && udev_run) |
140 | retval = udev_rules_run(udev); | |
c895fd00 | 141 | |
1aa1e248 | 142 | udev_device_cleanup(udev); |
f4fc0136 | 143 | return retval; |
c895fd00 KS |
144 | } |
145 | ||
7a770250 KS |
146 | enum event_state { |
147 | EVENT_QUEUED, | |
148 | EVENT_FINISHED, | |
149 | EVENT_FAILED, | |
150 | }; | |
151 | ||
b3518c16 | 152 | static void export_event_state(struct udevd_uevent_msg *msg, enum event_state state) |
7a770250 KS |
153 | { |
154 | char filename[PATH_SIZE]; | |
155 | char filename_failed[PATH_SIZE]; | |
9c6ad9fb | 156 | size_t start; |
b3518c16 | 157 | struct udevd_uevent_msg *loop_msg; |
051445e0 | 158 | int fd; |
7a770250 | 159 | |
a2f2270e | 160 | /* location of queue file */ |
7a770250 KS |
161 | strlcpy(filename, udev_root, sizeof(filename)); |
162 | strlcat(filename, "/", sizeof(filename)); | |
fc6da921 | 163 | start = strlcat(filename, EVENT_QUEUE_DIR"/", sizeof(filename)); |
9c6ad9fb | 164 | strlcat(filename, msg->devpath, sizeof(filename)); |
fc6da921 | 165 | path_encode(&filename[start], sizeof(filename) - start); |
7a770250 | 166 | |
a2f2270e | 167 | /* location of failed file */ |
7a770250 KS |
168 | strlcpy(filename_failed, udev_root, sizeof(filename_failed)); |
169 | strlcat(filename_failed, "/", sizeof(filename_failed)); | |
fc6da921 | 170 | start = strlcat(filename_failed, EVENT_FAILED_DIR"/", sizeof(filename_failed)); |
9c6ad9fb | 171 | strlcat(filename_failed, msg->devpath, sizeof(filename_failed)); |
fc6da921 | 172 | path_encode(&filename_failed[start], sizeof(filename) - start); |
7a770250 KS |
173 | |
174 | switch (state) { | |
175 | case EVENT_QUEUED: | |
176 | unlink(filename_failed); | |
1b75f109 | 177 | delete_path(filename_failed); |
a2f2270e | 178 | |
7a770250 | 179 | create_path(filename); |
051445e0 KS |
180 | fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); |
181 | if (fd > 0) | |
182 | close(fd); | |
7a770250 KS |
183 | return; |
184 | case EVENT_FINISHED: | |
1b75f109 | 185 | case EVENT_FAILED: |
a2f2270e KS |
186 | if (msg->devpath_old != NULL) { |
187 | /* "move" event - rename failed file to current name, do not delete failed */ | |
188 | char filename_failed_old[PATH_SIZE]; | |
189 | ||
190 | strlcpy(filename_failed_old, udev_root, sizeof(filename_failed_old)); | |
191 | strlcat(filename_failed_old, "/", sizeof(filename_failed_old)); | |
192 | start = strlcat(filename_failed_old, EVENT_FAILED_DIR"/", sizeof(filename_failed_old)); | |
193 | strlcat(filename_failed_old, msg->devpath_old, sizeof(filename_failed_old)); | |
194 | path_encode(&filename_failed_old[start], sizeof(filename) - start); | |
195 | ||
196 | if (rename(filename_failed_old, filename_failed) == 0) | |
197 | info("renamed devpath, moved failed state of '%s' to %s'", | |
198 | msg->devpath_old, msg->devpath); | |
199 | } else { | |
200 | unlink(filename_failed); | |
201 | delete_path(filename_failed); | |
202 | } | |
7a770250 | 203 | |
a2f2270e | 204 | /* skip if events for the same path are still pending */ |
7a770250 KS |
205 | list_for_each_entry(loop_msg, &running_list, node) |
206 | if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0) | |
207 | return; | |
1b75f109 | 208 | |
1a4e5303 KS |
209 | list_for_each_entry(loop_msg, &exec_list, node) |
210 | if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0) | |
211 | return; | |
212 | ||
a2f2270e | 213 | /* move failed event to the failed directory */ |
1b75f109 SJR |
214 | if (state == EVENT_FAILED) { |
215 | create_path(filename_failed); | |
216 | rename(filename, filename_failed); | |
217 | } else { | |
218 | unlink(filename); | |
219 | } | |
220 | ||
a2f2270e | 221 | /* clean up possibly empty queue directory */ |
1b75f109 SJR |
222 | delete_path(filename); |
223 | ||
7a770250 KS |
224 | return; |
225 | } | |
226 | } | |
227 | ||
b3518c16 | 228 | static void msg_queue_delete(struct udevd_uevent_msg *msg) |
fc465079 KS |
229 | { |
230 | list_del(&msg->node); | |
7a770250 | 231 | |
a2f2270e | 232 | /* mark as failed, if "add" event returns non-zero */ |
7a770250 KS |
233 | if (msg->exitstatus && strcmp(msg->action, "add") == 0) |
234 | export_event_state(msg, EVENT_FAILED); | |
235 | else | |
236 | export_event_state(msg, EVENT_FINISHED); | |
237 | ||
fc465079 KS |
238 | free(msg); |
239 | } | |
240 | ||
b3518c16 | 241 | static void udev_event_run(struct udevd_uevent_msg *msg) |
7fafc032 | 242 | { |
90c210eb | 243 | pid_t pid; |
f4fc0136 | 244 | int retval; |
90c210eb KS |
245 | |
246 | pid = fork(); | |
247 | switch (pid) { | |
248 | case 0: | |
33db4b8d | 249 | /* child */ |
833b3c68 KS |
250 | close(uevent_netlink_sock); |
251 | close(udevd_sock); | |
90cd961e | 252 | if (inotify_fd >= 0) |
c895fd00 | 253 | close(inotify_fd); |
c895fd00 KS |
254 | close(signal_pipe[READ_END]); |
255 | close(signal_pipe[WRITE_END]); | |
f602ccf0 | 256 | logging_close(); |
c895fd00 KS |
257 | |
258 | logging_init("udevd-event"); | |
085cce37 | 259 | setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY); |
b437ec95 | 260 | |
f4fc0136 | 261 | retval = udev_event_process(msg); |
4110664d | 262 | info("seq %llu finished with %i", msg->seqnum, retval); |
c895fd00 KS |
263 | |
264 | logging_close(); | |
f4fc0136 KS |
265 | if (retval) |
266 | exit(1); | |
c895fd00 | 267 | exit(0); |
90c210eb | 268 | case -1: |
ff3e4bed | 269 | err("fork of child failed: %s", strerror(errno)); |
ebfc1acd | 270 | msg_queue_delete(msg); |
2f6cbd19 | 271 | break; |
90c210eb | 272 | default: |
2f6cbd19 | 273 | /* get SIGCHLD in main loop */ |
40caaeec | 274 | info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old", |
0b3dfb3d | 275 | msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time); |
2f6cbd19 | 276 | msg->pid = pid; |
90c210eb | 277 | } |
7fafc032 KS |
278 | } |
279 | ||
b3518c16 | 280 | static void msg_queue_insert(struct udevd_uevent_msg *msg) |
fc465079 | 281 | { |
7baada47 KS |
282 | char filename[PATH_SIZE]; |
283 | int fd; | |
284 | ||
fc465079 | 285 | msg->queue_time = time(NULL); |
7baada47 KS |
286 | |
287 | strlcpy(filename, udev_root, sizeof(filename)); | |
288 | strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename)); | |
289 | fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); | |
90cd961e | 290 | if (fd >= 0) { |
7baada47 KS |
291 | char str[32]; |
292 | int len; | |
293 | ||
294 | len = sprintf(str, "%llu\n", msg->seqnum); | |
295 | write(fd, str, len); | |
296 | close(fd); | |
297 | } | |
fc465079 | 298 | |
7a770250 | 299 | export_event_state(msg, EVENT_QUEUED); |
a2f2270e | 300 | info("seq %llu forked, '%s' '%s'", msg->seqnum, msg->action, msg->subsystem); |
7a770250 | 301 | |
c7c00276 KS |
302 | /* run one event after the other in debug mode */ |
303 | if (debug_trace) { | |
304 | list_add_tail(&msg->node, &running_list); | |
305 | udev_event_run(msg); | |
306 | waitpid(msg->pid, NULL, 0); | |
307 | msg_queue_delete(msg); | |
308 | return; | |
309 | } | |
310 | ||
fc465079 KS |
311 | /* run all events with a timeout set immediately */ |
312 | if (msg->timeout != 0) { | |
313 | list_add_tail(&msg->node, &running_list); | |
314 | udev_event_run(msg); | |
315 | return; | |
316 | } | |
317 | ||
318 | list_add_tail(&msg->node, &exec_list); | |
319 | run_exec_q = 1; | |
320 | } | |
321 | ||
f051e340 KS |
322 | static int mem_size_mb(void) |
323 | { | |
9f1f67b1 KS |
324 | FILE* f; |
325 | char buf[4096]; | |
326 | long int memsize = -1; | |
f051e340 | 327 | |
9f1f67b1 KS |
328 | f = fopen("/proc/meminfo", "r"); |
329 | if (f == NULL) | |
f051e340 | 330 | return -1; |
f051e340 | 331 | |
9f1f67b1 KS |
332 | while (fgets(buf, sizeof(buf), f) != NULL) { |
333 | long int value; | |
f051e340 | 334 | |
9f1f67b1 KS |
335 | if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) { |
336 | memsize = value / 1024; | |
337 | break; | |
338 | } | |
339 | } | |
f051e340 | 340 | |
fdc9a0b5 | 341 | fclose(f); |
9f1f67b1 | 342 | return memsize; |
f051e340 KS |
343 | } |
344 | ||
345 | static int cpu_count(void) | |
346 | { | |
9f1f67b1 KS |
347 | FILE* f; |
348 | char buf[4096]; | |
f051e340 KS |
349 | int count = 0; |
350 | ||
9f1f67b1 KS |
351 | f = fopen("/proc/stat", "r"); |
352 | if (f == NULL) | |
f051e340 | 353 | return -1; |
f051e340 | 354 | |
9f1f67b1 KS |
355 | while (fgets(buf, sizeof(buf), f) != NULL) { |
356 | if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) | |
f051e340 | 357 | count++; |
f051e340 KS |
358 | } |
359 | ||
9f1f67b1 | 360 | fclose(f); |
f051e340 KS |
361 | if (count == 0) |
362 | return -1; | |
363 | return count; | |
364 | } | |
365 | ||
085cce37 KS |
366 | static int running_processes(void) |
367 | { | |
9f1f67b1 KS |
368 | FILE* f; |
369 | char buf[4096]; | |
370 | int running = -1; | |
085cce37 | 371 | |
9f1f67b1 KS |
372 | f = fopen("/proc/stat", "r"); |
373 | if (f == NULL) | |
085cce37 | 374 | return -1; |
085cce37 | 375 | |
9f1f67b1 KS |
376 | while (fgets(buf, sizeof(buf), f) != NULL) { |
377 | int value; | |
085cce37 | 378 | |
9f1f67b1 KS |
379 | if (sscanf(buf, "procs_running %u", &value) == 1) { |
380 | running = value; | |
381 | break; | |
382 | } | |
383 | } | |
085cce37 | 384 | |
9f1f67b1 | 385 | fclose(f); |
085cce37 KS |
386 | return running; |
387 | } | |
388 | ||
389 | /* return the number of process es in our session, count only until limit */ | |
390 | static int running_processes_in_session(pid_t session, int limit) | |
391 | { | |
392 | DIR *dir; | |
393 | struct dirent *dent; | |
394 | int running = 0; | |
395 | ||
396 | dir = opendir("/proc"); | |
397 | if (!dir) | |
398 | return -1; | |
399 | ||
400 | /* read process info from /proc */ | |
401 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { | |
402 | int f; | |
403 | char procdir[64]; | |
404 | char line[256]; | |
405 | const char *pos; | |
406 | char state; | |
407 | pid_t ppid, pgrp, sess; | |
408 | int len; | |
409 | ||
410 | if (!isdigit(dent->d_name[0])) | |
411 | continue; | |
412 | ||
413 | snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name); | |
414 | procdir[sizeof(procdir)-1] = '\0'; | |
415 | ||
416 | f = open(procdir, O_RDONLY); | |
417 | if (f == -1) | |
418 | continue; | |
419 | ||
b4f192f0 | 420 | len = read(f, line, sizeof(line)-1); |
085cce37 KS |
421 | close(f); |
422 | ||
423 | if (len <= 0) | |
424 | continue; | |
425 | else | |
426 | line[len] = '\0'; | |
427 | ||
428 | /* skip ugly program name */ | |
429 | pos = strrchr(line, ')') + 2; | |
430 | if (pos == NULL) | |
431 | continue; | |
432 | ||
433 | if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4) | |
434 | continue; | |
435 | ||
436 | /* count only processes in our session */ | |
437 | if (sess != session) | |
438 | continue; | |
439 | ||
440 | /* count only running, no sleeping processes */ | |
441 | if (state != 'R') | |
442 | continue; | |
443 | ||
444 | running++; | |
445 | if (limit > 0 && running >= limit) | |
446 | break; | |
447 | } | |
448 | closedir(dir); | |
449 | ||
450 | return running; | |
451 | } | |
452 | ||
7b6571a9 KS |
453 | static int compare_devpath(const char *running, const char *waiting) |
454 | { | |
455 | int i; | |
456 | ||
63f61c5c | 457 | for (i = 0; i < PATH_SIZE; i++) { |
7b6571a9 KS |
458 | /* identical device event found */ |
459 | if (running[i] == '\0' && waiting[i] == '\0') | |
460 | return 1; | |
461 | ||
462 | /* parent device event found */ | |
463 | if (running[i] == '\0' && waiting[i] == '/') | |
464 | return 2; | |
465 | ||
466 | /* child device event found */ | |
467 | if (running[i] == '/' && waiting[i] == '\0') | |
468 | return 3; | |
469 | ||
470 | /* no matching event */ | |
471 | if (running[i] != waiting[i]) | |
472 | break; | |
473 | } | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
fc630eda KS |
478 | /* lookup event for identical, parent, child, or physical device */ |
479 | static int devpath_busy(struct udevd_uevent_msg *msg, int limit) | |
7fafc032 | 480 | { |
b3518c16 | 481 | struct udevd_uevent_msg *loop_msg; |
a15f42c4 | 482 | int childs_count = 0; |
7b6571a9 | 483 | |
fc630eda KS |
484 | /* check exec-queue which may still contain delayed events we depend on */ |
485 | list_for_each_entry(loop_msg, &exec_list, node) { | |
486 | /* skip ourself and all later events */ | |
487 | if (loop_msg->seqnum >= msg->seqnum) | |
488 | break; | |
489 | ||
a2f2270e KS |
490 | /* check our old name */ |
491 | if (msg->devpath_old != NULL) | |
492 | if (strcmp(loop_msg->devpath , msg->devpath_old) == 0) | |
493 | return 2; | |
494 | ||
fc630eda KS |
495 | /* check identical, parent, or child device event */ |
496 | if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) { | |
497 | dbg("%llu, device event still pending %llu (%s)", | |
498 | msg->seqnum, loop_msg->seqnum, loop_msg->devpath); | |
a2f2270e | 499 | return 3; |
fc630eda KS |
500 | } |
501 | ||
502 | /* check physical device event (special case of parent) */ | |
503 | if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0) | |
504 | if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) { | |
505 | dbg("%llu, physical device event still pending %llu (%s)", | |
506 | msg->seqnum, loop_msg->seqnum, loop_msg->devpath); | |
a2f2270e | 507 | return 4; |
fc630eda KS |
508 | } |
509 | } | |
510 | ||
1113044b | 511 | /* check run queue for still running events */ |
f8a178a3 | 512 | list_for_each_entry(loop_msg, &running_list, node) { |
a15f42c4 | 513 | if (limit && childs_count++ > limit) { |
fc630eda | 514 | dbg("%llu, maximum number (%i) of childs reached", msg->seqnum, childs_count); |
a15f42c4 KS |
515 | return 1; |
516 | } | |
80513ea3 | 517 | |
a2f2270e KS |
518 | /* check our old name */ |
519 | if (msg->devpath_old != NULL) | |
520 | if (strcmp(loop_msg->devpath , msg->devpath_old) == 0) | |
521 | return 2; | |
522 | ||
fc630eda | 523 | /* check identical, parent, or child device event */ |
a15f42c4 | 524 | if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) { |
fc630eda | 525 | dbg("%llu, device event still running %llu (%s)", |
a15f42c4 | 526 | msg->seqnum, loop_msg->seqnum, loop_msg->devpath); |
a2f2270e | 527 | return 3; |
a15f42c4 | 528 | } |
79721e0a | 529 | |
fc630eda | 530 | /* check physical device event (special case of parent) */ |
79721e0a | 531 | if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0) |
a15f42c4 KS |
532 | if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) { |
533 | dbg("%llu, physical device event still running %llu (%s)", | |
534 | msg->seqnum, loop_msg->seqnum, loop_msg->devpath); | |
a2f2270e | 535 | return 4; |
a15f42c4 | 536 | } |
80513ea3 | 537 | } |
a15f42c4 | 538 | return 0; |
7fafc032 KS |
539 | } |
540 | ||
fc630eda | 541 | /* serializes events for the identical and parent and child devices */ |
fc465079 | 542 | static void msg_queue_manager(void) |
7fafc032 | 543 | { |
b3518c16 KS |
544 | struct udevd_uevent_msg *loop_msg; |
545 | struct udevd_uevent_msg *tmp_msg; | |
085cce37 KS |
546 | int running; |
547 | ||
8ab44e3f KS |
548 | if (list_empty(&exec_list)) |
549 | return; | |
550 | ||
085cce37 KS |
551 | running = running_processes(); |
552 | dbg("%d processes runnning on system", running); | |
553 | if (running < 0) | |
a15f42c4 | 554 | running = max_childs_running; |
53921bfa | 555 | |
f8a178a3 | 556 | list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) { |
085cce37 | 557 | /* check running processes in our session and possibly throttle */ |
a15f42c4 KS |
558 | if (running >= max_childs_running) { |
559 | running = running_processes_in_session(sid, max_childs_running+10); | |
560 | dbg("at least %d processes running in session", running); | |
561 | if (running >= max_childs_running) { | |
fc465079 | 562 | dbg("delay seq %llu, too many processes already running", loop_msg->seqnum); |
085cce37 KS |
563 | return; |
564 | } | |
565 | } | |
566 | ||
fc630eda KS |
567 | /* serialize and wait for parent or child events */ |
568 | if (devpath_busy(loop_msg, max_childs) != 0) { | |
a15f42c4 | 569 | dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath); |
fc465079 KS |
570 | continue; |
571 | } | |
572 | ||
573 | /* move event to run list */ | |
574 | list_move_tail(&loop_msg->node, &running_list); | |
575 | udev_event_run(loop_msg); | |
576 | running++; | |
577 | dbg("moved seq %llu to running list", loop_msg->seqnum); | |
53921bfa KS |
578 | } |
579 | } | |
580 | ||
b3518c16 | 581 | static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size) |
88f4b648 KS |
582 | { |
583 | int bufpos; | |
584 | int i; | |
b3518c16 | 585 | struct udevd_uevent_msg *msg; |
220dac4c | 586 | char *physdevdriver_key = NULL; |
5780be9e KS |
587 | int maj = 0; |
588 | int min = 0; | |
88f4b648 | 589 | |
b3518c16 | 590 | msg = malloc(sizeof(struct udevd_uevent_msg) + buf_size); |
88f4b648 KS |
591 | if (msg == NULL) |
592 | return NULL; | |
b3518c16 | 593 | memset(msg, 0x00, sizeof(struct udevd_uevent_msg) + buf_size); |
88f4b648 KS |
594 | |
595 | /* copy environment buffer and reconstruct envp */ | |
596 | memcpy(msg->envbuf, buf, buf_size); | |
597 | bufpos = 0; | |
3b47c739 | 598 | for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) { |
88f4b648 KS |
599 | int keylen; |
600 | char *key; | |
601 | ||
602 | key = &msg->envbuf[bufpos]; | |
603 | keylen = strlen(key); | |
604 | msg->envp[i] = key; | |
605 | bufpos += keylen + 1; | |
606 | dbg("add '%s' to msg.envp[%i]", msg->envp[i], i); | |
607 | ||
608 | /* remember some keys for further processing */ | |
609 | if (strncmp(key, "ACTION=", 7) == 0) | |
610 | msg->action = &key[7]; | |
ebfc1acd | 611 | else if (strncmp(key, "DEVPATH=", 8) == 0) |
88f4b648 | 612 | msg->devpath = &key[8]; |
ebfc1acd | 613 | else if (strncmp(key, "SUBSYSTEM=", 10) == 0) |
88f4b648 | 614 | msg->subsystem = &key[10]; |
254efc14 KS |
615 | else if (strncmp(key, "DRIVER=", 7) == 0) |
616 | msg->driver = &key[7]; | |
ebfc1acd | 617 | else if (strncmp(key, "SEQNUM=", 7) == 0) |
88f4b648 | 618 | msg->seqnum = strtoull(&key[7], NULL, 10); |
a2f2270e KS |
619 | else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) |
620 | msg->devpath_old = &key[12]; | |
ebfc1acd | 621 | else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) |
88f4b648 | 622 | msg->physdevpath = &key[12]; |
220dac4c KS |
623 | else if (strncmp(key, "PHYSDEVDRIVER=", 14) == 0) |
624 | physdevdriver_key = key; | |
ebfc1acd | 625 | else if (strncmp(key, "MAJOR=", 6) == 0) |
5780be9e | 626 | maj = strtoull(&key[6], NULL, 10); |
ebfc1acd | 627 | else if (strncmp(key, "MINOR=", 6) == 0) |
5780be9e | 628 | min = strtoull(&key[6], NULL, 10); |
ebfc1acd | 629 | else if (strncmp(key, "TIMEOUT=", 8) == 0) |
88f4b648 KS |
630 | msg->timeout = strtoull(&key[8], NULL, 10); |
631 | } | |
5780be9e | 632 | msg->devt = makedev(maj, min); |
88f4b648 | 633 | msg->envp[i++] = "UDEVD_EVENT=1"; |
220dac4c KS |
634 | |
635 | if (msg->driver == NULL && msg->physdevpath == NULL && physdevdriver_key != NULL) { | |
636 | /* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */ | |
637 | msg->envp[i++] = &physdevdriver_key[7]; | |
638 | msg->driver = &physdevdriver_key[14]; | |
639 | } | |
640 | ||
88f4b648 KS |
641 | msg->envp[i] = NULL; |
642 | ||
0ec819d9 KS |
643 | if (msg->devpath == NULL || msg->action == NULL) { |
644 | info("DEVPATH or ACTION missing, ignore message"); | |
e825b59b KS |
645 | free(msg); |
646 | return NULL; | |
647 | } | |
88f4b648 KS |
648 | return msg; |
649 | } | |
650 | ||
3b47c739 | 651 | /* receive the udevd message from userspace */ |
b3518c16 | 652 | static void get_ctrl_msg(void) |
7fafc032 | 653 | { |
b3518c16 | 654 | struct udevd_ctrl_msg ctrl_msg; |
4a231017 | 655 | ssize_t size; |
0028653c KS |
656 | struct msghdr smsg; |
657 | struct cmsghdr *cmsg; | |
658 | struct iovec iov; | |
659 | struct ucred *cred; | |
660 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; | |
8ab44e3f | 661 | int *intval; |
1343a952 | 662 | char *pos; |
a695feae | 663 | |
b3518c16 | 664 | memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg)); |
5005ca59 | 665 | iov.iov_base = &ctrl_msg; |
b3518c16 | 666 | iov.iov_len = sizeof(struct udevd_ctrl_msg); |
0028653c KS |
667 | |
668 | memset(&smsg, 0x00, sizeof(struct msghdr)); | |
669 | smsg.msg_iov = &iov; | |
670 | smsg.msg_iovlen = 1; | |
671 | smsg.msg_control = cred_msg; | |
672 | smsg.msg_controllen = sizeof(cred_msg); | |
673 | ||
3b47c739 | 674 | size = recvmsg(udevd_sock, &smsg, 0); |
4a231017 | 675 | if (size < 0) { |
2f6cbd19 | 676 | if (errno != EINTR) |
a2f87fdd | 677 | err("unable to receive user udevd message: %s", strerror(errno)); |
b437ec95 | 678 | return; |
53921bfa | 679 | } |
0028653c KS |
680 | cmsg = CMSG_FIRSTHDR(&smsg); |
681 | cred = (struct ucred *) CMSG_DATA(cmsg); | |
682 | ||
7b1cbec9 | 683 | if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { |
199cdd86 | 684 | err("no sender credentials received, message ignored"); |
b437ec95 | 685 | return; |
7b1cbec9 KS |
686 | } |
687 | ||
0028653c | 688 | if (cred->uid != 0) { |
199cdd86 | 689 | err("sender uid=%i, message ignored", cred->uid); |
b437ec95 | 690 | return; |
4a231017 KS |
691 | } |
692 | ||
b3518c16 | 693 | if (strncmp(ctrl_msg.magic, UDEVD_CTRL_MAGIC, sizeof(UDEVD_CTRL_MAGIC)) != 0 ) { |
5005ca59 | 694 | err("message magic '%s' doesn't match, ignore it", ctrl_msg.magic); |
b437ec95 | 695 | return; |
0028653c KS |
696 | } |
697 | ||
5005ca59 | 698 | switch (ctrl_msg.type) { |
1343a952 HH |
699 | case UDEVD_CTRL_ENV: |
700 | pos = strchr(ctrl_msg.buf, '='); | |
701 | if (pos == NULL) { | |
702 | err("wrong key format '%s'", ctrl_msg.buf); | |
703 | break; | |
704 | } | |
705 | pos[0] = '\0'; | |
706 | if (pos[1] == '\0') { | |
707 | info("udevd message (ENV) received, unset '%s'", ctrl_msg.buf); | |
708 | unsetenv(ctrl_msg.buf); | |
709 | } else { | |
710 | info("udevd message (ENV) received, set '%s=%s'", ctrl_msg.buf, &pos[1]); | |
711 | setenv(ctrl_msg.buf, &pos[1], 1); | |
712 | } | |
713 | break; | |
b3518c16 | 714 | case UDEVD_CTRL_STOP_EXEC_QUEUE: |
8ab44e3f | 715 | info("udevd message (STOP_EXEC_QUEUE) received"); |
3b47c739 KS |
716 | stop_exec_q = 1; |
717 | break; | |
b3518c16 | 718 | case UDEVD_CTRL_START_EXEC_QUEUE: |
8ab44e3f | 719 | info("udevd message (START_EXEC_QUEUE) received"); |
3b47c739 | 720 | stop_exec_q = 0; |
fc465079 | 721 | msg_queue_manager(); |
3b47c739 | 722 | break; |
b3518c16 KS |
723 | case UDEVD_CTRL_SET_LOG_LEVEL: |
724 | intval = (int *) ctrl_msg.buf; | |
8ab44e3f KS |
725 | info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval); |
726 | udev_log_priority = *intval; | |
916c5e47 KS |
727 | sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority); |
728 | putenv(udev_log); | |
8ab44e3f | 729 | break; |
b3518c16 KS |
730 | case UDEVD_CTRL_SET_MAX_CHILDS: |
731 | intval = (int *) ctrl_msg.buf; | |
8ab44e3f KS |
732 | info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval); |
733 | max_childs = *intval; | |
734 | break; | |
f051e340 KS |
735 | case UDEVD_CTRL_SET_MAX_CHILDS_RUNNING: |
736 | intval = (int *) ctrl_msg.buf; | |
737 | info("udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs=%i", *intval); | |
738 | max_childs_running = *intval; | |
739 | break; | |
b3518c16 | 740 | case UDEVD_CTRL_RELOAD_RULES: |
c895fd00 KS |
741 | info("udevd message (RELOAD_RULES) received"); |
742 | reload_config = 1; | |
743 | break; | |
3b47c739 | 744 | default: |
f051e340 | 745 | err("unknown control message type"); |
3b47c739 | 746 | } |
88f4b648 | 747 | } |
4a231017 | 748 | |
88f4b648 | 749 | /* receive the kernel user event message and do some sanity checks */ |
b3518c16 | 750 | static struct udevd_uevent_msg *get_netlink_msg(void) |
88f4b648 | 751 | { |
b3518c16 | 752 | struct udevd_uevent_msg *msg; |
88f4b648 KS |
753 | int bufpos; |
754 | ssize_t size; | |
7d1e179f | 755 | static char buffer[UEVENT_BUFFER_SIZE+512]; |
88f4b648 | 756 | char *pos; |
4a231017 | 757 | |
8ab44e3f | 758 | size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0); |
88f4b648 KS |
759 | if (size < 0) { |
760 | if (errno != EINTR) | |
a2f87fdd | 761 | err("unable to receive kernel netlink message: %s", strerror(errno)); |
88f4b648 KS |
762 | return NULL; |
763 | } | |
4a231017 | 764 | |
88f4b648 KS |
765 | if ((size_t)size > sizeof(buffer)-1) |
766 | size = sizeof(buffer)-1; | |
767 | buffer[size] = '\0'; | |
ebfc1acd | 768 | dbg("uevent_size=%zi", size); |
4a231017 | 769 | |
88f4b648 KS |
770 | /* start of event payload */ |
771 | bufpos = strlen(buffer)+1; | |
772 | msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos); | |
773 | if (msg == NULL) | |
774 | return NULL; | |
4a231017 | 775 | |
88f4b648 KS |
776 | /* validate message */ |
777 | pos = strchr(buffer, '@'); | |
778 | if (pos == NULL) { | |
199cdd86 | 779 | err("invalid uevent '%s'", buffer); |
88f4b648 KS |
780 | free(msg); |
781 | return NULL; | |
782 | } | |
783 | pos[0] = '\0'; | |
79721e0a | 784 | |
88f4b648 | 785 | if (msg->action == NULL) { |
dfedc446 | 786 | info("no ACTION in payload found, skip event '%s'", buffer); |
88f4b648 KS |
787 | free(msg); |
788 | return NULL; | |
789 | } | |
7f7ae03a | 790 | |
88f4b648 | 791 | if (strcmp(msg->action, buffer) != 0) { |
199cdd86 | 792 | err("ACTION in payload does not match uevent, skip event '%s'", buffer); |
88f4b648 KS |
793 | free(msg); |
794 | return NULL; | |
53921bfa | 795 | } |
a695feae | 796 | |
021a294c | 797 | return msg; |
a695feae | 798 | } |
1c5c245e | 799 | |
e5a5b54a | 800 | static void asmlinkage sig_handler(int signum) |
7fafc032 | 801 | { |
53921bfa KS |
802 | switch (signum) { |
803 | case SIGINT: | |
804 | case SIGTERM: | |
63cc8f04 | 805 | udev_exit = 1; |
53921bfa | 806 | break; |
2f6cbd19 | 807 | case SIGCHLD: |
f27125f9 | 808 | /* set flag, then write to pipe if needed */ |
5cab7caa | 809 | sigchilds_waiting = 1; |
2f6cbd19 | 810 | break; |
c895fd00 KS |
811 | case SIGHUP: |
812 | reload_config = 1; | |
813 | break; | |
f27125f9 | 814 | } |
5a73b25f | 815 | |
c6303c13 KS |
816 | /* write to pipe, which will wakeup select() in our mainloop */ |
817 | write(signal_pipe[WRITE_END], "", 1); | |
33db4b8d | 818 | } |
7fafc032 | 819 | |
f4fc0136 | 820 | static void udev_done(int pid, int exitstatus) |
2f6cbd19 KS |
821 | { |
822 | /* find msg associated with pid and delete it */ | |
b3518c16 | 823 | struct udevd_uevent_msg *msg; |
2f6cbd19 | 824 | |
f8a178a3 | 825 | list_for_each_entry(msg, &running_list, node) { |
2f6cbd19 | 826 | if (msg->pid == pid) { |
f4fc0136 KS |
827 | info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid, |
828 | exitstatus, time(NULL) - msg->queue_time); | |
829 | msg->exitstatus = exitstatus; | |
ebfc1acd | 830 | msg_queue_delete(msg); |
3169e8d1 | 831 | |
fc465079 | 832 | /* there may be events waiting with the same devpath */ |
f27125f9 | 833 | run_exec_q = 1; |
2f6cbd19 KS |
834 | return; |
835 | } | |
836 | } | |
837 | } | |
838 | ||
5cab7caa | 839 | static void reap_sigchilds(void) |
f27125f9 | 840 | { |
40caaeec | 841 | pid_t pid; |
f4fc0136 | 842 | int status; |
ce043f85 | 843 | |
40caaeec | 844 | while (1) { |
f4fc0136 | 845 | pid = waitpid(-1, &status, WNOHANG); |
40caaeec | 846 | if (pid <= 0) |
f27125f9 | 847 | break; |
f4fc0136 KS |
848 | if (WIFEXITED(status)) |
849 | status = WEXITSTATUS(status); | |
82de5983 KS |
850 | else if (WIFSIGNALED(status)) |
851 | status = WTERMSIG(status) + 128; | |
f4fc0136 KS |
852 | else |
853 | status = 0; | |
854 | udev_done(pid, status); | |
f27125f9 | 855 | } |
856 | } | |
857 | ||
3b47c739 | 858 | static int init_udevd_socket(void) |
33db4b8d | 859 | { |
53921bfa | 860 | struct sockaddr_un saddr; |
1dadabd7 | 861 | socklen_t addrlen; |
5cab7caa | 862 | const int feature_on = 1; |
c2cf4012 KS |
863 | int retval; |
864 | ||
865 | memset(&saddr, 0x00, sizeof(saddr)); | |
866 | saddr.sun_family = AF_LOCAL; | |
867 | /* use abstract namespace for socket path */ | |
b3518c16 | 868 | strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH); |
c2cf4012 KS |
869 | addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1; |
870 | ||
3b47c739 KS |
871 | udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0); |
872 | if (udevd_sock == -1) { | |
ff3e4bed | 873 | err("error getting socket: %s", strerror(errno)); |
c2cf4012 KS |
874 | return -1; |
875 | } | |
876 | ||
877 | /* the bind takes care of ensuring only one copy running */ | |
3b47c739 | 878 | retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen); |
c2cf4012 | 879 | if (retval < 0) { |
ff3e4bed | 880 | err("bind failed: %s", strerror(errno)); |
239cc98b KS |
881 | close(udevd_sock); |
882 | udevd_sock = -1; | |
c2cf4012 KS |
883 | return -1; |
884 | } | |
885 | ||
886 | /* enable receiving of the sender credentials */ | |
3b47c739 | 887 | setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on)); |
c2cf4012 KS |
888 | |
889 | return 0; | |
890 | } | |
891 | ||
8ab44e3f | 892 | static int init_uevent_netlink_sock(void) |
88f4b648 KS |
893 | { |
894 | struct sockaddr_nl snl; | |
a5c606f6 | 895 | const int buffersize = 16 * 1024 * 1024; |
88f4b648 KS |
896 | int retval; |
897 | ||
898 | memset(&snl, 0x00, sizeof(struct sockaddr_nl)); | |
899 | snl.nl_family = AF_NETLINK; | |
900 | snl.nl_pid = getpid(); | |
733f070d | 901 | snl.nl_groups = 1; |
88f4b648 | 902 | |
8ab44e3f KS |
903 | uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); |
904 | if (uevent_netlink_sock == -1) { | |
ff3e4bed | 905 | err("error getting socket: %s", strerror(errno)); |
88f4b648 KS |
906 | return -1; |
907 | } | |
908 | ||
cbbde2ba | 909 | /* set receive buffersize */ |
a5c606f6 | 910 | setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize)); |
cbbde2ba | 911 | |
833b3c68 | 912 | retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl)); |
88f4b648 | 913 | if (retval < 0) { |
ff3e4bed | 914 | err("bind failed: %s", strerror(errno)); |
8ab44e3f KS |
915 | close(uevent_netlink_sock); |
916 | uevent_netlink_sock = -1; | |
88f4b648 KS |
917 | return -1; |
918 | } | |
88f4b648 KS |
919 | return 0; |
920 | } | |
921 | ||
90cd961e KS |
922 | static void export_initial_seqnum(void) |
923 | { | |
924 | char filename[PATH_SIZE]; | |
925 | int fd; | |
926 | char seqnum[32]; | |
927 | ssize_t len = 0; | |
928 | ||
929 | strlcpy(filename, sysfs_path, sizeof(filename)); | |
930 | strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename)); | |
931 | fd = open(filename, O_RDONLY); | |
932 | if (fd >= 0) { | |
933 | len = read(fd, seqnum, sizeof(seqnum)-1); | |
934 | close(fd); | |
935 | } | |
936 | if (len <= 0) { | |
937 | strcpy(seqnum, "0\n"); | |
938 | len = 3; | |
939 | } | |
940 | strlcpy(filename, udev_root, sizeof(filename)); | |
941 | strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename)); | |
0e385fee | 942 | create_path(filename); |
90cd961e KS |
943 | fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); |
944 | if (fd >= 0) { | |
945 | write(fd, seqnum, len); | |
946 | close(fd); | |
947 | } | |
948 | } | |
949 | ||
c2cf4012 KS |
950 | int main(int argc, char *argv[], char *envp[]) |
951 | { | |
c2cf4012 | 952 | int retval; |
3904a758 | 953 | int fd; |
f8911dbb | 954 | struct sigaction act; |
f27125f9 | 955 | fd_set readfds; |
a15f42c4 | 956 | const char *value; |
561d4c5a | 957 | int daemonize = 0; |
b52a01ee KS |
958 | int option; |
959 | static const struct option options[] = { | |
960 | { "daemon", 0, NULL, 'd' }, | |
c7c00276 | 961 | { "debug-trace", 0, NULL, 't' }, |
9e8fe79b | 962 | { "verbose", 0, NULL, 'v' }, |
b52a01ee | 963 | { "help", 0, NULL, 'h' }, |
841e168c | 964 | { "version", 0, NULL, 'V' }, |
b52a01ee KS |
965 | {} |
966 | }; | |
e3396a2d | 967 | int rc = 1; |
0b3dfb3d | 968 | int maxfd; |
53921bfa | 969 | |
7257cb18 | 970 | logging_init("udevd"); |
1aa1e248 | 971 | udev_config_init(); |
eef7c9a3 | 972 | selinux_init(); |
896e5aa9 | 973 | dbg("version %s", UDEV_VERSION); |
95a6f4c8 | 974 | |
b52a01ee | 975 | while (1) { |
841e168c | 976 | option = getopt_long(argc, argv, "dtvhV", options, NULL); |
b52a01ee KS |
977 | if (option == -1) |
978 | break; | |
979 | ||
980 | switch (option) { | |
981 | case 'd': | |
561d4c5a | 982 | daemonize = 1; |
b52a01ee | 983 | break; |
c7c00276 KS |
984 | case 't': |
985 | debug_trace = 1; | |
986 | break; | |
9e8fe79b KS |
987 | case 'v': |
988 | verbose = 1; | |
989 | if (udev_log_priority < LOG_INFO) | |
990 | udev_log_priority = LOG_INFO; | |
991 | break; | |
b52a01ee | 992 | case 'h': |
841e168c MS |
993 | printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--verbose] [--version]\n"); |
994 | goto exit; | |
995 | case 'V': | |
996 | printf("%s\n", UDEV_VERSION); | |
e3396a2d | 997 | goto exit; |
b52a01ee KS |
998 | default: |
999 | goto exit; | |
561d4c5a | 1000 | } |
561d4c5a | 1001 | } |
40caaeec | 1002 | |
fc89fe7e KS |
1003 | if (getuid() != 0) { |
1004 | fprintf(stderr, "root privileges required\n"); | |
1005 | err("root privileges required"); | |
1006 | goto exit; | |
1007 | } | |
1008 | ||
5edec024 MS |
1009 | /* make sure std{in,out,err} fd's are in a sane state */ |
1010 | fd = open("/dev/null", O_RDWR); | |
1011 | if (fd < 0) { | |
1012 | fprintf(stderr, "cannot open /dev/null\n"); | |
1013 | err("cannot open /dev/null"); | |
1014 | } | |
1015 | if (fd > STDIN_FILENO) | |
1016 | dup2(fd, STDIN_FILENO); | |
1017 | if (write(STDOUT_FILENO, 0, 0) < 0) | |
1018 | dup2(fd, STDOUT_FILENO); | |
1019 | if (write(STDERR_FILENO, 0, 0) < 0) | |
1020 | dup2(fd, STDERR_FILENO); | |
1021 | ||
833b3c68 KS |
1022 | /* init sockets to receive events */ |
1023 | if (init_udevd_socket() < 0) { | |
1024 | if (errno == EADDRINUSE) { | |
e3396a2d KS |
1025 | fprintf(stderr, "another udev daemon already running\n"); |
1026 | err("another udev daemon already running"); | |
833b3c68 KS |
1027 | rc = 1; |
1028 | } else { | |
e3396a2d KS |
1029 | fprintf(stderr, "error initializing udevd socket\n"); |
1030 | err("error initializing udevd socket"); | |
833b3c68 KS |
1031 | rc = 2; |
1032 | } | |
1033 | goto exit; | |
1034 | } | |
1035 | ||
1036 | if (init_uevent_netlink_sock() < 0) { | |
e3396a2d KS |
1037 | fprintf(stderr, "error initializing netlink socket\n"); |
1038 | err("error initializing netlink socket"); | |
833b3c68 KS |
1039 | rc = 3; |
1040 | goto exit; | |
1041 | } | |
1042 | ||
ff2eecef SV |
1043 | /* setup signal handler pipe */ |
1044 | retval = pipe(signal_pipe); | |
1045 | if (retval < 0) { | |
1046 | err("error getting pipes: %s", strerror(errno)); | |
1047 | goto exit; | |
1048 | } | |
1049 | ||
1050 | retval = fcntl(signal_pipe[READ_END], F_GETFL, 0); | |
1051 | if (retval < 0) { | |
1052 | err("error fcntl on read pipe: %s", strerror(errno)); | |
1053 | goto exit; | |
1054 | } | |
1055 | retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK); | |
1056 | if (retval < 0) { | |
1057 | err("error fcntl on read pipe: %s", strerror(errno)); | |
1058 | goto exit; | |
1059 | } | |
1060 | ||
1061 | retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0); | |
1062 | if (retval < 0) { | |
1063 | err("error fcntl on write pipe: %s", strerror(errno)); | |
1064 | goto exit; | |
1065 | } | |
1066 | retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK); | |
1067 | if (retval < 0) { | |
1068 | err("error fcntl on write pipe: %s", strerror(errno)); | |
1069 | goto exit; | |
1070 | } | |
1071 | ||
e3396a2d | 1072 | /* parse the rules and keep them in memory */ |
1aa1e248 | 1073 | sysfs_init(); |
287814b2 | 1074 | udev_rules_init(&rules, 1); |
833b3c68 | 1075 | |
90cd961e KS |
1076 | export_initial_seqnum(); |
1077 | ||
561d4c5a | 1078 | if (daemonize) { |
f15515b5 KS |
1079 | pid_t pid; |
1080 | ||
1081 | pid = fork(); | |
1082 | switch (pid) { | |
1083 | case 0: | |
833b3c68 | 1084 | dbg("daemonized fork running"); |
f15515b5 KS |
1085 | break; |
1086 | case -1: | |
ff3e4bed | 1087 | err("fork of daemon failed: %s", strerror(errno)); |
833b3c68 | 1088 | rc = 4; |
f15515b5 KS |
1089 | goto exit; |
1090 | default: | |
833b3c68 | 1091 | dbg("child [%u] running, parent exits", pid); |
2f64aa40 | 1092 | rc = 0; |
833b3c68 | 1093 | goto exit; |
f15515b5 KS |
1094 | } |
1095 | } | |
1096 | ||
5edec024 MS |
1097 | /* redirect std{out,err} fd's */ |
1098 | if (!verbose) | |
1099 | dup2(fd, STDOUT_FILENO); | |
1100 | dup2(fd, STDERR_FILENO); | |
1101 | if (fd > STDERR_FILENO) | |
1102 | close(fd); | |
e3396a2d | 1103 | |
3904a758 | 1104 | /* set scheduling priority for the daemon */ |
085cce37 KS |
1105 | setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY); |
1106 | ||
3904a758 | 1107 | chdir("/"); |
74adec7d | 1108 | umask(022); |
3904a758 KS |
1109 | |
1110 | /* become session leader */ | |
1111 | sid = setsid(); | |
1112 | dbg("our session is %d", sid); | |
1113 | ||
1114 | /* OOM_DISABLE == -17 */ | |
1115 | fd = open("/proc/self/oom_adj", O_RDWR); | |
1116 | if (fd < 0) | |
ff3e4bed | 1117 | err("error disabling OOM: %s", strerror(errno)); |
3904a758 KS |
1118 | else { |
1119 | write(fd, "-17", 3); | |
1120 | close(fd); | |
1121 | } | |
1122 | ||
f27125f9 | 1123 | /* set signal handlers */ |
0786e8e5 | 1124 | memset(&act, 0x00, sizeof(struct sigaction)); |
6b493a20 | 1125 | act.sa_handler = (void (*)(int)) sig_handler; |
f27125f9 | 1126 | sigemptyset(&act.sa_mask); |
f8911dbb KS |
1127 | act.sa_flags = SA_RESTART; |
1128 | sigaction(SIGINT, &act, NULL); | |
1129 | sigaction(SIGTERM, &act, NULL); | |
f8911dbb | 1130 | sigaction(SIGCHLD, &act, NULL); |
63cc8f04 | 1131 | sigaction(SIGHUP, &act, NULL); |
7fafc032 | 1132 | |
c895fd00 KS |
1133 | /* watch rules directory */ |
1134 | inotify_fd = inotify_init(); | |
254d6d3c KS |
1135 | if (inotify_fd >= 0) { |
1136 | char filename[PATH_MAX]; | |
1137 | ||
9dd0c257 | 1138 | inotify_add_watch(inotify_fd, udev_rules_dir, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE); |
254d6d3c KS |
1139 | |
1140 | /* watch dynamic rules directory */ | |
1141 | strlcpy(filename, udev_root, sizeof(filename)); | |
1142 | strlcat(filename, "/"RULES_DYN_DIR, sizeof(filename)); | |
1143 | inotify_add_watch(inotify_fd, filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE); | |
1144 | } else if (errno == ENOSYS) | |
1145 | err("the kernel does not support inotify, udevd can't monitor rules file changes"); | |
750d10da MI |
1146 | else |
1147 | err("inotify_init failed: %s", strerror(errno)); | |
0028653c | 1148 | |
a15f42c4 KS |
1149 | /* maximum limit of forked childs */ |
1150 | value = getenv("UDEVD_MAX_CHILDS"); | |
1151 | if (value) | |
1152 | max_childs = strtoul(value, NULL, 10); | |
f051e340 KS |
1153 | else { |
1154 | int memsize = mem_size_mb(); | |
1155 | if (memsize > 0) | |
1156 | max_childs = 128 + (memsize / 4); | |
1157 | else | |
1158 | max_childs = UDEVD_MAX_CHILDS; | |
1159 | } | |
a15f42c4 KS |
1160 | info("initialize max_childs to %u", max_childs); |
1161 | ||
1162 | /* start to throttle forking if maximum number of _running_ childs is reached */ | |
1163 | value = getenv("UDEVD_MAX_CHILDS_RUNNING"); | |
1164 | if (value) | |
1165 | max_childs_running = strtoull(value, NULL, 10); | |
f051e340 KS |
1166 | else { |
1167 | int cpus = cpu_count(); | |
1168 | if (cpus > 0) | |
1169 | max_childs_running = 8 + (8 * cpus); | |
1170 | else | |
1171 | max_childs_running = UDEVD_MAX_CHILDS_RUNNING; | |
1172 | } | |
a15f42c4 | 1173 | info("initialize max_childs_running to %u", max_childs_running); |
8b726878 | 1174 | |
c895fd00 KS |
1175 | /* clear environment for forked event processes */ |
1176 | clearenv(); | |
1177 | ||
40caaeec | 1178 | /* export log_priority , as called programs may want to follow that setting */ |
916c5e47 KS |
1179 | sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority); |
1180 | putenv(udev_log); | |
c7c00276 KS |
1181 | if (debug_trace) |
1182 | putenv("DEBUG=1"); | |
40caaeec | 1183 | |
0b3dfb3d KS |
1184 | maxfd = udevd_sock; |
1185 | maxfd = UDEV_MAX(maxfd, uevent_netlink_sock); | |
1186 | maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]); | |
1187 | maxfd = UDEV_MAX(maxfd, inotify_fd); | |
1188 | ||
63cc8f04 | 1189 | while (!udev_exit) { |
b3518c16 | 1190 | struct udevd_uevent_msg *msg; |
40caaeec | 1191 | int fdcount; |
021a294c | 1192 | |
40caaeec | 1193 | FD_ZERO(&readfds); |
f1ff8d7b | 1194 | FD_SET(signal_pipe[READ_END], &readfds); |
40caaeec | 1195 | FD_SET(udevd_sock, &readfds); |
833b3c68 | 1196 | FD_SET(uevent_netlink_sock, &readfds); |
90cd961e | 1197 | if (inotify_fd >= 0) |
c895fd00 | 1198 | FD_SET(inotify_fd, &readfds); |
e5a2989e | 1199 | |
0b3dfb3d | 1200 | fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL); |
40caaeec | 1201 | if (fdcount < 0) { |
e5a2989e | 1202 | if (errno != EINTR) |
df4e89bf | 1203 | err("error in select: %s", strerror(errno)); |
f27125f9 | 1204 | continue; |
2f6cbd19 | 1205 | } |
e5a2989e | 1206 | |
b3518c16 | 1207 | /* get control message */ |
b437ec95 | 1208 | if (FD_ISSET(udevd_sock, &readfds)) |
b3518c16 | 1209 | get_ctrl_msg(); |
88f4b648 | 1210 | |
b3518c16 | 1211 | /* get netlink message */ |
833b3c68 | 1212 | if (FD_ISSET(uevent_netlink_sock, &readfds)) { |
8ab44e3f | 1213 | msg = get_netlink_msg(); |
0b3dfb3d | 1214 | if (msg) |
021a294c KS |
1215 | msg_queue_insert(msg); |
1216 | } | |
e5a2989e | 1217 | |
63cc8f04 | 1218 | /* received a signal, clear our notification pipe */ |
c6303c13 KS |
1219 | if (FD_ISSET(signal_pipe[READ_END], &readfds)) { |
1220 | char buf[256]; | |
1221 | ||
1222 | read(signal_pipe[READ_END], &buf, sizeof(buf)); | |
40caaeec | 1223 | } |
e5a2989e | 1224 | |
c895fd00 | 1225 | /* rules directory inotify watch */ |
90cd961e | 1226 | if ((inotify_fd >= 0) && FD_ISSET(inotify_fd, &readfds)) { |
c895fd00 KS |
1227 | int nbytes; |
1228 | ||
1229 | /* discard all possible events, we can just reload the config */ | |
254d6d3c | 1230 | if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes > 0) { |
c895fd00 KS |
1231 | char *buf; |
1232 | ||
1233 | reload_config = 1; | |
1234 | buf = malloc(nbytes); | |
f4dce170 | 1235 | if (buf == NULL) { |
c895fd00 KS |
1236 | err("error getting buffer for inotify, disable watching"); |
1237 | close(inotify_fd); | |
1238 | inotify_fd = -1; | |
1239 | } | |
1240 | read(inotify_fd, buf, nbytes); | |
1241 | free(buf); | |
1242 | } | |
1243 | } | |
1244 | ||
e3396a2d | 1245 | /* rules changed, set by inotify or a HUP signal */ |
c895fd00 KS |
1246 | if (reload_config) { |
1247 | reload_config = 0; | |
1aa1e248 | 1248 | udev_rules_cleanup(&rules); |
287814b2 | 1249 | udev_rules_init(&rules, 1); |
c895fd00 KS |
1250 | } |
1251 | ||
0b3dfb3d | 1252 | /* forked child has returned */ |
5cab7caa KS |
1253 | if (sigchilds_waiting) { |
1254 | sigchilds_waiting = 0; | |
1255 | reap_sigchilds(); | |
f27125f9 | 1256 | } |
e5a2989e | 1257 | |
f27125f9 | 1258 | if (run_exec_q) { |
f27125f9 | 1259 | run_exec_q = 0; |
3b47c739 | 1260 | if (!stop_exec_q) |
fc465079 | 1261 | msg_queue_manager(); |
53921bfa | 1262 | } |
53921bfa | 1263 | } |
e3396a2d | 1264 | rc = 0; |
ec9cc012 | 1265 | |
53921bfa | 1266 | exit: |
1aa1e248 KS |
1267 | udev_rules_cleanup(&rules); |
1268 | sysfs_cleanup(); | |
456cb387 | 1269 | selinux_exit(); |
c895fd00 | 1270 | |
90cd961e | 1271 | if (signal_pipe[READ_END] >= 0) |
f1ff8d7b | 1272 | close(signal_pipe[READ_END]); |
90cd961e | 1273 | if (signal_pipe[WRITE_END] >= 0) |
f1ff8d7b | 1274 | close(signal_pipe[WRITE_END]); |
2b996ad1 | 1275 | |
90cd961e | 1276 | if (udevd_sock >= 0) |
63cc8f04 | 1277 | close(udevd_sock); |
90cd961e | 1278 | if (inotify_fd >= 0) |
c895fd00 | 1279 | close(inotify_fd); |
90cd961e | 1280 | if (uevent_netlink_sock >= 0) |
63cc8f04 KS |
1281 | close(uevent_netlink_sock); |
1282 | ||
7257cb18 | 1283 | logging_close(); |
63cc8f04 | 1284 | |
833b3c68 | 1285 | return rc; |
7fafc032 | 1286 | } |