File.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <AK/Noncopyable.h>
  29. #include <AK/StdLibExtras.h>
  30. #include <unistd.h>
  31. namespace IPC {
  32. class File {
  33. AK_MAKE_NONCOPYABLE(File);
  34. public:
  35. // Must have a default constructor, because LibIPC
  36. // default-constructs arguments prior to decoding them.
  37. File() { }
  38. // Intentionally not `explicit`.
  39. File(int fd)
  40. : m_fd(fd)
  41. {
  42. }
  43. // Tagged constructor for fd's that should be closed on destruction unless take_fd() is called.
  44. // Note that the tags are the same, this is intentional to allow expressive invocation.
  45. enum Tag {
  46. ConstructWithReceivedFileDescriptor = 1,
  47. CloseAfterSending = 1,
  48. };
  49. File(int fd, Tag)
  50. : m_fd(fd)
  51. , m_close_on_destruction(true)
  52. {
  53. }
  54. File(File&& other)
  55. : m_fd(exchange(other.m_fd, -1))
  56. , m_close_on_destruction(exchange(other.m_close_on_destruction, false))
  57. {
  58. }
  59. File& operator=(File&& other)
  60. {
  61. if (this != &other) {
  62. m_fd = exchange(other.m_fd, -1);
  63. m_close_on_destruction = exchange(other.m_close_on_destruction, false);
  64. }
  65. return *this;
  66. }
  67. ~File()
  68. {
  69. if (m_close_on_destruction && m_fd != -1)
  70. close(m_fd);
  71. }
  72. int fd() const { return m_fd; }
  73. // NOTE: This is 'const' since generated IPC messages expose all parameters by const reference.
  74. [[nodiscard]] int take_fd() const
  75. {
  76. return exchange(m_fd, -1);
  77. }
  78. private:
  79. mutable int m_fd { -1 };
  80. bool m_close_on_destruction { false };
  81. };
  82. }