chiark / gitweb /
[PATCH] udev_dbus can now compile properly, but linnking is another story...
[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
37 #include "list.h"
38 #include "udev.h"
39 #include "udev_lib.h"
40 #include "udev_version.h"
41 #include "udevd.h"
42 #include "logging.h"
43
44 static int expected_seqnum = 0;
45 volatile static int children_waiting;
46 volatile static int msg_q_timeout;
47
48 LIST_HEAD(msg_list);
49 LIST_HEAD(exec_list);
50 LIST_HEAD(running_list);
51
52 static void exec_queue_manager(void);
53 static void msg_queue_manager(void);
54
55 #ifdef LOG
56 unsigned char logname[LOGNAME_SIZE];
57 void log_message (int level, const char *format, ...)
58 {
59         va_list args;
60
61         va_start(args, format);
62         vsyslog(level, format, args);
63         va_end(args);
64 }
65 #endif
66
67 static void msg_dump_queue(void)
68 {
69         struct hotplug_msg *msg;
70
71         list_for_each_entry(msg, &msg_list, list)
72                 dbg("sequence %d in queue", msg->seqnum);
73 }
74
75 static void msg_dump(struct hotplug_msg *msg)
76 {
77         dbg("sequence %d, '%s', '%s', '%s'",
78             msg->seqnum, msg->action, msg->devpath, msg->subsystem);
79 }
80
81 static struct hotplug_msg *msg_create(void)
82 {
83         struct hotplug_msg *new_msg;
84
85         new_msg = malloc(sizeof(struct hotplug_msg));
86         if (new_msg == NULL)
87                 dbg("error malloc");
88         return new_msg;
89 }
90
91 static void run_queue_delete(struct hotplug_msg *msg)
92 {
93         list_del(&msg->list);
94         free(msg);
95         exec_queue_manager();
96 }
97
98 /* orders the message in the queue by sequence number */
99 static void msg_queue_insert(struct hotplug_msg *msg)
100 {
101         struct hotplug_msg *loop_msg;
102
103         /* sort message by sequence number into list*/
104         list_for_each_entry(loop_msg, &msg_list, list)
105                 if (loop_msg->seqnum > msg->seqnum)
106                         break;
107         list_add_tail(&msg->list, &loop_msg->list);
108         dbg("queued message seq %d", msg->seqnum);
109
110         /* store timestamp of queuing */
111         msg->queue_time = time(NULL);
112
113         /* run msg queue manager */
114         msg_queue_manager();
115
116         return ;
117 }
118
119 /* forks event and removes event from run queue when finished */
120 static void udev_run(struct hotplug_msg *msg)
121 {
122         pid_t pid;
123         char action[ACTION_SIZE];
124         char devpath[DEVPATH_SIZE];
125         char *env[] = { action, devpath, NULL };
126
127         snprintf(action, sizeof(action), "ACTION=%s", msg->action);
128         snprintf(devpath, sizeof(devpath), "DEVPATH=%s", msg->devpath);
129
130         pid = fork();
131         switch (pid) {
132         case 0:
133                 /* child */
134                 execle(UDEV_BIN, "udev", msg->subsystem, NULL, env);
135                 dbg("exec of child failed");
136                 exit(1);
137                 break;
138         case -1:
139                 dbg("fork of child failed");
140                 run_queue_delete(msg);
141                 break;
142         default:
143                 /* get SIGCHLD in main loop */
144                 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
145                 msg->pid = pid;
146         }
147 }
148
149 /* returns already running task with devpath */
150 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
151 {
152         struct hotplug_msg *loop_msg;
153         list_for_each_entry(loop_msg, &running_list, list)
154                 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
155                         return loop_msg;
156         return NULL;
157 }
158
159 /* exec queue management routine executes the events and delays events for the same devpath */
160 static void exec_queue_manager()
161 {
162         struct hotplug_msg *loop_msg;
163         struct hotplug_msg *tmp_msg;
164         struct hotplug_msg *msg;
165
166         list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
167                 msg = running_with_devpath(loop_msg);
168                 if (!msg) {
169                         /* move event to run list */
170                         list_move_tail(&loop_msg->list, &running_list);
171                         udev_run(loop_msg);
172                         dbg("moved seq %d to running list", loop_msg->seqnum);
173                 } else {
174                         dbg("delay seq %d, cause seq %d already working on '%s'",
175                                 loop_msg->seqnum, msg->seqnum, msg->devpath);
176                 }
177         }
178 }
179
180 static void msg_move_exec(struct hotplug_msg *msg)
181 {
182         list_move_tail(&msg->list, &exec_list);
183         exec_queue_manager();
184         expected_seqnum = msg->seqnum+1;
185         dbg("moved seq %d to exec, next expected is %d",
186                 msg->seqnum, expected_seqnum);
187 }
188
189 /* msg queue management routine handles the timeouts and dispatches the events */
190 static void msg_queue_manager()
191 {
192         struct hotplug_msg *loop_msg;
193         struct hotplug_msg *tmp_msg;
194         time_t msg_age = 0;
195
196         dbg("msg queue manager, next expected is %d", expected_seqnum);
197 recheck:
198         list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
199                 /* move event with expected sequence to the exec list */
200                 if (loop_msg->seqnum == expected_seqnum) {
201                         msg_move_exec(loop_msg);
202                         continue;
203                 }
204
205                 /* move event with expired timeout to the exec list */
206                 msg_age = time(NULL) - loop_msg->queue_time;
207                 if (msg_age > EVENT_TIMEOUT_SEC-1) {
208                         msg_move_exec(loop_msg);
209                         goto recheck;
210                 } else {
211                         break;
212                 }
213         }
214
215         msg_dump_queue();
216
217         if (list_empty(&msg_list) == 0) {
218                 /* set timeout for remaining queued events */
219                 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
220                 dbg("next event expires in %li seconds",
221                     EVENT_TIMEOUT_SEC - msg_age);
222                 setitimer(ITIMER_REAL, &itv, 0);
223         }
224 }
225
226 /* receive the msg, do some basic sanity checks, and queue it */
227 static void handle_msg(int sock)
228 {
229         struct hotplug_msg *msg;
230         int retval;
231         struct msghdr smsg;
232         struct cmsghdr *cmsg;
233         struct iovec iov;
234         struct ucred *cred;
235         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
236
237         msg = msg_create();
238         if (msg == NULL) {
239                 dbg("unable to store message");
240                 return;
241         }
242
243         iov.iov_base = msg;
244         iov.iov_len = sizeof(struct hotplug_msg);
245
246         memset(&smsg, 0x00, sizeof(struct msghdr));
247         smsg.msg_iov = &iov;
248         smsg.msg_iovlen = 1;
249         smsg.msg_control = cred_msg;
250         smsg.msg_controllen = sizeof(cred_msg);
251
252         retval = recvmsg(sock, &smsg, 0);
253         if (retval <  0) {
254                 if (errno != EINTR)
255                         dbg("unable to receive message");
256                 return;
257         }
258         cmsg = CMSG_FIRSTHDR(&smsg);
259         cred = (struct ucred *) CMSG_DATA(cmsg);
260
261         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
262                 dbg("no sender credentials received, message ignored");
263                 goto skip;
264         }
265
266         if (cred->uid != 0) {
267                 dbg("sender uid=%i, message ignored", cred->uid);
268                 goto skip;
269         }
270
271         if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
272                 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
273                 goto skip;
274         }
275
276         /* if no seqnum is given, we move straight to exec queue */
277         if (msg->seqnum == -1) {
278                 list_add(&msg->list, &exec_list);
279                 exec_queue_manager();
280         } else {
281                 msg_queue_insert(msg);
282         }
283         return;
284
285 skip:
286         free(msg);
287         return;
288 }
289
290 static void sig_handler(int signum)
291 {
292         switch (signum) {
293                 case SIGINT:
294                 case SIGTERM:
295                         exit(20 + signum);
296                         break;
297                 case SIGALRM:
298                         msg_q_timeout = 1;
299                         break;
300                 case SIGCHLD:
301                         children_waiting = 1;
302                         break;
303                 default:
304                         dbg("unhandled signal");
305         }
306 }
307
308 static void udev_done(int pid)
309 {
310         /* find msg associated with pid and delete it */
311         struct hotplug_msg *msg;
312
313         list_for_each_entry(msg, &running_list, list) {
314                 if (msg->pid == pid) {
315                         dbg("<== exec seq %d came back", msg->seqnum);
316                         run_queue_delete(msg);
317                         return;
318                 }
319         }
320 }
321
322 int main(int argc, char *argv[])
323 {
324         int ssock;
325         struct sockaddr_un saddr;
326         socklen_t addrlen;
327         int retval;
328         const int on = 1;
329         struct sigaction act;
330
331         init_logging("udevd");
332         dbg("version %s", UDEV_VERSION);
333
334         if (getuid() != 0) {
335                 dbg("need to be root, exit");
336                 exit(1);
337         }
338
339         /* set signal handler */
340         act.sa_handler = sig_handler;
341         sigemptyset (&act.sa_mask);
342         act.sa_flags = SA_RESTART;
343         sigaction(SIGINT, &act, NULL);
344         sigaction(SIGTERM, &act, NULL);
345
346         /* we want these two to interrupt system calls */
347         act.sa_flags = 0;
348         sigaction(SIGALRM, &act, NULL);
349         sigaction(SIGCHLD, &act, NULL);
350
351         memset(&saddr, 0x00, sizeof(saddr));
352         saddr.sun_family = AF_LOCAL;
353         /* use abstract namespace for socket path */
354         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
355         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
356
357         ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
358         if (ssock == -1) {
359                 dbg("error getting socket, exit");
360                 exit(1);
361         }
362
363         /* the bind takes care of ensuring only one copy running */
364         retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
365         if (retval < 0) {
366                 dbg("bind failed, exit");
367                 goto exit;
368         }
369
370         /* enable receiving of the sender credentials */
371         setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
372
373         while (1) {
374                 handle_msg(ssock);
375
376                 while(msg_q_timeout) {
377                         msg_q_timeout = 0;
378                         msg_queue_manager();
379                 }
380
381                 while(children_waiting) {
382                         children_waiting = 0;
383                         /* reap all dead children */
384                         while(1) {
385                                 int pid = waitpid(-1, 0, WNOHANG);
386                                 if ((pid == -1) || (pid == 0))
387                                         break;
388                                 udev_done(pid);
389                         }
390                 }
391         }
392 exit:
393         close(ssock);
394         exit(1);
395 }