uevent.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2015 Denys Vlasenko
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. //config:config UEVENT
  7. //config: bool "uevent (3.5 kb)"
  8. //config: default y
  9. //config: help
  10. //config: uevent is a netlink listener for kernel uevent notifications
  11. //config: sent via netlink. It is usually used for dynamic device creation.
  12. //applet:IF_UEVENT(APPLET(uevent, BB_DIR_SBIN, BB_SUID_DROP))
  13. //kbuild:lib-$(CONFIG_UEVENT) += uevent.o
  14. //usage:#define uevent_trivial_usage
  15. //usage: "[PROG ARGS]"
  16. //usage:#define uevent_full_usage "\n\n"
  17. //usage: "uevent runs PROG for every netlink notification."
  18. //usage: "\n""PROG's environment contains data passed from the kernel."
  19. //usage: "\n""Typical usage (daemon for dynamic device node creation):"
  20. //usage: "\n"" # uevent mdev & mdev -s"
  21. #include "libbb.h"
  22. #include "common_bufsiz.h"
  23. #include <linux/netlink.h>
  24. #define env ((char **)bb_common_bufsiz1)
  25. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  26. enum {
  27. MAX_ENV = COMMON_BUFSIZE / sizeof(char*) - 1,
  28. // ^^^sizeof(env[0]) instead of sizeof(char*)
  29. // makes gcc-6.3.0 emit "strict-aliasing" warning.
  30. // socket receive buffer of 2MiB proved to be too small:
  31. // http://lists.busybox.net/pipermail/busybox/2019-December/087665.html
  32. // udevd seems to use a whooping 128MiB.
  33. // The socket receive buffer size is just a resource limit.
  34. // The buffers are allocated lazily so the memory is not wasted.
  35. KERN_RCVBUF = 128 * 1024 * 1024,
  36. // Might be made smaller: the kernel v5.4 passes up to 32 environment
  37. // variables with a total of 2kb on each event.
  38. // On top of that the action string and device path are added.
  39. USER_RCVBUF = 16 * 1024,
  40. };
  41. int uevent_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  42. int uevent_main(int argc UNUSED_PARAM, char **argv)
  43. {
  44. int fd;
  45. INIT_G();
  46. argv++;
  47. // Subscribe for UEVENT kernel messages.
  48. // Without a sufficiently big RCVBUF, a ton of simultaneous events
  49. // can trigger ENOBUFS on read, which is unrecoverable.
  50. // Reproducer:
  51. // uevent mdev &
  52. // find /sys -name uevent -exec sh -c 'echo add >"{}"' ';'
  53. reopen:
  54. fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, /*groups:*/ 1 << 0, KERN_RCVBUF);
  55. for (;;) {
  56. char *netbuf;
  57. char *s, *end;
  58. ssize_t len;
  59. int idx;
  60. // In many cases, a system sits for *days* waiting
  61. // for a new uevent notification to come in.
  62. // We use a fresh mmap so that buffer is not allocated
  63. // until kernel actually starts filling it.
  64. netbuf = xmmap_anon(USER_RCVBUF);
  65. // Here we block, possibly for a very long time
  66. len = safe_read(fd, netbuf, USER_RCVBUF - 1);
  67. if (len < 0) {
  68. if (errno == ENOBUFS) {
  69. // Ran out of socket receive buffer
  70. bb_simple_error_msg("uevent overrun");
  71. close(fd);
  72. munmap(netbuf, USER_RCVBUF);
  73. goto reopen;
  74. }
  75. bb_simple_perror_msg_and_die("read");
  76. }
  77. end = netbuf + len;
  78. *end = '\0';
  79. // Each netlink message starts with "ACTION@/path"
  80. // (which we currently ignore),
  81. // followed by environment variables.
  82. if (!argv[0])
  83. putchar('\n');
  84. idx = 0;
  85. s = netbuf;
  86. while (s < end) {
  87. if (!argv[0])
  88. puts(s);
  89. if (strchr(s, '=') && idx < MAX_ENV)
  90. env[idx++] = s;
  91. s += strlen(s) + 1;
  92. }
  93. env[idx] = NULL;
  94. if (argv[0]) {
  95. idx = 0;
  96. while (env[idx])
  97. putenv(env[idx++]);
  98. spawn_and_wait(argv);
  99. idx = 0;
  100. while (env[idx])
  101. bb_unsetenv(env[idx++]);
  102. }
  103. munmap(netbuf, USER_RCVBUF);
  104. }
  105. return 0; // not reached
  106. }