blob: c22216a8a11717913980c3755646b3a5b62c06fa
1 | /* |
2 | * Copyright 2015 Denys Vlasenko |
3 | * |
4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
5 | */ |
6 | //config:config UEVENT |
7 | //config: bool "uevent" |
8 | //config: default y |
9 | //config: select PLATFORM_LINUX |
10 | //config: help |
11 | //config: uevent is a netlink listener for kernel uevent notifications |
12 | //config: sent via netlink. It is usually used for dynamic device creation. |
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" |
27 | #include "common_bufsiz.h" |
28 | #include <linux/netlink.h> |
29 | |
30 | #define BUFFER_SIZE 16*1024 |
31 | |
32 | #define env ((char **)bb_common_bufsiz1) |
33 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
34 | enum { |
35 | MAX_ENV = COMMON_BUFSIZE / sizeof(env[0]) - 1, |
36 | }; |
37 | |
38 | #ifndef SO_RCVBUFFORCE |
39 | #define SO_RCVBUFFORCE 33 |
40 | #endif |
41 | enum { RCVBUF = 2 * 1024 * 1024 }; |
42 | |
43 | int uevent_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
44 | int uevent_main(int argc UNUSED_PARAM, char **argv) |
45 | { |
46 | struct sockaddr_nl sa; |
47 | int fd; |
48 | |
49 | INIT_G(); |
50 | |
51 | argv++; |
52 | |
53 | // Subscribe for UEVENT kernel messages |
54 | sa.nl_family = AF_NETLINK; |
55 | sa.nl_pad = 0; |
56 | sa.nl_pid = getpid(); |
57 | sa.nl_groups = 1 << 0; |
58 | fd = xsocket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); |
59 | xbind(fd, (struct sockaddr *) &sa, sizeof(sa)); |
60 | close_on_exec_on(fd); |
61 | |
62 | // Without a sufficiently big RCVBUF, a ton of simultaneous events |
63 | // can trigger ENOBUFS on read, which is unrecoverable. |
64 | // Reproducer: |
65 | // uevent mdev & |
66 | // find /sys -name uevent -exec sh -c 'echo add >"{}"' ';' |
67 | // |
68 | // SO_RCVBUFFORCE (root only) can go above net.core.rmem_max sysctl |
69 | setsockopt_SOL_SOCKET_int(fd, SO_RCVBUF, RCVBUF); |
70 | setsockopt_SOL_SOCKET_int(fd, SO_RCVBUFFORCE, RCVBUF); |
71 | if (0) { |
72 | int z; |
73 | socklen_t zl = sizeof(z); |
74 | getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &z, &zl); |
75 | bb_error_msg("SO_RCVBUF:%d", z); |
76 | } |
77 | |
78 | for (;;) { |
79 | char *netbuf; |
80 | char *s, *end; |
81 | ssize_t len; |
82 | int idx; |
83 | |
84 | // In many cases, a system sits for *days* waiting |
85 | // for a new uevent notification to come in. |
86 | // We use a fresh mmap so that buffer is not allocated |
87 | // until kernel actually starts filling it. |
88 | netbuf = mmap(NULL, BUFFER_SIZE, |
89 | PROT_READ | PROT_WRITE, |
90 | MAP_PRIVATE | MAP_ANON, |
91 | /* ignored: */ -1, 0); |
92 | if (netbuf == MAP_FAILED) |
93 | bb_perror_msg_and_die("mmap"); |
94 | |
95 | // Here we block, possibly for a very long time |
96 | len = safe_read(fd, netbuf, BUFFER_SIZE - 1); |
97 | if (len < 0) |
98 | bb_perror_msg_and_die("read"); |
99 | end = netbuf + len; |
100 | *end = '\0'; |
101 | |
102 | // Each netlink message starts with "ACTION@/path" |
103 | // (which we currently ignore), |
104 | // followed by environment variables. |
105 | if (!argv[0]) |
106 | putchar('\n'); |
107 | idx = 0; |
108 | s = netbuf; |
109 | while (s < end) { |
110 | if (!argv[0]) |
111 | puts(s); |
112 | if (strchr(s, '=') && idx < MAX_ENV) |
113 | env[idx++] = s; |
114 | s += strlen(s) + 1; |
115 | } |
116 | env[idx] = NULL; |
117 | |
118 | idx = 0; |
119 | while (env[idx]) |
120 | putenv(env[idx++]); |
121 | if (argv[0]) |
122 | spawn_and_wait(argv); |
123 | idx = 0; |
124 | while (env[idx]) |
125 | bb_unsetenv(env[idx++]); |
126 | munmap(netbuf, BUFFER_SIZE); |
127 | } |
128 | |
129 | return 0; // not reached |
130 | } |
131 |