(file) Return to cimserver_windows.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / WMIMapper / WMIServer

  1 karl  1.4 //%2003////////////////////////////////////////////////////////////////////////
  2 kumpf 1.1 //
  3 karl  1.4 // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development
  4           // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
  5           // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
  6           // IBM Corp.; EMC Corporation, The Open Group.
  7 kumpf 1.1 //
  8           // Permission is hereby granted, free of charge, to any person obtaining a copy
  9           // of this software and associated documentation files (the "Software"), to
 10           // deal in the Software without restriction, including without limitation the
 11           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 12           // sell copies of the Software, and to permit persons to whom the Software is
 13           // furnished to do so, subject to the following conditions:
 14           // 
 15           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 16           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 17           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 18           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 19           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 20           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 21           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 22           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23           //
 24           //==============================================================================
 25           //
 26           // Author: Mike Day (mdday@us.ibm.com)
 27           //
 28 kumpf 1.1 // Modified By: Mary Hinton (m.hinton@verizon.net)
 29           //              Sushma Fernandes (sushma_fernandes@hp.com)
 30 kumpf 1.3 //              Yi Zhou, Hewlett-Packard Company (yi_zhou@hp.com)
 31           //              Tony Fiorentino (fiorentino_tony@emc.com)
 32           //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 33           //              Jair Santos, Hewlett-Packard Company (jair.santos@hp.com)
 34 dj.gorey 1.5 //              Dan Gorey, IBM (djgorey@us.ibm.com)
 35 kumpf    1.1 //
 36              //%/////////////////////////////////////////////////////////////////////////////
 37              
 38              #include <windows.h>
 39              #include <process.h>    /* _beginthread, _endthread */
 40              #include <tchar.h>
 41              #include <direct.h>
 42              
 43 kumpf    1.3 #include "service.cpp"
 44              
 45 kumpf    1.1 PEGASUS_USING_PEGASUS;
 46              PEGASUS_USING_STD;
 47              
 48 kumpf    1.3 //-------------------------------------------------------------------------
 49              // DEFINES
 50              //-------------------------------------------------------------------------
 51              #define PEGASUS_SERVICE_NAME "wmiserver"
 52              #define PEGASUS_DISPLAY_NAME "Pegasus WMI Mapper"
 53              #define PEGASUS_DESCRIPTION  "Pegasus WBEM to WMI Mapper Manager Service"
 54              
 55              //-------------------------------------------------------------------------
 56              // GLOBALS
 57              //-------------------------------------------------------------------------
 58 kumpf    1.1 CIMServer *server_windows;
 59 kumpf    1.3 static Service pegasus_service(PEGASUS_SERVICE_NAME);
 60              static HANDLE pegasus_service_event;
 61              static LPCSTR g_cimservice_key  = TEXT("SYSTEM\\CurrentControlSet\\Services\\%s");
 62              static LPCSTR g_cimservice_home = TEXT("home");
 63              
 64              //-------------------------------------------------------------------------
 65              // PROTOTYPES
 66              //-------------------------------------------------------------------------
 67              int cimserver_windows_main(int flag, int argc, char **argv);
 68              extern void GetOptions(ConfigManager *cm,
 69                              int &argc,
 70                              char **argv,
 71                              const String &pegasusHome);
 72              static bool _getRegInfo(const char *lpchKeyword, char *lpchRetValue);
 73              static bool _setRegInfo(const char *lpchKeyword, const char *lpchValue);
 74              void setHome(String & home);
 75              
 76              //-------------------------------------------------------------------------
 77              // NO-OPs for windows platform
 78              //-------------------------------------------------------------------------
 79              int cimserver_fork(void) { return(0); }
 80 kumpf    1.3 int cimserver_kill(void) { return(0); }
 81              void notify_parent(int id) { return;    }
 82              
 83              //-------------------------------------------------------------------------
 84              // START MONITOR Asynchronously
 85              //-------------------------------------------------------------------------
 86              static void __cdecl cimserver_windows_thread(void *parm) 
 87              {
 88 kumpf    1.1 
 89 kumpf    1.3   // Get options (from command line and from configuration file); this
 90                // removes corresponding options and their arguments fromt he command
 91                // line.
 92              
 93                String pegasusHome;
 94 kumpf    1.2 
 95 kumpf    1.3   // Windows way to set home
 96                setHome(pegasusHome);
 97 kumpf    1.1 
 98 kumpf    1.3   ConfigManager::setPegasusHome(pegasusHome);
 99 kumpf    1.1 
100 kumpf    1.3   ConfigManager* configManager = ConfigManager::getInstance();
101                int dummy = 0;
102 kumpf    1.1 
103 kumpf    1.3   try
104 kumpf    1.1     {
105 kumpf    1.3       GetOptions(configManager, dummy, NULL, pegasusHome);
106 kumpf    1.1     }
107 kumpf    1.3   catch (Exception&)
108 kumpf    1.1     {
109 kumpf    1.3       exit(1);
110 kumpf    1.1     }
111              
112 kumpf    1.3   Boolean enableHttpConnection = String::equal(
113                  configManager->getCurrentValue("enableHttpConnection"), "true");
114                Boolean enableHttpsConnection = String::equal(
115                  configManager->getCurrentValue("enableHttpsConnection"), "true");
116              
117                if (!enableHttpConnection && !enableHttpsConnection)
118                {
119                  Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::WARNING,
120                    "Neither HTTP nor HTTPS connection is enabled.  "
121                    "CIMServer will not be started.");
122                  cerr << "Neither HTTP nor HTTPS connection is enabled.  "
123                    "CIMServer will not be started." << endl;
124                  exit(1);
125                }
126              
127                // Get the connection port configurations
128              
129                Uint32 portNumberHttps;
130                Uint32 portNumberHttp;
131              
132                if (enableHttpsConnection)
133 kumpf    1.3   {
134                  String httpsPort = configManager->getCurrentValue("httpsPort");
135                  CString portString = httpsPort.getCString();
136                  char* end = 0;
137                  Uint32 port = strtol(portString, &end, 10);
138                  assert(end != 0 && *end == '\0');
139              
140                  //
141                  // Look up the WBEM-HTTPS port number
142 kumpf    1.1     //
143 kumpf    1.3     portNumberHttps = System::lookupPort(WBEM_HTTPS_SERVICE_NAME, port);
144                }
145 kumpf    1.1 
146 kumpf    1.3   if (enableHttpConnection)
147                {
148                  String httpPort = configManager->getCurrentValue("httpPort");
149                  CString portString = httpPort.getCString();
150                  char* end = 0;
151                  Uint32 port = strtol(portString, &end, 10);
152                  assert(end != 0 && *end == '\0');
153 kumpf    1.1 
154                  //
155 kumpf    1.3     // Look up the WBEM-HTTP port number
156                  //
157                  portNumberHttp = System::lookupPort(WBEM_HTTP_SERVICE_NAME, port);
158                }
159 kumpf    1.1 
160 kumpf    1.3   // Set up the Logger
161                String logsDirectory = String::EMPTY;
162                logsDirectory = configManager->getCurrentValue("logdir");
163                logsDirectory = ConfigManager::getHomedPath(configManager->getCurrentValue("logdir"));
164              
165                Logger::setHomeDirectory(logsDirectory);
166              
167                // Put server start message to the logger
168                Logger::put(Logger::STANDARD_LOG, PEGASUS_SERVICE_NAME, Logger::INFORMATION,
169                            "Started $0 version $1.", PEGASUS_NAME, PEGASUS_VERSION);
170              
171                 // try loop to bind the address, and run the server
172                try
173                {
174 dj.gorey 1.5     #ifdef PEGASUS_USE_23HTTPMONITOR
175 kumpf    1.3     Monitor monitor(true);
176 dj.gorey 1.5     #else
177                  monitor_2 monitor();
178                  #endif
179 kumpf    1.3     
180                  CIMServer server(&monitor);
181                  server_windows = &server;
182 kumpf    1.2 
183 kumpf    1.3     if (enableHttpConnection)
184 kumpf    1.1     {
185 kumpf    1.3       server_windows->addAcceptor(false, portNumberHttp, false);
186                    Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::INFORMATION,
187                                "Listening on HTTP port $0.", portNumberHttp);
188 kumpf    1.1     }
189 kumpf    1.2     if (enableHttpsConnection)
190 kumpf    1.1     {
191 kumpf    1.3       server_windows->addAcceptor(false, portNumberHttps, true);
192                    Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::INFORMATION,
193                                "Listening on HTTPS port $0.", portNumberHttps);
194 kumpf    1.1     }
195 kumpf    1.2 
196 kumpf    1.3     server_windows->bind();
197              
198                  while(!server_windows->terminated())
199 kumpf    1.1     {
200 kumpf    1.3         server_windows->runForever();
201 kumpf    1.1     }
202 kumpf    1.3   }
203                catch(Exception& e)
204                {
205                  PEGASUS_STD(cerr) << "Error: " << e.getMessage() << PEGASUS_STD(endl);
206                }
207 kumpf    1.1 
208 kumpf    1.3   _endthreadex(NULL);
209 kumpf    1.1 }
210              
211              
212 kumpf    1.3 //-------------------------------------------------------------------------
213 kumpf    1.1 //  Windows NT Service Control Code 
214 kumpf    1.3 //-------------------------------------------------------------------------
215              
216              //-------------------------------------------------------------------------
217              // SERVICE (no parameters)
218              //-------------------------------------------------------------------------
219              void cim_server_service(int argc, char **argv)
220 kumpf    1.2 {
221 kumpf    1.3   Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
222                char console_title[_MAX_PATH] = {0};
223              
224                // Check if running from a console window
225                if (GetConsoleTitle(console_title, _MAX_PATH) > 0)
226                  return;
227              
228                pegasus_service_event = CreateEvent(NULL, FALSE, FALSE, NULL);
229              
230                // Run should exit the process if a service
231                status = pegasus_service.Run(cimserver_windows_main);
232 kumpf    1.1 
233 kumpf    1.3   // If we made it here there was a problem starting this process as a service
234                // Log the problem to the log file
235 kumpf    1.1 
236 kumpf    1.3   // TODO: log or echo something here
237 kumpf    1.1 }
238              
239 kumpf    1.3 //-------------------------------------------------------------------------
240              // START/STOP handler 
241              //-------------------------------------------------------------------------
242              int cimserver_windows_main(int flag, int argc, char *argv[])
243 kumpf    1.1 {
244 kumpf    1.3   switch (flag)
245                {
246                  case Service::STARTUP_FLAG:
247                    if (_beginthread(cimserver_windows_thread, 0, NULL))
248                      WaitForSingleObject(pegasus_service_event, INFINITE);
249                    break;
250              
251                  case Service::SHUTDOWN_FLAG:
252                    SetEvent(pegasus_service_event);
253                    break;
254              
255                  default:
256                    break;
257                }
258 kumpf    1.1 
259 kumpf    1.3   return 0;
260              }
261 kumpf    1.2 
262 kumpf    1.3 //-------------------------------------------------------------------------
263              // IS RUNNING?
264              //-------------------------------------------------------------------------
265              Boolean isCIMServerRunning(void)
266              {
267                Service::State state;
268                pegasus_service.GetState(&state);
269 kumpf    1.1 
270 kumpf    1.3   return (state == Service::SERVICE_STATE_RUNNING) ? true : false;
271              }
272 kumpf    1.1 
273 kumpf    1.3 //-------------------------------------------------------------------------
274              // INSTALL
275              //-------------------------------------------------------------------------
276              bool cimserver_install_nt_service(char *service_name)
277              {
278                Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
279                char filename[_MAX_PATH] = {0};
280                char displayname[_MAX_PATH] = {0};
281 kumpf    1.2 
282 kumpf    1.3   // If service name is specified, override default
283                if (service_name == NULL)
284                  {
285                    strcpy(displayname, PEGASUS_DISPLAY_NAME);
286                  }
287                else
288                  {
289                    pegasus_service.SetServiceName(service_name);
290                    sprintf(displayname, "%s - %s", PEGASUS_DISPLAY_NAME, service_name);
291                  }
292 kumpf    1.2 
293 kumpf    1.3   GetModuleFileName(NULL, filename, sizeof(filename));
294                status = pegasus_service.Install(displayname, PEGASUS_DESCRIPTION, filename);
295 kumpf    1.1 
296 kumpf    1.3   // Upon success, set home in registry
297                if (status == Service::SERVICE_RETURN_SUCCESS)
298 kumpf    1.1     {
299 kumpf    1.3       char pegasus_homepath[_MAX_PATH];
300                    System::extract_file_path(filename, pegasus_homepath);
301                    pegasus_homepath[strlen(pegasus_homepath)-1] = '\0';
302                    strcpy(filename, pegasus_homepath);
303                    System::extract_file_path(filename, pegasus_homepath);
304                    pegasus_homepath[strlen(pegasus_homepath)-1] = '\0';
305                    _setRegInfo(g_cimservice_home, pegasus_homepath);
306                  }
307              
308                return (status == Service::SERVICE_RETURN_SUCCESS) ? true : false;
309              }
310              
311              //-------------------------------------------------------------------------
312              // REMOVE
313              //-------------------------------------------------------------------------
314              bool cimserver_remove_nt_service(char *service_name) 
315              {
316                Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
317 kumpf    1.1 
318 kumpf    1.3   // If service name is specified, override default
319                if (service_name != NULL)
320                  {
321                    pegasus_service.SetServiceName(service_name);
322                  }
323 kumpf    1.1 
324 kumpf    1.3   status = pegasus_service.Remove();
325 kumpf    1.1 
326 kumpf    1.3   return (status == Service::SERVICE_RETURN_SUCCESS) ? true : false;
327 kumpf    1.1 }
328              
329 kumpf    1.3 //-------------------------------------------------------------------------
330              // START
331              //-------------------------------------------------------------------------
332              bool cimserver_start_nt_service(char *service_name) 
333 kumpf    1.1 {
334 kumpf    1.3   Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
335              
336                // If service name is specified, override default
337                if (service_name != NULL)
338                  {
339                    pegasus_service.SetServiceName(service_name);
340                  }
341              
342                status = pegasus_service.Start(5);
343 kumpf    1.1 
344 kumpf    1.3   return (status == Service::SERVICE_RETURN_SUCCESS) ? true : false;
345 kumpf    1.1 }
346              
347 kumpf    1.3 //-------------------------------------------------------------------------
348              // STOP
349              //-------------------------------------------------------------------------
350              bool cimserver_stop_nt_service(char *service_name) 
351 kumpf    1.1 {
352 kumpf    1.3   Service::ReturnCode status = Service::SERVICE_RETURN_SUCCESS;
353              
354                // If service name is specified, override default
355                if (service_name != NULL)
356                  {
357                    pegasus_service.SetServiceName(service_name);
358                  }
359              
360                status = pegasus_service.Stop(5);
361              
362                return (status == Service::SERVICE_RETURN_SUCCESS) ? true : false;
363 kumpf    1.1 }
364 kumpf    1.3 
365              //-------------------------------------------------------------------------
366              // HELPER Utilities
367              //-------------------------------------------------------------------------
368              static bool _getRegInfo(const char *lpchKeyword, char *lpchRetValue)
369 kumpf    1.2 {
370 kumpf    1.3   HKEY   hKey;
371                DWORD  dw                   = _MAX_PATH;
372                char   subKey[_MAX_PATH]    = {0};
373 kumpf    1.1   
374 kumpf    1.3   sprintf(subKey, g_cimservice_key, pegasus_service.GetServiceName());
375              
376                if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE,
377                                  subKey, 
378                                  0,
379                                  KEY_READ, 
380                                  &hKey)) != ERROR_SUCCESS)
381                  {
382                    return false;
383                  }
384              
385                if ((RegQueryValueEx(hKey, 
386                                     lpchKeyword, 
387                                     NULL, 
388                                     NULL, 
389                                     (LPBYTE)lpchRetValue,
390                                     &dw)) != ERROR_SUCCESS)
391                  {
392                    RegCloseKey(hKey);
393                    return false;
394                  }
395 kumpf    1.3 
396                RegCloseKey(hKey);
397              
398                return true;
399 kumpf    1.1 }
400              
401 kumpf    1.3 static bool _setRegInfo(const char *lpchKeyword, const char *lpchValue)
402 kumpf    1.1 {
403 kumpf    1.3   HKEY   hKey;
404                DWORD  dw                   = _MAX_PATH;
405                char   home_key[_MAX_PATH]  = {0};
406                char   subKey[_MAX_PATH]    = {0};
407              
408                if (lpchKeyword == NULL || lpchValue == NULL)
409                  return false;
410              
411                sprintf(subKey, g_cimservice_key, pegasus_service.GetServiceName());
412              
413                if ((RegCreateKeyEx (HKEY_LOCAL_MACHINE,
414                                    subKey,
415                                    0,
416                                    NULL,
417                                    0,
418                                    KEY_ALL_ACCESS,
419                                    NULL,
420                                    &hKey,
421                                    NULL) != ERROR_SUCCESS))
422                  {
423                    return false;
424 kumpf    1.3     }
425              
426                if ((RegSetValueEx(hKey, 
427                                   lpchKeyword, 
428                                   0, 
429                                   REG_SZ, 
430                                   (CONST BYTE *)lpchValue,
431                                   (DWORD)(strlen(lpchValue)+1))) != ERROR_SUCCESS)
432              	{
433              	  RegCloseKey(hKey);
434              	  return false;
435              	}
436 kumpf    1.1 
437 kumpf    1.3   RegCloseKey(hKey);
438 kumpf    1.2 
439 kumpf    1.3   return true;
440 kumpf    1.1 }
441 kumpf    1.3 
442              void setHome(String & home)
443              {
444                // Determine the absolute path to the running program
445                char exe_pathname[_MAX_PATH] = {0};
446                char home_pathname[_MAX_PATH] = {0};
447                GetModuleFileName(NULL, exe_pathname, sizeof(exe_pathname));
448              
449                // Pegasus home search rules:
450                // - look in registry (if set)
451                // - if not found, look in PEGASUS_HOME (if set)
452                // - if not found, use exe directory minus one level
453              
454                bool found_reg = _getRegInfo("home", home_pathname);
455                if (found_reg == true)
456                  {
457                    // Make sure home matches
458                    String current_home(home_pathname);
459                    String current_exe(exe_pathname);
460                    current_home.toLower();
461                    current_exe.toLower();
462 kumpf    1.3 
463                    Uint32 pos = current_exe.find(current_home);
464                    if (pos != PEG_NOT_FOUND)
465                      {
466                        home = home_pathname;
467                      }
468                    else
469                      {
470                        found_reg = false;
471                      }
472                  }
473                if (found_reg == false)
474                  {
475                    const char* tmp = getenv("PEGASUS_HOME");
476                    if (tmp)
477                      {
478                        home = tmp;
479                      }
480                    else
481                      {
482                        // ASSUMPTION: At a minimum, the cimserver program is running
483 kumpf    1.3           // from a "bin" directory
484                        home = FileSystem::extractFilePath(exe_pathname);
485                        home.remove(home.size()-1, 1);
486                        home = FileSystem::extractFilePath(home);
487                        home.remove(home.size()-1, 1);
488                      }
489                  }
490 karl     1.4 }
491              

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2