📄 cimserver.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Notes on deamon operation (Unix) and service operation (Win 32)://// To run pegasus as a daemon on Unix platforms: //// cimserver//// To NOT run pegasus as a daemon on Unix platforms, set the daemon config// property to false://// cimserver daemon=false//// The daemon config property has no effect on windows operation. //// To shutdown pegasus, use the -s option:// // cimserver -s //// To run pegasus as an NT service, there are FOUR different possibilities://// To INSTALL the Pegasus service, //// cimserver -install//// To REMOVE the Pegasus service, //// cimserver -remove//// To START the Pegasus service, //// net start cimserver// or// cimserver -start//// To STOP the Pegasus service, //// net stop cimserver// or// cimserver -stop//// Alternatively, you can use the windows service manager. Pegasus shows up // in the service database as "Pegasus CIM Object Manager"//// Mike Day, mdday@us.ibm.com// //////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Config.h>#include <Pegasus/Common/Constants.h>#include <iostream>#include <Pegasus/Common/PegasusAssert.h>#include <cstdlib>#include <fstream>#include <Pegasus/Common/FileSystem.h>#include <Pegasus/Common/Monitor.h>#include <Pegasus/Common/PegasusVersion.h>#include <Pegasus/Common/Logger.h>#include <Pegasus/Common/System.h>#include <Pegasus/Common/Tracer.h>#include <Pegasus/Common/LanguageParser.h>#include <Pegasus/Config/ConfigManager.h>#include <Pegasus/Client/CIMClient.h>#include <Pegasus/Server/ShutdownService.h>#include <Pegasus/Server/CIMServer.h>#include <Service/ServerProcess.h>#if defined(PEGASUS_OS_OS400)# include "vfyptrs.cinc"# include "OS400ConvertChar.h"#endifPEGASUS_USING_PEGASUS;PEGASUS_USING_STD;#define PEGASUS_PROCESS_NAME "WMI Mapper";//Windows service variables are not defined elsewhere in the product//enable ability to override these#ifndef PEGASUS_SERVICE_NAME#define PEGASUS_SERVICE_NAME "Pegasus WMI Mapper";#endif#ifndef PEGASUS_SERVICE_DESCRIPTION#define PEGASUS_SERVICE_DESCRIPTION "Pegasus WMI Mapper Service";#endifclass CIMServerProcess : public ServerProcess{public: CIMServerProcess(void) { cimserver_set_process(this); } virtual ~CIMServerProcess(void) { } //defined in PegasusVersion.h virtual const char* getProductName() const { return PEGASUS_PRODUCT_NAME; } virtual const char* getExtendedName() const { return PEGASUS_SERVICE_NAME; } virtual const char* getDescription() const { return PEGASUS_SERVICE_DESCRIPTION; } //defined in PegasusVersion.h virtual const char* getVersion() const { return PEGASUS_PRODUCT_VERSION; } virtual const char* getProcessName() const { return PEGASUS_PROCESS_NAME; } //defined in Constants.h virtual const char* getPIDFileName() const { return PEGASUS_CIMSERVER_START_FILE; } int cimserver_run(int argc, char** argv, bool shutdownOption); void cimserver_stop(void);};AutoPtr<CIMServerProcess> _cimServerProcess(new CIMServerProcess());static CIMServer* _cimServer = 0;static Monitor* _monitor = 0;//// The command name.//static const char COMMAND_NAME [] = "cimserver";//// The constant defining usage string.//static const char USAGE [] = "Usage: ";/**Constants representing the command line options.*/static const char OPTION_VERSION = 'v';static const char OPTION_HELP = 'h';static const char OPTION_HOME = 'D';static const char OPTION_SHUTDOWN = 's';static const char LONG_HELP [] = "help";static const char LONG_VERSION [] = "version";#if defined(PEGASUS_OS_HPUX)static const char OPTION_BINDVERBOSE = 'X';#endifstatic const String PROPERTY_TIMEOUT = "shutdownTimeout";ConfigManager* configManager;/** GetOptions function - This function defines the Options Table and sets up the options from that table using the config manager.*/void GetOptions( ConfigManager* cm, int& argc, char** argv){ try { cm->mergeConfigFiles(); cm->mergeCommandLine(argc, argv); } catch (NoSuchFile&) { throw; } catch (FileNotReadable&) { throw; } catch (CannotRenameFile&) { throw; } catch (ConfigFileSyntaxError&) { throw; } catch(UnrecognizedConfigProperty&) { throw; } catch(InvalidPropertyValue&) { throw; } catch (CannotOpenFile&) { throw; }}/* PrintHelp - This is temporary until we expand the options manager to allow options help to be defined with the OptionRow entries and presented from those entries.*/void PrintHelp(const char* arg0){ String usage = String (USAGE); usage.append (COMMAND_NAME); usage.append (" [ [ options ] | [ configProperty=value, ... ] ]\n"); usage.append (" options\n"); usage.append (" -v, --version - displays CIM Server version number\n"); usage.append (" -h, --help - prints this help message\n"); usage.append (" -s - shuts down CIM Server\n");#if !defined(PEGASUS_USE_RELEASE_DIRS) usage.append (" -D [home] - sets pegasus home directory\n");#endif#if defined(PEGASUS_OS_TYPE_WINDOWS) usage.append (" -install [name] - installs pegasus as a Windows 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 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 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 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"); cout << endl; cout << _cimServerProcess->getProductName() << " " << _cimServerProcess->getVersion() << endl; cout << endl; #if defined(PEGASUS_OS_TYPE_WINDOWS) MessageLoaderParms parms("src.Server.cimserver.MENU.WINDOWS", usage);#elif defined(PEGASUS_USE_RELEASE_DIRS) MessageLoaderParms parms("src.Server.cimserver.MENU.HPUXLINUXIA64GNU", usage);#else MessageLoaderParms parms("src.Server.cimserver.MENU.STANDARD", usage);#endif cout << MessageLoader::getMessage(parms) << endl;}//This needs to be called at various points in the code depending on the platform and error conditions.//We need to delete the _cimServer reference on exit in order for the destructors to get called.void deleteCIMServer(){ if (_cimServer) { delete _cimServer; _cimServer = 0; } if (_monitor) { delete _monitor; }}// l10n//// Dummy function for the Thread object associated with the initial thread.// Since the initial thread is used to process CIM requests, this is// needed to localize the exceptions thrown during CIM request processing.// Note: This function should never be called! // ThreadReturnType PEGASUS_THREAD_CDECL dummyThreadFunc(void *parm){ return((ThreadReturnType)0); }void shutdownCIMOM(Uint32 timeoutValue){ // // Create CIMClient object // CIMClient client; // // Get local host name // String hostStr = System::getHostName(); // // open connection to CIMOM // try { client.connectLocal(); // // set client timeout to 2 seconds // client.setTimeout(2000); } catch(Exception&) {#ifdef PEGASUS_OS_OS400 //l10n //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, //"Unable to connect to CIM Server. CIM Server may not be running." ); Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, "src.Server.cimserver.UNABLE_CONNECT_SERVER_MAY_NOT_BE_RUNNING", "Unable to connect to CIM Server. CIM Server may not be running." ); // The server job may still be active but not responding. // Kill the job if it exists. if(_cimServerProcess->cimserver_kill(0) == -1) _cimServerProcess->cimserver_exitRC(2); _cimServerProcess->cimserver_exitRC(1);#else //l10n //PEGASUS_STD(cerr) << "Unable to connect to CIM Server." << PEGASUS_STD(endl); //PEGASUS_STD(cerr) << "CIM Server may not be running." << PEGASUS_STD(endl); MessageLoaderParms parms("src.Server.cimserver.UNABLE_CONNECT_SERVER_MAY_NOT_BE_RUNNING", "Unable to connect to CIM Server.\nCIM Server may not be running.\n"); PEGASUS_STD(cerr) << MessageLoader::getMessage(parms); exit(1);#endif } try { // // construct CIMObjectPath // String referenceStr = "//"; referenceStr.append(hostStr); referenceStr.append("/"); referenceStr.append(PEGASUS_NAMESPACENAME_SHUTDOWN.getString()); referenceStr.append(":"); referenceStr.append(PEGASUS_CLASSNAME_SHUTDOWN.getString()); CIMObjectPath reference(referenceStr); // // issue the invokeMethod request on the shutdown method // Array<CIMParamValue> inParams; Array<CIMParamValue> outParams; // set force option to true for now inParams.append(CIMParamValue("force", CIMValue(Boolean(true)))); inParams.append(CIMParamValue("timeout", CIMValue(Uint32(timeoutValue)))); CIMValue retValue = client.invokeMethod( PEGASUS_NAMESPACENAME_SHUTDOWN, reference, "shutdown", inParams, outParams); } catch(CIMException& e) {#ifdef PEGASUS_OS_OS400 if (e.getCode() == CIM_ERR_INVALID_NAMESPACE) { //l10n //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, //"Failed to shutdown server: $0", "The repository may be empty."); Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, "src.Server.cimserver.SHUTDOWN_FAILED_REPOSITORY_EMPTY", "Error in server shutdown: The repository may be empty."); } else { //l10n //Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, //"Failed to shutdown server: $0", e.getMessage()); Logger::put_l(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE, "src.Server.cimserver.SHUTDOWN_FAILED", "Error in server shutdown: $0", e.getMessage()); } // Kill the server job. if(_cimServerProcess->cimserver_kill(0) == -1) _cimServerProcess->cimserver_exitRC(2);#else //l10n - TODO MessageLoaderParms parms("src.Server.cimserver.SHUTDOWN_FAILED", "Error in server shutdown: "); PEGASUS_STD(cerr) << MessageLoader::getMessage(parms); if (e.getCode() == CIM_ERR_INVALID_NAMESPACE) { //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -