version 1.13, 2001/06/16 17:30:38
|
version 1.30, 2002/03/21 18:49:58
|
|
|
// | // |
// Author: Mike Brasher (mbrasher@bmc.com) | // Author: Mike Brasher (mbrasher@bmc.com) |
// | // |
// Modified By: |
// Modified By: Ben Heilbronn (ben_heilbronn@hp.com) |
|
// |
|
// Sushma Fernandes (sushma_fernandes@hp.com) |
|
// |
|
// Nag Boranna (nagaraja_boranna@hp.com) |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
#ifdef PEGASUS_OS_HPUX | #ifdef PEGASUS_OS_HPUX |
# include <dl.h> | # include <dl.h> |
|
#elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM) |
|
# include <dll.h> |
#else | #else |
# include <dlfcn.h> | # include <dlfcn.h> |
#endif | #endif |
| |
# include <unistd.h> | # include <unistd.h> |
#include <dirent.h> | #include <dirent.h> |
|
#include <pwd.h> |
|
|
|
#ifndef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM |
|
#include <crypt.h> |
|
#else |
|
#include <unistd.h> |
|
#endif |
|
|
#include "System.h" | #include "System.h" |
#include <sys/stat.h> | #include <sys/stat.h> |
#include <sys/types.h> | #include <sys/types.h> |
#include <cstdio> | #include <cstdio> |
#include <time.h> | #include <time.h> |
|
#include <netdb.h> |
|
#include <Pegasus/Common/Tracer.h> |
|
#include <Pegasus/Common/Destroyer.h> |
|
#include <Pegasus/Common/Exception.h> |
|
|
|
#ifdef PEGASUS_PLATFORM_LINUX_IX86_GNU |
|
#include <pwd.h> |
|
#endif |
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
|
|
| |
DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName) | DynamicLibraryHandle System::loadDynamicLibrary(const char* fileName) |
{ | { |
#ifdef PEGASUS_OS_HPUX |
const char METHOD_NAME[] = "System::loadDynamicLibrary()"; |
char* p = strcpy(new char[strlen(fileName) + 4], fileName); |
|
char* dot = strrchr(p, '.'); |
|
| |
if (!dot) |
PEG_FUNC_ENTER(TRC_OS_ABSTRACTION, METHOD_NAME); |
return 0; |
|
| |
*dot = '\0'; |
Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2, |
strcat(p, ".sl"); |
"Attempting to load library %s", fileName); |
| |
void* handle = shl_load(p, BIND_IMMEDIATE | DYNAMIC_PATH, 0L); |
#if defined(PEGASUS_OS_HPUX) |
delete [] p; |
void* handle = shl_load(fileName, BIND_IMMEDIATE | DYNAMIC_PATH, 0L); |
|
|
|
Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL2, |
|
"After loading lib %s, error code is %d", fileName, errno); |
| |
|
PEG_FUNC_EXIT(TRC_OS_ABSTRACTION, METHOD_NAME); |
return DynamicLibraryHandle(handle); | return DynamicLibraryHandle(handle); |
|
#elif defined(PEGASUS_OS_TRU64) |
|
PEG_FUNC_EXIT(TRC_OS_ABSTRACTION, METHOD_NAME); |
|
return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW)); |
|
#elif defined(PEGASUS_OS_ZOS) |
|
PEG_FUNC_EXIT(TRC_OS_ABSTRACTION, METHOD_NAME); |
|
return DynamicLibraryHandle(dllload(fileName)); |
#else | #else |
|
PEG_FUNC_EXIT(TRC_OS_ABSTRACTION, METHOD_NAME); |
return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW | RTLD_GLOBAL)); | return DynamicLibraryHandle(dlopen(fileName, RTLD_NOW | RTLD_GLOBAL)); |
#endif | #endif |
|
|
|
} |
|
|
|
void System::unloadDynamicLibrary(DynamicLibraryHandle libraryHandle) |
|
{ |
|
// ATTN: Should this method indicate success/failure? |
|
#ifdef PEGASUS_OS_LINUX |
|
dlclose(libraryHandle); |
|
#endif |
|
|
|
#ifdef PEGASUS_OS_HPUX |
|
// Note: shl_unload will unload the library even if it has been loaded |
|
// multiple times. No reference count is kept. |
|
int ignored = shl_unload(shl_t(libraryHandle)); |
|
#endif |
|
} |
|
|
|
String System::dynamicLoadError() { |
|
// ATTN: Is this safe in a multi-threaded process? Should this string |
|
// be returned from loadDynamicLibrary? |
|
#ifdef PEGASUS_OS_HPUX |
|
// ATTN: If shl_load() returns NULL, this value should be strerror(errno) |
|
return String(); |
|
#elif defined(PEGASUS_OS_ZOS) |
|
return String(); |
|
#else |
|
String dlerr = dlerror(); |
|
return dlerr; |
|
#endif |
} | } |
| |
|
|
DynamicSymbolHandle System::loadDynamicSymbol( | DynamicSymbolHandle System::loadDynamicSymbol( |
DynamicLibraryHandle libraryHandle, | DynamicLibraryHandle libraryHandle, |
const char* symbolName) | const char* symbolName) |
|
|
| |
return 0; | return 0; |
| |
|
#elif defined(PEGASUS_OS_ZOS) |
|
return DynamicSymbolHandle(dllqueryfn((dllhandle *)libraryHandle, |
|
(char*)symbolName)); |
#else | #else |
| |
return DynamicSymbolHandle(dlsym(libraryHandle, (char*)symbolName)); | return DynamicSymbolHandle(dlsym(libraryHandle, (char*)symbolName)); |
|
|
#endif | #endif |
} | } |
| |
|
String System::getHostName() |
|
{ |
|
static char hostname[64]; |
|
|
|
if (!*hostname) |
|
gethostname(hostname, sizeof(hostname)); |
|
|
|
return hostname; |
|
} |
|
|
|
String System::getFullyQualifiedHostName () |
|
{ |
|
#ifdef PEGASUS_OS_HPUX |
|
char hostName [MAXHOSTNAMELEN]; |
|
struct hostent *he; |
|
String fqName; |
|
|
|
if (gethostname (hostName, MAXHOSTNAMELEN) != 0) |
|
{ |
|
return String::EMPTY; |
|
} |
|
|
|
if (he = gethostbyname (hostName)) |
|
{ |
|
strcpy (hostName, he->h_name); |
|
} |
|
|
|
fqName.assign (hostName); |
|
|
|
return fqName; |
|
#else |
|
// |
|
// ATTN: Implement this method to return the fully qualified host name |
|
// |
|
return String::EMPTY; |
|
#endif |
|
} |
|
|
|
String System::getSystemCreationClassName () |
|
{ |
|
#ifdef PEGASUS_OS_HPUX |
|
return "CIM_ComputerSystem"; |
|
#else |
|
// |
|
// ATTN: Implement this method to return the system creation class name |
|
// |
|
return String::EMPTY; |
|
#endif |
|
} |
|
|
|
Uint32 System::lookupPort( |
|
const char * serviceName, |
|
Uint32 defaultPort) |
|
{ |
|
Uint32 localPort; |
|
|
|
struct servent *serv; |
|
|
|
// |
|
// Get wbem-local port from /etc/services |
|
// |
|
if ( (serv = getservbyname(serviceName, TCP)) != NULL ) |
|
{ |
|
localPort = serv->s_port; |
|
} |
|
else |
|
{ |
|
localPort = defaultPort; |
|
} |
|
|
|
return localPort; |
|
} |
|
|
|
String System::getPassword(const char* prompt) |
|
{ |
|
|
|
String password; |
|
|
|
password = String(getpass( prompt )); |
|
|
|
return password; |
|
} |
|
|
|
String System::getCurrentLoginName() |
|
{ |
|
String userName = String::EMPTY; |
|
struct passwd* pwd = NULL; |
|
|
|
// |
|
// get the currently logged in user's UID. |
|
// |
|
pwd = getpwuid(getuid()); |
|
if ( pwd == NULL ) |
|
{ |
|
//ATTN: Log a message |
|
// "User might have been removed just after login" |
|
} |
|
else |
|
{ |
|
// |
|
// get the user name |
|
// |
|
userName.assign(pwd->pw_name); |
|
} |
|
|
|
return(userName); |
|
} |
|
|
|
String System::encryptPassword(const char* password, const char* salt) |
|
{ |
|
return ( String(crypt( password,salt)) ); |
|
} |
|
|
|
Boolean System::isSystemUser(char* userName) |
|
{ |
|
// |
|
// get the password entry for the user |
|
// |
|
if ( getpwnam(userName) == NULL ) |
|
{ |
|
return false; |
|
} |
|
return true; |
|
} |
|
|
|
Boolean System::isPrivilegedUser(const String userName) |
|
{ |
|
// |
|
// Check if username has been passed |
|
// |
|
if ( userName != String::EMPTY ) |
|
{ |
|
// |
|
// Check if the given user is a privileged user |
|
// |
|
struct passwd pwd; |
|
struct passwd *result; |
|
char pwdBuffer[1024]; |
|
|
|
ArrayDestroyer<char> userName_(userName.allocateCString()); |
|
if (getpwnam_r(userName_.getPointer(), &pwd, pwdBuffer, 1024, &result) == 0) |
|
{ |
|
if ( pwd.pw_uid == 0 ) |
|
{ |
|
return true; |
|
} |
|
return false; |
|
} |
|
} |
|
else |
|
{ |
|
// |
|
// Get the effective UID for the user |
|
// |
|
if ( geteuid() != 0 ) |
|
{ |
|
return false; |
|
} |
|
return true; |
|
} |
|
#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM |
|
return false; // keep the compiler happy |
|
#endif |
|
} |
|
|
|
String System::getPrivilegedUserName() |
|
{ |
|
static String userName = String::EMPTY; |
|
|
|
if (userName == String::EMPTY) |
|
{ |
|
struct passwd* pwd = NULL; |
|
|
|
// |
|
// get the privileged user's UID. |
|
// |
|
pwd = getpwuid(0); |
|
if ( pwd != NULL ) |
|
{ |
|
// |
|
// get the user name |
|
// |
|
userName.assign(pwd->pw_name); |
|
} |
|
else |
|
{ |
|
PEGASUS_ASSERT(0); |
|
} |
|
} |
|
|
|
return (userName); |
|
} |
|
|
|
Uint32 System::getPID() |
|
{ |
|
// |
|
// Get the Process ID |
|
// |
|
Uint32 pid = getpid(); |
|
|
|
return pid; |
|
} |
|
|
|
Boolean System::truncateFile( |
|
const char* path, |
|
size_t newSize) |
|
{ |
|
return (truncate(path, newSize) == 0); |
|
} |
|
|
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |