BUG 304 - Allow for multiple installed cimserver windows service instances
authortony <tony>
Tue, 27 May 2003 21:37:03 +0000 (21:37 +0000)
committertony <tony>
Tue, 27 May 2003 21:37:03 +0000 (21:37 +0000)
src/Server/cimserver.cpp
src/Server/cimserver_windows.cpp
src/Server/service.h

index 59703108c66e14e96c1abbe24625f327a6dd0483..f253c7d892f0283fef32cb888abf428c37a5822c 100644 (file)
@@ -216,20 +216,28 @@ void PrintHelp(const char* arg0)
     usage.append (COMMAND_NAME);
     usage.append (" [ [ options ] | [ configProperty=value, ... ] ]\n");
     usage.append ("  options\n");
-    usage.append ("    -v          - displays CIM Server version number\n");
-    usage.append ("    -h          - prints this help message\n");
-    usage.append ("    -s          - shuts down CIM Server\n");
+    usage.append ("    -v              - displays CIM Server version number\n");
+    usage.append ("    -h              - prints this help message\n");
+    usage.append ("    -s              - shuts down CIM Server\n");
 #if !defined(PEGASUS_OS_HPUX) && !defined(PEGASUS_PLATFORM_LINUX_IA64_GNU)
-    usage.append ("    -D [home]   - sets pegasus home directory\n");
+    usage.append ("    -D [home]       - sets pegasus home directory\n");
 #endif
 #if defined(PEGASUS_OS_TYPE_WINDOWS)
-    usage.append ("    -install    - installs pegasus as a Windows NT Service\n");
-    usage.append ("    -remove     - removes pegasus as a Windows NT Service\n");
-    usage.append ("    -start    - starts pegasus as a Windows NT Service\n");
-    usage.append ("    -stop     - stops pegasus as a Windows NT Service\n");
+    usage.append ("    -install [name] - installs pegasus as a Windows NT Service\n");
+    usage.append ("                      [name] is optional and overrides the\n");
+    usage.append ("                      default CIM Server Service Name\n");
+    usage.append ("    -remove [name]  - removes pegasus as a Windows NT Service\n");
+    usage.append ("                      [name] is optional and overrides the\n");
+    usage.append ("                      default CIM Server Service Name\n");
+    usage.append ("    -start [name]   - starts pegasus as a Windows NT Service\n");
+    usage.append ("                      [name] is optional and overrides the\n");
+    usage.append ("                      default CIM Server Service Name\n");
+    usage.append ("    -stop [name]    - stops pegasus as a Windows NT Service\n");
+    usage.append ("                      [name] is optional and overrides the\n");
+    usage.append ("                      default CIM Server Service Name\n\n");
 #endif
     usage.append ("  configProperty=value\n");
-    usage.append ("                - sets CIM Server configuration property\n");
+    usage.append ("                    - sets CIM Server configuration property\n");
 
     cout << endl;
 #if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_PLATFORM_LINUX_IA64_GNU)
@@ -552,7 +560,13 @@ int main(int argc, char** argv)
                   //
                   // Install as a NT service
                   //
