uevent.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.1 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. int fd;
  42. INIT_G();
  43. argv++;
  44. // Subscribe for UEVENT kernel messages.
  45. // Without a sufficiently big RCVBUF, a ton of simultaneous events
  46. // can trigger ENOBUFS on read, which is unrecoverable.
  47. // Reproducer:
  48. // uevent mdev &
  49. // find /sys -name uevent -exec sh -c 'echo add >"{}"' ';'
  50. fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, /*groups:*/ 1 << 0, RCVBUF);
  51. for (;;) {
  52. char *netbuf;
  53. char *s, *end;
  54. ssize_t len;
  55. int idx;
  56. // In many cases, a system sits for *days* waiting
  57. // for a new uevent notification to come in.
  58. // We use a fresh mmap so that buffer is not allocated
  59. // until kernel actually starts filling it.
  60. netbuf = mmap(NULL, BUFFER_SIZE,
  61. PROT_READ | PROT_WRITE,
  62. MAP_PRIVATE | MAP_ANON,
  63. /* ignored: */ -1, 0);
  64. if (netbuf == MAP_FAILED)
  65. bb_simple_perror_msg_and_die("mmap");
  66. // Here we block, possibly for a very long time
  67. len = safe_read(fd, netbuf, BUFFER_SIZE - 1);
  68. if (len < 0)
  69. bb_simple_perror_msg_and_die("read");
  70. end = netbuf + len;
  71. *end = '\0';
  72. // Each netlink message starts with "ACTION@/path"
  73. // (which we currently ignore),
  74. // followed by environment variables.
  75. if (!argv[0])
  76. putchar('\n');
  77. idx = 0;
  78. s = netbuf;
  79. while (s < end) {
  80. if (!argv[0])
  81. puts(s);
  82. if (strchr(s, '=') && idx < MAX_ENV)
  83. env[idx++] = s;
  84. s += strlen(s) + 1;
  85. }
  86. env[idx] = NULL;
  87. if (argv[0]) {
  88. idx = 0;
  89. while (env[idx])
  90. putenv(env[idx++]);
  91. spawn_and_wait(argv);
  92. idx = 0;
  93. while (env[idx])
  94. bb_unsetenv(env[idx++]);
  95. }
  96. munmap(netbuf, BUFFER_SIZE);
  97. }
  98. return 0; // not reached
  99. }