LocalSocket.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/LocalSocket.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <sys/socket.h>
  32. #include <sys/stat.h>
  33. #ifndef SOCK_NONBLOCK
  34. # include <sys/ioctl.h>
  35. #endif
  36. namespace Core {
  37. LocalSocket::LocalSocket(int fd, Object* parent)
  38. : Socket(Socket::Type::Local, parent)
  39. {
  40. // NOTE: This constructor is used by LocalServer::accept(), so the socket is already connected.
  41. m_connected = true;
  42. set_fd(fd);
  43. set_mode(IODevice::ReadWrite);
  44. set_error(0);
  45. }
  46. LocalSocket::LocalSocket(Object* parent)
  47. : Socket(Socket::Type::Local, parent)
  48. {
  49. #ifdef SOCK_NONBLOCK
  50. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  51. #else
  52. int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  53. int option = 1;
  54. ioctl(fd, FIONBIO, &option);
  55. fcntl(fd, F_SETFD, FD_CLOEXEC);
  56. #endif
  57. if (fd < 0) {
  58. set_error(errno);
  59. } else {
  60. set_fd(fd);
  61. set_mode(IODevice::ReadWrite);
  62. set_error(0);
  63. }
  64. }
  65. LocalSocket::~LocalSocket()
  66. {
  67. }
  68. HashMap<String, int> LocalSocket::s_overtaken_sockets {};
  69. bool LocalSocket::s_overtaken_sockets_parsed { false };
  70. void LocalSocket::parse_sockets_from_system_server()
  71. {
  72. VERIFY(!s_overtaken_sockets_parsed);
  73. constexpr auto socket_takeover = "SOCKET_TAKEOVER";
  74. const char* sockets = getenv(socket_takeover);
  75. if (!sockets) {
  76. s_overtaken_sockets_parsed = true;
  77. return;
  78. }
  79. for (auto& socket : StringView(sockets).split_view(' ')) {
  80. auto params = socket.split_view(':');
  81. s_overtaken_sockets.set(params[0].to_string(), strtol(params[1].to_string().characters(), nullptr, 10));
  82. }
  83. s_overtaken_sockets_parsed = true;
  84. // We wouldn't want our children to think we're passing
  85. // them a socket either, so unset the env variable.
  86. unsetenv(socket_takeover);
  87. }
  88. RefPtr<LocalSocket> LocalSocket::take_over_accepted_socket_from_system_server(String const& socket_path)
  89. {
  90. if (!s_overtaken_sockets_parsed)
  91. parse_sockets_from_system_server();
  92. int fd;
  93. if (socket_path.is_null()) {
  94. // We want the first (and only) socket.
  95. VERIFY(s_overtaken_sockets.size() == 1);
  96. fd = s_overtaken_sockets.begin()->value;
  97. } else {
  98. auto it = s_overtaken_sockets.find(socket_path);
  99. if (it == s_overtaken_sockets.end()) {
  100. dbgln("Non-existent socket requested");
  101. return nullptr;
  102. }
  103. fd = it->value;
  104. }
  105. // Sanity check: it has to be a socket.
  106. struct stat stat;
  107. int rc = fstat(fd, &stat);
  108. if (rc < 0 || !S_ISSOCK(stat.st_mode)) {
  109. if (rc != 0)
  110. perror("fstat");
  111. dbgln("ERROR: The fd we got from SystemServer is not a socket");
  112. return nullptr;
  113. }
  114. auto socket = LocalSocket::construct(fd);
  115. // It had to be !CLOEXEC for obvious reasons, but we
  116. // don't need it to be !CLOEXEC anymore, so set the
  117. // CLOEXEC flag now.
  118. fcntl(fd, F_SETFD, FD_CLOEXEC);
  119. return socket;
  120. }
  121. }