version 1.79, 2007/09/11 17:56:33
|
version 1.93, 2012/09/05 11:10:33
|
|
|
//%2006//////////////////////////////////////////////////////////////////////// |
//%LICENSE//////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development |
// Licensed to The Open Group (TOG) under one or more contributor license |
// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems. |
// agreements. Refer to the OpenPegasusNOTICE.txt file distributed with |
// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.; |
// this work for additional information regarding copyright ownership. |
// IBM Corp.; EMC Corporation, The Open Group. |
// Each contributor licenses this file to you under the OpenPegasus Open |
// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.; |
// Source License; you may not use this file except in compliance with the |
// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. |
// License. |
// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
// |
// EMC Corporation; VERITAS Software Corporation; The Open Group. |
// Permission is hereby granted, free of charge, to any person obtaining a |
// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
// copy of this software and associated documentation files (the "Software"), |
// EMC Corporation; Symantec Corporation; The Open Group. |
// to deal in the Software without restriction, including without limitation |
// |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
// and/or sell copies of the Software, and to permit persons to whom the |
// of this software and associated documentation files (the "Software"), to |
// Software is furnished to do so, subject to the following conditions: |
// deal in the Software without restriction, including without limitation the |
// |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
// The above copyright notice and this permission notice shall be included |
// sell copies of the Software, and to permit persons to whom the Software is |
// in all copies or substantial portions of the Software. |
// furnished to do so, subject to the following conditions: |
// |
// |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT |
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
// 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. |
|
// | // |
//============================================================================== |
////////////////////////////////////////////////////////////////////////// |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
|
|
microseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10); | microseconds = long((largeInt.QuadPart % (10000 * 1000)) / 10); |
} | } |
| |
|
Uint64 System::getCurrentTimeUsec() |
|
{ |
|
FILETIME ft; |
|
GetSystemTimeAsFileTime(&ft); |
|
ULARGE_INTEGER largeInt = { ft.dwLowDateTime, ft.dwHighDateTime }; |
|
largeInt.QuadPart -= 0x19db1ded53e8000; |
|
|
|
return Uint64(largeInt.QuadPart / 10); |
|
} |
|
|
String System::getCurrentASCIITime() | String System::getCurrentASCIITime() |
{ | { |
char tmpbuf[128]; | char tmpbuf[128]; |
|
|
| |
Boolean System::renameFile(const char* oldPath, const char* newPath) | Boolean System::renameFile(const char* oldPath, const char* newPath) |
{ | { |
return rename(oldPath, newPath) == 0; |
if (exists(oldPath)) |
} |
|
|
|
String System::getHostName() |
|
{ |
|
static String _hostname; |
|
if (0 == _hostname.size()) |
|
{ | { |
char hostname[PEGASUS_MAXHOSTNAMELEN + 1]; |
removeFile(newPath); |
//initialize the buffer to handle the case where gethostname fails. |
|
hostname[0] = 0; |
|
gethostname(hostname, sizeof(hostname)); |
|
hostname[sizeof(hostname)-1] = 0; |
|
_hostname.assign(hostname); |
|
} | } |
return _hostname; |
return rename(oldPath, newPath) == 0; |
} |
|
|
|
String System::getFullyQualifiedHostName () |
|
{ |
|
static char FQHostName[PEGASUS_MAXHOSTNAMELEN + 1]; |
|
|
|
if (!*FQHostName) |
|
{ |
|
String hostname = getHostName(); |
|
struct hostent* hostEnt; |
|
|
|
hostEnt = getHostByName((const char *)hostname.getCString()); |
|
if (hostEnt == NULL) |
|
{ |
|
return String::EMPTY; |
|
} |
|
strncpy(FQHostName, hostEnt->h_name, sizeof(FQHostName)-1); |
|
} |
|
|
|
return FQHostName; |
|
} | } |
| |
String System::getSystemCreationClassName () | String System::getSystemCreationClassName () |
|
|
// check if a given IP address is defined on the local network interfaces | // check if a given IP address is defined on the local network interfaces |
Boolean System::isIpOnNetworkInterface(Uint32 inIP) | Boolean System::isIpOnNetworkInterface(Uint32 inIP) |
{ | { |
|
Socket::initializeInterface(); |
SOCKET sock; | SOCKET sock; |
int interfaces = 0; | int interfaces = 0; |
int errcode; | int errcode; |
|
|
{ | { |
free(output_buf); | free(output_buf); |
closesocket(sock); | closesocket(sock); |
|
Socket::uninitializeInterface(); |
return true; | return true; |
} | } |
} | } |
|
|
else | else |
{ | { |
free(output_buf); | free(output_buf); |
|
Socket::uninitializeInterface(); |
return false; | return false; |
} | } |
free(output_buf); | free(output_buf); |
closesocket(sock); | closesocket(sock); |
|
Socket::uninitializeInterface(); |
} | } |
return false; | return false; |
} | } |
| |
|
void _getInterfaceAddrs(Array<String> &ips, int af) |
|
{ |
|
SOCKET sock; |
|
|
|
if (INVALID_SOCKET != (sock = WSASocket(af, SOCK_RAW, |
|
0, NULL, 0, 0))) |
|
{ |
|
DWORD bytesReturned; |
|
char buf[2048]; |
|
int interfaces = 0; |
|
char str[PEGASUS_INET6_ADDRSTR_LEN]; |
|
void *p = 0; |
|
if (0 == WSAIoctl(sock, SIO_ADDRESS_LIST_QUERY, NULL, 0, |
|
buf, 2048, &bytesReturned, NULL, |
|
NULL)) |
|
{ |
|
|
|
SOCKET_ADDRESS_LIST *addr_list; |
|
SOCKET_ADDRESS *addr; |
|
struct sockaddr *sin; |
|
addr_list = (SOCKET_ADDRESS_LIST *)buf; |
|
addr = addr_list->Address; |
|
int rc = 0; |
|
for (sin = (struct sockaddr *) addr->lpSockaddr ; |
|
interfaces < addr_list->iAddressCount; |
|
interfaces++) |
|
{ |
|
if (af == AF_INET) |
|
{ |
|
p = &((struct sockaddr_in*)sin)->sin_addr; |
|
} |
|
#ifdef PEGASUS_ENABLE_IPV6 |
|
else |
|
{ |
|
p = &((struct sockaddr_in6*)sin)->sin6_addr; |
|
} |
|
#endif |
|
// Don't gather loopback addrs |
|
if (!System::isLoopBack(af, p)) |
|
{ |
|
if (af == AF_INET) |
|
{ |
|
if ((rc = ::getnameinfo( |
|
sin, |
|
sizeof(struct sockaddr_in), |
|
str, |
|
sizeof(str), |
|
NULL, |
|
0, |
|
NI_NUMERICHOST)) == 0) |
|
{ |
|
ips.append(str); |
|
} |
|
//Error detected in getting name info, |
|
//display the error string |
|
else |
|
{ |
|
PEG_TRACE((TRC_OS_ABSTRACTION, Tracer::LEVEL1, |
|
"getnameinfo failed: %s", gai_strerror(rc))); |
|
} |
|
} |
|
#ifdef PEGASUS_ENABLE_IPV6 |
|
else if (af == AF_INET6) |
|
{ |
|
if((rc = System::getNameInfo( |
|
sin, |
|
sizeof(struct sockaddr_in6), |
|
str, |
|
sizeof(str), |
|
NULL, |
|
0, |
|
NI_NUMERICHOST)) == 0) |
|
{ |
|
ips.append(str); |
|
} |
|
} |
|
#endif |
|
} |
|
++addr; |
|
sin = (struct sockaddr*)addr->lpSockaddr; |
|
} |
|
} |
|
} |
|
} |
|
|
|
Array<String> System::getInterfaceAddrs() |
|
{ |
|
Socket::initializeInterface(); |
|
Array<String> ips; |
|
_getInterfaceAddrs(ips, AF_INET); |
|
#ifdef PEGASUS_ENABLE_IPV6 |
|
_getInterfaceAddrs(ips, AF_INET6); |
|
#endif |
|
Socket::uninitializeInterface(); |
|
return ips; |
|
} |
|
|
|
String System::getErrorMSG_NLS(int errorCode,int errorCode2) |
|
{ |
|
LPVOID winErrorMsg = NULL; |
|
|
|
if (FormatMessage( |
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|
FORMAT_MESSAGE_FROM_SYSTEM | |
|
FORMAT_MESSAGE_IGNORE_INSERTS, |
|
NULL, |
|
errorCode, |
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
|
(LPTSTR)&winErrorMsg, |
|
0, |
|
NULL)) |
|
{ |
|
MessageLoaderParms parms( |
|
"Common.System.ERROR_MESSAGE.STANDARD", |
|
"$0 (error code $1)",(char*)winErrorMsg,errorCode); |
|
LocalFree(winErrorMsg); |
|
return MessageLoader::getMessage(parms); |
|
} |
|
|
|
MessageLoaderParms parms( |
|
"Common.System.ERROR_MESSAGE.STANDARD", |
|
"$0 (error code $1)","",errorCode); |
|
return MessageLoader::getMessage(parms); |
|
|
|
} |
|
|
|
String System::getErrorMSG(int errorCode,int errorCode2) |
|
{ |
|
|
|
String buffer; |
|
LPVOID winErrorMsg = NULL; |
|
|
|
char strErrorCode[32]; |
|
sprintf(strErrorCode, "%d", errorCode); |
|
|
|
if (FormatMessage( |
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|
FORMAT_MESSAGE_FROM_SYSTEM | |
|
FORMAT_MESSAGE_IGNORE_INSERTS, |
|
NULL, |
|
errorCode, |
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
|
(LPTSTR)&winErrorMsg, |
|
0, |
|
NULL)) |
|
{ |
|
buffer.append((char*)winErrorMsg); |
|
LocalFree(winErrorMsg); |
|
} |
|
|
|
buffer.append(" (error code "); |
|
buffer.append(strErrorCode); |
|
buffer.append(")"); |
|
|
|
return buffer; |
|
} |
| |
| |
/////////////////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////////////////// |
|
|
&& (GetLastError() != ERROR_ALREADY_EXISTS) | && (GetLastError() != ERROR_ALREADY_EXISTS) |
&& (GetLastError() != ERROR_SUCCESS)) | && (GetLastError() != ERROR_SUCCESS)) |
{ | { |
PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL2, |
PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL1, |
"AutoFileLock: Failed to open lock file '%s', error code %d.", | "AutoFileLock: Failed to open lock file '%s', error code %d.", |
fileName, GetLastError())); | fileName, GetLastError())); |
return; | return; |
|
|
OVERLAPPED l={0,0,0,0,0}; | OVERLAPPED l={0,0,0,0,0}; |
if(LockFileEx(_hFile,LOCKFILE_EXCLUSIVE_LOCK, 0, 0, 0, &l) == 0) | if(LockFileEx(_hFile,LOCKFILE_EXCLUSIVE_LOCK, 0, 0, 0, &l) == 0) |
{ | { |
PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL2, |
PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL1, |
"AutoFileLock: Failed to Acquire lock on file %s, error code %d.", | "AutoFileLock: Failed to Acquire lock on file %s, error code %d.", |
fileName, GetLastError())); | fileName, GetLastError())); |
CloseHandle(_hFile); | CloseHandle(_hFile); |
|
|
} | } |
} | } |
| |
|
|
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |