doc/cxxapi/ApplicationPoolServer_8h-source.html in passenger-2.0.4 vs doc/cxxapi/ApplicationPoolServer_8h-source.html in passenger-2.0.5

- old
+ new

@@ -48,636 +48,643 @@ <a name="l00031"></a>00031 <span class="preprocessor">#include &lt;cstdlib&gt;</span> <a name="l00032"></a>00032 <span class="preprocessor">#include &lt;limits.h&gt;</span> <a name="l00033"></a>00033 <span class="preprocessor">#include &lt;errno.h&gt;</span> <a name="l00034"></a>00034 <span class="preprocessor">#include &lt;unistd.h&gt;</span> <a name="l00035"></a>00035 <span class="preprocessor">#include &lt;signal.h&gt;</span> -<a name="l00036"></a>00036 -<a name="l00037"></a>00037 <span class="preprocessor">#include "MessageChannel.h"</span> -<a name="l00038"></a>00038 <span class="preprocessor">#include "ApplicationPool.h"</span> -<a name="l00039"></a>00039 <span class="preprocessor">#include "Application.h"</span> -<a name="l00040"></a>00040 <span class="preprocessor">#include "Exceptions.h"</span> -<a name="l00041"></a>00041 <span class="preprocessor">#include "Logging.h"</span> -<a name="l00042"></a>00042 <span class="preprocessor">#include "System.h"</span> -<a name="l00043"></a>00043 -<a name="l00044"></a>00044 <span class="keyword">namespace </span>Passenger { -<a name="l00045"></a>00045 -<a name="l00046"></a>00046 <span class="keyword">using namespace </span>std; -<a name="l00047"></a>00047 <span class="keyword">using namespace </span>boost; -<a name="l00048"></a>00048 -<a name="l00049"></a>00049 <span class="comment"></span> -<a name="l00050"></a>00050 <span class="comment">/**</span> -<a name="l00051"></a>00051 <span class="comment"> * Multi-process usage support for ApplicationPool.</span> -<a name="l00052"></a>00052 <span class="comment"> *</span> -<a name="l00053"></a>00053 <span class="comment"> * ApplicationPoolServer implements a client/server architecture for ApplicationPool.</span> -<a name="l00054"></a>00054 <span class="comment"> * This allows one to use ApplicationPool in a multi-process environment (unlike</span> -<a name="l00055"></a>00055 <span class="comment"> * StandardApplicationPool). The cache/pool data is stored in the server. Different</span> -<a name="l00056"></a>00056 <span class="comment"> * processes can then access the pool through the server.</span> -<a name="l00057"></a>00057 <span class="comment"> *</span> -<a name="l00058"></a>00058 <span class="comment"> * ApplicationPoolServer itself does not inherit ApplicationPool. Instead, it returns</span> -<a name="l00059"></a>00059 <span class="comment"> * an ApplicationPool object via the connect() call. For example:</span> -<a name="l00060"></a>00060 <span class="comment"> * @code</span> -<a name="l00061"></a>00061 <span class="comment"> * // Create an ApplicationPoolServer.</span> -<a name="l00062"></a>00062 <span class="comment"> * ApplicationPoolServer server(...);</span> -<a name="l00063"></a>00063 <span class="comment"> * </span> -<a name="l00064"></a>00064 <span class="comment"> * // Now fork a child process, like Apache's prefork MPM eventually will.</span> -<a name="l00065"></a>00065 <span class="comment"> * pid_t pid = fork();</span> -<a name="l00066"></a>00066 <span class="comment"> * if (pid == 0) {</span> -<a name="l00067"></a>00067 <span class="comment"> * // Child process</span> -<a name="l00068"></a>00068 <span class="comment"> * </span> -<a name="l00069"></a>00069 <span class="comment"> * // Connect to the server. After connection, we have an ApplicationPool</span> -<a name="l00070"></a>00070 <span class="comment"> * // object!</span> -<a name="l00071"></a>00071 <span class="comment"> * ApplicationPoolPtr pool(server.connect());</span> -<a name="l00072"></a>00072 <span class="comment"> *</span> -<a name="l00073"></a>00073 <span class="comment"> * // We don't need to connect to the server anymore, so we detach from it.</span> -<a name="l00074"></a>00074 <span class="comment"> * // This frees up some resources, such as file descriptors.</span> -<a name="l00075"></a>00075 <span class="comment"> * server.detach();</span> -<a name="l00076"></a>00076 <span class="comment"> *</span> -<a name="l00077"></a>00077 <span class="comment"> * ApplicationPool::SessionPtr session(pool-&gt;get("/home/webapps/foo"));</span> -<a name="l00078"></a>00078 <span class="comment"> * do_something_with(session);</span> -<a name="l00079"></a>00079 <span class="comment"> *</span> -<a name="l00080"></a>00080 <span class="comment"> * _exit(0);</span> -<a name="l00081"></a>00081 <span class="comment"> * } else {</span> -<a name="l00082"></a>00082 <span class="comment"> * // Parent process</span> -<a name="l00083"></a>00083 <span class="comment"> * waitpid(pid, NULL, 0);</span> -<a name="l00084"></a>00084 <span class="comment"> * }</span> -<a name="l00085"></a>00085 <span class="comment"> * @endcode</span> -<a name="l00086"></a>00086 <span class="comment"> *</span> -<a name="l00087"></a>00087 <span class="comment"> * &lt;h2&gt;Implementation notes&lt;/h2&gt;</span> -<a name="l00088"></a>00088 <span class="comment"> *</span> -<a name="l00089"></a>00089 <span class="comment"> * &lt;h3&gt;Separate server executable&lt;/h3&gt;</span> -<a name="l00090"></a>00090 <span class="comment"> * The actual server is implemented in ApplicationPoolServerExecutable.cpp, this class is</span> -<a name="l00091"></a>00091 <span class="comment"> * just a convenience class for starting/stopping the server executable and connecting</span> -<a name="l00092"></a>00092 <span class="comment"> * to it.</span> -<a name="l00093"></a>00093 <span class="comment"> *</span> -<a name="l00094"></a>00094 <span class="comment"> * In the past, the server logic itself was implemented in this class. This implies that</span> -<a name="l00095"></a>00095 <span class="comment"> * the ApplicationPool server ran inside the Apache process. This presented us with several</span> -<a name="l00096"></a>00096 <span class="comment"> * problems:</span> -<a name="l00097"></a>00097 <span class="comment"> * - Because of the usage of threads in the ApplicationPool server, the Apache VM size would</span> -<a name="l00098"></a>00098 <span class="comment"> * go way up. This gave people the (wrong) impression that Passenger uses a lot of memory,</span> -<a name="l00099"></a>00099 <span class="comment"> * or that it leaks memory.</span> -<a name="l00100"></a>00100 <span class="comment"> * - Although it's not entirely confirmed, we suspect that it caused heap fragmentation as</span> -<a name="l00101"></a>00101 <span class="comment"> * well. Apache allocates lots and lots of small objects on the heap, and ApplicationPool</span> -<a name="l00102"></a>00102 <span class="comment"> * server isn't exactly helping. This too gave people the (wrong) impression that</span> -<a name="l00103"></a>00103 <span class="comment"> * Passenger leaks memory.</span> -<a name="l00104"></a>00104 <span class="comment"> * - It would unnecessarily bloat the VM size of Apache worker processes.</span> -<a name="l00105"></a>00105 <span class="comment"> * - We had to resort to all kinds of tricks to make sure that fork()ing a process doesn't</span> -<a name="l00106"></a>00106 <span class="comment"> * result in file descriptor leaks.</span> -<a name="l00107"></a>00107 <span class="comment"> * - Despite everything, there was still a small chance that file descriptor leaks would</span> -<a name="l00108"></a>00108 <span class="comment"> * occur, and this could not be fixed. The reason for this is that the Apache control</span> -<a name="l00109"></a>00109 <span class="comment"> * process may call fork() right after the ApplicationPool server has established a new</span> -<a name="l00110"></a>00110 <span class="comment"> * connection with a client.</span> -<a name="l00111"></a>00111 <span class="comment"> *</span> -<a name="l00112"></a>00112 <span class="comment"> * Because of these problems, it was decided to split the ApplicationPool server to a</span> -<a name="l00113"></a>00113 <span class="comment"> * separate executable. This comes with no performance hit.</span> -<a name="l00114"></a>00114 <span class="comment"> *</span> -<a name="l00115"></a>00115 <span class="comment"> * &lt;h3&gt;Anonymous server socket&lt;/h3&gt;</span> -<a name="l00116"></a>00116 <span class="comment"> * Notice that ApplicationPoolServer does do not use TCP sockets at all, or even named Unix</span> -<a name="l00117"></a>00117 <span class="comment"> * sockets, despite being a server that can handle multiple clients! So ApplicationPoolServer</span> -<a name="l00118"></a>00118 <span class="comment"> * will expose no open ports or temporary Unix socket files. Only child processes are able</span> -<a name="l00119"></a>00119 <span class="comment"> * to use the ApplicationPoolServer.</span> -<a name="l00120"></a>00120 <span class="comment"> *</span> -<a name="l00121"></a>00121 <span class="comment"> * This is implemented through anonymous Unix sockets (&lt;tt&gt;socketpair()&lt;/tt&gt;) and file descriptor</span> -<a name="l00122"></a>00122 <span class="comment"> * passing. It allows one to emulate &lt;tt&gt;accept()&lt;/tt&gt;. ApplicationPoolServer is connected to</span> -<a name="l00123"></a>00123 <span class="comment"> * the server executable through a Unix socket pair. connect() sends a connect request to the</span> -<a name="l00124"></a>00124 <span class="comment"> * server through that socket. The server will then create a new socket pair, and pass one of</span> -<a name="l00125"></a>00125 <span class="comment"> * them back. This new socket pair represents the newly established connection.</span> -<a name="l00126"></a>00126 <span class="comment"> *</span> -<a name="l00127"></a>00127 <span class="comment"> * @ingroup Support</span> -<a name="l00128"></a>00128 <span class="comment"> */</span> -<a name="l00129"></a><a class="code" href="classPassenger_1_1ApplicationPoolServer.html">00129</a> <span class="keyword">class </span><a class="code" href="classPassenger_1_1ApplicationPoolServer.html" title="Multi-process usage support for ApplicationPool.">ApplicationPoolServer</a> { -<a name="l00130"></a>00130 <span class="keyword">private</span>:<span class="comment"></span> -<a name="l00131"></a>00131 <span class="comment"> /**</span> -<a name="l00132"></a>00132 <span class="comment"> * Contains data shared between RemoteSession and Client.</span> -<a name="l00133"></a>00133 <span class="comment"> * Since RemoteSession and Client have different life times, i.e. one may be</span> -<a name="l00134"></a>00134 <span class="comment"> * destroyed before the other, they both use a smart pointer that points to</span> -<a name="l00135"></a>00135 <span class="comment"> * a SharedData. This way, the SharedData object is only destroyed when</span> -<a name="l00136"></a>00136 <span class="comment"> * both the RemoteSession and the Client object has been destroyed.</span> -<a name="l00137"></a>00137 <span class="comment"> */</span> -<a name="l00138"></a>00138 <span class="keyword">struct </span>SharedData {<span class="comment"></span> -<a name="l00139"></a>00139 <span class="comment"> /**</span> -<a name="l00140"></a>00140 <span class="comment"> * The socket connection to the ApplicationPool server, as was</span> -<a name="l00141"></a>00141 <span class="comment"> * established by ApplicationPoolServer::connect().</span> -<a name="l00142"></a>00142 <span class="comment"> */</span> -<a name="l00143"></a>00143 <span class="keywordtype">int</span> server; -<a name="l00144"></a>00144 -<a name="l00145"></a>00145 mutex lock; -<a name="l00146"></a>00146 -<a name="l00147"></a>00147 ~SharedData() { -<a name="l00148"></a>00148 <span class="keywordtype">int</span> ret; -<a name="l00149"></a>00149 <span class="keywordflow">do</span> { -<a name="l00150"></a>00150 ret = close(server); -<a name="l00151"></a>00151 } <span class="keywordflow">while</span> (ret == -1 &amp;&amp; errno == EINTR); -<a name="l00152"></a>00152 } -<a name="l00153"></a>00153 }; -<a name="l00154"></a>00154 -<a name="l00155"></a>00155 <span class="keyword">typedef</span> shared_ptr&lt;SharedData&gt; SharedDataPtr; -<a name="l00156"></a>00156 <span class="comment"></span> -<a name="l00157"></a>00157 <span class="comment"> /**</span> -<a name="l00158"></a>00158 <span class="comment"> * An Application::Session which works together with ApplicationPoolServer.</span> -<a name="l00159"></a>00159 <span class="comment"> */</span> -<a name="l00160"></a>00160 <span class="keyword">class </span>RemoteSession: <span class="keyword">public</span> <a class="code" href="classPassenger_1_1Application.html" title="Represents a single Ruby on Rails or Rack application instance.">Application</a>::Session { -<a name="l00161"></a>00161 <span class="keyword">private</span>: -<a name="l00162"></a>00162 SharedDataPtr data; -<a name="l00163"></a>00163 <span class="keywordtype">int</span> id; -<a name="l00164"></a>00164 <span class="keywordtype">int</span> fd; -<a name="l00165"></a>00165 pid_t pid; -<a name="l00166"></a>00166 <span class="keyword">public</span>: -<a name="l00167"></a>00167 RemoteSession(SharedDataPtr data, pid_t pid, <span class="keywordtype">int</span> <span class="keywordtype">id</span>, <span class="keywordtype">int</span> fd) { -<a name="l00168"></a>00168 this-&gt;data = data; -<a name="l00169"></a>00169 this-&gt;pid = pid; -<a name="l00170"></a>00170 this-&gt;<span class="keywordtype">id</span> = id; -<a name="l00171"></a>00171 this-&gt;fd = fd; -<a name="l00172"></a>00172 } -<a name="l00173"></a>00173 -<a name="l00174"></a>00174 <span class="keyword">virtual</span> ~RemoteSession() { -<a name="l00175"></a>00175 closeStream(); -<a name="l00176"></a>00176 mutex::scoped_lock(data-&gt;lock); -<a name="l00177"></a>00177 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a>(data-&gt;server).write(<span class="stringliteral">"close"</span>, <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(<span class="keywordtype">id</span>).c_str(), NULL); -<a name="l00178"></a>00178 } -<a name="l00179"></a>00179 -<a name="l00180"></a>00180 <span class="keyword">virtual</span> <span class="keywordtype">int</span> getStream()<span class="keyword"> const </span>{ -<a name="l00181"></a>00181 <span class="keywordflow">return</span> fd; -<a name="l00182"></a>00182 } -<a name="l00183"></a>00183 -<a name="l00184"></a>00184 <span class="keyword">virtual</span> <span class="keywordtype">void</span> shutdownReader() { -<a name="l00185"></a>00185 <span class="keywordflow">if</span> (fd != -1) { -<a name="l00186"></a>00186 <span class="keywordtype">int</span> ret = InterruptableCalls::shutdown(fd, SHUT_RD); -<a name="l00187"></a>00187 <span class="keywordflow">if</span> (ret == -1) { -<a name="l00188"></a>00188 <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 shutdown the writer stream"</span>, -<a name="l00189"></a>00189 errno); -<a name="l00190"></a>00190 } -<a name="l00191"></a>00191 } -<a name="l00192"></a>00192 } -<a name="l00193"></a>00193 -<a name="l00194"></a>00194 <span class="keyword">virtual</span> <span class="keywordtype">void</span> shutdownWriter() { -<a name="l00195"></a>00195 <span class="keywordflow">if</span> (fd != -1) { -<a name="l00196"></a>00196 <span class="keywordtype">int</span> ret = InterruptableCalls::shutdown(fd, SHUT_WR); -<a name="l00197"></a>00197 <span class="keywordflow">if</span> (ret == -1) { -<a name="l00198"></a>00198 <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 shutdown the writer stream"</span>, -<a name="l00199"></a>00199 errno); -<a name="l00200"></a>00200 } -<a name="l00201"></a>00201 } -<a name="l00202"></a>00202 } -<a name="l00203"></a>00203 -<a name="l00204"></a>00204 <span class="keyword">virtual</span> <span class="keywordtype">void</span> closeStream() { -<a name="l00205"></a>00205 <span class="keywordflow">if</span> (fd != -1) { -<a name="l00206"></a>00206 <span class="keywordtype">int</span> ret = InterruptableCalls::close(fd); -<a name="l00207"></a>00207 <span class="keywordflow">if</span> (ret == -1) { -<a name="l00208"></a>00208 <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 close the session stream"</span>, -<a name="l00209"></a>00209 errno); -<a name="l00210"></a>00210 } -<a name="l00211"></a>00211 fd = -1; -<a name="l00212"></a>00212 } -<a name="l00213"></a>00213 } -<a name="l00214"></a>00214 -<a name="l00215"></a>00215 <span class="keyword">virtual</span> <span class="keywordtype">void</span> discardStream() { -<a name="l00216"></a>00216 fd = -1; -<a name="l00217"></a>00217 } -<a name="l00218"></a>00218 -<a name="l00219"></a>00219 <span class="keyword">virtual</span> pid_t getPid()<span class="keyword"> const </span>{ -<a name="l00220"></a>00220 <span class="keywordflow">return</span> pid; -<a name="l00221"></a>00221 } -<a name="l00222"></a>00222 }; -<a name="l00223"></a>00223 <span class="comment"></span> -<a name="l00224"></a>00224 <span class="comment"> /**</span> -<a name="l00225"></a>00225 <span class="comment"> * An ApplicationPool implementation that works together with ApplicationPoolServer.</span> -<a name="l00226"></a>00226 <span class="comment"> * It doesn't do much by itself, its job is mostly to forward queries/commands to</span> -<a name="l00227"></a>00227 <span class="comment"> * the server and returning the result. Most of the logic is in the server executable.</span> -<a name="l00228"></a>00228 <span class="comment"> */</span> -<a name="l00229"></a>00229 <span class="keyword">class </span><a class="code" href="classClient.html#e625bb32c95bde75bfa4bdfdb8ee56db" title="Create a new Client object.">Client</a>: <span class="keyword">public</span> <a class="code" href="classPassenger_1_1ApplicationPool.html" title="A persistent pool of Applications.">ApplicationPool</a> { -<a name="l00230"></a>00230 <span class="keyword">private</span>: -<a name="l00231"></a>00231 <span class="comment">// The smart pointer only serves to keep the shared data alive.</span> -<a name="l00232"></a>00232 <span class="comment">// We access the shared data via a normal pointer, for performance.</span> -<a name="l00233"></a>00233 SharedDataPtr dataSmartPointer; -<a name="l00234"></a>00234 SharedData *data; -<a name="l00235"></a>00235 -<a name="l00236"></a>00236 <span class="keyword">public</span>:<span class="comment"></span> -<a name="l00237"></a>00237 <span class="comment"> /**</span> -<a name="l00238"></a>00238 <span class="comment"> * Create a new Client.</span> -<a name="l00239"></a>00239 <span class="comment"> *</span> -<a name="l00240"></a>00240 <span class="comment"> * @param sock The newly established socket connection with the ApplicationPoolServer.</span> -<a name="l00241"></a>00241 <span class="comment"> */</span> -<a name="l00242"></a>00242 Client(<span class="keywordtype">int</span> sock) { -<a name="l00243"></a>00243 dataSmartPointer = <a class="code" href="group__Support.html#g41b6c4a82fed72531a147de0505a8396" title="Convenience shortcut for creating a shared_ptr.">ptr</a>(<span class="keyword">new</span> SharedData()); -<a name="l00244"></a>00244 data = dataSmartPointer.get(); -<a name="l00245"></a>00245 data-&gt;server = sock; -<a name="l00246"></a>00246 } -<a name="l00247"></a>00247 -<a name="l00248"></a>00248 <span class="keyword">virtual</span> <span class="keywordtype">void</span> clear() { -<a name="l00249"></a>00249 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00250"></a>00250 mutex::scoped_lock l(data-&gt;lock); -<a name="l00251"></a>00251 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"clear"</span>, NULL); -<a name="l00252"></a>00252 } -<a name="l00253"></a>00253 -<a name="l00254"></a>00254 <span class="keyword">virtual</span> <span class="keywordtype">void</span> setMaxIdleTime(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> seconds) { -<a name="l00255"></a>00255 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00256"></a>00256 mutex::scoped_lock l(data-&gt;lock); -<a name="l00257"></a>00257 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"setMaxIdleTime"</span>, <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(seconds).c_str(), NULL); -<a name="l00258"></a>00258 } -<a name="l00259"></a>00259 -<a name="l00260"></a>00260 <span class="keyword">virtual</span> <span class="keywordtype">void</span> setMax(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> max) { -<a name="l00261"></a>00261 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00262"></a>00262 mutex::scoped_lock l(data-&gt;lock); -<a name="l00263"></a>00263 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"setMax"</span>, <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(max).c_str(), NULL); -<a name="l00264"></a>00264 } -<a name="l00265"></a>00265 -<a name="l00266"></a>00266 <span class="keyword">virtual</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> getActive()<span class="keyword"> const </span>{ -<a name="l00267"></a>00267 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00268"></a>00268 mutex::scoped_lock l(data-&gt;lock); -<a name="l00269"></a>00269 vector&lt;string&gt; args; -<a name="l00270"></a>00270 -<a name="l00271"></a>00271 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"getActive"</span>, NULL); -<a name="l00272"></a>00272 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(args); -<a name="l00273"></a>00273 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[0].c_str()); -<a name="l00274"></a>00274 } -<a name="l00275"></a>00275 -<a name="l00276"></a>00276 <span class="keyword">virtual</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> getCount()<span class="keyword"> const </span>{ -<a name="l00277"></a>00277 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00278"></a>00278 mutex::scoped_lock l(data-&gt;lock); -<a name="l00279"></a>00279 vector&lt;string&gt; args; -<a name="l00280"></a>00280 -<a name="l00281"></a>00281 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"getCount"</span>, NULL); -<a name="l00282"></a>00282 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(args); -<a name="l00283"></a>00283 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[0].c_str()); -<a name="l00284"></a>00284 } -<a name="l00285"></a>00285 -<a name="l00286"></a>00286 <span class="keyword">virtual</span> <span class="keywordtype">void</span> setMaxPerApp(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> max) { -<a name="l00287"></a>00287 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00288"></a>00288 mutex::scoped_lock l(data-&gt;lock); -<a name="l00289"></a>00289 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"setMaxPerApp"</span>, <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(max).c_str(), NULL); -<a name="l00290"></a>00290 } -<a name="l00291"></a>00291 -<a name="l00292"></a>00292 <span class="keyword">virtual</span> <span class="keywordtype">void</span> setUseGlobalQueue(<span class="keywordtype">bool</span> value) { -<a name="l00293"></a>00293 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00294"></a>00294 boost::mutex::scoped_lock l(data-&gt;lock); -<a name="l00295"></a>00295 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"setUseGlobalQueue"</span>, value ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>, NULL); -<a name="l00296"></a>00296 } -<a name="l00297"></a>00297 -<a name="l00298"></a>00298 <span class="keyword">virtual</span> pid_t getSpawnServerPid()<span class="keyword"> const </span>{ -<a name="l00299"></a>00299 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; -<a name="l00300"></a>00300 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00301"></a>00301 mutex::scoped_lock l(data-&gt;lock); -<a name="l00302"></a>00302 vector&lt;string&gt; args; -<a name="l00303"></a>00303 -<a name="l00304"></a>00304 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"getSpawnServerPid"</span>, NULL); -<a name="l00305"></a>00305 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(args); -<a name="l00306"></a>00306 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[0].c_str()); -<a name="l00307"></a>00307 } -<a name="l00308"></a>00308 -<a name="l00309"></a>00309 <span class="keyword">virtual</span> <a class="code" href="classPassenger_1_1Application.html#d14f673494991460b16246a527ad8ad9" title="Convenient alias for Session smart pointer.">Application::SessionPtr</a> <span class="keyword">get</span>( -<a name="l00310"></a>00310 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;appRoot, -<a name="l00311"></a>00311 <span class="keywordtype">bool</span> lowerPrivilege = <span class="keyword">true</span>, -<a name="l00312"></a>00312 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;lowestUser = <span class="stringliteral">"nobody"</span>, -<a name="l00313"></a>00313 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;environment = <span class="stringliteral">"production"</span>, -<a name="l00314"></a>00314 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;spawnMethod = <span class="stringliteral">"smart"</span>, -<a name="l00315"></a>00315 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;appType = <span class="stringliteral">"rails"</span> -<a name="l00316"></a>00316 ) { -<a name="l00317"></a>00317 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; -<a name="l00318"></a>00318 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); -<a name="l00319"></a>00319 mutex::scoped_lock l(data-&gt;lock); -<a name="l00320"></a>00320 vector&lt;string&gt; args; -<a name="l00321"></a>00321 <span class="keywordtype">int</span> stream; -<a name="l00322"></a>00322 <span class="keywordtype">bool</span> result; -<a name="l00323"></a>00323 -<a name="l00324"></a>00324 <span class="keywordflow">try</span> { -<a name="l00325"></a>00325 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"get"</span>, appRoot.c_str(), -<a name="l00326"></a>00326 (lowerPrivilege) ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>, -<a name="l00327"></a>00327 lowestUser.c_str(), -<a name="l00328"></a>00328 environment.c_str(), -<a name="l00329"></a>00329 spawnMethod.c_str(), -<a name="l00330"></a>00330 appType.c_str(), -<a name="l00331"></a>00331 NULL); -<a name="l00332"></a>00332 } <span class="keywordflow">catch</span> (<span class="keyword">const</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> &amp;) { -<a name="l00333"></a>00333 <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">"The ApplicationPool server exited unexpectedly."</span>); -<a name="l00334"></a>00334 } -<a name="l00335"></a>00335 <span class="keywordflow">try</span> { -<a name="l00336"></a>00336 result = channel.<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(args); -<a name="l00337"></a>00337 } <span class="keywordflow">catch</span> (<span class="keyword">const</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> &amp;e) { -<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">"Could not read a message from "</span> -<a name="l00339"></a>00339 <span class="stringliteral">"the ApplicationPool server"</span>, e.<a class="code" href="classPassenger_1_1SystemException.html#ee7a6672bf79b72a4c3ee70c57d6a47c" title="The value of errno at the time the error occured.">code</a>()); -<a name="l00340"></a>00340 } -<a name="l00341"></a>00341 <span class="keywordflow">if</span> (!result) { -<a name="l00342"></a>00342 <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">"The ApplicationPool server unexpectedly "</span> -<a name="l00343"></a>00343 <span class="stringliteral">"closed the connection."</span>); -<a name="l00344"></a>00344 } -<a name="l00345"></a>00345 <span class="keywordflow">if</span> (args[0] == <span class="stringliteral">"ok"</span>) { -<a name="l00346"></a>00346 stream = channel.<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="l00347"></a>00347 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g41b6c4a82fed72531a147de0505a8396" title="Convenience shortcut for creating a shared_ptr.">ptr</a>(<span class="keyword">new</span> RemoteSession(dataSmartPointer, -<a name="l00348"></a>00348 <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[1]), <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[2]), stream)); -<a name="l00349"></a>00349 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (args[0] == <span class="stringliteral">"SpawnException"</span>) { -<a name="l00350"></a>00350 <span class="keywordflow">if</span> (args[2] == <span class="stringliteral">"true"</span>) { -<a name="l00351"></a>00351 <span class="keywordtype">string</span> errorPage; -<a name="l00352"></a>00352 -<a name="l00353"></a>00353 <span class="keywordflow">if</span> (!channel.<a class="code" href="classPassenger_1_1MessageChannel.html#4ce6a0e751b5e3563bee583c231569bc" title="Read a scalar message from the underlying file descriptor.">readScalar</a>(errorPage)) { -<a name="l00354"></a>00354 <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">"The ApplicationPool server "</span> -<a name="l00355"></a>00355 <span class="stringliteral">"unexpectedly closed the connection."</span>); -<a name="l00356"></a>00356 } -<a name="l00357"></a>00357 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SpawnException.html" title="Thrown when SpawnManager or ApplicationPool fails to spawn an application instance...">SpawnException</a>(args[1], errorPage); -<a name="l00358"></a>00358 } <span class="keywordflow">else</span> { -<a name="l00359"></a>00359 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SpawnException.html" title="Thrown when SpawnManager or ApplicationPool fails to spawn an application instance...">SpawnException</a>(args[1]); -<a name="l00360"></a>00360 } -<a name="l00361"></a>00361 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (args[0] == <span class="stringliteral">"BusyException"</span>) { -<a name="l00362"></a>00362 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1BusyException.html" title="The application pool is too busy and cannot fulfill a get() request.">BusyException</a>(args[1]); -<a name="l00363"></a>00363 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (args[0] == <span class="stringliteral">"IOException"</span>) { -<a name="l00364"></a>00364 <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>(args[1]); -<a name="l00365"></a>00365 } <span class="keywordflow">else</span> { -<a name="l00366"></a>00366 <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">"The ApplicationPool server returned "</span> -<a name="l00367"></a>00367 <span class="stringliteral">"an unknown message: "</span> + <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(args)); -<a name="l00368"></a>00368 } -<a name="l00369"></a>00369 } -<a name="l00370"></a>00370 }; -<a name="l00371"></a>00371 +<a name="l00036"></a>00036 <span class="preprocessor">#include &lt;fcntl.h&gt;</span> +<a name="l00037"></a>00037 +<a name="l00038"></a>00038 <span class="preprocessor">#include "MessageChannel.h"</span> +<a name="l00039"></a>00039 <span class="preprocessor">#include "ApplicationPool.h"</span> +<a name="l00040"></a>00040 <span class="preprocessor">#include "Application.h"</span> +<a name="l00041"></a>00041 <span class="preprocessor">#include "Exceptions.h"</span> +<a name="l00042"></a>00042 <span class="preprocessor">#include "Logging.h"</span> +<a name="l00043"></a>00043 <span class="preprocessor">#include "System.h"</span> +<a name="l00044"></a>00044 +<a name="l00045"></a>00045 <span class="keyword">namespace </span>Passenger { +<a name="l00046"></a>00046 +<a name="l00047"></a>00047 <span class="keyword">using namespace </span>std; +<a name="l00048"></a>00048 <span class="keyword">using namespace </span>boost; +<a name="l00049"></a>00049 +<a name="l00050"></a>00050 <span class="comment"></span> +<a name="l00051"></a>00051 <span class="comment">/**</span> +<a name="l00052"></a>00052 <span class="comment"> * Multi-process usage support for ApplicationPool.</span> +<a name="l00053"></a>00053 <span class="comment"> *</span> +<a name="l00054"></a>00054 <span class="comment"> * ApplicationPoolServer implements a client/server architecture for ApplicationPool.</span> +<a name="l00055"></a>00055 <span class="comment"> * This allows one to use ApplicationPool in a multi-process environment (unlike</span> +<a name="l00056"></a>00056 <span class="comment"> * StandardApplicationPool). The cache/pool data is stored in the server. Different</span> +<a name="l00057"></a>00057 <span class="comment"> * processes can then access the pool through the server.</span> +<a name="l00058"></a>00058 <span class="comment"> *</span> +<a name="l00059"></a>00059 <span class="comment"> * ApplicationPoolServer itself does not inherit ApplicationPool. Instead, it returns</span> +<a name="l00060"></a>00060 <span class="comment"> * an ApplicationPool object via the connect() call. For example:</span> +<a name="l00061"></a>00061 <span class="comment"> * @code</span> +<a name="l00062"></a>00062 <span class="comment"> * // Create an ApplicationPoolServer.</span> +<a name="l00063"></a>00063 <span class="comment"> * ApplicationPoolServer server(...);</span> +<a name="l00064"></a>00064 <span class="comment"> * </span> +<a name="l00065"></a>00065 <span class="comment"> * // Now fork a child process, like Apache's prefork MPM eventually will.</span> +<a name="l00066"></a>00066 <span class="comment"> * pid_t pid = fork();</span> +<a name="l00067"></a>00067 <span class="comment"> * if (pid == 0) {</span> +<a name="l00068"></a>00068 <span class="comment"> * // Child process</span> +<a name="l00069"></a>00069 <span class="comment"> * </span> +<a name="l00070"></a>00070 <span class="comment"> * // Connect to the server. After connection, we have an ApplicationPool</span> +<a name="l00071"></a>00071 <span class="comment"> * // object!</span> +<a name="l00072"></a>00072 <span class="comment"> * ApplicationPoolPtr pool(server.connect());</span> +<a name="l00073"></a>00073 <span class="comment"> *</span> +<a name="l00074"></a>00074 <span class="comment"> * // We don't need to connect to the server anymore, so we detach from it.</span> +<a name="l00075"></a>00075 <span class="comment"> * // This frees up some resources, such as file descriptors.</span> +<a name="l00076"></a>00076 <span class="comment"> * server.detach();</span> +<a name="l00077"></a>00077 <span class="comment"> *</span> +<a name="l00078"></a>00078 <span class="comment"> * ApplicationPool::SessionPtr session(pool-&gt;get("/home/webapps/foo"));</span> +<a name="l00079"></a>00079 <span class="comment"> * do_something_with(session);</span> +<a name="l00080"></a>00080 <span class="comment"> *</span> +<a name="l00081"></a>00081 <span class="comment"> * _exit(0);</span> +<a name="l00082"></a>00082 <span class="comment"> * } else {</span> +<a name="l00083"></a>00083 <span class="comment"> * // Parent process</span> +<a name="l00084"></a>00084 <span class="comment"> * waitpid(pid, NULL, 0);</span> +<a name="l00085"></a>00085 <span class="comment"> * }</span> +<a name="l00086"></a>00086 <span class="comment"> * @endcode</span> +<a name="l00087"></a>00087 <span class="comment"> *</span> +<a name="l00088"></a>00088 <span class="comment"> * &lt;h2&gt;Implementation notes&lt;/h2&gt;</span> +<a name="l00089"></a>00089 <span class="comment"> *</span> +<a name="l00090"></a>00090 <span class="comment"> * &lt;h3&gt;Separate server executable&lt;/h3&gt;</span> +<a name="l00091"></a>00091 <span class="comment"> * The actual server is implemented in ApplicationPoolServerExecutable.cpp, this class is</span> +<a name="l00092"></a>00092 <span class="comment"> * just a convenience class for starting/stopping the server executable and connecting</span> +<a name="l00093"></a>00093 <span class="comment"> * to it.</span> +<a name="l00094"></a>00094 <span class="comment"> *</span> +<a name="l00095"></a>00095 <span class="comment"> * In the past, the server logic itself was implemented in this class. This implies that</span> +<a name="l00096"></a>00096 <span class="comment"> * the ApplicationPool server ran inside the Apache process. This presented us with several</span> +<a name="l00097"></a>00097 <span class="comment"> * problems:</span> +<a name="l00098"></a>00098 <span class="comment"> * - Because of the usage of threads in the ApplicationPool server, the Apache VM size would</span> +<a name="l00099"></a>00099 <span class="comment"> * go way up. This gave people the (wrong) impression that Passenger uses a lot of memory,</span> +<a name="l00100"></a>00100 <span class="comment"> * or that it leaks memory.</span> +<a name="l00101"></a>00101 <span class="comment"> * - Although it's not entirely confirmed, we suspect that it caused heap fragmentation as</span> +<a name="l00102"></a>00102 <span class="comment"> * well. Apache allocates lots and lots of small objects on the heap, and ApplicationPool</span> +<a name="l00103"></a>00103 <span class="comment"> * server isn't exactly helping. This too gave people the (wrong) impression that</span> +<a name="l00104"></a>00104 <span class="comment"> * Passenger leaks memory.</span> +<a name="l00105"></a>00105 <span class="comment"> * - It would unnecessarily bloat the VM size of Apache worker processes.</span> +<a name="l00106"></a>00106 <span class="comment"> * - We had to resort to all kinds of tricks to make sure that fork()ing a process doesn't</span> +<a name="l00107"></a>00107 <span class="comment"> * result in file descriptor leaks.</span> +<a name="l00108"></a>00108 <span class="comment"> * - Despite everything, there was still a small chance that file descriptor leaks would</span> +<a name="l00109"></a>00109 <span class="comment"> * occur, and this could not be fixed. The reason for this is that the Apache control</span> +<a name="l00110"></a>00110 <span class="comment"> * process may call fork() right after the ApplicationPool server has established a new</span> +<a name="l00111"></a>00111 <span class="comment"> * connection with a client.</span> +<a name="l00112"></a>00112 <span class="comment"> *</span> +<a name="l00113"></a>00113 <span class="comment"> * Because of these problems, it was decided to split the ApplicationPool server to a</span> +<a name="l00114"></a>00114 <span class="comment"> * separate executable. This comes with no performance hit.</span> +<a name="l00115"></a>00115 <span class="comment"> *</span> +<a name="l00116"></a>00116 <span class="comment"> * &lt;h3&gt;Anonymous server socket&lt;/h3&gt;</span> +<a name="l00117"></a>00117 <span class="comment"> * Notice that ApplicationPoolServer does do not use TCP sockets at all, or even named Unix</span> +<a name="l00118"></a>00118 <span class="comment"> * sockets, despite being a server that can handle multiple clients! So ApplicationPoolServer</span> +<a name="l00119"></a>00119 <span class="comment"> * will expose no open ports or temporary Unix socket files. Only child processes are able</span> +<a name="l00120"></a>00120 <span class="comment"> * to use the ApplicationPoolServer.</span> +<a name="l00121"></a>00121 <span class="comment"> *</span> +<a name="l00122"></a>00122 <span class="comment"> * This is implemented through anonymous Unix sockets (&lt;tt&gt;socketpair()&lt;/tt&gt;) and file descriptor</span> +<a name="l00123"></a>00123 <span class="comment"> * passing. It allows one to emulate &lt;tt&gt;accept()&lt;/tt&gt;. ApplicationPoolServer is connected to</span> +<a name="l00124"></a>00124 <span class="comment"> * the server executable through a Unix socket pair. connect() sends a connect request to the</span> +<a name="l00125"></a>00125 <span class="comment"> * server through that socket. The server will then create a new socket pair, and pass one of</span> +<a name="l00126"></a>00126 <span class="comment"> * them back. This new socket pair represents the newly established connection.</span> +<a name="l00127"></a>00127 <span class="comment"> *</span> +<a name="l00128"></a>00128 <span class="comment"> * @ingroup Support</span> +<a name="l00129"></a>00129 <span class="comment"> */</span> +<a name="l00130"></a><a class="code" href="classPassenger_1_1ApplicationPoolServer.html">00130</a> <span class="keyword">class </span><a class="code" href="classPassenger_1_1ApplicationPoolServer.html" title="Multi-process usage support for ApplicationPool.">ApplicationPoolServer</a> { +<a name="l00131"></a>00131 <span class="keyword">private</span>:<span class="comment"></span> +<a name="l00132"></a>00132 <span class="comment"> /**</span> +<a name="l00133"></a>00133 <span class="comment"> * Contains data shared between RemoteSession and Client.</span> +<a name="l00134"></a>00134 <span class="comment"> * Since RemoteSession and Client have different life times, i.e. one may be</span> +<a name="l00135"></a>00135 <span class="comment"> * destroyed before the other, they both use a smart pointer that points to</span> +<a name="l00136"></a>00136 <span class="comment"> * a SharedData. This way, the SharedData object is only destroyed when</span> +<a name="l00137"></a>00137 <span class="comment"> * both the RemoteSession and the Client object has been destroyed.</span> +<a name="l00138"></a>00138 <span class="comment"> */</span> +<a name="l00139"></a>00139 <span class="keyword">struct </span>SharedData {<span class="comment"></span> +<a name="l00140"></a>00140 <span class="comment"> /**</span> +<a name="l00141"></a>00141 <span class="comment"> * The socket connection to the ApplicationPool server, as was</span> +<a name="l00142"></a>00142 <span class="comment"> * established by ApplicationPoolServer::connect().</span> +<a name="l00143"></a>00143 <span class="comment"> */</span> +<a name="l00144"></a>00144 <span class="keywordtype">int</span> server; +<a name="l00145"></a>00145 +<a name="l00146"></a>00146 mutex lock; +<a name="l00147"></a>00147 +<a name="l00148"></a>00148 ~SharedData() { +<a name="l00149"></a>00149 <span class="keywordtype">int</span> ret; +<a name="l00150"></a>00150 <span class="keywordflow">do</span> { +<a name="l00151"></a>00151 ret = close(server); +<a name="l00152"></a>00152 } <span class="keywordflow">while</span> (ret == -1 &amp;&amp; errno == EINTR); +<a name="l00153"></a>00153 } +<a name="l00154"></a>00154 }; +<a name="l00155"></a>00155 +<a name="l00156"></a>00156 <span class="keyword">typedef</span> shared_ptr&lt;SharedData&gt; SharedDataPtr; +<a name="l00157"></a>00157 <span class="comment"></span> +<a name="l00158"></a>00158 <span class="comment"> /**</span> +<a name="l00159"></a>00159 <span class="comment"> * An Application::Session which works together with ApplicationPoolServer.</span> +<a name="l00160"></a>00160 <span class="comment"> */</span> +<a name="l00161"></a>00161 <span class="keyword">class </span>RemoteSession: <span class="keyword">public</span> <a class="code" href="classPassenger_1_1Application.html" title="Represents a single Ruby on Rails or Rack application instance.">Application</a>::Session { +<a name="l00162"></a>00162 <span class="keyword">private</span>: +<a name="l00163"></a>00163 SharedDataPtr data; +<a name="l00164"></a>00164 <span class="keywordtype">int</span> id; +<a name="l00165"></a>00165 <span class="keywordtype">int</span> fd; +<a name="l00166"></a>00166 pid_t pid; +<a name="l00167"></a>00167 <span class="keyword">public</span>: +<a name="l00168"></a>00168 RemoteSession(SharedDataPtr data, pid_t pid, <span class="keywordtype">int</span> <span class="keywordtype">id</span>, <span class="keywordtype">int</span> fd) { +<a name="l00169"></a>00169 this-&gt;data = data; +<a name="l00170"></a>00170 this-&gt;pid = pid; +<a name="l00171"></a>00171 this-&gt;<span class="keywordtype">id</span> = id; +<a name="l00172"></a>00172 this-&gt;fd = fd; +<a name="l00173"></a>00173 } +<a name="l00174"></a>00174 +<a name="l00175"></a>00175 <span class="keyword">virtual</span> ~RemoteSession() { +<a name="l00176"></a>00176 closeStream(); +<a name="l00177"></a>00177 mutex::scoped_lock(data-&gt;lock); +<a name="l00178"></a>00178 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a>(data-&gt;server).write(<span class="stringliteral">"close"</span>, <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(<span class="keywordtype">id</span>).c_str(), NULL); +<a name="l00179"></a>00179 } +<a name="l00180"></a>00180 +<a name="l00181"></a>00181 <span class="keyword">virtual</span> <span class="keywordtype">int</span> getStream()<span class="keyword"> const </span>{ +<a name="l00182"></a>00182 <span class="keywordflow">return</span> fd; +<a name="l00183"></a>00183 } +<a name="l00184"></a>00184 +<a name="l00185"></a>00185 <span class="keyword">virtual</span> <span class="keywordtype">void</span> shutdownReader() { +<a name="l00186"></a>00186 <span class="keywordflow">if</span> (fd != -1) { +<a name="l00187"></a>00187 <span class="keywordtype">int</span> ret = InterruptableCalls::shutdown(fd, SHUT_RD); +<a name="l00188"></a>00188 <span class="keywordflow">if</span> (ret == -1) { +<a name="l00189"></a>00189 <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 shutdown the writer stream"</span>, +<a name="l00190"></a>00190 errno); +<a name="l00191"></a>00191 } +<a name="l00192"></a>00192 } +<a name="l00193"></a>00193 } +<a name="l00194"></a>00194 +<a name="l00195"></a>00195 <span class="keyword">virtual</span> <span class="keywordtype">void</span> shutdownWriter() { +<a name="l00196"></a>00196 <span class="keywordflow">if</span> (fd != -1) { +<a name="l00197"></a>00197 <span class="keywordtype">int</span> ret = InterruptableCalls::shutdown(fd, SHUT_WR); +<a name="l00198"></a>00198 <span class="keywordflow">if</span> (ret == -1) { +<a name="l00199"></a>00199 <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 shutdown the writer stream"</span>, +<a name="l00200"></a>00200 errno); +<a name="l00201"></a>00201 } +<a name="l00202"></a>00202 } +<a name="l00203"></a>00203 } +<a name="l00204"></a>00204 +<a name="l00205"></a>00205 <span class="keyword">virtual</span> <span class="keywordtype">void</span> closeStream() { +<a name="l00206"></a>00206 <span class="keywordflow">if</span> (fd != -1) { +<a name="l00207"></a>00207 <span class="keywordtype">int</span> ret = InterruptableCalls::close(fd); +<a name="l00208"></a>00208 <span class="keywordflow">if</span> (ret == -1) { +<a name="l00209"></a>00209 <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 close the session stream"</span>, +<a name="l00210"></a>00210 errno); +<a name="l00211"></a>00211 } +<a name="l00212"></a>00212 fd = -1; +<a name="l00213"></a>00213 } +<a name="l00214"></a>00214 } +<a name="l00215"></a>00215 +<a name="l00216"></a>00216 <span class="keyword">virtual</span> <span class="keywordtype">void</span> discardStream() { +<a name="l00217"></a>00217 fd = -1; +<a name="l00218"></a>00218 } +<a name="l00219"></a>00219 +<a name="l00220"></a>00220 <span class="keyword">virtual</span> pid_t getPid()<span class="keyword"> const </span>{ +<a name="l00221"></a>00221 <span class="keywordflow">return</span> pid; +<a name="l00222"></a>00222 } +<a name="l00223"></a>00223 }; +<a name="l00224"></a>00224 <span class="comment"></span> +<a name="l00225"></a>00225 <span class="comment"> /**</span> +<a name="l00226"></a>00226 <span class="comment"> * An ApplicationPool implementation that works together with ApplicationPoolServer.</span> +<a name="l00227"></a>00227 <span class="comment"> * It doesn't do much by itself, its job is mostly to forward queries/commands to</span> +<a name="l00228"></a>00228 <span class="comment"> * the server and returning the result. Most of the logic is in the server executable.</span> +<a name="l00229"></a>00229 <span class="comment"> */</span> +<a name="l00230"></a>00230 <span class="keyword">class </span><a class="code" href="classClient.html#e625bb32c95bde75bfa4bdfdb8ee56db" title="Create a new Client object.">Client</a>: <span class="keyword">public</span> <a class="code" href="classPassenger_1_1ApplicationPool.html" title="A persistent pool of Applications.">ApplicationPool</a> { +<a name="l00231"></a>00231 <span class="keyword">private</span>: +<a name="l00232"></a>00232 <span class="comment">// The smart pointer only serves to keep the shared data alive.</span> +<a name="l00233"></a>00233 <span class="comment">// We access the shared data via a normal pointer, for performance.</span> +<a name="l00234"></a>00234 SharedDataPtr dataSmartPointer; +<a name="l00235"></a>00235 SharedData *data; +<a name="l00236"></a>00236 +<a name="l00237"></a>00237 <span class="keyword">public</span>:<span class="comment"></span> +<a name="l00238"></a>00238 <span class="comment"> /**</span> +<a name="l00239"></a>00239 <span class="comment"> * Create a new Client.</span> +<a name="l00240"></a>00240 <span class="comment"> *</span> +<a name="l00241"></a>00241 <span class="comment"> * @param sock The newly established socket connection with the ApplicationPoolServer.</span> +<a name="l00242"></a>00242 <span class="comment"> */</span> +<a name="l00243"></a>00243 Client(<span class="keywordtype">int</span> sock) { +<a name="l00244"></a>00244 dataSmartPointer = <a class="code" href="group__Support.html#g41b6c4a82fed72531a147de0505a8396" title="Convenience shortcut for creating a shared_ptr.">ptr</a>(<span class="keyword">new</span> SharedData()); +<a name="l00245"></a>00245 data = dataSmartPointer.get(); +<a name="l00246"></a>00246 data-&gt;server = sock; +<a name="l00247"></a>00247 } +<a name="l00248"></a>00248 +<a name="l00249"></a>00249 <span class="keyword">virtual</span> <span class="keywordtype">void</span> clear() { +<a name="l00250"></a>00250 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00251"></a>00251 mutex::scoped_lock l(data-&gt;lock); +<a name="l00252"></a>00252 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"clear"</span>, NULL); +<a name="l00253"></a>00253 } +<a name="l00254"></a>00254 +<a name="l00255"></a>00255 <span class="keyword">virtual</span> <span class="keywordtype">void</span> setMaxIdleTime(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> seconds) { +<a name="l00256"></a>00256 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00257"></a>00257 mutex::scoped_lock l(data-&gt;lock); +<a name="l00258"></a>00258 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"setMaxIdleTime"</span>, <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(seconds).c_str(), NULL); +<a name="l00259"></a>00259 } +<a name="l00260"></a>00260 +<a name="l00261"></a>00261 <span class="keyword">virtual</span> <span class="keywordtype">void</span> setMax(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> max) { +<a name="l00262"></a>00262 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00263"></a>00263 mutex::scoped_lock l(data-&gt;lock); +<a name="l00264"></a>00264 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"setMax"</span>, <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(max).c_str(), NULL); +<a name="l00265"></a>00265 } +<a name="l00266"></a>00266 +<a name="l00267"></a>00267 <span class="keyword">virtual</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> getActive()<span class="keyword"> const </span>{ +<a name="l00268"></a>00268 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00269"></a>00269 mutex::scoped_lock l(data-&gt;lock); +<a name="l00270"></a>00270 vector&lt;string&gt; args; +<a name="l00271"></a>00271 +<a name="l00272"></a>00272 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"getActive"</span>, NULL); +<a name="l00273"></a>00273 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(args); +<a name="l00274"></a>00274 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[0].c_str()); +<a name="l00275"></a>00275 } +<a name="l00276"></a>00276 +<a name="l00277"></a>00277 <span class="keyword">virtual</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> getCount()<span class="keyword"> const </span>{ +<a name="l00278"></a>00278 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00279"></a>00279 mutex::scoped_lock l(data-&gt;lock); +<a name="l00280"></a>00280 vector&lt;string&gt; args; +<a name="l00281"></a>00281 +<a name="l00282"></a>00282 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"getCount"</span>, NULL); +<a name="l00283"></a>00283 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(args); +<a name="l00284"></a>00284 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[0].c_str()); +<a name="l00285"></a>00285 } +<a name="l00286"></a>00286 +<a name="l00287"></a>00287 <span class="keyword">virtual</span> <span class="keywordtype">void</span> setMaxPerApp(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> max) { +<a name="l00288"></a>00288 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00289"></a>00289 mutex::scoped_lock l(data-&gt;lock); +<a name="l00290"></a>00290 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"setMaxPerApp"</span>, <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(max).c_str(), NULL); +<a name="l00291"></a>00291 } +<a name="l00292"></a>00292 +<a name="l00293"></a>00293 <span class="keyword">virtual</span> <span class="keywordtype">void</span> setUseGlobalQueue(<span class="keywordtype">bool</span> value) { +<a name="l00294"></a>00294 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00295"></a>00295 boost::mutex::scoped_lock l(data-&gt;lock); +<a name="l00296"></a>00296 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"setUseGlobalQueue"</span>, value ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>, NULL); +<a name="l00297"></a>00297 } +<a name="l00298"></a>00298 +<a name="l00299"></a>00299 <span class="keyword">virtual</span> pid_t getSpawnServerPid()<span class="keyword"> const </span>{ +<a name="l00300"></a>00300 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; +<a name="l00301"></a>00301 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00302"></a>00302 mutex::scoped_lock l(data-&gt;lock); +<a name="l00303"></a>00303 vector&lt;string&gt; args; +<a name="l00304"></a>00304 +<a name="l00305"></a>00305 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"getSpawnServerPid"</span>, NULL); +<a name="l00306"></a>00306 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(args); +<a name="l00307"></a>00307 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[0].c_str()); +<a name="l00308"></a>00308 } +<a name="l00309"></a>00309 +<a name="l00310"></a>00310 <span class="keyword">virtual</span> <a class="code" href="classPassenger_1_1Application.html#d14f673494991460b16246a527ad8ad9" title="Convenient alias for Session smart pointer.">Application::SessionPtr</a> <span class="keyword">get</span>( +<a name="l00311"></a>00311 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;appRoot, +<a name="l00312"></a>00312 <span class="keywordtype">bool</span> lowerPrivilege = <span class="keyword">true</span>, +<a name="l00313"></a>00313 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;lowestUser = <span class="stringliteral">"nobody"</span>, +<a name="l00314"></a>00314 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;environment = <span class="stringliteral">"production"</span>, +<a name="l00315"></a>00315 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;spawnMethod = <span class="stringliteral">"smart"</span>, +<a name="l00316"></a>00316 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;appType = <span class="stringliteral">"rails"</span> +<a name="l00317"></a>00317 ) { +<a name="l00318"></a>00318 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; +<a name="l00319"></a>00319 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(data-&gt;server); +<a name="l00320"></a>00320 mutex::scoped_lock l(data-&gt;lock); +<a name="l00321"></a>00321 vector&lt;string&gt; args; +<a name="l00322"></a>00322 <span class="keywordtype">int</span> stream; +<a name="l00323"></a>00323 <span class="keywordtype">bool</span> result; +<a name="l00324"></a>00324 +<a name="l00325"></a>00325 <span class="keywordflow">try</span> { +<a name="l00326"></a>00326 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#9ad7a978cf8409e01ab2f0a2b6be5a0a" title="Send an array message, which consists of the given elements, over the underlying...">write</a>(<span class="stringliteral">"get"</span>, appRoot.c_str(), +<a name="l00327"></a>00327 (lowerPrivilege) ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>, +<a name="l00328"></a>00328 lowestUser.c_str(), +<a name="l00329"></a>00329 environment.c_str(), +<a name="l00330"></a>00330 spawnMethod.c_str(), +<a name="l00331"></a>00331 appType.c_str(), +<a name="l00332"></a>00332 NULL); +<a name="l00333"></a>00333 } <span class="keywordflow">catch</span> (<span class="keyword">const</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> &amp;) { +<a name="l00334"></a>00334 <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">"The ApplicationPool server exited unexpectedly."</span>); +<a name="l00335"></a>00335 } +<a name="l00336"></a>00336 <span class="keywordflow">try</span> { +<a name="l00337"></a>00337 result = channel.<a class="code" href="classPassenger_1_1MessageChannel.html#129659b60d1a663337873d2af944431e" title="Read an array message from the underlying file descriptor.">read</a>(args); +<a name="l00338"></a>00338 } <span class="keywordflow">catch</span> (<span class="keyword">const</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> &amp;e) { +<a name="l00339"></a>00339 <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">"Could not read a message from "</span> +<a name="l00340"></a>00340 <span class="stringliteral">"the ApplicationPool server"</span>, e.<a class="code" href="classPassenger_1_1SystemException.html#ee7a6672bf79b72a4c3ee70c57d6a47c" title="The value of errno at the time the error occured.">code</a>()); +<a name="l00341"></a>00341 } +<a name="l00342"></a>00342 <span class="keywordflow">if</span> (!result) { +<a name="l00343"></a>00343 <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">"The ApplicationPool server unexpectedly "</span> +<a name="l00344"></a>00344 <span class="stringliteral">"closed the connection."</span>); +<a name="l00345"></a>00345 } +<a name="l00346"></a>00346 <span class="keywordflow">if</span> (args[0] == <span class="stringliteral">"ok"</span>) { +<a name="l00347"></a>00347 stream = channel.<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="l00348"></a>00348 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g41b6c4a82fed72531a147de0505a8396" title="Convenience shortcut for creating a shared_ptr.">ptr</a>(<span class="keyword">new</span> RemoteSession(dataSmartPointer, +<a name="l00349"></a>00349 <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[1]), <a class="code" href="group__Support.html#g73b17b509ee44938a56bf16cbf82fb48" title="Converts the given string to an integer.">atoi</a>(args[2]), stream)); +<a name="l00350"></a>00350 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (args[0] == <span class="stringliteral">"SpawnException"</span>) { +<a name="l00351"></a>00351 <span class="keywordflow">if</span> (args[2] == <span class="stringliteral">"true"</span>) { +<a name="l00352"></a>00352 <span class="keywordtype">string</span> errorPage; +<a name="l00353"></a>00353 +<a name="l00354"></a>00354 <span class="keywordflow">if</span> (!channel.<a class="code" href="classPassenger_1_1MessageChannel.html#4ce6a0e751b5e3563bee583c231569bc" title="Read a scalar message from the underlying file descriptor.">readScalar</a>(errorPage)) { +<a name="l00355"></a>00355 <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">"The ApplicationPool server "</span> +<a name="l00356"></a>00356 <span class="stringliteral">"unexpectedly closed the connection."</span>); +<a name="l00357"></a>00357 } +<a name="l00358"></a>00358 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SpawnException.html" title="Thrown when SpawnManager or ApplicationPool fails to spawn an application instance...">SpawnException</a>(args[1], errorPage); +<a name="l00359"></a>00359 } <span class="keywordflow">else</span> { +<a name="l00360"></a>00360 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1SpawnException.html" title="Thrown when SpawnManager or ApplicationPool fails to spawn an application instance...">SpawnException</a>(args[1]); +<a name="l00361"></a>00361 } +<a name="l00362"></a>00362 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (args[0] == <span class="stringliteral">"BusyException"</span>) { +<a name="l00363"></a>00363 <span class="keywordflow">throw</span> <a class="code" href="classPassenger_1_1BusyException.html" title="The application pool is too busy and cannot fulfill a get() request.">BusyException</a>(args[1]); +<a name="l00364"></a>00364 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (args[0] == <span class="stringliteral">"IOException"</span>) { +<a name="l00365"></a>00365 <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>(args[1]); +<a name="l00366"></a>00366 } <span class="keywordflow">else</span> { +<a name="l00367"></a>00367 <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">"The ApplicationPool server returned "</span> +<a name="l00368"></a>00368 <span class="stringliteral">"an unknown message: "</span> + <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(args)); +<a name="l00369"></a>00369 } +<a name="l00370"></a>00370 } +<a name="l00371"></a>00371 }; <a name="l00372"></a>00372 -<a name="l00373"></a>00373 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> SERVER_SOCKET_FD = 3; -<a name="l00374"></a>00374 -<a name="l00375"></a>00375 <span class="keywordtype">string</span> m_serverExecutable; -<a name="l00376"></a>00376 <span class="keywordtype">string</span> m_spawnServerCommand; -<a name="l00377"></a>00377 <span class="keywordtype">string</span> m_logFile; -<a name="l00378"></a>00378 <span class="keywordtype">string</span> m_rubyCommand; -<a name="l00379"></a>00379 <span class="keywordtype">string</span> m_user; -<a name="l00380"></a>00380 <span class="keywordtype">string</span> statusReportFIFO; -<a name="l00381"></a>00381 <span class="comment"></span> -<a name="l00382"></a>00382 <span class="comment"> /**</span> -<a name="l00383"></a>00383 <span class="comment"> * The PID of the ApplicationPool server process. If no server process</span> -<a name="l00384"></a>00384 <span class="comment"> * is running, then &lt;tt&gt;serverPid == 0&lt;/tt&gt;.</span> -<a name="l00385"></a>00385 <span class="comment"> *</span> -<a name="l00386"></a>00386 <span class="comment"> * @invariant</span> -<a name="l00387"></a>00387 <span class="comment"> * if serverPid == 0:</span> -<a name="l00388"></a>00388 <span class="comment"> * serverSocket == -1</span> -<a name="l00389"></a>00389 <span class="comment"> */</span> -<a name="l00390"></a>00390 pid_t serverPid; -<a name="l00391"></a>00391 <span class="comment"></span> -<a name="l00392"></a>00392 <span class="comment"> /**</span> -<a name="l00393"></a>00393 <span class="comment"> * The connection to the ApplicationPool server process. If no server</span> -<a name="l00394"></a>00394 <span class="comment"> * process is running, then &lt;tt&gt;serverSocket == -1&lt;/tt&gt;.</span> -<a name="l00395"></a>00395 <span class="comment"> *</span> -<a name="l00396"></a>00396 <span class="comment"> * @invariant</span> -<a name="l00397"></a>00397 <span class="comment"> * if serverPid == 0:</span> -<a name="l00398"></a>00398 <span class="comment"> * serverSocket == -1</span> -<a name="l00399"></a>00399 <span class="comment"> */</span> -<a name="l00400"></a>00400 <span class="keywordtype">int</span> serverSocket; -<a name="l00401"></a>00401 <span class="comment"></span> -<a name="l00402"></a>00402 <span class="comment"> /**</span> -<a name="l00403"></a>00403 <span class="comment"> * Shutdown the currently running ApplicationPool server process.</span> -<a name="l00404"></a>00404 <span class="comment"> *</span> -<a name="l00405"></a>00405 <span class="comment"> * @pre System call interruption is disabled.</span> -<a name="l00406"></a>00406 <span class="comment"> * @pre serverSocket != -1 &amp;&amp; serverPid != 0</span> -<a name="l00407"></a>00407 <span class="comment"> * @post serverSocket == -1 &amp;&amp; serverPid == 0</span> -<a name="l00408"></a>00408 <span class="comment"> */</span> -<a name="l00409"></a>00409 <span class="keywordtype">void</span> shutdownServer() { -<a name="l00410"></a>00410 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; -<a name="l00411"></a>00411 <span class="keywordtype">int</span> ret; -<a name="l00412"></a>00412 time_t begin; -<a name="l00413"></a>00413 <span class="keywordtype">bool</span> done = <span class="keyword">false</span>; -<a name="l00414"></a>00414 -<a name="l00415"></a>00415 InterruptableCalls::close(serverSocket); -<a name="l00416"></a>00416 <span class="keywordflow">if</span> (!statusReportFIFO.empty()) { -<a name="l00417"></a>00417 <span class="keywordflow">do</span> { -<a name="l00418"></a>00418 ret = unlink(statusReportFIFO.c_str()); -<a name="l00419"></a>00419 } <span class="keywordflow">while</span> (ret == -1 &amp;&amp; errno == EINTR); -<a name="l00420"></a>00420 } -<a name="l00421"></a>00421 -<a name="l00422"></a>00422 P_TRACE(2, <span class="stringliteral">"Waiting for existing ApplicationPoolServerExecutable (PID "</span> &lt;&lt; -<a name="l00423"></a>00423 serverPid &lt;&lt; <span class="stringliteral">") to exit..."</span>); -<a name="l00424"></a>00424 begin = InterruptableCalls::time(NULL); -<a name="l00425"></a>00425 <span class="keywordflow">while</span> (!done &amp;&amp; InterruptableCalls::time(NULL) &lt; begin + 5) { -<a name="l00426"></a>00426 <span class="comment">/*</span> -<a name="l00427"></a>00427 <span class="comment"> * Some Apache modules fork(), but don't close file descriptors.</span> -<a name="l00428"></a>00428 <span class="comment"> * mod_wsgi is one such example. Because of that, closing serverSocket</span> -<a name="l00429"></a>00429 <span class="comment"> * won't always cause the ApplicationPool server to exit. So we send it a</span> -<a name="l00430"></a>00430 <span class="comment"> * signal.</span> -<a name="l00431"></a>00431 <span class="comment"> */</span> -<a name="l00432"></a>00432 InterruptableCalls::kill(serverPid, SIGINT); -<a name="l00433"></a>00433 -<a name="l00434"></a>00434 ret = InterruptableCalls::waitpid(serverPid, NULL, WNOHANG); -<a name="l00435"></a>00435 done = ret &gt; 0 || ret == -1; -<a name="l00436"></a>00436 <span class="keywordflow">if</span> (!done) { -<a name="l00437"></a>00437 InterruptableCalls::usleep(100000); -<a name="l00438"></a>00438 } -<a name="l00439"></a>00439 } -<a name="l00440"></a>00440 <span class="keywordflow">if</span> (done) { -<a name="l00441"></a>00441 P_TRACE(2, <span class="stringliteral">"ApplicationPoolServerExecutable exited."</span>); -<a name="l00442"></a>00442 } <span class="keywordflow">else</span> { -<a name="l00443"></a>00443 P_DEBUG(<span class="stringliteral">"ApplicationPoolServerExecutable not exited in time. Killing it..."</span>); -<a name="l00444"></a>00444 InterruptableCalls::kill(serverPid, SIGTERM); -<a name="l00445"></a>00445 InterruptableCalls::waitpid(serverPid, NULL, 0); -<a name="l00446"></a>00446 } -<a name="l00447"></a>00447 -<a name="l00448"></a>00448 serverSocket = -1; -<a name="l00449"></a>00449 serverPid = 0; -<a name="l00450"></a>00450 } -<a name="l00451"></a>00451 <span class="comment"></span> -<a name="l00452"></a>00452 <span class="comment"> /**</span> -<a name="l00453"></a>00453 <span class="comment"> * Start an ApplicationPool server process. If there's already one running,</span> -<a name="l00454"></a>00454 <span class="comment"> * then the currently running one will be shutdown.</span> -<a name="l00455"></a>00455 <span class="comment"> *</span> -<a name="l00456"></a>00456 <span class="comment"> * @pre System call interruption is disabled.</span> -<a name="l00457"></a>00457 <span class="comment"> * @post serverSocket != -1 &amp;&amp; serverPid != 0</span> -<a name="l00458"></a>00458 <span class="comment"> * @throw SystemException Something went wrong.</span> -<a name="l00459"></a>00459 <span class="comment"> */</span> -<a name="l00460"></a>00460 <span class="keywordtype">void</span> restartServer() { -<a name="l00461"></a>00461 <span class="keywordtype">int</span> fds[2]; -<a name="l00462"></a>00462 pid_t pid; -<a name="l00463"></a>00463 -<a name="l00464"></a>00464 <span class="keywordflow">if</span> (serverPid != 0) { -<a name="l00465"></a>00465 shutdownServer(); -<a name="l00466"></a>00466 } -<a name="l00467"></a>00467 -<a name="l00468"></a>00468 <span class="keywordflow">if</span> (InterruptableCalls::socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) { -<a name="l00469"></a>00469 <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 create a Unix socket pair"</span>, errno); -<a name="l00470"></a>00470 } -<a name="l00471"></a>00471 -<a name="l00472"></a>00472 createStatusReportFIFO(); -<a name="l00473"></a>00473 -<a name="l00474"></a>00474 pid = InterruptableCalls::fork(); -<a name="l00475"></a>00475 <span class="keywordflow">if</span> (pid == 0) { <span class="comment">// Child process.</span> -<a name="l00476"></a>00476 dup2(fds[0], SERVER_SOCKET_FD); -<a name="l00477"></a>00477 -<a name="l00478"></a>00478 <span class="comment">// Close all unnecessary file descriptors</span> -<a name="l00479"></a>00479 <span class="keywordflow">for</span> (<span class="keywordtype">long</span> i = sysconf(_SC_OPEN_MAX) - 1; i &gt; SERVER_SOCKET_FD; i--) { -<a name="l00480"></a>00480 close(i); -<a name="l00481"></a>00481 } -<a name="l00482"></a>00482 -<a name="l00483"></a>00483 execlp( -<a name="l00484"></a>00484 #<span class="keywordflow">if</span> 0 -<a name="l00485"></a>00485 <span class="stringliteral">"valgrind"</span>, +<a name="l00373"></a>00373 +<a name="l00374"></a>00374 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> SERVER_SOCKET_FD = 3; +<a name="l00375"></a>00375 +<a name="l00376"></a>00376 <span class="keywordtype">string</span> m_serverExecutable; +<a name="l00377"></a>00377 <span class="keywordtype">string</span> m_spawnServerCommand; +<a name="l00378"></a>00378 <span class="keywordtype">string</span> m_logFile; +<a name="l00379"></a>00379 <span class="keywordtype">string</span> m_rubyCommand; +<a name="l00380"></a>00380 <span class="keywordtype">string</span> m_user; +<a name="l00381"></a>00381 <span class="keywordtype">string</span> statusReportFIFO; +<a name="l00382"></a>00382 <span class="comment"></span> +<a name="l00383"></a>00383 <span class="comment"> /**</span> +<a name="l00384"></a>00384 <span class="comment"> * The PID of the ApplicationPool server process. If no server process</span> +<a name="l00385"></a>00385 <span class="comment"> * is running, then &lt;tt&gt;serverPid == 0&lt;/tt&gt;.</span> +<a name="l00386"></a>00386 <span class="comment"> *</span> +<a name="l00387"></a>00387 <span class="comment"> * @invariant</span> +<a name="l00388"></a>00388 <span class="comment"> * if serverPid == 0:</span> +<a name="l00389"></a>00389 <span class="comment"> * serverSocket == -1</span> +<a name="l00390"></a>00390 <span class="comment"> */</span> +<a name="l00391"></a>00391 pid_t serverPid; +<a name="l00392"></a>00392 <span class="comment"></span> +<a name="l00393"></a>00393 <span class="comment"> /**</span> +<a name="l00394"></a>00394 <span class="comment"> * The connection to the ApplicationPool server process. If no server</span> +<a name="l00395"></a>00395 <span class="comment"> * process is running, then &lt;tt&gt;serverSocket == -1&lt;/tt&gt;.</span> +<a name="l00396"></a>00396 <span class="comment"> *</span> +<a name="l00397"></a>00397 <span class="comment"> * @invariant</span> +<a name="l00398"></a>00398 <span class="comment"> * if serverPid == 0:</span> +<a name="l00399"></a>00399 <span class="comment"> * serverSocket == -1</span> +<a name="l00400"></a>00400 <span class="comment"> */</span> +<a name="l00401"></a>00401 <span class="keywordtype">int</span> serverSocket; +<a name="l00402"></a>00402 <span class="comment"></span> +<a name="l00403"></a>00403 <span class="comment"> /**</span> +<a name="l00404"></a>00404 <span class="comment"> * Shutdown the currently running ApplicationPool server process.</span> +<a name="l00405"></a>00405 <span class="comment"> *</span> +<a name="l00406"></a>00406 <span class="comment"> * @pre System call interruption is disabled.</span> +<a name="l00407"></a>00407 <span class="comment"> * @pre serverSocket != -1 &amp;&amp; serverPid != 0</span> +<a name="l00408"></a>00408 <span class="comment"> * @post serverSocket == -1 &amp;&amp; serverPid == 0</span> +<a name="l00409"></a>00409 <span class="comment"> */</span> +<a name="l00410"></a>00410 <span class="keywordtype">void</span> shutdownServer() { +<a name="l00411"></a>00411 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; +<a name="l00412"></a>00412 <span class="keywordtype">int</span> ret; +<a name="l00413"></a>00413 time_t begin; +<a name="l00414"></a>00414 <span class="keywordtype">bool</span> done = <span class="keyword">false</span>; +<a name="l00415"></a>00415 +<a name="l00416"></a>00416 InterruptableCalls::close(serverSocket); +<a name="l00417"></a>00417 <span class="keywordflow">if</span> (!statusReportFIFO.empty()) { +<a name="l00418"></a>00418 <span class="keywordflow">do</span> { +<a name="l00419"></a>00419 ret = unlink(statusReportFIFO.c_str()); +<a name="l00420"></a>00420 } <span class="keywordflow">while</span> (ret == -1 &amp;&amp; errno == EINTR); +<a name="l00421"></a>00421 } +<a name="l00422"></a>00422 +<a name="l00423"></a>00423 P_TRACE(2, <span class="stringliteral">"Waiting for existing ApplicationPoolServerExecutable (PID "</span> &lt;&lt; +<a name="l00424"></a>00424 serverPid &lt;&lt; <span class="stringliteral">") to exit..."</span>); +<a name="l00425"></a>00425 begin = InterruptableCalls::time(NULL); +<a name="l00426"></a>00426 <span class="keywordflow">while</span> (!done &amp;&amp; InterruptableCalls::time(NULL) &lt; begin + 5) { +<a name="l00427"></a>00427 <span class="comment">/*</span> +<a name="l00428"></a>00428 <span class="comment"> * Some Apache modules fork(), but don't close file descriptors.</span> +<a name="l00429"></a>00429 <span class="comment"> * mod_wsgi is one such example. Because of that, closing serverSocket</span> +<a name="l00430"></a>00430 <span class="comment"> * won't always cause the ApplicationPool server to exit. So we send it a</span> +<a name="l00431"></a>00431 <span class="comment"> * signal.</span> +<a name="l00432"></a>00432 <span class="comment"> */</span> +<a name="l00433"></a>00433 InterruptableCalls::kill(serverPid, SIGINT); +<a name="l00434"></a>00434 +<a name="l00435"></a>00435 ret = InterruptableCalls::waitpid(serverPid, NULL, WNOHANG); +<a name="l00436"></a>00436 done = ret &gt; 0 || ret == -1; +<a name="l00437"></a>00437 <span class="keywordflow">if</span> (!done) { +<a name="l00438"></a>00438 InterruptableCalls::usleep(100000); +<a name="l00439"></a>00439 } +<a name="l00440"></a>00440 } +<a name="l00441"></a>00441 <span class="keywordflow">if</span> (done) { +<a name="l00442"></a>00442 P_TRACE(2, <span class="stringliteral">"ApplicationPoolServerExecutable exited."</span>); +<a name="l00443"></a>00443 } <span class="keywordflow">else</span> { +<a name="l00444"></a>00444 P_DEBUG(<span class="stringliteral">"ApplicationPoolServerExecutable not exited in time. Killing it..."</span>); +<a name="l00445"></a>00445 InterruptableCalls::kill(serverPid, SIGTERM); +<a name="l00446"></a>00446 InterruptableCalls::waitpid(serverPid, NULL, 0); +<a name="l00447"></a>00447 } +<a name="l00448"></a>00448 +<a name="l00449"></a>00449 serverSocket = -1; +<a name="l00450"></a>00450 serverPid = 0; +<a name="l00451"></a>00451 } +<a name="l00452"></a>00452 <span class="comment"></span> +<a name="l00453"></a>00453 <span class="comment"> /**</span> +<a name="l00454"></a>00454 <span class="comment"> * Start an ApplicationPool server process. If there's already one running,</span> +<a name="l00455"></a>00455 <span class="comment"> * then the currently running one will be shutdown.</span> +<a name="l00456"></a>00456 <span class="comment"> *</span> +<a name="l00457"></a>00457 <span class="comment"> * @pre System call interruption is disabled.</span> +<a name="l00458"></a>00458 <span class="comment"> * @post serverSocket != -1 &amp;&amp; serverPid != 0</span> +<a name="l00459"></a>00459 <span class="comment"> * @throw SystemException Something went wrong.</span> +<a name="l00460"></a>00460 <span class="comment"> */</span> +<a name="l00461"></a>00461 <span class="keywordtype">void</span> restartServer() { +<a name="l00462"></a>00462 <span class="keywordtype">int</span> fds[2]; +<a name="l00463"></a>00463 pid_t pid; +<a name="l00464"></a>00464 +<a name="l00465"></a>00465 <span class="keywordflow">if</span> (serverPid != 0) { +<a name="l00466"></a>00466 shutdownServer(); +<a name="l00467"></a>00467 } +<a name="l00468"></a>00468 +<a name="l00469"></a>00469 <span class="keywordflow">if</span> (InterruptableCalls::socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) { +<a name="l00470"></a>00470 <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 create a Unix socket pair"</span>, errno); +<a name="l00471"></a>00471 } +<a name="l00472"></a>00472 +<a name="l00473"></a>00473 createStatusReportFIFO(); +<a name="l00474"></a>00474 +<a name="l00475"></a>00475 pid = InterruptableCalls::fork(); +<a name="l00476"></a>00476 <span class="keywordflow">if</span> (pid == 0) { <span class="comment">// Child process.</span> +<a name="l00477"></a>00477 dup2(fds[0], SERVER_SOCKET_FD); +<a name="l00478"></a>00478 +<a name="l00479"></a>00479 <span class="comment">// Close all unnecessary file descriptors</span> +<a name="l00480"></a>00480 <span class="keywordflow">for</span> (<span class="keywordtype">long</span> i = sysconf(_SC_OPEN_MAX) - 1; i &gt; SERVER_SOCKET_FD; i--) { +<a name="l00481"></a>00481 close(i); +<a name="l00482"></a>00482 } +<a name="l00483"></a>00483 +<a name="l00484"></a>00484 execlp( +<a name="l00485"></a>00485 #<span class="keywordflow">if</span> 0 <a name="l00486"></a>00486 <span class="stringliteral">"valgrind"</span>, -<a name="l00487"></a>00487 #<span class="keywordflow">else</span> -<a name="l00488"></a>00488 m_serverExecutable.c_str(), -<a name="l00489"></a>00489 <span class="preprocessor"> #endif</span> -<a name="l00490"></a>00490 <span class="preprocessor"></span> m_serverExecutable.c_str(), -<a name="l00491"></a>00491 <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(Passenger::getLogLevel()).c_str(), -<a name="l00492"></a>00492 m_spawnServerCommand.c_str(), -<a name="l00493"></a>00493 m_logFile.c_str(), -<a name="l00494"></a>00494 m_rubyCommand.c_str(), -<a name="l00495"></a>00495 m_user.c_str(), -<a name="l00496"></a>00496 statusReportFIFO.c_str(), -<a name="l00497"></a>00497 NULL); -<a name="l00498"></a>00498 <span class="keywordtype">int</span> e = errno; -<a name="l00499"></a>00499 fprintf(stderr, <span class="stringliteral">"*** Passenger ERROR: Cannot execute %s: %s (%d)\n"</span>, -<a name="l00500"></a>00500 m_serverExecutable.c_str(), strerror(e), e); -<a name="l00501"></a>00501 fflush(stderr); -<a name="l00502"></a>00502 _exit(1); -<a name="l00503"></a>00503 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (pid == -1) { <span class="comment">// Error.</span> -<a name="l00504"></a>00504 InterruptableCalls::close(fds[0]); -<a name="l00505"></a>00505 InterruptableCalls::close(fds[1]); -<a name="l00506"></a>00506 <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 create a new process"</span>, errno); -<a name="l00507"></a>00507 } <span class="keywordflow">else</span> { <span class="comment">// Parent process.</span> -<a name="l00508"></a>00508 InterruptableCalls::close(fds[0]); -<a name="l00509"></a>00509 serverSocket = fds[1]; -<a name="l00510"></a>00510 serverPid = pid; -<a name="l00511"></a>00511 } -<a name="l00512"></a>00512 } -<a name="l00513"></a>00513 -<a name="l00514"></a>00514 <span class="keywordtype">void</span> createStatusReportFIFO() { -<a name="l00515"></a>00515 <span class="keywordtype">char</span> filename[PATH_MAX]; -<a name="l00516"></a>00516 <span class="keywordtype">int</span> ret; -<a name="l00517"></a>00517 -<a name="l00518"></a>00518 snprintf(filename, <span class="keyword">sizeof</span>(filename), <span class="stringliteral">"/tmp/passenger_status.%d.fifo"</span>, -<a name="l00519"></a>00519 getpid()); -<a name="l00520"></a>00520 filename[PATH_MAX - 1] = <span class="charliteral">'\0'</span>; -<a name="l00521"></a>00521 <span class="keywordflow">do</span> { -<a name="l00522"></a>00522 ret = mkfifo(filename, S_IRUSR | S_IWUSR); -<a name="l00523"></a>00523 } <span class="keywordflow">while</span> (ret == -1 &amp;&amp; errno == EINTR); -<a name="l00524"></a>00524 <span class="keywordflow">if</span> (ret == -1 &amp;&amp; errno != EEXIST) { -<a name="l00525"></a>00525 <span class="keywordtype">int</span> e = errno; -<a name="l00526"></a>00526 P_WARN(<span class="stringliteral">"*** WARNING: Could not create FIFO '"</span> &lt;&lt; filename &lt;&lt; -<a name="l00527"></a>00527 <span class="stringliteral">"': "</span> &lt;&lt; strerror(e) &lt;&lt; <span class="stringliteral">" ("</span> &lt;&lt; e &lt;&lt; <span class="stringliteral">")"</span> &lt;&lt; endl &lt;&lt; -<a name="l00528"></a>00528 <span class="stringliteral">"Disabling Passenger ApplicationPool status reporting."</span>); -<a name="l00529"></a>00529 statusReportFIFO = <span class="stringliteral">""</span>; -<a name="l00530"></a>00530 } <span class="keywordflow">else</span> { -<a name="l00531"></a>00531 statusReportFIFO = filename; -<a name="l00532"></a>00532 } -<a name="l00533"></a>00533 } -<a name="l00534"></a>00534 -<a name="l00535"></a>00535 <span class="keyword">public</span>:<span class="comment"></span> -<a name="l00536"></a>00536 <span class="comment"> /**</span> -<a name="l00537"></a>00537 <span class="comment"> * Create a new ApplicationPoolServer object.</span> -<a name="l00538"></a>00538 <span class="comment"> *</span> -<a name="l00539"></a>00539 <span class="comment"> * @param serverExecutable The filename of the ApplicationPool server</span> -<a name="l00540"></a>00540 <span class="comment"> * executable to use.</span> -<a name="l00541"></a>00541 <span class="comment"> * @param spawnServerCommand The filename of the spawn server to use.</span> -<a name="l00542"></a>00542 <span class="comment"> * @param logFile Specify a log file that the spawn server should use.</span> -<a name="l00543"></a>00543 <span class="comment"> * Messages on its standard output and standard error channels</span> -<a name="l00544"></a>00544 <span class="comment"> * will be written to this log file. If an empty string is</span> -<a name="l00545"></a>00545 <span class="comment"> * specified, no log file will be used, and the spawn server</span> -<a name="l00546"></a>00546 <span class="comment"> * will use the same standard output/error channels as the</span> -<a name="l00547"></a>00547 <span class="comment"> * current process.</span> -<a name="l00548"></a>00548 <span class="comment"> * @param rubyCommand The Ruby interpreter's command.</span> -<a name="l00549"></a>00549 <span class="comment"> * @param user The user that the spawn manager should run as. This</span> -<a name="l00550"></a>00550 <span class="comment"> * parameter only has effect if the current process is</span> -<a name="l00551"></a>00551 <span class="comment"> * running as root. If the empty string is given, or if</span> -<a name="l00552"></a>00552 <span class="comment"> * the &lt;tt&gt;user&lt;/tt&gt; is not a valid username, then</span> -<a name="l00553"></a>00553 <span class="comment"> * the spawn manager will be run as the current user.</span> -<a name="l00554"></a>00554 <span class="comment"> * @throws SystemException An error occured while trying to setup the spawn server</span> -<a name="l00555"></a>00555 <span class="comment"> * or the server socket.</span> -<a name="l00556"></a>00556 <span class="comment"> * @throws IOException The specified log file could not be opened.</span> -<a name="l00557"></a>00557 <span class="comment"> */</span> -<a name="l00558"></a><a class="code" href="classPassenger_1_1ApplicationPoolServer.html#2203db62a022579d569ceddff8ef6ca9">00558</a> <a class="code" href="classPassenger_1_1ApplicationPoolServer.html#2203db62a022579d569ceddff8ef6ca9" title="Create a new ApplicationPoolServer object.">ApplicationPoolServer</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;serverExecutable, -<a name="l00559"></a>00559 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;spawnServerCommand, -<a name="l00560"></a>00560 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;logFile = <span class="stringliteral">""</span>, -<a name="l00561"></a>00561 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;rubyCommand = <span class="stringliteral">"ruby"</span>, -<a name="l00562"></a>00562 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;user = <span class="stringliteral">""</span>) -<a name="l00563"></a>00563 : m_serverExecutable(serverExecutable), -<a name="l00564"></a>00564 m_spawnServerCommand(spawnServerCommand), -<a name="l00565"></a>00565 m_logFile(logFile), -<a name="l00566"></a>00566 m_rubyCommand(rubyCommand), -<a name="l00567"></a>00567 m_user(user) { -<a name="l00568"></a>00568 serverSocket = -1; -<a name="l00569"></a>00569 serverPid = 0; -<a name="l00570"></a>00570 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; -<a name="l00571"></a>00571 restartServer(); -<a name="l00572"></a>00572 } -<a name="l00573"></a>00573 -<a name="l00574"></a>00574 ~<a class="code" href="classPassenger_1_1ApplicationPoolServer.html" title="Multi-process usage support for ApplicationPool.">ApplicationPoolServer</a>() { -<a name="l00575"></a>00575 <span class="keywordflow">if</span> (serverSocket != -1) { -<a name="l00576"></a>00576 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; -<a name="l00577"></a>00577 shutdownServer(); -<a name="l00578"></a>00578 } +<a name="l00487"></a>00487 <span class="stringliteral">"valgrind"</span>, +<a name="l00488"></a>00488 #<span class="keywordflow">else</span> +<a name="l00489"></a>00489 m_serverExecutable.c_str(), +<a name="l00490"></a>00490 <span class="preprocessor"> #endif</span> +<a name="l00491"></a>00491 <span class="preprocessor"></span> m_serverExecutable.c_str(), +<a name="l00492"></a>00492 <a class="code" href="group__Support.html#gf6fab368d70c18fdf16bf5a24f630407" title="Convert anything to a string.">toString</a>(Passenger::getLogLevel()).c_str(), +<a name="l00493"></a>00493 m_spawnServerCommand.c_str(), +<a name="l00494"></a>00494 m_logFile.c_str(), +<a name="l00495"></a>00495 m_rubyCommand.c_str(), +<a name="l00496"></a>00496 m_user.c_str(), +<a name="l00497"></a>00497 statusReportFIFO.c_str(), +<a name="l00498"></a>00498 NULL); +<a name="l00499"></a>00499 <span class="keywordtype">int</span> e = errno; +<a name="l00500"></a>00500 fprintf(stderr, <span class="stringliteral">"*** Passenger ERROR: Cannot execute %s: %s (%d)\n"</span>, +<a name="l00501"></a>00501 m_serverExecutable.c_str(), strerror(e), e); +<a name="l00502"></a>00502 fflush(stderr); +<a name="l00503"></a>00503 _exit(1); +<a name="l00504"></a>00504 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (pid == -1) { <span class="comment">// Error.</span> +<a name="l00505"></a>00505 InterruptableCalls::close(fds[0]); +<a name="l00506"></a>00506 InterruptableCalls::close(fds[1]); +<a name="l00507"></a>00507 <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 create a new process"</span>, errno); +<a name="l00508"></a>00508 } <span class="keywordflow">else</span> { <span class="comment">// Parent process.</span> +<a name="l00509"></a>00509 InterruptableCalls::close(fds[0]); +<a name="l00510"></a>00510 serverSocket = fds[1]; +<a name="l00511"></a>00511 +<a name="l00512"></a>00512 <span class="keywordtype">int</span> flags = fcntl(serverSocket, F_GETFD); +<a name="l00513"></a>00513 <span class="keywordflow">if</span> (flags != -1) { +<a name="l00514"></a>00514 fcntl(serverSocket, F_SETFD, flags | FD_CLOEXEC); +<a name="l00515"></a>00515 } +<a name="l00516"></a>00516 +<a name="l00517"></a>00517 serverPid = pid; +<a name="l00518"></a>00518 } +<a name="l00519"></a>00519 } +<a name="l00520"></a>00520 +<a name="l00521"></a>00521 <span class="keywordtype">void</span> createStatusReportFIFO() { +<a name="l00522"></a>00522 <span class="keywordtype">char</span> filename[PATH_MAX]; +<a name="l00523"></a>00523 <span class="keywordtype">int</span> ret; +<a name="l00524"></a>00524 +<a name="l00525"></a>00525 snprintf(filename, <span class="keyword">sizeof</span>(filename), <span class="stringliteral">"/tmp/passenger_status.%d.fifo"</span>, +<a name="l00526"></a>00526 getpid()); +<a name="l00527"></a>00527 filename[PATH_MAX - 1] = <span class="charliteral">'\0'</span>; +<a name="l00528"></a>00528 <span class="keywordflow">do</span> { +<a name="l00529"></a>00529 ret = mkfifo(filename, S_IRUSR | S_IWUSR); +<a name="l00530"></a>00530 } <span class="keywordflow">while</span> (ret == -1 &amp;&amp; errno == EINTR); +<a name="l00531"></a>00531 <span class="keywordflow">if</span> (ret == -1 &amp;&amp; errno != EEXIST) { +<a name="l00532"></a>00532 <span class="keywordtype">int</span> e = errno; +<a name="l00533"></a>00533 P_WARN(<span class="stringliteral">"*** WARNING: Could not create FIFO '"</span> &lt;&lt; filename &lt;&lt; +<a name="l00534"></a>00534 <span class="stringliteral">"': "</span> &lt;&lt; strerror(e) &lt;&lt; <span class="stringliteral">" ("</span> &lt;&lt; e &lt;&lt; <span class="stringliteral">")"</span> &lt;&lt; endl &lt;&lt; +<a name="l00535"></a>00535 <span class="stringliteral">"Disabling Passenger ApplicationPool status reporting."</span>); +<a name="l00536"></a>00536 statusReportFIFO = <span class="stringliteral">""</span>; +<a name="l00537"></a>00537 } <span class="keywordflow">else</span> { +<a name="l00538"></a>00538 statusReportFIFO = filename; +<a name="l00539"></a>00539 } +<a name="l00540"></a>00540 } +<a name="l00541"></a>00541 +<a name="l00542"></a>00542 <span class="keyword">public</span>:<span class="comment"></span> +<a name="l00543"></a>00543 <span class="comment"> /**</span> +<a name="l00544"></a>00544 <span class="comment"> * Create a new ApplicationPoolServer object.</span> +<a name="l00545"></a>00545 <span class="comment"> *</span> +<a name="l00546"></a>00546 <span class="comment"> * @param serverExecutable The filename of the ApplicationPool server</span> +<a name="l00547"></a>00547 <span class="comment"> * executable to use.</span> +<a name="l00548"></a>00548 <span class="comment"> * @param spawnServerCommand The filename of the spawn server to use.</span> +<a name="l00549"></a>00549 <span class="comment"> * @param logFile Specify a log file that the spawn server should use.</span> +<a name="l00550"></a>00550 <span class="comment"> * Messages on its standard output and standard error channels</span> +<a name="l00551"></a>00551 <span class="comment"> * will be written to this log file. If an empty string is</span> +<a name="l00552"></a>00552 <span class="comment"> * specified, no log file will be used, and the spawn server</span> +<a name="l00553"></a>00553 <span class="comment"> * will use the same standard output/error channels as the</span> +<a name="l00554"></a>00554 <span class="comment"> * current process.</span> +<a name="l00555"></a>00555 <span class="comment"> * @param rubyCommand The Ruby interpreter's command.</span> +<a name="l00556"></a>00556 <span class="comment"> * @param user The user that the spawn manager should run as. This</span> +<a name="l00557"></a>00557 <span class="comment"> * parameter only has effect if the current process is</span> +<a name="l00558"></a>00558 <span class="comment"> * running as root. If the empty string is given, or if</span> +<a name="l00559"></a>00559 <span class="comment"> * the &lt;tt&gt;user&lt;/tt&gt; is not a valid username, then</span> +<a name="l00560"></a>00560 <span class="comment"> * the spawn manager will be run as the current user.</span> +<a name="l00561"></a>00561 <span class="comment"> * @throws SystemException An error occured while trying to setup the spawn server</span> +<a name="l00562"></a>00562 <span class="comment"> * or the server socket.</span> +<a name="l00563"></a>00563 <span class="comment"> * @throws IOException The specified log file could not be opened.</span> +<a name="l00564"></a>00564 <span class="comment"> */</span> +<a name="l00565"></a><a class="code" href="classPassenger_1_1ApplicationPoolServer.html#2203db62a022579d569ceddff8ef6ca9">00565</a> <a class="code" href="classPassenger_1_1ApplicationPoolServer.html#2203db62a022579d569ceddff8ef6ca9" title="Create a new ApplicationPoolServer object.">ApplicationPoolServer</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;serverExecutable, +<a name="l00566"></a>00566 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;spawnServerCommand, +<a name="l00567"></a>00567 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;logFile = <span class="stringliteral">""</span>, +<a name="l00568"></a>00568 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;rubyCommand = <span class="stringliteral">"ruby"</span>, +<a name="l00569"></a>00569 <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;user = <span class="stringliteral">""</span>) +<a name="l00570"></a>00570 : m_serverExecutable(serverExecutable), +<a name="l00571"></a>00571 m_spawnServerCommand(spawnServerCommand), +<a name="l00572"></a>00572 m_logFile(logFile), +<a name="l00573"></a>00573 m_rubyCommand(rubyCommand), +<a name="l00574"></a>00574 m_user(user) { +<a name="l00575"></a>00575 serverSocket = -1; +<a name="l00576"></a>00576 serverPid = 0; +<a name="l00577"></a>00577 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; +<a name="l00578"></a>00578 restartServer(); <a name="l00579"></a>00579 } -<a name="l00580"></a>00580 <span class="comment"></span> -<a name="l00581"></a>00581 <span class="comment"> /**</span> -<a name="l00582"></a>00582 <span class="comment"> * Connects to the server and returns a usable ApplicationPool object.</span> -<a name="l00583"></a>00583 <span class="comment"> * All cache/pool data of this ApplicationPool is actually stored on</span> -<a name="l00584"></a>00584 <span class="comment"> * the server and shared with other clients, but that is totally</span> -<a name="l00585"></a>00585 <span class="comment"> * transparent to the user of the ApplicationPool object.</span> -<a name="l00586"></a>00586 <span class="comment"> *</span> -<a name="l00587"></a>00587 <span class="comment"> * @note</span> -<a name="l00588"></a>00588 <span class="comment"> * All methods of the returned ApplicationPool object may throw</span> -<a name="l00589"></a>00589 <span class="comment"> * SystemException, IOException or boost::thread_interrupted.</span> -<a name="l00590"></a>00590 <span class="comment"> *</span> -<a name="l00591"></a>00591 <span class="comment"> * @warning</span> -<a name="l00592"></a>00592 <span class="comment"> * One may only use the returned ApplicationPool object for handling</span> -<a name="l00593"></a>00593 <span class="comment"> * one session at a time. For example, don't do stuff like this:</span> -<a name="l00594"></a>00594 <span class="comment"> * @code</span> -<a name="l00595"></a>00595 <span class="comment"> * ApplicationPoolPtr pool = server.connect();</span> -<a name="l00596"></a>00596 <span class="comment"> * Application::SessionPtr session1 = pool-&gt;get(...);</span> -<a name="l00597"></a>00597 <span class="comment"> * Application::SessionPtr session2 = pool-&gt;get(...);</span> -<a name="l00598"></a>00598 <span class="comment"> * @endcode</span> -<a name="l00599"></a>00599 <span class="comment"> * Otherwise, a deadlock can occur under certain circumstances.</span> -<a name="l00600"></a>00600 <span class="comment"> * @warning</span> -<a name="l00601"></a>00601 <span class="comment"> * Instead, one should call connect() multiple times:</span> -<a name="l00602"></a>00602 <span class="comment"> * @code</span> -<a name="l00603"></a>00603 <span class="comment"> * ApplicationPoolPtr pool1 = server.connect();</span> -<a name="l00604"></a>00604 <span class="comment"> * Application::SessionPtr session1 = pool1-&gt;get(...);</span> -<a name="l00605"></a>00605 <span class="comment"> * </span> -<a name="l00606"></a>00606 <span class="comment"> * ApplicationPoolPtr pool2 = server.connect();</span> -<a name="l00607"></a>00607 <span class="comment"> * Application::SessionPtr session2 = pool2-&gt;get(...);</span> -<a name="l00608"></a>00608 <span class="comment"> * @endcode</span> -<a name="l00609"></a>00609 <span class="comment"> *</span> -<a name="l00610"></a>00610 <span class="comment"> * @throws SystemException Something went wrong.</span> -<a name="l00611"></a>00611 <span class="comment"> * @throws IOException Something went wrong.</span> -<a name="l00612"></a>00612 <span class="comment"> */</span> -<a name="l00613"></a><a class="code" href="classPassenger_1_1ApplicationPoolServer.html#18f77057cc28e7924a8f4d1397aa0468">00613</a> ApplicationPoolPtr <a class="code" href="classPassenger_1_1ApplicationPoolServer.html#18f77057cc28e7924a8f4d1397aa0468" title="Connects to the server and returns a usable ApplicationPool object.">connect</a>() { -<a name="l00614"></a>00614 <span class="keywordflow">try</span> { -<a name="l00615"></a>00615 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; -<a name="l00616"></a>00616 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(serverSocket); -<a name="l00617"></a>00617 <span class="keywordtype">int</span> clientConnection; -<a name="l00618"></a>00618 -<a name="l00619"></a>00619 <span class="comment">// Write some random data to wake up the server.</span> -<a name="l00620"></a>00620 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#069314e4c7e1fe8c8ab36e16d2cc5fef" title="Send a block of data over the underlying file descriptor.">writeRaw</a>(<span class="stringliteral">"x"</span>, 1); -<a name="l00621"></a>00621 -<a name="l00622"></a>00622 clientConnection = channel.<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="l00623"></a>00623 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g41b6c4a82fed72531a147de0505a8396" title="Convenience shortcut for creating a shared_ptr.">ptr</a>(<span class="keyword">new</span> Client(clientConnection)); -<a name="l00624"></a>00624 } <span class="keywordflow">catch</span> (<span class="keyword">const</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> &amp;e) { -<a name="l00625"></a>00625 <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">"Could not connect to the ApplicationPool server"</span>, e.<a class="code" href="classPassenger_1_1SystemException.html#ee7a6672bf79b72a4c3ee70c57d6a47c" title="The value of errno at the time the error occured.">code</a>()); -<a name="l00626"></a>00626 } <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="classPassenger_1_1IOException.html" title="Represents an error that occured during an I/O operation.">IOException</a> &amp;e) { -<a name="l00627"></a>00627 <span class="keywordtype">string</span> message(<span class="stringliteral">"Could not connect to the ApplicationPool server: "</span>); -<a name="l00628"></a>00628 message.append(e.<a class="code" href="classPassenger_1_1IOException.html#1b65d0fdb9bc5136f5b7df759c14768a">what</a>()); -<a name="l00629"></a>00629 <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>(message); -<a name="l00630"></a>00630 } -<a name="l00631"></a>00631 } -<a name="l00632"></a>00632 <span class="comment"></span> -<a name="l00633"></a>00633 <span class="comment"> /**</span> -<a name="l00634"></a>00634 <span class="comment"> * Detach the server, thereby telling it that we don't want to connect</span> -<a name="l00635"></a>00635 <span class="comment"> * to it anymore. This frees up some resources in the current process,</span> -<a name="l00636"></a>00636 <span class="comment"> * such as file descriptors.</span> -<a name="l00637"></a>00637 <span class="comment"> *</span> -<a name="l00638"></a>00638 <span class="comment"> * This method is particularily useful to Apache worker processes that</span> -<a name="l00639"></a>00639 <span class="comment"> * have just established a connection with the ApplicationPool server.</span> -<a name="l00640"></a>00640 <span class="comment"> * Any sessions that are opened prior to calling detach(), will keep</span> -<a name="l00641"></a>00641 <span class="comment"> * working even after a detach().</span> -<a name="l00642"></a>00642 <span class="comment"> *</span> -<a name="l00643"></a>00643 <span class="comment"> * This method may only be called once. The ApplicationPoolServer object</span> -<a name="l00644"></a>00644 <span class="comment"> * will become unusable once detach() has been called, so call connect()</span> -<a name="l00645"></a>00645 <span class="comment"> * before calling detach().</span> -<a name="l00646"></a>00646 <span class="comment"> */</span> -<a name="l00647"></a><a class="code" href="classPassenger_1_1ApplicationPoolServer.html#b13957e32ae8ca36ba85f2e6ffefd5ce">00647</a> <span class="keywordtype">void</span> <a class="code" href="classPassenger_1_1ApplicationPoolServer.html#b13957e32ae8ca36ba85f2e6ffefd5ce" title="Detach the server, thereby telling it that we don&amp;#39;t want to connect to it anymore...">detach</a>() { -<a name="l00648"></a>00648 <span class="keywordtype">int</span> ret; -<a name="l00649"></a>00649 <span class="keywordflow">do</span> { -<a name="l00650"></a>00650 ret = close(serverSocket); -<a name="l00651"></a>00651 } <span class="keywordflow">while</span> (ret == -1 &amp;&amp; errno == EINTR); -<a name="l00652"></a>00652 serverSocket = -1; -<a name="l00653"></a>00653 } -<a name="l00654"></a>00654 }; -<a name="l00655"></a>00655 -<a name="l00656"></a>00656 <span class="keyword">typedef</span> shared_ptr&lt;ApplicationPoolServer&gt; ApplicationPoolServerPtr; -<a name="l00657"></a>00657 -<a name="l00658"></a>00658 } <span class="comment">// namespace Passenger</span> -<a name="l00659"></a>00659 -<a name="l00660"></a>00660 <span class="preprocessor">#endif </span><span class="comment">/* _PASSENGER_APPLICATION_POOL_SERVER_H_ */</span> +<a name="l00580"></a>00580 +<a name="l00581"></a>00581 ~<a class="code" href="classPassenger_1_1ApplicationPoolServer.html" title="Multi-process usage support for ApplicationPool.">ApplicationPoolServer</a>() { +<a name="l00582"></a>00582 <span class="keywordflow">if</span> (serverSocket != -1) { +<a name="l00583"></a>00583 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; +<a name="l00584"></a>00584 shutdownServer(); +<a name="l00585"></a>00585 } +<a name="l00586"></a>00586 } +<a name="l00587"></a>00587 <span class="comment"></span> +<a name="l00588"></a>00588 <span class="comment"> /**</span> +<a name="l00589"></a>00589 <span class="comment"> * Connects to the server and returns a usable ApplicationPool object.</span> +<a name="l00590"></a>00590 <span class="comment"> * All cache/pool data of this ApplicationPool is actually stored on</span> +<a name="l00591"></a>00591 <span class="comment"> * the server and shared with other clients, but that is totally</span> +<a name="l00592"></a>00592 <span class="comment"> * transparent to the user of the ApplicationPool object.</span> +<a name="l00593"></a>00593 <span class="comment"> *</span> +<a name="l00594"></a>00594 <span class="comment"> * @note</span> +<a name="l00595"></a>00595 <span class="comment"> * All methods of the returned ApplicationPool object may throw</span> +<a name="l00596"></a>00596 <span class="comment"> * SystemException, IOException or boost::thread_interrupted.</span> +<a name="l00597"></a>00597 <span class="comment"> *</span> +<a name="l00598"></a>00598 <span class="comment"> * @warning</span> +<a name="l00599"></a>00599 <span class="comment"> * One may only use the returned ApplicationPool object for handling</span> +<a name="l00600"></a>00600 <span class="comment"> * one session at a time. For example, don't do stuff like this:</span> +<a name="l00601"></a>00601 <span class="comment"> * @code</span> +<a name="l00602"></a>00602 <span class="comment"> * ApplicationPoolPtr pool = server.connect();</span> +<a name="l00603"></a>00603 <span class="comment"> * Application::SessionPtr session1 = pool-&gt;get(...);</span> +<a name="l00604"></a>00604 <span class="comment"> * Application::SessionPtr session2 = pool-&gt;get(...);</span> +<a name="l00605"></a>00605 <span class="comment"> * @endcode</span> +<a name="l00606"></a>00606 <span class="comment"> * Otherwise, a deadlock can occur under certain circumstances.</span> +<a name="l00607"></a>00607 <span class="comment"> * @warning</span> +<a name="l00608"></a>00608 <span class="comment"> * Instead, one should call connect() multiple times:</span> +<a name="l00609"></a>00609 <span class="comment"> * @code</span> +<a name="l00610"></a>00610 <span class="comment"> * ApplicationPoolPtr pool1 = server.connect();</span> +<a name="l00611"></a>00611 <span class="comment"> * Application::SessionPtr session1 = pool1-&gt;get(...);</span> +<a name="l00612"></a>00612 <span class="comment"> * </span> +<a name="l00613"></a>00613 <span class="comment"> * ApplicationPoolPtr pool2 = server.connect();</span> +<a name="l00614"></a>00614 <span class="comment"> * Application::SessionPtr session2 = pool2-&gt;get(...);</span> +<a name="l00615"></a>00615 <span class="comment"> * @endcode</span> +<a name="l00616"></a>00616 <span class="comment"> *</span> +<a name="l00617"></a>00617 <span class="comment"> * @throws SystemException Something went wrong.</span> +<a name="l00618"></a>00618 <span class="comment"> * @throws IOException Something went wrong.</span> +<a name="l00619"></a>00619 <span class="comment"> */</span> +<a name="l00620"></a><a class="code" href="classPassenger_1_1ApplicationPoolServer.html#18f77057cc28e7924a8f4d1397aa0468">00620</a> ApplicationPoolPtr <a class="code" href="classPassenger_1_1ApplicationPoolServer.html#18f77057cc28e7924a8f4d1397aa0468" title="Connects to the server and returns a usable ApplicationPool object.">connect</a>() { +<a name="l00621"></a>00621 <span class="keywordflow">try</span> { +<a name="l00622"></a>00622 <a class="code" href="classboost_1_1this__thread_1_1disable__syscall__interruption.html" title="Create this struct on the stack to temporarily disable system call interruption,...">this_thread::disable_syscall_interruption</a> dsi; +<a name="l00623"></a>00623 <a class="code" href="classPassenger_1_1MessageChannel.html" title="Convenience class for I/O operations on file descriptors.">MessageChannel</a> channel(serverSocket); +<a name="l00624"></a>00624 <span class="keywordtype">int</span> clientConnection; +<a name="l00625"></a>00625 +<a name="l00626"></a>00626 <span class="comment">// Write some random data to wake up the server.</span> +<a name="l00627"></a>00627 channel.<a class="code" href="classPassenger_1_1MessageChannel.html#069314e4c7e1fe8c8ab36e16d2cc5fef" title="Send a block of data over the underlying file descriptor.">writeRaw</a>(<span class="stringliteral">"x"</span>, 1); +<a name="l00628"></a>00628 +<a name="l00629"></a>00629 clientConnection = channel.<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="l00630"></a>00630 <span class="keywordflow">return</span> <a class="code" href="group__Support.html#g41b6c4a82fed72531a147de0505a8396" title="Convenience shortcut for creating a shared_ptr.">ptr</a>(<span class="keyword">new</span> Client(clientConnection)); +<a name="l00631"></a>00631 } <span class="keywordflow">catch</span> (<span class="keyword">const</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> &amp;e) { +<a name="l00632"></a>00632 <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">"Could not connect to the ApplicationPool server"</span>, e.<a class="code" href="classPassenger_1_1SystemException.html#ee7a6672bf79b72a4c3ee70c57d6a47c" title="The value of errno at the time the error occured.">code</a>()); +<a name="l00633"></a>00633 } <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="classPassenger_1_1IOException.html" title="Represents an error that occured during an I/O operation.">IOException</a> &amp;e) { +<a name="l00634"></a>00634 <span class="keywordtype">string</span> message(<span class="stringliteral">"Could not connect to the ApplicationPool server: "</span>); +<a name="l00635"></a>00635 message.append(e.<a class="code" href="classPassenger_1_1IOException.html#1b65d0fdb9bc5136f5b7df759c14768a">what</a>()); +<a name="l00636"></a>00636 <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>(message); +<a name="l00637"></a>00637 } +<a name="l00638"></a>00638 } +<a name="l00639"></a>00639 <span class="comment"></span> +<a name="l00640"></a>00640 <span class="comment"> /**</span> +<a name="l00641"></a>00641 <span class="comment"> * Detach the server, thereby telling it that we don't want to connect</span> +<a name="l00642"></a>00642 <span class="comment"> * to it anymore. This frees up some resources in the current process,</span> +<a name="l00643"></a>00643 <span class="comment"> * such as file descriptors.</span> +<a name="l00644"></a>00644 <span class="comment"> *</span> +<a name="l00645"></a>00645 <span class="comment"> * This method is particularily useful to Apache worker processes that</span> +<a name="l00646"></a>00646 <span class="comment"> * have just established a connection with the ApplicationPool server.</span> +<a name="l00647"></a>00647 <span class="comment"> * Any sessions that are opened prior to calling detach(), will keep</span> +<a name="l00648"></a>00648 <span class="comment"> * working even after a detach().</span> +<a name="l00649"></a>00649 <span class="comment"> *</span> +<a name="l00650"></a>00650 <span class="comment"> * This method may only be called once. The ApplicationPoolServer object</span> +<a name="l00651"></a>00651 <span class="comment"> * will become unusable once detach() has been called, so call connect()</span> +<a name="l00652"></a>00652 <span class="comment"> * before calling detach().</span> +<a name="l00653"></a>00653 <span class="comment"> */</span> +<a name="l00654"></a><a class="code" href="classPassenger_1_1ApplicationPoolServer.html#b13957e32ae8ca36ba85f2e6ffefd5ce">00654</a> <span class="keywordtype">void</span> <a class="code" href="classPassenger_1_1ApplicationPoolServer.html#b13957e32ae8ca36ba85f2e6ffefd5ce" title="Detach the server, thereby telling it that we don&amp;#39;t want to connect to it anymore...">detach</a>() { +<a name="l00655"></a>00655 <span class="keywordtype">int</span> ret; +<a name="l00656"></a>00656 <span class="keywordflow">do</span> { +<a name="l00657"></a>00657 ret = close(serverSocket); +<a name="l00658"></a>00658 } <span class="keywordflow">while</span> (ret == -1 &amp;&amp; errno == EINTR); +<a name="l00659"></a>00659 serverSocket = -1; +<a name="l00660"></a>00660 } +<a name="l00661"></a>00661 }; +<a name="l00662"></a>00662 +<a name="l00663"></a>00663 <span class="keyword">typedef</span> shared_ptr&lt;ApplicationPoolServer&gt; ApplicationPoolServerPtr; +<a name="l00664"></a>00664 +<a name="l00665"></a>00665 } <span class="comment">// namespace Passenger</span> +<a name="l00666"></a>00666 +<a name="l00667"></a>00667 <span class="preprocessor">#endif </span><span class="comment">/* _PASSENGER_APPLICATION_POOL_SERVER_H_ */</span> </pre></div></div> -<hr size="1"><address style="text-align: right;"><small>Generated on Mon Dec 1 14:27:22 2008 for Passenger by&nbsp; +<hr size="1"><address style="text-align: right;"><small>Generated on Fri Dec 5 13:53:01 2008 for Passenger by&nbsp; <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.5 </small></address> </body> </html>