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 | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 7 | //config: bool "uevent (3.2 kb)" |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 8 | //config: default y |
| 9 | //config: select PLATFORM_LINUX |
| 10 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 11 | //config: uevent is a netlink listener for kernel uevent notifications |
| 12 | //config: sent via netlink. It is usually used for dynamic device creation. |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 13 | |
| 14 | //applet:IF_UEVENT(APPLET(uevent, BB_DIR_SBIN, BB_SUID_DROP)) |
| 15 | |
| 16 | //kbuild:lib-$(CONFIG_UEVENT) += uevent.o |
| 17 | |
| 18 | //usage:#define uevent_trivial_usage |
| 19 | //usage: "[PROG [ARGS]]" |
| 20 | //usage:#define uevent_full_usage "\n\n" |
| 21 | //usage: "uevent runs PROG for every netlink notification." |
| 22 | //usage: "\n""PROG's environment contains data passed from the kernel." |
| 23 | //usage: "\n""Typical usage (daemon for dynamic device node creation):" |
| 24 | //usage: "\n"" # uevent mdev & mdev -s" |
| 25 | |
| 26 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 27 | #include "common_bufsiz.h" |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 28 | #include <linux/netlink.h> |
| 29 | |
| 30 | #define BUFFER_SIZE 16*1024 |
| 31 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 32 | #define env ((char **)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 33 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 34 | enum { |
Denys Vlasenko | c4367d7 | 2017-08-16 11:45:09 +0200 | [diff] [blame] | 35 | MAX_ENV = COMMON_BUFSIZE / sizeof(char*) - 1, |
| 36 | /* sizeof(env[0]) instead of sizeof(char*) |
| 37 | * makes gcc-6.3.0 emit "strict-aliasing" warning. |
| 38 | */ |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #ifndef SO_RCVBUFFORCE |
| 42 | #define SO_RCVBUFFORCE 33 |
| 43 | #endif |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 44 | enum { RCVBUF = 2 * 1024 * 1024 }; |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 45 | |
| 46 | int uevent_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 47 | int uevent_main(int argc UNUSED_PARAM, char **argv) |
| 48 | { |
| 49 | struct sockaddr_nl sa; |
| 50 | int fd; |
| 51 | |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 52 | INIT_G(); |
| 53 | |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 54 | argv++; |
| 55 | |
| 56 | // Subscribe for UEVENT kernel messages |
| 57 | sa.nl_family = AF_NETLINK; |
| 58 | sa.nl_pad = 0; |
| 59 | sa.nl_pid = getpid(); |
| 60 | sa.nl_groups = 1 << 0; |
| 61 | fd = xsocket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); |
| 62 | xbind(fd, (struct sockaddr *) &sa, sizeof(sa)); |
| 63 | close_on_exec_on(fd); |
| 64 | |
| 65 | // Without a sufficiently big RCVBUF, a ton of simultaneous events |
| 66 | // can trigger ENOBUFS on read, which is unrecoverable. |
| 67 | // Reproducer: |
| 68 | // uevent mdev & |
| 69 | // find /sys -name uevent -exec sh -c 'echo add >"{}"' ';' |
| 70 | // |
| 71 | // SO_RCVBUFFORCE (root only) can go above net.core.rmem_max sysctl |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 72 | setsockopt_SOL_SOCKET_int(fd, SO_RCVBUF, RCVBUF); |
| 73 | setsockopt_SOL_SOCKET_int(fd, SO_RCVBUFFORCE, RCVBUF); |
Denys Vlasenko | 71a5b67 | 2015-04-16 12:44:02 +0200 | [diff] [blame] | 74 | if (0) { |
| 75 | int z; |
| 76 | socklen_t zl = sizeof(z); |
| 77 | getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &z, &zl); |
| 78 | bb_error_msg("SO_RCVBUF:%d", z); |
| 79 | } |
| 80 | |
| 81 | for (;;) { |
| 82 | char *netbuf; |
| 83 | char *s, *end; |
| 84 | ssize_t len; |
| 85 | int idx; |
| 86 | |
| 87 | // In many cases, a system sits for *days* waiting |
| 88 | // for a new uevent notification to come in. |
| 89 | // We use a fresh mmap so that buffer is not allocated |
| 90 | // until kernel actually starts filling it. |
| 91 | netbuf = mmap(NULL, BUFFER_SIZE, |
| 92 | PROT_READ | PROT_WRITE, |
| 93 | MAP_PRIVATE | MAP_ANON, |
| 94 | /* ignored: */ -1, 0); |
| 95 | if (netbuf == MAP_FAILED) |
| 96 | bb_perror_msg_and_die("mmap"); |
| 97 | |
| 98 | // Here we block, possibly for a very long time |
| 99 | len = safe_read(fd, netbuf, BUFFER_SIZE - 1); |
| 100 | if (len < 0) |
| 101 | bb_perror_msg_and_die("read"); |
| 102 | end = netbuf + len; |
| 103 | *end = '\0'; |
| 104 | |
| 105 | // Each netlink message starts with "ACTION@/path" |
| 106 | // (which we currently ignore), |
| 107 | // followed by environment variables. |
| 108 | if (!argv[0]) |
| 109 | putchar('\n'); |
| 110 | idx = 0; |
| 111 | s = netbuf; |
| 112 | while (s < end) { |
| 113 | if (!argv[0]) |
| 114 | puts(s); |
| 115 | if (strchr(s, '=') && idx < MAX_ENV) |
| 116 | env[idx++] = s; |
| 117 | s += strlen(s) + 1; |
| 118 | } |
| 119 | env[idx] = NULL; |
| 120 | |
| 121 | idx = 0; |
| 122 | while (env[idx]) |
| 123 | putenv(env[idx++]); |
| 124 | if (argv[0]) |
| 125 | spawn_and_wait(argv); |
| 126 | idx = 0; |
| 127 | while (env[idx]) |
| 128 | bb_unsetenv(env[idx++]); |
| 129 | munmap(netbuf, BUFFER_SIZE); |
| 130 | } |
| 131 | |
| 132 | return 0; // not reached |
| 133 | } |