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