sendfd.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <Kernel/FileSystem/FileDescription.h>
  27. #include <Kernel/Net/LocalSocket.h>
  28. #include <Kernel/Process.h>
  29. namespace Kernel {
  30. int Process::sys$sendfd(int sockfd, int fd)
  31. {
  32. REQUIRE_PROMISE(sendfd);
  33. auto socket_description = file_description(sockfd);
  34. if (!socket_description)
  35. return -EBADF;
  36. if (!socket_description->is_socket())
  37. return -ENOTSOCK;
  38. auto& socket = *socket_description->socket();
  39. if (!socket.is_local())
  40. return -EAFNOSUPPORT;
  41. if (!socket.is_connected())
  42. return -ENOTCONN;
  43. auto passing_descriptor = file_description(fd);
  44. if (!passing_descriptor)
  45. return -EBADF;
  46. auto& local_socket = static_cast<LocalSocket&>(socket);
  47. return local_socket.sendfd(*socket_description, *passing_descriptor);
  48. }
  49. int Process::sys$recvfd(int sockfd, int options)
  50. {
  51. REQUIRE_PROMISE(recvfd);
  52. auto socket_description = file_description(sockfd);
  53. if (!socket_description)
  54. return -EBADF;
  55. if (!socket_description->is_socket())
  56. return -ENOTSOCK;
  57. auto& socket = *socket_description->socket();
  58. if (!socket.is_local())
  59. return -EAFNOSUPPORT;
  60. int new_fd = alloc_fd();
  61. if (new_fd < 0)
  62. return new_fd;
  63. auto& local_socket = static_cast<LocalSocket&>(socket);
  64. auto received_descriptor_or_error = local_socket.recvfd(*socket_description);
  65. if (received_descriptor_or_error.is_error())
  66. return received_descriptor_or_error.error();
  67. u32 fd_flags = 0;
  68. if (options & O_CLOEXEC)
  69. fd_flags |= FD_CLOEXEC;
  70. m_fds[new_fd].set(*received_descriptor_or_error.value(), fd_flags);
  71. return new_fd;
  72. }
  73. }