doc/cxxapi/MessageChannel_8h-source.html in passenger-1.0.4 vs doc/cxxapi/MessageChannel_8h-source.html in passenger-1.0.5
- old
+ new
@@ -272,208 +272,234 @@
<a name="l00257"></a>00257 <span class="comment"> */</span>
<a name="l00258"></a><a class="code" href="classPassenger_1_1MessageChannel.html#73e3f3cda384c085a2af0e820ccd3e98">00258</a> <span class="keywordtype">void</span> <a class="code" href="classPassenger_1_1MessageChannel.html#73e3f3cda384c085a2af0e820ccd3e98" title="Pass a file descriptor.">writeFileDescriptor</a>(<span class="keywordtype">int</span> fileDescriptor) {
<a name="l00259"></a>00259 <span class="keyword">struct </span>msghdr msg;
<a name="l00260"></a>00260 <span class="keyword">struct </span>iovec vec;
<a name="l00261"></a>00261 <span class="keywordtype">char</span> dummy[1];
-<a name="l00262"></a>00262 <span class="keywordtype">char</span> control_data[CMSG_SPACE(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>))];
-<a name="l00263"></a>00263 <span class="keyword">struct </span>cmsghdr *control_header;
-<a name="l00264"></a>00264
-<a name="l00265"></a>00265 msg.msg_name = NULL;
-<a name="l00266"></a>00266 msg.msg_namelen = 0;
-<a name="l00267"></a>00267
-<a name="l00268"></a>00268 <span class="comment">/* Linux and Solaris require msg_iov to be non-NULL. */</span>
-<a name="l00269"></a>00269 dummy[0] = <span class="charliteral">'\0'</span>;
-<a name="l00270"></a>00270 vec.iov_base = dummy;
-<a name="l00271"></a>00271 vec.iov_len = <span class="keyword">sizeof</span>(dummy);
-<a name="l00272"></a>00272 msg.msg_iov = &vec;
-<a name="l00273"></a>00273 msg.msg_iovlen = 1;
+<a name="l00262"></a>00262 <span class="preprocessor"> #ifdef __APPLE__</span>
+<a name="l00263"></a>00263 <span class="preprocessor"></span> <span class="keyword">struct </span>{
+<a name="l00264"></a>00264 <span class="keyword">struct </span>cmsghdr header;
+<a name="l00265"></a>00265 <span class="keywordtype">int</span> fd;
+<a name="l00266"></a>00266 } control_data;
+<a name="l00267"></a>00267 <span class="preprocessor"> #else</span>
+<a name="l00268"></a>00268 <span class="preprocessor"></span> <span class="keywordtype">char</span> control_data[CMSG_SPACE(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>))];
+<a name="l00269"></a>00269 <span class="preprocessor"> #endif</span>
+<a name="l00270"></a>00270 <span class="preprocessor"></span> <span class="keyword">struct </span>cmsghdr *control_header;
+<a name="l00271"></a>00271
+<a name="l00272"></a>00272 msg.msg_name = NULL;
+<a name="l00273"></a>00273 msg.msg_namelen = 0;
<a name="l00274"></a>00274
-<a name="l00275"></a>00275 msg.msg_control = (caddr_t) control_data;
-<a name="l00276"></a>00276 msg.msg_controllen = <span class="keyword">sizeof</span>(control_data);
-<a name="l00277"></a>00277 msg.msg_flags = 0;
-<a name="l00278"></a>00278
-<a name="l00279"></a>00279 control_header = CMSG_FIRSTHDR(&msg);
-<a name="l00280"></a>00280 control_header->cmsg_len = CMSG_LEN(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>));
-<a name="l00281"></a>00281 control_header->cmsg_level = SOL_SOCKET;
-<a name="l00282"></a>00282 control_header->cmsg_type = SCM_RIGHTS;
-<a name="l00283"></a>00283 memcpy(CMSG_DATA(control_header), &fileDescriptor, <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>));
-<a name="l00284"></a>00284
-<a name="l00285"></a>00285 <span class="keywordflow">if</span> (sendmsg(fd, &msg, 0) == -1) {
-<a name="l00286"></a>00286 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"Cannot send file descriptor with sendmsg()"</span>, errno);
-<a name="l00287"></a>00287 }
-<a name="l00288"></a>00288 }
-<a name="l00289"></a>00289 <span class="comment"></span>
-<a name="l00290"></a>00290 <span class="comment"> /**</span>
-<a name="l00291"></a>00291 <span class="comment"> * Read an array message from the underlying file descriptor.</span>
-<a name="l00292"></a>00292 <span class="comment"> *</span>
-<a name="l00293"></a>00293 <span class="comment"> * @param args The message will be put in this variable.</span>
-<a name="l00294"></a>00294 <span class="comment"> * @return Whether end-of-file has been reached. If so, then the contents</span>
-<a name="l00295"></a>00295 <span class="comment"> * of <tt>args</tt> will be undefined.</span>
-<a name="l00296"></a>00296 <span class="comment"> * @throws SystemException If an error occured while receiving the message.</span>
-<a name="l00297"></a>00297 <span class="comment"> * @see write()</span>
-<a name="l00298"></a>00298 <span class="comment"> */</span>
-<a name="l00299"></a><a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e">00299</a> <span class="keywordtype">bool</span> <a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(vector<string> &args) {
-<a name="l00300"></a>00300 uint16_t size;
-<a name="l00301"></a>00301 <span class="keywordtype">int</span> ret;
-<a name="l00302"></a>00302 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> alreadyRead = 0;
-<a name="l00303"></a>00303
-<a name="l00304"></a>00304 <span class="keywordflow">do</span> {
-<a name="l00305"></a>00305 <span class="keywordflow">do</span> {
-<a name="l00306"></a>00306 ret =<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor."> ::read</a>(fd, (<span class="keywordtype">char</span> *) &size + alreadyRead, <span class="keyword">sizeof</span>(size) - alreadyRead);
-<a name="l00307"></a>00307 } <span class="keywordflow">while</span> (ret == -1 && errno == EINTR);
-<a name="l00308"></a>00308 <span class="keywordflow">if</span> (ret == -1) {
-<a name="l00309"></a>00309 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"read() failed"</span>, errno);
-<a name="l00310"></a>00310 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret == 0) {
-<a name="l00311"></a>00311 <span class="keywordflow">return</span> <span class="keyword">false</span>;
-<a name="l00312"></a>00312 }
-<a name="l00313"></a>00313 alreadyRead += ret;
-<a name="l00314"></a>00314 } <span class="keywordflow">while</span> (alreadyRead < <span class="keyword">sizeof</span>(size));
-<a name="l00315"></a>00315 size = ntohs(size);
-<a name="l00316"></a>00316
-<a name="l00317"></a>00317 <span class="keywordtype">string</span> buffer;
-<a name="l00318"></a>00318 args.clear();
-<a name="l00319"></a>00319 buffer.reserve(size);
-<a name="l00320"></a>00320 <span class="keywordflow">while</span> (buffer.size() < size) {
-<a name="l00321"></a>00321 <span class="keywordtype">char</span> tmp[1024 * 8];
-<a name="l00322"></a>00322 <span class="keywordflow">do</span> {
-<a name="l00323"></a>00323 ret =<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor."> ::read</a>(fd, tmp, min(size - buffer.size(), <span class="keyword">sizeof</span>(tmp)));
-<a name="l00324"></a>00324 } <span class="keywordflow">while</span> (ret == -1 && errno == EINTR);
-<a name="l00325"></a>00325 <span class="keywordflow">if</span> (ret == -1) {
-<a name="l00326"></a>00326 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"read() failed"</span>, errno);
-<a name="l00327"></a>00327 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret == 0) {
-<a name="l00328"></a>00328 <span class="keywordflow">return</span> <span class="keyword">false</span>;
-<a name="l00329"></a>00329 }
-<a name="l00330"></a>00330 buffer.append(tmp, ret);
-<a name="l00331"></a>00331 }
-<a name="l00332"></a>00332
-<a name="l00333"></a>00333 <span class="keywordflow">if</span> (!buffer.empty()) {
-<a name="l00334"></a>00334 string::size_type start = 0, pos;
-<a name="l00335"></a>00335 <span class="keyword">const</span> <span class="keywordtype">string</span> &const_buffer(buffer);
-<a name="l00336"></a>00336 <span class="keywordflow">while</span> ((pos = const_buffer.find(<span class="charliteral">'\0'</span>, start)) != string::npos) {
-<a name="l00337"></a>00337 args.push_back(const_buffer.substr(start, pos - start));
-<a name="l00338"></a>00338 start = pos + 1;
-<a name="l00339"></a>00339 }
-<a name="l00340"></a>00340 }
-<a name="l00341"></a>00341 <span class="keywordflow">return</span> <span class="keyword">true</span>;
-<a name="l00342"></a>00342 }
-<a name="l00343"></a>00343 <span class="comment"></span>
-<a name="l00344"></a>00344 <span class="comment"> /**</span>
-<a name="l00345"></a>00345 <span class="comment"> * Read a scalar message from the underlying file descriptor.</span>
-<a name="l00346"></a>00346 <span class="comment"> *</span>
-<a name="l00347"></a>00347 <span class="comment"> * @param output The message will be put in here.</span>
-<a name="l00348"></a>00348 <span class="comment"> * @returns Whether end-of-file was reached during reading.</span>
-<a name="l00349"></a>00349 <span class="comment"> * @throws SystemException An error occured while writing the data to the file descriptor.</span>
-<a name="l00350"></a>00350 <span class="comment"> * @see writeScalar()</span>
-<a name="l00351"></a>00351 <span class="comment"> */</span>
-<a name="l00352"></a><a class="code" href="classPassenger_1_1MessageChannel.html#4ce6a0e751b5e3563bee583c231569bc">00352</a> <span class="keywordtype">bool</span> <a class="code" href="classPassenger_1_1MessageChannel.html#4ce6a0e751b5e3563bee583c231569bc" title="Read a scalar message from the underlying file descriptor.">readScalar</a>(<span class="keywordtype">string</span> &output) {
-<a name="l00353"></a>00353 uint32_t size;
-<a name="l00354"></a>00354 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> remaining;
-<a name="l00355"></a>00355
-<a name="l00356"></a>00356 <span class="keywordflow">if</span> (!<a class="code" href="classPassenger_1_1MessageChannel.html#cd1d1bd1cc787784c8b54f5471fff479" title="Read exactly size bytes of data from the underlying file descriptor, and put the...">readRaw</a>(&size, <span class="keyword">sizeof</span>(uint32_t))) {
-<a name="l00357"></a>00357 <span class="keywordflow">return</span> <span class="keyword">false</span>;
-<a name="l00358"></a>00358 }
-<a name="l00359"></a>00359 size = ntohl(size);
-<a name="l00360"></a>00360
-<a name="l00361"></a>00361 output.clear();
-<a name="l00362"></a>00362 output.reserve(size);
-<a name="l00363"></a>00363 remaining = size;
-<a name="l00364"></a>00364 <span class="keywordflow">while</span> (remaining > 0) {
-<a name="l00365"></a>00365 <span class="keywordtype">char</span> buf[1024 * 32];
-<a name="l00366"></a>00366 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> blockSize = min((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>) <span class="keyword">sizeof</span>(buf), remaining);
-<a name="l00367"></a>00367
-<a name="l00368"></a>00368 <span class="keywordflow">if</span> (!<a class="code" href="classPassenger_1_1MessageChannel.html#cd1d1bd1cc787784c8b54f5471fff479" title="Read exactly size bytes of data from the underlying file descriptor, and put the...">readRaw</a>(buf, blockSize)) {
-<a name="l00369"></a>00369 <span class="keywordflow">return</span> <span class="keyword">false</span>;
-<a name="l00370"></a>00370 }
-<a name="l00371"></a>00371 output.append(buf, blockSize);
-<a name="l00372"></a>00372 remaining -= blockSize;
-<a name="l00373"></a>00373 }
-<a name="l00374"></a>00374 <span class="keywordflow">return</span> <span class="keyword">true</span>;
-<a name="l00375"></a>00375 }
-<a name="l00376"></a>00376 <span class="comment"></span>
-<a name="l00377"></a>00377 <span class="comment"> /**</span>
-<a name="l00378"></a>00378 <span class="comment"> * Read exactly <tt>size</tt> bytes of data from the underlying file descriptor,</span>
-<a name="l00379"></a>00379 <span class="comment"> * and put the result in <tt>buf</tt>. If end-of-file has been reached, or if</span>
-<a name="l00380"></a>00380 <span class="comment"> * end-of-file was encountered before <tt>size</tt> bytes have been read, then</span>
-<a name="l00381"></a>00381 <span class="comment"> * <tt>false</tt> will be returned. Otherwise (i.e. if the read was successful),</span>
-<a name="l00382"></a>00382 <span class="comment"> * <tt>true</tt> will be returned.</span>
-<a name="l00383"></a>00383 <span class="comment"> *</span>
-<a name="l00384"></a>00384 <span class="comment"> * @param buf The buffer to place the read data in. This buffer must be at least</span>
-<a name="l00385"></a>00385 <span class="comment"> * <tt>size</tt> bytes long.</span>
-<a name="l00386"></a>00386 <span class="comment"> * @param size The number of bytes to read.</span>
-<a name="l00387"></a>00387 <span class="comment"> * @return Whether reading was successful or whether EOF was reached.</span>
-<a name="l00388"></a>00388 <span class="comment"> * @pre buf != NULL</span>
-<a name="l00389"></a>00389 <span class="comment"> * @throws SystemException Something went wrong during reading.</span>
-<a name="l00390"></a>00390 <span class="comment"> * @see writeRaw()</span>
-<a name="l00391"></a>00391 <span class="comment"> */</span>
-<a name="l00392"></a><a class="code" href="classPassenger_1_1MessageChannel.html#cd1d1bd1cc787784c8b54f5471fff479">00392</a> <span class="keywordtype">bool</span> <a class="code" href="classPassenger_1_1MessageChannel.html#cd1d1bd1cc787784c8b54f5471fff479" title="Read exactly size bytes of data from the underlying file descriptor, and put the...">readRaw</a>(<span class="keywordtype">void</span> *buf, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size) {
-<a name="l00393"></a>00393 ssize_t ret;
-<a name="l00394"></a>00394 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> alreadyRead = 0;
-<a name="l00395"></a>00395
-<a name="l00396"></a>00396 <span class="keywordflow">while</span> (alreadyRead < size) {
-<a name="l00397"></a>00397 <span class="keywordflow">do</span> {
-<a name="l00398"></a>00398 ret =<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor."> ::read</a>(fd, (<span class="keywordtype">char</span> *) buf + alreadyRead, size - alreadyRead);
-<a name="l00399"></a>00399 } <span class="keywordflow">while</span> (ret == -1 && errno == EINTR);
-<a name="l00400"></a>00400 <span class="keywordflow">if</span> (ret == -1) {
-<a name="l00401"></a>00401 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"read() failed"</span>, errno);
-<a name="l00402"></a>00402 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret == 0) {
-<a name="l00403"></a>00403 <span class="keywordflow">return</span> <span class="keyword">false</span>;
-<a name="l00404"></a>00404 } <span class="keywordflow">else</span> {
-<a name="l00405"></a>00405 alreadyRead += ret;
-<a name="l00406"></a>00406 }
-<a name="l00407"></a>00407 }
-<a name="l00408"></a>00408 <span class="keywordflow">return</span> <span class="keyword">true</span>;
-<a name="l00409"></a>00409 }
-<a name="l00410"></a>00410 <span class="comment"></span>
-<a name="l00411"></a>00411 <span class="comment"> /**</span>
-<a name="l00412"></a>00412 <span class="comment"> * Receive a file descriptor, which had been passed over the underlying</span>
-<a name="l00413"></a>00413 <span class="comment"> * file descriptor.</span>
-<a name="l00414"></a>00414 <span class="comment"> *</span>
-<a name="l00415"></a>00415 <span class="comment"> * @return The passed file descriptor.</span>
-<a name="l00416"></a>00416 <span class="comment"> * @throws SystemException If something went wrong during the</span>
-<a name="l00417"></a>00417 <span class="comment"> * receiving of a file descriptor. Perhaps the underlying</span>
-<a name="l00418"></a>00418 <span class="comment"> * file descriptor isn't a Unix socket.</span>
-<a name="l00419"></a>00419 <span class="comment"> * @throws IOException Whatever was received doesn't seem to be a</span>
-<a name="l00420"></a>00420 <span class="comment"> * file descriptor.</span>
-<a name="l00421"></a>00421 <span class="comment"> */</span>
-<a name="l00422"></a><a class="code" href="classPassenger_1_1MessageChannel.html#1561b7e4a0f4d39ea431f456e5655488">00422</a> <span class="keywordtype">int</span> <a class="code" href="classPassenger_1_1MessageChannel.html#1561b7e4a0f4d39ea431f456e5655488" title="Receive a file descriptor, which had been passed over the underlying file descriptor...">readFileDescriptor</a>() {
-<a name="l00423"></a>00423 <span class="keyword">struct </span>msghdr msg;
-<a name="l00424"></a>00424 <span class="keyword">struct </span>iovec vec;
-<a name="l00425"></a>00425 <span class="keywordtype">char</span> dummy[1];
-<a name="l00426"></a>00426 <span class="keywordtype">char</span> control_data[CMSG_SPACE(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>))];
-<a name="l00427"></a>00427 <span class="keyword">struct </span>cmsghdr *control_header;
-<a name="l00428"></a>00428
-<a name="l00429"></a>00429 msg.msg_name = NULL;
-<a name="l00430"></a>00430 msg.msg_namelen = 0;
-<a name="l00431"></a>00431
-<a name="l00432"></a>00432 dummy[0] = <span class="charliteral">'\0'</span>;
-<a name="l00433"></a>00433 vec.iov_base = dummy;
-<a name="l00434"></a>00434 vec.iov_len = <span class="keyword">sizeof</span>(dummy);
-<a name="l00435"></a>00435 msg.msg_iov = &vec;
-<a name="l00436"></a>00436 msg.msg_iovlen = 1;
-<a name="l00437"></a>00437
-<a name="l00438"></a>00438 msg.msg_control = (caddr_t) control_data;
-<a name="l00439"></a>00439 msg.msg_controllen = <span class="keyword">sizeof</span>(control_data);
-<a name="l00440"></a>00440 msg.msg_flags = 0;
-<a name="l00441"></a>00441
-<a name="l00442"></a>00442 <span class="keywordflow">if</span> (recvmsg(fd, &msg, 0) == -1) {
-<a name="l00443"></a>00443 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"Cannot read file descriptor with recvmsg()"</span>, errno);
-<a name="l00444"></a>00444 }
-<a name="l00445"></a>00445
-<a name="l00446"></a>00446 control_header = CMSG_FIRSTHDR(&msg);
-<a name="l00447"></a>00447 <span class="keywordflow">if</span> (msg.msg_controllen != <span class="keyword">sizeof</span>(control_data)
-<a name="l00448"></a>00448 || control_header->cmsg_len != CMSG_LEN(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>))
-<a name="l00449"></a>00449 || control_header->cmsg_level != SOL_SOCKET
-<a name="l00450"></a>00450 || control_header->cmsg_type != SCM_RIGHTS) {
-<a name="l00451"></a>00451 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1IOException.html" title="Represents an error that occured during an I/O operation.">IOException</a>(<span class="stringliteral">"No valid file descriptor received."</span>);
-<a name="l00452"></a>00452 }
-<a name="l00453"></a>00453 <span class="keywordflow">return</span> *((<span class="keywordtype">int</span> *) CMSG_DATA(control_header));
-<a name="l00454"></a>00454 }
-<a name="l00455"></a>00455 };
-<a name="l00456"></a>00456
-<a name="l00457"></a>00457 } <span class="comment">// namespace Passenger</span>
-<a name="l00458"></a>00458
-<a name="l00459"></a>00459 <span class="preprocessor">#endif </span><span class="comment">/* _PASSENGER_MESSAGE_CHANNEL_H_ */</span>
-</pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Thu May 1 20:23:46 2008 for Passenger by
+<a name="l00275"></a>00275 <span class="comment">/* Linux and Solaris require msg_iov to be non-NULL. */</span>
+<a name="l00276"></a>00276 dummy[0] = <span class="charliteral">'\0'</span>;
+<a name="l00277"></a>00277 vec.iov_base = dummy;
+<a name="l00278"></a>00278 vec.iov_len = <span class="keyword">sizeof</span>(dummy);
+<a name="l00279"></a>00279 msg.msg_iov = &vec;
+<a name="l00280"></a>00280 msg.msg_iovlen = 1;
+<a name="l00281"></a>00281
+<a name="l00282"></a>00282 msg.msg_control = (caddr_t) &control_data;
+<a name="l00283"></a>00283 msg.msg_controllen = <span class="keyword">sizeof</span>(control_data);
+<a name="l00284"></a>00284 msg.msg_flags = 0;
+<a name="l00285"></a>00285
+<a name="l00286"></a>00286 control_header = CMSG_FIRSTHDR(&msg);
+<a name="l00287"></a>00287 control_header->cmsg_level = SOL_SOCKET;
+<a name="l00288"></a>00288 control_header->cmsg_type = SCM_RIGHTS;
+<a name="l00289"></a>00289 <span class="preprocessor"> #ifdef __APPLE__</span>
+<a name="l00290"></a>00290 <span class="preprocessor"></span> control_header->cmsg_len = <span class="keyword">sizeof</span>(control_data);
+<a name="l00291"></a>00291 control_data.fd = fileDescriptor;
+<a name="l00292"></a>00292 <span class="preprocessor"> #else</span>
+<a name="l00293"></a>00293 <span class="preprocessor"></span> control_header->cmsg_len = CMSG_LEN(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>));
+<a name="l00294"></a>00294 memcpy(CMSG_DATA(control_header), &fileDescriptor, <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>));
+<a name="l00295"></a>00295 <span class="preprocessor"> #endif</span>
+<a name="l00296"></a>00296 <span class="preprocessor"></span>
+<a name="l00297"></a>00297 <span class="keywordflow">if</span> (sendmsg(fd, &msg, 0) == -1) {
+<a name="l00298"></a>00298 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"Cannot send file descriptor with sendmsg()"</span>, errno);
+<a name="l00299"></a>00299 }
+<a name="l00300"></a>00300 }
+<a name="l00301"></a>00301 <span class="comment"></span>
+<a name="l00302"></a>00302 <span class="comment"> /**</span>
+<a name="l00303"></a>00303 <span class="comment"> * Read an array message from the underlying file descriptor.</span>
+<a name="l00304"></a>00304 <span class="comment"> *</span>
+<a name="l00305"></a>00305 <span class="comment"> * @param args The message will be put in this variable.</span>
+<a name="l00306"></a>00306 <span class="comment"> * @return Whether end-of-file has been reached. If so, then the contents</span>
+<a name="l00307"></a>00307 <span class="comment"> * of <tt>args</tt> will be undefined.</span>
+<a name="l00308"></a>00308 <span class="comment"> * @throws SystemException If an error occured while receiving the message.</span>
+<a name="l00309"></a>00309 <span class="comment"> * @see write()</span>
+<a name="l00310"></a>00310 <span class="comment"> */</span>
+<a name="l00311"></a><a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e">00311</a> <span class="keywordtype">bool</span> <a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(vector<string> &args) {
+<a name="l00312"></a>00312 uint16_t size;
+<a name="l00313"></a>00313 <span class="keywordtype">int</span> ret;
+<a name="l00314"></a>00314 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> alreadyRead = 0;
+<a name="l00315"></a>00315
+<a name="l00316"></a>00316 <span class="keywordflow">do</span> {
+<a name="l00317"></a>00317 <span class="keywordflow">do</span> {
+<a name="l00318"></a>00318 ret =<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor."> ::read</a>(fd, (<span class="keywordtype">char</span> *) &size + alreadyRead, <span class="keyword">sizeof</span>(size) - alreadyRead);
+<a name="l00319"></a>00319 } <span class="keywordflow">while</span> (ret == -1 && errno == EINTR);
+<a name="l00320"></a>00320 <span class="keywordflow">if</span> (ret == -1) {
+<a name="l00321"></a>00321 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"read() failed"</span>, errno);
+<a name="l00322"></a>00322 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret == 0) {
+<a name="l00323"></a>00323 <span class="keywordflow">return</span> <span class="keyword">false</span>;
+<a name="l00324"></a>00324 }
+<a name="l00325"></a>00325 alreadyRead += ret;
+<a name="l00326"></a>00326 } <span class="keywordflow">while</span> (alreadyRead < <span class="keyword">sizeof</span>(size));
+<a name="l00327"></a>00327 size = ntohs(size);
+<a name="l00328"></a>00328
+<a name="l00329"></a>00329 <span class="keywordtype">string</span> buffer;
+<a name="l00330"></a>00330 args.clear();
+<a name="l00331"></a>00331 buffer.reserve(size);
+<a name="l00332"></a>00332 <span class="keywordflow">while</span> (buffer.size() < size) {
+<a name="l00333"></a>00333 <span class="keywordtype">char</span> tmp[1024 * 8];
+<a name="l00334"></a>00334 <span class="keywordflow">do</span> {
+<a name="l00335"></a>00335 ret =<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor."> ::read</a>(fd, tmp, min(size - buffer.size(), <span class="keyword">sizeof</span>(tmp)));
+<a name="l00336"></a>00336 } <span class="keywordflow">while</span> (ret == -1 && errno == EINTR);
+<a name="l00337"></a>00337 <span class="keywordflow">if</span> (ret == -1) {
+<a name="l00338"></a>00338 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"read() failed"</span>, errno);
+<a name="l00339"></a>00339 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret == 0) {
+<a name="l00340"></a>00340 <span class="keywordflow">return</span> <span class="keyword">false</span>;
+<a name="l00341"></a>00341 }
+<a name="l00342"></a>00342 buffer.append(tmp, ret);
+<a name="l00343"></a>00343 }
+<a name="l00344"></a>00344
+<a name="l00345"></a>00345 <span class="keywordflow">if</span> (!buffer.empty()) {
+<a name="l00346"></a>00346 string::size_type start = 0, pos;
+<a name="l00347"></a>00347 <span class="keyword">const</span> <span class="keywordtype">string</span> &const_buffer(buffer);
+<a name="l00348"></a>00348 <span class="keywordflow">while</span> ((pos = const_buffer.find(<span class="charliteral">'\0'</span>, start)) != string::npos) {
+<a name="l00349"></a>00349 args.push_back(const_buffer.substr(start, pos - start));
+<a name="l00350"></a>00350 start = pos + 1;
+<a name="l00351"></a>00351 }
+<a name="l00352"></a>00352 }
+<a name="l00353"></a>00353 <span class="keywordflow">return</span> <span class="keyword">true</span>;
+<a name="l00354"></a>00354 }
+<a name="l00355"></a>00355 <span class="comment"></span>
+<a name="l00356"></a>00356 <span class="comment"> /**</span>
+<a name="l00357"></a>00357 <span class="comment"> * Read a scalar message from the underlying file descriptor.</span>
+<a name="l00358"></a>00358 <span class="comment"> *</span>
+<a name="l00359"></a>00359 <span class="comment"> * @param output The message will be put in here.</span>
+<a name="l00360"></a>00360 <span class="comment"> * @returns Whether end-of-file was reached during reading.</span>
+<a name="l00361"></a>00361 <span class="comment"> * @throws SystemException An error occured while writing the data to the file descriptor.</span>
+<a name="l00362"></a>00362 <span class="comment"> * @see writeScalar()</span>
+<a name="l00363"></a>00363 <span class="comment"> */</span>
+<a name="l00364"></a><a class="code" href="classPassenger_1_1MessageChannel.html#4ce6a0e751b5e3563bee583c231569bc">00364</a> <span class="keywordtype">bool</span> <a class="code" href="classPassenger_1_1MessageChannel.html#4ce6a0e751b5e3563bee583c231569bc" title="Read a scalar message from the underlying file descriptor.">readScalar</a>(<span class="keywordtype">string</span> &output) {
+<a name="l00365"></a>00365 uint32_t size;
+<a name="l00366"></a>00366 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> remaining;
+<a name="l00367"></a>00367
+<a name="l00368"></a>00368 <span class="keywordflow">if</span> (!<a class="code" href="classPassenger_1_1MessageChannel.html#cd1d1bd1cc787784c8b54f5471fff479" title="Read exactly size bytes of data from the underlying file descriptor, and put the...">readRaw</a>(&size, <span class="keyword">sizeof</span>(uint32_t))) {
+<a name="l00369"></a>00369 <span class="keywordflow">return</span> <span class="keyword">false</span>;
+<a name="l00370"></a>00370 }
+<a name="l00371"></a>00371 size = ntohl(size);
+<a name="l00372"></a>00372
+<a name="l00373"></a>00373 output.clear();
+<a name="l00374"></a>00374 output.reserve(size);
+<a name="l00375"></a>00375 remaining = size;
+<a name="l00376"></a>00376 <span class="keywordflow">while</span> (remaining > 0) {
+<a name="l00377"></a>00377 <span class="keywordtype">char</span> buf[1024 * 32];
+<a name="l00378"></a>00378 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> blockSize = min((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>) <span class="keyword">sizeof</span>(buf), remaining);
+<a name="l00379"></a>00379
+<a name="l00380"></a>00380 <span class="keywordflow">if</span> (!<a class="code" href="classPassenger_1_1MessageChannel.html#cd1d1bd1cc787784c8b54f5471fff479" title="Read exactly size bytes of data from the underlying file descriptor, and put the...">readRaw</a>(buf, blockSize)) {
+<a name="l00381"></a>00381 <span class="keywordflow">return</span> <span class="keyword">false</span>;
+<a name="l00382"></a>00382 }
+<a name="l00383"></a>00383 output.append(buf, blockSize);
+<a name="l00384"></a>00384 remaining -= blockSize;
+<a name="l00385"></a>00385 }
+<a name="l00386"></a>00386 <span class="keywordflow">return</span> <span class="keyword">true</span>;
+<a name="l00387"></a>00387 }
+<a name="l00388"></a>00388 <span class="comment"></span>
+<a name="l00389"></a>00389 <span class="comment"> /**</span>
+<a name="l00390"></a>00390 <span class="comment"> * Read exactly <tt>size</tt> bytes of data from the underlying file descriptor,</span>
+<a name="l00391"></a>00391 <span class="comment"> * and put the result in <tt>buf</tt>. If end-of-file has been reached, or if</span>
+<a name="l00392"></a>00392 <span class="comment"> * end-of-file was encountered before <tt>size</tt> bytes have been read, then</span>
+<a name="l00393"></a>00393 <span class="comment"> * <tt>false</tt> will be returned. Otherwise (i.e. if the read was successful),</span>
+<a name="l00394"></a>00394 <span class="comment"> * <tt>true</tt> will be returned.</span>
+<a name="l00395"></a>00395 <span class="comment"> *</span>
+<a name="l00396"></a>00396 <span class="comment"> * @param buf The buffer to place the read data in. This buffer must be at least</span>
+<a name="l00397"></a>00397 <span class="comment"> * <tt>size</tt> bytes long.</span>
+<a name="l00398"></a>00398 <span class="comment"> * @param size The number of bytes to read.</span>
+<a name="l00399"></a>00399 <span class="comment"> * @return Whether reading was successful or whether EOF was reached.</span>
+<a name="l00400"></a>00400 <span class="comment"> * @pre buf != NULL</span>
+<a name="l00401"></a>00401 <span class="comment"> * @throws SystemException Something went wrong during reading.</span>
+<a name="l00402"></a>00402 <span class="comment"> * @see writeRaw()</span>
+<a name="l00403"></a>00403 <span class="comment"> */</span>
+<a name="l00404"></a><a class="code" href="classPassenger_1_1MessageChannel.html#cd1d1bd1cc787784c8b54f5471fff479">00404</a> <span class="keywordtype">bool</span> <a class="code" href="classPassenger_1_1MessageChannel.html#cd1d1bd1cc787784c8b54f5471fff479" title="Read exactly size bytes of data from the underlying file descriptor, and put the...">readRaw</a>(<span class="keywordtype">void</span> *buf, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size) {
+<a name="l00405"></a>00405 ssize_t ret;
+<a name="l00406"></a>00406 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> alreadyRead = 0;
+<a name="l00407"></a>00407
+<a name="l00408"></a>00408 <span class="keywordflow">while</span> (alreadyRead < size) {
+<a name="l00409"></a>00409 <span class="keywordflow">do</span> {
+<a name="l00410"></a>00410 ret =<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor."> ::read</a>(fd, (<span class="keywordtype">char</span> *) buf + alreadyRead, size - alreadyRead);
+<a name="l00411"></a>00411 } <span class="keywordflow">while</span> (ret == -1 && errno == EINTR);
+<a name="l00412"></a>00412 <span class="keywordflow">if</span> (ret == -1) {
+<a name="l00413"></a>00413 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"read() failed"</span>, errno);
+<a name="l00414"></a>00414 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret == 0) {
+<a name="l00415"></a>00415 <span class="keywordflow">return</span> <span class="keyword">false</span>;
+<a name="l00416"></a>00416 } <span class="keywordflow">else</span> {
+<a name="l00417"></a>00417 alreadyRead += ret;
+<a name="l00418"></a>00418 }
+<a name="l00419"></a>00419 }
+<a name="l00420"></a>00420 <span class="keywordflow">return</span> <span class="keyword">true</span>;
+<a name="l00421"></a>00421 }
+<a name="l00422"></a>00422 <span class="comment"></span>
+<a name="l00423"></a>00423 <span class="comment"> /**</span>
+<a name="l00424"></a>00424 <span class="comment"> * Receive a file descriptor, which had been passed over the underlying</span>
+<a name="l00425"></a>00425 <span class="comment"> * file descriptor.</span>
+<a name="l00426"></a>00426 <span class="comment"> *</span>
+<a name="l00427"></a>00427 <span class="comment"> * @return The passed file descriptor.</span>
+<a name="l00428"></a>00428 <span class="comment"> * @throws SystemException If something went wrong during the</span>
+<a name="l00429"></a>00429 <span class="comment"> * receiving of a file descriptor. Perhaps the underlying</span>
+<a name="l00430"></a>00430 <span class="comment"> * file descriptor isn't a Unix socket.</span>
+<a name="l00431"></a>00431 <span class="comment"> * @throws IOException Whatever was received doesn't seem to be a</span>
+<a name="l00432"></a>00432 <span class="comment"> * file descriptor.</span>
+<a name="l00433"></a>00433 <span class="comment"> */</span>
+<a name="l00434"></a><a class="code" href="classPassenger_1_1MessageChannel.html#1561b7e4a0f4d39ea431f456e5655488">00434</a> <span class="keywordtype">int</span> <a class="code" href="classPassenger_1_1MessageChannel.html#1561b7e4a0f4d39ea431f456e5655488" title="Receive a file descriptor, which had been passed over the underlying file descriptor...">readFileDescriptor</a>() {
+<a name="l00435"></a>00435 <span class="keyword">struct </span>msghdr msg;
+<a name="l00436"></a>00436 <span class="keyword">struct </span>iovec vec;
+<a name="l00437"></a>00437 <span class="keywordtype">char</span> dummy[1];
+<a name="l00438"></a>00438 <span class="preprocessor"> #ifdef __APPLE__</span>
+<a name="l00439"></a>00439 <span class="preprocessor"></span> <span class="comment">// File descriptor passing macros (CMSG_*) seem to be broken</span>
+<a name="l00440"></a>00440 <span class="comment">// on 64-bit MacOS X. This structure works around the problem.</span>
+<a name="l00441"></a>00441 <span class="keyword">struct </span>{
+<a name="l00442"></a>00442 <span class="keyword">struct </span>cmsghdr header;
+<a name="l00443"></a>00443 <span class="keywordtype">int</span> fd;
+<a name="l00444"></a>00444 } control_data;
+<a name="l00445"></a>00445 <span class="preprocessor"> #define EXPECTED_CMSG_LEN sizeof(control_data)</span>
+<a name="l00446"></a>00446 <span class="preprocessor"></span><span class="preprocessor"> #else</span>
+<a name="l00447"></a>00447 <span class="preprocessor"></span> <span class="keywordtype">char</span> control_data[CMSG_SPACE(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>))];
+<a name="l00448"></a>00448 <span class="preprocessor"> #define EXPECTED_CMSG_LEN CMSG_LEN(sizeof(int))</span>
+<a name="l00449"></a>00449 <span class="preprocessor"></span><span class="preprocessor"> #endif</span>
+<a name="l00450"></a>00450 <span class="preprocessor"></span> <span class="keyword">struct </span>cmsghdr *control_header;
+<a name="l00451"></a>00451
+<a name="l00452"></a>00452 msg.msg_name = NULL;
+<a name="l00453"></a>00453 msg.msg_namelen = 0;
+<a name="l00454"></a>00454
+<a name="l00455"></a>00455 dummy[0] = <span class="charliteral">'\0'</span>;
+<a name="l00456"></a>00456 vec.iov_base = dummy;
+<a name="l00457"></a>00457 vec.iov_len = <span class="keyword">sizeof</span>(dummy);
+<a name="l00458"></a>00458 msg.msg_iov = &vec;
+<a name="l00459"></a>00459 msg.msg_iovlen = 1;
+<a name="l00460"></a>00460
+<a name="l00461"></a>00461 msg.msg_control = (caddr_t) &control_data;
+<a name="l00462"></a>00462 msg.msg_controllen = <span class="keyword">sizeof</span>(control_data);
+<a name="l00463"></a>00463 msg.msg_flags = 0;
+<a name="l00464"></a>00464
+<a name="l00465"></a>00465 <span class="keywordflow">if</span> (recvmsg(fd, &msg, 0) == -1) {
+<a name="l00466"></a>00466 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SystemException.html" title="Represents an error returned by a system call or a standard library call.">SystemException</a>(<span class="stringliteral">"Cannot read file descriptor with recvmsg()"</span>, errno);
+<a name="l00467"></a>00467 }
+<a name="l00468"></a>00468
+<a name="l00469"></a>00469 control_header = CMSG_FIRSTHDR(&msg);
+<a name="l00470"></a>00470 <span class="keywordflow">if</span> (control_header->cmsg_len != EXPECTED_CMSG_LEN
+<a name="l00471"></a>00471 || control_header->cmsg_level != SOL_SOCKET
+<a name="l00472"></a>00472 || control_header->cmsg_type != SCM_RIGHTS) {
+<a name="l00473"></a>00473 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1IOException.html" title="Represents an error that occured during an I/O operation.">IOException</a>(<span class="stringliteral">"No valid file descriptor received."</span>);
+<a name="l00474"></a>00474 }
+<a name="l00475"></a>00475 <span class="preprocessor"> #ifdef __APPLE__</span>
+<a name="l00476"></a>00476 <span class="preprocessor"></span> <span class="keywordflow">return</span> control_data.fd;
+<a name="l00477"></a>00477 <span class="preprocessor"> #else</span>
+<a name="l00478"></a>00478 <span class="preprocessor"></span> <span class="keywordflow">return</span> *((<span class="keywordtype">int</span> *) CMSG_DATA(control_header));
+<a name="l00479"></a>00479 <span class="preprocessor"> #endif</span>
+<a name="l00480"></a>00480 <span class="preprocessor"></span> }
+<a name="l00481"></a>00481 };
+<a name="l00482"></a>00482
+<a name="l00483"></a>00483 } <span class="comment">// namespace Passenger</span>
+<a name="l00484"></a>00484
+<a name="l00485"></a>00485 <span class="preprocessor">#endif </span><span class="comment">/* _PASSENGER_MESSAGE_CHANNEL_H_ */</span>
+</pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Wed May 7 20:28:18 2008 for Passenger by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address>
</body>
</html>