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