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.
23 #include <sys/types.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
40 #include "udev_version.h"
45 static pthread_mutex_t msg_lock;
46 static pthread_mutex_t msg_active_lock;
47 static pthread_cond_t msg_active;
48 static pthread_mutex_t exec_lock;
49 static pthread_mutex_t exec_active_lock;
50 static pthread_cond_t exec_active;
51 static pthread_mutex_t running_lock;
52 static pthread_attr_t thr_attr;
53 static int expected_seqnum = 0;
57 LIST_HEAD(running_list);
60 static void msg_dump_queue(void)
62 struct hotplug_msg *msg;
64 list_for_each_entry(msg, &msg_list, list)
65 dbg("sequence %d in queue", msg->seqnum);
68 static void msg_dump(struct hotplug_msg *msg)
70 dbg("sequence %d, '%s', '%s', '%s'",
71 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
74 /* allocates a new message */
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) {
84 memset(new_msg, 0x00, sizeof(struct hotplug_msg));
88 /* orders the message in the queue by sequence number */
89 static void msg_queue_insert(struct hotplug_msg *msg)
91 struct hotplug_msg *loop_msg;
93 /* sort message by sequence number into list*/
94 list_for_each_entry(loop_msg, &msg_list, list)
95 if (loop_msg->seqnum > msg->seqnum)
97 list_add_tail(&msg->list, &loop_msg->list);
98 dbg("queued message seq %d", msg->seqnum);
100 /* store timestamp of queuing */
101 msg->queue_time = time(NULL);
103 /* signal queue activity to manager */
104 pthread_mutex_lock(&msg_active_lock);
105 pthread_cond_signal(&msg_active);
106 pthread_mutex_unlock(&msg_active_lock);
111 /* forks event and removes event from run queue when finished */
112 static void *run_threads(void * parm)
115 struct hotplug_msg *msg;
118 setenv("ACTION", msg->action, 1);
119 setenv("DEVPATH", msg->devpath, 1);
125 execl(UDEV_BIN, "udev", msg->subsystem, NULL);
126 dbg("exec of child failed");
130 dbg("fork of child failed");
133 /* wait for exit of child */
134 dbg("==> exec seq %d [%d] working at '%s'",
135 msg->seqnum, pid, msg->devpath);
137 dbg("<== exec seq %d came back", msg->seqnum);
141 /* remove event from run list */
142 pthread_mutex_lock(&running_lock);
143 list_del_init(&msg->list);
144 pthread_mutex_unlock(&running_lock);
148 /* signal queue activity to exec manager */
149 pthread_mutex_lock(&exec_active_lock);
150 pthread_cond_signal(&exec_active);
151 pthread_mutex_unlock(&exec_active_lock);
156 /* returns already running task with devpath */
157 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
159 struct hotplug_msg *loop_msg;
160 struct hotplug_msg *tmp_msg;
162 list_for_each_entry_safe(loop_msg, tmp_msg, &running_list, list)
163 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
168 /* queue management executes the events and delays events for the same devpath */
169 static void *exec_queue_manager(void * parm)
171 struct hotplug_msg *loop_msg;
172 struct hotplug_msg *tmp_msg;
173 struct hotplug_msg *msg;
177 pthread_mutex_lock(&exec_lock);
178 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
179 msg = running_with_devpath(loop_msg);
181 /* move event to run list */
182 pthread_mutex_lock(&running_lock);
183 list_move_tail(&loop_msg->list, &running_list);
184 pthread_mutex_unlock(&running_lock);
186 pthread_create(&run_tid, &thr_attr, run_threads, (void *) loop_msg);
188 dbg("moved seq %d to running list", loop_msg->seqnum);
190 dbg("delay seq %d, cause seq %d already working on '%s'",
191 loop_msg->seqnum, msg->seqnum, msg->devpath);
194 pthread_mutex_unlock(&exec_lock);
196 /* wait for activation, new events or childs coming back */
197 pthread_mutex_lock(&exec_active_lock);
198 pthread_cond_wait(&exec_active, &exec_active_lock);
199 pthread_mutex_unlock(&exec_active_lock);
203 /* move message from incoming to exec queue */
204 static void msg_move_exec(struct list_head *head)
206 list_move_tail(head, &exec_list);
207 /* signal queue activity to manager */
208 pthread_mutex_lock(&exec_active_lock);
209 pthread_cond_signal(&exec_active);
210 pthread_mutex_unlock(&exec_active_lock);
213 /* queue management thread handles the timeouts and dispatches the events */
214 static void *msg_queue_manager(void * parm)
216 struct hotplug_msg *loop_msg;
217 struct hotplug_msg *tmp_msg;
222 dbg("msg queue manager, next expected is %d", expected_seqnum);
223 pthread_mutex_lock(&msg_lock);
224 pthread_mutex_lock(&exec_lock);
226 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
227 /* move event with expected sequence to the exec list */
228 if (loop_msg->seqnum == expected_seqnum) {
229 msg_move_exec(&loop_msg->list);
231 dbg("moved seq %d to exec, next expected is %d",
232 loop_msg->seqnum, expected_seqnum);
236 /* move event with expired timeout to the exec list */
237 msg_age = time(NULL) - loop_msg->queue_time;
238 if (msg_age > EVENT_TIMEOUT_SEC-1) {
239 msg_move_exec(&loop_msg->list);
240 expected_seqnum = loop_msg->seqnum+1;
241 dbg("moved seq %d to exec, reset next expected to %d",
242 loop_msg->seqnum, expected_seqnum);
250 pthread_mutex_unlock(&exec_lock);
251 pthread_mutex_unlock(&msg_lock);
253 /* wait until queue gets active or next message timeout expires */
254 pthread_mutex_lock(&msg_active_lock);
256 if (list_empty(&msg_list) == 0) {
257 tv.tv_sec = time(NULL) + EVENT_TIMEOUT_SEC - msg_age;
259 dbg("next event expires in %li seconds",
260 EVENT_TIMEOUT_SEC - msg_age);
261 pthread_cond_timedwait(&msg_active, &msg_active_lock, &tv);
263 pthread_cond_wait(&msg_active, &msg_active_lock);
265 pthread_mutex_unlock(&msg_active_lock);
269 /* every connect creates a thread which gets the msg, queues it and exits */
270 static void *client_threads(void * parm)
273 struct hotplug_msg *msg;
280 dbg("unable to store message");
284 retval = recv(sock, msg, sizeof(struct hotplug_msg), 0);
286 dbg("unable to receive message");
290 if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
291 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
295 pthread_mutex_lock(&msg_lock);
296 msg_queue_insert(msg);
297 pthread_mutex_unlock(&msg_lock);
304 static void sig_handler(int signum)
310 unlink(UDEVD_SOCKET);
314 dbg("unhandled signal");
318 static int one_and_only(void)
323 /* see if we can open */
324 lock_file = open(UDEVD_LOCK, O_RDWR | O_CREAT, 0x640);
328 /* see if we can lock */
329 if (lockf(lock_file, F_TLOCK, 0) < 0) {
330 dbg("file is already locked, exit");
335 snprintf(string, sizeof(string), "%d\n", getpid());
336 write(lock_file, string, strlen(string));
341 int main(int argc, char *argv[])
345 struct sockaddr_un saddr;
346 struct sockaddr_un caddr;
349 pthread_t mgr_msg_tid;
350 pthread_t mgr_exec_tid;
353 /* only let one version of the daemon run at any one time */
354 if (one_and_only() != 0)
357 signal(SIGINT, sig_handler);
358 signal(SIGTERM, sig_handler);
360 memset(&saddr, 0x00, sizeof(saddr));
361 saddr.sun_family = AF_LOCAL;
362 strcpy(saddr.sun_path, UDEVD_SOCKET);
364 unlink(UDEVD_SOCKET);
365 ssock = socket(AF_LOCAL, SOCK_STREAM, 0);
367 dbg("error getting socket");
371 retval = bind(ssock, &saddr, sizeof(saddr));
373 dbg("bind failed\n");
377 retval = listen(ssock, SOMAXCONN);
379 dbg("listen failed\n");
383 pthread_mutex_init(&msg_lock, NULL);
384 pthread_mutex_init(&msg_active_lock, NULL);
385 pthread_mutex_init(&exec_lock, NULL);
386 pthread_mutex_init(&exec_active_lock, NULL);
387 pthread_mutex_init(&running_lock, NULL);
389 /* set default attributes for created threads */
390 pthread_attr_init(&thr_attr);
391 pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
393 /* init queue management */
394 pthread_create(&mgr_msg_tid, &thr_attr, msg_queue_manager, NULL);
395 pthread_create(&mgr_exec_tid, &thr_attr, exec_queue_manager, NULL);
397 clen = sizeof(caddr);
400 csock = accept(ssock, &caddr, &clen);
404 dbg("client accept failed\n");
406 pthread_create(&cli_tid, &thr_attr, client_threads, (void *) csock);
410 unlink(UDEVD_SOCKET);