コード例 #1
0
ファイル: testlist.cpp プロジェクト: ivandzen/HSDB
Bool testlist::Child::pushFreeChild(const Child & child)  {
    if( !child.isExists())
        return false;
    child.setNextChildFreeChild(getFirstFreeChild());
    setFirstFreeChild(child);
    setNumFreeChilds(getNumFreeChilds() + 1);
    child.setupFreeChildHandler();
    return true;
}
コード例 #2
0
ファイル: testlist.cpp プロジェクト: ivandzen/HSDB
testlist::Child testlist::Child::popFreeChild()  {
    Child result = getFirstFreeChild();
    if(result.isValid())
    {
        result.removeFreeChildHandler();
        setFirstFreeChild(result.getNextChildFreeChild());
        result.setNextChildFreeChild(Child());
        setNumFreeChilds(getNumFreeChilds() - 1);
    }
    return result;
}
コード例 #3
0
ファイル: testlist.cpp プロジェクト: ivandzen/HSDB
Bool testlist::Child::removeFreeChild(const Child & child)  {
    if( !child.isExists() )
        return false;
    Child prev;
    Child current;
    for(current = getFirstFreeChild(); current.isValid() && current != child; current = current.getNextChildFreeChild())
        prev = current;
    if(!current.isValid())
    {
        return false; //! data corrupted
    }
    current.removeFreeChildHandler();
    if(prev.isValid())
        prev.setNextChildFreeChild(current.getNextChildFreeChild());
    else
        setFirstFreeChild(current.getNextChildFreeChild());
    current.setNextChildFreeChild(Child());
    setNumFreeChilds(getNumFreeChilds() - 1);
    return true;
}
コード例 #4
0
//private
void setupMonitoring(bool isRetry, char* processName, unsigned long int duration, RegisterEntry* tail)
{
    int num = 0;
    Process** runningProcesses = searchRunningProcesses(&num, processName, false, saveLogReport);
    if (runningProcesses == NULL)
    {
        // Nothing to be done
        if (num == 0)
        {
            if (!isRetry)
            {
                LogReport report;
                report.message = stringJoin("No process found with name: ", processName);
                report.type = INFO;
                saveLogReport(report, false);
                free(report.message);
            }

            return;
        }
        exit(-1);
    }

    int i;
    for(i = 0; i < num; ++i)
    {
        Process* p = runningProcesses[i];

        if (p -> pid == getpid())
        {
            // If procnannys were killed in the beginning, but a new one was started in between and the user expects to track that.
            // Should never happen/be done.
            LogReport report;
            report.message = "Config file had procnanny as one of the entries. It will be ignored if no other procnanny is found.";
            report.type = WARNING;
            saveLogReport(report, false);
            continue;
        }

        if (isProcessAlreadyBeingMonitored(p->pid))
        {
            continue;
        }

        logProcessMonitoringInit(processName, p->pid);

        RegisterEntry* freeChild = getFirstFreeChild();
        if (freeChild == NULL)
        {
            // fork a new child
            int writeToChildFD[2];
            int readFromChildFD[2];
            if (pipe(writeToChildFD) < 0)
            {
                destroyProcessArray(runningProcesses, num);
                LogReport report;
                report.message = "Pipe creation error when trying to monitor new process.";
                report.type = ERROR;
                saveLogReport(report, true);
                // TODO: Kill all children
                // TODO: Log all final kill count
                exit(-1);
            }

            if (pipe(readFromChildFD) < 0)
            {
                destroyProcessArray(runningProcesses, num);
                LogReport report;
                report.message = "Pipe creation error when trying to monitor new process.";
                report.type = ERROR;
                saveLogReport(report, true);
                // TODO: Kill all children
                // TODO: Log all final kill count
                exit(-1);
            }

            pid_t forkPid = fork();
            switch (forkPid)
            {
                case -1:
                    destroyProcessArray(runningProcesses, num);
                    exit(-1);

                case CHILD:
                    close(writeToChildFD[1]);
                    close(readFromChildFD[0]);
                    writingToParent = readFromChildFD[1];
                    readingFromParent = writeToChildFD[0];
                    pid_t targetPid = p->pid;
                    destroyProcessArray(runningProcesses, num);
                    cleanupGlobals();

                    while (true)
                    {
                        ProcessStatusCode childStatus = childMain(targetPid, duration);
                        write(writingToParent, &childStatus, 1);

                        MonitorMessage message;
                        assert(read(readingFromParent, &message, sizeof(MonitorMessage)) == sizeof(MonitorMessage));
                        targetPid = message.targetPid;
                        duration = message.monitorDuration;
                    }

                    break;

                default:
                     // parent
                    close(writeToChildFD[0]);
                    close(readFromChildFD[1]);
                    tail->monitoringProcess = forkPid;
                    tail->monitoredProcess = p->pid;
                    tail->monitorDuration = duration;
                    tail->monitoredName = copyString(p->command);
                    tail->startingTime = time(NULL);
                    tail->isAvailable = false;
                    tail->writeToChildFD = writeToChildFD[1];
                    tail->readFromChildFD = readFromChildFD[0];
                    FD_SET(tail->readFromChildFD, &activeFds);
                    tail->next = constuctorRegisterEntry((pid_t)0, NULL, NULL);
                    tail = tail->next;
                    break;
            }

        }
        else
        {
            // use freeChild
            freeChild->isAvailable = false;
            freeChild->monitoredProcess = p->pid;
            free(freeChild->monitoredName);
            freeChild->monitoredName = copyString(p->command);
            freeChild->monitorDuration = duration;
            freeChild->startingTime = time(NULL);
            MonitorMessage message;
            message.targetPid = p->pid;
            message.monitorDuration = duration;
            write(freeChild->writeToChildFD, &message, sizeof(MonitorMessage));
        }
    }

    destroyProcessArray(runningProcesses, num);
}