]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevd.c
[PATCH] convert udevsend/udevd to DGRAM and single-threaded
[thirdparty/systemd.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 unsigned char logname[42];
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 static void msg_dump_queue(void)
56 {
57 struct hotplug_msg *msg;
58
59 list_for_each_entry(msg, &msg_list, list)
60 dbg("sequence %d in queue", msg->seqnum);
61 }
62
63 static void msg_dump(struct hotplug_msg *msg)
64 {
65 dbg("sequence %d, '%s', '%s', '%s'",
66 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
67 }
68
69 static struct hotplug_msg *msg_create(void)
70 {
71 struct hotplug_msg *new_msg;
72
73 new_msg = malloc(sizeof(struct hotplug_msg));
74 if (new_msg == NULL)
75 dbg("error malloc");
76 return new_msg;
77 }
78
79 static void run_queue_delete(struct hotplug_msg *msg)
80 {
81 list_del(&msg->list);
82 free(msg);
83 exec_queue_manager();
84 }
85
86 /* orders the message in the queue by sequence number */
87 static void msg_queue_insert(struct hotplug_msg *msg)
88 {
89 struct hotplug_msg *loop_msg;
90
91 /* sort message by sequence number into list*/
92 list_for_each_entry(loop_msg, &msg_list, list)
93 if (loop_msg->seqnum > msg->seqnum)
94 break;
95 list_add_tail(&msg->list, &loop_msg->list);
96 dbg("queued message seq %d", msg->seqnum);
97
98 /* store timestamp of queuing */
99 msg->queue_time = time(NULL);
100
101 /* run msg queue manager */
102 msg_queue_manager();
103
104 return ;
105 }
106
107 /* forks event and removes event from run queue when finished */
108 static void udev_run(struct hotplug_msg *msg)
109 {
110 pid_t pid;
111 setenv("ACTION", msg->action, 1);
112 setenv("DEVPATH", msg->devpath, 1);
113
114 pid = fork();
115 switch (pid) {
116 case 0:
117 /* child */
118 execl(UDEV_BIN, "udev", msg->subsystem, NULL);
119 dbg("exec of child failed");
120 exit(1);
121 break;
122 case -1:
123 dbg("fork of child failed");
124 run_queue_delete(msg);
125 break;
126 default:
127 /* get SIGCHLD in main loop */
128 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
129 msg->pid = pid;
130 }
131 }
132
133 /* returns already running task with devpath */
134 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
135 {
136 struct hotplug_msg *loop_msg;
137 list_for_each_entry(loop_msg, &running_list, list)
138 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
139 return loop_msg;
140 return NULL;
141 }
142
143 /* exec queue management routine executes the events and delays events for the same devpath */
144 static void exec_queue_manager()
145 {
146 struct hotplug_msg *loop_msg;
147 struct hotplug_msg *tmp_msg;
148 struct hotplug_msg *msg;
149
150 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
151 msg = running_with_devpath(loop_msg);
152 if (!msg) {
153 /* move event to run list */
154 list_move_tail(&loop_msg->list, &running_list);
155 udev_run(loop_msg);
156 dbg("moved seq %d to running list", loop_msg->seqnum);
157 } else {
158 dbg("delay seq %d, cause seq %d already working on '%s'",
159 loop_msg->seqnum, msg->seqnum, msg->devpath);
160 }
161 }
162 }
163
164 static void msg_move_exec(struct hotplug_msg *msg)
165 {
166 list_move_tail(&msg->list, &exec_list);
167 exec_queue_manager();
168 expected_seqnum = msg->seqnum+1;
169 dbg("moved seq %d to exec, next expected is %d",
170 msg->seqnum, expected_seqnum);
171 }
172
173 /* msg queue management routine handles the timeouts and dispatches the events */
174 static void msg_queue_manager()
175 {
176 struct hotplug_msg *loop_msg;
177 struct hotplug_msg *tmp_msg;
178 time_t msg_age = 0;
179
180 dbg("msg queue manager, next expected is %d", expected_seqnum);
181 recheck:
182 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
183 /* move event with expected sequence to the exec list */
184 if (loop_msg->seqnum == expected_seqnum) {
185 msg_move_exec(loop_msg);
186 continue;
187 }
188
189 /* move event with expired timeout to the exec list */
190 msg_age = time(NULL) - loop_msg->queue_time;
191 if (msg_age > EVENT_TIMEOUT_SEC-1) {
192 msg_move_exec(loop_msg);
193 goto recheck;
194 } else {
195 break;
196 }
197 }
198
199 msg_dump_queue();
200
201 if (list_empty(&msg_list) == 0) {
202 /* set timeout for remaining queued events */
203 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
204 dbg("next event expires in %li seconds",
205 EVENT_TIMEOUT_SEC - msg_age);
206 setitimer(ITIMER_REAL, &itv, 0);
207 }
208 }
209
210 /* receive the msg, do some basic sanity checks, and queue it */
211 static void handle_msg(int sock)
212 {
213 struct hotplug_msg *msg;
214 int retval;
215
216 msg = msg_create();
217 if (msg == NULL) {
218 dbg("unable to store message");
219 return;
220 }
221
222 retval = recv(sock, msg, sizeof(struct hotplug_msg), 0);
223 if (retval < 0) {
224 if (errno != EINTR)
225 dbg("unable to receive message");
226 return;
227 }
228
229 if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
230 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
231 free(msg);
232 return;
233 }
234
235 /* if no seqnum is given, we move straight to exec queue */
236 if (msg->seqnum == -1) {
237 list_add(&msg->list, &exec_list);
238 exec_queue_manager();
239 } else {
240 msg_queue_insert(msg);
241 }
242 }
243
244 static void sig_handler(int signum)
245 {
246 switch (signum) {
247 case SIGINT:
248 case SIGTERM:
249 exit(20 + signum);
250 break;
251 case SIGALRM:
252 msg_q_timeout = 1;
253 break;
254 case SIGCHLD:
255 children_waiting = 1;
256 break;
257 default:
258 dbg("unhandled signal");
259 }
260 }
261
262 static void udev_done(int pid)
263 {
264 /* find msg associated with pid and delete it */
265 struct hotplug_msg *msg;
266
267 list_for_each_entry(msg, &running_list, list) {
268 if (msg->pid == pid) {
269 dbg("<== exec seq %d came back", msg->seqnum);
270 run_queue_delete(msg);
271 return;
272 }
273 }
274 }
275
276 int main(int argc, char *argv[])
277 {
278 int ssock;
279 struct sockaddr_un saddr;
280 socklen_t addrlen;
281 int retval;
282
283 init_logging("udevd");
284
285 signal(SIGINT, sig_handler);
286 signal(SIGTERM, sig_handler);
287 signal(SIGALRM, sig_handler);
288 signal(SIGCHLD, sig_handler);
289
290 /* we want these two to interrupt system calls */
291 siginterrupt(SIGALRM, 1);
292 siginterrupt(SIGCHLD, 1);
293
294 memset(&saddr, 0x00, sizeof(saddr));
295 saddr.sun_family = AF_LOCAL;
296 /* use abstract namespace for socket path */
297 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
298 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
299
300 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
301 if (ssock == -1) {
302 dbg("error getting socket");
303 exit(1);
304 }
305
306 /* the bind takes care of ensuring only one copy running */
307 retval = bind(ssock, &saddr, addrlen);
308 if (retval < 0) {
309 dbg("bind failed\n");
310 goto exit;
311 }
312
313 while (1) {
314 handle_msg(ssock);
315
316 while(msg_q_timeout) {
317 msg_q_timeout = 0;
318 msg_queue_manager();
319 }
320
321 while(children_waiting) {
322 children_waiting = 0;
323 /* reap all dead children */
324 while(1) {
325 int pid = waitpid(-1, 0, WNOHANG);
326 if ((pid == -1) || (pid == 0))
327 break;
328 udev_done(pid);
329 }
330 }
331 }
332 exit:
333 close(ssock);
334 exit(1);
335 }