public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Search Repo:
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
Merge branch 'external_app_pool'
Hongli Lai (Phusion) (author)
Thu May 01 01:37:57 -0700 2008
commit  147107259333b0a81b8440dfb5ecbed756627ec0
tree    f6ea80725d78ed29cd0d4b153c36bc7923b82dd5
parent  3ec5dfc66bbf8b99d5b6f3894c13bdcdda0b7ef7 parent  87db9e1464a75cb23f702086b45f174711f4012a
...
8
9
10
 
11
12
13
...
8
9
10
11
12
13
14
0
@@ -8,6 +8,7 @@
0
 ext/apache2/*.lo
0
 ext/apache2/*.slo
0
 ext/apache2/.libs
0
+ext/apache2/ApplicationPoolServerExecutable
0
 ext/passenger/Makefile
0
 ext/boost/src/*.a
0
 doc/rdoc
...
23
24
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
27
28
...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
0
@@ -23,6 +23,36 @@
0
 
0
  rake
0
 
0
+== Directory structure
0
+
0
+The most important directories are:
0
+[ lib/passenger ]
0
+ The source code for the spawn server, which is written in Ruby.
0
+[ ext/passenger ]
0
+ Native extensions for Ruby, used by the spawn server.
0
+[ ext/apache2 ]
0
+ The Passenger Apache 2 module (mod_passenger).
0
+[ bin ]
0
+ Executables.
0
+[ doc ]
0
+ Various documentation.
0
+[ test ]
0
+ Unit tests and integration tests.
0
+[ test/support ]
0
+ Support/utility code, used in the tests.
0
+[ test/stub ]
0
+ Stub code, used in the tests.
0
+
0
+Less important directories:
0
+[ lib/rake ]
0
+ Rake tasks.
0
+[ ext/boost ]
0
+ A vendor copy of the Boost C++ library (www.boost.org).
0
+[ benchmark ]
0
+ Benchmark tools.
0
+[ misc ]
0
+ Miscellaneous tools.
0
+
0
 == Tests
0
 
0
 Run all tests:
...
106
107
108
109
 
110
111
112
113
...
118
119
120
121
 
122
123
 
 
 
 
124
125
126
...
134
135
136
 
 
 
 
 
 
 
 
 
 
 
 
 
137
138
139
...
169
170
171
172
 
 
173
174
175
176
177
...
188
189
190
191
192
 
193
194
195
 
196
197
198
199
200
201
...
212
213
214
215
 
 
 
 
 
216
217
218
219
220
 
 
 
 
 
221
222
223
...
106
107
108
 
109
110
111
112
113
...
118
119
120
 
121
122
 
123
124
125
126
127
128
129
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
185
186
187
 
188
189
190
191
192
193
194
...
205
206
207
 
 
208
209
210
 
211
212
 
213
214
215
216
...
227
228
229
 
230
231
232
233
234
235
236
237
238
 
239
240
241
242
243
244
245
246
0
@@ -106,7 +106,7 @@
0
     'Configuration.o' => %w(Configuration.cpp Configuration.h),
0
     'Hooks.o' => %w(Hooks.cpp Hooks.h
0
         Configuration.h ApplicationPool.h StandardApplicationPool.h
0
- ApplicationPoolClientServer.h
0
+ ApplicationPoolServer.h
0
         SpawnManager.h Exceptions.h Application.h MessageChannel.h
0
         Utils.h),
0
     'Utils.o' => %w(Utils.cpp Utils.h),
0
0
@@ -118,9 +118,12 @@
0
   apxs_objects = APACHE2::OBJECTS.keys.join(',')
0
 
0
   desc "Build mod_passenger Apache 2 module"
0
- task :apache2 => ['mod_passenger.so', ', :native_support]
0
+ task :apache2 => ['mod_passenger.so', 'ApplicationPoolServerExecutable', :native_support]
0
   
0
- file 'mod_passenger.so' => ['../boost/src/libboost_thread.a', 'mod_passenger.o'] + APACHE2::OBJECTS.keys do
0
+ file 'mod_passenger.so' => [
0
+ '../boost/src/libboost_thread.a',
0
+ 'mod_passenger.o'
0
+ ] + APACHE2::OBJECTS.keys do
0
     # apxs totally sucks. We couldn't get it working correctly
0
     # on MacOS X (it had various problems with building universal
0
     # binaries), so we decided to ditch it and build/install the
0
@@ -134,6 +137,19 @@
0
       linkflags
0
   end
0
   
0
+ file 'ApplicationPoolServerExecutable' => [
0
+ '../boost/src/libboost_thread.a',
0
+ 'ApplicationPoolServerExecutable.cpp',
0
+ 'ApplicationPool.h',
0
+ 'StandardApplicationPool.h',
0
+ 'Utils.o',
0
+ 'Logging.o'
0
+ ] do
0
+ create_executable "ApplicationPoolServerExecutable",
0
+ 'ApplicationPoolServerExecutable.cpp Utils.o Logging.o',
0
+ "-I.. #{CXXFLAGS} #{LDFLAGS} ../boost/src/libboost_thread.a -lpthread"
0
+ end
0
+
0
   desc "Install mod_passenger Apache 2 module"
0
   task 'apache2:install' => :apache2 do
0
     install_dir = `#{APXS2} -q LIBEXECDIR`.strip
0
@@ -169,7 +185,8 @@
0
   
0
   desc "Remove generated files for mod_passenger Apache 2 module"
0
   task 'apache2:clean' do
0
- files = [APACHE2::OBJECTS.keys, %w(mod_passenger.o mod_passenger.so)]
0
+ files = [APACHE2::OBJECTS.keys, %w(mod_passenger.o mod_passenger.so
0
+ ApplicationPoolServerExecutable)]
0
     sh("rm", "-rf", *files.flatten)
0
   end
0
 end
0
0
0
@@ -188,13 +205,11 @@
0
       ../ext/apache2/SpawnManager.h
0
       ../ext/apache2/Application.h),
0
     'ApplicationPoolServerTest.o' => %w(ApplicationPoolServerTest.cpp
0
- ../ext/apache2/StandardApplicationPool.h
0
- ../ext/apache2/ApplicationPoolClientServer.h),
0
+ ../ext/apache2/ApplicationPoolServer.h),
0
     'ApplicationPoolServer_ApplicationPoolTest.o' => %w(ApplicationPoolServer_ApplicationPoolTest.cpp
0
       ApplicationPoolTest.cpp
0
- ../ext/apache2/ApplicationPoolClientServer.h
0
+ ../ext/apache2/ApplicationPoolServer.h
0
       ../ext/apache2/ApplicationPool.h
0
- ../ext/apache2/StandardApplicationPool.h
0
       ../ext/apache2/SpawnManager.h
0
       ../ext/apache2/Application.h),
0
     'StandardApplicationPoolTest.o' => %w(StandardApplicationPoolTest.cpp
0
0
@@ -212,12 +227,20 @@
0
   task :test => [:'test:apache2', :'test:ruby', :'test:integration']
0
   
0
   desc "Run unit tests for the Apache 2 module"
0
- task 'test:apache2' => ['Apache2ModuleTests', :native_support] do
0
+ task 'test:apache2' => [
0
+ 'Apache2ModuleTests',
0
+ '../ext/apache2/ApplicationPoolServerExecutable',
0
+ :native_support
0
+ ] do
0
     sh "./Apache2ModuleTests"
0
   end
0
   
0
   desc "Run unit tests for the Apache 2 module in Valgrind"
0
- task 'test:valgrind' => ['Apache2ModuleTests', :native_support] do
0
+ task 'test:valgrind' => [
0
+ 'Apache2ModuleTests',
0
+ '../ext/apache2/ApplicationPoolServerExecutable',
0
+ :native_support
0
+ ] do
0
     sh "valgrind #{ENV['ARGS']} ./Apache2ModuleTests"
0
   end
0
   
...
177
178
179
180
 
181
182
183
...
177
178
179
 
180
181
182
183
0
@@ -177,7 +177,7 @@
0
     line
0
     render_template 'apache2_config_snippets',
0
       :module_location => "#{PASSENGER_ROOT}/ext/apache2/mod_passenger.so",
0
- :spawn_server_location => "#{PASSENGER_ROOT}/bin/passenger-spawn-server",
0
+ :passenger_root => PASSENGER_ROOT,
0
       :ruby => RUBY
0
     wait
0
   end
...
234
235
236
237
238
 
 
239
240
241
242
243
244
 
245
246
247
...
601
602
603
604
 
605
606
607
...
752
753
754
755
 
756
757
758
...
765
766
767
768
 
769
770
771
...
234
235
236
 
 
237
238
239
240
241
 
 
 
242
243
244
245
...
599
600
601
 
602
603
604
605
...
750
751
752
 
753
754
755
756
...
763
764
765
 
766
767
768
769
0
@@ -234,14 +234,12 @@
0
 Passenger's behavior. Passenger's Apache module supports the following
0
 configuration options:
0
 
0
-RailsSpawnServer <filename>::
0
- The location to the Passenger spawn server. This configuration option is
0
+PassengerRoot <directory>::
0
+ The location to the Passenger root directory. This configuration option is
0
   essential to Passenger. The correct value is given by the installer,
0
   and should usually not be changed manually.
0
 +
0
-This option may only occur once, in the global server configuration. If
0
-this option is not given, then Passenger will look for the program
0
-`passenger-spawn-server` in `$PATH`.
0
+This required option may only occur once, in the global server configuration.
0
 
0
 RailsBaseURI <uri>::
0
   Used to specify that the given URI is a Rails application. See
0
@@ -601,7 +599,7 @@
0
 
0
 ==== The Apache error log says that the spawn manager script does not exist, or that it does not have permission to execute it ====
0
 
0
-If you are sure that the RailsSpawnServer configuration option is set correctly,
0
+If you are sure that the 'PassengerRoot' configuration option is set correctly,
0
 then this problem is most likely caused by the fact that you're running Apache
0
 with SELinux. On Fedora, CentOS and RedHat Enterprise Linux, Apache is locked
0
 down by SELinux policies.
0
@@ -752,7 +750,7 @@
0
 involves two steps:
0
 
0
 1. Moving the directory.
0
-2. Updating the ``RailsSpawnServer'' configuration option in Apache.
0
+2. Updating the ``PassengerRoot'' configuration option in Apache.
0
 
0
 For example, if Passenger is located in '/opt/passenger/', and you'd like to
0
 move it to '/usr/local/passenger/', then do this:
0
@@ -765,7 +763,7 @@
0
 2. Edit your Apache configuration file, and set:
0
 +
0
 ------------------------------------
0
-RailsSpawnServer /usr/local/passenger/bin/passenger-spawn-server
0
+PassengerRoot /usr/local/passenger
0
 ------------------------------------
0
 
0
 === Installing multiple Ruby on Rails versions ===
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,654 +1 @@
0
-/*
0
- * Phusion Passenger - http://www.modrails.com/
0
- * Copyright (C) 2008 Phusion
0
- *
0
- * This program is free software; you can redistribute it and/or modify
0
- * it under the terms of the GNU General Public License as published by
0
- * the Free Software Foundation; version 2 of the License.
0
- *
0
- * This program is distributed in the hope that it will be useful,
0
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
0
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0
- * GNU General Public License for more details.
0
- *
0
- * You should have received a copy of the GNU General Public License along
0
- * with this program; if not, write to the Free Software Foundation, Inc.,
0
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0
- */
0
-#ifndef _PASSENGER_APPLICATION_POOL_CLIENT_SERVER_H_
0
-#define _PASSENGER_APPLICATION_POOL_CLIENT_SERVER_H_
0
-
0
-#include <boost/bind.hpp>
0
-#include <boost/thread/thread.hpp>
0
-
0
-#include <set>
0
-
0
-#include <sys/types.h>
0
-#include <sys/socket.h>
0
-#include <cstdlib>
0
-#include <errno.h>
0
-#include <unistd.h>
0
-
0
-#include "StandardApplicationPool.h"
0
-#include "MessageChannel.h"
0
-#include "Exceptions.h"
0
-#include "Logging.h"
0
-
0
-namespace Passenger {
0
-
0
-using namespace std;
0
-using namespace boost;
0
-
0
-/**
0
- * Multi-process usage support for ApplicationPool.
0
- *
0
- * ApplicationPoolServer implements a client/server architecture for ApplicationPool.
0
- * This allows one to use ApplicationPool in a multi-process environment (unlike
0
- * StandardApplicationPool). The cache/pool data is stored in the server. Different
0
- * processes can then access the pool through the server.
0
- *
0
- * ApplicationPoolServer itself does not inherit ApplicationPool. Instead, it returns
0
- * an ApplicationPool object via the connect() call. For example:
0
- * @code
0
- * // Create an ApplicationPoolServer.
0
- * ApplicationPoolServer server(...);
0
- *
0
- * // Now fork a child process, like Apache's prefork MPM eventually will.
0
- * pid_t pid = fork();
0
- * if (pid == 0) {
0
- * // Child process
0
- *
0
- * // Connect to the server. After connection, we have an ApplicationPool
0
- * // object!
0
- * ApplicationPoolPtr pool(server.connect());
0
- *
0
- * // The child process doesn't run a server (only the parent process does)
0
- * // so we call detach to free the server resources (things like file
0
- * // descriptors).
0
- * server.detach();
0
- *
0
- * ApplicationPool::SessionPtr session(pool->get("/home/webapps/foo"));
0
- * do_something_with(session);
0
- *
0
- * _exit(0);
0
- * } else {
0
- * // Parent process
0
- * waitpid(pid, NULL, 0);
0
- * }
0
- * @endcode
0
- *
0
- * @warning
0
- * ApplicationPoolServer uses threads internally. Threads will disappear after a fork(),
0
- * so ApplicationPoolServer will become usable as a server after a fork(). After a fork(),
0
- * you can still call connect() (and, of course, detach()), but the same
0
- * ApplicationPoolServer better still be running in the parent process. So in case of
0
- * Apache with the prefork MPM, be sure to create an ApplicationPoolServer
0
- * <em>after</em> Apache has daemonized.
0
- *
0
- * <h2>Implementation notes</h2>
0
- * Notice that ApplicationPoolServer does do not use TCP sockets at all, or even named Unix
0
- * sockets, depite being a server that can handle multiple clients! So ApplicationPoolServer
0
- * will expose no open ports or temporary Unix socket files. Only child processes are able
0
- * to use the ApplicationPoolServer.
0
- *
0
- * This is implemented through anonymous Unix sockets (<tt>socketpair()</tt>) and file descriptor
0
- * passing. It allows one to emulate <tt>accept()</tt>. During initialization,
0
- * ApplicationPoolServer creates a pair of Unix sockets, one called <tt>serverSocket</tt>
0
- * and the other called <tt>connectSocket</tt>. There is a thread which continuously
0
- * listens on serverSocket for incoming data. The data itself is not important, because it
0
- * only serves to wake up the thread. ApplicationPoolServer::connect() sends some data through
0
- * connectSocket, which wakes up the server thread. The server thread will then create
0
- * a pair of Unix sockets. One of them is passed through serverSocket. The other will be
0
- * handled by a newly created client thread. So the socket that was passed through serverSocket
0
- * is the client's connection to the server, while the other socket is the server's connection
0
- * to the client.
0
- *
0
- * Note that serverSocket and connectSocket are solely used for setting up new connections
0
- * ala accept(). They are not used for any actual data. In fact, they cannot be used in any
0
- * other way without some sort of inter-process synchronization mechanism, because all
0
- * child processes are connected to the same serverSocket. In contrast,
0
- * ApplicationPoolServer::connect() allows one to setup a private communicate channel between
0
- * the server and the current child process.
0
- *
0
- * Also note that each client is handled by a seperate thread. This is necessary because
0
- * ApplicationPoolServer internally uses StandardApplicationPool, and the current algorithm
0
- * for StandardApplicationPool::get() can block (in the case that the spawning limit has
0
- * been exceeded). While it is possible to get around this problem without using threads,
0
- * a thread-based implementation is easier to write.
0
- *
0
- * @ingroup Support
0
- */
0
-class ApplicationPoolServer {
0
-private:
0
- /**
0
- * Contains data shared between RemoteSession and Client.
0
- * Since RemoteSession and Client have different life times, i.e. one may be
0
- * destroyed before the other, they both use a smart pointer that points to
0
- * a SharedData. This way, the SharedData object is only destroyed when
0
- * both the RemoteSession and the Client object has been destroyed.
0
- */
0
- struct SharedData {
0
- /**
0
- * The socket connection to the server, as was established by
0
- * ApplicationPoolServer::connect().
0
- */
0
- int server;
0
-
0
- ~SharedData() {
0
- close(server);
0
- }
0
- };
0
-
0
- typedef shared_ptr<SharedData> SharedDataPtr;
0
-
0
- /**
0
- * An Application::Session which works together with ApplicationPoolServer.
0
- */
0
- class RemoteSession: public Application::Session {
0
- private:
0
- SharedDataPtr data;
0
- int id;
0
- int reader;
0
- int writer;
0
- pid_t pid;
0
- public:
0
- RemoteSession(SharedDataPtr data, pid_t pid, int id, int reader, int writer) {
0
- this->data = data;
0
- this->pid = pid;
0
- this->id = id;
0
- this->reader = reader;
0
- this->writer = writer;
0
- }
0
-
0
- virtual ~RemoteSession() {
0
- closeReader();
0
- closeWriter();
0
- MessageChannel(data->server).write("close", toString(id).c_str(), NULL);
0
- }
0
-
0
- virtual int getReader() const {
0
- return reader;
0
- }
0
-
0
- virtual void closeReader() {
0
- if (reader != -1) {
0
- close(reader);
0
- reader = -1;
0
- }
0
- }
0
-
0
- virtual int getWriter() const {
0
- return writer;
0
- }
0
-
0
- virtual void closeWriter() {
0
- if (writer != -1) {
0
- close(writer);
0
- writer = -1;
0
- }
0
- }
0
-
0
- virtual pid_t getPid() const {
0
- return pid;
0
- }
0
- };
0
-
0
- /**
0
- * An ApplicationPool implementation that works together with ApplicationPoolServer.
0
- * It doesn't do much by itself, its job is mostly to forward queries/commands to
0
- * the server and returning the result. Most of the logic is in the server.
0
- */
0
- class Client: public ApplicationPool {
0
- private:
0
- SharedDataPtr data;
0
-
0
- public:
0
- /**
0
- * Create a new Client.
0
- *
0
- * @param sock The newly established socket connection with the ApplicationPoolServer.
0
- */
0
- Client(int sock) {
0
- data = ptr(new SharedData());
0
- data->server = sock;
0
- }
0
-
0
- virtual void clear() {
0
- MessageChannel channel(data->server);
0
- channel.write("clear", NULL);
0
- }
0
-
0
- virtual void setMaxIdleTime(unsigned int seconds) {
0
- MessageChannel channel(data->server);
0
- channel.write("setMaxIdleTime", toString(seconds).c_str(), NULL);
0
- }
0
-
0
- virtual void setMax(unsigned int max) {
0
- MessageChannel channel(data->server);
0
- channel.write("setMax", toString(max).c_str(), NULL);
0
- }
0
-
0
- virtual unsigned int getActive() const {
0
- MessageChannel channel(data->server);
0
- vector<string> args;
0
-
0
- channel.write("getActive", NULL);
0
- channel.read(args);
0
- return atoi(args[0].c_str());
0
- }
0
-
0
- virtual unsigned int getCount() const {
0
- MessageChannel channel(data->server);
0
- vector<string> args;
0
-
0
- channel.write("getCount", NULL);
0
- channel.read(args);
0
- return atoi(args[0].c_str());
0
- }
0
-
0
- virtual pid_t getSpawnServerPid() const {
0
- MessageChannel channel(data->server);
0
- vector<string> args;
0
-
0
- channel.write("getSpawnServerPid", NULL);
0
- channel.read(args);
0
- return atoi(args[0].c_str());
0
- }
0
-
0
- virtual Application::SessionPtr get(const string &appRoot, bool lowerPrivilege = true, const string &lowestUser = "nobody") {
0
- MessageChannel channel(data->server);
0
- vector<string> args;
0
- int reader, writer;
0
-
0
- channel.write("get", appRoot.c_str(),
0
- (lowerPrivilege) ? "true" : "false",
0
- lowestUser.c_str(), NULL);
0
- if (!channel.read(args)) {
0
- throw IOException("The ApplicationPool server unexpectedly closed the connection.");
0
- }
0
- if (args[0] == "ok") {
0
- reader = channel.readFileDescriptor();
0
- writer = channel.readFileDescriptor();
0
- return ptr(new RemoteSession(data, atoi(args[1]), atoi(args[2]), reader, writer));
0
- } else if (args[0] == "SpawnException") {
0
- if (args[2] == "true") {
0
- string errorPage;
0
-
0
- if (!channel.readScalar(errorPage)) {
0
- throw IOException("The ApplicationPool server unexpectedly closed the connection.");
0
- }
0
- throw SpawnException(args[1], errorPage);
0
- } else {
0
- throw SpawnException(args[1]);
0
- }
0
- } else if (args[0] == "IOException") {
0
- throw IOException(args[1]);
0
- } else {
0
- throw IOException("The ApplicationPool server returned an unknown message.");
0
- }
0
- }
0
- };
0
-
0
- /**
0
- * Contains information about exactly one client.
0
- */
0
- struct ClientInfo {
0
- /** The connection to the client. */
0
- int fd;
0
- /** The thread which handles the client. */
0
- thread *thr;
0
- bool detached;
0
-
0
- ClientInfo() {
0
- detached = false;
0
- }
0
-
0
- void detach() {
0
- detached = true;
0
- close(fd);
0
- fd = -1;
0
- }
0
-
0
- ~ClientInfo() {
0
- /* For some reason, joining or deleting (detaching)
0
- * the thread after fork() will cause a segfault.
0
- * I haven't figured out why that happens, so for now
0
- * I'll just ignore the thread (which isn't running
0
- * anyway).
0
- */
0
- if (!detached) {
0
- close(fd);
0
- delete thr;
0
- }
0
- }
0
- };
0
-
0
- typedef shared_ptr<ClientInfo> ClientInfoPtr;
0
-
0
- StandardApplicationPool pool;
0
- int serverSocket;
0
- int connectSocket;
0
- bool done, detached;
0
-
0
- mutex lock;
0
- thread *serverThread;
0
- set<ClientInfoPtr> clients;
0
-
0
- /* TODO: the current design makes it possible to leak file descriptors.
0
- * For example, suppose that a fork() happens right after
0
- * serverThreadMainLoop() created a socketpair. Uh-oh. The problem is that
0
- * Apache can fork no matter what the threads are currently doing.
0
- *
0
- * This problem can be solved by running the server thread main loop in
0
- * its own process, instead of a thread in the Apache control process.
0
- *
0
- * This situation is a corner case though, and doesn't happen very often.
0
- * When it does happen, the problem isn't that great: an Apache worker
0
- * process will eventually get killed, thus freeing all its file
0
- * descriptors. So it should be acceptable to fix this problem in
0
- * a post-1.0.1 release.
0
- */
0
-
0
- /**
0
- * The entry point of the server thread which sets up private connections.
0
- * See the class overview's implementation notes for details.
0
- */
0
- void serverThreadMainLoop() {
0
- while (!done) {
0
- int fds[2], ret;
0
- char x;
0
-
0
- // The received data only serves to wake up the server socket,
0
- // and is not important.
0
- do {
0
- ret = read(serverSocket, &x, 1);
0
- } while (ret == -1 && errno == EINTR);
0
- if (ret == 0) {
0
- break;
0
- }
0
-
0
- // Incoming connect request.
0
- do {
0
- ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
0
- } while (ret == -1 && errno == EINTR);
0
- if (ret == -1) {
0
- int e = errno;
0
- P_ERROR("Cannot create an anonymous Unix socket: " <<