blob: 015f1ee782a6f9690f4068ceac8b60eb77508a2c [file] [log] [blame]
Denys Vlasenko71a5b672015-04-16 12:44:02 +02001/*
2 * Copyright 2015 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
Denys Vlasenko71a5b672015-04-16 12:44:02 +02006//config:config UEVENT
Denys Vlasenkob097a842018-12-28 03:20:17 +01007//config: bool "uevent (3.1 kb)"
Denys Vlasenko71a5b672015-04-16 12:44:02 +02008//config: default y
Denys Vlasenko71a5b672015-04-16 12:44:02 +02009//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020010//config: uevent is a netlink listener for kernel uevent notifications
11//config: sent via netlink. It is usually used for dynamic device creation.
Denys Vlasenko71a5b672015-04-16 12:44:02 +020012
13//applet:IF_UEVENT(APPLET(uevent, BB_DIR_SBIN, BB_SUID_DROP))
14
15//kbuild:lib-$(CONFIG_UEVENT) += uevent.o
16
17//usage:#define uevent_trivial_usage
18//usage: "[PROG [ARGS]]"
19//usage:#define uevent_full_usage "\n\n"
20//usage: "uevent runs PROG for every netlink notification."
21//usage: "\n""PROG's environment contains data passed from the kernel."
22//usage: "\n""Typical usage (daemon for dynamic device node creation):"
23//usage: "\n"" # uevent mdev & mdev -s"
24
25#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020026#include "common_bufsiz.h"
Denys Vlasenko71a5b672015-04-16 12:44:02 +020027#include <linux/netlink.h>
28
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020029#define env ((char **)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020030#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denys Vlasenko71a5b672015-04-16 12:44:02 +020031enum {
Denys Vlasenkoc4367d72017-08-16 11:45:09 +020032 MAX_ENV = COMMON_BUFSIZE / sizeof(char*) - 1,
Denys Vlasenkocb143b92020-11-22 16:22:16 +010033 // ^^^sizeof(env[0]) instead of sizeof(char*)
34 // makes gcc-6.3.0 emit "strict-aliasing" warning.
Denys Vlasenko71a5b672015-04-16 12:44:02 +020035
Denys Vlasenkocb143b92020-11-22 16:22:16 +010036 // socket receive buffer of 2MiB proved to be too small:
37 // http://lists.busybox.net/pipermail/busybox/2019-December/087665.html
38 // udevd seems to use a whooping 128MiB.
39 // The socket receive buffer size is just a resource limit.
40 // The buffers are allocated lazily so the memory is not wasted.
Denys Vlasenkoa569fd32020-11-22 13:34:07 +010041 KERN_RCVBUF = 128 * 1024 * 1024,
Denys Vlasenkocb143b92020-11-22 16:22:16 +010042
43 // Might be made smaller: the kernel v5.4 passes up to 32 environment
44 // variables with a total of 2kb on each event.
45 // On top of that the action string and device path are added.
Denys Vlasenkoa569fd32020-11-22 13:34:07 +010046 USER_RCVBUF = 16 * 1024,
47};
Denys Vlasenko71a5b672015-04-16 12:44:02 +020048
49int uevent_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
50int uevent_main(int argc UNUSED_PARAM, char **argv)
51{
Denys Vlasenko71a5b672015-04-16 12:44:02 +020052 int fd;
53
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020054 INIT_G();
55
Denys Vlasenko71a5b672015-04-16 12:44:02 +020056 argv++;
57
Denys Vlasenko45e39672019-06-03 14:16:52 +020058 // Subscribe for UEVENT kernel messages.
Denys Vlasenko71a5b672015-04-16 12:44:02 +020059 // Without a sufficiently big RCVBUF, a ton of simultaneous events
60 // can trigger ENOBUFS on read, which is unrecoverable.
61 // Reproducer:
62 // uevent mdev &
63 // find /sys -name uevent -exec sh -c 'echo add >"{}"' ';'
Denys Vlasenkocb143b92020-11-22 16:22:16 +010064 reopen:
Denys Vlasenkoa569fd32020-11-22 13:34:07 +010065 fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, /*groups:*/ 1 << 0, KERN_RCVBUF);
Denys Vlasenko71a5b672015-04-16 12:44:02 +020066
67 for (;;) {
68 char *netbuf;
69 char *s, *end;
70 ssize_t len;
71 int idx;
72
73 // In many cases, a system sits for *days* waiting
74 // for a new uevent notification to come in.
75 // We use a fresh mmap so that buffer is not allocated
76 // until kernel actually starts filling it.
Denys Vlasenkofd3c5122020-12-14 18:25:28 +010077 netbuf = xmmap_anon(USER_RCVBUF);
Denys Vlasenko71a5b672015-04-16 12:44:02 +020078
79 // Here we block, possibly for a very long time
Denys Vlasenkoa569fd32020-11-22 13:34:07 +010080 len = safe_read(fd, netbuf, USER_RCVBUF - 1);
Denys Vlasenkocb143b92020-11-22 16:22:16 +010081 if (len < 0) {
82 if (errno == ENOBUFS) {
83 // Ran out of socket receive buffer
84 bb_simple_error_msg("uevent overrun");
85 close(fd);
86 munmap(netbuf, USER_RCVBUF);
87 goto reopen;
88 }
James Byrne69374872019-07-02 11:35:03 +020089 bb_simple_perror_msg_and_die("read");
Denys Vlasenkocb143b92020-11-22 16:22:16 +010090 }
Denys Vlasenko71a5b672015-04-16 12:44:02 +020091 end = netbuf + len;
92 *end = '\0';
93
94 // Each netlink message starts with "ACTION@/path"
95 // (which we currently ignore),
96 // followed by environment variables.
97 if (!argv[0])
98 putchar('\n');
99 idx = 0;
100 s = netbuf;
101 while (s < end) {
102 if (!argv[0])
103 puts(s);
104 if (strchr(s, '=') && idx < MAX_ENV)
105 env[idx++] = s;
106 s += strlen(s) + 1;
107 }
108 env[idx] = NULL;
109
Denys Vlasenko45e39672019-06-03 14:16:52 +0200110 if (argv[0]) {
111 idx = 0;
112 while (env[idx])
113 putenv(env[idx++]);
Denys Vlasenko71a5b672015-04-16 12:44:02 +0200114 spawn_and_wait(argv);
Denys Vlasenko45e39672019-06-03 14:16:52 +0200115 idx = 0;
116 while (env[idx])
117 bb_unsetenv(env[idx++]);
118 }
Denys Vlasenkoa569fd32020-11-22 13:34:07 +0100119 munmap(netbuf, USER_RCVBUF);
Denys Vlasenko71a5b672015-04-16 12:44:02 +0200120 }
121
122 return 0; // not reached
123}