00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _PASSENGER_DUMMY_SPAWN_MANAGER_H_
00019 #define _PASSENGER_DUMMY_SPAWN_MANAGER_H_
00020
00021
00022 #define DUMMY_REQUEST_HANDLER_EXECUTABLE "/home/hongli/Projects/passenger/benchmark/DummyRequestHandler"
00023
00024 #include <string>
00025
00026 #include <sys/types.h>
00027 #include <sys/wait.h>
00028 #include <cstdio>
00029 #include <unistd.h>
00030 #include <errno.h>
00031
00032 #include "Application.h"
00033 #include "Exceptions.h"
00034
00035 namespace Passenger {
00036
00037 using namespace std;
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 class DummySpawnManager {
00056 public:
00057 ApplicationPtr spawn(const string &appRoot, const string &user = "", const string &group = "") {
00058 int fds[2];
00059 pid_t pid;
00060
00061 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) {
00062 throw SystemException("Cannot create a Unix socket", errno);
00063 }
00064 pid = fork();
00065 if (pid == 0) {
00066 pid = fork();
00067 if (pid == 0) {
00068 dup2(fds[0], STDIN_FILENO);
00069 close(fds[0]);
00070 close(fds[1]);
00071 execlp(DUMMY_REQUEST_HANDLER_EXECUTABLE, DUMMY_REQUEST_HANDLER_EXECUTABLE, NULL);
00072 int e = errno;
00073 fprintf(stderr, "Unable to run %s: %s\n", DUMMY_REQUEST_HANDLER_EXECUTABLE, strerror(e));
00074 fflush(stderr);
00075 _exit(1);
00076 } else if (pid == -1) {
00077 perror("Cannot fork a new process");
00078 fflush(stderr);
00079 _exit(1);
00080 } else {
00081 _exit(0);
00082 }
00083 } else if (pid == -1) {
00084 close(fds[0]);
00085 close(fds[1]);
00086 throw SystemException("Cannot fork a new process", errno);
00087 } else {
00088 close(fds[0]);
00089 waitpid(pid, NULL, 0);
00090 return ApplicationPtr(new Application(appRoot, pid, fds[1]));
00091 }
00092 }
00093
00094 pid_t getServerPid() const {
00095 return 0;
00096 }
00097 };
00098
00099 }
00100
00101 #endif