chiark / gitweb /
[PATCH] udevd - fix socket path length
[elogind.git] / udevd.c
1 /*
2  * udevd.c - hotplug event serializer
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *
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.
10  *
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.
15  *
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.
19  *
20  */
21
22 #include <pthread.h>
23 #include <stddef.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <signal.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37
38 #include "list.h"
39 #include "udev.h"
40 #include "udev_version.h"
41 #include "udevd.h"
42 #include "logging.h"
43
44
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;
55
56 LIST_HEAD(msg_list);
57 LIST_HEAD(exec_list);
58 LIST_HEAD(running_list);
59
60
61 static void msg_dump_queue(void)
62 {
63         struct hotplug_msg *msg;
64
65         list_for_each_entry(msg, &msg_list, list)
66                 dbg("sequence %d in queue", msg->seqnum);
67 }
68
69 static void msg_dump(struct hotplug_msg *msg)
70 {
71         dbg("sequence %d, '%s', '%s', '%s'",
72             msg->seqnum, msg->action, msg->devpath, msg->subsystem);
73 }
74
75 static struct hotplug_msg *msg_create(void)
76 {
77         struct hotplug_msg *new_msg;
78
79         new_msg = malloc(sizeof(struct hotplug_msg));
80         if (new_msg == NULL) {
81                 dbg("error malloc");
82                 return NULL;
83         }
84         return new_msg;
85 }
86
87 static void msg_delete(struct hotplug_msg *msg)
88 {
89         if (msg != NULL)
90                 free(msg);
91 }
92
93 /* orders the message in the queue by sequence number */
94 static void msg_queue_insert(struct hotplug_msg *msg)
95 {
96         struct hotplug_msg *loop_msg;
97
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)
101                         break;
102         list_add_tail(&msg->list, &loop_msg->list);
103         dbg("queued message seq %d", msg->seqnum);
104
105         /* store timestamp of queuing */
106         msg->queue_time = time(NULL);
107
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);
112
113         return ;
114 }
115
116 /* forks event and removes event from run queue when finished */
117 static void *run_threads(void * parm)
118 {
119         pid_t pid;
120         struct hotplug_msg *msg;
121
122         msg = parm;
123         setenv("ACTION", msg->action, 1);
124         setenv("DEVPATH", msg->devpath, 1);
125
126         pid = fork();
127         switch (pid) {
128         case 0:
129                 /* child */
130                 execl(UDEV_BIN, "udev", msg->subsystem, NULL);
131                 dbg("exec of child failed");
132                 exit(1);
133                 break;
134         case -1:
135                 dbg("fork of child failed");
136                 goto exit;
137         default:
138                 /* wait for exit of child */
139                 dbg("==> exec seq %d [%d] working at '%s'",
140                     msg->seqnum, pid, msg->devpath);
141                 wait(NULL);
142                 dbg("<== exec seq %d came back", msg->seqnum);
143         }
144
145 exit:
146         /* remove event from run list */
147         pthread_mutex_lock(&running_lock);
148         list_del_init(&msg->list);
149         pthread_mutex_unlock(&running_lock);
150
151         msg_delete(msg);
152
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);
157
158         pthread_exit(0);
159 }
160
161 /* returns already running task with devpath */
162 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
163 {
164         struct hotplug_msg *loop_msg;
165         struct hotplug_msg *tmp_msg;
166
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)
169                         return loop_msg;
170         return NULL;
171 }
172
173 /* queue management executes the events and delays events for the same devpath */
174 static void *exec_queue_manager(void * parm)
175 {
176         struct hotplug_msg *loop_msg;
177         struct hotplug_msg *tmp_msg;
178         struct hotplug_msg *msg;
179         pthread_t run_tid;
180
181         while (1) {
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);
185                         if (msg == NULL) {
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);
190
191                                 pthread_create(&run_tid, &thr_attr, run_threads, (void *) loop_msg);
192
193                                 dbg("moved seq %d to running list", loop_msg->seqnum);
194                         } else {
195                                 dbg("delay seq %d, cause seq %d already working on '%s'",
196                                     loop_msg->seqnum, msg->seqnum, msg->devpath);
197                         }
198                 }
199                 pthread_mutex_unlock(&exec_lock);
200
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);
205         }
206 }
207
208 static void exec_queue_activate(void)
209 {
210         pthread_mutex_lock(&exec_active_lock);
211         pthread_cond_signal(&exec_active);
212         pthread_mutex_unlock(&exec_active_lock);
213 }
214
215 /* move message from incoming to exec queue */
216 static void msg_move_exec(struct list_head *head)
217 {
218         list_move_tail(head, &exec_list);
219         exec_queue_activate();
220 }
221
222 /* queue management thread handles the timeouts and dispatches the events */
223 static void *msg_queue_manager(void * parm)
224 {
225         struct hotplug_msg *loop_msg;
226         struct hotplug_msg *tmp_msg;
227         time_t msg_age = 0;
228         struct timespec tv;
229
230         while (1) {
231                 dbg("msg queue manager, next expected is %d", expected_seqnum);
232                 pthread_mutex_lock(&msg_lock);
233                 pthread_mutex_lock(&exec_lock);
234 recheck:
235                 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
236                         /* move event with expected sequence to the exec list */
237                         if (loop_msg->seqnum == expected_seqnum) {
238                                 msg_move_exec(&loop_msg->list);
239                                 expected_seqnum++;
240                                 dbg("moved seq %d to exec, next expected is %d",
241                                     loop_msg->seqnum, expected_seqnum);
242                                 continue;
243                         }
244
245                         /* move event with expired timeout to the exec list */
246                         msg_age = time(NULL) - loop_msg->queue_time;
247                         if (msg_age > EVENT_TIMEOUT_SEC-1) {
248                                 msg_move_exec(&loop_msg->list);
249                                 expected_seqnum = loop_msg->seqnum+1;
250                                 dbg("moved seq %d to exec, reset next expected to %d",
251                                     loop_msg->seqnum, expected_seqnum);
252                                 goto recheck;
253                         } else {
254                                 break;
255                         }
256                 }
257
258                 msg_dump_queue();
259                 pthread_mutex_unlock(&exec_lock);
260                 pthread_mutex_unlock(&msg_lock);
261
262                 /* wait until queue gets active or next message timeout expires */
263                 pthread_mutex_lock(&msg_active_lock);
264
265                 if (list_empty(&msg_list) == 0) {
266                         tv.tv_sec = time(NULL) + EVENT_TIMEOUT_SEC - msg_age;
267                         tv.tv_nsec = 0;
268                         dbg("next event expires in %li seconds",
269                             EVENT_TIMEOUT_SEC - msg_age);
270                         pthread_cond_timedwait(&msg_active, &msg_active_lock, &tv);
271                 } else {
272                         pthread_cond_wait(&msg_active, &msg_active_lock);
273                 }
274                 pthread_mutex_unlock(&msg_active_lock);
275         }
276 }
277
278 /* every connect creates a thread which gets the msg, queues it and exits */
279 static void *client_threads(void * parm)
280 {
281         int sock;
282         struct hotplug_msg *msg;
283         int retval;
284
285         sock = (int) parm;
286
287         msg = msg_create();
288         if (msg == NULL) {
289                 dbg("unable to store message");
290                 goto exit;
291         }
292
293         retval = recv(sock, msg, sizeof(struct hotplug_msg), 0);
294         if (retval <  0) {
295                 dbg("unable to receive message");
296                 goto exit;
297         }
298
299         if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
300                 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
301                 msg_delete(msg);
302                 goto exit;
303         }
304
305         /* if no seqnum is given, we move straight to exec queue */
306         if (msg->seqnum == 0) {
307                 pthread_mutex_lock(&exec_lock);
308                 list_add(&msg->list, &exec_list);
309                 exec_queue_activate();
310                 pthread_mutex_unlock(&exec_lock);
311         } else {
312                 pthread_mutex_lock(&msg_lock);
313                 msg_queue_insert(msg);
314                 pthread_mutex_unlock(&msg_lock);
315         }
316
317 exit:
318         close(sock);
319         pthread_exit(0);
320 }
321
322 static void sig_handler(int signum)
323 {
324         switch (signum) {
325                 case SIGINT:
326                 case SIGTERM:
327                         unlink(UDEVD_LOCK);
328                         exit(20 + signum);
329                         break;
330                 default:
331                         dbg("unhandled signal");
332         }
333 }
334
335 static int one_and_only(void)
336 {
337         char string[50];
338         int lock_file;
339
340         lock_file = open(UDEVD_LOCK, O_RDWR | O_CREAT, 0x640);
341         if (lock_file < 0)
342                 return -1;
343
344         /* see if we can lock */
345         if (lockf(lock_file, F_TLOCK, 0) < 0) {
346                 dbg("file is already locked, exit");
347                 close(lock_file);
348                 return -1;
349         }
350
351         snprintf(string, sizeof(string), "%d\n", getpid());
352         write(lock_file, string, strlen(string));
353
354         return 0;
355 }
356
357 int main(int argc, char *argv[])
358 {
359         int ssock;
360         int csock;
361         struct sockaddr_un saddr;
362         struct sockaddr_un caddr;
363         socklen_t addrlen;
364         socklen_t clen;
365         pthread_t cli_tid;
366         pthread_t mgr_msg_tid;
367         pthread_t mgr_exec_tid;
368         int retval;
369
370         init_logging("udevd");
371
372         /* only let one version of the daemon run at any one time */
373         if (one_and_only() != 0)
374                 exit(0);
375
376         signal(SIGINT, sig_handler);
377         signal(SIGTERM, sig_handler);
378
379         memset(&saddr, 0x00, sizeof(saddr));
380         saddr.sun_family = AF_LOCAL;
381         /* use abstract namespace for socket path */
382         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
383         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
384
385         ssock = socket(AF_LOCAL, SOCK_STREAM, 0);
386         if (ssock == -1) {
387                 dbg("error getting socket");
388                 exit(1);
389         }
390
391         retval = bind(ssock, &saddr, addrlen);
392         if (retval < 0) {
393                 dbg("bind failed\n");
394                 goto exit;
395         }
396
397         retval = listen(ssock, SOMAXCONN);
398         if (retval < 0) {
399                 dbg("listen failed\n");
400                 goto exit;
401         }
402
403         pthread_mutex_init(&msg_lock, NULL);
404         pthread_mutex_init(&msg_active_lock, NULL);
405         pthread_mutex_init(&exec_lock, NULL);
406         pthread_mutex_init(&exec_active_lock, NULL);
407         pthread_mutex_init(&running_lock, NULL);
408
409         /* set default attributes for created threads */
410         pthread_attr_init(&thr_attr);
411         pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
412         pthread_attr_setstacksize(&thr_attr, 16 * 1024);
413
414         /* init queue management */
415         pthread_create(&mgr_msg_tid, &thr_attr, msg_queue_manager, NULL);
416         pthread_create(&mgr_exec_tid, &thr_attr, exec_queue_manager, NULL);
417
418         clen = sizeof(caddr);
419         /* main loop */
420         while (1) {
421                 csock = accept(ssock, &caddr, &clen);
422                 if (csock < 0) {
423                         dbg("client accept failed\n");
424                         continue;
425                 }
426                 pthread_create(&cli_tid, &thr_attr, client_threads, (void *) csock);
427         }
428 exit:
429         close(ssock);
430         exit(1);
431 }