]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev/udevd.c
Remap MSI Laptop touchpad on/off key to F22 and F23
[thirdparty/systemd.git] / udev / udevd.c
CommitLineData
7fafc032 1/*
1e03b754 2 * Copyright (C) 2004-2009 Kay Sievers <kay.sievers@vrfy.org>
2f6cbd19 3 * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
bb38678e
SJR
4 * Copyright (C) 2009 Canonical Ltd.
5 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
7fafc032 6 *
55e9959b
KS
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
7fafc032 11 *
55e9959b
KS
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
7fafc032 16 *
55e9959b
KS
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7fafc032
KS
19 */
20
a695feae 21#include <stddef.h>
7fafc032
KS
22#include <signal.h>
23#include <unistd.h>
24#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
c3804728 27#include <stdbool.h>
7fafc032 28#include <string.h>
085cce37 29#include <ctype.h>
085cce37 30#include <fcntl.h>
0b3dfb3d 31#include <time.h>
b52a01ee 32#include <getopt.h>
78230c0d 33#include <dirent.h>
820fc48f 34#include <sys/time.h>
1e03b754
KS
35#include <sys/prctl.h>
36#include <sys/socket.h>
a65aa40f 37#include <sys/un.h>
1e03b754 38#include <sys/signalfd.h>
138068d6 39#include <sys/select.h>
3210a72b 40#include <sys/poll.h>
138068d6 41#include <sys/wait.h>
dc117daa 42#include <sys/stat.h>
c895fd00 43#include <sys/ioctl.h>
01618658 44#include <sys/inotify.h>
761dfddc 45#include <sys/utsname.h>
7fafc032
KS
46
47#include "udev.h"
392ef7a2 48#include "sd-daemon.h"
7fafc032 49
d59f11e1
KS
50#define UDEVD_PRIORITY -4
51#define UDEV_PRIORITY -2
52
c3804728 53static bool debug;
9e8fe79b 54
7d563a17
KS
55static void log_fn(struct udev *udev, int priority,
56 const char *file, int line, const char *fn,
57 const char *format, va_list args)
58{
59 if (debug) {
820fc48f
KS
60 char buf[1024];
61 struct timeval tv;
62 struct timezone tz;
63
64 vsnprintf(buf, sizeof(buf), format, args);
65 gettimeofday(&tv, &tz);
66 fprintf(stderr, "%llu.%06u [%u] %s: %s",
67 (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec,
68 (int) getpid(), fn, buf);
7d563a17
KS
69 } else {
70 vsyslog(priority, format, args);
71 }
72}
73
d7ddce18 74static struct udev_rules *rules;
f503f6b2 75static struct udev_queue_export *udev_queue_export;
d59f11e1 76static struct udev_ctrl *udev_ctrl;
1e03b754
KS
77static struct udev_monitor *monitor;
78static int worker_watch[2];
79static pid_t settle_pid;
c3804728
KS
80static bool stop_exec_queue;
81static bool reload_config;
87d55ff6
KS
82static int children;
83static int children_max;
c830e98d 84static int exec_delay;
d412a685 85static sigset_t orig_sigmask;
0bc74ea7 86static struct udev_list_node event_list;
1e03b754 87static struct udev_list_node worker_list;
c3804728 88static bool udev_exit;
1e03b754
KS
89static volatile sig_atomic_t worker_exit;
90
91enum poll_fd {
92 FD_CONTROL,
93 FD_NETLINK,
94 FD_INOTIFY,
95 FD_SIGNAL,
96 FD_WORKER,
97};
98
99static struct pollfd pfd[] = {
100 [FD_NETLINK] = { .events = POLLIN },
101 [FD_WORKER] = { .events = POLLIN },
102 [FD_SIGNAL] = { .events = POLLIN },
103 [FD_INOTIFY] = { .events = POLLIN },
104 [FD_CONTROL] = { .events = POLLIN },
105};
106
107enum event_state {
108 EVENT_UNDEF,
109 EVENT_QUEUED,
110 EVENT_RUNNING,
111};
112
113struct event {
114 struct udev_list_node node;
115 struct udev *udev;
116 struct udev_device *dev;
117 enum event_state state;
118 int exitcode;
119 unsigned long long int delaying_seqnum;
120 unsigned long long int seqnum;
121 const char *devpath;
122 size_t devpath_len;
123 const char *devpath_old;
19711e19
KS
124 dev_t devnum;
125 bool is_block;
fc416258 126 int ifindex;
1e03b754
KS
127};
128
129static struct event *node_to_event(struct udev_list_node *node)
7e027927
KS
130{
131 char *event;
132
133 event = (char *)node;
1e03b754
KS
134 event -= offsetof(struct event, node);
135 return (struct event *)event;
136}
137
138enum worker_state {
139 WORKER_UNDEF,
140 WORKER_RUNNING,
141 WORKER_IDLE,
142 WORKER_KILLED,
143};
144
145struct worker {
146 struct udev_list_node node;
bc113de9
KS
147 struct udev *udev;
148 int refcount;
1e03b754
KS
149 pid_t pid;
150 struct udev_monitor *monitor;
151 enum worker_state state;
152 struct event *event;
153};
154
155/* passed from worker to main process */
156struct worker_message {
157 pid_t pid;
158 int exitcode;
159};
160
161static struct worker *node_to_worker(struct udev_list_node *node)
162{
163 char *worker;
164
165 worker = (char *)node;
166 worker -= offsetof(struct worker, node);
167 return (struct worker *)worker;
7e027927
KS
168}
169
1e03b754 170static void event_queue_delete(struct event *event)
fc465079 171{
7e027927 172 udev_list_node_remove(&event->node);
7a770250 173
a2f2270e 174 /* mark as failed, if "add" event returns non-zero */
4b06c409 175 if (event->exitcode != 0 && strcmp(udev_device_get_action(event->dev), "remove") != 0)
f503f6b2 176 udev_queue_export_device_failed(udev_queue_export, event->dev);
7a770250 177 else
f503f6b2 178 udev_queue_export_device_finished(udev_queue_export, event->dev);
aa8734ff 179
45798927 180 info(event->udev, "seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
aa8734ff 181 udev_device_unref(event->dev);
1e03b754 182 free(event);
aa8734ff 183}
7a770250 184
619b97ff 185static void event_sig_handler(int signum)
aa8734ff 186{
1e03b754
KS
187 switch (signum) {
188 case SIGALRM:
619b97ff 189 _exit(1);
1e03b754
KS
190 break;
191 case SIGTERM:
c3804728 192 worker_exit = true;
1e03b754
KS
193 break;
194 }
195}
196
bc113de9
KS
197static struct worker *worker_ref(struct worker *worker)
198{
199 worker->refcount++;
200 return worker;
201}
202
1e03b754
KS
203static void worker_unref(struct worker *worker)
204{
bc113de9
KS
205 worker->refcount--;
206 if (worker->refcount > 0)
207 return;
208
209 udev_list_node_remove(&worker->node);
1e03b754 210 udev_monitor_unref(worker->monitor);
87d55ff6 211 children--;
bc113de9 212 info(worker->udev, "worker [%u] cleaned up\n", worker->pid);
1e03b754 213 free(worker);
fc465079
KS
214}
215
1e03b754 216static void worker_new(struct event *event)
7fafc032 217{
1e03b754
KS
218 struct worker *worker;
219 struct udev_monitor *worker_monitor;
90c210eb 220 pid_t pid;
aa8734ff 221 struct sigaction act;
ce449f89 222
1e03b754
KS
223 /* listen for new events */
224 worker_monitor = udev_monitor_new_from_netlink(event->udev, NULL);
225 if (worker_monitor == NULL)
226 return;
227 /* allow the main daemon netlink address to send devices to the worker */
228 udev_monitor_allow_unicast_sender(worker_monitor, monitor);
229 udev_monitor_enable_receiving(worker_monitor);
230
231 worker = calloc(1, sizeof(struct worker));
1851332c
YK
232 if (worker == NULL) {
233 udev_monitor_unref(worker_monitor);
1e03b754 234 return;
1851332c 235 }
bc113de9
KS
236 /* worker + event reference */
237 worker->refcount = 2;
238 worker->udev = event->udev;
0bc74ea7 239
90c210eb
KS
240 pid = fork();
241 switch (pid) {
1e03b754 242 case 0: {
adda4c68 243 sigset_t sigmask;
1e03b754 244 struct udev_device *dev;
adda4c68
KS
245 struct pollfd pmon = {
246 .fd = udev_monitor_get_fd(worker_monitor),
247 .events = POLLIN,
248 };
1e03b754 249
f503f6b2 250 udev_queue_export_unref(udev_queue_export);
9290143d 251 udev_monitor_unref(monitor);
d59f11e1 252 udev_ctrl_unref(udev_ctrl);
1e03b754
KS
253 close(pfd[FD_SIGNAL].fd);
254 close(worker_watch[READ_END]);
9060b066
KS
255 udev_log_close();
256 udev_log_init("udevd-work");
085cce37 257 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
b437ec95 258
aa8734ff
KS
259 /* set signal handlers */
260 memset(&act, 0x00, sizeof(act));
619b97ff 261 act.sa_handler = event_sig_handler;
aa8734ff
KS
262 sigemptyset (&act.sa_mask);
263 act.sa_flags = 0;
1e03b754 264 sigaction(SIGTERM, &act, NULL);
aa8734ff
KS
265 sigaction(SIGALRM, &act, NULL);
266
adda4c68
KS
267 /* unblock SIGALRM */
268 sigfillset(&sigmask);
269 sigdelset(&sigmask, SIGALRM);
270 sigprocmask(SIG_SETMASK, &sigmask, NULL);
271 /* SIGTERM is unblocked in ppoll() */
272 sigdelset(&sigmask, SIGTERM);
aa8734ff 273
1e03b754
KS
274 /* request TERM signal if parent exits */
275 prctl(PR_SET_PDEATHSIG, SIGTERM);
aa8734ff 276
1e03b754
KS
277 /* initial device */
278 dev = event->dev;
aa8734ff 279
adda4c68 280 do {
1e03b754 281 struct udev_event *udev_event;
f7c5b04f 282 struct worker_message msg = {};
1e03b754 283 int err;
f7c5b04f 284 int failed = 0;
aa8734ff 285
820fc48f 286 info(event->udev, "seq %llu running\n", udev_device_get_seqnum(dev));
1e03b754
KS
287 udev_event = udev_event_new(dev);
288 if (udev_event == NULL)
289 _exit(3);
0bc74ea7 290
1e03b754
KS
291 /* set timeout to prevent hanging processes */
292 alarm(UDEV_EVENT_TIMEOUT);
293
c830e98d
KS
294 if (exec_delay > 0)
295 udev_event->exec_delay = exec_delay;
296
1e03b754
KS
297 /* apply rules, create node, symlinks */
298 err = udev_event_execute_rules(udev_event, rules);
299
300 /* rules may change/disable the timeout */
301 if (udev_device_get_event_timeout(dev) >= 0)
302 alarm(udev_device_get_event_timeout(dev));
303
f46d2e54
KS
304 if (err == 0)
305 failed = udev_event_execute_run(udev_event, &orig_sigmask);
1e03b754 306
1e03b754
KS
307 alarm(0);
308
309 /* apply/restore inotify watch */
310 if (err == 0 && udev_event->inotify_watch) {
311 udev_watch_begin(udev_event->udev, dev);
312 udev_device_update_db(dev);
313 }
5ae82640 314
1e03b754
KS
315 /* send processed event back to libudev listeners */
316 udev_monitor_send_device(worker_monitor, NULL, dev);
11625409 317
f46d2e54 318 /* send udevd the result of the event execution */
f7c5b04f
KS
319 if (err != 0)
320 msg.exitcode = err;
321 else if (failed != 0)
322 msg.exitcode = failed;
1e03b754
KS
323 msg.pid = getpid();
324 send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
325
adda4c68
KS
326 info(event->udev, "seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
327 udev_event_unref(udev_event);
328 udev_device_unref(dev);
329 dev = NULL;
330
331 /* wait for more device messages or signal from udevd */
332 while (!worker_exit) {
333 int fdcount;
334
335 fdcount = ppoll(&pmon, 1, NULL, &sigmask);
336 if (fdcount < 0)
337 continue;
338
339 if (pmon.revents & POLLIN) {
340 dev = udev_monitor_receive_device(worker_monitor);
341 if (dev != NULL)
342 break;
343 }
344 }
345 } while (dev != NULL);
1e03b754
KS
346
347 udev_monitor_unref(worker_monitor);
9060b066 348 udev_log_close();
c895fd00 349 exit(0);
1e03b754 350 }
90c210eb 351 case -1:
1e03b754
KS
352 udev_monitor_unref(worker_monitor);
353 event->state = EVENT_QUEUED;
354 free(worker);
aa8734ff 355 err(event->udev, "fork of child failed: %m\n");
2f6cbd19 356 break;
90c210eb 357 default:
1e03b754
KS
358 /* close monitor, but keep address around */
359 udev_monitor_disconnect(worker_monitor);
360 worker->monitor = worker_monitor;
361 worker->pid = pid;
362 worker->state = WORKER_RUNNING;
363 worker->event = event;
364 event->state = EVENT_RUNNING;
365 udev_list_node_append(&worker->node, &worker_list);
87d55ff6 366 children++;
12bc9c54 367 info(event->udev, "seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
1e03b754 368 break;
90c210eb 369 }
7fafc032
KS
370}
371
665ee17d 372static void event_run(struct event *event, bool force)
fc465079 373{
1e03b754
KS
374 struct udev_list_node *loop;
375
376 udev_list_node_foreach(loop, &worker_list) {
377 struct worker *worker = node_to_worker(loop);
378 ssize_t count;
7baada47 379
1e03b754
KS
380 if (worker->state != WORKER_IDLE)
381 continue;
0f624f16 382
1e03b754
KS
383 count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
384 if (count < 0) {
a073cfa8 385 err(event->udev, "worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
1e03b754 386 kill(worker->pid, SIGKILL);
bc113de9 387 worker->state = WORKER_KILLED;
1e03b754
KS
388 continue;
389 }
bc113de9
KS
390 worker_ref(worker);
391 worker->event = event;
392 worker->state = WORKER_RUNNING;
393 event->state = EVENT_RUNNING;
1e03b754
KS
394 return;
395 }
396
87d55ff6 397 if (!force && children >= children_max) {
c830e98d
KS
398 if (children_max > 1)
399 info(event->udev, "maximum number (%i) of children reached\n", children);
1e03b754
KS
400 return;
401 }
402
403 /* start new worker and pass initial device */
404 worker_new(event);
405}
406
40929a02 407static int event_queue_insert(struct udev_device *dev)
1e03b754
KS
408{
409 struct event *event;
410
411 event = calloc(1, sizeof(struct event));
412 if (event == NULL)
40929a02 413 return -1;
1e03b754
KS
414
415 event->udev = udev_device_get_udev(dev);
416 event->dev = dev;
417 event->seqnum = udev_device_get_seqnum(dev);
418 event->devpath = udev_device_get_devpath(dev);
419 event->devpath_len = strlen(event->devpath);
420 event->devpath_old = udev_device_get_devpath_old(dev);
19711e19
KS
421 event->devnum = udev_device_get_devnum(dev);
422 event->is_block = (strcmp("block", udev_device_get_subsystem(dev)) == 0);
fc416258 423 event->ifindex = udev_device_get_ifindex(dev);
1e03b754
KS
424
425 udev_queue_export_device_queued(udev_queue_export, dev);
426 info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
427 udev_device_get_action(dev), udev_device_get_subsystem(dev));
428
429 event->state = EVENT_QUEUED;
0bc74ea7 430 udev_list_node_append(&event->node, &event_list);
c7c00276 431
fc465079 432 /* run all events with a timeout set immediately */
1e03b754 433 if (udev_device_get_timeout(dev) > 0) {
665ee17d 434 event_run(event, true);
40929a02 435 return 0;
fc465079 436 }
40929a02
YK
437
438 return 0;
fc465079
KS
439}
440
adda4c68 441static void worker_kill(struct udev *udev, int retain)
1e03b754
KS
442{
443 struct udev_list_node *loop;
444 int max;
445
87d55ff6 446 if (children <= retain)
1e03b754
KS
447 return;
448
87d55ff6 449 max = children - retain;
1e03b754
KS
450
451 udev_list_node_foreach(loop, &worker_list) {
452 struct worker *worker = node_to_worker(loop);
453
454 if (max-- <= 0)
455 break;
456
457 if (worker->state == WORKER_KILLED)
458 continue;
459
460 worker->state = WORKER_KILLED;
461 kill(worker->pid, SIGTERM);
462 }
463}
464
e3196993 465/* lookup event for identical, parent, child device */
19711e19 466static bool is_devpath_busy(struct event *event)
7fafc032 467{
7e027927 468 struct udev_list_node *loop;
1e03b754 469 size_t common;
7b6571a9 470
0bc74ea7
KS
471 /* check if queue contains events we depend on */
472 udev_list_node_foreach(loop, &event_list) {
1e03b754 473 struct event *loop_event = node_to_event(loop);
7e027927 474
0bc74ea7 475 /* we already found a later event, earlier can not block us, no need to check again */
1e03b754 476 if (loop_event->seqnum < event->delaying_seqnum)
0bc74ea7
KS
477 continue;
478
479 /* event we checked earlier still exists, no need to check again */
1e03b754 480 if (loop_event->seqnum == event->delaying_seqnum)
19711e19 481 return true;
0bc74ea7
KS
482
483 /* found ourself, no later event can block us */
1e03b754 484 if (loop_event->seqnum >= event->seqnum)
fc630eda
KS
485 break;
486
19711e19
KS
487 /* check major/minor */
488 if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
489 return true;
490
fc416258
KS
491 /* check network device ifindex */
492 if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
493 return true;
494
a2f2270e 495 /* check our old name */
19711e19
KS
496 if (event->devpath_old != NULL && strcmp(loop_event->devpath, event->devpath_old) == 0) {
497 event->delaying_seqnum = loop_event->seqnum;
498 return true;
499 }
a2f2270e 500
1e03b754
KS
501 /* compare devpath */
502 common = MIN(loop_event->devpath_len, event->devpath_len);
503
504 /* one devpath is contained in the other? */
505 if (memcmp(loop_event->devpath, event->devpath, common) != 0)
506 continue;
507
508 /* identical device event found */
509 if (loop_event->devpath_len == event->devpath_len) {
fc416258
KS
510 /* devices names might have changed/swapped in the meantime */
511 if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
512 continue;
513 if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
514 continue;
1e03b754 515 event->delaying_seqnum = loop_event->seqnum;
19711e19 516 return true;
c3b145a3 517 }
1e03b754
KS
518
519 /* parent device event found */
520 if (event->devpath[common] == '/') {
521 event->delaying_seqnum = loop_event->seqnum;
19711e19 522 return true;
1e03b754
KS
523 }
524
525 /* child device event found */
526 if (loop_event->devpath[common] == '/') {
527 event->delaying_seqnum = loop_event->seqnum;
19711e19 528 return true;
1e03b754
KS
529 }
530
531 /* no matching device */
532 continue;
80513ea3 533 }
1e03b754 534
19711e19 535 return false;
7fafc032
KS
536}
537
1e03b754 538static void events_start(struct udev *udev)
7fafc032 539{
7e027927 540 struct udev_list_node *loop;
8ab44e3f 541
1e03b754
KS
542 udev_list_node_foreach(loop, &event_list) {
543 struct event *event = node_to_event(loop);
0bc74ea7 544
1e03b754 545 if (event->state != EVENT_QUEUED)
0bc74ea7
KS
546 continue;
547
548 /* do not start event if parent or child event is still running */
19711e19 549 if (is_devpath_busy(event)) {
1e03b754 550 dbg(udev, "delay seq %llu (%s)\n", event->seqnum, event->devpath);
fc465079
KS
551 continue;
552 }
553
665ee17d 554 event_run(event, false);
1e03b754
KS
555 }
556}
557
558static void worker_returned(void)
559{
88cbfb09 560 for (;;) {
1e03b754
KS
561 struct worker_message msg;
562 ssize_t size;
563 struct udev_list_node *loop;
564
565 size = recv(pfd[FD_WORKER].fd, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
566 if (size != sizeof(struct worker_message))
567 break;
568
569 /* lookup worker who sent the signal */
570 udev_list_node_foreach(loop, &worker_list) {
571 struct worker *worker = node_to_worker(loop);
d1cc6562 572
1e03b754
KS
573 if (worker->pid != msg.pid)
574 continue;
575
576 /* worker returned */
577 worker->event->exitcode = msg.exitcode;
578 event_queue_delete(worker->event);
579 worker->event = NULL;
adda4c68
KS
580 if (worker->state != WORKER_KILLED)
581 worker->state = WORKER_IDLE;
bc113de9 582 worker_unref(worker);
1e03b754 583 break;
d1cc6562 584 }
e825b59b 585 }
88f4b648
KS
586}
587
3b47c739 588/* receive the udevd message from userspace */
d59f11e1 589static void handle_ctrl_msg(struct udev_ctrl *uctrl)
7fafc032 590{
d59f11e1
KS
591 struct udev *udev = udev_ctrl_get_udev(uctrl);
592 struct udev_ctrl_msg *ctrl_msg;
593 const char *str;
594 int i;
7b1cbec9 595
d59f11e1
KS
596 ctrl_msg = udev_ctrl_receive_msg(uctrl);
597 if (ctrl_msg == NULL)
b437ec95 598 return;
4a231017 599
d59f11e1
KS
600 i = udev_ctrl_get_set_log_level(ctrl_msg);
601 if (i >= 0) {
602 info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
603 udev_set_log_priority(udev, i);
adda4c68 604 worker_kill(udev, 0);
0028653c
KS
605 }
606
d59f11e1 607 if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
7d563a17 608 info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
c3804728 609 stop_exec_queue = true;
d59f11e1
KS
610 }
611
612 if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
7d563a17 613 info(udev, "udevd message (START_EXEC_QUEUE) received\n");
c3804728 614 stop_exec_queue = false;
d59f11e1
KS
615 }
616
617 if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
7d563a17 618 info(udev, "udevd message (RELOAD_RULES) received\n");
c3804728 619 reload_config = true;
3b47c739 620 }
d59f11e1
KS
621
622 str = udev_ctrl_get_set_env(ctrl_msg);
623 if (str != NULL) {
aa8734ff
KS
624 char *key;
625
626 key = strdup(str);
627 if (key != NULL) {
628 char *val;
629
630 val = strchr(key, '=');
631 if (val != NULL) {
632 val[0] = '\0';
633 val = &val[1];
634 if (val[0] == '\0') {
635 info(udev, "udevd message (ENV) received, unset '%s'\n", key);
636 udev_add_property(udev, key, NULL);
637 } else {
638 info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
639 udev_add_property(udev, key, val);
640 }
d59f11e1 641 } else {
aa8734ff 642 err(udev, "wrong key format '%s'\n", key);
d59f11e1 643 }
aa8734ff 644 free(key);
d59f11e1 645 }
adda4c68 646 worker_kill(udev, 0);
d59f11e1
KS
647 }
648
87d55ff6 649 i = udev_ctrl_get_set_children_max(ctrl_msg);
d59f11e1 650 if (i >= 0) {
87d55ff6
KS
651 info(udev, "udevd message (SET_MAX_CHILDREN) received, children_max=%i\n", i);
652 children_max = i;
d59f11e1 653 }
bb38678e
SJR
654
655 settle_pid = udev_ctrl_get_settle(ctrl_msg);
656 if (settle_pid > 0) {
657 info(udev, "udevd message (SETTLE) received\n");
1e03b754
KS
658 kill(settle_pid, SIGUSR1);
659 settle_pid = 0;
bb38678e 660 }
d59f11e1 661 udev_ctrl_msg_unref(ctrl_msg);
88f4b648 662}
4a231017 663
bd284db1
SJR
664/* read inotify messages */
665static int handle_inotify(struct udev *udev)
666{
4daa146b 667 int nbytes, pos;
bd284db1
SJR
668 char *buf;
669 struct inotify_event *ev;
bd284db1 670
1e03b754 671 if ((ioctl(pfd[FD_INOTIFY].fd, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
bd284db1
SJR
672 return 0;
673
674 buf = malloc(nbytes);
675 if (buf == NULL) {
45798927 676 err(udev, "error getting buffer for inotify\n");
1e03b754 677 return -1;
bd284db1
SJR
678 }
679
1e03b754 680 nbytes = read(pfd[FD_INOTIFY].fd, buf, nbytes);
bd284db1 681
80be8c48 682 for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
047f88bc 683 struct udev_device *dev;
bd284db1 684
80be8c48 685 ev = (struct inotify_event *)(buf + pos);
b8e96d67 686 if (ev->len) {
79e1912b
KS
687 const char *s;
688
689 info(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
690 s = strstr(ev->name, ".rules");
691 if (s == NULL)
692 continue;
693 if (strlen(s) != strlen(".rules"))
694 continue;
c3804728 695 reload_config = true;
b8e96d67
SJR
696 continue;
697 }
698
047f88bc
SJR
699 dev = udev_watch_lookup(udev, ev->wd);
700 if (dev != NULL) {
79e1912b 701 info(udev, "inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
bd284db1
SJR
702 if (ev->mask & IN_CLOSE_WRITE) {
703 char filename[UTIL_PATH_SIZE];
704 int fd;
705
047f88bc 706 info(udev, "device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
065db052 707 util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
bd284db1
SJR
708 fd = open(filename, O_WRONLY);
709 if (fd < 0 || write(fd, "change", 6) < 0)
710 info(udev, "error writing uevent: %m\n");
711 close(fd);
712 }
713 if (ev->mask & IN_IGNORED)
047f88bc
SJR
714 udev_watch_end(udev, dev);
715
716 udev_device_unref(dev);
bd284db1 717 }
b8e96d67 718
bd284db1
SJR
719 }
720
1e03b754 721 free(buf);
4aca304e 722 return 0;
bd284db1
SJR
723}
724
45798927 725static void handle_signal(struct udev *udev, int signo)
7fafc032 726{
1e03b754
KS
727 switch (signo) {
728 case SIGINT:
729 case SIGTERM:
c3804728 730 udev_exit = true;
1e03b754
KS
731 break;
732 case SIGCHLD:
88cbfb09 733 for (;;) {
1e03b754 734 pid_t pid;
45798927 735 int status;
1e03b754 736 struct udev_list_node *loop, *tmp;
5a73b25f 737
45798927 738 pid = waitpid(-1, &status, WNOHANG);
1e03b754
KS
739 if (pid <= 0)
740 break;
7fafc032 741
1e03b754
KS
742 udev_list_node_foreach_safe(loop, tmp, &worker_list) {
743 struct worker *worker = node_to_worker(loop);
7e027927 744
1e03b754
KS
745 if (worker->pid != pid)
746 continue;
2f6cbd19 747
bc113de9
KS
748 info(udev, "worker [%u] exit\n", pid);
749 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
adda4c68 750 err(udev, "worker [%u] unexpectedly returned with status 0x%04x\n", pid, status);
bc113de9
KS
751 if (worker->event != NULL) {
752 err(udev, "worker [%u] failed while handling '%s'\n", pid, worker->event->devpath);
753 worker->event->exitcode = -32;
754 event_queue_delete(worker->event);
755 /* drop reference from running event */
756 worker_unref(worker);
757 }
1e03b754 758 }
1e03b754 759 worker_unref(worker);
1e03b754
KS
760 break;
761 }
762 }
763 break;
764 case SIGHUP:
c3804728 765 reload_config = true;
1e03b754 766 break;
f27125f9 767 }
768}
769
761dfddc
KS
770static void static_dev_create_from_modules(struct udev *udev)
771{
772 struct utsname kernel;
773 char modules[UTIL_PATH_SIZE];
774 char buf[4096];
775 FILE *f;
776
777 uname(&kernel);
778 util_strscpyl(modules, sizeof(modules), "/lib/modules/", kernel.release, "/modules.devname", NULL);
779 f = fopen(modules, "r");
780 if (f == NULL)
781 return;
782
783 while (fgets(buf, sizeof(buf), f) != NULL) {
784 char *s;
785 const char *modname;
786 const char *devname;
787 const char *devno;
788 int maj, min;
789 char type;
790 mode_t mode;
791 char filename[UTIL_PATH_SIZE];
792
793 if (buf[0] == '#')
794 continue;
795
796 modname = buf;
797 s = strchr(modname, ' ');
798 if (s == NULL)
799 continue;
800 s[0] = '\0';
801
802 devname = &s[1];
803 s = strchr(devname, ' ');
804 if (s == NULL)
805 continue;
806 s[0] = '\0';
807
808 devno = &s[1];
809 s = strchr(devno, ' ');
810 if (s == NULL)
811 s = strchr(devno, '\n');
812 if (s != NULL)
813 s[0] = '\0';
814 if (sscanf(devno, "%c%u:%u", &type, &maj, &min) != 3)
815 continue;
816
817 if (type == 'c')
818 mode = 0600 | S_IFCHR;
819 else if (type == 'b')
820 mode = 0600 | S_IFBLK;
821 else
822 continue;
823
824 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/", devname, NULL);
825 util_create_path(udev, filename);
826 udev_selinux_setfscreatecon(udev, filename, mode);
827 info(udev, "mknod '%s' %c%u:%u\n", filename, type, maj, min);
828 if (mknod(filename, mode, makedev(maj, min)) < 0 && errno == EEXIST)
829 utimensat(AT_FDCWD, filename, NULL, 0);
830 udev_selinux_resetfscreatecon(udev);
831 }
832
833 fclose(f);
834}
835
cb9a0eee
KS
836static int copy_dir(struct udev *udev, DIR *dir_from, DIR *dir_to, int maxdepth)
837{
838 struct dirent *dent;
839
840 for (dent = readdir(dir_from); dent != NULL; dent = readdir(dir_from)) {
841 struct stat stats;
842
843 if (dent->d_name[0] == '.')
844 continue;
845 if (fstatat(dirfd(dir_from), dent->d_name, &stats, AT_SYMLINK_NOFOLLOW) != 0)
846 continue;
847
848 if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
849 udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, stats.st_mode & 0777);
850 if (mknodat(dirfd(dir_to), dent->d_name, stats.st_mode, stats.st_rdev) == 0) {
851 fchmodat(dirfd(dir_to), dent->d_name, stats.st_mode & 0777, 0);
852 fchownat(dirfd(dir_to), dent->d_name, stats.st_uid, stats.st_gid, 0);
853 } else {
854 utimensat(dirfd(dir_to), dent->d_name, NULL, 0);
855 }
856 udev_selinux_resetfscreatecon(udev);
857 } else if (S_ISLNK(stats.st_mode)) {
858 char target[UTIL_PATH_SIZE];
859 ssize_t len;
860
861 len = readlinkat(dirfd(dir_from), dent->d_name, target, sizeof(target));
862 if (len <= 0 || len == (ssize_t)sizeof(target))
863 continue;
864 target[len] = '\0';
865 udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, S_IFLNK);
866 if (symlinkat(target, dirfd(dir_to), dent->d_name) < 0 && errno == EEXIST)
867 utimensat(dirfd(dir_to), dent->d_name, NULL, AT_SYMLINK_NOFOLLOW);
868 udev_selinux_resetfscreatecon(udev);
869 } else if (S_ISDIR(stats.st_mode)) {
870 DIR *dir2_from, *dir2_to;
871
872 if (maxdepth == 0)
873 continue;
874
875 udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, S_IFDIR|0755);
876 mkdirat(dirfd(dir_to), dent->d_name, 0755);
877 udev_selinux_resetfscreatecon(udev);
878
879 dir2_to = fdopendir(openat(dirfd(dir_to), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
880 if (dir2_to == NULL)
881 continue;
882
883 dir2_from = fdopendir(openat(dirfd(dir_from), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
884 if (dir2_from == NULL) {
885 closedir(dir2_to);
886 continue;
887 }
888
889 copy_dir(udev, dir2_from, dir2_to, maxdepth-1);
890
891 closedir(dir2_to);
892 closedir(dir2_from);
893 }
894 }
895
896 return 0;
897}
898
761dfddc 899static void static_dev_create_links(struct udev *udev, DIR *dir)
cb9a0eee
KS
900{
901 struct stdlinks {
902 const char *link;
903 const char *target;
904 };
905 static const struct stdlinks stdlinks[] = {
906 { "core", "/proc/kcore" },
4bfa5cf5 907 { "fd", "/proc/self/fd" },
cb9a0eee
KS
908 { "stdin", "/proc/self/fd/0" },
909 { "stdout", "/proc/self/fd/1" },
910 { "stderr", "/proc/self/fd/2" },
911 };
912 unsigned int i;
cb9a0eee 913
cb9a0eee 914 for (i = 0; i < ARRAY_SIZE(stdlinks); i++) {
08f11597
YK
915 struct stat sb;
916
917 if (stat(stdlinks[i].target, &sb) == 0) {
918 udev_selinux_setfscreateconat(udev, dirfd(dir), stdlinks[i].link, S_IFLNK);
919 if (symlinkat(stdlinks[i].target, dirfd(dir), stdlinks[i].link) < 0 && errno == EEXIST)
920 utimensat(dirfd(dir), stdlinks[i].link, NULL, AT_SYMLINK_NOFOLLOW);
921 udev_selinux_resetfscreatecon(udev);
922 }
cb9a0eee 923 }
761dfddc
KS
924}
925
926static void static_dev_create_from_devices(struct udev *udev, DIR *dir)
927{
928 DIR *dir_from;
cb9a0eee 929
cb9a0eee 930 dir_from = opendir(LIBEXECDIR "/devices");
761dfddc
KS
931 if (dir_from == NULL)
932 return;
933 copy_dir(udev, dir_from, dir, 8);
934 closedir(dir_from);
935}
936
937static void static_dev_create(struct udev *udev)
938{
939 DIR *dir;
cb9a0eee 940
761dfddc
KS
941 dir = opendir(udev_get_dev_path(udev));
942 if (dir == NULL)
943 return;
944
945 static_dev_create_links(udev, dir);
946 static_dev_create_from_devices(udev, dir);
947
948 closedir(dir);
cb9a0eee
KS
949}
950
951static int mem_size_mb(void)
952{
953 FILE *f;
954 char buf[4096];
955 long int memsize = -1;
956
957 f = fopen("/proc/meminfo", "r");
958 if (f == NULL)
959 return -1;
960
961 while (fgets(buf, sizeof(buf), f) != NULL) {
962 long int value;
963
964 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
965 memsize = value / 1024;
966 break;
967 }
968 }
969
970 fclose(f);
971 return memsize;
972}
973
59345311 974int main(int argc, char *argv[])
c2cf4012 975{
7d563a17 976 struct udev *udev;
3904a758 977 int fd;
3c189886 978 FILE *f;
1e03b754 979 sigset_t mask;
c3804728 980 int daemonize = false;
5f03ed8a 981 int resolve_names = 1;
b52a01ee 982 static const struct option options[] = {
033e9f8c 983 { "daemon", no_argument, NULL, 'd' },
033e9f8c 984 { "debug", no_argument, NULL, 'D' },
337d1027 985 { "children-max", required_argument, NULL, 'c' },
c830e98d 986 { "exec-delay", required_argument, NULL, 'e' },
337d1027 987 { "resolve-names", required_argument, NULL, 'N' },
033e9f8c
KS
988 { "help", no_argument, NULL, 'h' },
989 { "version", no_argument, NULL, 'V' },
b52a01ee
KS
990 {}
991 };
e3396a2d 992 int rc = 1;
53921bfa 993
7d563a17
KS
994 udev = udev_new();
995 if (udev == NULL)
996 goto exit;
997
9060b066 998 udev_log_init("udevd");
7d563a17 999 udev_set_log_fn(udev, log_fn);
aa8734ff 1000 info(udev, "version %s\n", VERSION);
c3b1fa66 1001 udev_selinux_init(udev);
95a6f4c8 1002
88cbfb09 1003 for (;;) {
7d563a17
KS
1004 int option;
1005
4ab3c463 1006 option = getopt_long(argc, argv, "c:deDthV", options, NULL);
b52a01ee
KS
1007 if (option == -1)
1008 break;
1009
1010 switch (option) {
1011 case 'd':
c3804728 1012 daemonize = true;
b52a01ee 1013 break;
337d1027 1014 case 'c':
c830e98d
KS
1015 children_max = strtoul(optarg, NULL, 0);
1016 break;
1017 case 'e':
1018 exec_delay = strtoul(optarg, NULL, 0);
c7c00276 1019 break;
c70560fe 1020 case 'D':
c3804728 1021 debug = true;
7d563a17
KS
1022 if (udev_get_log_priority(udev) < LOG_INFO)
1023 udev_set_log_priority(udev, LOG_INFO);
9e8fe79b 1024 break;
5f03ed8a
SJR
1025 case 'N':
1026 if (strcmp (optarg, "early") == 0) {
1027 resolve_names = 1;
9032f119
SJR
1028 } else if (strcmp (optarg, "late") == 0) {
1029 resolve_names = 0;
5f03ed8a
SJR
1030 } else if (strcmp (optarg, "never") == 0) {
1031 resolve_names = -1;
1032 } else {
9032f119
SJR
1033 fprintf(stderr, "resolve-names must be early, late or never\n");
1034 err(udev, "resolve-names must be early, late or never\n");
5f03ed8a
SJR
1035 goto exit;
1036 }
1037 break;
b52a01ee 1038 case 'h':
c830e98d
KS
1039 printf("Usage: udevd OPTIONS\n"
1040 " --daemon\n"
1041 " --debug\n"
1042 " --children-max=<maximum number of workers>\n"
1043 " --exec-delay=<seconds to wait before executing RUN=>\n"
1044 " --resolve-names=early|late|never\n"
1045 " --version\n"
1046 " --help\n"
1047 "\n");
841e168c
MS
1048 goto exit;
1049 case 'V':
01618658 1050 printf("%s\n", VERSION);
e3396a2d 1051 goto exit;
b52a01ee
KS
1052 default:
1053 goto exit;
561d4c5a 1054 }
561d4c5a 1055 }
40caaeec 1056
c830e98d
KS
1057 /*
1058 * read the kernel commandline, in case we need to get into debug mode
1059 * udev.log-priority=<level> syslog priority
1060 * udev.children-max=<number of workers> events are fully serialized if set to 1
1061 *
1062 */
1063 f = fopen("/proc/cmdline", "r");
1064 if (f != NULL) {
1065 char cmdline[4096];
1066
1067 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
1068 char *pos;
1069
1070 pos = strstr(cmdline, "udev.log-priority=");
1071 if (pos != NULL) {
1072 pos += strlen("udev.log-priority=");
1073 udev_set_log_priority(udev, util_log_priority(pos));
1074 }
1075
1076 pos = strstr(cmdline, "udev.children-max=");
1077 if (pos != NULL) {
1078 pos += strlen("udev.children-max=");
1079 children_max = strtoul(pos, NULL, 0);
1080 }
1081
1082 pos = strstr(cmdline, "udev.exec-delay=");
1083 if (pos != NULL) {
1084 pos += strlen("udev.exec-delay=");
1085 exec_delay = strtoul(pos, NULL, 0);
1086 }
1087 }
1088 fclose(f);
1089 }
1090
1182b947
KS
1091 if (getuid() != 0) {
1092 fprintf(stderr, "root privileges required\n");
1093 err(udev, "root privileges required\n");
1094 goto exit;
1095 }
1096
1097 /* set umask before creating any file/directory */
1098 chdir("/");
1099 umask(022);
1100
1a6ab670
MS
1101 /* create standard links, copy static nodes, create nodes from modules */
1102 static_dev_create(udev);
1103 static_dev_create_from_modules(udev);
1104
1182b947 1105 /* before opening new files, make sure std{in,out,err} fds are in a sane state */
5edec024
MS
1106 fd = open("/dev/null", O_RDWR);
1107 if (fd < 0) {
1108 fprintf(stderr, "cannot open /dev/null\n");
7d563a17 1109 err(udev, "cannot open /dev/null\n");
5edec024 1110 }
5edec024
MS
1111 if (write(STDOUT_FILENO, 0, 0) < 0)
1112 dup2(fd, STDOUT_FILENO);
1113 if (write(STDERR_FILENO, 0, 0) < 0)
1114 dup2(fd, STDERR_FILENO);
1115
d59f11e1
KS
1116 udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
1117 if (udev_ctrl == NULL) {
1118 fprintf(stderr, "error initializing control socket");
1119 err(udev, "error initializing udevd socket");
1120 rc = 1;
1121 goto exit;
1122 }
d59f11e1
KS
1123 if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
1124 fprintf(stderr, "error binding control socket, seems udevd is already running\n");
1125 err(udev, "error binding control socket, seems udevd is already running\n");
1126 rc = 1;
833b3c68
KS
1127 goto exit;
1128 }
1e03b754 1129 pfd[FD_CONTROL].fd = udev_ctrl_get_fd(udev_ctrl);
833b3c68 1130
1e03b754
KS
1131 monitor = udev_monitor_new_from_netlink(udev, "kernel");
1132 if (monitor == NULL || udev_monitor_enable_receiving(monitor) < 0) {
e3396a2d 1133 fprintf(stderr, "error initializing netlink socket\n");
7d563a17 1134 err(udev, "error initializing netlink socket\n");
833b3c68
KS
1135 rc = 3;
1136 goto exit;
1137 }
1e03b754
KS
1138 udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
1139 pfd[FD_NETLINK].fd = udev_monitor_get_fd(monitor);
1140
1141 pfd[FD_INOTIFY].fd = udev_watch_init(udev);
1142 if (pfd[FD_INOTIFY].fd < 0) {
1143 fprintf(stderr, "error initializing inotify\n");
1144 err(udev, "error initializing inotify\n");
1145 rc = 4;
1146 goto exit;
1147 }
1148
1149 if (udev_get_rules_path(udev) != NULL) {
1150 inotify_add_watch(pfd[FD_INOTIFY].fd, udev_get_rules_path(udev),
6f1892dc 1151 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1e03b754
KS
1152 } else {
1153 char filename[UTIL_PATH_SIZE];
081be002 1154 struct stat statbuf;
1e03b754 1155
6133f343 1156 inotify_add_watch(pfd[FD_INOTIFY].fd, LIBEXECDIR "/rules.d",
6f1892dc 1157 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1e03b754 1158 inotify_add_watch(pfd[FD_INOTIFY].fd, SYSCONFDIR "/udev/rules.d",
6f1892dc 1159 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1e03b754
KS
1160
1161 /* watch dynamic rules directory */
1162 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL);
081be002
KS
1163 if (stat(filename, &statbuf) != 0) {
1164 util_create_path(udev, filename);
1165 udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
1166 mkdir(filename, 0755);
1167 udev_selinux_resetfscreatecon(udev);
1168 }
1e03b754 1169 inotify_add_watch(pfd[FD_INOTIFY].fd, filename,
6f1892dc 1170 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1e03b754
KS
1171 }
1172 udev_watch_restore(udev);
1173
1174 /* block and listen to all signals on signalfd */
1175 sigfillset(&mask);
d412a685 1176 sigprocmask(SIG_SETMASK, &mask, &orig_sigmask);
1e03b754
KS
1177 pfd[FD_SIGNAL].fd = signalfd(-1, &mask, 0);
1178 if (pfd[FD_SIGNAL].fd < 0) {
1179 fprintf(stderr, "error getting signalfd\n");
1180 err(udev, "error getting signalfd\n");
1181 rc = 5;
1182 goto exit;
1183 }
1184
1185 /* unnamed socket from workers to the main daemon */
26347a4c 1186 if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
1e03b754
KS
1187 fprintf(stderr, "error getting socketpair\n");
1188 err(udev, "error getting socketpair\n");
1189 rc = 6;
1190 goto exit;
1191 }
1192 pfd[FD_WORKER].fd = worker_watch[READ_END];
833b3c68 1193
5f03ed8a 1194 rules = udev_rules_new(udev, resolve_names);
d7ddce18
KS
1195 if (rules == NULL) {
1196 err(udev, "error reading rules\n");
1197 goto exit;
1198 }
1e03b754 1199
f503f6b2
AJ
1200 udev_queue_export = udev_queue_export_new(udev);
1201 if (udev_queue_export == NULL) {
1202 err(udev, "error creating queue file\n");
1203 goto exit;
1204 }
90cd961e 1205
1182b947
KS
1206 if (!debug) {
1207 dup2(fd, STDIN_FILENO);
1208 dup2(fd, STDOUT_FILENO);
1209 dup2(fd, STDERR_FILENO);
1210 }
1211 if (fd > STDERR_FILENO)
1212 close(fd);
1213
561d4c5a 1214 if (daemonize) {
f15515b5
KS
1215 pid_t pid;
1216
1217 pid = fork();
1218 switch (pid) {
1219 case 0:
f15515b5
KS
1220 break;
1221 case -1:
659353f5 1222 err(udev, "fork of daemon failed: %m\n");
833b3c68 1223 rc = 4;
f15515b5
KS
1224 goto exit;
1225 default:
2f64aa40 1226 rc = 0;
833b3c68 1227 goto exit;
f15515b5 1228 }
a65aa40f 1229 } else {
392ef7a2 1230 sd_notify(1, "READY=1");
f15515b5
KS
1231 }
1232
1182b947
KS
1233 /* set scheduling priority for the main daemon process */
1234 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
1235
1236 setsid();
1237
3c189886
KS
1238 f = fopen("/dev/kmsg", "w");
1239 if (f != NULL) {
c25bfbfb 1240 fprintf(f, "<6>udev[%u]: starting version " VERSION "\n", getpid());
3c189886
KS
1241 fclose(f);
1242 }
1e03b754 1243
c61eea94 1244 fd = open("/proc/self/oom_score_adj", O_RDWR);
45798927 1245 if (fd < 0) {
c61eea94
LT
1246 /* Fallback to old interface */
1247 fd = open("/proc/self/oom_adj", O_RDWR);
1248 if (fd < 0) {
1249 err(udev, "error disabling OOM: %m\n");
1250 } else {
1251 /* OOM_DISABLE == -17 */
1252 write(fd, "-17", 3);
1253 close(fd);
1254 }
45798927 1255 } else {
c61eea94 1256 write(fd, "-1000", 5);
3904a758
KS
1257 close(fd);
1258 }
1259
337d1027 1260 if (children_max <= 0) {
c830e98d
KS
1261 int memsize = mem_size_mb();
1262
1263 /* set value depending on the amount of RAM */
1264 if (memsize > 0)
1265 children_max = 128 + (memsize / 8);
1266 else
1267 children_max = 128;
337d1027
KS
1268 }
1269 info(udev, "set children_max to %u\n", children_max);
a15f42c4 1270
761dfddc
KS
1271 udev_rules_apply_static_dev_perms(rules);
1272
1e03b754
KS
1273 udev_list_init(&event_list);
1274 udev_list_init(&worker_list);
1275
63cc8f04 1276 while (!udev_exit) {
e9a77fd8 1277 int fdcount;
1e03b754 1278 int timeout;
3210a72b 1279
1e03b754 1280 /* set timeout to kill idle workers */
87d55ff6 1281 if (udev_list_is_empty(&event_list) && children > 2)
1e03b754
KS
1282 timeout = 3 * 1000;
1283 else
1284 timeout = -1;
1285 /* wait for events */
1286 fdcount = poll(pfd, ARRAY_SIZE(pfd), timeout);
1287 if (fdcount < 0)
1288 continue;
021a294c 1289
1e03b754
KS
1290 /* timeout - kill idle workers */
1291 if (fdcount == 0)
adda4c68 1292 worker_kill(udev, 2);
e9a77fd8 1293
1e03b754
KS
1294 /* event has finished */
1295 if (pfd[FD_WORKER].revents & POLLIN)
1296 worker_returned();
e9a77fd8 1297
1e03b754
KS
1298 /* get kernel uevent */
1299 if (pfd[FD_NETLINK].revents & POLLIN) {
1300 struct udev_device *dev;
3210a72b 1301
1e03b754
KS
1302 dev = udev_monitor_receive_device(monitor);
1303 if (dev != NULL)
40929a02
YK
1304 if (event_queue_insert(dev) < 0)
1305 udev_device_unref(dev);
2f6cbd19 1306 }
e5a2989e 1307
1e03b754
KS
1308 /* start new events */
1309 if (!udev_list_is_empty(&event_list) && !stop_exec_queue)
1310 events_start(udev);
aa8734ff 1311
1e03b754
KS
1312 /* get signal */
1313 if (pfd[FD_SIGNAL].revents & POLLIN) {
1314 struct signalfd_siginfo fdsi;
1315 ssize_t size;
aa8734ff 1316
1e03b754
KS
1317 size = read(pfd[FD_SIGNAL].fd, &fdsi, sizeof(struct signalfd_siginfo));
1318 if (size == sizeof(struct signalfd_siginfo))
45798927 1319 handle_signal(udev, fdsi.ssi_signo);
021a294c 1320 }
e5a2989e 1321
1e03b754
KS
1322 /* device node and rules directory inotify watch */
1323 if (pfd[FD_INOTIFY].revents & POLLIN)
4aca304e 1324 handle_inotify(udev);
c895fd00 1325
1e03b754
KS
1326 /*
1327 * get control message
1328 *
1329 * This needs to be after the inotify handling, to make sure,
1330 * that the settle signal is send back after the possibly generated
1331 * "change" events by the inotify device node watch.
1332 */
1333 if (pfd[FD_CONTROL].revents & POLLIN)
1334 handle_ctrl_msg(udev_ctrl);
3210a72b 1335
e3396a2d 1336 /* rules changed, set by inotify or a HUP signal */
c895fd00 1337 if (reload_config) {
d7ddce18
KS
1338 struct udev_rules *rules_new;
1339
adda4c68 1340 worker_kill(udev, 0);
5f03ed8a 1341 rules_new = udev_rules_new(udev, resolve_names);
d7ddce18
KS
1342 if (rules_new != NULL) {
1343 udev_rules_unref(rules);
1344 rules = rules_new;
1345 }
1e03b754 1346 reload_config = 0;
bb38678e 1347 }
53921bfa 1348 }
1e03b754 1349
f503f6b2 1350 udev_queue_export_cleanup(udev_queue_export);
e3396a2d 1351 rc = 0;
53921bfa 1352exit:
f503f6b2 1353 udev_queue_export_unref(udev_queue_export);
d7ddce18 1354 udev_rules_unref(rules);
d59f11e1 1355 udev_ctrl_unref(udev_ctrl);
1e03b754
KS
1356 if (pfd[FD_SIGNAL].fd >= 0)
1357 close(pfd[FD_SIGNAL].fd);
1358 if (worker_watch[READ_END] >= 0)
1359 close(worker_watch[READ_END]);
1360 if (worker_watch[WRITE_END] >= 0)
1361 close(worker_watch[WRITE_END]);
1362 udev_monitor_unref(monitor);
c3b1fa66 1363 udev_selinux_exit(udev);
e598c573 1364 udev_unref(udev);
9060b066 1365 udev_log_close();
833b3c68 1366 return rc;
7fafc032 1367}