-
Notifications
You must be signed in to change notification settings - Fork 5
/
fdset.h
169 lines (127 loc) · 2.67 KB
/
fdset.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef __fdset__
#define __fdset__
#include <array>
#include <initializer_list>
#include <string>
#include <vector>
#include <unistd.h>
class fdset;
class fdmask;
/*
* fdmask does not own the file descriptors and will not close them.
*
*/
class fdmask {
public:
fdmask() = default;
fdmask(const fdmask &) = default;
fdmask(fdmask &&) = default;
fdmask(const std::array<int, 3> &rhs) : _fds(rhs)
{}
fdmask(int a, int b, int c) : _fds{{ a, b, c }}
{}
#if 0
fdmask(std::initializer_list<int> rhs) : _fds(rhs)
{}
#endif
fdmask &operator=(const fdmask &) = default;
fdmask &operator=(fdmask &&) = default;
fdmask &operator=(const std::array<int, 3> &rhs) {
_fds = rhs;
return *this;
}
#if 0
fdmask &operator=(std::initializer_list<int> rhs) {
_fds = rhs;
}
#endif
void dup() const {
// dup fds to stdin/stdout/stderr.
// called after fork, before exec.
#define __(index, target) \
if (_fds[index] >= 0 && _fds[index] != target) dup2(_fds[index], target)
__(0, STDIN_FILENO);
__(1, STDOUT_FILENO);
__(2, STDERR_FILENO);
#undef __
}
int operator[](unsigned index) const {
if (_fds[index] >= 0) return _fds[index];
return default_fd(index);
}
fdmask &operator|=(const fdmask &rhs) {
for (unsigned i = 0; i < 3; ++i) {
if (_fds[i] < 0) _fds[i] = rhs._fds[i];
}
return *this;
}
private:
static int default_fd(int index) {
static constexpr std::array<int, 3> _default_fds = {{ STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO }};
return _default_fds[index];
}
friend class fdset;
std::array<int, 3> _fds = {{ -1, -1, -1 }};
};
/*
* fd set owns it's descriptors and will close them.
*
*
*/
class fdset {
public:
fdset() = default;
fdset(const fdset &) = delete;
fdset(fdset && rhs) {
std::swap(rhs._fds, _fds);
}
~fdset() {
close();
}
fdset &operator=(const fdset &) = delete;
fdset &operator=(fdset &&rhs) {
if (&rhs != this) {
std::swap(_fds, rhs._fds);
rhs.close();
}
return *this;
}
void close(void) {
for (int &fd : _fds) {
if (fd >= 0) {
::close(fd);
fd = -1;
}
}
}
void set(int index, int fd) {
std::swap(fd, _fds[index]);
if (fd >= 0) ::close(fd);
}
fdmask to_mask() const {
return fdmask(_fds);
}
void swap_in_out() {
std::swap(_fds[0], _fds[1]);
}
private:
void reset() {
_fds = {{ -1, -1, -1 }};
}
std::array<int, 3> _fds = {{ -1, -1, -1 }};
};
inline fdmask operator|(const fdmask &lhs, const fdmask &rhs) {
fdmask tmp(lhs);
tmp |= rhs;
return tmp;
}
inline fdmask operator|(const fdset &lhs, const fdmask &rhs) {
fdmask tmp(lhs.to_mask());
tmp |= rhs;
return tmp;
}
struct process {
std::vector<std::string> arguments;
fdset fds;
};
#endif