]>
Commit | Line | Data |
---|---|---|
7fafc032 | 1 | /* |
1298001e | 2 | * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org> |
2f6cbd19 | 3 | * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca> |
bb38678e SJR |
4 | * Copyright (C) 2009 Canonical Ltd. |
5 | * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com> | |
7fafc032 | 6 | * |
55e9959b KS |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 2 of the License, or | |
10 | * (at your option) any later version. | |
7fafc032 | 11 | * |
55e9959b KS |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
7fafc032 | 16 | * |
55e9959b KS |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
7fafc032 KS |
19 | */ |
20 | ||
a695feae | 21 | #include <stddef.h> |
7fafc032 KS |
22 | #include <signal.h> |
23 | #include <unistd.h> | |
24 | #include <errno.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
c3804728 | 27 | #include <stdbool.h> |
7fafc032 | 28 | #include <string.h> |
085cce37 | 29 | #include <ctype.h> |
085cce37 | 30 | #include <fcntl.h> |
0b3dfb3d | 31 | #include <time.h> |
b52a01ee | 32 | #include <getopt.h> |
78230c0d | 33 | #include <dirent.h> |
3ebdb81e | 34 | #include <sys/file.h> |
820fc48f | 35 | #include <sys/time.h> |
1e03b754 KS |
36 | #include <sys/prctl.h> |
37 | #include <sys/socket.h> | |
a65aa40f | 38 | #include <sys/un.h> |
1e03b754 | 39 | #include <sys/signalfd.h> |
ff2c503d | 40 | #include <sys/epoll.h> |
ede34445 | 41 | #include <sys/mount.h> |
3210a72b | 42 | #include <sys/poll.h> |
138068d6 | 43 | #include <sys/wait.h> |
dc117daa | 44 | #include <sys/stat.h> |
c895fd00 | 45 | #include <sys/ioctl.h> |
01618658 | 46 | #include <sys/inotify.h> |
761dfddc | 47 | #include <sys/utsname.h> |
7fafc032 KS |
48 | |
49 | #include "udev.h" | |
f3a740a5 | 50 | #include "udev-util.h" |
392ef7a2 | 51 | #include "sd-daemon.h" |
194bbe33 | 52 | #include "cgroup-util.h" |
5ba2dc25 | 53 | #include "dev-setup.h" |
a5c32cff | 54 | #include "fileio.h" |
7fafc032 | 55 | |
c3804728 | 56 | static bool debug; |
9e8fe79b | 57 | |
80df994c | 58 | void udev_main_log(struct udev *udev, int priority, |
912541b0 KS |
59 | const char *file, int line, const char *fn, |
60 | const char *format, va_list args) | |
7d563a17 | 61 | { |
baa30fbc | 62 | log_metav(priority, file, line, fn, format, args); |
7d563a17 KS |
63 | } |
64 | ||
d7ddce18 | 65 | static struct udev_rules *rules; |
d59f11e1 | 66 | static struct udev_ctrl *udev_ctrl; |
1e03b754 | 67 | static struct udev_monitor *monitor; |
3c6ee190 | 68 | static int worker_watch[2] = { -1, -1 }; |
ff2c503d KS |
69 | static int fd_signal = -1; |
70 | static int fd_ep = -1; | |
71 | static int fd_inotify = -1; | |
c3804728 | 72 | static bool stop_exec_queue; |
7c85d636 | 73 | static bool reload; |
87d55ff6 KS |
74 | static int children; |
75 | static int children_max; | |
c830e98d | 76 | static int exec_delay; |
2181d30a | 77 | static sigset_t sigmask_orig; |
ff2c503d KS |
78 | static UDEV_LIST(event_list); |
79 | static UDEV_LIST(worker_list); | |
e3dc56a2 | 80 | static char *udev_cgroup; |
c3804728 | 81 | static bool udev_exit; |
1e03b754 | 82 | |
1e03b754 | 83 | enum event_state { |
912541b0 KS |
84 | EVENT_UNDEF, |
85 | EVENT_QUEUED, | |
86 | EVENT_RUNNING, | |
1e03b754 KS |
87 | }; |
88 | ||
89 | struct event { | |
912541b0 KS |
90 | struct udev_list_node node; |
91 | struct udev *udev; | |
92 | struct udev_device *dev; | |
93 | enum event_state state; | |
94 | int exitcode; | |
95 | unsigned long long int delaying_seqnum; | |
96 | unsigned long long int seqnum; | |
97 | const char *devpath; | |
98 | size_t devpath_len; | |
99 | const char *devpath_old; | |
100 | dev_t devnum; | |
912541b0 | 101 | int ifindex; |
ea6039a3 | 102 | bool is_block; |
d8d4bee7 | 103 | #ifdef HAVE_FIRMWARE |
ea6039a3 | 104 | bool nodelay; |
61e536e1 | 105 | #endif |
1e03b754 KS |
106 | }; |
107 | ||
b27ee00b | 108 | static inline struct event *node_to_event(struct udev_list_node *node) |
7e027927 | 109 | { |
b27ee00b | 110 | return container_of(node, struct event, node); |
1e03b754 KS |
111 | } |
112 | ||
ff2c503d KS |
113 | static void event_queue_cleanup(struct udev *udev, enum event_state type); |
114 | ||
1e03b754 | 115 | enum worker_state { |
912541b0 KS |
116 | WORKER_UNDEF, |
117 | WORKER_RUNNING, | |
118 | WORKER_IDLE, | |
119 | WORKER_KILLED, | |
1e03b754 KS |
120 | }; |
121 | ||
122 | struct worker { | |
912541b0 KS |
123 | struct udev_list_node node; |
124 | struct udev *udev; | |
125 | int refcount; | |
126 | pid_t pid; | |
127 | struct udev_monitor *monitor; | |
128 | enum worker_state state; | |
129 | struct event *event; | |
40fe8b11 | 130 | usec_t event_start_usec; |
1e03b754 KS |
131 | }; |
132 | ||
133 | /* passed from worker to main process */ | |
134 | struct worker_message { | |
912541b0 KS |
135 | pid_t pid; |
136 | int exitcode; | |
1e03b754 KS |
137 | }; |
138 | ||
b27ee00b | 139 | static inline struct worker *node_to_worker(struct udev_list_node *node) |
1e03b754 | 140 | { |
b27ee00b | 141 | return container_of(node, struct worker, node); |
7e027927 KS |
142 | } |
143 | ||
9ea28c55 | 144 | static void event_queue_delete(struct event *event) |
fc465079 | 145 | { |
912541b0 | 146 | udev_list_node_remove(&event->node); |
912541b0 KS |
147 | udev_device_unref(event->dev); |
148 | free(event); | |
aa8734ff | 149 | } |
7a770250 | 150 | |
bc113de9 KS |
151 | static struct worker *worker_ref(struct worker *worker) |
152 | { | |
912541b0 KS |
153 | worker->refcount++; |
154 | return worker; | |
bc113de9 KS |
155 | } |
156 | ||
ff2c503d KS |
157 | static void worker_cleanup(struct worker *worker) |
158 | { | |
912541b0 KS |
159 | udev_list_node_remove(&worker->node); |
160 | udev_monitor_unref(worker->monitor); | |
161 | children--; | |
162 | free(worker); | |
ff2c503d KS |
163 | } |
164 | ||
1e03b754 KS |
165 | static void worker_unref(struct worker *worker) |
166 | { | |
912541b0 KS |
167 | worker->refcount--; |
168 | if (worker->refcount > 0) | |
169 | return; | |
9f6445e3 | 170 | log_debug("worker [%u] cleaned up", worker->pid); |
912541b0 | 171 | worker_cleanup(worker); |
ff2c503d KS |
172 | } |
173 | ||
174 | static void worker_list_cleanup(struct udev *udev) | |
175 | { | |
912541b0 | 176 | struct udev_list_node *loop, *tmp; |
ff2c503d | 177 | |
912541b0 KS |
178 | udev_list_node_foreach_safe(loop, tmp, &worker_list) { |
179 | struct worker *worker = node_to_worker(loop); | |
ff2c503d | 180 | |
912541b0 KS |
181 | worker_cleanup(worker); |
182 | } | |
fc465079 KS |
183 | } |
184 | ||
1e03b754 | 185 | static void worker_new(struct event *event) |
7fafc032 | 186 | { |
912541b0 KS |
187 | struct udev *udev = event->udev; |
188 | struct worker *worker; | |
189 | struct udev_monitor *worker_monitor; | |
190 | pid_t pid; | |
191 | ||
192 | /* listen for new events */ | |
193 | worker_monitor = udev_monitor_new_from_netlink(udev, NULL); | |
194 | if (worker_monitor == NULL) | |
195 | return; | |
196 | /* allow the main daemon netlink address to send devices to the worker */ | |
197 | udev_monitor_allow_unicast_sender(worker_monitor, monitor); | |
198 | udev_monitor_enable_receiving(worker_monitor); | |
199 | ||
955d98c9 | 200 | worker = new0(struct worker, 1); |
912541b0 KS |
201 | if (worker == NULL) { |
202 | udev_monitor_unref(worker_monitor); | |
203 | return; | |
204 | } | |
205 | /* worker + event reference */ | |
206 | worker->refcount = 2; | |
207 | worker->udev = udev; | |
208 | ||
209 | pid = fork(); | |
210 | switch (pid) { | |
211 | case 0: { | |
212 | struct udev_device *dev = NULL; | |
213 | int fd_monitor; | |
214 | struct epoll_event ep_signal, ep_monitor; | |
215 | sigset_t mask; | |
216 | int rc = EXIT_SUCCESS; | |
217 | ||
43095991 | 218 | /* take initial device from queue */ |
912541b0 KS |
219 | dev = event->dev; |
220 | event->dev = NULL; | |
221 | ||
222 | free(worker); | |
223 | worker_list_cleanup(udev); | |
224 | event_queue_cleanup(udev, EVENT_UNDEF); | |
912541b0 KS |
225 | udev_monitor_unref(monitor); |
226 | udev_ctrl_unref(udev_ctrl); | |
227 | close(fd_signal); | |
228 | close(fd_ep); | |
229 | close(worker_watch[READ_END]); | |
230 | ||
231 | sigfillset(&mask); | |
232 | fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); | |
233 | if (fd_signal < 0) { | |
9f6445e3 | 234 | log_error("error creating signalfd %m"); |
912541b0 KS |
235 | rc = 2; |
236 | goto out; | |
237 | } | |
238 | ||
239 | fd_ep = epoll_create1(EPOLL_CLOEXEC); | |
240 | if (fd_ep < 0) { | |
9f6445e3 | 241 | log_error("error creating epoll fd: %m"); |
912541b0 KS |
242 | rc = 3; |
243 | goto out; | |
244 | } | |
245 | ||
29804cc1 | 246 | memzero(&ep_signal, sizeof(struct epoll_event)); |
912541b0 KS |
247 | ep_signal.events = EPOLLIN; |
248 | ep_signal.data.fd = fd_signal; | |
249 | ||
250 | fd_monitor = udev_monitor_get_fd(worker_monitor); | |
29804cc1 | 251 | memzero(&ep_monitor, sizeof(struct epoll_event)); |
912541b0 KS |
252 | ep_monitor.events = EPOLLIN; |
253 | ep_monitor.data.fd = fd_monitor; | |
254 | ||
255 | if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 || | |
256 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) { | |
9f6445e3 | 257 | log_error("fail to add fds to epoll: %m"); |
912541b0 KS |
258 | rc = 4; |
259 | goto out; | |
260 | } | |
261 | ||
262 | /* request TERM signal if parent exits */ | |
263 | prctl(PR_SET_PDEATHSIG, SIGTERM); | |
264 | ||
145dae7e | 265 | /* reset OOM score, we only protect the main daemon */ |
574d5f2d | 266 | write_string_file("/proc/self/oom_score_adj", "0"); |
145dae7e | 267 | |
912541b0 KS |
268 | for (;;) { |
269 | struct udev_event *udev_event; | |
270 | struct worker_message msg; | |
3ebdb81e | 271 | int fd_lock = -1; |
bf9bead1 | 272 | int err = 0; |
912541b0 | 273 | |
9f6445e3 | 274 | log_debug("seq %llu running", udev_device_get_seqnum(dev)); |
912541b0 KS |
275 | udev_event = udev_event_new(dev); |
276 | if (udev_event == NULL) { | |
277 | rc = 5; | |
278 | goto out; | |
279 | } | |
280 | ||
281 | /* needed for SIGCHLD/SIGTERM in spawn() */ | |
282 | udev_event->fd_signal = fd_signal; | |
283 | ||
284 | if (exec_delay > 0) | |
285 | udev_event->exec_delay = exec_delay; | |
286 | ||
3ebdb81e KS |
287 | /* |
288 | * Take a "read lock" on the device node; this establishes | |
289 | * a concept of device "ownership" to serialize device | |
290 | * access. External processes holding a "write lock" will | |
291 | * cause udev to skip the event handling; in the case udev | |
292 | * acquired the lock, the external process will block until | |
293 | * udev has finished its event handling. | |
294 | */ | |
e918a1b5 KS |
295 | |
296 | /* | |
297 | * <kabi_> since we make check - device seems unused - we try | |
298 | * ioctl to deactivate - and device is found to be opened | |
299 | * <kay> sure, you try to take a write lock | |
300 | * <kay> if you get it udev is out | |
301 | * <kay> if you can't get it, udev is busy | |
302 | * <kabi_> we cannot deactivate openned device (as it is in-use) | |
303 | * <kay> maybe we should just exclude dm from that thing entirely | |
304 | * <kabi_> IMHO this sounds like a good plan for this moment | |
305 | */ | |
306 | if (streq_ptr("block", udev_device_get_subsystem(dev)) && | |
638ca89c | 307 | !startswith(udev_device_get_sysname(dev), "dm-")) { |
3ebdb81e KS |
308 | struct udev_device *d = dev; |
309 | ||
310 | if (streq_ptr("partition", udev_device_get_devtype(d))) | |
311 | d = udev_device_get_parent(d); | |
312 | ||
313 | if (d) { | |
314 | fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); | |
315 | if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) { | |
316 | log_debug("Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d)); | |
317 | err = -EWOULDBLOCK; | |
3d06f418 | 318 | fd_lock = safe_close(fd_lock); |
3ebdb81e KS |
319 | goto skip; |
320 | } | |
321 | } | |
322 | } | |
323 | ||
912541b0 | 324 | /* apply rules, create node, symlinks */ |
1ea97217 | 325 | udev_event_execute_rules(udev_event, rules, &sigmask_orig); |
912541b0 | 326 | |
1ea97217 | 327 | udev_event_execute_run(udev_event, &sigmask_orig); |
912541b0 KS |
328 | |
329 | /* apply/restore inotify watch */ | |
bf9bead1 | 330 | if (udev_event->inotify_watch) { |
912541b0 KS |
331 | udev_watch_begin(udev, dev); |
332 | udev_device_update_db(dev); | |
333 | } | |
334 | ||
3d06f418 | 335 | safe_close(fd_lock); |
3ebdb81e | 336 | |
912541b0 KS |
337 | /* send processed event back to libudev listeners */ |
338 | udev_monitor_send_device(worker_monitor, NULL, dev); | |
339 | ||
3ebdb81e | 340 | skip: |
912541b0 | 341 | /* send udevd the result of the event execution */ |
29804cc1 | 342 | memzero(&msg, sizeof(struct worker_message)); |
3ebdb81e | 343 | msg.exitcode = err; |
912541b0 KS |
344 | msg.pid = getpid(); |
345 | send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0); | |
346 | ||
9f6445e3 | 347 | log_debug("seq %llu processed with %i", udev_device_get_seqnum(dev), err); |
912541b0 KS |
348 | |
349 | udev_device_unref(dev); | |
350 | dev = NULL; | |
351 | ||
73814ca2 CW |
352 | if (udev_event->sigterm) { |
353 | udev_event_unref(udev_event); | |
47e737dc | 354 | goto out; |
73814ca2 CW |
355 | } |
356 | ||
357 | udev_event_unref(udev_event); | |
47e737dc | 358 | |
912541b0 KS |
359 | /* wait for more device messages from main udevd, or term signal */ |
360 | while (dev == NULL) { | |
361 | struct epoll_event ev[4]; | |
362 | int fdcount; | |
363 | int i; | |
364 | ||
8fef0ff2 | 365 | fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1); |
912541b0 KS |
366 | if (fdcount < 0) { |
367 | if (errno == EINTR) | |
368 | continue; | |
9f6445e3 | 369 | log_error("failed to poll: %m"); |
912541b0 KS |
370 | goto out; |
371 | } | |
372 | ||
373 | for (i = 0; i < fdcount; i++) { | |
374 | if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) { | |
375 | dev = udev_monitor_receive_device(worker_monitor); | |
376 | break; | |
377 | } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) { | |
378 | struct signalfd_siginfo fdsi; | |
379 | ssize_t size; | |
380 | ||
381 | size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo)); | |
382 | if (size != sizeof(struct signalfd_siginfo)) | |
383 | continue; | |
384 | switch (fdsi.ssi_signo) { | |
385 | case SIGTERM: | |
386 | goto out; | |
387 | } | |
388 | } | |
389 | } | |
390 | } | |
391 | } | |
82063a88 | 392 | out: |
912541b0 | 393 | udev_device_unref(dev); |
3d06f418 KS |
394 | safe_close(fd_signal); |
395 | safe_close(fd_ep); | |
912541b0 KS |
396 | close(fd_inotify); |
397 | close(worker_watch[WRITE_END]); | |
398 | udev_rules_unref(rules); | |
e64fae55 | 399 | udev_builtin_exit(udev); |
912541b0 KS |
400 | udev_monitor_unref(worker_monitor); |
401 | udev_unref(udev); | |
baa30fbc | 402 | log_close(); |
912541b0 KS |
403 | exit(rc); |
404 | } | |
405 | case -1: | |
406 | udev_monitor_unref(worker_monitor); | |
407 | event->state = EVENT_QUEUED; | |
408 | free(worker); | |
9f6445e3 | 409 | log_error("fork of child failed: %m"); |
912541b0 KS |
410 | break; |
411 | default: | |
412 | /* close monitor, but keep address around */ | |
413 | udev_monitor_disconnect(worker_monitor); | |
414 | worker->monitor = worker_monitor; | |
415 | worker->pid = pid; | |
416 | worker->state = WORKER_RUNNING; | |
40fe8b11 | 417 | worker->event_start_usec = now(CLOCK_MONOTONIC); |
912541b0 KS |
418 | worker->event = event; |
419 | event->state = EVENT_RUNNING; | |
420 | udev_list_node_append(&worker->node, &worker_list); | |
421 | children++; | |
9f6445e3 | 422 | log_debug("seq %llu forked new worker [%u]", udev_device_get_seqnum(event->dev), pid); |
912541b0 KS |
423 | break; |
424 | } | |
7fafc032 KS |
425 | } |
426 | ||
57c6f8ae | 427 | static void event_run(struct event *event) |
fc465079 | 428 | { |
912541b0 KS |
429 | struct udev_list_node *loop; |
430 | ||
431 | udev_list_node_foreach(loop, &worker_list) { | |
432 | struct worker *worker = node_to_worker(loop); | |
433 | ssize_t count; | |
434 | ||
435 | if (worker->state != WORKER_IDLE) | |
436 | continue; | |
437 | ||
438 | count = udev_monitor_send_device(monitor, worker->monitor, event->dev); | |
439 | if (count < 0) { | |
9f6445e3 | 440 | log_error("worker [%u] did not accept message %zi (%m), kill it", worker->pid, count); |
912541b0 KS |
441 | kill(worker->pid, SIGKILL); |
442 | worker->state = WORKER_KILLED; | |
443 | continue; | |
444 | } | |
445 | worker_ref(worker); | |
446 | worker->event = event; | |
447 | worker->state = WORKER_RUNNING; | |
40fe8b11 | 448 | worker->event_start_usec = now(CLOCK_MONOTONIC); |
912541b0 KS |
449 | event->state = EVENT_RUNNING; |
450 | return; | |
451 | } | |
452 | ||
453 | if (children >= children_max) { | |
454 | if (children_max > 1) | |
9f6445e3 | 455 | log_debug("maximum number (%i) of children reached", children); |
912541b0 KS |
456 | return; |
457 | } | |
458 | ||
459 | /* start new worker and pass initial device */ | |
460 | worker_new(event); | |
1e03b754 KS |
461 | } |
462 | ||
40929a02 | 463 | static int event_queue_insert(struct udev_device *dev) |
1e03b754 | 464 | { |
912541b0 KS |
465 | struct event *event; |
466 | ||
955d98c9 | 467 | event = new0(struct event, 1); |
912541b0 KS |
468 | if (event == NULL) |
469 | return -1; | |
470 | ||
471 | event->udev = udev_device_get_udev(dev); | |
472 | event->dev = dev; | |
473 | event->seqnum = udev_device_get_seqnum(dev); | |
474 | event->devpath = udev_device_get_devpath(dev); | |
475 | event->devpath_len = strlen(event->devpath); | |
476 | event->devpath_old = udev_device_get_devpath_old(dev); | |
477 | event->devnum = udev_device_get_devnum(dev); | |
ea6039a3 | 478 | event->is_block = streq("block", udev_device_get_subsystem(dev)); |
912541b0 | 479 | event->ifindex = udev_device_get_ifindex(dev); |
d8d4bee7 | 480 | #ifdef HAVE_FIRMWARE |
ea6039a3 KS |
481 | if (streq(udev_device_get_subsystem(dev), "firmware")) |
482 | event->nodelay = true; | |
61e536e1 | 483 | #endif |
912541b0 | 484 | |
9f6445e3 | 485 | log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev), |
912541b0 KS |
486 | udev_device_get_action(dev), udev_device_get_subsystem(dev)); |
487 | ||
488 | event->state = EVENT_QUEUED; | |
489 | udev_list_node_append(&event->node, &event_list); | |
490 | return 0; | |
fc465079 KS |
491 | } |
492 | ||
194bbe33 | 493 | static void worker_kill(struct udev *udev) |
1e03b754 | 494 | { |
912541b0 | 495 | struct udev_list_node *loop; |
1e03b754 | 496 | |
912541b0 KS |
497 | udev_list_node_foreach(loop, &worker_list) { |
498 | struct worker *worker = node_to_worker(loop); | |
1e03b754 | 499 | |
912541b0 KS |
500 | if (worker->state == WORKER_KILLED) |
501 | continue; | |
1e03b754 | 502 | |
912541b0 KS |
503 | worker->state = WORKER_KILLED; |
504 | kill(worker->pid, SIGTERM); | |
505 | } | |
1e03b754 KS |
506 | } |
507 | ||
e3196993 | 508 | /* lookup event for identical, parent, child device */ |
19711e19 | 509 | static bool is_devpath_busy(struct event *event) |
7fafc032 | 510 | { |
912541b0 KS |
511 | struct udev_list_node *loop; |
512 | size_t common; | |
513 | ||
514 | /* check if queue contains events we depend on */ | |
515 | udev_list_node_foreach(loop, &event_list) { | |
516 | struct event *loop_event = node_to_event(loop); | |
517 | ||
518 | /* we already found a later event, earlier can not block us, no need to check again */ | |
519 | if (loop_event->seqnum < event->delaying_seqnum) | |
520 | continue; | |
521 | ||
522 | /* event we checked earlier still exists, no need to check again */ | |
523 | if (loop_event->seqnum == event->delaying_seqnum) | |
524 | return true; | |
525 | ||
526 | /* found ourself, no later event can block us */ | |
527 | if (loop_event->seqnum >= event->seqnum) | |
528 | break; | |
529 | ||
530 | /* check major/minor */ | |
531 | if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block) | |
532 | return true; | |
533 | ||
534 | /* check network device ifindex */ | |
535 | if (event->ifindex != 0 && event->ifindex == loop_event->ifindex) | |
536 | return true; | |
537 | ||
538 | /* check our old name */ | |
090be865 | 539 | if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) { |
912541b0 KS |
540 | event->delaying_seqnum = loop_event->seqnum; |
541 | return true; | |
542 | } | |
543 | ||
544 | /* compare devpath */ | |
545 | common = MIN(loop_event->devpath_len, event->devpath_len); | |
546 | ||
547 | /* one devpath is contained in the other? */ | |
548 | if (memcmp(loop_event->devpath, event->devpath, common) != 0) | |
549 | continue; | |
550 | ||
551 | /* identical device event found */ | |
552 | if (loop_event->devpath_len == event->devpath_len) { | |
553 | /* devices names might have changed/swapped in the meantime */ | |
554 | if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block)) | |
555 | continue; | |
556 | if (event->ifindex != 0 && event->ifindex != loop_event->ifindex) | |
557 | continue; | |
558 | event->delaying_seqnum = loop_event->seqnum; | |
559 | return true; | |
560 | } | |
561 | ||
d8d4bee7 | 562 | #ifdef HAVE_FIRMWARE |
ea6039a3 KS |
563 | /* allow to bypass the dependency tracking */ |
564 | if (event->nodelay) | |
565 | continue; | |
61e536e1 | 566 | #endif |
ea6039a3 | 567 | |
912541b0 KS |
568 | /* parent device event found */ |
569 | if (event->devpath[common] == '/') { | |
570 | event->delaying_seqnum = loop_event->seqnum; | |
571 | return true; | |
572 | } | |
573 | ||
574 | /* child device event found */ | |
575 | if (loop_event->devpath[common] == '/') { | |
576 | event->delaying_seqnum = loop_event->seqnum; | |
577 | return true; | |
578 | } | |
579 | ||
580 | /* no matching device */ | |
581 | continue; | |
582 | } | |
583 | ||
584 | return false; | |
7fafc032 KS |
585 | } |
586 | ||
ff2c503d | 587 | static void event_queue_start(struct udev *udev) |
7fafc032 | 588 | { |
912541b0 | 589 | struct udev_list_node *loop; |
8ab44e3f | 590 | |
912541b0 KS |
591 | udev_list_node_foreach(loop, &event_list) { |
592 | struct event *event = node_to_event(loop); | |
0bc74ea7 | 593 | |
912541b0 KS |
594 | if (event->state != EVENT_QUEUED) |
595 | continue; | |
0bc74ea7 | 596 | |
912541b0 | 597 | /* do not start event if parent or child event is still running */ |
baa30fbc | 598 | if (is_devpath_busy(event)) |
912541b0 | 599 | continue; |
fc465079 | 600 | |
912541b0 KS |
601 | event_run(event); |
602 | } | |
1e03b754 KS |
603 | } |
604 | ||
ff2c503d KS |
605 | static void event_queue_cleanup(struct udev *udev, enum event_state match_type) |
606 | { | |
912541b0 | 607 | struct udev_list_node *loop, *tmp; |
ff2c503d | 608 | |
912541b0 KS |
609 | udev_list_node_foreach_safe(loop, tmp, &event_list) { |
610 | struct event *event = node_to_event(loop); | |
ff2c503d | 611 | |
912541b0 KS |
612 | if (match_type != EVENT_UNDEF && match_type != event->state) |
613 | continue; | |
ff2c503d | 614 | |
9ea28c55 | 615 | event_queue_delete(event); |
912541b0 | 616 | } |
ff2c503d KS |
617 | } |
618 | ||
619 | static void worker_returned(int fd_worker) | |
1e03b754 | 620 | { |
912541b0 KS |
621 | for (;;) { |
622 | struct worker_message msg; | |
623 | ssize_t size; | |
624 | struct udev_list_node *loop; | |
625 | ||
626 | size = recv(fd_worker, &msg, sizeof(struct worker_message), MSG_DONTWAIT); | |
627 | if (size != sizeof(struct worker_message)) | |
628 | break; | |
629 | ||
630 | /* lookup worker who sent the signal */ | |
631 | udev_list_node_foreach(loop, &worker_list) { | |
632 | struct worker *worker = node_to_worker(loop); | |
633 | ||
634 | if (worker->pid != msg.pid) | |
635 | continue; | |
636 | ||
637 | /* worker returned */ | |
e64fae55 KS |
638 | if (worker->event) { |
639 | worker->event->exitcode = msg.exitcode; | |
9ea28c55 | 640 | event_queue_delete(worker->event); |
e64fae55 KS |
641 | worker->event = NULL; |
642 | } | |
912541b0 KS |
643 | if (worker->state != WORKER_KILLED) |
644 | worker->state = WORKER_IDLE; | |
645 | worker_unref(worker); | |
646 | break; | |
647 | } | |
648 | } | |
88f4b648 KS |
649 | } |
650 | ||
3b47c739 | 651 | /* receive the udevd message from userspace */ |
ff2c503d | 652 | static struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl) |
7fafc032 | 653 | { |
912541b0 KS |
654 | struct udev *udev = udev_ctrl_get_udev(uctrl); |
655 | struct udev_ctrl_connection *ctrl_conn; | |
656 | struct udev_ctrl_msg *ctrl_msg = NULL; | |
657 | const char *str; | |
658 | int i; | |
659 | ||
660 | ctrl_conn = udev_ctrl_get_connection(uctrl); | |
661 | if (ctrl_conn == NULL) | |
662 | goto out; | |
663 | ||
664 | ctrl_msg = udev_ctrl_receive_msg(ctrl_conn); | |
665 | if (ctrl_msg == NULL) | |
666 | goto out; | |
667 | ||
668 | i = udev_ctrl_get_set_log_level(ctrl_msg); | |
669 | if (i >= 0) { | |
ed14edc0 | 670 | log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", i); |
baa30fbc | 671 | log_set_max_level(i); |
912541b0 | 672 | udev_set_log_priority(udev, i); |
194bbe33 | 673 | worker_kill(udev); |
912541b0 KS |
674 | } |
675 | ||
676 | if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) { | |
9f6445e3 | 677 | log_debug("udevd message (STOP_EXEC_QUEUE) received"); |
912541b0 KS |
678 | stop_exec_queue = true; |
679 | } | |
680 | ||
681 | if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) { | |
9f6445e3 | 682 | log_debug("udevd message (START_EXEC_QUEUE) received"); |
912541b0 KS |
683 | stop_exec_queue = false; |
684 | } | |
685 | ||
686 | if (udev_ctrl_get_reload(ctrl_msg) > 0) { | |
9f6445e3 | 687 | log_debug("udevd message (RELOAD) received"); |
912541b0 KS |
688 | reload = true; |
689 | } | |
690 | ||
691 | str = udev_ctrl_get_set_env(ctrl_msg); | |
692 | if (str != NULL) { | |
693 | char *key; | |
694 | ||
695 | key = strdup(str); | |
696 | if (key != NULL) { | |
697 | char *val; | |
698 | ||
699 | val = strchr(key, '='); | |
700 | if (val != NULL) { | |
701 | val[0] = '\0'; | |
702 | val = &val[1]; | |
703 | if (val[0] == '\0') { | |
9f6445e3 | 704 | log_debug("udevd message (ENV) received, unset '%s'", key); |
912541b0 KS |
705 | udev_add_property(udev, key, NULL); |
706 | } else { | |
9f6445e3 | 707 | log_debug("udevd message (ENV) received, set '%s=%s'", key, val); |
912541b0 KS |
708 | udev_add_property(udev, key, val); |
709 | } | |
710 | } else { | |
9f6445e3 | 711 | log_error("wrong key format '%s'", key); |
912541b0 KS |
712 | } |
713 | free(key); | |
714 | } | |
194bbe33 | 715 | worker_kill(udev); |
912541b0 KS |
716 | } |
717 | ||
718 | i = udev_ctrl_get_set_children_max(ctrl_msg); | |
719 | if (i >= 0) { | |
9f6445e3 | 720 | log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i", i); |
912541b0 KS |
721 | children_max = i; |
722 | } | |
723 | ||
724 | if (udev_ctrl_get_ping(ctrl_msg) > 0) | |
9f6445e3 | 725 | log_debug("udevd message (SYNC) received"); |
912541b0 KS |
726 | |
727 | if (udev_ctrl_get_exit(ctrl_msg) > 0) { | |
9f6445e3 | 728 | log_debug("udevd message (EXIT) received"); |
912541b0 KS |
729 | udev_exit = true; |
730 | /* keep reference to block the client until we exit */ | |
731 | udev_ctrl_connection_ref(ctrl_conn); | |
732 | } | |
ff2c503d | 733 | out: |
912541b0 KS |
734 | udev_ctrl_msg_unref(ctrl_msg); |
735 | return udev_ctrl_connection_unref(ctrl_conn); | |
88f4b648 | 736 | } |
4a231017 | 737 | |
f3a740a5 | 738 | static int synthesize_change(struct udev_device *dev) { |
edd32000 | 739 | char filename[UTIL_PATH_SIZE]; |
f3a740a5 | 740 | int r; |
edd32000 | 741 | |
f3a740a5 | 742 | if (streq_ptr("block", udev_device_get_subsystem(dev)) && |
ede34445 | 743 | streq_ptr("disk", udev_device_get_devtype(dev)) && |
638ca89c | 744 | !startswith(udev_device_get_sysname(dev), "dm-")) { |
e9fc29f4 KS |
745 | bool part_table_read = false; |
746 | bool has_partitions = false; | |
ede34445 | 747 | int fd; |
f3a740a5 KS |
748 | struct udev *udev = udev_device_get_udev(dev); |
749 | _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL; | |
750 | struct udev_list_entry *item; | |
751 | ||
ede34445 | 752 | /* |
e9fc29f4 KS |
753 | * Try to re-read the partition table. This only succeeds if |
754 | * none of the devices is busy. The kernel returns 0 if no | |
755 | * partition table is found, and we will not get an event for | |
756 | * the disk. | |
ede34445 | 757 | */ |
02ba8fb3 | 758 | fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); |
ede34445 | 759 | if (fd >= 0) { |
02ba8fb3 KS |
760 | r = flock(fd, LOCK_EX|LOCK_NB); |
761 | if (r >= 0) | |
762 | r = ioctl(fd, BLKRRPART, 0); | |
763 | ||
ede34445 KS |
764 | close(fd); |
765 | if (r >= 0) | |
e9fc29f4 | 766 | part_table_read = true; |
ede34445 KS |
767 | } |
768 | ||
e9fc29f4 | 769 | /* search for partitions */ |
f3a740a5 KS |
770 | e = udev_enumerate_new(udev); |
771 | if (!e) | |
772 | return -ENOMEM; | |
773 | ||
774 | r = udev_enumerate_add_match_parent(e, dev); | |
775 | if (r < 0) | |
776 | return r; | |
777 | ||
778 | r = udev_enumerate_add_match_subsystem(e, "block"); | |
779 | if (r < 0) | |
780 | return r; | |
781 | ||
782 | r = udev_enumerate_scan_devices(e); | |
e9fc29f4 KS |
783 | |
784 | udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) { | |
785 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; | |
786 | ||
787 | d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)); | |
788 | if (!d) | |
789 | continue; | |
790 | ||
791 | if (!streq_ptr("partition", udev_device_get_devtype(d))) | |
792 | continue; | |
793 | ||
794 | has_partitions = true; | |
795 | break; | |
796 | } | |
797 | ||
798 | /* | |
799 | * We have partitions and re-read the table, the kernel already sent | |
800 | * out a "change" event for the disk, and "remove/add" for all | |
801 | * partitions. | |
802 | */ | |
803 | if (part_table_read && has_partitions) | |
804 | return 0; | |
805 | ||
806 | /* | |
807 | * We have partitions but re-reading the partition table did not | |
808 | * work, synthesize "change" for the disk and all partitions. | |
809 | */ | |
810 | log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev)); | |
811 | strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL); | |
812 | write_string_file(filename, "change"); | |
813 | ||
f3a740a5 KS |
814 | udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) { |
815 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; | |
816 | ||
817 | d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)); | |
818 | if (!d) | |
819 | continue; | |
820 | ||
821 | if (!streq_ptr("partition", udev_device_get_devtype(d))) | |
822 | continue; | |
823 | ||
824 | log_debug("device %s closed, synthesising partition '%s' 'change'", | |
825 | udev_device_get_devnode(dev), udev_device_get_devnode(d)); | |
826 | strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL); | |
827 | write_string_file(filename, "change"); | |
828 | } | |
ede34445 KS |
829 | |
830 | return 0; | |
f3a740a5 KS |
831 | } |
832 | ||
ede34445 KS |
833 | log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev)); |
834 | strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL); | |
835 | write_string_file(filename, "change"); | |
836 | ||
f3a740a5 | 837 | return 0; |
edd32000 KS |
838 | } |
839 | ||
bd284db1 SJR |
840 | static int handle_inotify(struct udev *udev) |
841 | { | |
912541b0 KS |
842 | int nbytes, pos; |
843 | char *buf; | |
844 | struct inotify_event *ev; | |
edd32000 | 845 | int r; |
912541b0 | 846 | |
edd32000 KS |
847 | r = ioctl(fd_inotify, FIONREAD, &nbytes); |
848 | if (r < 0 || nbytes <= 0) | |
849 | return -errno; | |
912541b0 KS |
850 | |
851 | buf = malloc(nbytes); | |
edd32000 | 852 | if (!buf) { |
9f6445e3 | 853 | log_error("error getting buffer for inotify"); |
edd32000 | 854 | return -ENOMEM; |
912541b0 KS |
855 | } |
856 | ||
857 | nbytes = read(fd_inotify, buf, nbytes); | |
858 | ||
859 | for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) { | |
860 | struct udev_device *dev; | |
861 | ||
862 | ev = (struct inotify_event *)(buf + pos); | |
863 | dev = udev_watch_lookup(udev, ev->wd); | |
edd32000 KS |
864 | if (!dev) |
865 | continue; | |
912541b0 | 866 | |
edd32000 KS |
867 | log_debug("inotify event: %x for %s", ev->mask, udev_device_get_devnode(dev)); |
868 | if (ev->mask & IN_CLOSE_WRITE) | |
869 | synthesize_change(dev); | |
870 | else if (ev->mask & IN_IGNORED) | |
871 | udev_watch_end(udev, dev); | |
912541b0 | 872 | |
edd32000 | 873 | udev_device_unref(dev); |
912541b0 KS |
874 | } |
875 | ||
876 | free(buf); | |
877 | return 0; | |
bd284db1 SJR |
878 | } |
879 | ||
45798927 | 880 | static void handle_signal(struct udev *udev, int signo) |
7fafc032 | 881 | { |
912541b0 KS |
882 | switch (signo) { |
883 | case SIGINT: | |
884 | case SIGTERM: | |
885 | udev_exit = true; | |
886 | break; | |
887 | case SIGCHLD: | |
888 | for (;;) { | |
889 | pid_t pid; | |
890 | int status; | |
891 | struct udev_list_node *loop, *tmp; | |
892 | ||
893 | pid = waitpid(-1, &status, WNOHANG); | |
894 | if (pid <= 0) | |
895 | break; | |
896 | ||
897 | udev_list_node_foreach_safe(loop, tmp, &worker_list) { | |
898 | struct worker *worker = node_to_worker(loop); | |
899 | ||
900 | if (worker->pid != pid) | |
901 | continue; | |
9f6445e3 | 902 | log_debug("worker [%u] exit", pid); |
912541b0 KS |
903 | |
904 | if (WIFEXITED(status)) { | |
905 | if (WEXITSTATUS(status) != 0) | |
9f6445e3 | 906 | log_error("worker [%u] exit with return code %i", |
97fec53e | 907 | pid, WEXITSTATUS(status)); |
912541b0 | 908 | } else if (WIFSIGNALED(status)) { |
9f6445e3 | 909 | log_error("worker [%u] terminated by signal %i (%s)", |
97fec53e | 910 | pid, WTERMSIG(status), strsignal(WTERMSIG(status))); |
912541b0 | 911 | } else if (WIFSTOPPED(status)) { |
9f6445e3 | 912 | log_error("worker [%u] stopped", pid); |
912541b0 | 913 | } else if (WIFCONTINUED(status)) { |
9f6445e3 | 914 | log_error("worker [%u] continued", pid); |
912541b0 | 915 | } else { |
9f6445e3 | 916 | log_error("worker [%u] exit with status 0x%04x", pid, status); |
912541b0 KS |
917 | } |
918 | ||
919 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { | |
e64fae55 | 920 | if (worker->event) { |
9f6445e3 | 921 | log_error("worker [%u] failed while handling '%s'", |
baa30fbc | 922 | pid, worker->event->devpath); |
912541b0 | 923 | worker->event->exitcode = -32; |
9ea28c55 KS |
924 | event_queue_delete(worker->event); |
925 | ||
912541b0 KS |
926 | /* drop reference taken for state 'running' */ |
927 | worker_unref(worker); | |
928 | } | |
929 | } | |
930 | worker_unref(worker); | |
931 | break; | |
932 | } | |
933 | } | |
934 | break; | |
935 | case SIGHUP: | |
936 | reload = true; | |
937 | break; | |
938 | } | |
f27125f9 | 939 | } |
940 | ||
7459bcdc KS |
941 | static int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink) |
942 | { | |
912541b0 KS |
943 | int ctrl = -1, netlink = -1; |
944 | int fd, n; | |
945 | ||
946 | n = sd_listen_fds(true); | |
947 | if (n <= 0) | |
948 | return -1; | |
949 | ||
950 | for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) { | |
951 | if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1)) { | |
952 | if (ctrl >= 0) | |
953 | return -1; | |
954 | ctrl = fd; | |
955 | continue; | |
956 | } | |
957 | ||
958 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1)) { | |
959 | if (netlink >= 0) | |
960 | return -1; | |
961 | netlink = fd; | |
962 | continue; | |
963 | } | |
964 | ||
965 | return -1; | |
966 | } | |
967 | ||
968 | if (ctrl < 0 || netlink < 0) | |
969 | return -1; | |
970 | ||
9f6445e3 | 971 | log_debug("ctrl=%i netlink=%i", ctrl, netlink); |
912541b0 KS |
972 | *rctrl = ctrl; |
973 | *rnetlink = netlink; | |
974 | return 0; | |
7459bcdc KS |
975 | } |
976 | ||
e6f86cac KS |
977 | /* |
978 | * read the kernel commandline, in case we need to get into debug mode | |
979 | * udev.log-priority=<level> syslog priority | |
980 | * udev.children-max=<number of workers> events are fully serialized if set to 1 | |
981 | * udev.exec-delay=<number of seconds> delay execution of every executed program | |
982 | */ | |
983 | static void kernel_cmdline_options(struct udev *udev) | |
984 | { | |
74df0fca LP |
985 | _cleanup_free_ char *line = NULL; |
986 | char *w, *state; | |
e6f86cac | 987 | size_t l; |
74df0fca | 988 | int r; |
e6f86cac | 989 | |
74df0fca LP |
990 | r = proc_cmdline(&line); |
991 | if (r < 0) | |
992 | log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r)); | |
993 | if (r <= 0) | |
e6f86cac KS |
994 | return; |
995 | ||
996 | FOREACH_WORD_QUOTED(w, l, line, state) { | |
997 | char *s, *opt; | |
998 | ||
999 | s = strndup(w, l); | |
1000 | if (!s) | |
1001 | break; | |
1002 | ||
1003 | /* accept the same options for the initrd, prefixed with "rd." */ | |
1004 | if (in_initrd() && startswith(s, "rd.")) | |
1005 | opt = s + 3; | |
1006 | else | |
1007 | opt = s; | |
1008 | ||
023b9d33 KS |
1009 | if (startswith(opt, "udev.log-priority=")) { |
1010 | int prio; | |
e6f86cac | 1011 | |
023b9d33 KS |
1012 | prio = util_log_priority(opt + 18); |
1013 | log_set_max_level(prio); | |
1014 | udev_set_log_priority(udev, prio); | |
1015 | } else if (startswith(opt, "udev.children-max=")) { | |
e6f86cac | 1016 | children_max = strtoul(opt + 18, NULL, 0); |
023b9d33 | 1017 | } else if (startswith(opt, "udev.exec-delay=")) { |
e6f86cac | 1018 | exec_delay = strtoul(opt + 16, NULL, 0); |
023b9d33 | 1019 | } |
e6f86cac KS |
1020 | |
1021 | free(s); | |
1022 | } | |
e6f86cac KS |
1023 | } |
1024 | ||
59345311 | 1025 | int main(int argc, char *argv[]) |
c2cf4012 | 1026 | { |
912541b0 | 1027 | struct udev *udev; |
912541b0 KS |
1028 | sigset_t mask; |
1029 | int daemonize = false; | |
1030 | int resolve_names = 1; | |
1031 | static const struct option options[] = { | |
1032 | { "daemon", no_argument, NULL, 'd' }, | |
1033 | { "debug", no_argument, NULL, 'D' }, | |
1034 | { "children-max", required_argument, NULL, 'c' }, | |
1035 | { "exec-delay", required_argument, NULL, 'e' }, | |
1036 | { "resolve-names", required_argument, NULL, 'N' }, | |
1037 | { "help", no_argument, NULL, 'h' }, | |
1038 | { "version", no_argument, NULL, 'V' }, | |
1039 | {} | |
1040 | }; | |
1041 | int fd_ctrl = -1; | |
1042 | int fd_netlink = -1; | |
1043 | int fd_worker = -1; | |
1044 | struct epoll_event ep_ctrl, ep_inotify, ep_signal, ep_netlink, ep_worker; | |
1045 | struct udev_ctrl_connection *ctrl_conn = NULL; | |
912541b0 KS |
1046 | int rc = 1; |
1047 | ||
1048 | udev = udev_new(); | |
1049 | if (udev == NULL) | |
1050 | goto exit; | |
1051 | ||
4b261568 | 1052 | log_set_target(LOG_TARGET_AUTO); |
baa30fbc | 1053 | log_parse_environment(); |
4b261568 | 1054 | log_open(); |
689a97f5 | 1055 | |
912541b0 | 1056 | udev_set_log_fn(udev, udev_main_log); |
689a97f5 TG |
1057 | log_set_max_level(udev_get_log_priority(udev)); |
1058 | ||
9f6445e3 | 1059 | log_debug("version %s", VERSION); |
0f9963a8 | 1060 | label_init("/dev"); |
912541b0 KS |
1061 | |
1062 | for (;;) { | |
1063 | int option; | |
1064 | ||
5bbbe461 | 1065 | option = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL); |
912541b0 KS |
1066 | if (option == -1) |
1067 | break; | |
1068 | ||
1069 | switch (option) { | |
1070 | case 'd': | |
1071 | daemonize = true; | |
1072 | break; | |
1073 | case 'c': | |
1074 | children_max = strtoul(optarg, NULL, 0); | |
1075 | break; | |
1076 | case 'e': | |
1077 | exec_delay = strtoul(optarg, NULL, 0); | |
1078 | break; | |
1079 | case 'D': | |
1080 | debug = true; | |
194bbe33 | 1081 | log_set_max_level(LOG_DEBUG); |
7781e063 | 1082 | udev_set_log_priority(udev, LOG_DEBUG); |
912541b0 KS |
1083 | break; |
1084 | case 'N': | |
090be865 | 1085 | if (streq(optarg, "early")) { |
912541b0 | 1086 | resolve_names = 1; |
090be865 | 1087 | } else if (streq(optarg, "late")) { |
912541b0 | 1088 | resolve_names = 0; |
090be865 | 1089 | } else if (streq(optarg, "never")) { |
912541b0 KS |
1090 | resolve_names = -1; |
1091 | } else { | |
1092 | fprintf(stderr, "resolve-names must be early, late or never\n"); | |
9f6445e3 | 1093 | log_error("resolve-names must be early, late or never"); |
912541b0 KS |
1094 | goto exit; |
1095 | } | |
1096 | break; | |
1097 | case 'h': | |
1098 | printf("Usage: udevd OPTIONS\n" | |
1099 | " --daemon\n" | |
1100 | " --debug\n" | |
1101 | " --children-max=<maximum number of workers>\n" | |
1102 | " --exec-delay=<seconds to wait before executing RUN=>\n" | |
1103 | " --resolve-names=early|late|never\n" | |
1104 | " --version\n" | |
1105 | " --help\n" | |
1106 | "\n"); | |
1107 | goto exit; | |
1108 | case 'V': | |
1109 | printf("%s\n", VERSION); | |
1110 | goto exit; | |
1111 | default: | |
1112 | goto exit; | |
1113 | } | |
1114 | } | |
1115 | ||
e6f86cac | 1116 | kernel_cmdline_options(udev); |
912541b0 KS |
1117 | |
1118 | if (getuid() != 0) { | |
1119 | fprintf(stderr, "root privileges required\n"); | |
9f6445e3 | 1120 | log_error("root privileges required"); |
912541b0 KS |
1121 | goto exit; |
1122 | } | |
1123 | ||
1124 | /* set umask before creating any file/directory */ | |
1125 | chdir("/"); | |
1126 | umask(022); | |
1127 | ||
6ada823a | 1128 | mkdir("/run/udev", 0755); |
912541b0 | 1129 | |
01ed0e23 | 1130 | dev_setup(NULL); |
912541b0 KS |
1131 | |
1132 | /* before opening new files, make sure std{in,out,err} fds are in a sane state */ | |
1133 | if (daemonize) { | |
1134 | int fd; | |
1135 | ||
1136 | fd = open("/dev/null", O_RDWR); | |
1137 | if (fd >= 0) { | |
1138 | if (write(STDOUT_FILENO, 0, 0) < 0) | |
1139 | dup2(fd, STDOUT_FILENO); | |
1140 | if (write(STDERR_FILENO, 0, 0) < 0) | |
1141 | dup2(fd, STDERR_FILENO); | |
1142 | if (fd > STDERR_FILENO) | |
1143 | close(fd); | |
1144 | } else { | |
1145 | fprintf(stderr, "cannot open /dev/null\n"); | |
9f6445e3 | 1146 | log_error("cannot open /dev/null"); |
912541b0 KS |
1147 | } |
1148 | } | |
1149 | ||
1150 | if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) { | |
c5315881 | 1151 | /* get control and netlink socket from systemd */ |
912541b0 KS |
1152 | udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl); |
1153 | if (udev_ctrl == NULL) { | |
baa30fbc | 1154 | log_error("error taking over udev control socket"); |
912541b0 KS |
1155 | rc = 1; |
1156 | goto exit; | |
1157 | } | |
1158 | ||
1159 | monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink); | |
1160 | if (monitor == NULL) { | |
9f6445e3 | 1161 | log_error("error taking over netlink socket"); |
912541b0 KS |
1162 | rc = 3; |
1163 | goto exit; | |
1164 | } | |
194bbe33 KS |
1165 | |
1166 | /* get our own cgroup, we regularly kill everything udev has left behind */ | |
7027ff61 | 1167 | if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0) |
194bbe33 | 1168 | udev_cgroup = NULL; |
912541b0 KS |
1169 | } else { |
1170 | /* open control and netlink socket */ | |
1171 | udev_ctrl = udev_ctrl_new(udev); | |
1172 | if (udev_ctrl == NULL) { | |
1173 | fprintf(stderr, "error initializing udev control socket"); | |
baa30fbc | 1174 | log_error("error initializing udev control socket"); |
912541b0 KS |
1175 | rc = 1; |
1176 | goto exit; | |
1177 | } | |
1178 | fd_ctrl = udev_ctrl_get_fd(udev_ctrl); | |
1179 | ||
1180 | monitor = udev_monitor_new_from_netlink(udev, "kernel"); | |
1181 | if (monitor == NULL) { | |
1182 | fprintf(stderr, "error initializing netlink socket\n"); | |
9f6445e3 | 1183 | log_error("error initializing netlink socket"); |
912541b0 KS |
1184 | rc = 3; |
1185 | goto exit; | |
1186 | } | |
1187 | fd_netlink = udev_monitor_get_fd(monitor); | |
1188 | } | |
1189 | ||
1190 | if (udev_monitor_enable_receiving(monitor) < 0) { | |
1191 | fprintf(stderr, "error binding netlink socket\n"); | |
9f6445e3 | 1192 | log_error("error binding netlink socket"); |
912541b0 KS |
1193 | rc = 3; |
1194 | goto exit; | |
1195 | } | |
1196 | ||
1197 | if (udev_ctrl_enable_receiving(udev_ctrl) < 0) { | |
1198 | fprintf(stderr, "error binding udev control socket\n"); | |
9f6445e3 | 1199 | log_error("error binding udev control socket"); |
912541b0 KS |
1200 | rc = 1; |
1201 | goto exit; | |
1202 | } | |
1203 | ||
9ea28c55 | 1204 | udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024); |
912541b0 KS |
1205 | |
1206 | if (daemonize) { | |
1207 | pid_t pid; | |
912541b0 KS |
1208 | |
1209 | pid = fork(); | |
1210 | switch (pid) { | |
1211 | case 0: | |
1212 | break; | |
1213 | case -1: | |
9f6445e3 | 1214 | log_error("fork of daemon failed: %m"); |
912541b0 KS |
1215 | rc = 4; |
1216 | goto exit; | |
1217 | default: | |
1218 | rc = EXIT_SUCCESS; | |
1219 | goto exit_daemonize; | |
1220 | } | |
1221 | ||
1222 | setsid(); | |
1223 | ||
574d5f2d | 1224 | write_string_file("/proc/self/oom_score_adj", "-1000"); |
912541b0 KS |
1225 | } else { |
1226 | sd_notify(1, "READY=1"); | |
1227 | } | |
1228 | ||
c6eefe36 | 1229 | print_kmsg("starting version " VERSION "\n"); |
912541b0 KS |
1230 | |
1231 | if (!debug) { | |
1232 | int fd; | |
1233 | ||
1234 | fd = open("/dev/null", O_RDWR); | |
1235 | if (fd >= 0) { | |
1236 | dup2(fd, STDIN_FILENO); | |
1237 | dup2(fd, STDOUT_FILENO); | |
1238 | dup2(fd, STDERR_FILENO); | |
1239 | close(fd); | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | fd_inotify = udev_watch_init(udev); | |
1244 | if (fd_inotify < 0) { | |
1245 | fprintf(stderr, "error initializing inotify\n"); | |
9f6445e3 | 1246 | log_error("error initializing inotify"); |
912541b0 KS |
1247 | rc = 4; |
1248 | goto exit; | |
1249 | } | |
1250 | udev_watch_restore(udev); | |
1251 | ||
1252 | /* block and listen to all signals on signalfd */ | |
1253 | sigfillset(&mask); | |
1254 | sigprocmask(SIG_SETMASK, &mask, &sigmask_orig); | |
1255 | fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); | |
1256 | if (fd_signal < 0) { | |
1257 | fprintf(stderr, "error creating signalfd\n"); | |
9f6445e3 | 1258 | log_error("error creating signalfd"); |
912541b0 KS |
1259 | rc = 5; |
1260 | goto exit; | |
1261 | } | |
1262 | ||
1263 | /* unnamed socket from workers to the main daemon */ | |
1264 | if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) { | |
1265 | fprintf(stderr, "error creating socketpair\n"); | |
9f6445e3 | 1266 | log_error("error creating socketpair"); |
912541b0 KS |
1267 | rc = 6; |
1268 | goto exit; | |
1269 | } | |
1270 | fd_worker = worker_watch[READ_END]; | |
1271 | ||
1272 | udev_builtin_init(udev); | |
1273 | ||
1274 | rules = udev_rules_new(udev, resolve_names); | |
1275 | if (rules == NULL) { | |
9f6445e3 | 1276 | log_error("error reading rules"); |
912541b0 KS |
1277 | goto exit; |
1278 | } | |
1279 | ||
29804cc1 | 1280 | memzero(&ep_ctrl, sizeof(struct epoll_event)); |
912541b0 KS |
1281 | ep_ctrl.events = EPOLLIN; |
1282 | ep_ctrl.data.fd = fd_ctrl; | |
1283 | ||
29804cc1 | 1284 | memzero(&ep_inotify, sizeof(struct epoll_event)); |
912541b0 KS |
1285 | ep_inotify.events = EPOLLIN; |
1286 | ep_inotify.data.fd = fd_inotify; | |
1287 | ||
29804cc1 | 1288 | memzero(&ep_signal, sizeof(struct epoll_event)); |
912541b0 KS |
1289 | ep_signal.events = EPOLLIN; |
1290 | ep_signal.data.fd = fd_signal; | |
1291 | ||
29804cc1 | 1292 | memzero(&ep_netlink, sizeof(struct epoll_event)); |
912541b0 KS |
1293 | ep_netlink.events = EPOLLIN; |
1294 | ep_netlink.data.fd = fd_netlink; | |
1295 | ||
29804cc1 | 1296 | memzero(&ep_worker, sizeof(struct epoll_event)); |
912541b0 KS |
1297 | ep_worker.events = EPOLLIN; |
1298 | ep_worker.data.fd = fd_worker; | |
1299 | ||
1300 | fd_ep = epoll_create1(EPOLL_CLOEXEC); | |
1301 | if (fd_ep < 0) { | |
9f6445e3 | 1302 | log_error("error creating epoll fd: %m"); |
912541b0 KS |
1303 | goto exit; |
1304 | } | |
1305 | if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 || | |
1306 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 || | |
1307 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 || | |
1308 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 || | |
1309 | epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) { | |
9f6445e3 | 1310 | log_error("fail to add fds to epoll: %m"); |
912541b0 KS |
1311 | goto exit; |
1312 | } | |
1313 | ||
912541b0 | 1314 | if (children_max <= 0) { |
8cc3f8c0 | 1315 | cpu_set_t cpu_set; |
912541b0 | 1316 | |
8cc3f8c0 HH |
1317 | children_max = 8; |
1318 | ||
1319 | if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) { | |
1320 | children_max += CPU_COUNT(&cpu_set) * 2; | |
1321 | } | |
912541b0 | 1322 | } |
9f6445e3 | 1323 | log_debug("set children_max to %u", children_max); |
912541b0 | 1324 | |
84b6ad70 TG |
1325 | rc = udev_rules_apply_static_dev_perms(rules); |
1326 | if (rc < 0) | |
9f6445e3 | 1327 | log_error("failed to apply permissions on static device nodes - %s", strerror(-rc)); |
912541b0 KS |
1328 | |
1329 | udev_list_node_init(&event_list); | |
1330 | udev_list_node_init(&worker_list); | |
1331 | ||
1332 | for (;;) { | |
40fe8b11 | 1333 | static usec_t last_usec; |
912541b0 KS |
1334 | struct epoll_event ev[8]; |
1335 | int fdcount; | |
1336 | int timeout; | |
1337 | bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl; | |
1338 | int i; | |
1339 | ||
1340 | if (udev_exit) { | |
1341 | /* close sources of new events and discard buffered events */ | |
1342 | if (fd_ctrl >= 0) { | |
1343 | epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL); | |
1344 | fd_ctrl = -1; | |
1345 | } | |
1346 | if (monitor != NULL) { | |
1347 | epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL); | |
1348 | udev_monitor_unref(monitor); | |
1349 | monitor = NULL; | |
1350 | } | |
1351 | if (fd_inotify >= 0) { | |
1352 | epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL); | |
1353 | close(fd_inotify); | |
1354 | fd_inotify = -1; | |
1355 | } | |
1356 | ||
1357 | /* discard queued events and kill workers */ | |
1358 | event_queue_cleanup(udev, EVENT_QUEUED); | |
194bbe33 | 1359 | worker_kill(udev); |
912541b0 KS |
1360 | |
1361 | /* exit after all has cleaned up */ | |
9ea28c55 | 1362 | if (udev_list_node_is_empty(&event_list) && children == 0) |
912541b0 KS |
1363 | break; |
1364 | ||
1365 | /* timeout at exit for workers to finish */ | |
9ea28c55 KS |
1366 | timeout = 30 * MSEC_PER_SEC; |
1367 | } else if (udev_list_node_is_empty(&event_list) && children == 0) { | |
e64fae55 | 1368 | /* we are idle */ |
912541b0 | 1369 | timeout = -1; |
194bbe33 KS |
1370 | |
1371 | /* cleanup possible left-over processes in our cgroup */ | |
1372 | if (udev_cgroup) | |
1373 | cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL); | |
e64fae55 KS |
1374 | } else { |
1375 | /* kill idle or hanging workers */ | |
9ea28c55 | 1376 | timeout = 3 * MSEC_PER_SEC; |
912541b0 | 1377 | } |
9ea28c55 KS |
1378 | |
1379 | /* tell settle that we are busy or idle */ | |
1380 | if (!udev_list_node_is_empty(&event_list)) { | |
1381 | int fd; | |
1382 | ||
1383 | fd = open("/run/udev/queue", O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444); | |
1384 | if (fd >= 0) | |
1385 | close(fd); | |
1386 | } else { | |
1387 | unlink("/run/udev/queue"); | |
1388 | } | |
1389 | ||
8fef0ff2 | 1390 | fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout); |
912541b0 KS |
1391 | if (fdcount < 0) |
1392 | continue; | |
1393 | ||
1394 | if (fdcount == 0) { | |
e64fae55 KS |
1395 | struct udev_list_node *loop; |
1396 | ||
1397 | /* timeout */ | |
912541b0 | 1398 | if (udev_exit) { |
9f6445e3 | 1399 | log_error("timeout, giving up waiting for workers to finish"); |
912541b0 KS |
1400 | break; |
1401 | } | |
1402 | ||
e64fae55 KS |
1403 | /* kill idle workers */ |
1404 | if (udev_list_node_is_empty(&event_list)) { | |
9f6445e3 | 1405 | log_debug("cleanup idle workers"); |
194bbe33 | 1406 | worker_kill(udev); |
e64fae55 KS |
1407 | } |
1408 | ||
1409 | /* check for hanging events */ | |
1410 | udev_list_node_foreach(loop, &worker_list) { | |
1411 | struct worker *worker = node_to_worker(loop); | |
1412 | ||
1413 | if (worker->state != WORKER_RUNNING) | |
1414 | continue; | |
1415 | ||
9ea28c55 | 1416 | if ((now(CLOCK_MONOTONIC) - worker->event_start_usec) > 30 * USEC_PER_SEC) { |
9f6445e3 | 1417 | log_error("worker [%u] %s timeout; kill it", worker->pid, |
e64fae55 KS |
1418 | worker->event ? worker->event->devpath : "<idle>"); |
1419 | kill(worker->pid, SIGKILL); | |
1420 | worker->state = WORKER_KILLED; | |
9ea28c55 | 1421 | |
e64fae55 KS |
1422 | /* drop reference taken for state 'running' */ |
1423 | worker_unref(worker); | |
1424 | if (worker->event) { | |
9ea28c55 | 1425 | log_error("seq %llu '%s' killed", udev_device_get_seqnum(worker->event->dev), worker->event->devpath); |
e64fae55 | 1426 | worker->event->exitcode = -64; |
9ea28c55 | 1427 | event_queue_delete(worker->event); |
e64fae55 KS |
1428 | worker->event = NULL; |
1429 | } | |
1430 | } | |
1431 | } | |
1432 | ||
912541b0 KS |
1433 | } |
1434 | ||
1435 | is_worker = is_signal = is_inotify = is_netlink = is_ctrl = false; | |
1436 | for (i = 0; i < fdcount; i++) { | |
1437 | if (ev[i].data.fd == fd_worker && ev[i].events & EPOLLIN) | |
1438 | is_worker = true; | |
1439 | else if (ev[i].data.fd == fd_netlink && ev[i].events & EPOLLIN) | |
1440 | is_netlink = true; | |
1441 | else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) | |
1442 | is_signal = true; | |
1443 | else if (ev[i].data.fd == fd_inotify && ev[i].events & EPOLLIN) | |
1444 | is_inotify = true; | |
1445 | else if (ev[i].data.fd == fd_ctrl && ev[i].events & EPOLLIN) | |
1446 | is_ctrl = true; | |
1447 | } | |
1448 | ||
1449 | /* check for changed config, every 3 seconds at most */ | |
9ea28c55 | 1450 | if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * USEC_PER_SEC) { |
6ada823a | 1451 | if (udev_rules_check_timestamp(rules)) |
912541b0 KS |
1452 | reload = true; |
1453 | if (udev_builtin_validate(udev)) | |
1454 | reload = true; | |
1455 | ||
40fe8b11 | 1456 | last_usec = now(CLOCK_MONOTONIC); |
912541b0 KS |
1457 | } |
1458 | ||
1459 | /* reload requested, HUP signal received, rules changed, builtin changed */ | |
1460 | if (reload) { | |
194bbe33 | 1461 | worker_kill(udev); |
912541b0 KS |
1462 | rules = udev_rules_unref(rules); |
1463 | udev_builtin_exit(udev); | |
7781e063 | 1464 | reload = false; |
912541b0 KS |
1465 | } |
1466 | ||
1467 | /* event has finished */ | |
1468 | if (is_worker) | |
1469 | worker_returned(fd_worker); | |
1470 | ||
1471 | if (is_netlink) { | |
1472 | struct udev_device *dev; | |
1473 | ||
1474 | dev = udev_monitor_receive_device(monitor); | |
1475 | if (dev != NULL) { | |
40fe8b11 | 1476 | udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC)); |
912541b0 KS |
1477 | if (event_queue_insert(dev) < 0) |
1478 | udev_device_unref(dev); | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | /* start new events */ | |
1483 | if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) { | |
7781e063 | 1484 | udev_builtin_init(udev); |
912541b0 KS |
1485 | if (rules == NULL) |
1486 | rules = udev_rules_new(udev, resolve_names); | |
1487 | if (rules != NULL) | |
1488 | event_queue_start(udev); | |
1489 | } | |
1490 | ||
1491 | if (is_signal) { | |
1492 | struct signalfd_siginfo fdsi; | |
1493 | ssize_t size; | |
1494 | ||
1495 | size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo)); | |
1496 | if (size == sizeof(struct signalfd_siginfo)) | |
1497 | handle_signal(udev, fdsi.ssi_signo); | |
1498 | } | |
1499 | ||
1500 | /* we are shutting down, the events below are not handled anymore */ | |
1501 | if (udev_exit) | |
1502 | continue; | |
1503 | ||
1504 | /* device node watch */ | |
1505 | if (is_inotify) | |
1506 | handle_inotify(udev); | |
1507 | ||
1508 | /* | |
1509 | * This needs to be after the inotify handling, to make sure, | |
1510 | * that the ping is send back after the possibly generated | |
1511 | * "change" events by the inotify device node watch. | |
1512 | * | |
1513 | * A single time we may receive a client connection which we need to | |
1514 | * keep open to block the client. It will be closed right before we | |
1515 | * exit. | |
1516 | */ | |
1517 | if (is_ctrl) | |
1518 | ctrl_conn = handle_ctrl_msg(udev_ctrl); | |
1519 | } | |
1520 | ||
1521 | rc = EXIT_SUCCESS; | |
53921bfa | 1522 | exit: |
912541b0 | 1523 | udev_ctrl_cleanup(udev_ctrl); |
9ea28c55 | 1524 | unlink("/run/udev/queue"); |
1f5a5100 | 1525 | exit_daemonize: |
912541b0 KS |
1526 | if (fd_ep >= 0) |
1527 | close(fd_ep); | |
1528 | worker_list_cleanup(udev); | |
1529 | event_queue_cleanup(udev, EVENT_UNDEF); | |
1530 | udev_rules_unref(rules); | |
1531 | udev_builtin_exit(udev); | |
1532 | if (fd_signal >= 0) | |
1533 | close(fd_signal); | |
1534 | if (worker_watch[READ_END] >= 0) | |
1535 | close(worker_watch[READ_END]); | |
1536 | if (worker_watch[WRITE_END] >= 0) | |
1537 | close(worker_watch[WRITE_END]); | |
1538 | udev_monitor_unref(monitor); | |
912541b0 KS |
1539 | udev_ctrl_connection_unref(ctrl_conn); |
1540 | udev_ctrl_unref(udev_ctrl); | |
e9a5ef7c | 1541 | label_finish(); |
912541b0 | 1542 | udev_unref(udev); |
baa30fbc | 1543 | log_close(); |
912541b0 | 1544 | return rc; |
7fafc032 | 1545 | } |