-                  if(cimserver_install_nt_service())
+                  char *opt_arg = NULL;
+                  if (i+1 < argc)
+                  {
+                    opt_arg = argv[i+1];
+                    
+                  }
+                  if(cimserver_install_nt_service(opt_arg))
                   {
                       cout << "\nPegasus installed as NT Service";
                       exit(0);
@@ -567,7 +581,12 @@ int main(int argc, char** argv)
                   //
                   // Remove Pegasus as an NT service
                   //
-                  if(cimserver_remove_nt_service())
+                  char *opt_arg = NULL;
+                  if (i+1 < argc)
+                  {
+                    opt_arg = argv[i+1];                    
+                  }
+                  if(cimserver_remove_nt_service(opt_arg))
                   {
                       cout << "\nPegasus removed as NT Service";
                       exit(0);
@@ -583,7 +602,12 @@ int main(int argc, char** argv)
                   //
                   // Start as a NT service
                   //
-                  if(cimserver_start_nt_service())
+                  char *opt_arg = NULL;
+                  if (i+1 < argc)
+                  {
+                    opt_arg = argv[i+1];                    
+                  }
+                  if(cimserver_start_nt_service(opt_arg))
                   {
                       cout << "\nPegasus started as NT Service";
                       exit(0);
@@ -598,7 +622,12 @@ int main(int argc, char** argv)
                   //
                   // Stop as a NT service
                   //
-                  if(cimserver_stop_nt_service())
+                  char *opt_arg = NULL;
+                  if (i+1 < argc)
+                  {
+                    opt_arg = argv[i+1];                    
+                  }
+                  if(cimserver_stop_nt_service(opt_arg))
                   {
                       cout << "\nPegasus stopped as NT Service";
                       exit(0);
index e2f7d3ee9995b5be36f7a20eaf164aabff474008..9e681c7dba2b5a6425607db3af5bb0444e977b09 100644 (file)
@@ -54,7 +54,7 @@ PEGASUS_USING_STD;
 CIMServer *server_windows;
 static Service pegasus_service(PEGASUS_SERVICE_NAME);
 static HANDLE pegasus_service_event;
-static LPCSTR g_cimservice_key  = TEXT("SYSTEM\\CurrentControlSet\\Services\\cimserver");
+static LPCSTR g_cimservice_key  = TEXT("SYSTEM\\CurrentControlSet\\Services\\%s");
 static LPCSTR g_cimservice_home = TEXT("home");
 
 //-------------------------------------------------------------------------
@@ -265,13 +265,25 @@ Boolean isCIMServerRunning(void)
 //-------------------------------------------------------------------------
 // INSTALL
 //-------------------------------------------------------------------------
-bool cimserver_install_nt_service(void)
+bool cimserver_install_nt_service(char *service_name)
 {
   Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
-  char filename[_MAX_PATH];
+  char filename[_MAX_PATH] = {0};
+  char displayname[_MAX_PATH] = {0};
+
+  // If service name is specified, override default
+  if (service_name == NULL)
+    {
+      strcpy(displayname, PEGASUS_DISPLAY_NAME);
+    }
+  else
+    {
+      pegasus_service.SetServiceName(service_name);
+      sprintf(displayname, "%s - %s", PEGASUS_DISPLAY_NAME, service_name);
+    }
 
   GetModuleFileName(NULL, filename, sizeof(filename));
-  status = pegasus_service.Install(PEGASUS_DISPLAY_NAME, PEGASUS_DESCRIPTION, filename);
+  status = pegasus_service.Install(displayname, PEGASUS_DESCRIPTION, filename);
 
   // Upon success, set home in registry
   if (status == Service::SERVICE_RETURN_SUCCESS)
@@ -291,10 +303,16 @@ bool cimserver_install_nt_service(void)
 //-------------------------------------------------------------------------
 // REMOVE
 //-------------------------------------------------------------------------
-bool cimserver_remove_nt_service(void
+bool cimserver_remove_nt_service(char *service_name
 {
   Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
 
+  // If service name is specified, override default
+  if (service_name != NULL)
+    {
+      pegasus_service.SetServiceName(service_name);
+    }
+
   status = pegasus_service.Remove();
 
   return (status == Service::SERVICE_RETURN_SUCCESS) ? true : false;
@@ -303,10 +321,16 @@ bool cimserver_remove_nt_service(void)
 //-------------------------------------------------------------------------
 // START
 //-------------------------------------------------------------------------
-bool cimserver_start_nt_service(void
+bool cimserver_start_nt_service(char *service_name
 {
   Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
 
+  // If service name is specified, override default
+  if (service_name != NULL)
+    {
+      pegasus_service.SetServiceName(service_name);
+    }
+
   status = pegasus_service.Start(5);
 
   return (status == Service::SERVICE_RETURN_SUCCESS) ? true : false;
@@ -315,10 +339,16 @@ bool cimserver_start_nt_service(void)
 //-------------------------------------------------------------------------
 // STOP
 //-------------------------------------------------------------------------
-bool cimserver_stop_nt_service(void
+bool cimserver_stop_nt_service(char *service_name
 {
   Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
 
+  // If service name is specified, override default
+  if (service_name != NULL)
+    {
+      pegasus_service.SetServiceName(service_name);
+    }
+
   status = pegasus_service.Stop(5);
 
   return (status == Service::SERVICE_RETURN_SUCCESS) ? true : false;
@@ -331,9 +361,12 @@ static bool _getRegInfo(const char *lpchKeyword, char *lpchRetValue)
 {
   HKEY   hKey;
   DWORD  dw                   = _MAX_PATH;
+  char   subKey[_MAX_PATH]    = {0};
+  
+  sprintf(subKey, g_cimservice_key, pegasus_service.GetServiceName());
 
   if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE,
-                    g_cimservice_key, 
+                    subKey, 
                     0,
                     KEY_READ, 
                     &hKey)) != ERROR_SUCCESS)
@@ -362,12 +395,15 @@ static bool _setRegInfo(const char *lpchKeyword, const char *lpchValue)
   HKEY   hKey;
   DWORD  dw                   = _MAX_PATH;
   char   home_key[_MAX_PATH]  = {0};
+  char   subKey[_MAX_PATH]    = {0};
 
   if (lpchKeyword == NULL || lpchValue == NULL)
     return false;
 
- if ((RegCreateKeyEx (HKEY_LOCAL_MACHINE,
-                      g_cimservice_key,
+  sprintf(subKey, g_cimservice_key, pegasus_service.GetServiceName());
+
+  if ((RegCreateKeyEx (HKEY_LOCAL_MACHINE,
+                      subKey,
                       0,
                       NULL,
                       0,
index 9af8757071e261be0642a7b6f7e32438c3ecd7bf..33bed68f9bf3e82d1b8a8bc7440b4dfb41f19b38 100644 (file)
@@ -103,6 +103,14 @@ public:
   static ReturnCode Run(SERVICE_MAIN_T service_main, DWORD flags = 0);
   ReturnCode GetState(State *state);
   static bool LogEvent(WORD event_type, DWORD event_id, const char *string);
+  static void SetServiceName(char *service_name)
+    {
+      g_service_name = service_name;
+    }
+  static char *GetServiceName(void)
+    {
+      return g_service_name;
+    }
 
 private:
   static int                    g_argc;