LocalSocket.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. pid_t LocalSocket::peer_pid() const
  49. {
  50. #ifdef AK_OS_MACOS
  51. pid_t pid;
  52. socklen_t pid_size = sizeof(pid);
  53. if (getsockopt(fd(), SOL_LOCAL, LOCAL_PEERPID, &pid, &pid_size) < 0) {
  54. dbgln("LocalSocket: getsockopt failed, {}", strerror(errno));
  55. VERIFY_NOT_REACHED();
  56. }
  57. return pid;
  58. #else
  59. struct ucred creds = {};
  60. socklen_t creds_size = sizeof(creds);
  61. if (getsockopt(fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0) {
  62. dbgln("LocalSocket: getsockopt failed, {}", strerror(errno));
  63. VERIFY_NOT_REACHED();
  64. }
  65. return creds.pid;
  66. #endif
  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. }