LocalSocket.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #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. #else
  60. struct ucred creds = {};
  61. socklen_t creds_size = sizeof(creds);
  62. #endif
  63. #ifdef AK_OS_MACOS
  64. if (getsockopt(fd(), SOL_LOCAL, LOCAL_PEERPID, &pid, &pid_size) < 0) {
  65. #elif defined(__FreeBSD__)
  66. if (getsockopt(fd(), SOL_LOCAL, LOCAL_PEERCRED, &creds, &creds_size) < 0) {
  67. #else
  68. if (getsockopt(fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0) {
  69. #endif
  70. dbgln("LocalSocket: getsockopt failed, {}", strerror(errno));
  71. VERIFY_NOT_REACHED();
  72. }
  73. #ifdef AK_OS_MACOS
  74. return pid;
  75. #elif defined(__FreeBSD__)
  76. return creds.cr_pid;
  77. #else
  78. return creds.pid;
  79. #endif
  80. }
  81. HashMap<String, int> LocalSocket::s_overtaken_sockets {};
  82. bool LocalSocket::s_overtaken_sockets_parsed { false };
  83. void LocalSocket::parse_sockets_from_system_server()
  84. {
  85. VERIFY(!s_overtaken_sockets_parsed);
  86. constexpr auto socket_takeover = "SOCKET_TAKEOVER";
  87. const char* sockets = getenv(socket_takeover);
  88. if (!sockets) {
  89. s_overtaken_sockets_parsed = true;
  90. return;
  91. }
  92. for (auto& socket : StringView(sockets).split_view(' ')) {
  93. auto params = socket.split_view(':');
  94. s_overtaken_sockets.set(params[0].to_string(), strtol(params[1].to_string().characters(), nullptr, 10));
  95. }
  96. s_overtaken_sockets_parsed = true;
  97. // We wouldn't want our children to think we're passing
  98. // them a socket either, so unset the env variable.
  99. unsetenv(socket_takeover);
  100. }
  101. RefPtr<LocalSocket> LocalSocket::take_over_accepted_socket_from_system_server(String const& socket_path)
  102. {
  103. if (!s_overtaken_sockets_parsed)
  104. parse_sockets_from_system_server();
  105. int fd;
  106. if (socket_path.is_null()) {
  107. // We want the first (and only) socket.
  108. VERIFY(s_overtaken_sockets.size() == 1);
  109. fd = s_overtaken_sockets.begin()->value;
  110. } else {
  111. auto it = s_overtaken_sockets.find(socket_path);
  112. if (it == s_overtaken_sockets.end()) {
  113. dbgln("Non-existent socket requested");
  114. return nullptr;
  115. }
  116. fd = it->value;
  117. }
  118. // Sanity check: it has to be a socket.
  119. struct stat stat;
  120. int rc = fstat(fd, &stat);
  121. if (rc < 0 || !S_ISSOCK(stat.st_mode)) {
  122. if (rc != 0)
  123. perror("fstat");
  124. dbgln("ERROR: The fd we got from SystemServer is not a socket");
  125. return nullptr;
  126. }
  127. auto socket = LocalSocket::construct(fd);
  128. // It had to be !CLOEXEC for obvious reasons, but we
  129. // don't need it to be !CLOEXEC anymore, so set the
  130. // CLOEXEC flag now.
  131. fcntl(fd, F_SETFD, FD_CLOEXEC);
  132. return socket;
  133. }
  134. }