uevent.c 3.4 KB

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