2 * udevd.c - hotplug event serializer
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <sys/types.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 #include "udev_version.h"
45 unsigned char logname[42];
46 static pthread_mutex_t msg_lock;
47 static pthread_mutex_t msg_active_lock;
48 static pthread_cond_t msg_active;
49 static pthread_mutex_t exec_lock;
50 static pthread_mutex_t exec_active_lock;
51 static pthread_cond_t exec_active;
52 static pthread_mutex_t running_lock;
53 static pthread_attr_t thr_attr;
54 static int expected_seqnum = 0;
58 LIST_HEAD(running_list);
61 static void msg_dump_queue(void)
63 struct hotplug_msg *msg;
65 list_for_each_entry(msg, &msg_list, list)
66 dbg("sequence %d in queue", msg->seqnum);
69 static void msg_dump(struct hotplug_msg *msg)
71 dbg("sequence %d, '%s', '%s', '%s'",
72 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
75 static struct hotplug_msg *msg_create(void)
77 struct hotplug_msg *new_msg;
79 new_msg = malloc(sizeof(struct hotplug_msg));
80 if (new_msg == NULL) {
87 static void msg_delete(struct hotplug_msg *msg)
93 /* orders the message in the queue by sequence number */
94 static void msg_queue_insert(struct hotplug_msg *msg)
96 struct hotplug_msg *loop_msg;
98 /* sort message by sequence number into list*/
99 list_for_each_entry(loop_msg, &msg_list, list)
100 if (loop_msg->seqnum > msg->seqnum)
102 list_add_tail(&msg->list, &loop_msg->list);
103 dbg("queued message seq %d", msg->seqnum);
105 /* store timestamp of queuing */
106 msg->queue_time = time(NULL);
108 /* signal queue activity to manager */
109 pthread_mutex_lock(&msg_active_lock);
110 pthread_cond_signal(&msg_active);
111 pthread_mutex_unlock(&msg_active_lock);
116 /* forks event and removes event from run queue when finished */
117 static void *run_threads(void * parm)
120 struct hotplug_msg *msg;
123 setenv("ACTION", msg->action, 1);
124 setenv("DEVPATH", msg->devpath, 1);
130 execl(UDEV_BIN, "udev", msg->subsystem, NULL);
131 dbg("exec of child failed");
135 dbg("fork of child failed");
138 /* wait for exit of child */
139 dbg("==> exec seq %d [%d] working at '%s'",
140 msg->seqnum, pid, msg->devpath);
142 dbg("<== exec seq %d came back", msg->seqnum);
146 /* remove event from run list */
147 pthread_mutex_lock(&running_lock);
148 list_del_init(&msg->list);
149 pthread_mutex_unlock(&running_lock);
153 /* signal queue activity to exec manager */
154 pthread_mutex_lock(&exec_active_lock);
155 pthread_cond_signal(&exec_active);
156 pthread_mutex_unlock(&exec_active_lock);
161 /* returns already running task with devpath */
162 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
164 struct hotplug_msg *loop_msg;
165 struct hotplug_msg *tmp_msg;
167 list_for_each_entry_safe(loop_msg, tmp_msg, &running_list, list)
168 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
173 /* queue management executes the events and delays events for the same devpath */
174 static void *exec_queue_manager(void * parm)
176 struct hotplug_msg *loop_msg;
177 struct hotplug_msg *tmp_msg;
178 struct hotplug_msg *msg;
182 pthread_mutex_lock(&exec_lock);
183 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
184 msg = running_with_devpath(loop_msg);
186 /* move event to run list */
187 pthread_mutex_lock(&running_lock);
188 list_move_tail(&loop_msg->list, &running_list);
189 pthread_mutex_unlock(&running_lock);
191 pthread_create(&run_tid, &thr_attr, run_threads, (void *) loop_msg);
193 dbg("moved seq %d to running list", loop_msg->seqnum);
195 dbg("delay seq %d, cause seq %d already working on '%s'",
196 loop_msg->seqnum, msg->seqnum, msg->devpath);
199 pthread_mutex_unlock(&exec_lock);
201 /* wait for activation, new events or childs coming back */
202 pthread_mutex_lock(&exec_active_lock);
203 pthread_cond_wait(&exec_active, &exec_active_lock);
204 pthread_mutex_unlock(&exec_active_lock);
208 /* move message from incoming to exec queue */
209 static void msg_move_exec(struct list_head *head)
211 list_move_tail(head, &exec_list);
212 /* signal queue activity to manager */
213 pthread_mutex_lock(&exec_active_lock);
214 pthread_cond_signal(&exec_active);
215 pthread_mutex_unlock(&exec_active_lock);
218 /* queue management thread handles the timeouts and dispatches the events */
219 static void *msg_queue_manager(void * parm)
221 struct hotplug_msg *loop_msg;
222 struct hotplug_msg *tmp_msg;
227 dbg("msg queue manager, next expected is %d", expected_seqnum);
228 pthread_mutex_lock(&msg_lock);
229 pthread_mutex_lock(&exec_lock);
231 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
232 /* move event with expected sequence to the exec list */
233 if (loop_msg->seqnum == expected_seqnum) {
234 msg_move_exec(&loop_msg->list);
236 dbg("moved seq %d to exec, next expected is %d",
237 loop_msg->seqnum, expected_seqnum);
241 /* move event with expired timeout to the exec list */
242 msg_age = time(NULL) - loop_msg->queue_time;
243 if (msg_age > EVENT_TIMEOUT_SEC-1) {
244 msg_move_exec(&loop_msg->list);
245 expected_seqnum = loop_msg->seqnum+1;
246 dbg("moved seq %d to exec, reset next expected to %d",
247 loop_msg->seqnum, expected_seqnum);
255 pthread_mutex_unlock(&exec_lock);
256 pthread_mutex_unlock(&msg_lock);
258 /* wait until queue gets active or next message timeout expires */
259 pthread_mutex_lock(&msg_active_lock);
261 if (list_empty(&msg_list) == 0) {
262 tv.tv_sec = time(NULL) + EVENT_TIMEOUT_SEC - msg_age;
264 dbg("next event expires in %li seconds",
265 EVENT_TIMEOUT_SEC - msg_age);
266 pthread_cond_timedwait(&msg_active, &msg_active_lock, &tv);
268 pthread_cond_wait(&msg_active, &msg_active_lock);
270 pthread_mutex_unlock(&msg_active_lock);
274 /* every connect creates a thread which gets the msg, queues it and exits */
275 static void *client_threads(void * parm)
278 struct hotplug_msg *msg;
285 dbg("unable to store message");
289 retval = recv(sock, msg, sizeof(struct hotplug_msg), 0);
291 dbg("unable to receive message");
295 if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
296 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
301 pthread_mutex_lock(&msg_lock);
302 msg_queue_insert(msg);
303 pthread_mutex_unlock(&msg_lock);
310 static void sig_handler(int signum)
320 dbg("unhandled signal");
324 static int one_and_only(void)
329 lock_file = open(UDEVD_LOCK, O_RDWR | O_CREAT, 0x640);
333 /* see if we can lock */
334 if (lockf(lock_file, F_TLOCK, 0) < 0) {
335 dbg("file is already locked, exit");
340 snprintf(string, sizeof(string), "%d\n", getpid());
341 write(lock_file, string, strlen(string));
346 int main(int argc, char *argv[])
350 struct sockaddr_un saddr;
351 struct sockaddr_un caddr;
354 pthread_t mgr_msg_tid;
355 pthread_t mgr_exec_tid;
358 init_logging("udevd");
360 /* only let one version of the daemon run at any one time */
361 if (one_and_only() != 0)
364 signal(SIGINT, sig_handler);
365 signal(SIGTERM, sig_handler);
367 memset(&saddr, 0x00, sizeof(saddr));
368 saddr.sun_family = AF_LOCAL;
369 strcpy(saddr.sun_path, UDEVD_SOCK);
372 ssock = socket(AF_LOCAL, SOCK_STREAM, 0);
374 dbg("error getting socket");
378 retval = bind(ssock, &saddr, sizeof(saddr));
380 dbg("bind failed\n");
384 retval = listen(ssock, SOMAXCONN);
386 dbg("listen failed\n");
390 pthread_mutex_init(&msg_lock, NULL);
391 pthread_mutex_init(&msg_active_lock, NULL);
392 pthread_mutex_init(&exec_lock, NULL);
393 pthread_mutex_init(&exec_active_lock, NULL);
394 pthread_mutex_init(&running_lock, NULL);
396 /* set default attributes for created threads */
397 pthread_attr_init(&thr_attr);
398 pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
399 pthread_attr_setstacksize(&thr_attr, 16 * 1024);
401 /* init queue management */
402 pthread_create(&mgr_msg_tid, &thr_attr, msg_queue_manager, NULL);
403 pthread_create(&mgr_exec_tid, &thr_attr, exec_queue_manager, NULL);
405 clen = sizeof(caddr);
408 csock = accept(ssock, &caddr, &clen);
410 dbg("client accept failed\n");
413 pthread_create(&cli_tid, &thr_attr, client_threads, (void *) csock);