public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
Refactoring: make code DRYer by moving spawn options to a seperate 
SpawnOptions class.
Hongli Lai (Phusion) (author)
Mon Jul 07 04:45:45 -0700 2008
commit  7ed8332bb28190b0d8363cefa033e93265a0aea4
tree    da93a2457db3d63d34ec46adc91d0b1ba6191919
parent  ca2beb2e0be3d7977a21eedf6d7b94afb96e51af
...
24
25
26
 
27
28
29
...
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
...
123
124
125
126
127
128
 
 
 
 
 
 
 
 
129
130
131
...
24
25
26
27
28
29
30
...
92
93
94
 
95
96
97
98
99
100
101
 
 
 
 
 
 
 
 
 
 
 
 
 
102
103
104
105
106
107
108
...
115
116
117
 
 
 
118
119
120
121
122
123
124
125
126
127
128
0
@@ -24,6 +24,7 @@
0
 #include <sys/types.h>
0
 
0
 #include "Application.h"
0
+#include "SpawnOptions.h"
0
 
0
 namespace Passenger {
0
 
0
@@ -91,26 +92,17 @@ public:
0
   virtual ~ApplicationPool() {};
0
   
0
   /**
0
- * Open a new session with the application specified by <tt>appRoot</tt>.
0
+ * Open a new session with the application specified by <tt>spawnOptions.appRoot</tt>.
0
    * See the class description for ApplicationPool, as well as Application::connect(),
0
    * on how to use the returned session object.
0
    *
0
    * Internally, this method may either spawn a new application instance, or use
0
    * an existing one.
0
    *
0
- * If <tt>lowerPrivilege</tt> is true, then any newly spawned application
0
- * instances will have lower privileges. See SpawnManager::SpawnManager()'s
0
- * description of <tt>lowerPrivilege</tt> and <tt>lowestUser</tt> for details.
0
- *
0
- * @param appRoot The application root of a RoR application, i.e. the folder that
0
- * contains 'app/', 'public/', 'config/', etc. This must be a valid
0
- * directory, but does not have to be an absolute path.
0
- * @param lowerPrivilege Whether to lower the application's privileges.
0
- * @param lowestUser The user to fallback to if lowering privilege fails.
0
- * @param environment The RAILS_ENV/RACK_ENV environment that should be used. May not be empty.
0
- * @param spawnMethod The spawn method to use. Either "smart" or "conservative".
0
- * See the Ruby class SpawnManager for details.
0
- * @param appType The application type. Either "rails" or "rack".
0
+ * @param spawnOptions An object containing information on which application to open
0
+ * a session with, as well as spawning details. Spawning details will be used
0
+ * if the pool decides that spawning a new application instance is necessary.
0
+ * See SpawnManager and SpawnOptions for details.
0
    * @return A session object.
0
    * @throw SpawnException An attempt was made to spawn a new application instance, but that attempt failed.
0
    * @throw BusyException The application pool is too busy right now, and cannot
0
@@ -123,9 +115,14 @@ public:
0
    * <tt>get("/home/../home/foo")</tt>, then ApplicationPool will think
0
    * they're 2 different applications, and thus will spawn 2 application instances.
0
    */
0
- virtual Application::SessionPtr get(const string &appRoot, bool lowerPrivilege = true,
0
- const string &lowestUser = "nobody", const string &environment = "production",
0
- const string &spawnMethod = "smart", const string &appType = "rails") = 0;
0
+ virtual Application::SessionPtr get(const SpawnOptions &spawnOptions) = 0;
0
+
0
+ /**
0
+ * Convenience shortcut for calling get() with default spawn options.
0
+ */
0
+ virtual Application::SessionPtr get(const string &appRoot) {
0
+ return get(SpawnOptions(appRoot));
0
+ }
0
   
0
   /**
0
    * Clear all application instances that are currently in the pool.
...
311
312
313
314
315
316
317
318
319
320
321
 
322
323
324
...
327
328
329
330
331
332
333
334
335
 
 
 
 
 
 
 
336
337
338
...
311
312
313
 
 
 
 
 
 
 
 
314
315
316
317
...
320
321
322
 
 
 
 
 
 
323
324
325
326
327
328
329
330
331
332
0
@@ -311,14 +311,7 @@ private:
0
       return atoi(args[0].c_str());
0
     }
0
     
0
- virtual Application::SessionPtr get(
0
- const string &appRoot,
0
- bool lowerPrivilege = true,
0
- const string &lowestUser = "nobody",
0
- const string &environment = "production",
0
- const string &spawnMethod = "smart",
0
- const string &appType = "rails"
0
- ) {
0
+ virtual Application::SessionPtr get(const SpawnOptions &spawnOptions) {
0
       this_thread::disable_syscall_interruption dsi;
0
       MessageChannel channel(data->server);
0
       boost::mutex::scoped_lock l(data->lock);
0
@@ -327,12 +320,13 @@ private:
0
       bool result;
0
       
0
       try {
0
- channel.write("get", appRoot.c_str(),
0
- (lowerPrivilege) ? "true" : "false",
0
- lowestUser.c_str(),
0
- environment.c_str(),
0
- spawnMethod.c_str(),
0
- appType.c_str(),
0
+ channel.write("get",
0
+ spawnOptions.appRoot.c_str(),
0
+ (spawnOptions.lowerPrivilege) ? "true" : "false",
0
+ spawnOptions.lowestUser.c_str(),
0
+ spawnOptions.environment.c_str(),
0
+ spawnOptions.spawnMethod.c_str(),
0
+ spawnOptions.appType.c_str(),
0
           NULL);
0
       } catch (const SystemException &) {
0
         throw IOException("The ApplicationPool server exited unexpectedly.");
...
230
231
232
233
234
 
 
 
235
236
237
...
230
231
232
 
 
233
234
235
236
237
238
0
@@ -230,8 +230,9 @@ private:
0
     bool failed = false;
0
     
0
     try {
0
- session = server.pool.get(args[1], args[2] == "true", args[3],
0
- args[4], args[5], args[6]);
0
+ session = server.pool.get(SpawnOptions(
0
+ args[1], args[2] == "true", args[3],
0
+ args[4], args[5], args[6]));
0
       sessions[lastSessionID] = session;
0
       lastSessionID++;
0
     } catch (const SpawnException &e) {
...
693
694
695
696
 
697
698
699
 
700
701
702
...
693
694
695
 
696
697
698
 
699
700
701
702
0
@@ -693,10 +693,10 @@ public:
0
           spawnMethod = "smart";
0
         }
0
         
0
- session = applicationPool->get(
0
+ session = applicationPool->get(SpawnOptions(
0
           canonicalizePath(mapper.getPublicDirectory() + "/.."),
0
           true, defaultUser, environment, spawnMethod,
0
- mapper.getApplicationTypeString());
0
+ mapper.getApplicationTypeString()));
0
         P_TRACE(3, "Forwarding " << r->uri << " to PID " << session->getPid());
0
       } catch (const SpawnException &e) {
0
         if (e.hasErrorPage()) {
...
39
40
41
 
42
43
44
...
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
...
336
337
338
339
340
 
 
341
342
343
344
345
346
347
348
349
350
 
351
352
353
...
364
365
366
367
368
 
369
370
371
...
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
...
39
40
41
42
43
44
45
...
245
246
247
 
 
 
 
 
 
248
249
250
251
 
 
 
 
 
 
 
 
252
253
254
255
256
257
258
 
 
 
 
 
 
259
260
261
262
263
264
265
266
267
...
325
326
327
 
 
328
329
330
331
332
333
334
335
 
 
 
 
336
337
338
339
...
350
351
352
 
 
353
354
355
356
...
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
0
@@ -39,6 +39,7 @@
0
 #include <signal.h>
0
 
0
 #include "Application.h"
0
+#include "SpawnOptions.h"
0
 #include "MessageChannel.h"
0
 #include "Exceptions.h"
0
 #include "Logging.h"
0
@@ -244,35 +245,23 @@ private:
0
   /**
0
    * Send the spawn command to the spawn server.
0
    *
0
- * @param appRoot The application root of the application to spawn.
0
- * @param lowerPrivilege Whether to lower the application's privileges.
0
- * @param lowestUser The user to fallback to if lowering privilege fails.
0
- * @param environment The RAILS_ENV/RACK_ENV environment that should be used.
0
- * @param spawnMethod The spawn method to use.
0
- * @param appType The application type.
0
+ * @param spawnOptions The spawn options to use.
0
    * @return An Application smart pointer, representing the spawned application.
0
    * @throws SpawnException Something went wrong.
0
    */
0
- ApplicationPtr sendSpawnCommand(
0
- const string &appRoot,
0
- bool lowerPrivilege,
0
- const string &lowestUser,
0
- const string &environment,
0
- const string &spawnMethod,
0
- const string &appType
0
- ) {
0
+ ApplicationPtr sendSpawnCommand(const SpawnOptions &spawnOptions) {
0
     TRACE_POINT();
0
     vector<string> args;
0
     int ownerPipe;
0
     
0
     try {
0
       channel.write("spawn_application",
0
- appRoot.c_str(),
0
- (lowerPrivilege) ? "true" : "false",
0
- lowestUser.c_str(),
0
- environment.c_str(),
0
- spawnMethod.c_str(),
0
- appType.c_str(),
0
+ spawnOptions.appRoot.c_str(),
0
+ (spawnOptions.lowerPrivilege) ? "true" : "false",
0
+ spawnOptions.lowestUser.c_str(),
0
+ spawnOptions.environment.c_str(),
0
+ spawnOptions.spawnMethod.c_str(),
0
+ spawnOptions.appType.c_str(),
0
         NULL);
0
     } catch (const SystemException &e) {
0
       throw SpawnException(string("Could not write 'spawn_application' "
0
@@ -336,18 +325,15 @@ private:
0
         ret = chown(args[1].c_str(), getuid(), getgid());
0
       } while (ret == -1 && errno == EINTR);
0
     }
0
- return ApplicationPtr(new Application(appRoot, pid, args[1],
0
- usingAbstractNamespace, ownerPipe));
0
+ return ApplicationPtr(new Application(spawnOptions.appRoot,
0
+ pid, args[1], usingAbstractNamespace, ownerPipe));
0
   }
0
   
0
   /**
0
    * @throws boost::thread_interrupted
0
    */
0
   ApplicationPtr
0
- handleSpawnException(const SpawnException &e, const string &appRoot,
0
- bool lowerPrivilege, const string &lowestUser,
0
- const string &environment, const string &spawnMethod,
0
- const string &appType) {
0
+ handleSpawnException(const SpawnException &e, const SpawnOptions &spawnOptions) {
0
     TRACE_POINT();
0
     bool restarted;
0
     try {
0
@@ -364,8 +350,7 @@ private:
0
       restarted = false;
0
     }
0
     if (restarted) {
0
- return sendSpawnCommand(appRoot, lowerPrivilege, lowestUser,
0
- environment, spawnMethod, appType);
0
+ return sendSpawnCommand(spawnOptions);
0
     } else {
0
       throw SpawnException("The spawn server died unexpectedly, and restarting it failed.");
0
     }
0
@@ -479,66 +464,32 @@ public:
0
   }
0
   
0
   /**
0
- * Spawn a new instance of a Ruby on Rails or Rack application.
0
+ * Spawn a new instance of an application. Spawning details are to be passed
0
+ * via the <tt>spawnOptions</tt> parameter.
0
    *
0
    * If the spawn server died during the spawning process, then the server
0
    * will be automatically restarted, and another spawn attempt will be made.
0
    * If restarting the server fails, or if the second spawn attempt fails,
0
    * then an exception will be thrown.
0
    *
0
- * If <tt>lowerPrivilege</tt> is true, then it will be attempt to
0
- * switch the spawned application instance to the user who owns the
0
- * application's <tt>config/environment.rb</tt>, and to the default
0
- * group of that user.
0
- *
0
- * If that user doesn't exist on the system, or if that user is root,
0
- * then it will be attempted to switch to the username given by
0
- * <tt>lowestUser</tt> (and to the default group of that user).
0
- * If <tt>lowestUser</tt> doesn't exist either, or if switching user failed
0
- * (because the spawn server process does not have the privilege to do so),
0
- * then the application will be spawned anyway, without reporting an error.
0
- *
0
- * It goes without saying that lowering privilege is only possible if
0
- * the spawn server is running as root (and thus, by induction, that
0
- * Passenger and Apache's control process are also running as root).
0
- * Note that if Apache is listening on port 80, then its control process must
0
- * be running as root. See "doc/Security of user switching.txt" for
0
- * a detailed explanation.
0
- *
0
- * @param appRoot The application root of a RoR application, i.e. the folder that
0
- * contains 'app/', 'public/', 'config/', etc. This must be a valid directory,
0
- * but the path does not have to be absolute.
0
- * @param lowerPrivilege Whether to lower the application's privileges.
0
- * @param lowestUser The user to fallback to if lowering privilege fails.
0
- * @param environment The RAILS_ENV/RACK_ENV environment that should be used. May not be empty.
0
- * @param spawnMethod The spawn method to use. Either "smart" or "conservative".
0
- * See the Ruby class SpawnManager for details.
0
- * @param appType The application type. Either "rails" or "rack".
0
+ * @param spawnOptions An object containing the details for this spawn operation,
0
+ * such as which application to spawn. See SpawnOptions for details.
0
    * @return A smart pointer to an Application object, which represents the application
0
    * instance that has been spawned. Use this object to communicate with the
0
    * spawned application.
0
    * @throws SpawnException Something went wrong.
0
    * @throws boost::thread_interrupted
0
    */
0
- ApplicationPtr spawn(
0
- const string &appRoot,
0
- bool lowerPrivilege = true,
0
- const string &lowestUser = "nobody",
0
- const string &environment = "production",
0
- const string &spawnMethod = "smart",
0
- const string &appType = "rails"
0
- ) {
0
+ ApplicationPtr spawn(const SpawnOptions &spawnOptions) {
0
     TRACE_POINT();
0
     boost::mutex::scoped_lock l(lock);
0
     try {
0
- return sendSpawnCommand(appRoot, lowerPrivilege, lowestUser,
0
- environment, spawnMethod, appType);
0
+ return sendSpawnCommand(spawnOptions);
0
     } catch (const SpawnException &e) {
0
       if (e.hasErrorPage()) {
0
         throw;
0
       } else {
0
- return handleSpawnException(e, appRoot, lowerPrivilege,
0
- lowestUser, environment, spawnMethod, appType);
0
+ return handleSpawnException(e, spawnOptions);
0
       }
0
     }
0
   }
...
377
378
379
380
381
382
383
384
385
386
387
388
 
389
390
 
391
392
393
...
449
450
451
452
453
454
 
455
456
457
...
487
488
489
490
491
 
492
493
494
...
597
598
599
600
601
602
603
604
605
606
607
 
608
609
610
...
615
616
617
618
619
 
620
621
622
...
634
635
636
637
 
638
639
640
...
648
649
650
651
 
652
653
654
...
377
378
379
 
 
 
 
 
 
 
 
 
380
381
382
383
384
385
386
...
442
443
444
 
 
 
445
446
447
448
...
478
479
480
 
 
481
482
483
484
...
587
588
589
 
 
 
 
 
 
 
 
590
591
592
593
...
598
599
600
 
 
601
602
603
604
...
616
617
618
 
619
620
621
622
...
630
631
632
 
633
634
635
636
0
@@ -377,17 +377,10 @@ private:
0
    * @throws SystemException
0
    */
0
   pair<AppContainerPtr, Domain *>
0
- spawnOrUseExisting(
0
- boost::mutex::scoped_lock &l,
0
- const string &appRoot,
0
- bool lowerPrivilege,
0
- const string &lowestUser,
0
- const string &environment,
0
- const string &spawnMethod,
0
- const string &appType
0
- ) {
0
+ spawnOrUseExisting(boost::mutex::scoped_lock &l, const SpawnOptions &spawnOptions) {
0
     this_thread::disable_interruption di;
0
     this_thread::disable_syscall_interruption dsi;
0
+ const string &appRoot(spawnOptions.appRoot);
0
     AppContainerPtr container;
0
     Domain *domain;
0
     AppContainerList *instances;
0
@@ -449,9 +442,7 @@ private:
0
           {
0
             this_thread::restore_interruption ri(di);
0
             this_thread::restore_syscall_interruption rsi(dsi);
0
- container->app = spawnManager.spawn(appRoot,
0
- lowerPrivilege, lowestUser, environment,
0
- spawnMethod, appType);
0
+ container->app = spawnManager.spawn(spawnOptions);
0
           }
0
           container->sessions = 0;
0
           instances->push_back(container);
0
@@ -487,8 +478,7 @@ private:
0
         {
0
           this_thread::restore_interruption ri(di);
0
           this_thread::restore_syscall_interruption rsi(dsi);
0
- container->app = spawnManager.spawn(appRoot, lowerPrivilege, lowestUser,
0
- environment, spawnMethod, appType);
0
+ container->app = spawnManager.spawn(spawnOptions);
0
         }
0
         container->sessions = 0;
0
         it = domains.find(appRoot);
0
@@ -597,14 +587,7 @@ public:
0
     delete cleanerThread;
0
   }
0
   
0
- virtual Application::SessionPtr get(
0
- const string &appRoot,
0
- bool lowerPrivilege = true,
0
- const string &lowestUser = "nobody",
0
- const string &environment = "production",
0
- const string &spawnMethod = "smart",
0
- const string &appType = "rails"
0
- ) {
0
+ virtual Application::SessionPtr get(const SpawnOptions &spawnOptions) {
0
     TRACE_POINT();
0
     using namespace boost::posix_time;
0
     unsigned int attempt = 0;
0
@@ -615,8 +598,7 @@ public:
0
       attempt++;
0
       
0
       pair<AppContainerPtr, Domain *> p(
0
- spawnOrUseExisting(l, appRoot, lowerPrivilege, lowestUser,
0
- environment, spawnMethod, appType)
0
+ spawnOrUseExisting(l, spawnOptions)
0
       );
0
       AppContainerPtr &container = p.first;
0
       Domain *domain = p.second;
0
@@ -634,7 +616,7 @@ public:
0
         if (attempt == MAX_GET_ATTEMPTS) {
0
           string message("Cannot connect to an existing "
0
             "application instance for '");
0
- message.append(appRoot);
0
+ message.append(spawnOptions.appRoot);
0
           message.append("': ");
0
           try {
0
             const SystemException &syse =
0
@@ -648,7 +630,7 @@ public:
0
           AppContainerList &instances(domain->instances);
0
           instances.erase(container->iterator);
0
           if (instances.empty()) {
0
- domains.erase(appRoot);
0
+ domains.erase(spawnOptions.appRoot);
0
           }
0
           count--;
0
           active--;
...
19
20
21
22
 
23
24
25
...
32
33
34
35
 
36
37
38
...
54
55
56
57
 
58
59
60
...
19
20
21
 
22
23
24
25
...
32
33
34
 
35
36
37
38
...
54
55
56
 
57
58
59
60
0
@@ -19,7 +19,7 @@ namespace tut {
0
 
0
   TEST_METHOD(1) {
0
     // Spawning an application should return a valid Application object.
0
- ApplicationPtr app(manager.spawn("."));
0
+ ApplicationPtr app(manager.spawn(SpawnOptions(".")));
0
     ensure_equals("The Application object's PID is the same as the one specified by the stub",
0
       app->getPid(), 1234);
0
   }
0
@@ -32,7 +32,7 @@ namespace tut {
0
     // Give the spawn server the time to properly terminate.
0
     usleep(500000);
0
     
0
- ApplicationPtr app(manager.spawn("."));
0
+ ApplicationPtr app(manager.spawn(SpawnOptions(".")));
0
     ensure_equals("The Application object's PID is the same as the one specified by the stub",
0
       app->getPid(), 1234);
0
     
0
@@ -54,7 +54,7 @@ namespace tut {
0
     
0
       try {
0
         manager.nextRestartShouldFail = true;
0
- ApplicationPtr app(manager.spawn("."));
0
+ ApplicationPtr app(manager.spawn(SpawnOptions(".")));
0
         fail("SpawnManager did not throw a SpawnException");
0
       } catch (const SpawnException &e) {
0
         // Success.

Comments

    No one has commented yet.