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 {
00260 struct cmsghdr header;
00261 int fd;
00262 } control;
00263
00264 control.header.cmsg_len = sizeof(control);
00265 control.header.cmsg_level = SOL_SOCKET;
00266 control.header.cmsg_type = SCM_RIGHTS;
00267 control.fd = fileDescriptor;
00268
00269 struct msghdr msg;
00270 struct iovec vec;
00271 char dummy[1];
00272
00273 msg.msg_name = NULL;
00274 msg.msg_namelen = 0;
00275
00276
00277 dummy[0] = '\0';
00278 vec.iov_base = dummy;
00279 vec.iov_len = sizeof(dummy);
00280 msg.msg_iov = &vec;
00281 msg.msg_iovlen = 1;
00282
00283 msg.msg_control = (caddr_t) &control;
00284 msg.msg_controllen = sizeof(control);
00285 msg.msg_flags = 0;
00286
00287 if (sendmsg(fd, &msg, 0) == -1) {
00288 throw SystemException("Cannot send file descriptor with sendmsg()", errno);
00289 }
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 bool read(vector<string> &args) {
00302 uint16_t size;
00303 int ret;
00304 unsigned int alreadyRead = 0;
00305
00306 do {
00307 do {
00308 ret = ::read(fd, (char *) &size + alreadyRead, sizeof(size) - alreadyRead);
00309 } while (ret == -1 && errno == EINTR);
00310 if (ret == -1) {
00311 throw SystemException("read() failed", errno);
00312 } else if (ret == 0) {
00313 return false;
00314 }
00315 alreadyRead += ret;
00316 } while (alreadyRead < sizeof(size));
00317 size = ntohs(size);
00318
00319 string buffer;
00320 args.clear();
00321 buffer.reserve(size);
00322 while (buffer.size() < size) {
00323 char tmp[1024 * 8];
00324 do {
00325 ret = ::read(fd, tmp, min(size - buffer.size(), sizeof(tmp)));
00326 } while (ret == -1 && errno == EINTR);
00327 if (ret == -1) {
00328 throw SystemException("read() failed", errno);
00329 } else if (ret == 0) {
00330 return false;
00331 }
00332 buffer.append(tmp, ret);
00333 }
00334
00335 if (!buffer.empty()) {
00336 string::size_type start = 0, pos;
00337 const string &const_buffer(buffer);
00338 while ((pos = const_buffer.find('\0', start)) != string::npos) {
00339 args.push_back(const_buffer.substr(start, pos - start));
00340 start = pos + 1;
00341 }
00342 }
00343 return true;
00344 }
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 bool readScalar(string &output) {
00355 uint32_t size;
00356 unsigned int remaining;
00357
00358 if (!readRaw(&size, sizeof(uint32_t))) {
00359 return false;
00360 }
00361 size = ntohl(size);
00362
00363 output.clear();
00364 output.reserve(size);
00365 remaining = size;
00366 while (remaining > 0) {
00367 char buf[1024 * 32];
00368 unsigned int blockSize = min((unsigned int) sizeof(buf), remaining);
00369
00370 if (!readRaw(buf, blockSize)) {
00371 return false;
00372 }
00373 output.append(buf, blockSize);
00374 remaining -= blockSize;
00375 }
00376 return true;
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 bool readRaw(void *buf, unsigned int size) {
00395 ssize_t ret;
00396 unsigned int alreadyRead = 0;
00397
00398 while (alreadyRead < size) {
00399 do {
00400 ret = ::read(fd, (char *) buf + alreadyRead, size - alreadyRead);
00401 } while (ret == -1 && errno == EINTR);
00402 if (ret == -1) {
00403 throw SystemException("read() failed", errno);
00404 } else if (ret == 0) {
00405 return false;
00406 } else {
00407 alreadyRead += ret;
00408 }
00409 }
00410 return true;
00411 }
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 int readFileDescriptor() {
00425 struct {
00426 struct cmsghdr header;
00427 int fd;
00428 } control;
00429
00430 control.header.cmsg_len = sizeof(control);
00431 control.header.cmsg_level = SOL_SOCKET;
00432 control.header.cmsg_type = SCM_RIGHTS;
00433 control.fd = -1;
00434
00435 struct msghdr msg;
00436 struct iovec vec;
00437 char dummy[1];
00438
00439 msg.msg_name = NULL;
00440 msg.msg_namelen = 0;
00441
00442 dummy[0] = '\0';
00443 vec.iov_base = dummy;
00444 vec.iov_len = sizeof(dummy);
00445 msg.msg_iov = &vec;
00446 msg.msg_iovlen = 1;
00447
00448 msg.msg_control = (caddr_t) &control;
00449 msg.msg_controllen = sizeof(control);
00450 msg.msg_flags = 0;
00451
00452 if (recvmsg(fd, &msg, 0) == -1) {
00453 throw SystemException("Cannot read file descriptor with recvmsg()", errno);
00454 }
00455
00456 if (msg.msg_controllen != sizeof(control)
00457 || control.header.cmsg_len != sizeof(control)
00458 || control.header.cmsg_level != SOL_SOCKET
00459 || control.header.cmsg_type != SCM_RIGHTS) {
00460 throw IOException("No valid file descriptor received.");
00461 }
00462 return control.fd;
00463 }
00464 };
00465
00466 }
00467
00468 #endif