[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[myserver-commit] [2841] Define configuration files in myserver. cpp and
From: |
Giuseppe Scrivano |
Subject: |
[myserver-commit] [2841] Define configuration files in myserver. cpp and not in the Server class. |
Date: |
Wed, 24 Sep 2008 19:43:55 +0000 |
Revision: 2841
http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2841
Author: gscrivano
Date: 2008-09-24 19:43:55 +0000 (Wed, 24 Sep 2008)
Log Message:
-----------
Define configuration files in myserver.cpp and not in the Server class. It
closes the task #8587. Thanks to Daniele Cocca <address@hidden>.
Ticket Links:
:-----------
http://savannah.gnu.org/task/?8587
Modified Paths:
--------------
trunk/myserver/include/server/server.h
trunk/myserver/src/myserver.cpp
trunk/myserver/src/server/server.cpp
Modified: trunk/myserver/include/server/server.h
===================================================================
--- trunk/myserver/include/server/server.h 2008-09-24 17:21:34 UTC (rev
2840)
+++ trunk/myserver/include/server/server.h 2008-09-24 19:43:55 UTC (rev
2841)
@@ -65,40 +65,40 @@
class Server : public MulticastRegistry<string, void*, int>
{
public:
- ProcessServerManager* getProcessServerManager()
- {
- return &processServerManager;
- }
- PluginsManager* getPluginsManager(){return &pluginsManager;}
- bool stopServer(){return mustEndServer;}
- HomeDir* getHomeDir();
- static void createInstance();
- static Server* getInstance()
- {
- return instance;
- }
+ ProcessServerManager* getProcessServerManager()
+ {
+ return &processServerManager;
+ }
+ PluginsManager* getPluginsManager(){return &pluginsManager;}
+ bool stopServer(){return mustEndServer;}
+ HomeDir* getHomeDir();
+ static void createInstance();
+ static Server* getInstance()
+ {
+ return instance;
+ }
int loadLibraries();
- CachedFileFactory* getCachedFiles();
+ CachedFileFactory* getCachedFiles();
const char* getHashedData(const char* name);
void setGlobalData(const char* name, void* data);
void* getGlobalData(const char* name);
FiltersFactory* getFiltersFactory();
- int getMaxThreads();
+ int getMaxThreads();
u_long getUid();
u_long getGid();
int countAvailableThreads();
- void checkThreadsNumber();
+ void checkThreadsNumber();
int removeThread(u_long ID);
int isServerReady();
ProtocolsManager* getProtocolsManager();
void disableAutoReboot();
void enableAutoReboot();
int isAutorebootEnabled();
- int isRebooting(){return rebooting;}
+ int isRebooting(){return rebooting;}
void rebootOnNextLoop();
const char* getMainConfFile();
const char* getVhostConfFile();
@@ -107,28 +107,28 @@
const char* getLanguageFile();
const char* getExternalPath();
XmlParser* getLanguageParser();
- ~Server();
- Protocol* getProtocol(const char *protocolName);
- int addConnection(Socket,MYSERVER_SOCKADDRIN*);
- u_long getNumConnections();
- u_long getNumTotalConnections();
+ ~Server();
+ Protocol* getProtocol(const char *protocolName);
+ int addConnection(Socket,MYSERVER_SOCKADDRIN*);
+ u_long getNumConnections();
+ u_long getNumTotalConnections();
void getConnections(list<ConnectionPtr>&);
- ConnectionPtr getConnection(int);
- u_long getTimeout();
- const char *getAddresses();
- const char *getPath();
- u_long getNumThreads();
- const char *getDefaultFilenamePath(u_long ID = 0);
- const char *getServerName();
- u_long getVerbosity();
- const char *getServerAdmin();
- int getMaxLogFileSize();
- int mustUseLogonOption();
- void setVerbosity(u_long);
- void start();
- void stop();
- void finalCleanup();
- int terminate();
+ ConnectionPtr getConnection(int);
+ u_long getTimeout();
+ const char *getAddresses();
+ const char *getPath();
+ u_long getNumThreads();
+ const char *getDefaultFilenamePath(u_long ID = 0);
+ const char *getServerName();
+ u_long getVerbosity();
+ const char *getServerAdmin();
+ int getMaxLogFileSize();
+ int mustUseLogonOption();
+ void setVerbosity(u_long);
+ void start(string &, string &, string &, string &, string &);
+ void stop();
+ void finalCleanup();
+ int terminate();
int logWriteln(const char*);
int logWriteln(string const &str)
{return logWriteln(str.c_str());};
@@ -137,120 +137,118 @@
int logLockAccess();
int logUnlockAccess();
int setLogFile(char*);
- u_long getBuffersize();
- u_long getBuffersize2();
+ u_long getBuffersize();
+ u_long getBuffersize2();
u_long getThrottlingRate();
- int waitNewConnection(u_long tid, u_long timeout);
- XmlParser *getConfiguration(){return &configurationFileManager;}
- ListenThreads *getListenThreads(){return &listenThreads;}
+ int waitNewConnection(u_long tid, u_long timeout);
+ XmlParser *getConfiguration(){return &configurationFileManager;}
+ ListenThreads *getListenThreads(){return &listenThreads;}
- void *getEnvString(){return envString;}
- VhostManager *getVhosts(){return vhostList;}
- MimeManager *getMimeManager(){return mimeManager;}
+ void *getEnvString(){return envString;}
+ VhostManager *getVhosts(){return vhostList;}
+ MimeManager *getMimeManager(){return mimeManager;}
- void setProcessPermissions();
- ConnectionsScheduler* getConnectionsScheduler(){return
&connectionsScheduler;}
- int deleteConnection(ConnectionPtr, int);
+ void setProcessPermissions();
+ ConnectionsScheduler* getConnectionsScheduler(){return
&connectionsScheduler;}
+ int deleteConnection(ConnectionPtr, int);
- void increaseFreeThread();
- void decreaseFreeThread();
+ void increaseFreeThread();
+ void decreaseFreeThread();
private:
friend class ClientsThread;
#ifdef WIN32
- friend int __stdcall control_handler(u_long control_type);
+ friend int __stdcall control_handler(u_long control_type);
#endif
#ifdef NOT_WIN
- friend int control_handler (u_long control_type);
+ friend int control_handler (u_long control_type);
#endif
- /*!
- *When the flag mustEndServer is 1 all the threads are
- *stopped and the application stop its execution.
- */
- int mustEndServer;
+ /*!
+ *When the flag mustEndServer is 1 all the threads are
+ *stopped and the application stop its execution.
+ */
+ int mustEndServer;
- /*! Singleton instance. Call createInstance before use it. */
- static Server* instance;
+ /*! Singleton instance. Call createInstance before use it. */
+ static Server* instance;
- /*! Do not allow to create directly objects. */
- Server();
+ /*! Do not allow to create directly objects. */
+ Server();
void mainLoop();
void loadPlugins();
void displayBoot();
- CachedFileFactory cachedFiles;
+ CachedFileFactory cachedFiles;
- void *envString;
- VhostManager *vhostList;
- MimeManager *mimeManager;
- ListenThreads listenThreads;
- HomeDir homeDir;
+ void *envString;
+ VhostManager *vhostList;
+ MimeManager *mimeManager;
+ ListenThreads listenThreads;
+ HomeDir homeDir;
HashMap<string, string*> hashedData;
HashMap<string, void*> globalData;
FiltersFactory filtersFactory;
DynamicFiltersManager filters;
- DynExecutorManager executors;
+ DynExecutorManager executors;
u_long uid;
u_long gid;
int currentThreadID;
- /*! Used when rebooting to load new configuration files. */
- int pausing;
- ProtocolsManager protocols;
- XmlParser configurationFileManager;
- XmlParser languageParser;
+ /*! Used when rebooting to load new configuration files. */
+ int pausing;
+ ProtocolsManager protocols;
+ XmlParser configurationFileManager;
+ XmlParser languageParser;
int autoRebootEnabled;
int toReboot;
- int rebooting;
+ int rebooting;
LogManager logManager;
int serverReady;
- u_long verbosity;
+ u_long verbosity;
u_long throttlingRate;
- u_long buffersize;
- u_long buffersize2;
- /*! Buffer that contains all the local machine IP values. */
- string *ipAddresses;
- char serverName[HOST_NAME_MAX + 1];
- string* path;
+ u_long buffersize;
+ u_long buffersize2;
+ /*! Buffer that contains all the local machine IP values. */
+ string *ipAddresses;
+ char serverName[HOST_NAME_MAX + 1];
+ string* path;
string* externalPath;
- string* serverAdmin;
- int initialize();
+ string* serverAdmin;
+ int initialize();
int addThread(int staticThread = 0);
- ConnectionPtr addConnectionToList(Socket* s, MYSERVER_SOCKADDRIN
*asock_in,
+ ConnectionPtr addConnectionToList(Socket* s, MYSERVER_SOCKADDRIN *asock_in,
char *ipAddr, char *localIpAddr,
u_short port, u_short localPort, int);
- u_long maxConnections;
- u_long maxConnectionsToAccept;
- void clearAllConnections();
- int freeHashedData();
- u_long connectionTimeout;
- u_long maxLogFileSize;
- int loadMainConfFilesLocation();
- int loadVHostConfFilesLocation();
- int loadMimeConfFilesLocation();
- int loadExternalPath();
- int loadLanguagesPath();
- int loadConfFilesLocation();
- Mutex* connectionsMutex;
- u_long nStaticThreads;
+ u_long maxConnections;
+ u_long maxConnectionsToAccept;
+ void clearAllConnections();
+ int freeHashedData();
+ u_long connectionTimeout;
+ u_long maxLogFileSize;
+ int copyConfigurationFromDefault(const char *);
+ void logWriteNTimes(const char *, unsigned);
+ int checkConfigurationPaths();
+ bool resetConfigurationPaths(string &, string &, string &, string &, string
&);
+ Mutex* connectionsMutex;
+ u_long nStaticThreads;
u_long nMaxThreads;
- u_long freeThreads;
+ u_long freeThreads;
- u_long purgeThreadsThreshold;
+ u_long purgeThreadsThreshold;
Mutex* threadsMutex;
list<ClientsThread*> threads;
int purgeThreads();
- int reboot();
- string* languageFile;
- string* languagesPath;
- string* mainConfigurationFile;
- string* vhostConfigurationFile;
- string* mimeConfigurationFile;
- PluginsManager pluginsManager;
- GenericPluginsManager genericPluginsManager;
- ProcessServerManager processServerManager;
- ConnectionsScheduler connectionsScheduler;
+ int reboot();
+ string* languageFile;
+ string* languagesPath;
+ string* mainConfigurationFile;
+ string* vhostConfigurationFile;
+ string* mimeConfigurationFile;
+ PluginsManager pluginsManager;
+ GenericPluginsManager genericPluginsManager;
+ ProcessServerManager processServerManager;
+ ConnectionsScheduler connectionsScheduler;
bool endServer;
};
Modified: trunk/myserver/src/myserver.cpp
===================================================================
--- trunk/myserver/src/myserver.cpp 2008-09-24 17:21:34 UTC (rev 2840)
+++ trunk/myserver/src/myserver.cpp 2008-09-24 19:43:55 UTC (rev 2841)
@@ -17,6 +17,7 @@
#include "stdafx.h"
#include <include/server/server.h>
+#include <include/base/file/files_utility.h>
#include <include/base/string/stringutils.h>
#include <include/conf/security/security.h>
extern "C" {
@@ -38,11 +39,11 @@
#define MYSERVER_RUNAS_CONSOLE 1
#define MYSERVER_RUNAS_SERVICE 2
-void consoleService ();
+void consoleService(string &, string &, string &, string &, string &);
#ifdef WIN32
void __stdcall myServerCtrlHandler(u_long fdwControl);
-void __stdcall myServerMain (u_long , LPTSTR *argv);
+void __stdcall myServerMain (u_long , LPTSTR *argv);
static BOOL SignalHandler(DWORD type) ;
#else
int writePidfile(const char*);
@@ -51,7 +52,6 @@
void registerService();
void removeService();
void RunAsService();
-static char *path;
int argn;
char **argv;
@@ -75,8 +75,8 @@
registerSignals();
}
#else
-static BOOL SignalHandler(DWORD type)
-{
+static BOOL SignalHandler(DWORD type)
+{
Server::getInstance()->logWriteln("Exiting...");
Server::getInstance()->stop();
registerSignals();
@@ -89,7 +89,7 @@
{
#ifdef NOT_WIN
struct sigaction sig1, sig2, sig3;
- sig1.sa_flags = sig2.sa_flags = sig3.sa_flags = SA_RESETHAND;
+
memset(&sig1, 0, sizeof(sig1));
memset(&sig2, 0, sizeof(sig2));
memset(&sig3, 0, sizeof(sig3));
@@ -124,25 +124,25 @@
static char argsDoc[] = "";
/* Use the GNU C argp parser under not windows environments. */
-static struct argp_option options[] =
+static struct argp_option options[] =
{
/* LONG NAME - SHORT NAME - PARAMETER NAME - FLAGS - DESCRIPTION. */
{"version", 'v', "VERSION", OPTION_ARG_OPTIONAL , "Print the version for the
application"},
{"run", 'r', "RUN", OPTION_ARG_OPTIONAL, "Specify how run the server (by
default console mode)"},
{"logfile", 'l', "log", 0, "Specify the file to use to log main myserver
messages"},
- {"pidfile", 'p', "pidfile", OPTION_HIDDEN, "Specify the file where write the
PID"},
+ {"pidfile", 'p', "pidfile", OPTION_HIDDEN, "Specify the file where write the
PID"},
{0}
};
static error_t parseOpt(int key, char *arg, struct argp_state *state)
-{
+{
argp_input *in = static_cast<argp_input*>(state->input);
switch(key)
{
case 'v':
in->version = 1;
break;
-
+
case 'r':
if(arg)
{
@@ -158,15 +158,15 @@
case 'l':
in->logFileName = arg;
break;
-
+
case 'p':
in->pidFileName = arg;
break;
-
+
case ARGP_KEY_ARG:
case ARGP_KEY_END:
break;
-
+
default:
return static_cast<error_t>(ARGP_ERR_UNKNOWN);
}
@@ -177,7 +177,230 @@
#endif
+
/*!
+ *Load the external path.
+ *Return nonzero on errors.
+ */
+int loadExternalPath(string &externalPath)
+{
+ try
+ {
+ externalPath = "";
+
+#ifdef NOT_WIN
+ if(FilesUtility::fileExists("plugins"))
+ externalPath.assign("plugins");
+ else
+ {
+#ifdef PREFIX
+ externalPath.assign(PREFIX);
+ externalPath.append("/lib/myserver/plugins");
+#else
+ externalPath.assign("/usr/lib/myserver/plugins");
+#endif
+ }
+
+#endif
+
+#ifdef WIN32
+ externalPath.assign("plugins");
+#endif
+ }
+ catch (...)
+ {
+ }
+ return 0;
+}
+
+
+/*!
+ *Load the languages path.
+ *Return nonzero on errors.
+ */
+int loadLanguagesPath(string &languagesPath)
+{
+ try
+ {
+ languagesPath = "";
+
+#ifndef WIN32
+ /*
+ *Do not use the files in the directory /usr/share/myserver/languages
+ *if exists a local directory.
+ */
+ if (FilesUtility::fileExists("languages"))
+ {
+ languagesPath.assign(getdefaultwd(0, 0));
+ languagesPath.append("/languages/");
+ }
+ else
+ {
+#ifdef PREFIX
+ languagesPath.assign(PREFIX);
+ languagesPath.append("/share/myserver/languages/");
+#else
+ /* Default PREFIX is /usr/. */
+ languagesPath.assign("/usr/share/myserver/languages/");
+#endif
+ }
+#endif
+
+#ifdef WIN32
+ languagesPath.assign( "languages/" );
+#endif
+ }
+ catch (...)
+ {
+ }
+ return 0;
+}
+
+/*!
+ *Load the vhost configuration files locations.
+ *Return nonzero on errors.
+ */
+int loadVHostConfFilesLocation(string &vhostConfigurationFile)
+{
+ try
+ {
+ vhostConfigurationFile = "";
+
+#ifndef WIN32
+ /*
+ *Under an *nix environment look for .xml files in the following order.
+ *1) myserver executable working directory
+ *2) ~/.myserver/
+ *3) /etc/myserver/
+ *4) default files will be copied in myserver executable working
+ */
+ if (FilesUtility::fileExists("virtualhosts.xml"))
+ {
+ vhostConfigurationFile.assign("virtualhosts.xml");
+ }
+ else if (FilesUtility::fileExists("~/.myserver/virtualhosts.xml"))
+ {
+ vhostConfigurationFile.assign("~/.myserver/virtualhosts.xml");
+ }
+ else if (FilesUtility::fileExists("/etc/myserver/virtualhosts.xml"))
+ {
+ vhostConfigurationFile.assign("/etc/myserver/virtualhosts.xml");
+ }
+#endif
+ }
+ catch (...)
+ {
+ }
+ return 0;
+}
+
+
+/*!
+ *Load the mime configuration files locations.
+ *Return nonzero on errors.
+ */
+int loadMimeConfFilesLocation(string &mimeConfigurationFile)
+{
+ try
+ {
+ mimeConfigurationFile = "";
+
+#ifndef WIN32
+ /*
+ *Under an *nix environment look for .xml files in the following order.
+ *1) myserver executable working directory
+ *2) ~/.myserver/
+ *3) /etc/myserver/
+ *4) default files will be copied in myserver executable working
+ */
+ if (FilesUtility::fileExists("MIMEtypes.xml"))
+ {
+ mimeConfigurationFile.assign("MIMEtypes.xml");
+ }
+ else if (FilesUtility::fileExists("~/.myserver/MIMEtypes.xml"))
+ {
+ mimeConfigurationFile.assign("~/.myserver/MIMEtypes.xml");
+ }
+ else if (FilesUtility::fileExists("/etc/myserver/MIMEtypes.xml"))
+ {
+ mimeConfigurationFile.assign("/etc/myserver/MIMEtypes.xml");
+ }
+#endif
+ }
+ catch (...)
+ {
+ }
+ return 0;
+}
+
+
+/*!
+ *Load the main configuration files locations.
+ *Return nonzero on errors.
+ */
+int loadMainConfFilesLocation(string &mainConfigurationFile)
+{
+ try
+ {
+ mainConfigurationFile = "";
+
+#ifndef WIN32
+
+ /*
+ *Under an *nix environment look for .xml files in the following order.
+ *1) myserver executable working directory
+ *2) ~/.myserver/
+ *3) /etc/myserver/
+ *4) default files will be copied in myserver executable working
+ */
+ if (FilesUtility::fileExists("myserver.xml"))
+ {
+ mainConfigurationFile.assign("myserver.xml");
+ }
+ else if (FilesUtility::fileExists("~/.myserver/myserver.xml"))
+ {
+ mainConfigurationFile.assign("~/.myserver/myserver.xml");
+ }
+ else if (FilesUtility::fileExists("/etc/myserver/myserver.xml"))
+ {
+ mainConfigurationFile.assign("/etc/myserver/myserver.xml");
+ }
+#endif
+ }
+ catch (...)
+ {
+ }
+ return 0;
+}
+
+
+/*!
+ *Load the configuration files locations.
+ *Return nonzero on errors.
+ */
+int loadConfFilesLocation(string &mainConfigurationFile, string
&mimeConfigurationFile,
+ string &vhostConfigurationFile, string &externalPath, string
&languagesPath)
+{
+ if (loadMainConfFilesLocation(mainConfigurationFile))
+ return -1;
+
+ if (loadMimeConfFilesLocation(mimeConfigurationFile))
+ return -1;
+
+ if (loadVHostConfFilesLocation(vhostConfigurationFile))
+ return -1;
+
+ if (loadExternalPath(externalPath))
+ return -1;
+
+ if (loadLanguagesPath(languagesPath))
+ return -1;
+
+ return 0;
+}
+
+
+/*!
*Main function for MyServer.
*/
int main (int argn, char **argv)
@@ -190,7 +413,8 @@
pid_t pid;
pid_t sid;
#endif
-
+ string mainConf, mimeConf, vhostConf, externPath, langPath;
+
::argn=argn;
::argv=argv;
@@ -205,11 +429,12 @@
/* Die if we get exceptions here. */
return(1);
};
-
+
{
u_int pathLen = 0;
u_int len = 0;
bool differentCwd = false;
+ char *path;
pathLen = strlen(argv[0]);
path = new char[pathLen + 1];
@@ -225,23 +450,23 @@
break;
}
}
- len = pathLen;
- while((path[len] != '\\') && (path[len] != '/'))
- len--;
- path[len] = '\0';
-
/* Current working directory is where the myserver executable is. */
if(differentCwd)
{
+ len = pathLen;
+ while((path[len] != '\\') && (path[len] != '/'))
+ len--;
+ path[len] = '\0';
+
setcwd(path);
}
/* We can free path memory now. */
delete [] path;
}
-
-
+
+
#ifdef ARGP
/* Reset the struct. */
input.version = 0;
@@ -263,19 +488,19 @@
if(input.version)
{
cout << MYSERVER_VERSION << endl;
-
- cout
+
+ cout
#ifdef __DATE__
- << "Compiled on " << __DATE__
+ << "Compiled on " << __DATE__
#endif
<< endl;
cout << "http://www.gnu.org/software/myserver" << endl;
- return 0;
+ return 0;
}
#else
if(argn > 1)
- {
+ {
if(!strcmpi(argv[1], "VERSION"))
{
cout << MYSERVER_VERSION << endl;
@@ -318,10 +543,13 @@
*/
try
{
+ setcwdBuffer();
+ loadConfFilesLocation(mainConf, mimeConf, vhostConf, externPath,
langPath);
+
switch(runas)
{
case MYSERVER_RUNAS_CONSOLE:
- consoleService();
+ consoleService(mainConf, mimeConf, vhostConf, externPath, langPath);
break;
case MYSERVER_RUNAS_SERVICE:
#ifdef WIN32
@@ -334,17 +562,17 @@
*/
pid = fork();
- /*
+ /*
*An error happened, return with errors.
*/
- if (pid < 0)
+ if (pid < 0)
{
return 1;
}
/*
- *A good process id, return with success.
+ *A good process id, return with success.
*/
- if (pid > 0)
+ if (pid > 0)
{
return 0;
}
@@ -367,15 +595,15 @@
/*
*Error in setting a new sid, return the error.
*/
- if (sid < 0)
+ if (sid < 0)
{
return 1;
}
-
+
/*
*Finally run the server from the forked process.
*/
- consoleService();
+ consoleService(mainConf, mimeConf, vhostConf, externPath, langPath);
#endif
break;
}
@@ -385,7 +613,7 @@
return 1;
};
return 0;
-}
+}
#ifndef WIN32
@@ -395,7 +623,7 @@
int writePidfile(const char* filename)
{
int pidfile;
- pid_t pid = getpid();
+ pid_t pid = getpid();
char buff[12];
int ret;
pidfile = open(filename, O_RDWR | O_CREAT);
@@ -414,9 +642,9 @@
/*!
*Start MyServer in console mode.
*/
-void consoleService ()
+void consoleService(string &mainConf, string &mimeConf, string &vhostConf,
string &externPath, string &langPath)
{
- Server::getInstance()->start();
+ Server::getInstance()->start(mainConf, mimeConf, vhostConf, externPath,
langPath);
}
@@ -424,8 +652,8 @@
*These functions are available only on the windows platform.
*/
#ifdef WIN32
-SERVICE_STATUS MyServiceStatus;
-SERVICE_STATUS_HANDLE MyServiceStatusHandle;
+SERVICE_STATUS MyServiceStatus;
+SERVICE_STATUS_HANDLE MyServiceStatusHandle;
/*!
*Entry-point for the NT service.
@@ -439,7 +667,7 @@
MyServiceStatus.dwServiceSpecificExitCode = NO_ERROR;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
-
+
try
{
Server::createInstance();
@@ -450,25 +678,25 @@
return;
};
-
- MyServiceStatusHandle = RegisterServiceCtrlHandler( "MyServer",
+
+ MyServiceStatusHandle = RegisterServiceCtrlHandler( "MyServer",
myServerCtrlHandler );
if(MyServiceStatusHandle)
{
MyServiceStatus.dwCurrentState = SERVICE_START_PENDING;
SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus );
-
- MyServiceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP
+
+ MyServiceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN);
MyServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus );
-
+
Server::getInstance()->start();
-
+
MyServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus );
-
- MyServiceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP
+
+ MyServiceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN);
MyServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus );
@@ -484,17 +712,17 @@
{
case SERVICE_CONTROL_INTERROGATE:
break;
-
+
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
MyServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus( MyServiceStatusHandle, &MyServiceStatus );
Server::getInstance()->stop();
return;
-
+
case SERVICE_CONTROL_PAUSE:
break;
-
+
case SERVICE_CONTROL_CONTINUE:
break;
default:
@@ -548,7 +776,7 @@
GetCurrentDirectory(MAX_PATH,path);
strcat(path,"\\");
strcat(path,"myserver.exe SERVICE");
-
+
manager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (manager)
{
@@ -592,7 +820,7 @@
}
/*!
- *Start the service.
+ *Start the service.
*/
void RunAsService()
{
Modified: trunk/myserver/src/server/server.cpp
===================================================================
--- trunk/myserver/src/server/server.cpp 2008-09-24 17:21:34 UTC (rev
2840)
+++ trunk/myserver/src/server/server.cpp 2008-09-24 19:43:55 UTC (rev
2841)
@@ -101,14 +101,127 @@
freeThreads = 0;
}
+
/*!
+ *Reinitialize the configuration paths, setting them to the specified ones.
+ *Returns false on error.
+ */
+bool Server::resetConfigurationPaths(string &mainConf, string &mimeConf,
string &vhostConf, string &externPath, string &langPath)
+{
+ if (!mainConfigurationFile)
+ mainConfigurationFile = new string(mainConf);
+ else
+ mainConfigurationFile->assign(mainConf);
+
+ if (!mimeConfigurationFile)
+ mimeConfigurationFile = new string(mimeConf);
+ else
+ mimeConfigurationFile->assign(mimeConf);
+
+ if (!vhostConfigurationFile)
+ vhostConfigurationFile = new string(vhostConf);
+ else
+ vhostConfigurationFile->assign(vhostConf);
+
+ if (!externalPath)
+ externalPath = new string(externPath);
+ else
+ externalPath->assign(externPath);
+
+ if (!languagesPath)
+ languagesPath = new string(langPath);
+ else
+ languagesPath->assign(langPath);
+
+ return true;
+}
+
+/*!
+ *Check that the configuration paths are not empty, otherwise fall back to
+ *the default ones.
+ *Returns nonzero on error.
+ */
+int Server::checkConfigurationPaths()
+{
+ if (mainConfigurationFile->length() == 0)
+ {
+ mainConfigurationFile->assign("myserver.xml");
+
+ if (copyConfigurationFromDefault("myserver.xml"))
+ {
+ logPreparePrintError();
+ logWriteln("Error loading configuration file\n");
+ logEndPrintError();
+ return -1;
+ }
+ }
+
+ if (mimeConfigurationFile->length() == 0)
+ {
+ mimeConfigurationFile->assign("MIMEtypes.xml");
+
+ if (copyConfigurationFromDefault("MIMEtypes.xml"))
+ {
+ logPreparePrintError();
+ logWriteln(languageParser.getValue("ERR_LOADMIME"));
+ logEndPrintError();
+ return -1;
+ }
+ }
+
+ if (vhostConfigurationFile->length() == 0)
+ {
+ vhostConfigurationFile->assign("virtualhosts.xml");
+
+ if (copyConfigurationFromDefault("virtualhosts.xml") != 0)
+ {
+ logPreparePrintError();
+ logWriteln(languageParser.getValue("ERR_LOADVHOSTS"));
+ logEndPrintError();
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+
+/*!
+ *Copy a configuration file from the default one.
+ *Return nonzero on errors.
+ */
+int Server::copyConfigurationFromDefault(const char *fileName)
+{
+ File inputF;
+ File outputF;
+ int ret;
+ string sSource(fileName);
+
+ sSource.append(".default");
+ ret = inputF.openFile(sSource, File::MYSERVER_OPEN_READ
+ | File::MYSERVER_OPEN_IFEXISTS);
+ if (ret)
+ return -1;
+
+ ret = outputF.openFile(fileName, File::MYSERVER_OPEN_WRITE
+ | File::MYSERVER_OPEN_ALWAYS);
+ if (ret)
+ return -1;
+
+ FilesUtility::copyFile(inputF, outputF);
+
+ inputF.close();
+ outputF.close();
+}
+
+
+/*!
*Load here all the libraries.
*/
int Server::loadLibraries()
{
Process::initialize();
- setcwdBuffer();
XmlParser::startXML();
myserver_safetime_init();
@@ -140,7 +253,7 @@
/*!
*Start the server.
*/
-void Server::start()
+void Server::start(string &mainConf, string &mimeConf, string &vhostConf,
string &externPath, string &langPath)
{
u_long i;
string buffer;
@@ -166,8 +279,14 @@
*Setup the server configuration.
*/
logWriteln("Initializing server configuration...");
- err = 0;
+ if (!resetConfigurationPaths(mainConf, mimeConf, vhostConf, externPath,
langPath))
+ return;
+
+ err = checkConfigurationPaths();
+ if (err)
+ return;
+
err = initialize();
if(err)
return;
@@ -520,6 +639,13 @@
}
}
+void Server::logWriteNTimes(const char *str, unsigned n)
+{
+ while (n--)
+ logManager.write(str);
+ logManager.writeln("");
+}
+
/*!
*Display the MyServer boot.
*/
@@ -560,21 +686,16 @@
{
try
{
+ size_t length;
string softwareSignature;
softwareSignature.assign("************");
softwareSignature.append(MYSERVER_VERSION);
softwareSignature.append("************");
+ length = softwareSignature.length();
- i = softwareSignature.length();
- while(i--)
- logManager.write("*");
- logManager.writeln("");
- logManager.write(softwareSignature.c_str());
- logManager.write("\n");
- i = softwareSignature.length();
- while(i--)
- logManager.write("*");
- logManager.writeln("");
+ logWriteNTimes("*", length);
+ logManager.writeln(softwareSignature.c_str());
+ logWriteNTimes("*", length);
}
catch(exception& e)
{
@@ -897,9 +1018,6 @@
serverAdmin = 0;
autoRebootEnabled = 1;
- if(loadConfFilesLocation())
- return -1;
-
if(configurationFileManager.open(mainConfigurationFile->c_str()))
return -1;
@@ -1081,6 +1199,7 @@
return 0;
}
+
/*!
*Check if there are free threads to handle a new request. If there
*are not enough threads create a new one.
@@ -1530,341 +1649,8 @@
return externalPath ? externalPath->c_str() : "";
}
-/*!
- *Load the main configuration files locations.
- *Return nonzero on errors.
- */
-int Server::loadMainConfFilesLocation()
-{
- int ret;
- try
- {
- if (mainConfigurationFile)
- delete mainConfigurationFile;
- mainConfigurationFile = new string();
-#ifndef WIN32
-
- /*
- *Under an *nix environment look for .xml files in the following order.
- *1) myserver executable working directory
- *2) ~/.myserver/
- *3) /etc/myserver/
- *4) default files will be copied in myserver executable working
- */
- if (FilesUtility::fileExists("myserver.xml"))
- {
- mainConfigurationFile->assign("myserver.xml");
- }
- else if (FilesUtility::fileExists("~/.myserver/myserver.xml"))
- {
- mainConfigurationFile->assign("~/.myserver/myserver.xml");
- }
- else if (FilesUtility::fileExists("/etc/myserver/myserver.xml"))
- {
- mainConfigurationFile->assign("/etc/myserver/myserver.xml");
- }
- else
-#endif
- /* If the myserver.xml files doesn't exist copy it from the default one.
*/
- if (mainConfigurationFile->length() == 0)
- {
- mainConfigurationFile->assign("myserver.xml");
- File inputF;
- File outputF;
- ret = inputF.openFile("myserver.xml.default", File::MYSERVER_OPEN_READ
- | File::MYSERVER_OPEN_IFEXISTS);
- if (ret)
- {
- logPreparePrintError();
- logWriteln("Error loading configuration file\n");
- logEndPrintError();
- return -1;
- }
- ret = outputF.openFile("myserver.xml", File::MYSERVER_OPEN_WRITE
- | File::MYSERVER_OPEN_ALWAYS);
- if (ret)
- {
- logPreparePrintError();
- logWriteln("Error loading configuration file\n");
- logEndPrintError();
- return -1;
- }
-
- FilesUtility::copyFile(inputF, outputF);
-
- inputF.close();
- outputF.close();
- }
- else
- {
- mainConfigurationFile->assign("myserver.xml");
- }
- }
- catch (...)
- {
- }
- return 0;
-}
-
-
/*!
- *Load the mime configuration files locations.
- *Return nonzero on errors.
- */
-int Server::loadMimeConfFilesLocation()
-{
- int ret;
- try
- {
- if (mimeConfigurationFile)
- delete mimeConfigurationFile;
-
- mimeConfigurationFile = new string();
-
-#ifndef WIN32
- /*
- *Under an *nix environment look for .xml files in the following order.
- *1) myserver executable working directory
- *2) ~/.myserver/
- *3) /etc/myserver/
- *4) default files will be copied in myserver executable working
- */
- if (FilesUtility::fileExists("MIMEtypes.xml"))
- {
- mimeConfigurationFile->assign("MIMEtypes.xml");
- }
- else if (FilesUtility::fileExists("~/.myserver/MIMEtypes.xml"))
- {
- mimeConfigurationFile->assign("~/.myserver/MIMEtypes.xml");
- }
- else if (FilesUtility::fileExists("/etc/myserver/MIMEtypes.xml"))
- {
- mimeConfigurationFile->assign("/etc/myserver/MIMEtypes.xml");
- }
- else
-#endif
- /*
- *If the MIMEtypes.xml files doesn't exist copy it
- *from the default one.
- */
- if (mimeConfigurationFile->length() == 0)
- {
- File inputF;
- File outputF;
- mimeConfigurationFile->assign("MIMEtypes.xml");
- ret = inputF.openFile("MIMEtypes.xml.default", File::MYSERVER_OPEN_READ
- | File::MYSERVER_OPEN_IFEXISTS);
- if (ret)
- {
- logPreparePrintError();
- logWriteln(languageParser.getValue("ERR_LOADMIME"));
- logEndPrintError();
- return -1;
- }
- ret = outputF.openFile("MIMEtypes.xml", File::MYSERVER_OPEN_WRITE
- | File::MYSERVER_OPEN_ALWAYS);
- if (ret)
- return -1;
-
- FilesUtility::copyFile(inputF, outputF);
-
- inputF.close();
- outputF.close();
- }
-
- }
- catch (...)
- {
- }
- return 0;
-}
-
-
-/*!
- *Load the vhost configuration files locations.
- *Return nonzero on errors.
- */
-int Server::loadVHostConfFilesLocation()
-{
- int ret;
- try
- {
- if (vhostConfigurationFile)
- delete vhostConfigurationFile;
-
- vhostConfigurationFile = new string();
-
-#ifndef WIN32
- /*
- *Under an *nix environment look for .xml files in the following order.
- *1) myserver executable working directory
- *2) ~/.myserver/
- *3) /etc/myserver/
- *4) default files will be copied in myserver executable working
- */
- if (FilesUtility::fileExists("virtualhosts.xml"))
- {
- vhostConfigurationFile->assign("virtualhosts.xml");
- }
- else if (FilesUtility::fileExists("~/.myserver/virtualhosts.xml"))
- {
- vhostConfigurationFile->assign("~/.myserver/virtualhosts.xml");
- }
- else if (FilesUtility::fileExists("/etc/myserver/virtualhosts.xml"))
- {
- vhostConfigurationFile->assign("/etc/myserver/virtualhosts.xml");
- }
- else
-#endif
- /*
- *If the virtualhosts.xml file doesn't exist copy it
- *from the default one.
- */
- if (vhostConfigurationFile->length() == 0)
- {
- File inputF;
- File outputF;
-
- vhostConfigurationFile->assign("virtualhosts.xml");
- ret = inputF.openFile("virtualhosts.xml.default",
- File::MYSERVER_OPEN_READ | File::MYSERVER_OPEN_IFEXISTS);
- if (ret)
- {
- logPreparePrintError();
- logWriteln(languageParser.getValue("ERR_LOADVHOSTS"));
- logEndPrintError();
- return -1;
- }
- ret = outputF.openFile("virtualhosts.xml", File::MYSERVER_OPEN_WRITE
- | File::MYSERVER_OPEN_ALWAYS);
- if (ret)
- return -1;
-
- FilesUtility::copyFile(inputF, outputF);
-
- inputF.close();
- outputF.close();
- }
- }
- catch (...)
- {
- }
- return 0;
-}
-
-/*!
- *Load the external path.
- *Return nonzero on errors.
- */
-int Server::loadExternalPath()
-{
- try
- {
- if (externalPath)
- delete externalPath;
- externalPath = new string();
-
-#ifdef NOT_WIN
- if(FilesUtility::fileExists("plugins"))
- externalPath->assign("plugins");
- else
- {
-#ifdef PREFIX
- externalPath->assign(PREFIX);
- externalPath->append("/lib/myserver/plugins");
-#else
- externalPath->assign("/usr/lib/myserver/plugins");
-#endif
- }
-
-#endif
-
-#ifdef WIN32
- externalPath->assign("plugins");
-#endif
- }
- catch (...)
- {
- }
- return 0;
-}
-
-
-
-
-/*!
- *Load the languages path.
- *Return nonzero on errors.
- */
-int Server::loadLanguagesPath()
-{
- try
- {
- if (languagesPath)
- delete languagesPath;
-
- languagesPath = new string();
-
-#ifndef WIN32
- /*
- *Do not use the files in the directory /usr/share/myserver/languages
- *if exists a local directory.
- */
- if (FilesUtility::fileExists("languages"))
- {
- languagesPath->assign(getdefaultwd(0, 0));
- languagesPath->append("/languages/");
- }
- else
- {
-#ifdef PREFIX
- languagesPath->assign(PREFIX);
- languagesPath->append("/share/myserver/languages/");
-#else
- /* Default PREFIX is /usr/. */
- languagesPath->assign("/usr/share/myserver/languages/");
-#endif
- }
-#endif
-
-#ifdef WIN32
- languagesPath->assign( "languages/" );
-#endif
- }
- catch (...)
- {
- }
- return 0;
-}
-
-
-/*!
- *Load the configuration files locations.
- *Return nonzero on errors.
- */
-int Server::loadConfFilesLocation()
-{
- if (loadMainConfFilesLocation())
- return -1;
-
- if (loadMimeConfFilesLocation())
- return -1;
-
- if (loadVHostConfFilesLocation())
- return -1;
-
- if (loadExternalPath())
- return -1;
-
- if (loadLanguagesPath())
- return -1;
-
- return 0;
-}
-
-
-/*!
*If specified set the uid/gid for the process.
*/
void Server::setProcessPermissions()
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [myserver-commit] [2841] Define configuration files in myserver. cpp and not in the Server class.,
Giuseppe Scrivano <=