]>
Commit | Line | Data |
---|---|---|
7fafc032 | 1 | /* |
53921bfa | 2 | * udevd.c - hotplug event serializer |
7fafc032 | 3 | * |
6c18b1fb | 4 | * Copyright (C) 2004-2005 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> |
7fafc032 KS |
24 | #include <signal.h> |
25 | #include <unistd.h> | |
26 | #include <errno.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
085cce37 KS |
30 | #include <ctype.h> |
31 | #include <dirent.h> | |
32 | #include <fcntl.h> | |
138068d6 KS |
33 | #include <sys/select.h> |
34 | #include <sys/wait.h> | |
e5a2989e | 35 | #include <sys/time.h> |
53921bfa KS |
36 | #include <sys/types.h> |
37 | #include <sys/socket.h> | |
38 | #include <sys/un.h> | |
e5a2989e | 39 | #include <sys/sysinfo.h> |
dc117daa | 40 | #include <sys/stat.h> |
7fafc032 | 41 | |
a695feae | 42 | #include "list.h" |
63f61c5c | 43 | #include "udev_libc_wrapper.h" |
7fafc032 | 44 | #include "udev.h" |
35b7d88c | 45 | #include "udev_version.h" |
9af5bb2f | 46 | #include "udev_utils.h" |
7fafc032 KS |
47 | #include "udevd.h" |
48 | #include "logging.h" | |
49 | ||
13f24d59 KS |
50 | /* global variables*/ |
51 | static int udevsendsock; | |
085cce37 | 52 | static pid_t sid; |
13f24d59 | 53 | |
f27125f9 | 54 | static int pipefds[2]; |
8b726878 | 55 | static long startup_time; |
cdc60e8a | 56 | static unsigned long long expected_seqnum = 0; |
5cab7caa | 57 | static volatile int sigchilds_waiting; |
c8fa2d8b HH |
58 | static volatile int run_msg_q; |
59 | static volatile int sig_flag; | |
f27125f9 | 60 | static int run_exec_q; |
7fafc032 | 61 | |
4a539daf KS |
62 | static LIST_HEAD(msg_list); |
63 | static LIST_HEAD(exec_list); | |
64 | static LIST_HEAD(running_list); | |
7fafc032 | 65 | |
2f6cbd19 KS |
66 | static void exec_queue_manager(void); |
67 | static void msg_queue_manager(void); | |
f27125f9 | 68 | static void user_sighandler(void); |
5cab7caa | 69 | static void reap_sigchilds(void); |
7d855c31 | 70 | char *udev_bin; |
7fafc032 | 71 | |
6c18b1fb | 72 | #ifdef USE_LOG |
6b493a20 | 73 | void log_message (int priority, const char *format, ...) |
51a8bb2f | 74 | { |
6b493a20 KS |
75 | va_list args; |
76 | ||
77 | if (priority > udev_log_priority) | |
78 | return; | |
d026a35d GKH |
79 | |
80 | va_start(args, format); | |
6b493a20 | 81 | vsyslog(priority, format, args); |
d026a35d | 82 | va_end(args); |
51a8bb2f | 83 | } |
d026a35d | 84 | #endif |
51a8bb2f | 85 | |
53921bfa | 86 | static void msg_dump_queue(void) |
7fafc032 | 87 | { |
f27125f9 | 88 | #ifdef DEBUG |
53921bfa | 89 | struct hotplug_msg *msg; |
7fafc032 | 90 | |
f8a178a3 | 91 | list_for_each_entry(msg, &msg_list, node) |
cdc60e8a | 92 | dbg("sequence %llu in queue", msg->seqnum); |
f27125f9 | 93 | #endif |
35b7d88c KS |
94 | } |
95 | ||
2f6cbd19 | 96 | static void run_queue_delete(struct hotplug_msg *msg) |
8e2229c4 | 97 | { |
f8a178a3 | 98 | list_del(&msg->node); |
2f6cbd19 | 99 | free(msg); |
8e2229c4 KS |
100 | } |
101 | ||
53921bfa KS |
102 | /* orders the message in the queue by sequence number */ |
103 | static void msg_queue_insert(struct hotplug_msg *msg) | |
35b7d88c | 104 | { |
53921bfa | 105 | struct hotplug_msg *loop_msg; |
e5a2989e | 106 | struct sysinfo info; |
35b7d88c | 107 | |
021a294c KS |
108 | if (msg->seqnum == 0) { |
109 | dbg("no SEQNUM, move straight to the exec queue"); | |
f8a178a3 | 110 | list_add(&msg->node, &exec_list); |
021a294c KS |
111 | run_exec_q = 1; |
112 | return; | |
113 | } | |
114 | ||
7f7ae03a KS |
115 | /* don't delay messages with timeout set */ |
116 | if (msg->timeout) { | |
117 | dbg("move seq %llu with timeout %u to exec queue", msg->seqnum, msg->timeout); | |
118 | list_add(&msg->node, &exec_list); | |
119 | run_exec_q = 1; | |
120 | return; | |
121 | } | |
122 | ||
021a294c | 123 | /* sort message by sequence number into list */ |
f8a178a3 | 124 | list_for_each_entry_reverse(loop_msg, &msg_list, node) { |
f27125f9 | 125 | if (loop_msg->seqnum < msg->seqnum) |
53921bfa | 126 | break; |
35b7d88c | 127 | |
c2cf4012 | 128 | if (loop_msg->seqnum == msg->seqnum) { |
6b493a20 | 129 | info("ignoring duplicate message seq %llu", msg->seqnum); |
c2cf4012 KS |
130 | return; |
131 | } | |
132 | } | |
133 | ||
53921bfa | 134 | /* store timestamp of queuing */ |
e5a2989e KS |
135 | sysinfo(&info); |
136 | msg->queue_time = info.uptime; | |
137 | ||
f8a178a3 | 138 | list_add(&msg->node, &loop_msg->node); |
cdc60e8a | 139 | dbg("queued message seq %llu", msg->seqnum); |
35b7d88c | 140 | |
2f6cbd19 | 141 | /* run msg queue manager */ |
f27125f9 | 142 | run_msg_q = 1; |
7fafc032 | 143 | |
c2cf4012 | 144 | return; |
7fafc032 KS |
145 | } |
146 | ||
53921bfa | 147 | /* forks event and removes event from run queue when finished */ |
821d0ec8 | 148 | static void execute_udev(struct hotplug_msg *msg) |
7fafc032 | 149 | { |
4497fcbf | 150 | char *const argv[] = { "udev", msg->subsystem, NULL }; |
90c210eb | 151 | pid_t pid; |
90c210eb KS |
152 | |
153 | pid = fork(); | |
154 | switch (pid) { | |
155 | case 0: | |
33db4b8d | 156 | /* child */ |
13f24d59 | 157 | close(udevsendsock); |
f602ccf0 | 158 | logging_close(); |
085cce37 KS |
159 | |
160 | setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY); | |
4497fcbf | 161 | execve(udev_bin, argv, msg->envp); |
6b493a20 | 162 | err("exec of child failed"); |
7257cb18 | 163 | _exit(1); |
90c210eb KS |
164 | break; |
165 | case -1: | |
6b493a20 | 166 | err("fork of child failed"); |
2f6cbd19 KS |
167 | run_queue_delete(msg); |
168 | break; | |
90c210eb | 169 | default: |
2f6cbd19 | 170 | /* get SIGCHLD in main loop */ |
cdc60e8a | 171 | dbg("==> exec seq %llu [%d] working at '%s'", msg->seqnum, pid, msg->devpath); |
2f6cbd19 | 172 | msg->pid = pid; |
90c210eb | 173 | } |
7fafc032 KS |
174 | } |
175 | ||
085cce37 KS |
176 | static int running_processes(void) |
177 | { | |
178 | int f; | |
179 | static char buf[4096]; | |
180 | int len; | |
181 | int running; | |
182 | const char *pos; | |
183 | ||
184 | f = open("/proc/stat", O_RDONLY); | |
185 | if (f == -1) | |
186 | return -1; | |
187 | ||
188 | len = read(f, buf, sizeof(buf)); | |
189 | close(f); | |
190 | ||
191 | if (len <= 0) | |
192 | return -1; | |
193 | else | |
194 | buf[len] = '\0'; | |
195 | ||
196 | pos = strstr(buf, "procs_running "); | |
197 | if (pos == NULL) | |
198 | return -1; | |
199 | ||
200 | if (sscanf(pos, "procs_running %u", &running) != 1) | |
201 | return -1; | |
202 | ||
203 | return running; | |
204 | } | |
205 | ||
206 | /* return the number of process es in our session, count only until limit */ | |
207 | static int running_processes_in_session(pid_t session, int limit) | |
208 | { | |
209 | DIR *dir; | |
210 | struct dirent *dent; | |
211 | int running = 0; | |
212 | ||
213 | dir = opendir("/proc"); | |
214 | if (!dir) | |
215 | return -1; | |
216 | ||
217 | /* read process info from /proc */ | |
218 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { | |
219 | int f; | |
220 | char procdir[64]; | |
221 | char line[256]; | |
222 | const char *pos; | |
223 | char state; | |
224 | pid_t ppid, pgrp, sess; | |
225 | int len; | |
226 | ||
227 | if (!isdigit(dent->d_name[0])) | |
228 | continue; | |
229 | ||
230 | snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name); | |
231 | procdir[sizeof(procdir)-1] = '\0'; | |
232 | ||
233 | f = open(procdir, O_RDONLY); | |
234 | if (f == -1) | |
235 | continue; | |
236 | ||
237 | len = read(f, line, sizeof(line)); | |
238 | close(f); | |
239 | ||
240 | if (len <= 0) | |
241 | continue; | |
242 | else | |
243 | line[len] = '\0'; | |
244 | ||
245 | /* skip ugly program name */ | |
246 | pos = strrchr(line, ')') + 2; | |
247 | if (pos == NULL) | |
248 | continue; | |
249 | ||
250 | if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4) | |
251 | continue; | |
252 | ||
253 | /* count only processes in our session */ | |
254 | if (sess != session) | |
255 | continue; | |
256 | ||
257 | /* count only running, no sleeping processes */ | |
258 | if (state != 'R') | |
259 | continue; | |
260 | ||
261 | running++; | |
262 | if (limit > 0 && running >= limit) | |
263 | break; | |
264 | } | |
265 | closedir(dir); | |
266 | ||
267 | return running; | |
268 | } | |
269 | ||
7b6571a9 KS |
270 | static int compare_devpath(const char *running, const char *waiting) |
271 | { | |
272 | int i; | |
273 | ||
63f61c5c | 274 | for (i = 0; i < PATH_SIZE; i++) { |
7b6571a9 KS |
275 | /* identical device event found */ |
276 | if (running[i] == '\0' && waiting[i] == '\0') | |
277 | return 1; | |
278 | ||
279 | /* parent device event found */ | |
280 | if (running[i] == '\0' && waiting[i] == '/') | |
281 | return 2; | |
282 | ||
283 | /* child device event found */ | |
284 | if (running[i] == '/' && waiting[i] == '\0') | |
285 | return 3; | |
286 | ||
287 | /* no matching event */ | |
288 | if (running[i] != waiting[i]) | |
289 | break; | |
290 | } | |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
295 | /* returns still running task for the same device, its parent or its physical device */ | |
53921bfa | 296 | static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg) |
7fafc032 | 297 | { |
53921bfa | 298 | struct hotplug_msg *loop_msg; |
7b6571a9 KS |
299 | |
300 | if (msg->devpath == NULL) | |
301 | return NULL; | |
79721e0a | 302 | |
7f7ae03a KS |
303 | /* skip any events with a timeout set */ |
304 | if (msg->timeout) | |
305 | return NULL; | |
306 | ||
f8a178a3 | 307 | list_for_each_entry(loop_msg, &running_list, node) { |
7b6571a9 | 308 | if (loop_msg->devpath == NULL) |
80513ea3 KS |
309 | continue; |
310 | ||
7b6571a9 KS |
311 | /* return running parent/child device event */ |
312 | if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) | |
313 | return loop_msg; | |
79721e0a | 314 | |
7b6571a9 | 315 | /* return running physical device event */ |
79721e0a | 316 | if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0) |
7b6571a9 KS |
317 | if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) |
318 | return loop_msg; | |
80513ea3 KS |
319 | } |
320 | ||
53921bfa | 321 | return NULL; |
7fafc032 KS |
322 | } |
323 | ||
79721e0a | 324 | /* exec queue management routine executes the events and serializes events in the same sequence */ |
1ceba936 | 325 | static void exec_queue_manager(void) |
7fafc032 | 326 | { |
53921bfa KS |
327 | struct hotplug_msg *loop_msg; |
328 | struct hotplug_msg *tmp_msg; | |
a695feae | 329 | struct hotplug_msg *msg; |
085cce37 KS |
330 | int running; |
331 | ||
332 | running = running_processes(); | |
333 | dbg("%d processes runnning on system", running); | |
334 | if (running < 0) | |
335 | running = THROTTLE_MAX_RUNNING_CHILDS; | |
53921bfa | 336 | |
f8a178a3 | 337 | list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) { |
085cce37 KS |
338 | /* check running processes in our session and possibly throttle */ |
339 | if (running >= THROTTLE_MAX_RUNNING_CHILDS) { | |
340 | running = running_processes_in_session(sid, THROTTLE_MAX_RUNNING_CHILDS+10); | |
341 | dbg("%d processes running in session", running); | |
342 | if (running >= THROTTLE_MAX_RUNNING_CHILDS) { | |
343 | dbg("delay seq %llu, cause too many processes already running", loop_msg->seqnum); | |
344 | return; | |
345 | } | |
346 | } | |
347 | ||
2f6cbd19 KS |
348 | msg = running_with_devpath(loop_msg); |
349 | if (!msg) { | |
350 | /* move event to run list */ | |
f8a178a3 | 351 | list_move_tail(&loop_msg->node, &running_list); |
821d0ec8 | 352 | execute_udev(loop_msg); |
085cce37 | 353 | running++; |
cdc60e8a | 354 | dbg("moved seq %llu to running list", loop_msg->seqnum); |
2f6cbd19 | 355 | } else { |
79721e0a KS |
356 | dbg("delay seq %llu (%s), cause seq %llu (%s) is still running", |
357 | loop_msg->seqnum, loop_msg->devpath, msg->seqnum, msg->devpath); | |
53921bfa | 358 | } |
53921bfa KS |
359 | } |
360 | } | |
361 | ||
2f6cbd19 | 362 | static void msg_move_exec(struct hotplug_msg *msg) |
86590cd5 | 363 | { |
f8a178a3 | 364 | list_move_tail(&msg->node, &exec_list); |
f27125f9 | 365 | run_exec_q = 1; |
2f6cbd19 | 366 | expected_seqnum = msg->seqnum+1; |
cdc60e8a | 367 | dbg("moved seq %llu to exec, next expected is %llu", |
2f6cbd19 | 368 | msg->seqnum, expected_seqnum); |
86590cd5 KS |
369 | } |
370 | ||
2f6cbd19 | 371 | /* msg queue management routine handles the timeouts and dispatches the events */ |
1ceba936 | 372 | static void msg_queue_manager(void) |
53921bfa KS |
373 | { |
374 | struct hotplug_msg *loop_msg; | |
a695feae | 375 | struct hotplug_msg *tmp_msg; |
e5a2989e KS |
376 | struct sysinfo info; |
377 | long msg_age = 0; | |
8b726878 KS |
378 | static int timeout = EVENT_INIT_TIMEOUT_SEC; |
379 | static int init = 1; | |
a695feae | 380 | |
cdc60e8a | 381 | dbg("msg queue manager, next expected is %llu", expected_seqnum); |
a695feae | 382 | recheck: |
f8a178a3 | 383 | list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, node) { |
2f6cbd19 KS |
384 | /* move event with expected sequence to the exec list */ |
385 | if (loop_msg->seqnum == expected_seqnum) { | |
386 | msg_move_exec(loop_msg); | |
387 | continue; | |
a695feae | 388 | } |
35b7d88c | 389 | |
8b726878 KS |
390 | /* see if we are in the initialization phase and wait for the very first events */ |
391 | if (init && (info.uptime - startup_time >= INIT_TIME_SEC)) { | |
392 | init = 0; | |
393 | timeout = EVENT_TIMEOUT_SEC; | |
394 | dbg("initialization phase passed, set timeout to %i seconds", EVENT_TIMEOUT_SEC); | |
395 | } | |
396 | ||
2f6cbd19 | 397 | /* move event with expired timeout to the exec list */ |
e5a2989e KS |
398 | sysinfo(&info); |
399 | msg_age = info.uptime - loop_msg->queue_time; | |
cdc60e8a | 400 | dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age); |
8b726878 | 401 | if (msg_age >= timeout) { |
2f6cbd19 KS |
402 | msg_move_exec(loop_msg); |
403 | goto recheck; | |
53921bfa | 404 | } else { |
2f6cbd19 | 405 | break; |
53921bfa | 406 | } |
2f6cbd19 KS |
407 | } |
408 | ||
409 | msg_dump_queue(); | |
410 | ||
e5a2989e | 411 | /* set timeout for remaining queued events */ |
2f6cbd19 | 412 | if (list_empty(&msg_list) == 0) { |
8b726878 KS |
413 | struct itimerval itv = {{0, 0}, {timeout - msg_age, 0}}; |
414 | dbg("next event expires in %li seconds", timeout - msg_age); | |
1ceba936 | 415 | setitimer(ITIMER_REAL, &itv, NULL); |
7fafc032 | 416 | } |
7fafc032 KS |
417 | } |
418 | ||
021a294c KS |
419 | /* receive the udevsend message and do some sanity checks */ |
420 | static struct hotplug_msg *get_udevsend_msg(void) | |
7fafc032 | 421 | { |
4a231017 | 422 | static struct udevsend_msg usend_msg; |
53921bfa | 423 | struct hotplug_msg *msg; |
4a231017 KS |
424 | int bufpos; |
425 | int i; | |
426 | ssize_t size; | |
0028653c KS |
427 | struct msghdr smsg; |
428 | struct cmsghdr *cmsg; | |
429 | struct iovec iov; | |
430 | struct ucred *cred; | |
431 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; | |
4a231017 | 432 | int envbuf_size; |
a695feae | 433 | |
4a231017 KS |
434 | memset(&usend_msg, 0x00, sizeof(struct udevsend_msg)); |
435 | iov.iov_base = &usend_msg; | |
436 | iov.iov_len = sizeof(struct udevsend_msg); | |
0028653c KS |
437 | |
438 | memset(&smsg, 0x00, sizeof(struct msghdr)); | |
439 | smsg.msg_iov = &iov; | |
440 | smsg.msg_iovlen = 1; | |
441 | smsg.msg_control = cred_msg; | |
442 | smsg.msg_controllen = sizeof(cred_msg); | |
443 | ||
021a294c | 444 | size = recvmsg(udevsendsock, &smsg, 0); |
4a231017 | 445 | if (size < 0) { |
2f6cbd19 | 446 | if (errno != EINTR) |
021a294c KS |
447 | dbg("unable to receive udevsend message"); |
448 | return NULL; | |
53921bfa | 449 | } |
0028653c KS |
450 | cmsg = CMSG_FIRSTHDR(&smsg); |
451 | cred = (struct ucred *) CMSG_DATA(cmsg); | |
452 | ||
7b1cbec9 | 453 | if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { |
6b493a20 | 454 | info("no sender credentials received, message ignored"); |
021a294c | 455 | return NULL; |
7b1cbec9 KS |
456 | } |
457 | ||
0028653c | 458 | if (cred->uid != 0) { |
6b493a20 | 459 | info("sender uid=%i, message ignored", cred->uid); |
021a294c | 460 | return NULL; |
4a231017 KS |
461 | } |
462 | ||
463 | if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) { | |
6b493a20 | 464 | info("message magic '%s' doesn't match, ignore it", usend_msg.magic); |
021a294c | 465 | return NULL; |
0028653c KS |
466 | } |
467 | ||
4a231017 KS |
468 | envbuf_size = size - offsetof(struct udevsend_msg, envbuf); |
469 | dbg("envbuf_size=%i", envbuf_size); | |
470 | msg = malloc(sizeof(struct hotplug_msg) + envbuf_size); | |
021a294c KS |
471 | if (msg == NULL) |
472 | return NULL; | |
473 | ||
4a231017 KS |
474 | memset(msg, 0x00, sizeof(struct hotplug_msg) + envbuf_size); |
475 | ||
476 | /* copy environment buffer and reconstruct envp */ | |
477 | memcpy(msg->envbuf, usend_msg.envbuf, envbuf_size); | |
478 | bufpos = 0; | |
6f59ed55 | 479 | for (i = 0; (bufpos < envbuf_size) && (i < HOTPLUG_NUM_ENVP-2); i++) { |
4a231017 KS |
480 | int keylen; |
481 | char *key; | |
482 | ||
483 | key = &msg->envbuf[bufpos]; | |
484 | keylen = strlen(key); | |
485 | msg->envp[i] = key; | |
486 | bufpos += keylen + 1; | |
487 | dbg("add '%s' to msg.envp[%i]", msg->envp[i], i); | |
488 | ||
489 | /* remember some keys for further processing */ | |
490 | if (strncmp(key, "ACTION=", 7) == 0) | |
491 | msg->action = &key[7]; | |
492 | ||
493 | if (strncmp(key, "DEVPATH=", 8) == 0) | |
494 | msg->devpath = &key[8]; | |
495 | ||
496 | if (strncmp(key, "SUBSYSTEM=", 10) == 0) | |
497 | msg->subsystem = &key[10]; | |
498 | ||
499 | if (strncmp(key, "SEQNUM=", 7) == 0) | |
500 | msg->seqnum = strtoull(&key[7], NULL, 10); | |
79721e0a KS |
501 | |
502 | if (strncmp(key, "PHYSDEVPATH=", 12) == 0) | |
503 | msg->physdevpath = &key[12]; | |
7f7ae03a KS |
504 | |
505 | if (strncmp(key, "TIMEOUT=", 8) == 0) | |
506 | msg->timeout = strtoull(&key[8], NULL, 10); | |
53921bfa | 507 | } |
eabfc973 | 508 | msg->envp[i++] = "UDEVD_EVENT=1"; |
4a231017 | 509 | msg->envp[i] = NULL; |
a695feae | 510 | |
021a294c | 511 | return msg; |
a695feae | 512 | } |
1c5c245e | 513 | |
e5a5b54a | 514 | static void asmlinkage sig_handler(int signum) |
7fafc032 | 515 | { |
f27125f9 | 516 | int rc; |
47bf9196 | 517 | |
53921bfa KS |
518 | switch (signum) { |
519 | case SIGINT: | |
520 | case SIGTERM: | |
53921bfa KS |
521 | exit(20 + signum); |
522 | break; | |
2f6cbd19 | 523 | case SIGALRM: |
f27125f9 | 524 | /* set flag, then write to pipe if needed */ |
525 | run_msg_q = 1; | |
526 | goto do_write; | |
2f6cbd19 KS |
527 | break; |
528 | case SIGCHLD: | |
f27125f9 | 529 | /* set flag, then write to pipe if needed */ |
5cab7caa | 530 | sigchilds_waiting = 1; |
f27125f9 | 531 | goto do_write; |
2f6cbd19 | 532 | break; |
f27125f9 | 533 | } |
5a73b25f | 534 | |
f27125f9 | 535 | do_write: |
536 | /* if pipe is empty, write to pipe to force select to return | |
537 | * immediately when it gets called | |
538 | */ | |
539 | if (!sig_flag) { | |
540 | rc = write(pipefds[1],&signum,sizeof(signum)); | |
5a73b25f | 541 | if (rc >= 0) |
f27125f9 | 542 | sig_flag = 1; |
7fafc032 | 543 | } |
33db4b8d | 544 | } |
7fafc032 | 545 | |
2f6cbd19 KS |
546 | static void udev_done(int pid) |
547 | { | |
548 | /* find msg associated with pid and delete it */ | |
549 | struct hotplug_msg *msg; | |
550 | ||
f8a178a3 | 551 | list_for_each_entry(msg, &running_list, node) { |
2f6cbd19 | 552 | if (msg->pid == pid) { |
cdc60e8a | 553 | dbg("<== exec seq %llu came back", msg->seqnum); |
2f6cbd19 | 554 | run_queue_delete(msg); |
3169e8d1 | 555 | |
f27125f9 | 556 | /* we want to run the exec queue manager since there may |
557 | * be events waiting with the devpath of the one that | |
558 | * just finished | |
559 | */ | |
560 | run_exec_q = 1; | |
2f6cbd19 KS |
561 | return; |
562 | } | |
563 | } | |
564 | } | |
565 | ||
5cab7caa | 566 | static void reap_sigchilds(void) |
f27125f9 | 567 | { |
f27125f9 | 568 | while(1) { |
1ceba936 | 569 | int pid = waitpid(-1, NULL, WNOHANG); |
f27125f9 | 570 | if ((pid == -1) || (pid == 0)) |
571 | break; | |
572 | udev_done(pid); | |
573 | } | |
574 | } | |
575 | ||
576 | /* just read everything from the pipe and clear the flag, | |
5cab7caa | 577 | * the flags was set in the signal handler |
f27125f9 | 578 | */ |
1ceba936 | 579 | static void user_sighandler(void) |
f27125f9 | 580 | { |
581 | int sig; | |
ce043f85 | 582 | |
f27125f9 | 583 | while(1) { |
5cab7caa | 584 | int rc = read(pipefds[0], &sig, sizeof(sig)); |
f27125f9 | 585 | if (rc < 0) |
586 | break; | |
587 | ||
588 | sig_flag = 0; | |
589 | } | |
590 | } | |
591 | ||
c2cf4012 | 592 | static int init_udevsend_socket(void) |
33db4b8d | 593 | { |
53921bfa | 594 | struct sockaddr_un saddr; |
1dadabd7 | 595 | socklen_t addrlen; |
5cab7caa | 596 | const int feature_on = 1; |
c2cf4012 KS |
597 | int retval; |
598 | ||
599 | memset(&saddr, 0x00, sizeof(saddr)); | |
600 | saddr.sun_family = AF_LOCAL; | |
601 | /* use abstract namespace for socket path */ | |
602 | strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH); | |
603 | addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1; | |
604 | ||
605 | udevsendsock = socket(AF_LOCAL, SOCK_DGRAM, 0); | |
606 | if (udevsendsock == -1) { | |
6b493a20 | 607 | err("error getting socket, %s", strerror(errno)); |
c2cf4012 KS |
608 | return -1; |
609 | } | |
610 | ||
611 | /* the bind takes care of ensuring only one copy running */ | |
612 | retval = bind(udevsendsock, (struct sockaddr *) &saddr, addrlen); | |
613 | if (retval < 0) { | |
6b493a20 | 614 | err("bind failed, %s", strerror(errno)); |
c2cf4012 KS |
615 | close(udevsendsock); |
616 | return -1; | |
617 | } | |
618 | ||
619 | /* enable receiving of the sender credentials */ | |
620 | setsockopt(udevsendsock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on)); | |
621 | ||
622 | return 0; | |
623 | } | |
624 | ||
625 | int main(int argc, char *argv[], char *envp[]) | |
626 | { | |
627 | struct sysinfo info; | |
628 | int maxsockplus; | |
629 | int retval; | |
630 | int fd; | |
f8911dbb | 631 | struct sigaction act; |
f27125f9 | 632 | fd_set readfds; |
1e266989 | 633 | const char *udevd_expected_seqnum; |
53921bfa | 634 | |
7257cb18 | 635 | logging_init("udevd"); |
6b493a20 | 636 | udev_init_config(); |
896e5aa9 | 637 | dbg("version %s", UDEV_VERSION); |
95a6f4c8 | 638 | |
7b1cbec9 | 639 | if (getuid() != 0) { |
6b493a20 | 640 | err("need to be root, exit"); |
ec9cc012 | 641 | goto exit; |
7b1cbec9 | 642 | } |
5cab7caa | 643 | |
f15515b5 KS |
644 | /* daemonize on request */ |
645 | if (argc == 2 && strcmp(argv[1], "-d") == 0) { | |
646 | pid_t pid; | |
647 | ||
648 | pid = fork(); | |
649 | switch (pid) { | |
650 | case 0: | |
651 | dbg("damonized fork running"); | |
652 | break; | |
653 | case -1: | |
6b493a20 | 654 | err("fork of daemon failed"); |
f15515b5 KS |
655 | goto exit; |
656 | default: | |
657 | logging_close(); | |
658 | exit(0); | |
659 | } | |
660 | } | |
661 | ||
085cce37 KS |
662 | /* become session leader */ |
663 | sid = setsid(); | |
664 | dbg("our session is %d", sid); | |
665 | ||
5cab7caa | 666 | /* make sure we don't lock any path */ |
c8fa2d8b | 667 | chdir("/"); |
5cab7caa KS |
668 | umask(umask(077) | 022); |
669 | ||
085cce37 KS |
670 | /*set a reasonable scheduling priority for the daemon */ |
671 | setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY); | |
672 | ||
c8fa2d8b HH |
673 | /* Set fds to dev/null */ |
674 | fd = open( "/dev/null", O_RDWR ); | |
ce043f85 KS |
675 | if (fd >= 0) { |
676 | dup2(fd, 0); | |
677 | dup2(fd, 1); | |
678 | dup2(fd, 2); | |
679 | if (fd > 2) | |
680 | close(fd); | |
681 | } else | |
6b493a20 | 682 | err("error opening /dev/null %s", strerror(errno)); |
5cab7caa | 683 | |
f27125f9 | 684 | /* setup signal handler pipe */ |
e5a2989e KS |
685 | retval = pipe(pipefds); |
686 | if (retval < 0) { | |
6b493a20 | 687 | err("error getting pipes: %s", strerror(errno)); |
ec9cc012 | 688 | goto exit; |
e5a2989e KS |
689 | } |
690 | ||
691 | retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK); | |
c8fa2d8b | 692 | if (retval < 0) { |
6b493a20 | 693 | err("error fcntl on read pipe: %s", strerror(errno)); |
ec9cc012 | 694 | goto exit; |
c8fa2d8b HH |
695 | } |
696 | retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC); | |
ce043f85 | 697 | if (retval < 0) |
6b493a20 | 698 | err("error fcntl on read pipe: %s", strerror(errno)); |
e5a2989e KS |
699 | |
700 | retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK); | |
701 | if (retval < 0) { | |
6b493a20 | 702 | err("error fcntl on write pipe: %s", strerror(errno)); |
ec9cc012 | 703 | goto exit; |
e5a2989e | 704 | } |
c8fa2d8b | 705 | retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC); |
ce043f85 | 706 | if (retval < 0) |
6b493a20 | 707 | err("error fcntl on write pipe: %s", strerror(errno)); |
f27125f9 | 708 | |
709 | /* set signal handlers */ | |
0786e8e5 | 710 | memset(&act, 0x00, sizeof(struct sigaction)); |
6b493a20 | 711 | act.sa_handler = (void (*)(int)) sig_handler; |
f27125f9 | 712 | sigemptyset(&act.sa_mask); |
f8911dbb KS |
713 | act.sa_flags = SA_RESTART; |
714 | sigaction(SIGINT, &act, NULL); | |
715 | sigaction(SIGTERM, &act, NULL); | |
f8911dbb KS |
716 | sigaction(SIGALRM, &act, NULL); |
717 | sigaction(SIGCHLD, &act, NULL); | |
7fafc032 | 718 | |
c2cf4012 KS |
719 | if (init_udevsend_socket() < 0) { |
720 | if (errno == EADDRINUSE) | |
ce043f85 | 721 | dbg("another udevd running, exit"); |
c2cf4012 KS |
722 | else |
723 | dbg("error initialising udevsend socket: %s", strerror(errno)); | |
53921bfa | 724 | |
53921bfa KS |
725 | goto exit; |
726 | } | |
727 | ||
7d855c31 KS |
728 | /* possible override of udev binary, used for testing */ |
729 | udev_bin = getenv("UDEV_BIN"); | |
730 | if (udev_bin != NULL) | |
6b493a20 | 731 | info("udev binary is set to '%s'", udev_bin); |
7d855c31 KS |
732 | else |
733 | udev_bin = UDEV_BIN; | |
0028653c | 734 | |
085cce37 | 735 | /* possible init of expected_seqnum value */ |
1e266989 KS |
736 | udevd_expected_seqnum = getenv("UDEVD_EXPECTED_SEQNUM"); |
737 | if (udevd_expected_seqnum != NULL) { | |
738 | expected_seqnum = strtoull(udevd_expected_seqnum, NULL, 10); | |
6b493a20 | 739 | info("initialize expected_seqnum to %llu", expected_seqnum); |
1e266989 KS |
740 | } |
741 | ||
085cce37 | 742 | /* get current time to provide shorter timeout on startup */ |
8b726878 KS |
743 | sysinfo(&info); |
744 | startup_time = info.uptime; | |
745 | ||
e5a2989e | 746 | FD_ZERO(&readfds); |
13f24d59 | 747 | FD_SET(udevsendsock, &readfds); |
e5a2989e | 748 | FD_SET(pipefds[0], &readfds); |
13f24d59 | 749 | maxsockplus = udevsendsock+1; |
2f6cbd19 | 750 | while (1) { |
021a294c KS |
751 | struct hotplug_msg *msg; |
752 | ||
f27125f9 | 753 | fd_set workreadfds = readfds; |
754 | retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL); | |
e5a2989e | 755 | |
f27125f9 | 756 | if (retval < 0) { |
e5a2989e KS |
757 | if (errno != EINTR) |
758 | dbg("error in select: %s", strerror(errno)); | |
f27125f9 | 759 | continue; |
2f6cbd19 | 760 | } |
e5a2989e | 761 | |
021a294c KS |
762 | if (FD_ISSET(udevsendsock, &workreadfds)) { |
763 | msg = get_udevsend_msg(); | |
764 | if (msg) | |
765 | msg_queue_insert(msg); | |
766 | } | |
e5a2989e | 767 | |
f27125f9 | 768 | if (FD_ISSET(pipefds[0], &workreadfds)) |
769 | user_sighandler(); | |
e5a2989e | 770 | |
5cab7caa KS |
771 | if (sigchilds_waiting) { |
772 | sigchilds_waiting = 0; | |
773 | reap_sigchilds(); | |
f27125f9 | 774 | } |
e5a2989e | 775 | |
f27125f9 | 776 | if (run_msg_q) { |
777 | run_msg_q = 0; | |
778 | msg_queue_manager(); | |
779 | } | |
e5a2989e | 780 | |
f27125f9 | 781 | if (run_exec_q) { |
5cab7caa KS |
782 | /* clean up running_list before calling exec_queue_manager() */ |
783 | if (sigchilds_waiting) { | |
784 | sigchilds_waiting = 0; | |
785 | reap_sigchilds(); | |
2f6cbd19 | 786 | } |
f27125f9 | 787 | |
788 | run_exec_q = 0; | |
789 | exec_queue_manager(); | |
53921bfa | 790 | } |
53921bfa | 791 | } |
ec9cc012 | 792 | |
53921bfa | 793 | exit: |
7257cb18 | 794 | logging_close(); |
5cab7caa | 795 | return 1; |
7fafc032 | 796 | } |