📄 systemposix.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.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////#if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)# define _OPEN_SYS_EXT# include <sys/ps.h># include <sys/__messag.h>#elif defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)# include <fcntl.h># include <qycmutilu2.H># include <unistd.cleinc># include "qycmmsgclsMessage.H" // ycmMessage class# include "OS400SystemState.h" // OS400LoadDynamicLibrary, etc# include "EBCDIC_OS400.h"#elif defined(PEGASUS_OS_VMS)# include <descrip.h> // $DESCRIPTOR# include <iodef.h> // IO$_SENSEMODE# include <ttdef.h> // TT$M_NOBRDCST# include <tt2def.h> // TT2$M_PASTHRU# include <starlet.h>#endif#include <unistd.h>#include <dirent.h>#include <pwd.h>#include <grp.h>#include <errno.h>#if defined(PEGASUS_OS_SOLARIS)# include <string.h>#endif#if !defined(PEGASUS_OS_VMS) && \ !defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) && \ !defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM) && \ !defined(PEGASUS_OS_DARWIN)# include <crypt.h>#endif#include "Network.h"#if defined(PEGASUS_USE_SYSLOGS)# include <syslog.h>#endif#include <sys/stat.h>#include <sys/types.h>#include <cstdio>#include <time.h>#include <sys/time.h>#include "System.h"#include <Pegasus/Common/Tracer.h>#include <Pegasus/Common/InternalException.h>#include <Pegasus/Common/Mutex.h>#if defined(PEGASUS_OS_LSB)# include <termios.h># include <stdio.h># include <stdlib.h>#endif#include <net/if.h>#include "Once.h"PEGASUS_NAMESPACE_BEGIN//==============================================================================//// QlgPath (PEGASUS_OS_OS400 only)////==============================================================================#ifdef PEGASUS_OS_OS400struct QlgPath{ QlgPath(const char* path); operator Qlg_Path_Name_T*() { return &qlg_struct; } Qlg_Path_Name_T qlg_struct; const char* pn;};QlgPath::QlgPath(const char* path) : pn(path){ memset((void*)&qlg_struct, 0, sizeof(Qlg_Path_Name_T)); qlg_struct.CCSID = 1208;#pragma convert(37) memcpy(qlg_struct.Country_ID,"US",2); memcpy(qlg_struct.Language_ID,"ENU",3);#pragma convert(0) qlg_struct.Path_Type = QLG_PTR_SINGLE; qlg_struct.Path_Length = strlen(path); qlg_struct.Path_Name_Delimiter[0] = '/';}#endif /* PEGASUS_OS_OS400 *///==============================================================================//// System////==============================================================================// System ID constants for Logger::put and Logger::trace#if defined(PEGASUS_PLATFORM_OS400_ISERIES_IBM)const String System::CIMSERVER = "qycmcimom"; // Server system ID#elif defined(PEGASUS_OS_ZOS)const String System::CIMSERVER = "CFZCIM"; // Server system ID#elseconst String System::CIMSERVER = "cimserver"; // Server system ID#endifvoid System::getCurrentTime(Uint32& seconds, Uint32& milliseconds){ timeval tv; gettimeofday(&tv, 0); seconds = Uint32(tv.tv_sec); milliseconds = Uint32(tv.tv_usec) / 1000;}void System::getCurrentTimeUsec(Uint32& seconds, Uint32& microseconds){ timeval tv; gettimeofday(&tv, 0); seconds = Uint32(tv.tv_sec); microseconds = Uint32(tv.tv_usec);}String System::getCurrentASCIITime(){ char str[50]; time_t rawTime; struct tm tmBuffer; time(&rawTime); strftime(str, 40,"%m/%d/%Y-%T", localtime_r(&rawTime, &tmBuffer)); return String(str);}static inline void _sleep_wrapper(Uint32 seconds){ sleep(seconds);}void System::sleep(Uint32 seconds){ _sleep_wrapper(seconds);}Boolean System::exists(const char* path){#if defined(PEGASUS_OS_OS400) return QlgAccess(QlgPath(path), F_OK) == 0;#else return access(path, F_OK) == 0;#endif}Boolean System::canRead(const char* path){#if defined(PEGASUS_OS_OS400) return QlgAccess(QlgPath(path), R_OK) == 0;#else return access(path, R_OK) == 0;#endif}Boolean System::canWrite(const char* path){#if defined(PEGASUS_OS_OS400) return QlgAccess(QlgPath(path), W_OK) == 0;#else return access(path, W_OK) == 0;#endif}Boolean System::getCurrentDirectory(char* path, Uint32 size){#if defined(PEGASUS_OS_OS400) return QlgGetcwd(QlgPath(path), size) == 0;#else return getcwd(path, size) != NULL;#endif}Boolean System::isDirectory(const char* path){ struct stat st;#if defined(PEGASUS_OS_OS400) if (QlgStat(QlgPath(path), &st) != 0) return false;#else if (stat(path, &st) != 0) return false;#endif return S_ISDIR(st.st_mode);}Boolean System::changeDirectory(const char* path){#if defined(PEGASUS_OS_OS400) return QlgChdir(QlgPath(path)) == 0;#else return chdir(path) == 0;#endif}Boolean System::makeDirectory(const char* path){#if defined(PEGASUS_OS_OS400) return QlgMkdir(QlgPath(path), 0777) == 0;#else return mkdir(path, 0777) == 0;#endif}Boolean System::getFileSize(const char* path, Uint32& size){ struct stat st;#if defined(PEGASUS_OS_OS400) if (QlgStat(QlgPath(path), &st) != 0) return false;#else if (stat(path, &st) != 0) return false;#endif size = st.st_size; return true;}Boolean System::removeDirectory(const char* path){#if defined(PEGASUS_OS_OS400) return QlgRmdir(QlgPath(path)) == 0;#else return rmdir(path) == 0;#endif}Boolean System::removeFile(const char* path){#if defined(PEGASUS_OS_OS400) return QlgUnlink(QlgPath(path)) == 0;#else return unlink(path) == 0;#endif}Boolean System::renameFile(const char* oldPath, const char* newPath){#if defined(PEGASUS_OS_OS400) if (QlgLink(QlgPath(oldPath), QlgPath(newPath)) != 0) return false; return QlgUnlink(QlgPath(oldPath)) == 0;#elif defined(PEGASUS_OS_VMS)// Note: link() on OpenVMS has a different meaning so rename is used.// unlink() is a synonym for remove() so it can be used. if (rename(oldPath, newPath) != 0) return false; return true;#else if (link(oldPath, newPath) != 0) return false; return unlink(oldPath) == 0;#endif}String System::getHostName(){ static char _hostname[PEGASUS_MAXHOSTNAMELEN + 1]; static MutexType _mutex = PEGASUS_MUTEX_INITIALIZER; // Use double-checked locking pattern to avoid overhead of // mutex on subsequenct calls. if (_hostname[0] == '\0') { mutex_lock(&_mutex); if (_hostname[0] == '\0') { gethostname(_hostname, sizeof(_hostname)); _hostname[sizeof(_hostname)-1] = 0;#if defined(PEGASUS_OS_OS400) EtoA(_hostname);#endif } mutex_unlock(&_mutex); } return _hostname;}static int _getHostByName( const char* hostName, char* hostNameOut, size_t hostNameOutSize){ struct hostent *hostEntry;#if defined(PEGASUS_OS_LINUX) char hostEntryBuffer[8192]; struct hostent hostEntryStruct; int hostEntryErrno; gethostbyname_r( hostName, &hostEntryStruct, hostEntryBuffer, sizeof(hostEntryBuffer), &hostEntry, &hostEntryErrno);#elif defined(PEGASUS_OS_SOLARIS) char hostEntryBuffer[8192]; struct hostent hostEntryStruct; int hostEntryErrno; hostEntry = gethostbyname_r( hostName, &hostEntryStruct, hostEntryBuffer, sizeof(hostEntryBuffer), &hostEntryErrno);#else /* default */ hostEntry = gethostbyname(hostName);#endif if (hostEntry) { strncpy(hostNameOut, hostEntry->h_name, hostNameOutSize - 1); return 0; } return -1;}String System::getFullyQualifiedHostName (){#if defined(PEGASUS_OS_ZOS) char hostName[PEGASUS_MAXHOSTNAMELEN + 1]; String fqName; struct addrinfo *resolv; struct addrinfo hint; struct hostent *he; // receive short name of the local host if (gethostname(hostName, PEGASUS_MAXHOSTNAMELEN) != 0) { return String::EMPTY; } resolv = new struct addrinfo; hint.ai_flags = AI_CANONNAME; hint.ai_family = AF_UNSPEC; // any family hint.ai_socktype = 0; // any socket type hint.ai_protocol = 0; // any protocol int success = getaddrinfo(hostName, NULL, &hint, &resolv); if (success==0) { // assign fully qualified hostname fqName.assign(resolv->ai_canonname); } else { if ((he = gethostbyname(hostName))) { strcpy (hostName, he->h_name); } // assign hostName // if gethostbyname was successful assign that result // else assign unqualified hostname fqName.assign(hostName); } freeaddrinfo(resolv); delete resolv; return fqName;#else /* !PEGASUS_OS_ZOS */ char hostName[PEGASUS_MAXHOSTNAMELEN + 1]; if (gethostname(hostName, sizeof(hostName)) != 0) return String::EMPTY; hostName[sizeof(hostName)-1] = 0; _getHostByName(hostName, hostName, sizeof(hostName));# if defined(PEGASUS_OS_OS400) EtoA(hostName);# endif return String(hostName);#endif /* !PEGASUS_OS_ZOS */}String System::getSystemCreationClassName (){ // // The value returned should match the value of the CreationClassName key // property used in the instrumentation of the CIM_ComputerSystem class // as determined by the provider for the CIM_ComputerSystem class // return "CIM_ComputerSystem";}Uint32 System::lookupPort( const char * serviceName, Uint32 defaultPort){ Uint32 localPort; struct servent *serv; // // Get wbem-local port from /etc/services //#if defined(PEGASUS_OS_SOLARIS)# define SERV_BUFF_SIZE 1024 struct servent serv_result; char buf[SERV_BUFF_SIZE]; if ( (serv = getservbyname_r(serviceName, TCP, &serv_result, buf, SERV_BUFF_SIZE)) != NULL )#elif defined(PEGASUS_OS_OS400) struct servent serv_result; serv = &serv_result; struct servent_data buf; memset(&buf, 0x00, sizeof(struct servent_data)); char srvnameEbcdic[256]; strcpy(srvnameEbcdic, serviceName); AtoE(srvnameEbcdic); char tcpEbcdic[64]; strcpy(tcpEbcdic, TCP); AtoE(tcpEbcdic); if ( (getservbyname_r(srvnameEbcdic, tcpEbcdic, &serv_result, &buf)) == 0 )#else // PEGASUS_OS_SOLARIS if ( (serv = getservbyname(serviceName, TCP)) != NULL )#endif // PEGASUS_OS_SOLARIS { localPort = htons((uint16_t)serv->s_port); } else { localPort = defaultPort; } return localPort;}#if defined(PEGASUS_OS_LSB)/* getpass equivalent. Adapted from example implementation described in GLIBC documentation (http://www.dusek.ch/manual/glibc/libc_32.html) and "Advanced Programming in the UNIX Environment" by Richard Stevens, pg. 350.*/char *getpassword(const char *prompt){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -