OpenVPN 3 Core Library
Loading...
Searching...
No Matches
scoped_fd.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// A scoped file descriptor that is automatically closed by its destructor.
13
14#ifndef OPENVPN_COMMON_SCOPED_FD_H
15#define OPENVPN_COMMON_SCOPED_FD_H
16
17#include <unistd.h> // for close()
18#include <errno.h>
19
20namespace openvpn {
21
23{
24 ScopedFD(const ScopedFD &) = delete;
25 ScopedFD &operator=(const ScopedFD &) = delete;
26
27 public:
28 typedef int base_type;
29
31 : fd(undefined())
32 {
33 }
34
35 explicit ScopedFD(const int fd_arg)
36 : fd(fd_arg)
37 {
38 }
39
40 static int undefined()
41 {
42 return -1;
43 }
44
45 int release()
46 {
47 const int ret = fd;
48 fd = -1;
49 // OPENVPN_LOG("**** SFD RELEASE=" << ret);
50 return ret;
51 }
52
53 static bool defined_static(int fd)
54 {
55 return fd >= 0;
56 }
57
58 bool defined() const
59 {
60 return defined_static(fd);
61 }
62
63 int operator()() const
64 {
65 return fd;
66 }
67
68 void reset(const int fd_arg)
69 {
70 close();
71 fd = fd_arg;
72 // OPENVPN_LOG("**** SFD RESET=" << fd);
73 }
74
75 void reset()
76 {
77 close();
78 }
79
80 // unusual semantics: replace fd without closing it first
81 void replace(const int fd_arg)
82 {
83 // OPENVPN_LOG("**** SFD REPLACE " << fd << " -> " << fd_arg);
84 fd = fd_arg;
85 }
86
87 // return false if close error
88 bool close()
89 {
90 return close_with_errno() == 0;
91 }
92
93 // return errno value if close error, otherwise return 0
95 {
96 int eno = 0;
97 if (defined())
98 {
99 if (::close(fd) == -1)
100 eno = errno;
101 // OPENVPN_LOG("**** SFD CLOSE fd=" << fd << " errno=" << eno);
102 fd = -1;
103 }
104 return eno;
105 }
106
107 virtual ~ScopedFD()
108 {
109 // OPENVPN_LOG("**** SFD DESTRUCTOR");
110 close();
111 }
112
113 ScopedFD(ScopedFD &&other) noexcept
114 {
115 fd = other.fd;
116 other.fd = -1;
117 }
118
119 ScopedFD &operator=(ScopedFD &&other) noexcept
120 {
121 close();
122 fd = other.fd;
123 other.fd = -1;
124 return *this;
125 }
126
127 private:
128 int fd;
129};
130
131} // namespace openvpn
132
133#endif // OPENVPN_COMMON_SCOPED_FD_H
ScopedFD & operator=(const ScopedFD &)=delete
ScopedFD(const int fd_arg)
Definition scoped_fd.hpp:35
static int undefined()
Definition scoped_fd.hpp:40
void reset(const int fd_arg)
Definition scoped_fd.hpp:68
void replace(const int fd_arg)
Definition scoped_fd.hpp:81
virtual ~ScopedFD()
int operator()() const
Definition scoped_fd.hpp:63
ScopedFD(const ScopedFD &)=delete
static bool defined_static(int fd)
Definition scoped_fd.hpp:53
ScopedFD(ScopedFD &&other) noexcept
ScopedFD & operator=(ScopedFD &&other) noexcept
bool defined() const
Definition scoped_fd.hpp:58
std::string ret