Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Denys Vlasenko |
| 3 | * |
| 4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 5 | */ |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 6 | //config:config UEVENT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 7 | //config: bool "uevent (3.1 kb)" |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 8 | //config: default y |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 9 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 10 | //config: uevent is a netlink listener for kernel uevent notifications |
| 11 | //config: sent via netlink. It is usually used for dynamic device creation. |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 12 | |
| 13 | //applet:IF_UEVENT(APPLET(uevent, BB_DIR_SBIN, BB_SUID_DROP)) |
| 14 | |
| 15 | //kbuild:lib-$(CONFIG_UEVENT) += uevent.o |
| 16 | |
| 17 | //usage:#define uevent_trivial_usage |
| 18 | //usage: "[PROG [ARGS]]" |
| 19 | //usage:#define uevent_full_usage "\n\n" |
| 20 | //usage: "uevent runs PROG for every netlink notification." |
| 21 | //usage: "\n""PROG's environment contains data passed from the kernel." |
| 22 | //usage: "\n""Typical usage (daemon for dynamic device node creation):" |
| 23 | //usage: "\n"" # uevent mdev & mdev -s" |
| 24 | |
| 25 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 26 | #include "common_bufsiz.h" |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 27 | #include <linux/netlink.h> |
| 28 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 29 | #define env ((char **)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 30 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 31 | enum { |
Denys Vlasenko | c4367d7 | 2017-08-16 11:45:09 +0200 | [diff] [blame] | 32 | MAX_ENV = COMMON_BUFSIZE / sizeof(char*) - 1, |
Denys Vlasenko | cb143b9 | 2020-11-22 16:22:16 +0100 | [diff] [blame] | 33 | // ^^^sizeof(env[0]) instead of sizeof(char*) |
| 34 | // makes gcc-6.3.0 emit "strict-aliasing" warning. |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 35 | |
Denys Vlasenko | cb143b9 | 2020-11-22 16:22:16 +0100 | [diff] [blame] | 36 | // socket receive buffer of 2MiB proved to be too small: |
| 37 | // http://lists.busybox.net/pipermail/busybox/2019-December/087665.html |
| 38 | // udevd seems to use a whooping 128MiB. |
| 39 | // The socket receive buffer size is just a resource limit. |
| 40 | // The buffers are allocated lazily so the memory is not wasted. |
Denys Vlasenko | a569fd3 | 2020-11-22 13:34:07 +0100 | [diff] [blame] | 41 | KERN_RCVBUF = 128 * 1024 * 1024, |
Denys Vlasenko | cb143b9 | 2020-11-22 16:22:16 +0100 | [diff] [blame] | 42 | |
| 43 | // Might be made smaller: the kernel v5.4 passes up to 32 environment |
| 44 | // variables with a total of 2kb on each event. |
| 45 | // On top of that the action string and device path are added. |
Denys Vlasenko | a569fd3 | 2020-11-22 13:34:07 +0100 | [diff] [blame] | 46 | USER_RCVBUF = 16 * 1024, |
| 47 | }; |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 48 | |
| 49 | int uevent_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 50 | int uevent_main(int argc UNUSED_PARAM, char **argv) |
| 51 | { |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 52 | int fd; |
| 53 | |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 54 | INIT_G(); |
| 55 | |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 56 | argv++; |
| 57 | |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 58 | // Subscribe for UEVENT kernel messages. |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 59 | // Without a sufficiently big RCVBUF, a ton of simultaneous events |
| 60 | // can trigger ENOBUFS on read, which is unrecoverable. |
| 61 | // Reproducer: |
| 62 | // uevent mdev & |
| 63 | // find /sys -name uevent -exec sh -c 'echo add >"{}"' ';' |
Denys Vlasenko | cb143b9 | 2020-11-22 16:22:16 +0100 | [diff] [blame] | 64 | reopen: |
Denys Vlasenko | a569fd3 | 2020-11-22 13:34:07 +0100 | [diff] [blame] | 65 | fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, /*groups:*/ 1 << 0, KERN_RCVBUF); |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 66 | |
| 67 | for (;;) { |
| 68 | char *netbuf; |
| 69 | char *s, *end; |
| 70 | ssize_t len; |
| 71 | int idx; |
| 72 | |
| 73 | // In many cases, a system sits for *days* waiting |
| 74 | // for a new uevent notification to come in. |
| 75 | // We use a fresh mmap so that buffer is not allocated |
| 76 | // until kernel actually starts filling it. |
Denys Vlasenko | fd3c512 | 2020-12-14 18:25:28 +0100 | [diff] [blame] | 77 | netbuf = xmmap_anon(USER_RCVBUF); |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 78 | |
| 79 | // Here we block, possibly for a very long time |
Denys Vlasenko | a569fd3 | 2020-11-22 13:34:07 +0100 | [diff] [blame] | 80 | len = safe_read(fd, netbuf, USER_RCVBUF - 1); |
Denys Vlasenko | cb143b9 | 2020-11-22 16:22:16 +0100 | [diff] [blame] | 81 | if (len < 0) { |
| 82 | if (errno == ENOBUFS) { |
| 83 | // Ran out of socket receive buffer |
| 84 | bb_simple_error_msg("uevent overrun"); |
| 85 | close(fd); |
| 86 | munmap(netbuf, USER_RCVBUF); |
| 87 | goto reopen; |
| 88 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 89 | bb_simple_perror_msg_and_die("read"); |
Denys Vlasenko | cb143b9 | 2020-11-22 16:22:16 +0100 | [diff] [blame] | 90 | } |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 91 | end = netbuf + len; |
| 92 | *end = '\0'; |
| 93 | |
| 94 | // Each netlink message starts with "ACTION@/path" |
| 95 | // (which we currently ignore), |
| 96 | // followed by environment variables. |
| 97 | if (!argv[0]) |
| 98 | putchar('\n'); |
| 99 | idx = 0; |
| 100 | s = netbuf; |
| 101 | while (s < end) { |
| 102 | if (!argv[0]) |
| 103 | puts(s); |
| 104 | if (strchr(s, '=') && idx < MAX_ENV) |
| 105 | env[idx++] = s; |
| 106 | s += strlen(s) + 1; |
| 107 | } |
| 108 | env[idx] = NULL; |
| 109 | |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 110 | if (argv[0]) { |
| 111 | idx = 0; |
| 112 | while (env[idx]) |
| 113 | putenv(env[idx++]); |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 114 | spawn_and_wait(argv); |
Denys Vlasenko | 45e3967 | 2019-06-03 14:16:52 +0200 | [diff] [blame] | 115 | idx = 0; |
| 116 | while (env[idx]) |
| 117 | bb_unsetenv(env[idx++]); |
| 118 | } |
Denys Vlasenko | a569fd3 | 2020-11-22 13:34:07 +0100 | [diff] [blame] | 119 | munmap(netbuf, USER_RCVBUF); |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return 0; // not reached |
| 123 | } |