mirror of
https://git.busybox.net/busybox.git
synced 2024-11-27 23:53:29 +08:00
c52cbea2bb
function old new delta setsockopt_int - 23 +23 do_load 918 934 +16 setsockopt_SOL_SOCKET_int - 14 +14 setsockopt_keepalive - 10 +10 setsockopt_SOL_SOCKET_1 - 10 +10 buffer_fill_and_print 169 178 +9 setsockopt_1 - 8 +8 nfsmount 3560 3566 +6 redirect 1277 1282 +5 tcpudpsvd_main 1782 1786 +4 d6_send_kernel_packet 272 275 +3 i2cget_main 380 382 +2 ed_main 2544 2545 +1 scan_recursive 380 378 -2 nbdclient_main 492 490 -2 hash_find 235 233 -2 cmdputs 334 332 -2 parse_command 1443 1440 -3 static.two 4 - -4 ntpd_main 1039 1035 -4 const_int_1 4 - -4 const_IPTOS_LOWDELAY 4 - -4 RCVBUF 4 - -4 ntp_init 474 469 -5 change_listen_mode 316 310 -6 uevent_main 416 409 -7 arping_main 1697 1690 -7 telnet_main 1612 1603 -9 socket_want_pktinfo 42 33 -9 setsockopt_reuseaddr 21 10 -11 setsockopt_broadcast 21 10 -11 httpd_main 772 757 -15 get_remote_transfer_fd 109 94 -15 make_new_session 503 487 -16 ftpd_main 2177 2160 -17 read_bunzip 1896 1866 -30 common_traceroute_main 4099 4058 -41 common_ping_main 1836 1783 -53 ------------------------------------------------------------------------------ (add/remove: 5/4 grow/shrink: 8/21 up/down: 111/-283) Total: -172 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
128 lines
3.2 KiB
C
128 lines
3.2 KiB
C
/*
|
|
* Copyright 2015 Denys Vlasenko
|
|
*
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
*/
|
|
|
|
//config:config UEVENT
|
|
//config: bool "uevent"
|
|
//config: default y
|
|
//config: select PLATFORM_LINUX
|
|
//config: help
|
|
//config: uevent is a netlink listener for kernel uevent notifications
|
|
//config: sent via netlink. It is usually used for dynamic device creation.
|
|
|
|
//applet:IF_UEVENT(APPLET(uevent, BB_DIR_SBIN, BB_SUID_DROP))
|
|
|
|
//kbuild:lib-$(CONFIG_UEVENT) += uevent.o
|
|
|
|
//usage:#define uevent_trivial_usage
|
|
//usage: "[PROG [ARGS]]"
|
|
//usage:#define uevent_full_usage "\n\n"
|
|
//usage: "uevent runs PROG for every netlink notification."
|
|
//usage: "\n""PROG's environment contains data passed from the kernel."
|
|
//usage: "\n""Typical usage (daemon for dynamic device node creation):"
|
|
//usage: "\n"" # uevent mdev & mdev -s"
|
|
|
|
#include "libbb.h"
|
|
#include <linux/netlink.h>
|
|
|
|
#define BUFFER_SIZE 16*1024
|
|
|
|
#define env ((char **)&bb_common_bufsiz1)
|
|
enum {
|
|
MAX_ENV = COMMON_BUFSIZE / sizeof(env[0]) - 1,
|
|
};
|
|
|
|
#ifndef SO_RCVBUFFORCE
|
|
#define SO_RCVBUFFORCE 33
|
|
#endif
|
|
enum { RCVBUF = 2 * 1024 * 1024 };
|
|
|
|
int uevent_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
int uevent_main(int argc UNUSED_PARAM, char **argv)
|
|
{
|
|
struct sockaddr_nl sa;
|
|
int fd;
|
|
|
|
argv++;
|
|
|
|
// Subscribe for UEVENT kernel messages
|
|
sa.nl_family = AF_NETLINK;
|
|
sa.nl_pad = 0;
|
|
sa.nl_pid = getpid();
|
|
sa.nl_groups = 1 << 0;
|
|
fd = xsocket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
|
|
xbind(fd, (struct sockaddr *) &sa, sizeof(sa));
|
|
close_on_exec_on(fd);
|
|
|
|
// Without a sufficiently big RCVBUF, a ton of simultaneous events
|
|
// can trigger ENOBUFS on read, which is unrecoverable.
|
|
// Reproducer:
|
|
// uevent mdev &
|
|
// find /sys -name uevent -exec sh -c 'echo add >"{}"' ';'
|
|
//
|
|
// SO_RCVBUFFORCE (root only) can go above net.core.rmem_max sysctl
|
|
setsockopt_SOL_SOCKET_int(fd, SO_RCVBUF, RCVBUF);
|
|
setsockopt_SOL_SOCKET_int(fd, SO_RCVBUFFORCE, RCVBUF);
|
|
if (0) {
|
|
int z;
|
|
socklen_t zl = sizeof(z);
|
|
getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &z, &zl);
|
|
bb_error_msg("SO_RCVBUF:%d", z);
|
|
}
|
|
|
|
for (;;) {
|
|
char *netbuf;
|
|
char *s, *end;
|
|
ssize_t len;
|
|
int idx;
|
|
|
|
// In many cases, a system sits for *days* waiting
|
|
// for a new uevent notification to come in.
|
|
// We use a fresh mmap so that buffer is not allocated
|
|
// until kernel actually starts filling it.
|
|
netbuf = mmap(NULL, BUFFER_SIZE,
|
|
PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANON,
|
|
/* ignored: */ -1, 0);
|
|
if (netbuf == MAP_FAILED)
|
|
bb_perror_msg_and_die("mmap");
|
|
|
|
// Here we block, possibly for a very long time
|
|
len = safe_read(fd, netbuf, BUFFER_SIZE - 1);
|
|
if (len < 0)
|
|
bb_perror_msg_and_die("read");
|
|
end = netbuf + len;
|
|
*end = '\0';
|
|
|
|
// Each netlink message starts with "ACTION@/path"
|
|
// (which we currently ignore),
|
|
// followed by environment variables.
|
|
if (!argv[0])
|
|
putchar('\n');
|
|
idx = 0;
|
|
s = netbuf;
|
|
while (s < end) {
|
|
if (!argv[0])
|
|
puts(s);
|
|
if (strchr(s, '=') && idx < MAX_ENV)
|
|
env[idx++] = s;
|
|
s += strlen(s) + 1;
|
|
}
|
|
env[idx] = NULL;
|
|
|
|
idx = 0;
|
|
while (env[idx])
|
|
putenv(env[idx++]);
|
|
if (argv[0])
|
|
spawn_and_wait(argv);
|
|
idx = 0;
|
|
while (env[idx])
|
|
bb_unsetenv(env[idx++]);
|
|
munmap(netbuf, BUFFER_SIZE);
|
|
}
|
|
|
|
return 0; // not reached
|
|
}
|