⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 systemposix.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    // Search supplemental groups.    // Get a user group entry    //    if (getgrnam_r((char *)groupName, &grp,        grpBuffer, GRP_BUFF_SIZE, &grpresult) != 0)    {        String errorMsg = String("getgrnam_r failure : ") +            String(strerror(errno));        PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2, errorMsg);        Logger::put(            Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING, errorMsg);        throw InternalSystemError();    }    // Check if the requested group was found.    if (grpresult == NULL)    {        return false;    }    Uint32 j = 0;    //    // Get all the members of the group    //    member = grp.gr_mem[j++];    while (member)    {        //        // Check if the user is a member of the group        //        if ( strcmp(userName, member) == 0 )        {            retVal = true;            break;        }        member = grp.gr_mem[j++];    }    return retVal;}#endif /* !PEGASUS_OS_VMS || PEGASUS_ENABLE_USERGROUP_AUTHORIZATION */#ifndef PEGASUS_OS_OS400Boolean System::lookupUserId(    const char* userName,    PEGASUS_UID_T& uid,    PEGASUS_GID_T& gid){    const unsigned int PWD_BUFF_SIZE = 1024;    struct passwd pwd;    struct passwd *result;    char pwdBuffer[PWD_BUFF_SIZE];# if defined(PEGASUS_OS_OS400)    AtoE((char *)userName);# endif    int rc = getpwnam_r(userName, &pwd, pwdBuffer, PWD_BUFF_SIZE, &result);# if defined(PEGASUS_OS_OS400)    EtoA((char *)userName);# endif    if (rc != 0)    {        PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2,            String("getpwnam_r failed: ") + String(strerror(errno)));        return false;    }    if (result == 0)    {        PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2,            "getpwnam_r failed.");        return false;    }    uid = pwd.pw_uid;    gid = pwd.pw_gid;    return true;}Boolean System::changeUserContext(    const PEGASUS_UID_T& uid,    const PEGASUS_GID_T& gid){    Tracer::trace(TRC_OS_ABSTRACTION, Tracer::LEVEL4,        "Changing user context to: uid = %d, gid = %d",        (int)uid, (int)gid);    if (setgid(gid) != 0)    {        PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2,            String("setgid failed: ") + String(strerror(errno)));        return false;    }    if (setuid(uid) != 0)    {        PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2,            String("setuid failed: ") + String(strerror(errno)));        return false;    }    return true;}#endif /* PEGASUS_OS_OS400 */Uint32 System::getPID(){    return getpid();}Boolean System::truncateFile(    const char* path,    size_t newSize){#if defined(PEGASUS_OS_OS400)    int fd = QlgOpen(QlgPath(path), O_WRONLY);    if (fd != -1)    {       int rc = ftruncate(fd, newSize);       close(fd);       return (rc == 0);    }    return false;#else    return (truncate(path, newSize) == 0);#endif}Boolean System::is_absolute_path(const char *path){    if (path == NULL)        return false;    if (path[0] == '/')        return true;    return false;}Boolean System::changeFilePermissions(const char* path, mode_t mode){    Sint32 ret = 0;    const char * tmp = path;#if defined(PEGASUS_OS_OS400)    // ATTN: Update this code to handle UTF8 when path contains UTF8    AtoE((char *)tmp);#endif    return chmod(tmp, mode) == 0 ? true : false;}Boolean System::verifyFileOwnership(const char* path){    struct stat st;#if defined(PEGASUS_OS_OS400)    if (QlgStat(QlgPath(path), &st) != 0)        return false;#else    if (lstat(path, &st) != 0)        return false;#endif    return ((st.st_uid == geteuid()) &&    // Verify the file owner            S_ISREG(st.st_mode) &&         // Verify it is a regular file            (st.st_nlink == 1));           // Verify it is not a hard link}void System::syslog(const String& ident, Uint32 severity, const char* message){#if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_OS_LINUX)    // Since the openlog(), syslog(), and closelog() function calls must be    // coordinated (see below), we need a thread control.    static Mutex logMutex;    AutoMutex loglock(logMutex);    // Get a const char* representation of the identifier string.  Note: The    // character string passed to the openlog() function must persist until    // closelog() is called.  The syslog() method uses this pointer directly    // rather than a copy of the string it refers to.    CString identCString = ident.getCString();    openlog(identCString, LOG_PID, LOG_DAEMON);    // Map from the Logger log level to the system log level.    Uint32 syslogLevel;    if (severity & Logger::FATAL)    {        syslogLevel = LOG_CRIT;    }    else if (severity & Logger::SEVERE)    {        syslogLevel = LOG_ERR;    }    else if (severity & Logger::WARNING)    {        syslogLevel = LOG_WARNING;    }    else if (severity & Logger::INFORMATION)    {        syslogLevel = LOG_INFO;    }    else // if (severity & Logger::TRACE)    {        syslogLevel = LOG_DEBUG;    }    // Write the message to the system log.    ::syslog(syslogLevel, "%s", message);    closelog();#elif defined(PEGASUS_OS_OS400)    std::string replacementData = message;    // All messages will go to the joblog. In the future    // some messages may go to other message queues yet    // to be determined.    if ((severity & Logger::TRACE) ||        (severity & Logger::INFORMATION))    {        // turn into ycmMessage so we can put it in the job log# pragma convert(37)        ycmMessage theMessage("CPIDF80",                              message,                              strlen(message),                              "Logger",                              ycmCTLCIMID,                              TRUE);# pragma convert(0)        // put the message in the joblog        theMessage.joblogIt(UnitOfWorkError,                            ycmMessage::Informational);    }    if ((severity & Logger::WARNING) ||        (severity & Logger::SEVERE)  ||        (severity & Logger::FATAL))    {        // turn into ycmMessage so we can put it in the job log# pragma convert(37)        ycmMessage theMessage("CPDDF82",                              message,                              strlen(message),                              "Logger",                              ycmCTLCIMID,                              TRUE);# pragma convert(0)        // put the message in the joblog        theMessage.joblogIt(UnitOfWorkError,                            ycmMessage::Diagnostic);    }#elif defined(PEGASUS_OS_ZOS) && defined(PEGASUS_USE_SYSLOGS)#define ZOS_MSGID_LENGTH 11    char*           zosMessageString;    Uint32          messageLength = strlen(message);    Uint32          syslogLevel = LOG_DEBUG;    const char*     zos_msgid;    // determine syslog level and create zos_msgid string    if ((severity & Logger::SEVERE) || (severity & Logger::FATAL) )    {        syslogLevel = LOG_ERR;        zos_msgid = "CFZ00004E: ";    }    else if (severity & Logger::WARNING)    {        syslogLevel = LOG_WARNING;        zos_msgid = "CFZ00002W: ";    }    else if (severity & Logger::INFORMATION)    {        syslogLevel = LOG_INFO;        zos_msgid = "CFZ00001I: ";    }    else    {        syslogLevel = LOG_DEBUG;        zos_msgid = "CFZ00001I: ";    }    // we cut at 4000 characters    // leaving room for 11 additional message characters    //     if (messageLength > 4000)        messageLength = 4000;    // reserve memory for the message string, also prepend    // z/OS message id CFZ* if necessary    if (strncmp(message, "CFZ", 3) != 0)    {        // reserve message + 11 char message prepend + 1 byte for \0 char        zosMessageString = (char*) malloc(messageLength+ZOS_MSGID_LENGTH+1);        memcpy(zosMessageString, zos_msgid, ZOS_MSGID_LENGTH);        memcpy(zosMessageString+ZOS_MSGID_LENGTH, message, messageLength);        messageLength+=ZOS_MSGID_LENGTH;    } else    {        zosMessageString = (char*) malloc(messageLength+1);        memcpy(zosMessageString, message, messageLength);    }    // terminate with a null character    zosMessageString[messageLength]='\0';    // write first to syslog, __console changes the content of    // message string    ::syslog(syslogLevel, "%s", zosMessageString);        CString identCString = ident.getCString();    // Issue important messages to the z/OS console    // audit messages will go to a different syslog like place    if (!(severity & Logger::TRACE) &&        !(strcmp("cimserver audit",identCString) == 0))    {        struct __cons_msg   cons;        int                 concmd=0;                memset(&cons,0,sizeof(cons));        cons.__format.__f1.__msg_length = messageLength;        cons.__format.__f1.__msg = zosMessageString;        __console(&cons, NULL, &concmd);    }    free(zosMessageString);#else /* default */    // Not implemented!#endif /* default */}void System::openlog(const char *ident, int logopt, int facility){#if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_OS_LINUX) || \    (defined(PEGASUS_OS_ZOS) && defined(PEGASUS_USE_SYSLOGS))    ::openlog(ident, logopt, facility);#else /* default */    // Not implemented!#endif /* default */}void System::closelog(){#if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_OS_LINUX) ||  \    (defined(PEGASUS_OS_ZOS) && defined(PEGASUS_USE_SYSLOGS))    ::closelog();#else /* default */    // Not implemented!#endif /* default */}// check if a given IP address is defined on the local network interfacesBoolean System::isIpOnNetworkInterface(Uint32 inIP){    // Function compares all IP addresses defined on    // local network interface with a given IP address    #define PEGASUS_MAX_NETWORK_INTERFACES 32    struct ifconf conf;    conf.ifc_buf =        (char *)calloc(PEGASUS_MAX_NETWORK_INTERFACES, sizeof(struct ifreq));    conf.ifc_len = PEGASUS_MAX_NETWORK_INTERFACES * sizeof(struct ifreq);    if (-1 < ioctl(AF_INET, SIOCGIFCONF, &conf))    {        struct ifreq* r = conf.ifc_req;        sockaddr_in* addr;        addr = reinterpret_cast<struct sockaddr_in*>(&r->ifr_addr);        while (addr->sin_addr.s_addr != 0)        {            Uint32 ip = addr->sin_addr.s_addr;            if (ip == inIP)            {                free(conf.ifc_buf);                return true;            }            // next interface            r++;            addr = reinterpret_cast<struct sockaddr_in*>(&r->ifr_addr);        }    }    free(conf.ifc_buf);    return false;}///////////////////////////////////////////////////////////////////////////////// AutoFileLock class///////////////////////////////////////////////////////////////////////////////AutoFileLock::AutoFileLock(const char* fileName){#ifdef PEGASUS_OS_TYPE_UNIX    _fl.l_type = F_WRLCK;    _fl.l_whence = SEEK_SET;    _fl.l_start = 0;    _fl.l_len = 0;    _fl.l_pid = getpid();    do    {        _fd = open(fileName, O_WRONLY);    } while ((_fd == -1) && (errno == EINTR));    if (_fd != -1)    {        int rc;        do        {            rc = fcntl(_fd, F_SETLKW, &_fl);        } while ((rc == -1) && (errno == EINTR));        if (rc == -1)        {            Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,                "AutoFileLock: Failed to lock file '%s', error code %d.",                fileName, errno);            _fd = -1;        }    }    else    {        Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,            "AutoFileLock: Failed to open lock file '%s', error code %d.",            fileName, errno);    }#endif}AutoFileLock::~AutoFileLock(){#ifdef PEGASUS_OS_TYPE_UNIX    if (_fd != -1)    {        _fl.l_type = F_UNLCK;        int rc = fcntl(_fd, F_SETLK, &_fl);        if (rc == -1)        {            Tracer::trace(TRC_DISCARDED_DATA, Tracer::LEVEL2,                "AutoFileLock: Failed to unlock file, error code %d.",                errno);        }        close(_fd);    }#endif}//==============================================================================//// PEGASUS_OS_AIX////==============================================================================// System Initializater for AIX#ifdef PEGASUS_OS_AIX# include <cstdlib>class SystemInitializer{public:    /**     *     * Default constructor.     *     */    SystemInitializer();};SystemInitializer::SystemInitializer(){    putenv("XPG_SUS_ENV=ON");}static SystemInitializer initializer;#endif /* PEGASUS_OS_AIX */PEGASUS_NAMESPACE_END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -