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