2 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
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.
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.
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.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <sys/select.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <linux/types.h>
41 #include <linux/netlink.h>
44 #include "udev_rules.h"
46 #include "udev_selinux.h"
48 static int debug_trace
;
51 static struct udev_rules rules
;
52 static int udevd_sock
= -1;
53 static int uevent_netlink_sock
= -1;
54 static int inotify_fd
= -1;
57 static int signal_pipe
[2] = {-1, -1};
58 static volatile int sigchilds_waiting
;
59 static volatile int udev_exit
;
60 static volatile int reload_config
;
61 static int run_exec_q
;
62 static int stop_exec_q
;
63 static int max_childs
;
64 static int max_childs_running
;
65 static char udev_log
[32];
67 static LIST_HEAD(exec_list
);
68 static LIST_HEAD(running_list
);
72 void log_message(int priority
, const char *format
, ...)
76 if (priority
> udev_log_priority
)
79 va_start(args
, format
);
80 vsyslog(priority
, format
, args
);
84 va_start(args
, format
);
85 vprintf(format
, args
);
93 static void asmlinkage
udev_event_sig_handler(int signum
)
95 if (signum
== SIGALRM
)
99 static int udev_event_process(struct udevd_uevent_msg
*msg
)
101 struct sigaction act
;
102 struct udevice
*udev
;
106 /* set signal handlers */
107 memset(&act
, 0x00, sizeof(act
));
108 act
.sa_handler
= (void (*)(int)) udev_event_sig_handler
;
109 sigemptyset (&act
.sa_mask
);
111 sigaction(SIGALRM
, &act
, NULL
);
113 /* reset to default */
114 act
.sa_handler
= SIG_DFL
;
115 sigaction(SIGINT
, &act
, NULL
);
116 sigaction(SIGTERM
, &act
, NULL
);
117 sigaction(SIGCHLD
, &act
, NULL
);
118 sigaction(SIGHUP
, &act
, NULL
);
120 /* trigger timeout to prevent hanging processes */
121 alarm(UDEV_ALARM_TIMEOUT
);
123 /* reconstruct event environment from message */
124 for (i
= 0; msg
->envp
[i
]; i
++)
125 putenv(msg
->envp
[i
]);
127 udev
= udev_device_init();
130 strlcpy(udev
->action
, msg
->action
, sizeof(udev
->action
));
131 sysfs_device_set_values(udev
->dev
, msg
->devpath
, msg
->subsystem
, msg
->driver
);
132 udev
->devt
= msg
->devt
;
134 retval
= udev_device_event(&rules
, udev
);
136 /* run programs collected by RUN-key*/
137 if (retval
== 0 && !udev
->ignore_device
&& udev_run
) {
138 struct name_entry
*name_loop
;
140 dbg("executing run list");
141 list_for_each_entry(name_loop
, &udev
->run_list
, node
) {
142 if (strncmp(name_loop
->name
, "socket:", strlen("socket:")) == 0)
143 pass_env_to_socket(&name_loop
->name
[strlen("socket:")], udev
->dev
->devpath
, udev
->action
);
145 char program
[PATH_SIZE
];
147 strlcpy(program
, name_loop
->name
, sizeof(program
));
148 udev_rules_apply_format(udev
, program
, sizeof(program
));
149 if (run_program(program
, udev
->dev
->subsystem
, NULL
, 0, NULL
,
150 (udev_log_priority
>= LOG_INFO
)))
156 udev_device_cleanup(udev
);
166 static void export_event_state(struct udevd_uevent_msg
*msg
, enum event_state state
)
168 char filename
[PATH_SIZE
];
169 char filename_failed
[PATH_SIZE
];
170 size_t start
, end
, i
;
171 struct udevd_uevent_msg
*loop_msg
;
174 /* add location of queue files */
175 strlcpy(filename
, udev_root
, sizeof(filename
));
176 strlcat(filename
, "/", sizeof(filename
));
177 start
= strlcat(filename
, EVENT_QUEUE_DIR
, sizeof(filename
));
178 end
= strlcat(filename
, msg
->devpath
, sizeof(filename
));
179 if (end
> sizeof(filename
))
180 end
= sizeof(filename
);
182 /* replace '/' to transform path into a filename */
183 for (i
= start
+1; i
< end
; i
++)
184 if (filename
[i
] == '/')
185 filename
[i
] = PATH_TO_NAME_CHAR
;
187 /* add location of failed files */
188 strlcpy(filename_failed
, udev_root
, sizeof(filename_failed
));
189 strlcat(filename_failed
, "/", sizeof(filename_failed
));
190 start
= strlcat(filename_failed
, EVENT_FAILED_DIR
, sizeof(filename_failed
));
191 end
= strlcat(filename_failed
, msg
->devpath
, sizeof(filename_failed
));
192 if (end
> sizeof(filename_failed
))
193 end
= sizeof(filename_failed
);
195 /* replace '/' to transform path into a filename */
196 for (i
= start
+1; i
< end
; i
++)
197 if (filename_failed
[i
] == '/')
198 filename_failed
[i
] = PATH_TO_NAME_CHAR
;
202 unlink(filename_failed
);
203 delete_path(filename_failed
);
204 create_path(filename
);
205 fd
= open(filename
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0644);
211 unlink(filename_failed
);
212 delete_path(filename_failed
);
214 /* don't remove, if events for the same path are still pending */
215 list_for_each_entry(loop_msg
, &running_list
, node
)
216 if (loop_msg
->devpath
&& strcmp(loop_msg
->devpath
, msg
->devpath
) == 0)
219 list_for_each_entry(loop_msg
, &exec_list
, node
)
220 if (loop_msg
->devpath
&& strcmp(loop_msg
->devpath
, msg
->devpath
) == 0)
223 /* move failed events to the failed directory */
224 if (state
== EVENT_FAILED
) {
225 create_path(filename_failed
);
226 rename(filename
, filename_failed
);
231 /* clean up the queue directory */
232 delete_path(filename
);
238 static void msg_queue_delete(struct udevd_uevent_msg
*msg
)
240 list_del(&msg
->node
);
242 /* mark as failed, if add event returns non-zero */
243 if (msg
->exitstatus
&& strcmp(msg
->action
, "add") == 0)
244 export_event_state(msg
, EVENT_FAILED
);
246 export_event_state(msg
, EVENT_FINISHED
);
251 static void udev_event_run(struct udevd_uevent_msg
*msg
)
260 close(uevent_netlink_sock
);
264 close(signal_pipe
[READ_END
]);
265 close(signal_pipe
[WRITE_END
]);
268 logging_init("udevd-event");
269 setpriority(PRIO_PROCESS
, 0, UDEV_PRIORITY
);
271 retval
= udev_event_process(msg
);
272 info("seq %llu finished", msg
->seqnum
);
279 err("fork of child failed: %s", strerror(errno
));
280 msg_queue_delete(msg
);
283 /* get SIGCHLD in main loop */
284 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
285 msg
->seqnum
, pid
, msg
->action
, msg
->subsystem
, time(NULL
) - msg
->queue_time
);
290 static void msg_queue_insert(struct udevd_uevent_msg
*msg
)
292 char filename
[PATH_SIZE
];
295 msg
->queue_time
= time(NULL
);
297 strlcpy(filename
, udev_root
, sizeof(filename
));
298 strlcat(filename
, "/" EVENT_SEQNUM
, sizeof(filename
));
299 fd
= open(filename
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0644);
304 len
= sprintf(str
, "%llu\n", msg
->seqnum
);
309 export_event_state(msg
, EVENT_QUEUED
);
311 /* run one event after the other in debug mode */
313 list_add_tail(&msg
->node
, &running_list
);
315 waitpid(msg
->pid
, NULL
, 0);
316 msg_queue_delete(msg
);
320 /* run all events with a timeout set immediately */
321 if (msg
->timeout
!= 0) {
322 list_add_tail(&msg
->node
, &running_list
);
327 list_add_tail(&msg
->node
, &exec_list
);
331 static int mem_size_mb(void)
339 f
= open("/proc/meminfo", O_RDONLY
);
343 len
= read(f
, buf
, sizeof(buf
)-1);
350 pos
= strstr(buf
, "MemTotal: ");
354 if (sscanf(pos
, "MemTotal: %ld kB", &memsize
) != 1)
357 return memsize
/ 1024;
360 static int cpu_count(void)
368 f
= open("/proc/stat", O_RDONLY
);
372 len
= read(f
, buf
, sizeof(buf
)-1);
378 pos
= strstr(buf
, "cpu");
382 while (pos
!= NULL
) {
383 if (strncmp(pos
, "cpu", 3) == 0 &&isdigit(pos
[3]))
385 pos
= strstr(&pos
[3], "cpu");
393 static int running_processes(void)
401 f
= open("/proc/stat", O_RDONLY
);
405 len
= read(f
, buf
, sizeof(buf
)-1);
411 pos
= strstr(buf
, "procs_running ");
415 if (sscanf(pos
, "procs_running %u", &running
) != 1)
421 /* return the number of process es in our session, count only until limit */
422 static int running_processes_in_session(pid_t session
, int limit
)
428 dir
= opendir("/proc");
432 /* read process info from /proc */
433 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
439 pid_t ppid
, pgrp
, sess
;
442 if (!isdigit(dent
->d_name
[0]))
445 snprintf(procdir
, sizeof(procdir
), "/proc/%s/stat", dent
->d_name
);
446 procdir
[sizeof(procdir
)-1] = '\0';
448 f
= open(procdir
, O_RDONLY
);
452 len
= read(f
, line
, sizeof(line
)-1);
460 /* skip ugly program name */
461 pos
= strrchr(line
, ')') + 2;
465 if (sscanf(pos
, "%c %d %d %d ", &state
, &ppid
, &pgrp
, &sess
) != 4)
468 /* count only processes in our session */
472 /* count only running, no sleeping processes */
477 if (limit
> 0 && running
>= limit
)
485 static int compare_devpath(const char *running
, const char *waiting
)
489 for (i
= 0; i
< PATH_SIZE
; i
++) {
490 /* identical device event found */
491 if (running
[i
] == '\0' && waiting
[i
] == '\0')
494 /* parent device event found */
495 if (running
[i
] == '\0' && waiting
[i
] == '/')
498 /* child device event found */
499 if (running
[i
] == '/' && waiting
[i
] == '\0')
502 /* no matching event */
503 if (running
[i
] != waiting
[i
])
510 /* returns still running task for the same device, its parent or its physical device */
511 static int running_with_devpath(struct udevd_uevent_msg
*msg
, int limit
)
513 struct udevd_uevent_msg
*loop_msg
;
514 int childs_count
= 0;
516 list_for_each_entry(loop_msg
, &running_list
, node
) {
517 if (limit
&& childs_count
++ > limit
) {
518 dbg("%llu, maximum number (%i) of child reached", msg
->seqnum
, childs_count
);
522 /* return running parent/child device event */
523 if (compare_devpath(loop_msg
->devpath
, msg
->devpath
) != 0) {
524 dbg("%llu, child device event still running %llu (%s)",
525 msg
->seqnum
, loop_msg
->seqnum
, loop_msg
->devpath
);
529 /* return running physical device event */
530 if (msg
->physdevpath
&& msg
->action
&& strcmp(msg
->action
, "add") == 0)
531 if (compare_devpath(loop_msg
->devpath
, msg
->physdevpath
) != 0) {
532 dbg("%llu, physical device event still running %llu (%s)",
533 msg
->seqnum
, loop_msg
->seqnum
, loop_msg
->devpath
);
541 /* exec queue management routine executes the events and serializes events in the same sequence */
542 static void msg_queue_manager(void)
544 struct udevd_uevent_msg
*loop_msg
;
545 struct udevd_uevent_msg
*tmp_msg
;
548 if (list_empty(&exec_list
))
551 running
= running_processes();
552 dbg("%d processes runnning on system", running
);
554 running
= max_childs_running
;
556 list_for_each_entry_safe(loop_msg
, tmp_msg
, &exec_list
, node
) {
557 /* check running processes in our session and possibly throttle */
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
) {
562 dbg("delay seq %llu, too many processes already running", loop_msg
->seqnum
);
567 /* don't run two processes for the same devpath and wait for the parent*/
568 if (running_with_devpath(loop_msg
, max_childs
)) {
569 dbg("delay seq %llu (%s)", loop_msg
->seqnum
, loop_msg
->devpath
);
573 /* move event to run list */
574 list_move_tail(&loop_msg
->node
, &running_list
);
575 udev_event_run(loop_msg
);
577 dbg("moved seq %llu to running list", loop_msg
->seqnum
);
581 static struct udevd_uevent_msg
*get_msg_from_envbuf(const char *buf
, int buf_size
)
585 struct udevd_uevent_msg
*msg
;
586 char *physdevdriver_key
= NULL
;
590 msg
= malloc(sizeof(struct udevd_uevent_msg
) + buf_size
);
593 memset(msg
, 0x00, sizeof(struct udevd_uevent_msg
) + buf_size
);
595 /* copy environment buffer and reconstruct envp */
596 memcpy(msg
->envbuf
, buf
, buf_size
);
598 for (i
= 0; (bufpos
< buf_size
) && (i
< UEVENT_NUM_ENVP
-2); i
++) {
602 key
= &msg
->envbuf
[bufpos
];
603 keylen
= strlen(key
);
605 bufpos
+= keylen
+ 1;
606 dbg("add '%s' to msg.envp[%i]", msg
->envp
[i
], i
);
608 /* remember some keys for further processing */
609 if (strncmp(key
, "ACTION=", 7) == 0)
610 msg
->action
= &key
[7];
611 else if (strncmp(key
, "DEVPATH=", 8) == 0)
612 msg
->devpath
= &key
[8];
613 else if (strncmp(key
, "SUBSYSTEM=", 10) == 0)
614 msg
->subsystem
= &key
[10];
615 else if (strncmp(key
, "DRIVER=", 7) == 0)
616 msg
->driver
= &key
[7];
617 else if (strncmp(key
, "SEQNUM=", 7) == 0)
618 msg
->seqnum
= strtoull(&key
[7], NULL
, 10);
619 else if (strncmp(key
, "PHYSDEVPATH=", 12) == 0)
620 msg
->physdevpath
= &key
[12];
621 else if (strncmp(key
, "PHYSDEVDRIVER=", 14) == 0)
622 physdevdriver_key
= key
;
623 else if (strncmp(key
, "MAJOR=", 6) == 0)
624 maj
= strtoull(&key
[6], NULL
, 10);
625 else if (strncmp(key
, "MINOR=", 6) == 0)
626 min
= strtoull(&key
[6], NULL
, 10);
627 else if (strncmp(key
, "TIMEOUT=", 8) == 0)
628 msg
->timeout
= strtoull(&key
[8], NULL
, 10);
630 msg
->devt
= makedev(maj
, min
);
631 msg
->envp
[i
++] = "UDEVD_EVENT=1";
633 if (msg
->driver
== NULL
&& msg
->physdevpath
== NULL
&& physdevdriver_key
!= NULL
) {
634 /* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */
635 msg
->envp
[i
++] = &physdevdriver_key
[7];
636 msg
->driver
= &physdevdriver_key
[14];
641 if (msg
->devpath
== NULL
|| msg
->action
== NULL
) {
642 info("DEVPATH or ACTION missing, ignore message");
649 /* receive the udevd message from userspace */
650 static void get_ctrl_msg(void)
652 struct udevd_ctrl_msg ctrl_msg
;
655 struct cmsghdr
*cmsg
;
658 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
661 memset(&ctrl_msg
, 0x00, sizeof(struct udevd_ctrl_msg
));
662 iov
.iov_base
= &ctrl_msg
;
663 iov
.iov_len
= sizeof(struct udevd_ctrl_msg
);
665 memset(&smsg
, 0x00, sizeof(struct msghdr
));
668 smsg
.msg_control
= cred_msg
;
669 smsg
.msg_controllen
= sizeof(cred_msg
);
671 size
= recvmsg(udevd_sock
, &smsg
, 0);
674 err("unable to receive user udevd message: %s", strerror(errno
));
677 cmsg
= CMSG_FIRSTHDR(&smsg
);
678 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
680 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
681 err("no sender credentials received, message ignored");
685 if (cred
->uid
!= 0) {
686 err("sender uid=%i, message ignored", cred
->uid
);
690 if (strncmp(ctrl_msg
.magic
, UDEVD_CTRL_MAGIC
, sizeof(UDEVD_CTRL_MAGIC
)) != 0 ) {
691 err("message magic '%s' doesn't match, ignore it", ctrl_msg
.magic
);
695 switch (ctrl_msg
.type
) {
696 case UDEVD_CTRL_STOP_EXEC_QUEUE
:
697 info("udevd message (STOP_EXEC_QUEUE) received");
700 case UDEVD_CTRL_START_EXEC_QUEUE
:
701 info("udevd message (START_EXEC_QUEUE) received");
705 case UDEVD_CTRL_SET_LOG_LEVEL
:
706 intval
= (int *) ctrl_msg
.buf
;
707 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval
);
708 udev_log_priority
= *intval
;
709 sprintf(udev_log
, "UDEV_LOG=%i", udev_log_priority
);
712 case UDEVD_CTRL_SET_MAX_CHILDS
:
713 intval
= (int *) ctrl_msg
.buf
;
714 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval
);
715 max_childs
= *intval
;
717 case UDEVD_CTRL_SET_MAX_CHILDS_RUNNING
:
718 intval
= (int *) ctrl_msg
.buf
;
719 info("udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs=%i", *intval
);
720 max_childs_running
= *intval
;
722 case UDEVD_CTRL_RELOAD_RULES
:
723 info("udevd message (RELOAD_RULES) received");
727 err("unknown control message type");
731 /* receive the kernel user event message and do some sanity checks */
732 static struct udevd_uevent_msg
*get_netlink_msg(void)
734 struct udevd_uevent_msg
*msg
;
737 static char buffer
[UEVENT_BUFFER_SIZE
+512];
740 size
= recv(uevent_netlink_sock
, &buffer
, sizeof(buffer
), 0);
743 err("unable to receive kernel netlink message: %s", strerror(errno
));
747 if ((size_t)size
> sizeof(buffer
)-1)
748 size
= sizeof(buffer
)-1;
750 dbg("uevent_size=%zi", size
);
752 /* start of event payload */
753 bufpos
= strlen(buffer
)+1;
754 msg
= get_msg_from_envbuf(&buffer
[bufpos
], size
-bufpos
);
758 /* validate message */
759 pos
= strchr(buffer
, '@');
761 err("invalid uevent '%s'", buffer
);
767 if (msg
->action
== NULL
) {
768 info("no ACTION in payload found, skip event '%s'", buffer
);
773 if (strcmp(msg
->action
, buffer
) != 0) {
774 err("ACTION in payload does not match uevent, skip event '%s'", buffer
);
782 static void asmlinkage
sig_handler(int signum
)
790 /* set flag, then write to pipe if needed */
791 sigchilds_waiting
= 1;
798 /* write to pipe, which will wakeup select() in our mainloop */
799 write(signal_pipe
[WRITE_END
], "", 1);
802 static void udev_done(int pid
, int exitstatus
)
804 /* find msg associated with pid and delete it */
805 struct udevd_uevent_msg
*msg
;
807 list_for_each_entry(msg
, &running_list
, node
) {
808 if (msg
->pid
== pid
) {
809 info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg
->seqnum
, msg
->pid
,
810 exitstatus
, time(NULL
) - msg
->queue_time
);
811 msg
->exitstatus
= exitstatus
;
812 msg_queue_delete(msg
);
814 /* there may be events waiting with the same devpath */
821 static void reap_sigchilds(void)
827 pid
= waitpid(-1, &status
, WNOHANG
);
830 if (WIFEXITED(status
))
831 status
= WEXITSTATUS(status
);
832 else if (WIFSIGNALED(status
))
833 status
= WTERMSIG(status
) + 128;
836 udev_done(pid
, status
);
840 static int init_udevd_socket(void)
842 struct sockaddr_un saddr
;
844 const int feature_on
= 1;
847 memset(&saddr
, 0x00, sizeof(saddr
));
848 saddr
.sun_family
= AF_LOCAL
;
849 /* use abstract namespace for socket path */
850 strcpy(&saddr
.sun_path
[1], UDEVD_CTRL_SOCK_PATH
);
851 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(saddr
.sun_path
+1) + 1;
853 udevd_sock
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
854 if (udevd_sock
== -1) {
855 err("error getting socket: %s", strerror(errno
));
859 /* the bind takes care of ensuring only one copy running */
860 retval
= bind(udevd_sock
, (struct sockaddr
*) &saddr
, addrlen
);
862 err("bind failed: %s", strerror(errno
));
868 /* enable receiving of the sender credentials */
869 setsockopt(udevd_sock
, SOL_SOCKET
, SO_PASSCRED
, &feature_on
, sizeof(feature_on
));
874 static int init_uevent_netlink_sock(void)
876 struct sockaddr_nl snl
;
877 const int buffersize
= 16 * 1024 * 1024;
880 memset(&snl
, 0x00, sizeof(struct sockaddr_nl
));
881 snl
.nl_family
= AF_NETLINK
;
882 snl
.nl_pid
= getpid();
885 uevent_netlink_sock
= socket(PF_NETLINK
, SOCK_DGRAM
, NETLINK_KOBJECT_UEVENT
);
886 if (uevent_netlink_sock
== -1) {
887 err("error getting socket: %s", strerror(errno
));
891 /* set receive buffersize */
892 setsockopt(uevent_netlink_sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &buffersize
, sizeof(buffersize
));
894 retval
= bind(uevent_netlink_sock
, (struct sockaddr
*) &snl
, sizeof(struct sockaddr_nl
));
896 err("bind failed: %s", strerror(errno
));
897 close(uevent_netlink_sock
);
898 uevent_netlink_sock
= -1;
904 static void export_initial_seqnum(void)
906 char filename
[PATH_SIZE
];
911 strlcpy(filename
, sysfs_path
, sizeof(filename
));
912 strlcat(filename
, "/kernel/uevent_seqnum", sizeof(filename
));
913 fd
= open(filename
, O_RDONLY
);
915 len
= read(fd
, seqnum
, sizeof(seqnum
)-1);
919 strcpy(seqnum
, "0\n");
922 strlcpy(filename
, udev_root
, sizeof(filename
));
923 strlcat(filename
, "/" EVENT_SEQNUM
, sizeof(filename
));
924 create_path(filename
);
925 fd
= open(filename
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0644);
927 write(fd
, seqnum
, len
);
932 int main(int argc
, char *argv
[], char *envp
[])
936 struct sigaction act
;
941 static const struct option options
[] = {
942 { "daemon", 0, NULL
, 'd' },
943 { "debug-trace", 0, NULL
, 't' },
944 { "verbose", 0, NULL
, 'v' },
945 { "help", 0, NULL
, 'h' },
951 logging_init("udevd");
954 dbg("version %s", UDEV_VERSION
);
956 /* parse commandline options */
958 option
= getopt_long(argc
, argv
, "dtvh", options
, NULL
);
971 if (udev_log_priority
< LOG_INFO
)
972 udev_log_priority
= LOG_INFO
;
975 printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--verbose]\n");
983 fprintf(stderr
, "root privileges required\n");
984 err("root privileges required");
988 /* init sockets to receive events */
989 if (init_udevd_socket() < 0) {
990 if (errno
== EADDRINUSE
) {
991 fprintf(stderr
, "another udev daemon already running\n");
992 err("another udev daemon already running");
995 fprintf(stderr
, "error initializing udevd socket\n");
996 err("error initializing udevd socket");
1002 if (init_uevent_netlink_sock() < 0) {
1003 fprintf(stderr
, "error initializing netlink socket\n");
1004 err("error initializing netlink socket");
1009 /* setup signal handler pipe */
1010 retval
= pipe(signal_pipe
);
1012 err("error getting pipes: %s", strerror(errno
));
1016 retval
= fcntl(signal_pipe
[READ_END
], F_GETFL
, 0);
1018 err("error fcntl on read pipe: %s", strerror(errno
));
1021 retval
= fcntl(signal_pipe
[READ_END
], F_SETFL
, retval
| O_NONBLOCK
);
1023 err("error fcntl on read pipe: %s", strerror(errno
));
1027 retval
= fcntl(signal_pipe
[WRITE_END
], F_GETFL
, 0);
1029 err("error fcntl on write pipe: %s", strerror(errno
));
1032 retval
= fcntl(signal_pipe
[WRITE_END
], F_SETFL
, retval
| O_NONBLOCK
);
1034 err("error fcntl on write pipe: %s", strerror(errno
));
1038 /* parse the rules and keep them in memory */
1040 udev_rules_init(&rules
, 1);
1042 export_initial_seqnum();
1050 dbg("daemonized fork running");
1053 err("fork of daemon failed: %s", strerror(errno
));
1057 dbg("child [%u] running, parent exits", pid
);
1063 /* redirect std fd's */
1064 fd
= open("/dev/null", O_RDWR
);
1066 dup2(fd
, STDIN_FILENO
);
1068 dup2(fd
, STDOUT_FILENO
);
1069 dup2(fd
, STDERR_FILENO
);
1070 if (fd
> STDERR_FILENO
)
1073 err("error opening /dev/null: %s", strerror(errno
));
1075 /* set scheduling priority for the daemon */
1076 setpriority(PRIO_PROCESS
, 0, UDEVD_PRIORITY
);
1081 /* become session leader */
1083 dbg("our session is %d", sid
);
1085 /* OOM_DISABLE == -17 */
1086 fd
= open("/proc/self/oom_adj", O_RDWR
);
1088 err("error disabling OOM: %s", strerror(errno
));
1090 write(fd
, "-17", 3);
1094 /* set signal handlers */
1095 memset(&act
, 0x00, sizeof(struct sigaction
));
1096 act
.sa_handler
= (void (*)(int)) sig_handler
;
1097 sigemptyset(&act
.sa_mask
);
1098 act
.sa_flags
= SA_RESTART
;
1099 sigaction(SIGINT
, &act
, NULL
);
1100 sigaction(SIGTERM
, &act
, NULL
);
1101 sigaction(SIGCHLD
, &act
, NULL
);
1102 sigaction(SIGHUP
, &act
, NULL
);
1104 /* watch rules directory */
1105 inotify_fd
= inotify_init();
1106 if (inotify_fd
>= 0)
1107 inotify_add_watch(inotify_fd
, udev_rules_dir
, IN_CREATE
| IN_DELETE
| IN_MOVE
| IN_CLOSE_WRITE
);
1108 else if (errno
== ENOSYS
)
1109 err("the kernel does not support inotify, udevd can't monitor configuration file changes");
1111 err("inotify_init failed: %s", strerror(errno
));
1113 /* maximum limit of forked childs */
1114 value
= getenv("UDEVD_MAX_CHILDS");
1116 max_childs
= strtoul(value
, NULL
, 10);
1118 int memsize
= mem_size_mb();
1120 max_childs
= 128 + (memsize
/ 4);
1122 max_childs
= UDEVD_MAX_CHILDS
;
1124 info("initialize max_childs to %u", max_childs
);
1126 /* start to throttle forking if maximum number of _running_ childs is reached */
1127 value
= getenv("UDEVD_MAX_CHILDS_RUNNING");
1129 max_childs_running
= strtoull(value
, NULL
, 10);
1131 int cpus
= cpu_count();
1133 max_childs_running
= 8 + (8 * cpus
);
1135 max_childs_running
= UDEVD_MAX_CHILDS_RUNNING
;
1137 info("initialize max_childs_running to %u", max_childs_running
);
1139 /* clear environment for forked event processes */
1142 /* export log_priority , as called programs may want to follow that setting */
1143 sprintf(udev_log
, "UDEV_LOG=%i", udev_log_priority
);
1149 maxfd
= UDEV_MAX(maxfd
, uevent_netlink_sock
);
1150 maxfd
= UDEV_MAX(maxfd
, signal_pipe
[READ_END
]);
1151 maxfd
= UDEV_MAX(maxfd
, inotify_fd
);
1153 while (!udev_exit
) {
1154 struct udevd_uevent_msg
*msg
;
1158 FD_SET(signal_pipe
[READ_END
], &readfds
);
1159 FD_SET(udevd_sock
, &readfds
);
1160 FD_SET(uevent_netlink_sock
, &readfds
);
1161 if (inotify_fd
>= 0)
1162 FD_SET(inotify_fd
, &readfds
);
1164 fdcount
= select(maxfd
+1, &readfds
, NULL
, NULL
, NULL
);
1167 err("error in select: %s", strerror(errno
));
1171 /* get control message */
1172 if (FD_ISSET(udevd_sock
, &readfds
))
1175 /* get netlink message */
1176 if (FD_ISSET(uevent_netlink_sock
, &readfds
)) {
1177 msg
= get_netlink_msg();
1179 msg_queue_insert(msg
);
1182 /* received a signal, clear our notification pipe */
1183 if (FD_ISSET(signal_pipe
[READ_END
], &readfds
)) {
1186 read(signal_pipe
[READ_END
], &buf
, sizeof(buf
));
1189 /* rules directory inotify watch */
1190 if ((inotify_fd
>= 0) && FD_ISSET(inotify_fd
, &readfds
)) {
1193 /* discard all possible events, we can just reload the config */
1194 if ((ioctl(inotify_fd
, FIONREAD
, &nbytes
) == 0) && nbytes
) {
1198 buf
= malloc(nbytes
);
1200 err("error getting buffer for inotify, disable watching");
1204 read(inotify_fd
, buf
, nbytes
);
1209 /* rules changed, set by inotify or a HUP signal */
1210 if (reload_config
) {
1212 udev_rules_cleanup(&rules
);
1213 udev_rules_init(&rules
, 1);
1216 /* forked child has returned */
1217 if (sigchilds_waiting
) {
1218 sigchilds_waiting
= 0;
1225 msg_queue_manager();
1231 udev_rules_cleanup(&rules
);
1234 if (signal_pipe
[READ_END
] >= 0)
1235 close(signal_pipe
[READ_END
]);
1236 if (signal_pipe
[WRITE_END
] >= 0)
1237 close(signal_pipe
[WRITE_END
]);
1239 if (udevd_sock
>= 0)
1241 if (inotify_fd
>= 0)
1243 if (uevent_netlink_sock
>= 0)
1244 close(uevent_netlink_sock
);