LocalSocket.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <sys/socket.h>
  31. #include <sys/stat.h>
  32. #ifndef SOCK_NONBLOCK
  33. # include <sys/ioctl.h>
  34. #endif
  35. namespace Core {
  36. LocalSocket::LocalSocket(int fd, Object* parent)
  37. : Socket(Socket::Type::Local, parent)
  38. {
  39. // NOTE: This constructor is used by LocalServer::accept(), so the socket is already connected.
  40. m_connected = true;
  41. set_fd(fd);
  42. set_mode(IODevice::ReadWrite);
  43. set_error(0);
  44. }
  45. LocalSocket::LocalSocket(Object* parent)
  46. : Socket(Socket::Type::Local, parent)
  47. {
  48. #ifdef SOCK_NONBLOCK
  49. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  50. #else
  51. int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  52. int option = 1;
  53. ioctl(fd, FIONBIO, &option);
  54. fcntl(fd, F_SETFD, FD_CLOEXEC);
  55. #endif
  56. if (fd < 0) {
  57. set_error(errno);
  58. } else {
  59. set_fd(fd);
  60. set_mode(IODevice::ReadWrite);
  61. set_error(0);
  62. }
  63. }
  64. LocalSocket::~LocalSocket()
  65. {
  66. }
  67. RefPtr<LocalSocket> LocalSocket::take_over_accepted_socket_from_system_server()
  68. {
  69. constexpr auto socket_takeover = "SOCKET_TAKEOVER";
  70. if (!getenv(socket_takeover))
  71. return nullptr;
  72. // The SystemServer has passed us the socket as fd 3,
  73. // so use that instead of creating our own.
  74. constexpr int fd = 3;
  75. // Sanity check: it has to be a socket.
  76. struct stat stat;
  77. int rc = fstat(fd, &stat);
  78. if (rc < 0 || !S_ISSOCK(stat.st_mode)) {
  79. if (rc != 0)
  80. perror("fstat");
  81. dbgln("ERROR: The fd we got from SystemServer is not a socket");
  82. return nullptr;
  83. }
  84. auto socket = LocalSocket::construct(fd);
  85. // It had to be !CLOEXEC for obvious reasons, but we
  86. // don't need it to be !CLOEXEC anymore, so set the
  87. // CLOEXEC flag now.
  88. fcntl(fd, F_SETFD, FD_CLOEXEC);
  89. // We wouldn't want our children to think we're passing
  90. // them a socket either, so unset the env variable.
  91. unsetenv(socket_takeover);
  92. return socket;
  93. }
  94. }