2015-04-16 16:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copyright 2015 Denys Vlasenko
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
//config:config UEVENT
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "uevent (3.1 kb)"
|
2015-04-16 16:14:02 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: uevent is a netlink listener for kernel uevent notifications
|
|
|
|
//config: sent via netlink. It is usually used for dynamic device creation.
|
2015-04-16 16:14:02 +05:30
|
|
|
|
|
|
|
//applet:IF_UEVENT(APPLET(uevent, BB_DIR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_UEVENT) += uevent.o
|
|
|
|
|
|
|
|
//usage:#define uevent_trivial_usage
|
2020-12-18 08:42:51 +05:30
|
|
|
//usage: "[PROG ARGS]"
|
2015-04-16 16:14:02 +05:30
|
|
|
//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"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2015-04-16 16:14:02 +05:30
|
|
|
#include <linux/netlink.h>
|
|
|
|
|
2016-04-21 19:56:30 +05:30
|
|
|
#define env ((char **)bb_common_bufsiz1)
|
2016-04-21 21:48:48 +05:30
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
2015-04-16 16:14:02 +05:30
|
|
|
enum {
|
2017-08-16 15:15:09 +05:30
|
|
|
MAX_ENV = COMMON_BUFSIZE / sizeof(char*) - 1,
|
2020-11-22 20:52:16 +05:30
|
|
|
// ^^^sizeof(env[0]) instead of sizeof(char*)
|
|
|
|
// makes gcc-6.3.0 emit "strict-aliasing" warning.
|
|
|
|
|
|
|
|
// socket receive buffer of 2MiB proved to be too small:
|
|
|
|
// http://lists.busybox.net/pipermail/busybox/2019-December/087665.html
|
|
|
|
// udevd seems to use a whooping 128MiB.
|
|
|
|
// The socket receive buffer size is just a resource limit.
|
|
|
|
// The buffers are allocated lazily so the memory is not wasted.
|
2020-11-22 18:04:07 +05:30
|
|
|
KERN_RCVBUF = 128 * 1024 * 1024,
|
2020-11-22 20:52:16 +05:30
|
|
|
|
|
|
|
// Might be made smaller: the kernel v5.4 passes up to 32 environment
|
|
|
|
// variables with a total of 2kb on each event.
|
|
|
|
// On top of that the action string and device path are added.
|
2020-11-22 18:04:07 +05:30
|
|
|
USER_RCVBUF = 16 * 1024,
|
|
|
|
};
|
2015-04-16 16:14:02 +05:30
|
|
|
|
|
|
|
int uevent_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int uevent_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2016-04-21 21:48:48 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2015-04-16 16:14:02 +05:30
|
|
|
argv++;
|
|
|
|
|
2019-06-03 17:46:52 +05:30
|
|
|
// Subscribe for UEVENT kernel messages.
|
2015-04-16 16:14:02 +05:30
|
|
|
// 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 >"{}"' ';'
|
2020-11-22 20:52:16 +05:30
|
|
|
reopen:
|
2020-11-22 18:04:07 +05:30
|
|
|
fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, /*groups:*/ 1 << 0, KERN_RCVBUF);
|
2015-04-16 16:14:02 +05:30
|
|
|
|
|
|
|
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.
|
2020-12-14 22:55:28 +05:30
|
|
|
netbuf = xmmap_anon(USER_RCVBUF);
|
2015-04-16 16:14:02 +05:30
|
|
|
|
|
|
|
// Here we block, possibly for a very long time
|
2020-11-22 18:04:07 +05:30
|
|
|
len = safe_read(fd, netbuf, USER_RCVBUF - 1);
|
2020-11-22 20:52:16 +05:30
|
|
|
if (len < 0) {
|
|
|
|
if (errno == ENOBUFS) {
|
|
|
|
// Ran out of socket receive buffer
|
|
|
|
bb_simple_error_msg("uevent overrun");
|
|
|
|
close(fd);
|
|
|
|
munmap(netbuf, USER_RCVBUF);
|
|
|
|
goto reopen;
|
|
|
|
}
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg_and_die("read");
|
2020-11-22 20:52:16 +05:30
|
|
|
}
|
2015-04-16 16:14:02 +05:30
|
|
|
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;
|
|
|
|
|
2019-06-03 17:46:52 +05:30
|
|
|
if (argv[0]) {
|
|
|
|
idx = 0;
|
|
|
|
while (env[idx])
|
|
|
|
putenv(env[idx++]);
|
2015-04-16 16:14:02 +05:30
|
|
|
spawn_and_wait(argv);
|
2019-06-03 17:46:52 +05:30
|
|
|
idx = 0;
|
|
|
|
while (env[idx])
|
|
|
|
bb_unsetenv(env[idx++]);
|
|
|
|
}
|
2020-11-22 18:04:07 +05:30
|
|
|
munmap(netbuf, USER_RCVBUF);
|
2015-04-16 16:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // not reached
|
|
|
|
}
|