LocalSocket.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <LibCore/System.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/socket.h>
  13. #if defined(__FreeBSD__)
  14. # include <sys/ucred.h>
  15. #endif
  16. #ifndef SOCK_NONBLOCK
  17. # include <sys/ioctl.h>
  18. #endif
  19. namespace Core {
  20. LocalSocket::LocalSocket(int fd, Object* parent)
  21. : Socket(Socket::Type::Local, parent)
  22. {
  23. // NOTE: This constructor is used by LocalServer::accept(), so the socket is already connected.
  24. m_connected = true;
  25. set_fd(fd);
  26. set_mode(OpenMode::ReadWrite);
  27. set_error(0);
  28. }
  29. LocalSocket::LocalSocket(Object* parent)
  30. : Socket(Socket::Type::Local, parent)
  31. {
  32. #ifdef SOCK_NONBLOCK
  33. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  34. #else
  35. int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  36. int option = 1;
  37. ioctl(fd, FIONBIO, &option);
  38. fcntl(fd, F_SETFD, FD_CLOEXEC);
  39. #endif
  40. if (fd < 0) {
  41. set_error(errno);
  42. } else {
  43. set_fd(fd);
  44. set_mode(OpenMode::ReadWrite);
  45. set_error(0);
  46. }
  47. }
  48. LocalSocket::~LocalSocket()
  49. {
  50. }
  51. pid_t LocalSocket::peer_pid() const
  52. {
  53. #ifdef AK_OS_MACOS
  54. pid_t pid;
  55. socklen_t pid_size = sizeof(pid);
  56. #elif defined(__FreeBSD__)
  57. struct xucred creds = {};
  58. socklen_t creds_size = sizeof(creds);
  59. #elif defined(__OpenBSD__)
  60. struct sockpeercred creds = {};
  61. socklen_t creds_size = sizeof(creds);
  62. #else
  63. struct ucred creds = {};
  64. socklen_t creds_size = sizeof(creds);
  65. #endif
  66. #ifdef AK_OS_MACOS
  67. if (getsockopt(fd(), SOL_LOCAL, LOCAL_PEERPID, &pid, &pid_size) < 0) {
  68. #elif defined(__FreeBSD__)
  69. if (getsockopt(fd(), SOL_LOCAL, LOCAL_PEERCRED, &creds, &creds_size) < 0) {
  70. #else
  71. if (getsockopt(fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0) {
  72. #endif
  73. dbgln("LocalSocket: getsockopt failed, {}", strerror(errno));
  74. VERIFY_NOT_REACHED();
  75. }
  76. #ifdef AK_OS_MACOS
  77. return pid;
  78. #elif defined(__FreeBSD__)
  79. return creds.cr_pid;
  80. #else
  81. return creds.pid;
  82. #endif
  83. }
  84. HashMap<String, int> LocalSocket::s_overtaken_sockets {};
  85. bool LocalSocket::s_overtaken_sockets_parsed { false };
  86. void LocalSocket::parse_sockets_from_system_server()
  87. {
  88. VERIFY(!s_overtaken_sockets_parsed);
  89. constexpr auto socket_takeover = "SOCKET_TAKEOVER";
  90. const char* sockets = getenv(socket_takeover);
  91. if (!sockets) {
  92. s_overtaken_sockets_parsed = true;
  93. return;
  94. }
  95. for (auto& socket : StringView(sockets).split_view(' ')) {
  96. auto params = socket.split_view(':');
  97. s_overtaken_sockets.set(params[0].to_string(), strtol(params[1].to_string().characters(), nullptr, 10));
  98. }
  99. s_overtaken_sockets_parsed = true;
  100. // We wouldn't want our children to think we're passing
  101. // them a socket either, so unset the env variable.
  102. unsetenv(socket_takeover);
  103. }
  104. ErrorOr<NonnullRefPtr<LocalSocket>> LocalSocket::take_over_accepted_socket_from_system_server(String const& socket_path)
  105. {
  106. if (!s_overtaken_sockets_parsed)
  107. parse_sockets_from_system_server();
  108. int fd;
  109. if (socket_path.is_null()) {
  110. // We want the first (and only) socket.
  111. VERIFY(s_overtaken_sockets.size() == 1);
  112. fd = s_overtaken_sockets.begin()->value;
  113. } else {
  114. auto it = s_overtaken_sockets.find(socket_path);
  115. if (it == s_overtaken_sockets.end())
  116. return Error::from_string_literal("Non-existent socket requested"sv);
  117. fd = it->value;
  118. }
  119. // Sanity check: it has to be a socket.
  120. auto stat = TRY(Core::System::fstat(fd));
  121. if (!S_ISSOCK(stat.st_mode))
  122. return Error::from_string_literal("The fd we got from SystemServer is not a socket"sv);
  123. auto socket = LocalSocket::construct(fd);
  124. // It had to be !CLOEXEC for obvious reasons, but we
  125. // don't need it to be !CLOEXEC anymore, so set the
  126. // CLOEXEC flag now.
  127. TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
  128. return socket;
  129. }
  130. }