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