forked from OpenVPN/openvpn3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoped_fd.hpp
140 lines (115 loc) · 3.04 KB
/
scoped_fd.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2020 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// A scoped file descriptor that is automatically closed by its destructor.
#ifndef OPENVPN_COMMON_SCOPED_FD_H
#define OPENVPN_COMMON_SCOPED_FD_H
#include <unistd.h> // for close()
#include <errno.h>
namespace openvpn {
class ScopedFD
{
ScopedFD(const ScopedFD&) = delete;
ScopedFD& operator=(const ScopedFD&) = delete;
public:
typedef int base_type;
ScopedFD() : fd(undefined()) {}
explicit ScopedFD(const int fd_arg)
: fd(fd_arg) {}
static int undefined() { return -1; }
int release()
{
const int ret = fd;
fd = -1;
//OPENVPN_LOG("**** SFD RELEASE=" << ret);
return ret;
}
static bool defined_static(int fd)
{
return fd >= 0;
}
bool defined() const
{
return defined_static(fd);
}
int operator()() const
{
return fd;
}
void reset(const int fd_arg)
{
close();
fd = fd_arg;
//OPENVPN_LOG("**** SFD RESET=" << fd);
}
void reset()
{
close();
}
// unusual semantics: replace fd without closing it first
void replace(const int fd_arg)
{
//OPENVPN_LOG("**** SFD REPLACE " << fd << " -> " << fd_arg);
fd = fd_arg;
}
// return false if close error
bool close()
{
return close_with_errno() == 0;
}
// return errno value if close error, otherwise return 0
int close_with_errno()
{
int eno = 0;
if (defined())
{
if (::close(fd) == -1)
eno = errno;
post_close(eno);
//OPENVPN_LOG("**** SFD CLOSE fd=" << fd << " errno=" << eno);
fd = -1;
}
return eno;
}
virtual void post_close(const int close_errno)
{
}
virtual ~ScopedFD()
{
//OPENVPN_LOG("**** SFD DESTRUCTOR");
close();
}
ScopedFD(ScopedFD&& other) noexcept
{
fd = other.fd;
other.fd = -1;
}
ScopedFD& operator=(ScopedFD&& other) noexcept
{
close();
fd = other.fd;
other.fd = -1;
return *this;
}
private:
int fd;
};
} // namespace openvpn
#endif // OPENVPN_COMMON_SCOPED_FD_H