bool SCXFile::ReadAvailableBytesAsUnsigned(const SCXFilePath& , unsigned char* , size_t , size_t /*= 0*/) { throw SCXNotSupportedException(L"Reads not supported on windows", SCXSRCLOCATION); #else bool SCXFile::ReadAvailableBytesAsUnsigned(const SCXFilePath& path, unsigned char* buf, size_t size, size_t offset /*= 0*/) { int fd = open(SCXFileSystem::EncodePath(path).c_str(),O_RDONLY); if (-1 == fd) { throw SCXErrnoException(L"open(" + path.Get() + L")", errno, SCXSRCLOCATION); } size_t remainder; void *pDevice = NULL; #ifdef _SC_PAGESIZE //!< Size of a page in bytes remainder = offset% sysconf(_SC_PAGESIZE); #else remainder = offset% getpagesize(); #endif // //For API mmap, the sixth parameter mmapoffset must be a multiple of pagesize. // size_t mmapoffset = offset - remainder; pDevice = mmap(NULL, remainder + size, PROT_READ, MAP_SHARED, fd, mmapoffset); if (MAP_FAILED == pDevice) { return false; } memcpy(buf, reinterpret_cast<unsigned char*>(pDevice) + remainder, size); munmap(reinterpret_cast<char*>(pDevice), remainder + size); close(fd); return true; #endif }
/** Get software version Minor Parameters: versionMinor - RETURN: version Minor of software instance Retval: true if a value is supported by this platform Throw: SCXNotSupportedException - For not implemented platform */ bool InstalledSoftwareInstance::GetVersionMinor(unsigned int& versionMinor) const { bool fRet = false; #if defined(sun) || defined(linux) || defined(hpux) versionMinor = m_versionMinor; fRet = true; #else throw SCXNotSupportedException(L"Minor Version", SCXSRCLOCATION); #endif return fRet; }
/** Get software product Version Parameters: productVersion - RETURN: product Version of software instance Retval: true if a value is supported by this platform Throw: SCXNotSupportedException - For not implemented platform */ bool InstalledSoftwareInstance::GetProductVersion(wstring& productVersion) const { bool fRet = false; #if defined(sun) || defined(linux) || defined(aix) || defined(hpux) productVersion = m_productVersion; fRet = true; #else throw SCXNotSupportedException(L"Product Version", SCXSRCLOCATION); #endif return fRet; }
/** Get software evidence source Parameters: evidenceSource - RETURN: evidence source of software instance Retval: true if a value is supported by this platform Throw: SCXNotSupportedException - For not implemented platform */ bool InstalledSoftwareInstance::GetEvidenceSource(wstring& evidenceSource) const { bool fRet = false; #if defined(sun) || defined(linux) || defined(aix) evidenceSource = m_evidenceSource; fRet = true; #else throw SCXNotSupportedException(L"Evidence Source", SCXSRCLOCATION); #endif return fRet; }
/** Get software publisher Parameters: publisher - RETURN: publisher of software instance Retval: true if a value is supported by this platform Throw: SCXNotSupportedException - For not implemented platform */ bool InstalledSoftwareInstance::GetPublisher(wstring& publisher) const { bool fRet = false; #if defined(sun) || defined(linux) || defined(hpux) publisher = m_publisher; fRet = true; #elif defined(aix) fRet = false; #else throw SCXNotSupportedException(L"Publisher", SCXSRCLOCATION); #endif return fRet; }
/** Get software install Date Parameters:`installDate - RETURN: install Date of software instance Retval: true if a value is supported by this platform Throw: SCXNotSupportedException - For not implemented platform */ bool InstalledSoftwareInstance::GetInstallDate(SCXCalendarTime& installDate) const { bool fRet = false; #if defined(sun) || defined(linux) || defined(aix) || defined(hpux) if (m_installDate.IsInitialized()) { installDate = m_installDate; fRet = true; } #else throw SCXNotSupportedException(L"Install Date", SCXSRCLOCATION); #endif return fRet; }
/** Get software InstalledLocation Parameters: installedLocation - RETURN: installed Location of software instance Retval: true if a value is supported by this platform Throw: SCXNotSupportedException - For not implemented platform */ bool InstalledSoftwareInstance::GetInstalledLocation(wstring& installedLocation) const { bool fRet = false; #if defined(sun) || defined(hpux) installedLocation = m_installedLocation; fRet = true; #elif defined(linux) || defined(aix) installedLocation = installedLocation; // Silence warning of unused arg. // Implemented platform,and the property can't be supported. fRet = false; #else throw SCXNotSupportedException(L"Installed Location", SCXSRCLOCATION); #endif return fRet; }
/** Get Display Name Parameters: displayName - RETURN: display Name of software instance Retval: true if a value is supported by this platform Throw: SCXNotSupportedException - For not implemented platform */ bool InstalledSoftwareInstance::GetDisplayName(wstring& displayName) const { bool fRet = false; #if defined(sun) || defined(linux) || defined(aix) || defined(hpux) // Set property here, otherwise building process would be failed in AIX & HPUX. displayName = m_displayName; // Implemented platform,and the property can be supported. fRet = true; #else // Not implemented platform throw SCXNotSupportedException(L"Display Name", SCXSRCLOCATION); #endif return fRet; }
size_t SCXFile::ReadAvailableBytes(const SCXFilePath& , char* , size_t , size_t /*= 0*/) { throw SCXNotSupportedException(L"non-blocking reads not supported on windows", SCXSRCLOCATION); #else size_t SCXFile::ReadAvailableBytes(const SCXFilePath& path, char* buf, size_t size, size_t offset /*= 0*/) { int fd = open(SCXFileSystem::EncodePath(path).c_str(), 0, O_RDONLY); if (-1 == fd) { throw SCXErrnoException(L"open(" + path.Get() + L")", errno, SCXSRCLOCATION); } int fd_flags = fcntl(fd, F_GETFL); if (fd_flags < 0) { close(fd); throw SCXErrnoException(L"fcntl(F_GETFL, " + path.Get() + L")", errno, SCXSRCLOCATION); } if (-1 == fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK)) { close(fd); throw SCXErrnoException(L"fcntl(F_SETFL, O_NONBLOCK, " + path.Get() + L")", errno, SCXSRCLOCATION); } if (-1 == lseek(fd, offset, SEEK_SET)) { close(fd); if (ESPIPE == errno) { return 0; } throw SCXErrnoException(L"lseek(" + path.Get() + L")", errno, SCXSRCLOCATION); } size_t r = 0; while (r < size) { // Using a loop to read required on solaris. size_t t = read(fd, buf+r, size-r); if (static_cast<size_t>(-1) == t) { if (EAGAIN == errno) { break; } close(fd); throw SCXErrnoException(L"read(" + path.Get() + L")", errno, SCXSRCLOCATION); } r += t; if (0 == t) { break; } } close(fd); return r; #endif }
/** Opens a file stream assuming the file is encoded according to the system locale \param[in] file The file to open \param[in] mode How to open it, explicitly. \throws SCXFilePathNotFoundException \throws SCXUnauthorizedFileSystemAccessException \throws SCXNotSupportedException \throws InvalidArgumentException Arguments Unlike STL there is no implicit (default) mode, the requested mode has to explicitly stated. The content of the file is assumed to be encoded according to system default. */ SCXHandle<std::wfstream> SCXFile::OpenWFstream(const SCXFilePath& file, std::ios_base::openmode mode) { if (!(mode & std::ios::in) && !(mode & std::ios::out)) { throw SCXInvalidArgumentException(L"mode", L"Specify ios::in or ios::out, or both", SCXSRCLOCATION); } if (mode & std::ios::binary) { throw SCXNotSupportedException(L"wide streams must not be binary", SCXSRCLOCATION); } #if defined(WIN32) SCXHandle<std::wfstream> streamPtr(new std::wfstream(file.Get().c_str(), mode)); #elif defined(SCX_UNIX) SCXHandle<std::wfstream> streamPtr(new std::wfstream(SCXFileSystem::EncodePath(file).c_str(), mode)); #else #error #endif if (streamPtr->good()) { SCXFileSystem::Attributes attribs(SCXFileSystem::GetAttributes(file)); if (attribs.count(SCXFileSystem::eDirectory) > 0) { throw SCXUnauthorizedFileSystemAccessException(file, attribs, SCXSRCLOCATION); } } else { SCXFileInfo info(file); if (mode & std::ios::in) { if (!info.PathExists()) { throw SCXFilePathNotFoundException(file, SCXSRCLOCATION); } else { throw SCXUnauthorizedFileSystemAccessException(file, SCXFileSystem::GetAttributes(file), SCXSRCLOCATION); } } else if (mode & std::ios::out) { throw SCXUnauthorizedFileSystemAccessException(file, SCXFileSystem::GetAttributes(file), SCXSRCLOCATION); } else { SCXASSERT(!"Invalid mode"); } } return streamPtr; }
/** Get DHCPEnabled status from the platform's config file \param configData - contents of the DHCP config file \param interface - the interface for which we want DHCPEnabled status */ bool NetworkInterfaceConfigurationEnumeration::GetDHCPEnabledFromConfigData ( std::vector<wstring>configData, wstring interface ) { if (interface.size() + configData.size() == 0) { return false; } #if defined(PF_DISTRO_REDHAT) || defined(PF_DISTRO_ULINUX) || defined(PF_DISTRO_SUSE) /* Typical file DEVICE="eth0" # This is also in the file name BOOTPROTO="dhcp" HWADDR="00:21:5E:DB:AC:98" ONBOOT="yes" */ bool result = false; SCXRegex bootproto(L"BOOTPROTO.*dhcp"); for (uint i = 0; i < configData.size(); i++) { wstring line = configData[i]; if (bootproto.IsMatch(configData[i])) { result = true; } } return result; #elif defined(sun) return configData.size() > 0; // If the file exists and has data that means DHCP is enabled #elif defined(hpux) // We are looking for a line // // DHCP_ENABLED[<i>]="<1|0>" // // where 1 means enabled and <i> corresponds to a line of the form // // INTERFACE_NAME[<i>]="<interface name>" // // and <interface name> matches the member variable of NetworkInterfaceConfigurationInstance instance // // Example file where we are looking for network interface "lan0" /* INTERFACE_NAME[0]=lan0 #<---------- for interface "lan0" this means we are looking for index 0 IP_ADDRESS[0]=10.217.5.127 SUBNET_MASK[0]=255.255.254.0 BROADCAST_ADDRESS[0]="" INTERFACE_STATE[0]="" DHCP_ENABLE[0]=1 #<----------- this is the DHCP_ENABLED line we are therefore interested in INTERFACE_MODULES[0]="" INTERFACE_NAME[1]=lan666 IP_ADDRESS[1]=55.5.12.12 DHCP_ENABLE[1]=0 */ bool result = false; uint idx = numeric_limits<unsigned int>::max(); vector<wstring> tokens; for (uint i = 0; i < configData.size(); i++) { wstring line = configData[i]; StrTokenize(line, tokens, L"[]="); if (tokens.size() < 1) { continue; } if (tokens[0].compare(L"INTERFACE_NAME") == 0) { if (tokens[2].compare(interface) == 0) { idx = StrToUInt(tokens[1]); } } else if (line.find(L"DHCP_ENABLE") == 0) { if (idx == StrToUInt(tokens[1])); { result = (tokens[2].compare(L"1") == 0) ? 1 : 0; } } } return result; #elif defined(aix) // The file has entries that look like this: // // # Blah // Mumble ... // interface en1 // { // option 13 0 // option 54 10.152.203.45 // option 9 0 // } // We want the interface name to match our interface // Then search for option 54, which is 'Server Identifier' according // to 'DHCP Server Configuration File' section of 'AIX 6.1 Information' doc // from IBM. // The number at the end of that line is address of the server, or '0' (or blank) // if DHCP is not enabled on this interface. bool result = false; vector<wstring> tokens; for(uint i = 0; i < configData.size(); ++i) { wstring line = configData[i]; StrTokenize(line, tokens, L"\t "); if(tokens.size() < 2) { continue; } // look for "interface" tag followed by our interface, e.g. "interface en1" if(tokens[0].compare(L"interface") == 0 && tokens[1].compare(interface) == 0) { // we could get cute with recursion here but let's just look for // our line .. if we hit a closing curly brace or "interface" tag // call it quits // Loop starting at the same position in the vector .. for(/* i does not change */ ; i < configData.size() ; ++i) { line = configData[i]; StrTokenize(line, tokens, L"\t "); if(tokens.size() < 1) continue; // if this hits we ran past the end of this interface section .. // since we are searching in the interface specified by client, we are done ... if(tokens[0].compare(L"}") == 0) return false; // The only interesting line looks like "option 54 <IPAddress | 0>" so must have 3 tokens if(tokens.size() < 2) continue; // Get line with an address, look at it .. if(tokens[0].compare(L"option") == 0 && tokens[1].compare(L"54") == 0) { // no ip address, no DHCP .... if(tokens[2].compare(L"0") == 0) break; // back to searching for "interface" block // If address has even one ['.' | ':'] char we will assume it is a good [IP | IPv6] address and return true if(tokens[2].find(L'.') != wstring::npos || tokens[2].find(L':') != wstring::npos) return true; // There can be no two entries of the form "interface <our_interface>" in a file // so no good IP address here means we are done ... else return false; } } } } return result; #else throw SCXNotSupportedException(L"GetDHCPEnabledFromConfigData", SCXSRCLOCATION); #endif }