uevent.c 3.2 KB

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