aux.cpp 1.04 KB
#include "aux.h"

void timer_handler (int signum)
{
        if(signum == SIGVTALRM)
        {
                cout << servicename << " CRITICAL - timeout occurred" << endl;
                exit(2);
        }
}

int str2int(string str)
{
        int num;
        stringstream sstream;
        sstream << str;
        if(!(sstream >> num))
        {
                throw integerConversionException("Integer conversion error");
        }
        return num;
}

string int2str(int x)
{
	string str;
        stringstream sstream;
        sstream << x;
        sstream >> str;
        return str;
}

int exec(string cmd, string *output)
{
        *output = "";
        FILE* pipe = popen(cmd.c_str(), "r");
        if (!pipe)
        {
                cout << "Error opening child process" << endl;
                exit(3);
        }
        char buffer[128];
        while(!feof(pipe))
        {
                if(fgets(buffer, 128, pipe) != NULL)
                {
                        *output += buffer;
                }
        }
        return pclose(pipe)/256;
}