3
0

uevent.c 3.3 KB

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