File: FileDescriptor.h

package info (click to toggle)
ruby-passenger 3.0.13debian-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,920 kB
  • sloc: cpp: 99,104; ruby: 18,098; ansic: 9,846; sh: 8,632; python: 141; makefile: 30
file content (292 lines) | stat: -rw-r--r-- 7,671 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 *  Phusion Passenger - http://www.modrails.com/
 *  Copyright (c) 2010 Phusion
 *
 *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
#ifndef _PASSENGER_FILE_DESCRIPTOR_H_
#define _PASSENGER_FILE_DESCRIPTOR_H_

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <oxt/system_calls.hpp>

#include <utility>
#include <unistd.h>
#include <cerrno>

#include <Exceptions.h>

namespace Passenger {

using namespace std;
using namespace boost;
using namespace oxt;


#ifndef _PASSENGER_SAFELY_CLOSE_DEFINED_
	#define _PASSENGER_SAFELY_CLOSE_DEFINED_
	void safelyClose(int fd, bool ignoreErrors = false);
#endif


/**
 * Wrapper class around a file descriptor integer, for RAII behavior.
 *
 * A FileDescriptor object behaves just like an int, so that you can pass it to
 * system calls such as read(). It performs reference counting. When the last
 * copy of a FileDescriptor has been destroyed, the underlying file descriptor
 * will be automatically closed. In this case, any close() system call errors
 * are silently ignored. If you are interested in whether the close() system
 * call succeeded, then you should call FileDescriptor::close().
 *
 * This class is *not* thread-safe. It is safe to call system calls on the
 * underlying file descriptor from multiple threads, but it's not safe to
 * call FileDescriptor::close() from multiple threads if all those
 * FileDescriptor objects point to the same underlying file descriptor.
 */
class FileDescriptor {
private:
	struct SharedData {
		int fd;
		
		SharedData(int fd) {
			this->fd = fd;
		}
		
		~SharedData() {
			if (fd >= 0) {
				this_thread::disable_syscall_interruption dsi;
				syscalls::close(fd);
			}
		}
		
		void close(bool checkErrors = true) {
			if (fd >= 0) {
				this_thread::disable_syscall_interruption dsi;
				int theFd = fd;
				fd = -1;
				safelyClose(theFd, !checkErrors);
			}
		}
		
		void detach() {
			fd = -1;
		}
	};

	/** Shared pointer for reference counting on this file descriptor */
	shared_ptr<SharedData> data;

public:
	/** 
	 * Creates a new empty FileDescriptor instance that has no underlying
	 * file descriptor.
	 *
	 * @post *this == -1
	 */
	FileDescriptor() { }
	
	/**
	 * Creates a new FileDescriptor instance with the given fd as a handle.
	 *
	 * @post *this == fd
	 */
	FileDescriptor(int fd) {
		if (fd >= 0) {
			/* Make sure that the 'new' operator doesn't overwrite
			 * errno so that we can write code like this:
			 *
			 *    FileDescriptor fd = open(...);
			 *    if (fd == -1) {
			 *       print_error(errno);
			 *    }
			 */
			int e = errno;
			data = make_shared<SharedData>(fd);
			errno = e;
		}
	}
	
	/**
	 * Close the underlying file descriptor. If it was already closed, then
	 * nothing will happen. If there are multiple copies of this FileDescriptor
	 * then the underlying file descriptor will be closed for every one of them.
	 *
	 * @params checkErrors Whether a SystemException should be thrown in case
	 *                     closing the file descriptor fails. If false, errors
	 *                     are silently ignored.
	 * @throws SystemException Something went wrong while closing
	 *                         the file descriptor. Only thrown if
	 *                         checkErrors is true.
	 * @post *this == -1
	 */
	void close(bool checkErrors = true) {
		if (data != NULL) {
			data->close(checkErrors);
			data.reset();
		}
	}
	
	/**
	 * Detach from the underlying file descriptor without closing it.
	 * This FileDescriptor and all copies will no longer affect the
	 * underlying file descriptors.
	 *
	 * @return The underlying file descriptor, or -1 if already closed.
	 * @post *this == -1
	 */
	int detach() {
		if (data != NULL) {
			int fd = data->fd;
			data->detach();
			data.reset();
			return fd;
		} else {
			return -1;
		}
	}
	
	/**
	 * Overloads the integer cast operator so that it will return the underlying
	 * file descriptor handle as an integer.
	 *
	 * Returns -1 if FileDescriptor::close() was called.
	 */
	operator int () const {
		if (data == NULL) {
			return -1;
		} else {
			return data->fd;
		}
	}
	
	FileDescriptor &operator=(int fd) {
		/* Make sure that the 'new' and 'delete' operators don't
		 * overwrite errno so that we can write code like this:
		 *
		 *    FileDescriptor fd;
		 *    fd = open(...);
		 *    if (fd == -1) {
		 *       print_error(errno);
		 *    }
		 */
		int e = errno;
		if (fd >= 0) {
			data = make_shared<SharedData>(fd);
		} else {
			data.reset();
		}
		errno = e;
		return *this;
	}
	
	FileDescriptor &operator=(const FileDescriptor &other) {
		/* Make sure that the 'delete' operator implicitly invoked by
		 * shared_ptr doesn't overwrite errno so that we can write code
		 * like this:
		 *
		 *    FileDescriptor fd;
		 *    fd = other_file_descriptor_object;
		 *    if (fd == -1) {
		 *       print_error(errno);
		 *    }
		 */
		int e = errno;
		data = other.data;
		errno = e;
		return *this;
	}
};

/**
 * A structure containing two FileDescriptor objects. Behaves like a pair
 * and like a two-element array.
 */
class FileDescriptorPair: public pair<FileDescriptor, FileDescriptor> {
public:
	FileDescriptorPair() { }
	
	FileDescriptorPair(const FileDescriptor &a, const FileDescriptor &b)
		: pair<FileDescriptor, FileDescriptor>(a, b)
		{ }
	
	FileDescriptor &operator[](int index) {
		if (index == 0) {
			return first;
		} else if (index == 1) {
			return second;
		} else {
			throw ArgumentException("Index must be either 0 of 1");
		}
	}
};

// Convenience aliases.
typedef FileDescriptorPair Pipe;
typedef FileDescriptorPair SocketPair;

/**
 * A synchronization mechanism that's implemented with file descriptors,
 * and as such can be used in combination with select() and friends.
 *
 * One can wait for an event on an EventFd by select()ing it on read events.
 * Another thread can signal the EventFd by calling notify().
 */
class EventFd {
private:
	int reader;
	int writer;
	
public:
	EventFd() {
		int fds[2];
		
		if (syscalls::pipe(fds) == -1) {
			int e = errno;
			throw SystemException("Cannot create a pipe", e);
		}
		reader = fds[0];
		writer = fds[1];
	}
	
	~EventFd() {
		this_thread::disable_syscall_interruption dsi;
		syscalls::close(reader);
		syscalls::close(writer);
	}
	
	void notify() {
		ssize_t ret = syscalls::write(writer, "x", 1);
		if (ret == -1 && errno != EAGAIN) {
			int e = errno;
			throw SystemException("Cannot write notification data", e);
		}
	}
	
	int fd() const {
		return reader;
	}
};

} // namespace Passenger

#endif /* _PASSENGER_FILE_DESCRIPTOR_H_ */