chiark / gitweb /
[PATCH] udevd race conditions and performance, assorted cleanups
[elogind.git] / udevd.c
1 /*
2  * udevd.c - hotplug event serializer
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
6  *
7  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation version 2 of the License.
11  *
12  *      This program is distributed in the hope that it will be useful, but
13  *      WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *      General Public License for more details.
16  *
17  *      You should have received a copy of the GNU General Public License along
18  *      with this program; if not, write to the Free Software Foundation, Inc.,
19  *      675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stddef.h>
24 #include <sys/wait.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <sys/time.h>
36 #include <fcntl.h>
37
38 #include "list.h"
39 #include "udev.h"
40 #include "udev_lib.h"
41 #include "udev_version.h"
42 #include "udevd.h"
43 #include "logging.h"
44
45 static int pipefds[2];
46 static int expected_seqnum = 0;
47 volatile static int children_waiting;
48 volatile static int run_msg_q;
49 volatile static int sig_flag;
50 static int run_exec_q;
51
52 static LIST_HEAD(msg_list);
53 static LIST_HEAD(exec_list);
54 static LIST_HEAD(running_list);
55
56 static void exec_queue_manager(void);
57 static void msg_queue_manager(void);
58 static void user_sighandler(void);
59 static void reap_kids(void);
60
61 #ifdef LOG
62 unsigned char logname[LOGNAME_SIZE];
63 void log_message (int level, const char *format, ...)
64 {
65         va_list args;
66
67         va_start(args, format);
68         vsyslog(level, format, args);
69         va_end(args);
70 }
71 #endif
72
73 static void msg_dump_queue(void)
74 {
75 #ifdef DEBUG
76         struct hotplug_msg *msg;
77
78         list_for_each_entry(msg, &msg_list, list)
79                 dbg("sequence %d in queue", msg->seqnum);
80 #endif
81 }
82
83 static void msg_dump(struct hotplug_msg *msg)
84 {
85         dbg("sequence %d, '%s', '%s', '%s'",
86             msg->seqnum, msg->action, msg->devpath, msg->subsystem);
87 }
88
89 static struct hotplug_msg *msg_create(void)
90 {
91         struct hotplug_msg *new_msg;
92
93         new_msg = malloc(sizeof(struct hotplug_msg));
94         if (new_msg == NULL)
95                 dbg("error malloc");
96         return new_msg;
97 }
98
99 static void run_queue_delete(struct hotplug_msg *msg)
100 {
101         list_del(&msg->list);
102         free(msg);
103 }
104
105 /* orders the message in the queue by sequence number */
106 static void msg_queue_insert(struct hotplug_msg *msg)
107 {
108         struct hotplug_msg *loop_msg;
109
110         /* sort message by sequence number into list. events
111          * will tend to come in order, so scan the list backwards
112          */
113         list_for_each_entry_reverse(loop_msg, &msg_list, list)
114                 if (loop_msg->seqnum < msg->seqnum)
115                         break;
116         list_add(&msg->list, &loop_msg->list);
117         dbg("queued message seq %d", msg->seqnum);
118
119         /* store timestamp of queuing */
120         msg->queue_time = time(NULL);
121
122         /* run msg queue manager */
123         run_msg_q = 1;
124
125         return ;
126 }
127
128 /* forks event and removes event from run queue when finished */
129 static void udev_run(struct hotplug_msg *msg)
130 {
131         pid_t pid;
132         char action[ACTION_SIZE];
133         char devpath[DEVPATH_SIZE];
134         char *env[] = { action, devpath, NULL };
135
136         snprintf(action, sizeof(action), "ACTION=%s", msg->action);
137         snprintf(devpath, sizeof(devpath), "DEVPATH=%s", msg->devpath);
138
139         pid = fork();
140         switch (pid) {
141         case 0:
142                 /* child */
143                 execle(UDEV_BIN, "udev", msg->subsystem, NULL, env);
144                 dbg("exec of child failed");
145                 exit(1);
146                 break;
147         case -1:
148                 dbg("fork of child failed");
149                 run_queue_delete(msg);
150                 /* note: we never managed to run, so we had no impact on 
151                  * running_with_devpath(), so don't bother setting run_exec_q
152                  */
153                 break;
154         default:
155                 /* get SIGCHLD in main loop */
156                 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
157                 msg->pid = pid;
158         }
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         list_for_each_entry(loop_msg, &running_list, list)
166                 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
167                         return loop_msg;
168         return NULL;
169 }
170
171 /* exec queue management routine executes the events and delays events for the same devpath */
172 static void exec_queue_manager()
173 {
174         struct hotplug_msg *loop_msg;
175         struct hotplug_msg *tmp_msg;
176         struct hotplug_msg *msg;
177
178         list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
179                 msg = running_with_devpath(loop_msg);
180                 if (!msg) {
181                         /* move event to run list */
182                         list_move_tail(&loop_msg->list, &running_list);
183                         udev_run(loop_msg);
184                         dbg("moved seq %d to running list", loop_msg->seqnum);
185                 } else {
186                         dbg("delay seq %d, cause seq %d already working on '%s'",
187                                 loop_msg->seqnum, msg->seqnum, msg->devpath);
188                 }
189         }
190 }
191
192 static void msg_move_exec(struct hotplug_msg *msg)
193 {
194         list_move_tail(&msg->list, &exec_list);
195         run_exec_q = 1;
196         expected_seqnum = msg->seqnum+1;
197         dbg("moved seq %d to exec, next expected is %d",
198                 msg->seqnum, expected_seqnum);
199 }
200
201 /* msg queue management routine handles the timeouts and dispatches the events */
202 static void msg_queue_manager()
203 {
204         struct hotplug_msg *loop_msg;
205         struct hotplug_msg *tmp_msg;
206         time_t msg_age = 0;
207
208         dbg("msg queue manager, next expected is %d", expected_seqnum);
209 recheck:
210         list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
211                 /* move event with expected sequence to the exec list */
212                 if (loop_msg->seqnum == expected_seqnum) {
213                         msg_move_exec(loop_msg);
214                         continue;
215                 }
216
217                 /* move event with expired timeout to the exec list */
218                 msg_age = time(NULL) - loop_msg->queue_time;
219                 if (msg_age > EVENT_TIMEOUT_SEC-1) {
220                         msg_move_exec(loop_msg);
221                         goto recheck;
222                 } else {
223                         break;
224                 }
225         }
226
227         msg_dump_queue();
228
229         if (list_empty(&msg_list) == 0) {
230                 /* set timeout for remaining queued events */
231                 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
232                 dbg("next event expires in %li seconds",
233                     EVENT_TIMEOUT_SEC - msg_age);
234                 setitimer(ITIMER_REAL, &itv, 0);
235         }
236 }
237
238 /* receive the msg, do some basic sanity checks, and queue it */
239 static void handle_msg(int sock)
240 {
241         struct hotplug_msg *msg;
242         int retval;
243         struct msghdr smsg;
244         struct cmsghdr *cmsg;
245         struct iovec iov;
246         struct ucred *cred;
247         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
248
249         msg = msg_create();
250         if (msg == NULL) {
251                 dbg("unable to store message");
252                 return;
253         }
254
255         iov.iov_base = msg;
256         iov.iov_len = sizeof(struct hotplug_msg);
257
258         memset(&smsg, 0x00, sizeof(struct msghdr));
259         smsg.msg_iov = &iov;
260         smsg.msg_iovlen = 1;
261         smsg.msg_control = cred_msg;
262         smsg.msg_controllen = sizeof(cred_msg);
263
264         retval = recvmsg(sock, &smsg, 0);
265         if (retval <  0) {
266                 if (errno != EINTR)
267                         dbg("unable to receive message");
268                 return;
269         }
270         cmsg = CMSG_FIRSTHDR(&smsg);
271         cred = (struct ucred *) CMSG_DATA(cmsg);
272
273         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
274                 dbg("no sender credentials received, message ignored");
275                 goto skip;
276         }
277
278         if (cred->uid != 0) {
279                 dbg("sender uid=%i, message ignored", cred->uid);
280                 goto skip;
281         }
282
283         if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
284                 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
285                 goto skip;
286         }
287
288         /* if no seqnum is given, we move straight to exec queue */
289         if (msg->seqnum == -1) {
290                 list_add(&msg->list, &exec_list);
291                 run_exec_q = 1;
292         } else {
293                 msg_queue_insert(msg);
294         }
295         return;
296
297 skip:
298         free(msg);
299         return;
300 }
301
302 static void sig_handler(int signum)
303 {
304         int rc;
305         switch (signum) {
306                 case SIGINT:
307                 case SIGTERM:
308                         exit(20 + signum);
309                         break;
310                 case SIGALRM:
311                         /* set flag, then write to pipe if needed */
312                         run_msg_q = 1;
313                         goto do_write;
314                         break;
315                 case SIGCHLD:
316                         /* set flag, then write to pipe if needed */
317                         children_waiting = 1;
318                         goto do_write;
319                         break;
320                 default:
321                         dbg("unhandled signal");
322                         return;
323         }
324         
325 do_write:
326         /* if pipe is empty, write to pipe to force select to return
327          * immediately when it gets called
328          */
329         if (!sig_flag) {
330                 rc = write(pipefds[1],&signum,sizeof(signum));
331                 if (rc < 0)
332                         dbg("unable to write to pipe");
333                 else
334                         sig_flag = 1;
335         }
336 }
337
338 static void udev_done(int pid)
339 {
340         /* find msg associated with pid and delete it */
341         struct hotplug_msg *msg;
342
343         list_for_each_entry(msg, &running_list, list) {
344                 if (msg->pid == pid) {
345                         dbg("<== exec seq %d came back", msg->seqnum);
346                         run_queue_delete(msg);
347                         
348                         /* we want to run the exec queue manager since there may
349                          * be events waiting with the devpath of the one that
350                          * just finished
351                          */
352                         run_exec_q = 1;
353                         return;
354                 }
355         }
356 }
357
358 static void reap_kids()
359 {
360         /* reap all dead children */
361         while(1) {
362                 int pid = waitpid(-1, 0, WNOHANG);
363                 if ((pid == -1) || (pid == 0))
364                         break;
365                 udev_done(pid);
366         }
367 }
368
369 /* just read everything from the pipe and clear the flag,
370  * the useful flags were set in the signal handler
371  */
372 static void user_sighandler()
373 {
374         int sig;
375         while(1) {
376                 int rc = read(pipefds[0],&sig,sizeof(sig));
377                 if (rc < 0)
378                         break;
379
380                 sig_flag = 0;
381         }
382 }
383
384
385 int main(int argc, char *argv[])
386 {
387         int ssock, maxsockplus;
388         struct sockaddr_un saddr;
389         socklen_t addrlen;
390         int retval;
391         const int on = 1;
392         struct sigaction act;
393         fd_set readfds;
394
395         init_logging("udevd");
396         dbg("version %s", UDEV_VERSION);
397
398         if (getuid() != 0) {
399                 dbg("need to be root, exit");
400                 exit(1);
401         }
402         
403         /* setup signal handler pipe */
404    retval = pipe(pipefds);
405    if (retval < 0) {
406       dbg("error getting pipes: %s", strerror(errno));
407       exit(1);
408    }
409         
410    retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
411    if (retval < 0) {
412       dbg("fcntl on read pipe: %s", strerror(errno));
413       exit(1);
414    }
415    
416    retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
417    if (retval < 0) {
418       dbg("fcntl on write pipe: %s", strerror(errno));
419       exit(1);
420    }
421
422         /* set signal handlers */
423         act.sa_handler = sig_handler;
424         sigemptyset(&act.sa_mask);
425         act.sa_flags = SA_RESTART;
426         sigaction(SIGINT, &act, NULL);
427         sigaction(SIGTERM, &act, NULL);
428         sigaction(SIGALRM, &act, NULL);
429         sigaction(SIGCHLD, &act, NULL);
430
431         memset(&saddr, 0x00, sizeof(saddr));
432         saddr.sun_family = AF_LOCAL;
433         /* use abstract namespace for socket path */
434         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
435         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
436
437         ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
438         if (ssock == -1) {
439                 dbg("error getting socket, exit");
440                 exit(1);
441         }
442
443         /* the bind takes care of ensuring only one copy running */
444         retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
445         if (retval < 0) {
446                 dbg("bind failed, exit");
447                 goto exit;
448         }
449
450         /* enable receiving of the sender credentials */
451         setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
452
453    FD_ZERO(&readfds);
454    FD_SET(ssock, &readfds);
455    FD_SET(pipefds[0], &readfds);
456         maxsockplus = ssock+1;
457         while (1) {
458                 fd_set workreadfds = readfds;
459                 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
460                 
461                 if (retval < 0) {
462                         dbg("error in select: %s", strerror(errno));
463                         continue;
464                 }
465                 
466                 if (FD_ISSET(ssock, &workreadfds))
467                         handle_msg(ssock);
468                 
469                 if (FD_ISSET(pipefds[0], &workreadfds))
470                         user_sighandler();
471                 
472                 if (children_waiting) {
473                         children_waiting = 0;
474                         reap_kids();
475                 }
476                 
477                 if (run_msg_q) {
478                         run_msg_q = 0;
479                         msg_queue_manager();
480                 }
481                 
482                 if (run_exec_q) {
483                         
484                         /* this is tricky.  exec_queue_manager() loops over exec_list, and
485                          * calls running_with_devpath(), which loops over running_list. This gives
486                          * O(N*M), which can get *nasty*.  Clean up running_list before
487                          * calling exec_queue_manager().
488                          */
489                         
490                         if (children_waiting) {
491                                 children_waiting = 0;
492                                 reap_kids();
493                         }
494
495                         run_exec_q = 0;
496                         exec_queue_manager();
497                 }
498         }
499 exit:
500         close(ssock);
501         exit(1);
502 }