busybox/util-linux/uevent.c

117 lines
3.0 KiB
C
Raw Normal View History

/*
* Copyright 2015 Denys Vlasenko
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
//config:config UEVENT
//config: bool "uevent (3.1 kb)"
//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 "common_bufsiz.h"
#include <linux/netlink.h>
#define BUFFER_SIZE 16*1024
#define env ((char **)bb_common_bufsiz1)
#define INIT_G() do { setup_common_bufsiz(); } while (0)
enum {
MAX_ENV = COMMON_BUFSIZE / sizeof(char*) - 1,
/* sizeof(env[0]) instead of sizeof(char*)
* makes gcc-6.3.0 emit "strict-aliasing" warning.
*/
};
#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)
{
int fd;
INIT_G();
argv++;
// Subscribe for UEVENT kernel messages.
// 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 >"{}"' ';'
fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, /*groups:*/ 1 << 0, RCVBUF);
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)
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("mmap");
// Here we block, possibly for a very long time
len = safe_read(fd, netbuf, BUFFER_SIZE - 1);
if (len < 0)
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");
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;
if (argv[0]) {
idx = 0;
while (env[idx])
putenv(env[idx++]);
spawn_and_wait(argv);
idx = 0;
while (env[idx])
bb_unsetenv(env[idx++]);
}
munmap(netbuf, BUFFER_SIZE);
}
return 0; // not reached
}