00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _PASSENGER_MESSAGE_CHANNEL_H_
00019 #define _PASSENGER_MESSAGE_CHANNEL_H_
00020
00021 #include <algorithm>
00022 #include <string>
00023 #include <list>
00024 #include <vector>
00025
00026 #include <sys/types.h>
00027 #include <sys/socket.h>
00028 #include <arpa/inet.h>
00029 #include <errno.h>
00030 #include <unistd.h>
00031 #include <cstdarg>
00032
00033 #include "Exceptions.h"
00034 #include "Utils.h"
00035
00036 namespace Passenger {
00037
00038 using namespace std;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 class MessageChannel {
00096 private:
00097 const static char DELIMITER = '\0';
00098 int fd;
00099
00100 public:
00101
00102
00103
00104
00105
00106
00107 MessageChannel() {
00108 this->fd = -1;
00109 }
00110
00111
00112
00113
00114 MessageChannel(int fd) {
00115 this->fd = fd;
00116 }
00117
00118
00119
00120
00121
00122 void close() {
00123 if (fd != -1) {
00124 ::close(fd);
00125 fd = -1;
00126 }
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 void write(const list<string> &args) {
00139 list<string>::const_iterator it;
00140 string data;
00141 uint16_t dataSize = 0;
00142
00143 for (it = args.begin(); it != args.end(); it++) {
00144 dataSize += it->size() + 1;
00145 }
00146 data.reserve(dataSize + sizeof(dataSize));
00147 dataSize = htons(dataSize);
00148 data.append((const char *) &dataSize, sizeof(dataSize));
00149 for (it = args.begin(); it != args.end(); it++) {
00150 data.append(*it);
00151 data.append(1, DELIMITER);
00152 }
00153
00154 writeRaw(data);
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 void write(const char *name, ...) {
00169 list<string> args;
00170 args.push_back(name);
00171
00172 va_list ap;
00173 va_start(ap, name);
00174 while (true) {
00175 const char *arg = va_arg(ap, const char *);
00176 if (arg == NULL) {
00177 break;
00178 } else {
00179 args.push_back(arg);
00180 }
00181 }
00182 va_end(ap);
00183 write(args);
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193 void writeScalar(const string &str) {
00194 writeScalar(str.c_str(), str.size());
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 void writeScalar(const char *data, unsigned int size) {
00207 uint32_t l = htonl(size);
00208 writeRaw((const char *) &l, sizeof(uint32_t));
00209 writeRaw(data, size);
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 void writeRaw(const char *data, unsigned int size) {
00223 ssize_t ret;
00224 unsigned int written = 0;
00225 do {
00226 do {
00227 ret = ::write(fd, data + written, size - written);
00228 } while (ret == -1 && errno == EINTR);
00229 if (ret == -1) {
00230 throw SystemException("write() failed", errno);
00231 } else {
00232 written += ret;
00233 }
00234 } while (written < size);
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 void writeRaw(const string &data) {
00246 writeRaw(data.c_str(), data.size());
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 void writeFileDescriptor(int fileDescriptor) {
00259 struct msghdr msg;
00260 struct iovec vec;
00261 char dummy[1];
00262 #ifdef __APPLE__
00263 struct {
00264 struct cmsghdr header;
00265 int fd;
00266 } control_data;
00267 #else
00268 char control_data[CMSG_SPACE(sizeof(int))];
00269 #endif
00270 struct cmsghdr *control_header;
00271
00272 msg.msg_name = NULL;
00273 msg.msg_namelen = 0;
00274
00275
00276 dummy[0] = '\0';
00277 vec.iov_base = dummy;
00278 vec.iov_len = sizeof(dummy);
00279 msg.msg_iov = &vec;
00280 msg.msg_iovlen = 1;
00281
00282 msg.msg_control = (caddr_t) &control_data;
00283 msg.msg_controllen = sizeof(control_data);
00284 msg.msg_flags = 0;
00285
00286 control_header = CMSG_FIRSTHDR(&msg);
00287 control_header->cmsg_level = SOL_SOCKET;
00288 control_header->cmsg_type = SCM_RIGHTS;
00289 #ifdef __APPLE__
00290 control_header->cmsg_len = sizeof(control_data);
00291 control_data.fd = fileDescriptor;
00292 #else
00293 control_header->cmsg_len = CMSG_LEN(sizeof(int));
00294 memcpy(CMSG_DATA(control_header), &fileDescriptor, sizeof(int));
00295 #endif
00296
00297 if (sendmsg(fd, &msg, 0) == -1) {
00298 throw SystemException("Cannot send file descriptor with sendmsg()", errno);
00299 }
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 bool read(vector<string> &args) {
00312 uint16_t size;
00313 int ret;
00314 unsigned int alreadyRead = 0;
00315
00316 do {
00317 do {
00318 ret = ::read(fd, (char *) &size + alreadyRead, sizeof(size) - alreadyRead);
00319 } while (ret == -1 && errno == EINTR);
00320 if (ret == -1) {
00321 throw SystemException("read() failed", errno);
00322 } else if (ret == 0) {
00323 return false;
00324 }
00325 alreadyRead += ret;
00326 } while (alreadyRead < sizeof(size));
00327 size = ntohs(size);
00328
00329 string buffer;
00330 args.clear();
00331 buffer.reserve(size);
00332 while (buffer.size() < size) {
00333 char tmp[1024 * 8];
00334 do {
00335 ret = ::read(fd, tmp, min(size - buffer.size(), sizeof(tmp)));
00336 } while (ret == -1 && errno == EINTR);
00337 if (ret == -1) {
00338 throw SystemException("read() failed", errno);
00339 } else if (ret == 0) {
00340 return false;
00341 }
00342 buffer.append(tmp, ret);
00343 }
00344
00345 if (!buffer.empty()) {
00346 string::size_type start = 0, pos;
00347 const string &const_buffer(buffer);
00348 while ((pos = const_buffer.find('\0', start)) != string::npos) {
00349 args.push_back(const_buffer.substr(start, pos - start));
00350 start = pos + 1;
00351 }
00352 }
00353 return true;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 bool readScalar(string &output) {
00365 uint32_t size;
00366 unsigned int remaining;
00367
00368 if (!readRaw(&size, sizeof(uint32_t))) {
00369 return false;
00370 }
00371 size = ntohl(size);
00372
00373 output.clear();
00374 output.reserve(size);
00375 remaining = size;
00376 while (remaining > 0) {
00377 char buf[1024 * 32];
00378 unsigned int blockSize = min((unsigned int) sizeof(buf), remaining);
00379
00380 if (!readRaw(buf, blockSize)) {
00381 return false;
00382 }
00383 output.append(buf, blockSize);
00384 remaining -= blockSize;
00385 }
00386 return true;
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 bool readRaw(void *buf, unsigned int size) {
00405 ssize_t ret;
00406 unsigned int alreadyRead = 0;
00407
00408 while (alreadyRead < size) {
00409 do {
00410 ret = ::read(fd, (char *) buf + alreadyRead, size - alreadyRead);
00411 } while (ret == -1 && errno == EINTR);
00412 if (ret == -1) {
00413 throw SystemException("read() failed", errno);
00414 } else if (ret == 0) {
00415 return false;
00416 } else {
00417 alreadyRead += ret;
00418 }
00419 }
00420 return true;
00421 }
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434 int readFileDescriptor() {
00435 struct msghdr msg;
00436 struct iovec vec;
00437 char dummy[1];
00438 #ifdef __APPLE__
00439
00440
00441 struct {
00442 struct cmsghdr header;
00443 int fd;
00444 } control_data;
00445 #define EXPECTED_CMSG_LEN sizeof(control_data)
00446 #else
00447 char control_data[CMSG_SPACE(sizeof(int))];
00448 #define EXPECTED_CMSG_LEN CMSG_LEN(sizeof(int))
00449 #endif
00450 struct cmsghdr *control_header;
00451
00452 msg.msg_name = NULL;
00453 msg.msg_namelen = 0;
00454
00455 dummy[0] = '\0';
00456 vec.iov_base = dummy;
00457 vec.iov_len = sizeof(dummy);
00458 msg.msg_iov = &vec;
00459 msg.msg_iovlen = 1;
00460
00461 msg.msg_control = (caddr_t) &control_data;
00462 msg.msg_controllen = sizeof(control_data);
00463 msg.msg_flags = 0;
00464
00465 if (recvmsg(fd, &msg, 0) == -1) {
00466 throw SystemException("Cannot read file descriptor with recvmsg()", errno);
00467 }
00468
00469 control_header = CMSG_FIRSTHDR(&msg);
00470 if (control_header->cmsg_len != EXPECTED_CMSG_LEN
00471 || control_header->cmsg_level != SOL_SOCKET
00472 || control_header->cmsg_type != SCM_RIGHTS) {
00473 throw IOException("No valid file descriptor received.");
00474 }
00475 #ifdef __APPLE__
00476 return control_data.fd;
00477 #else
00478 return *((int *) CMSG_DATA(control_header));
00479 #endif
00480 }
00481 };
00482
00483 }
00484
00485 #endif