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