Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include "FileDescriptor.h"
21 : #include "IpcLogging.h"
22 :
23 : #include <cerrno>
24 : #include <fcntl.h>
25 : #include <unistd.h>
26 :
27 : namespace firebolt::rialto::ipc
28 : {
29 0 : FileDescriptor::FileDescriptor() : m_fd(-1) {}
30 :
31 0 : FileDescriptor::FileDescriptor(int fd) : m_fd(-1)
32 : {
33 0 : if (fd >= 0)
34 : {
35 0 : m_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
36 0 : if (m_fd < 0)
37 0 : RIALTO_IPC_LOG_SYS_WARN(errno, "failed to dup supplied fd");
38 : }
39 : }
40 :
41 0 : FileDescriptor::FileDescriptor(const FileDescriptor &other) : m_fd(-1)
42 : {
43 0 : if (other.m_fd >= 0)
44 : {
45 0 : m_fd = fcntl(other.m_fd, F_DUPFD_CLOEXEC, 3);
46 0 : if (m_fd < 0)
47 0 : RIALTO_IPC_LOG_SYS_WARN(errno, "failed to dup supplied fd");
48 : }
49 : }
50 :
51 0 : FileDescriptor &FileDescriptor::operator=(FileDescriptor &&other) noexcept
52 : {
53 0 : if ((m_fd >= 0) && (::close(m_fd) != 0))
54 0 : RIALTO_IPC_LOG_SYS_ERROR(errno, "failed to close file descriptor");
55 :
56 0 : m_fd = other.m_fd;
57 0 : other.m_fd = -1;
58 :
59 0 : return *this;
60 : }
61 :
62 0 : FileDescriptor &FileDescriptor::operator=(const FileDescriptor &other)
63 : {
64 0 : if (this == &other)
65 0 : return *this;
66 :
67 0 : if ((m_fd >= 0) && (::close(m_fd) != 0))
68 0 : RIALTO_IPC_LOG_SYS_ERROR(errno, "failed to close file descriptor");
69 :
70 0 : m_fd = -1;
71 :
72 0 : if (other.m_fd >= 0)
73 : {
74 0 : m_fd = fcntl(other.m_fd, F_DUPFD_CLOEXEC, 3);
75 0 : if (m_fd < 0)
76 0 : RIALTO_IPC_LOG_SYS_WARN(errno, "failed to dup supplied fd");
77 : }
78 :
79 0 : return *this;
80 : }
81 :
82 0 : FileDescriptor::~FileDescriptor()
83 : {
84 0 : reset();
85 : }
86 :
87 0 : bool FileDescriptor::isValid() const
88 : {
89 0 : return (m_fd >= 0);
90 : }
91 :
92 0 : int FileDescriptor::fd() const
93 : {
94 0 : return m_fd;
95 : }
96 :
97 0 : void FileDescriptor::reset(int fd)
98 : {
99 0 : if ((m_fd >= 0) && (::close(m_fd) != 0))
100 0 : RIALTO_IPC_LOG_SYS_ERROR(errno, "failed to close file descriptor");
101 :
102 0 : if (fd < 0)
103 : {
104 0 : m_fd = -1;
105 : }
106 : else
107 : {
108 0 : m_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
109 0 : if (m_fd < 0)
110 0 : RIALTO_IPC_LOG_SYS_WARN(errno, "failed to dup supplied fd");
111 : }
112 : }
113 :
114 0 : void FileDescriptor::clear()
115 : {
116 0 : reset();
117 : }
118 :
119 0 : int FileDescriptor::release()
120 : {
121 0 : int fd = m_fd;
122 0 : m_fd = -1;
123 0 : return fd;
124 : }
125 :
126 : } // namespace firebolt::rialto::ipc
|