00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _PASSENGER_APPLICATION_POOL_SERVER_H_
00021 #define _PASSENGER_APPLICATION_POOL_SERVER_H_
00022
00023 #include <boost/shared_ptr.hpp>
00024 #include <boost/thread/mutex.hpp>
00025
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 #include <sys/wait.h>
00029 #include <sys/socket.h>
00030 #include <cstdio>
00031 #include <cstdlib>
00032 #include <limits.h>
00033 #include <errno.h>
00034 #include <unistd.h>
00035 #include <signal.h>
00036
00037 #include "MessageChannel.h"
00038 #include "ApplicationPool.h"
00039 #include "Application.h"
00040 #include "Exceptions.h"
00041 #include "Logging.h"
00042 #include "System.h"
00043
00044 namespace Passenger {
00045
00046 using namespace std;
00047 using namespace boost;
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 class ApplicationPoolServer {
00130 private:
00131
00132
00133
00134
00135
00136
00137
00138 struct SharedData {
00139
00140
00141
00142
00143 int server;
00144
00145 mutex lock;
00146
00147 ~SharedData() {
00148 int ret;
00149 do {
00150 ret = close(server);
00151 } while (ret == -1 && errno == EINTR);
00152 }
00153 };
00154
00155 typedef shared_ptr<SharedData> SharedDataPtr;
00156
00157
00158
00159
00160 class RemoteSession: public Application::Session {
00161 private:
00162 SharedDataPtr data;
00163 int id;
00164 int fd;
00165 pid_t pid;
00166 public:
00167 RemoteSession(SharedDataPtr data, pid_t pid, int id, int fd) {
00168 this->data = data;
00169 this->pid = pid;
00170 this->id = id;
00171 this->fd = fd;
00172 }
00173
00174 virtual ~RemoteSession() {
00175 closeStream();
00176 mutex::scoped_lock(data->lock);
00177 MessageChannel(data->server).write("close", toString(id).c_str(), NULL);
00178 }
00179
00180 virtual int getStream() const {
00181 return fd;
00182 }
00183
00184 virtual void shutdownReader() {
00185 if (fd != -1) {
00186 int ret = InterruptableCalls::shutdown(fd, SHUT_RD);
00187 if (ret == -1) {
00188 throw SystemException("Cannot shutdown the writer stream",
00189 errno);
00190 }
00191 }
00192 }
00193
00194 virtual void shutdownWriter() {
00195 if (fd != -1) {
00196 int ret = InterruptableCalls::shutdown(fd, SHUT_WR);
00197 if (ret == -1) {
00198 throw SystemException("Cannot shutdown the writer stream",
00199 errno);
00200 }
00201 }
00202 }
00203
00204 virtual void closeStream() {
00205 if (fd != -1) {
00206 int ret = InterruptableCalls::close(fd);
00207 if (ret == -1) {
00208 throw SystemException("Cannot close the session stream",
00209 errno);
00210 }
00211 fd = -1;
00212 }
00213 }
00214
00215 virtual void discardStream() {
00216 fd = -1;
00217 }
00218
00219 virtual pid_t getPid() const {
00220 return pid;
00221 }
00222 };
00223
00224
00225
00226
00227
00228
00229 class Client: public ApplicationPool {
00230 private:
00231
00232
00233 SharedDataPtr dataSmartPointer;
00234 SharedData *data;
00235
00236 public:
00237
00238
00239
00240
00241
00242 Client(int sock) {
00243 dataSmartPointer = ptr(new SharedData());
00244 data = dataSmartPointer.get();
00245 data->server = sock;
00246 }
00247
00248 virtual void clear() {
00249 MessageChannel channel(data->server);
00250 mutex::scoped_lock l(data->lock);
00251 channel.write("clear", NULL);
00252 }
00253
00254 virtual void setMaxIdleTime(unsigned int seconds) {
00255 MessageChannel channel(data->server);
00256 mutex::scoped_lock l(data->lock);
00257 channel.write("setMaxIdleTime", toString(seconds).c_str(), NULL);
00258 }
00259
00260 virtual void setMax(unsigned int max) {
00261 MessageChannel channel(data->server);
00262 mutex::scoped_lock l(data->lock);
00263 channel.write("setMax", toString(max).c_str(), NULL);
00264 }
00265
00266 virtual unsigned int getActive() const {
00267 MessageChannel channel(data->server);
00268 mutex::scoped_lock l(data->lock);
00269 vector<string> args;
00270
00271 channel.write("getActive", NULL);
00272 channel.read(args);
00273 return atoi(args[0].c_str());
00274 }
00275
00276 virtual unsigned int getCount() const {
00277 MessageChannel channel(data->server);
00278 mutex::scoped_lock l(data->lock);
00279 vector<string> args;
00280
00281 channel.write("getCount", NULL);
00282 channel.read(args);
00283 return atoi(args[0].c_str());
00284 }
00285
00286 virtual void setMaxPerApp(unsigned int max) {
00287 MessageChannel channel(data->server);
00288 mutex::scoped_lock l(data->lock);
00289 channel.write("setMaxPerApp", toString(max).c_str(), NULL);
00290 }
00291
00292 virtual pid_t getSpawnServerPid() const {
00293 this_thread::disable_syscall_interruption dsi;
00294 MessageChannel channel(data->server);
00295 mutex::scoped_lock l(data->lock);
00296 vector<string> args;
00297
00298 channel.write("getSpawnServerPid", NULL);
00299 channel.read(args);
00300 return atoi(args[0].c_str());
00301 }
00302
00303 virtual Application::SessionPtr get(
00304 const string &appRoot,
00305 bool lowerPrivilege = true,
00306 const string &lowestUser = "nobody",
00307 const string &environment = "production",
00308 const string &spawnMethod = "smart",
00309 const string &appType = "rails"
00310 ) {
00311 this_thread::disable_syscall_interruption dsi;
00312 MessageChannel channel(data->server);
00313 mutex::scoped_lock l(data->lock);
00314 vector<string> args;
00315 int stream;
00316 bool result;
00317
00318 try {
00319 channel.write("get", appRoot.c_str(),
00320 (lowerPrivilege) ? "true" : "false",
00321 lowestUser.c_str(),
00322 environment.c_str(),
00323 spawnMethod.c_str(),
00324 appType.c_str(),
00325 NULL);
00326 } catch (const SystemException &) {
00327 throw IOException("The ApplicationPool server exited unexpectedly.");
00328 }
00329 try {
00330 result = channel.read(args);
00331 } catch (const SystemException &e) {
00332 throw SystemException("Could not read a message from "
00333 "the ApplicationPool server", e.code());
00334 }
00335 if (!result) {
00336 throw IOException("The ApplicationPool server unexpectedly "
00337 "closed the connection.");
00338 }
00339 if (args[0] == "ok") {
00340 stream = channel.readFileDescriptor();
00341 return ptr(new RemoteSession(dataSmartPointer,
00342 atoi(args[1]), atoi(args[2]), stream));
00343 } else if (args[0] == "SpawnException") {
00344 if (args[2] == "true") {
00345 string errorPage;
00346
00347 if (!channel.readScalar(errorPage)) {
00348 throw IOException("The ApplicationPool server "
00349 "unexpectedly closed the connection.");
00350 }
00351 throw SpawnException(args[1], errorPage);
00352 } else {
00353 throw SpawnException(args[1]);
00354 }
00355 } else if (args[0] == "BusyException") {
00356 throw BusyException(args[1]);
00357 } else if (args[0] == "IOException") {
00358 throw IOException(args[1]);
00359 } else {
00360 throw IOException("The ApplicationPool server returned "
00361 "an unknown message: " + toString(args));
00362 }
00363 }
00364 };
00365
00366
00367 static const int SERVER_SOCKET_FD = 3;
00368
00369 string m_serverExecutable;
00370 string m_spawnServerCommand;
00371 string m_logFile;
00372 string m_rubyCommand;
00373 string m_user;
00374 string statusReportFIFO;
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 pid_t serverPid;
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 int serverSocket;
00395
00396
00397
00398
00399
00400
00401
00402
00403 void shutdownServer() {
00404 this_thread::disable_syscall_interruption dsi;
00405 int ret;
00406 time_t begin;
00407 bool done = false;
00408
00409 InterruptableCalls::close(serverSocket);
00410 if (!statusReportFIFO.empty()) {
00411 do {
00412 ret = unlink(statusReportFIFO.c_str());
00413 } while (ret == -1 && errno == EINTR);
00414 }
00415
00416 P_TRACE(2, "Waiting for existing ApplicationPoolServerExecutable (PID " <<
00417 serverPid << ") to exit...");
00418 begin = InterruptableCalls::time(NULL);
00419 while (!done && InterruptableCalls::time(NULL) < begin + 5) {
00420
00421
00422
00423
00424
00425
00426 InterruptableCalls::kill(serverPid, SIGINT);
00427
00428 ret = InterruptableCalls::waitpid(serverPid, NULL, WNOHANG);
00429 done = ret > 0 || ret == -1;
00430 if (!done) {
00431 InterruptableCalls::usleep(100000);
00432 }
00433 }
00434 if (done) {
00435 P_TRACE(2, "ApplicationPoolServerExecutable exited.");
00436 } else {
00437 P_DEBUG("ApplicationPoolServerExecutable not exited in time. Killing it...");
00438 InterruptableCalls::kill(serverPid, SIGTERM);
00439 InterruptableCalls::waitpid(serverPid, NULL, 0);
00440 }
00441
00442 serverSocket = -1;
00443 serverPid = 0;
00444 }
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 void restartServer() {
00455 int fds[2];
00456 pid_t pid;
00457
00458 if (serverPid != 0) {
00459 shutdownServer();
00460 }
00461
00462 if (InterruptableCalls::socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) {
00463 throw SystemException("Cannot create a Unix socket pair", errno);
00464 }
00465
00466 createStatusReportFIFO();
00467
00468 pid = InterruptableCalls::fork();
00469 if (pid == 0) {
00470 dup2(fds[0], SERVER_SOCKET_FD);
00471
00472
00473 for (long i = sysconf(_SC_OPEN_MAX) - 1; i > SERVER_SOCKET_FD; i--) {
00474 close(i);
00475 }
00476
00477 execlp(
00478 #if 0
00479 "valgrind",
00480 "valgrind",
00481 #else
00482 m_serverExecutable.c_str(),
00483 #endif
00484 m_serverExecutable.c_str(),
00485 toString(Passenger::getLogLevel()).c_str(),
00486 m_spawnServerCommand.c_str(),
00487 m_logFile.c_str(),
00488 m_rubyCommand.c_str(),
00489 m_user.c_str(),
00490 statusReportFIFO.c_str(),
00491 NULL);
00492 int e = errno;
00493 fprintf(stderr, "*** Passenger ERROR: Cannot execute %s: %s (%d)\n",
00494 m_serverExecutable.c_str(), strerror(e), e);
00495 fflush(stderr);
00496 _exit(1);
00497 } else if (pid == -1) {
00498 InterruptableCalls::close(fds[0]);
00499 InterruptableCalls::close(fds[1]);
00500 throw SystemException("Cannot create a new process", errno);
00501 } else {
00502 InterruptableCalls::close(fds[0]);
00503 serverSocket = fds[1];
00504 serverPid = pid;
00505 }
00506 }
00507
00508 void createStatusReportFIFO() {
00509 char filename[PATH_MAX];
00510 int ret;
00511
00512 snprintf(filename, sizeof(filename), "/tmp/passenger_status.%d.fifo",
00513 getpid());
00514 filename[PATH_MAX - 1] = '\0';
00515 do {
00516 ret = mkfifo(filename, S_IRUSR | S_IWUSR);
00517 } while (ret == -1 && errno == EINTR);
00518 if (ret == -1 && errno != EEXIST) {
00519 int e = errno;
00520 P_WARN("*** WARNING: Could not create FIFO '" << filename <<
00521 "': " << strerror(e) << " (" << e << ")" << endl <<
00522 "Disabling Passenger ApplicationPool status reporting.");
00523 statusReportFIFO = "";
00524 } else {
00525 statusReportFIFO = filename;
00526 }
00527 }
00528
00529 public:
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 ApplicationPoolServer(const string &serverExecutable,
00553 const string &spawnServerCommand,
00554 const string &logFile = "",
00555 const string &rubyCommand = "ruby",
00556 const string &user = "")
00557 : m_serverExecutable(serverExecutable),
00558 m_spawnServerCommand(spawnServerCommand),
00559 m_logFile(logFile),
00560 m_rubyCommand(rubyCommand),
00561 m_user(user) {
00562 serverSocket = -1;
00563 serverPid = 0;
00564 this_thread::disable_syscall_interruption dsi;
00565 restartServer();
00566 }
00567
00568 ~ApplicationPoolServer() {
00569 if (serverSocket != -1) {
00570 this_thread::disable_syscall_interruption dsi;
00571 shutdownServer();
00572 }
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607 ApplicationPoolPtr connect() {
00608 try {
00609 this_thread::disable_syscall_interruption dsi;
00610 MessageChannel channel(serverSocket);
00611 int clientConnection;
00612
00613
00614 channel.writeRaw("x", 1);
00615
00616 clientConnection = channel.readFileDescriptor();
00617 return ptr(new Client(clientConnection));
00618 } catch (const SystemException &e) {
00619 throw SystemException("Could not connect to the ApplicationPool server", e.code());
00620 } catch (const IOException &e) {
00621 string message("Could not connect to the ApplicationPool server: ");
00622 message.append(e.what());
00623 throw IOException(message);
00624 }
00625 }
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641 void detach() {
00642 int ret;
00643 do {
00644 ret = close(serverSocket);
00645 } while (ret == -1 && errno == EINTR);
00646 serverSocket = -1;
00647 }
00648 };
00649
00650 typedef shared_ptr<ApplicationPoolServer> ApplicationPoolServerPtr;
00651
00652 }
00653
00654 #endif