LocalSocket.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/LocalSocket.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/socket.h>
  12. #include <sys/stat.h>
  13. #ifndef SOCK_NONBLOCK
  14. # include <sys/ioctl.h>
  15. #endif
  16. namespace Core {
  17. LocalSocket::LocalSocket(int fd, Object* parent)
  18. : Socket(Socket::Type::Local, parent)
  19. {
  20. // NOTE: This constructor is used by LocalServer::accept(), so the socket is already connected.
  21. m_connected = true;
  22. set_fd(fd);
  23. set_mode(OpenMode::ReadWrite);
  24. set_error(0);
  25. }
  26. LocalSocket::LocalSocket(Object* parent)
  27. : Socket(Socket::Type::Local, parent)
  28. {
  29. #ifdef SOCK_NONBLOCK
  30. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  31. #else
  32. int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  33. int option = 1;
  34. ioctl(fd, FIONBIO, &option);
  35. fcntl(fd, F_SETFD, FD_CLOEXEC);
  36. #endif
  37. if (fd < 0) {
  38. set_error(errno);
  39. } else {
  40. set_fd(fd);
  41. set_mode(OpenMode::ReadWrite);
  42. set_error(0);
  43. }
  44. }
  45. LocalSocket::~LocalSocket()
  46. {
  47. }
  48. HashMap<String, int> LocalSocket::s_overtaken_sockets {};
  49. bool LocalSocket::s_overtaken_sockets_parsed { false };
  50. void LocalSocket::parse_sockets_from_system_server()
  51. {
  52. VERIFY(!s_overtaken_sockets_parsed);
  53. constexpr auto socket_takeover = "SOCKET_TAKEOVER";
  54. const char* sockets = getenv(socket_takeover);
  55. if (!sockets) {
  56. s_overtaken_sockets_parsed = true;
  57. return;
  58. }
  59. for (auto& socket : StringView(sockets).split_view(' ')) {
  60. auto params = socket.split_view(':');
  61. s_overtaken_sockets.set(params[0].to_string(), strtol(params[1].to_string().characters(), nullptr, 10));
  62. }
  63. s_overtaken_sockets_parsed = true;
  64. // We wouldn't want our children to think we're passing
  65. // them a socket either, so unset the env variable.
  66. unsetenv(socket_takeover);
  67. }
  68. RefPtr<LocalSocket> LocalSocket::take_over_accepted_socket_from_system_server(String const& socket_path)
  69. {
  70. if (!s_overtaken_sockets_parsed)
  71. parse_sockets_from_system_server();
  72. int fd;
  73. if (socket_path.is_null()) {
  74. // We want the first (and only) socket.
  75. VERIFY(s_overtaken_sockets.size() == 1);
  76. fd = s_overtaken_sockets.begin()->value;
  77. } else {
  78. auto it = s_overtaken_sockets.find(socket_path);
  79. if (it == s_overtaken_sockets.end()) {
  80. dbgln("Non-existent socket requested");
  81. return nullptr;
  82. }
  83. fd = it->value;
  84. }
  85. // Sanity check: it has to be a socket.
  86. struct stat stat;
  87. int rc = fstat(fd, &stat);
  88. if (rc < 0 || !S_ISSOCK(stat.st_mode)) {
  89. if (rc != 0)
  90. perror("fstat");
  91. dbgln("ERROR: The fd we got from SystemServer is not a socket");
  92. return nullptr;
  93. }
  94. auto socket = LocalSocket::construct(fd);
  95. // It had to be !CLOEXEC for obvious reasons, but we
  96. // don't need it to be !CLOEXEC anymore, so set the
  97. // CLOEXEC flag now.
  98. fcntl(fd, F_SETFD, FD_CLOEXEC);
  99. return socket;
  100. }
  101. }