git-svn-id: svn://db.shs.com.ru/pip@400 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
41
src_main/system/picodec.cpp
Executable file
41
src_main/system/picodec.cpp
Executable file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Text codings coder, based on "iconv"
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "picodec.h"
|
||||
|
||||
|
||||
PIStringList PICodec::availableCodecs() {
|
||||
exec("/usr/bin/iconv", "-l");
|
||||
waitForFinish();
|
||||
PIString str(readOutput());
|
||||
str.cutLeft(str.find("\n "));
|
||||
str.replaceAll("\n", "");
|
||||
return str.split("//");
|
||||
}
|
||||
|
||||
|
||||
PIByteArray PICodec::exec_iconv(const PIString & from, const PIString & to, const PIByteArray & str) {
|
||||
tf.openTemporary(PIIODevice::ReadWrite);
|
||||
tf.clear();
|
||||
tf << str;
|
||||
tf.close();
|
||||
exec("/usr/bin/iconv", PIStringList() << ("-f=" + from) << ("-t=" + to) << tf.path());
|
||||
waitForFinish();
|
||||
return readOutput();
|
||||
}
|
||||
51
src_main/system/picodec.h
Executable file
51
src_main/system/picodec.h
Executable file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Text codings coder, based on "iconv"
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PICODEC_H
|
||||
#define PICODEC_H
|
||||
|
||||
#include "piprocess.h"
|
||||
|
||||
class PIP_EXPORT PICodec: private PIProcess
|
||||
{
|
||||
PIOBJECT(PICodec)
|
||||
public:
|
||||
PICodec(): PIProcess() {setGrabOutput(true);}
|
||||
PICodec(const PIString & from, const PIString & to): PIProcess() {setCodings(from, to);}
|
||||
~PICodec() {tf.remove();}
|
||||
|
||||
void setFromCoding(const PIString & from) {c_from = from;}
|
||||
void setToCoding(const PIString & to) {c_to = to;}
|
||||
void setCodings(const PIString & from, const PIString & to) {c_from = from; c_to = to;}
|
||||
|
||||
PIStringList availableCodecs();
|
||||
PIString encode(PIString & str) {return PIString(exec_iconv(c_from, c_to, str.toByteArray()));}
|
||||
PIString encode(const PIByteArray & str) {return PIString(exec_iconv(c_from, c_to, str));}
|
||||
PIString decode(PIString & str) {return PIString(exec_iconv(c_to, c_from, str.toByteArray()));}
|
||||
PIString decode(const PIByteArray & str) {return PIString(exec_iconv(c_to, c_from, str));}
|
||||
|
||||
private:
|
||||
PIByteArray exec_iconv(const PIString & from, const PIString & to, const PIByteArray & str);
|
||||
|
||||
PIString c_from, c_to;
|
||||
PIFile tf;
|
||||
|
||||
};
|
||||
|
||||
#endif // PICODEC_H
|
||||
119
src_main/system/piintrospection.cpp
Normal file
119
src_main/system/piintrospection.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Introspection module
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piintrospection.h"
|
||||
#include "piincludes.h"
|
||||
#include "pisysteminfo.h"
|
||||
|
||||
|
||||
PIIntrospectionContainers::PIIntrospectionContainers() {
|
||||
count = 0;
|
||||
bytes_allocated = bytes_used = 0;
|
||||
}
|
||||
|
||||
|
||||
PIIntrospectionThreads::PIIntrospectionThreads() {
|
||||
}
|
||||
|
||||
|
||||
PIIntrospectionContainers * __PIIntrospectionContainers__::p = 0;
|
||||
PIIntrospectionThreads * __PIIntrospectionThreads__::p = 0;
|
||||
PIIntrospectionServer * __PIIntrospectionServer__::p = 0;
|
||||
|
||||
|
||||
void PIIntrospectionThreads::registerThread(int id, short prior, const PIString & name) {
|
||||
mutex.lock();
|
||||
ThreadInfo & ti(threads[id]);
|
||||
ti.id = id;
|
||||
ti.priority = prior;
|
||||
ti.name = name;
|
||||
//piCout << "register thread" << id << name;
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void PIIntrospectionThreads::unregisterThread(int id) {
|
||||
mutex.lock();
|
||||
threads.remove(id);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void PIIntrospectionContainers::containerNew() {
|
||||
mutex.lock();
|
||||
count++;
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void PIIntrospectionContainers::containerDelete() {
|
||||
mutex.lock();
|
||||
count--;
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void PIIntrospectionContainers::containerAlloc(ullong cnt) {
|
||||
mutex.lock();
|
||||
bytes_allocated += cnt;
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void PIIntrospectionContainers::containerFree(ullong cnt) {
|
||||
mutex.lock();
|
||||
bytes_allocated -= cnt;
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void PIIntrospectionContainers::containerUsed(ullong cnt) {
|
||||
mutex.lock();
|
||||
bytes_used += cnt;
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void PIIntrospectionContainers::containerUnused(ullong cnt) {
|
||||
mutex.lock();
|
||||
bytes_used -= cnt;
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
PIIntrospectionServer::PIIntrospectionServer(): PIPeer(genName()) {
|
||||
CONNECTU(&itimer, tickEvent, this, timerEvent)
|
||||
itimer.start(100);
|
||||
}
|
||||
|
||||
|
||||
PIString PIIntrospectionServer::genName() {
|
||||
randomize();
|
||||
return "__introspection__server_" + PIString::fromNumber(randomi() % 1000);
|
||||
}
|
||||
|
||||
|
||||
void PIIntrospectionServer::timerEvent() {
|
||||
PIByteArray ba;
|
||||
PIINTROSPECTION_THREADS->mutex.lock();
|
||||
ba << appname << *(PIINTROSPECTION_CONTAINERS) << PIINTROSPECTION_THREADS->threads.values();
|
||||
PIINTROSPECTION_THREADS->mutex.unlock();
|
||||
//piCout << "send" << appname;
|
||||
send("__introspection_client__", ba);
|
||||
}
|
||||
115
src_main/system/piintrospection.h
Normal file
115
src_main/system/piintrospection.h
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Introspection module
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIINTROSPECTION_H
|
||||
#define PIINTROSPECTION_H
|
||||
|
||||
#include "pipeer.h"
|
||||
#include "pimutex.h"
|
||||
|
||||
|
||||
class PIP_EXPORT PIIntrospectionThreads
|
||||
{
|
||||
friend class __PIIntrospectionThreads__;
|
||||
public:
|
||||
PIIntrospectionThreads();
|
||||
|
||||
struct ThreadInfo {
|
||||
ThreadInfo() {id = 0; priority = 0;}
|
||||
PIString name;
|
||||
int id;
|
||||
short priority;
|
||||
};
|
||||
|
||||
void registerThread(int id, short prior, const PIString & name);
|
||||
void unregisterThread(int id);
|
||||
|
||||
PIMap<int, ThreadInfo> threads;
|
||||
PIMutex mutex;
|
||||
};
|
||||
|
||||
|
||||
class PIP_EXPORT PIIntrospectionContainers
|
||||
{
|
||||
friend class __PIIntrospectionContainers__;
|
||||
public:
|
||||
PIIntrospectionContainers();
|
||||
|
||||
void containerNew();
|
||||
void containerDelete();
|
||||
void containerAlloc(ullong cnt);
|
||||
void containerFree(ullong cnt);
|
||||
void containerUsed(ullong cnt);
|
||||
void containerUnused(ullong cnt);
|
||||
|
||||
uint count;
|
||||
ullong bytes_allocated;
|
||||
ullong bytes_used;
|
||||
PIMutex mutex;
|
||||
};
|
||||
|
||||
|
||||
class PIP_EXPORT PIIntrospectionServer: public PIPeer
|
||||
{
|
||||
PIOBJECT_SUBCLASS(PIIntrospectionServer, PIPeer)
|
||||
friend class __PIIntrospectionServer__;
|
||||
PIIntrospectionServer();
|
||||
public:
|
||||
PIString appname;
|
||||
private:
|
||||
EVENT_HANDLER(void, timerEvent);
|
||||
PIString genName();
|
||||
PITimer itimer;
|
||||
};
|
||||
|
||||
|
||||
class __PIIntrospectionContainers__ {
|
||||
public:
|
||||
__PIIntrospectionContainers__() {if (!p) p = new PIIntrospectionContainers();}
|
||||
static PIIntrospectionContainers * get() {static __PIIntrospectionContainers__ * r = new __PIIntrospectionContainers__(); return r->p;}
|
||||
static PIIntrospectionContainers * p;
|
||||
};
|
||||
|
||||
class __PIIntrospectionThreads__ {
|
||||
public:
|
||||
__PIIntrospectionThreads__() {if (!p) p = new PIIntrospectionThreads();}
|
||||
static PIIntrospectionThreads * get() {static __PIIntrospectionThreads__ * r = new __PIIntrospectionThreads__(); return r->p;}
|
||||
static PIIntrospectionThreads * p;
|
||||
};
|
||||
|
||||
class __PIIntrospectionServer__ {
|
||||
public:
|
||||
__PIIntrospectionServer__() {if (!p) p = new PIIntrospectionServer();}
|
||||
~__PIIntrospectionServer__() {if (!p) return; delete p; p = 0;}
|
||||
static PIIntrospectionServer * get() {static __PIIntrospectionServer__ * r = new __PIIntrospectionServer__(); return r->p;}
|
||||
static PIIntrospectionServer * p;
|
||||
};
|
||||
|
||||
|
||||
inline PIByteArray & operator <<(PIByteArray & b, const PIIntrospectionContainers & v) {b << v.count << v.bytes_allocated << v.bytes_used; return b;}
|
||||
inline PIByteArray & operator <<(PIByteArray & b, const PIIntrospectionThreads::ThreadInfo & v) {b << v.id << v.priority << v.name; return b;}
|
||||
|
||||
inline PIByteArray & operator >>(PIByteArray & b, PIIntrospectionContainers & v) {b >> v.count >> v.bytes_allocated >> v.bytes_used; return b;}
|
||||
inline PIByteArray & operator >>(PIByteArray & b, PIIntrospectionThreads::ThreadInfo & v) {b >> v.id >> v.priority >> v.name; return b;}
|
||||
|
||||
#define PIINTROSPECTION_CONTAINERS __PIIntrospectionContainers__::get()
|
||||
#define PIINTROSPECTION_THREADS __PIIntrospectionThreads__::get()
|
||||
#define PIINTROSPECTION_SERVER __PIIntrospectionServer__::get()
|
||||
|
||||
#endif // PIINTROSPECTION_H
|
||||
32
src_main/system/piintrospection_proxy.cpp
Normal file
32
src_main/system/piintrospection_proxy.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Introspection module
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piintrospection_proxy.h"
|
||||
#include "piintrospection.h"
|
||||
#include "pisysteminfo.h"
|
||||
|
||||
void __PIIntrospection__registerThread(int id, short prior, const PIString & name) {PIINTROSPECTION_THREADS->registerThread(id, prior, name);}
|
||||
void __PIIntrospection__unregisterThread(int id) {PIINTROSPECTION_THREADS->unregisterThread(id);}
|
||||
void __PIIntrospection__containerNew() {PIINTROSPECTION_CONTAINERS->containerNew();}
|
||||
void __PIIntrospection__containerDelete() {PIINTROSPECTION_CONTAINERS->containerDelete();}
|
||||
void __PIIntrospection__containerAlloc(ullong cnt) {PIINTROSPECTION_CONTAINERS->containerAlloc(cnt);}
|
||||
void __PIIntrospection__containerFree(ullong cnt) {PIINTROSPECTION_CONTAINERS->containerFree(cnt);}
|
||||
void __PIIntrospection__containerUsed(ullong cnt) {PIINTROSPECTION_CONTAINERS->containerUsed(cnt);}
|
||||
void __PIIntrospection__containerUnused(ullong cnt) {PIINTROSPECTION_CONTAINERS->containerUnused(cnt);}
|
||||
void __PIIntrospection__start() {PIINTROSPECTION_SERVER->appname = PISystemInfo::instance()->execCommand;}
|
||||
69
src_main/system/piintrospection_proxy.h
Normal file
69
src_main/system/piintrospection_proxy.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Introspection module
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIINTROSPECTION_PROXY_H
|
||||
#define PIINTROSPECTION_PROXY_H
|
||||
|
||||
#include "pibase.h"
|
||||
|
||||
class PIString;
|
||||
|
||||
void __PIIntrospection__registerThread(int id, short prior, const PIString & name);
|
||||
void __PIIntrospection__unregisterThread(int id);
|
||||
void __PIIntrospection__containerNew();
|
||||
void __PIIntrospection__containerDelete();
|
||||
void __PIIntrospection__containerAlloc(ullong cnt);
|
||||
void __PIIntrospection__containerFree(ullong cnt);
|
||||
void __PIIntrospection__containerUsed(ullong cnt);
|
||||
void __PIIntrospection__containerUnused(ullong cnt);
|
||||
void __PIIntrospection__start();
|
||||
|
||||
#ifdef PIP_INTROSPECTION_CONTAINERS
|
||||
# define PIINTROSPECTION_CONTAINER_NEW() __PIIntrospection__containerNew();
|
||||
# define PIINTROSPECTION_CONTAINER_DELETE() __PIIntrospection__containerDelete();
|
||||
# define PIINTROSPECTION_CONTAINER_USED(cnt) __PIIntrospection__containerUsed(cnt);
|
||||
# define PIINTROSPECTION_CONTAINER_UNUSED(cnt) __PIIntrospection__containerUnused(cnt);
|
||||
# define PIINTROSPECTION_CONTAINER_ALLOC(cnt) __PIIntrospection__containerAlloc(cnt);
|
||||
# define PIINTROSPECTION_CONTAINER_FREE(cnt) __PIIntrospection__containerFree(cnt);
|
||||
#else
|
||||
# define PIINTROSPECTION_CONTAINER_NEW()
|
||||
# define PIINTROSPECTION_CONTAINER_DELETE()
|
||||
# define PIINTROSPECTION_CONTAINER_USED(tcnt)
|
||||
# define PIINTROSPECTION_CONTAINER_UNUSED(cnt)
|
||||
# define PIINTROSPECTION_CONTAINER_ALLOC(cnt)
|
||||
# define PIINTROSPECTION_CONTAINER_FREE(cnt)
|
||||
#endif
|
||||
|
||||
#ifdef PIP_INTROSPECTION_THREADS
|
||||
# define PIINTROSPECTION_REGISTER_THREAD(id, pr, name) __PIIntrospection__registerThread(id, pr, name);
|
||||
# define PIINTROSPECTION_UNREGISTER_THREAD(id) __PIIntrospection__unregisterThread(id);
|
||||
# define PIINTROSPECTION_START __PIIntrospection__start();
|
||||
#else
|
||||
# define PIINTROSPECTION_REGISTER_THREAD(id, pr, name)
|
||||
# define PIINTROSPECTION_UNREGISTER_THREAD(id)
|
||||
# define PIINTROSPECTION_START
|
||||
#endif
|
||||
|
||||
#if defined(PIP_INTROSPECTION_CONTAINERS) || defined(PIP_INTROSPECTION_THREADS)
|
||||
# define PIINTROSPECTION_START __PIIntrospection__start();
|
||||
#else
|
||||
# define PIINTROSPECTION_START
|
||||
#endif
|
||||
|
||||
#endif // PIINTROSPECTION_PROXY_H
|
||||
109
src_main/system/pilibrary.cpp
Normal file
109
src_main/system/pilibrary.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Dynamic library
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pilibrary.h"
|
||||
#include "piincludes_p.h"
|
||||
#ifndef WINDOWS
|
||||
# include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PILibrary)
|
||||
#ifdef WINDOWS
|
||||
HMODULE
|
||||
#else
|
||||
void *
|
||||
#endif
|
||||
hLib;
|
||||
PRIVATE_DEFINITION_END(PILibrary)
|
||||
|
||||
|
||||
PILibrary::PILibrary(const PIString & path_) {
|
||||
PRIVATE->hLib = 0;
|
||||
load(path_);
|
||||
}
|
||||
|
||||
|
||||
PILibrary::~PILibrary() {
|
||||
unload();
|
||||
}
|
||||
|
||||
|
||||
bool PILibrary::load(const PIString & path_) {
|
||||
libpath = path_;
|
||||
return loadInternal();
|
||||
}
|
||||
|
||||
|
||||
bool PILibrary::load() {
|
||||
return loadInternal();
|
||||
}
|
||||
|
||||
|
||||
void PILibrary::unload() {
|
||||
if (PRIVATE->hLib == 0) return;
|
||||
#ifdef WINDOWS
|
||||
FreeLibrary(PRIVATE->hLib);
|
||||
#else
|
||||
dlclose(PRIVATE->hLib);
|
||||
#endif
|
||||
PRIVATE->hLib = 0;
|
||||
}
|
||||
|
||||
|
||||
bool PILibrary::isLoaded() const {
|
||||
return PRIVATE->hLib;
|
||||
}
|
||||
|
||||
|
||||
void * PILibrary::resolve(const char * symbol) {
|
||||
if (!isLoaded()) return 0;
|
||||
void * ret;
|
||||
#ifdef WINDOWS
|
||||
ret = (void*)GetProcAddress(PRIVATE->hLib, symbol);
|
||||
#else
|
||||
ret = dlsym(PRIVATE->hLib, symbol);
|
||||
#endif
|
||||
getLastError();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool PILibrary::loadInternal() {
|
||||
unload();
|
||||
if (libpath.isEmpty()) return false;
|
||||
#ifdef WINDOWS
|
||||
PRIVATE->hLib = LoadLibrary(libpath.data());
|
||||
#else
|
||||
PRIVATE->hLib = dlopen(libpath.data(), RTLD_LAZY);
|
||||
#endif
|
||||
getLastError();
|
||||
return PRIVATE->hLib;
|
||||
}
|
||||
|
||||
|
||||
void PILibrary::getLastError() {
|
||||
#ifdef WINDOWS
|
||||
liberror = errorString();
|
||||
#else
|
||||
const char * e = dlerror();
|
||||
if (e) liberror = e;
|
||||
else liberror.clear();
|
||||
#endif
|
||||
}
|
||||
47
src_main/system/pilibrary.h
Normal file
47
src_main/system/pilibrary.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Dynamic library
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PILIBRARY_H
|
||||
#define PILIBRARY_H
|
||||
|
||||
#include "pistring.h"
|
||||
|
||||
class PIP_EXPORT PILibrary {
|
||||
public:
|
||||
PILibrary(const PIString & path_ = PIString());
|
||||
~PILibrary();
|
||||
|
||||
bool load(const PIString & path_);
|
||||
bool load();
|
||||
void unload();
|
||||
void * resolve(const char * symbol);
|
||||
|
||||
bool isLoaded() const;
|
||||
PIString lastError() const {return liberror;}
|
||||
|
||||
private:
|
||||
bool loadInternal();
|
||||
void getLastError();
|
||||
|
||||
PRIVATE_DECLARATION
|
||||
PIString libpath, liberror;
|
||||
|
||||
};
|
||||
|
||||
#endif // PILIBRARY_H
|
||||
26
src_main/system/pimonitor.cpp
Executable file
26
src_main/system/pimonitor.cpp
Executable file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Counter of some PIP types
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pimonitor.h"
|
||||
|
||||
PIMonitor piMonitor;
|
||||
|
||||
PIMonitor::PIMonitor() {
|
||||
containers = strings = threads = timers = serials = ethernets = files = objects = 0;
|
||||
}
|
||||
39
src_main/system/pimonitor.h
Executable file
39
src_main/system/pimonitor.h
Executable file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Counter of some PIP types
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIMONITOR_H
|
||||
#define PIMONITOR_H
|
||||
|
||||
#include "pip_export.h"
|
||||
|
||||
#if defined(DOXYGEN) || defined(__GNUC__)
|
||||
# undef PIP_EXPORT
|
||||
# define PIP_EXPORT
|
||||
#endif
|
||||
|
||||
class PIP_EXPORT PIMonitor
|
||||
{
|
||||
public:
|
||||
PIMonitor();
|
||||
|
||||
int containers, strings, threads, timers, serials, ethernets, files, objects;
|
||||
|
||||
};
|
||||
|
||||
#endif // PIMONITOR_H
|
||||
280
src_main/system/piprocess.cpp
Executable file
280
src_main/system/piprocess.cpp
Executable file
@@ -0,0 +1,280 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Process
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "piprocess.h"
|
||||
#ifndef WINDOWS
|
||||
# include <sys/wait.h>
|
||||
# include <csignal>
|
||||
#endif
|
||||
#ifdef MAC_OS
|
||||
# include <crt_externs.h>
|
||||
#endif
|
||||
|
||||
PRIVATE_DEFINITION_START(PIProcess)
|
||||
#ifdef WINDOWS
|
||||
STARTUPINFOA si;
|
||||
PROCESS_INFORMATION pi;
|
||||
#else
|
||||
pid_t pid;
|
||||
#endif
|
||||
PRIVATE_DEFINITION_END(PIProcess)
|
||||
|
||||
|
||||
|
||||
PIProcess::PIProcess(): PIThread() {
|
||||
exit_code = -1;
|
||||
#ifdef WINDOWS
|
||||
PRIVATE->pi.dwProcessId = 0;
|
||||
#else
|
||||
PRIVATE->pid = 0;
|
||||
#endif
|
||||
is_exec = false;
|
||||
g_in = g_out = g_err = false;
|
||||
t_in = t_out = t_err = false;
|
||||
tf_in = tf_out = tf_err = 0;
|
||||
env = PIProcess::currentEnvironment();
|
||||
}
|
||||
|
||||
|
||||
PIProcess::~PIProcess() {
|
||||
if (t_in) f_in.remove();
|
||||
if (t_out) f_out.remove();
|
||||
if (t_err) f_err.remove();
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::exec_() {
|
||||
is_exec = false;
|
||||
startOnce();
|
||||
//cout << "exec wait" << endl;
|
||||
while (!is_exec)
|
||||
msleep(1);
|
||||
//cout << "exec end" << endl;
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::startProc(bool detached) {
|
||||
//cout << "run" << endl;
|
||||
PIString str;
|
||||
/// arguments convertion
|
||||
#ifdef WINDOWS
|
||||
int as = 0;
|
||||
//args.pop_front();
|
||||
piForeachC (PIString & i, args)
|
||||
as += i.lengthAscii() + 3;
|
||||
char * a = new char[as];
|
||||
memset(a, ' ', as - 1);
|
||||
as = 0;
|
||||
for (int i = 0; i < args.size_s(); ++i) {
|
||||
str = args[i];
|
||||
a[as] = '"';
|
||||
memcpy(&a[as + 1], str.data(), str.lengthAscii());
|
||||
a[as + str.length() + 1] = '"';
|
||||
as += str.length() + 3;
|
||||
}
|
||||
a[as - 1] = 0;
|
||||
//piCout << a;
|
||||
#else
|
||||
char * a[args.size_s() + 1];
|
||||
for (int i = 0; i < args.size_s(); ++i) {
|
||||
str = args[i];
|
||||
a[i] = new char[str.size() + 1];
|
||||
memcpy(a[i], str.data(), str.lengthAscii());
|
||||
a[i][str.size()] = 0;
|
||||
//cout << a[i] << endl;
|
||||
}
|
||||
a[args.size_s()] = 0;
|
||||
#endif
|
||||
/// environment convertion
|
||||
char ** e = new char*[env.size_s() + 1];
|
||||
for (int i = 0; i < env.size_s(); ++i) {
|
||||
str = env[i];
|
||||
e[i] = new char[str.size() + 1];
|
||||
memcpy(e[i], str.data(), str.lengthAscii());
|
||||
e[i][str.size()] = 0;
|
||||
//cout << e[i] << endl;
|
||||
}
|
||||
e[env.size_s()] = 0;
|
||||
/// files for stdin/out/err
|
||||
t_in = t_out = t_err = false;
|
||||
if (f_in.path().isEmpty()) {
|
||||
f_in.openTemporary(PIIODevice::ReadWrite);
|
||||
t_in = true;
|
||||
}
|
||||
//f_in.open(PIIODevice::ReadWrite); f_in.close();
|
||||
if (f_out.path().isEmpty()) {
|
||||
f_out.openTemporary(PIIODevice::ReadWrite);
|
||||
t_out = true;
|
||||
}
|
||||
//f_out.open(PIIODevice::WriteOnly); f_out.close();
|
||||
if (f_err.path().isEmpty()) {
|
||||
f_err.openTemporary(PIIODevice::ReadWrite);
|
||||
t_err = true;
|
||||
}
|
||||
//f_err.open(PIIODevice::WriteOnly); f_err.close();
|
||||
|
||||
str = args.front();
|
||||
is_exec = true;
|
||||
if (!detached) execStarted(str);
|
||||
#ifndef WINDOWS
|
||||
int pid_ = fork();
|
||||
if (!detached) PRIVATE->pid = pid_;
|
||||
if (pid_ == 0) {
|
||||
#endif
|
||||
tf_in = tf_out = tf_err = 0;
|
||||
//cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
|
||||
//cout << f_out.path() << endl;
|
||||
if (g_in) tf_in = freopen(f_in.path().data(), "r", stdin);
|
||||
if (g_out) tf_out = freopen(f_out.path().data(), "w", stdout);
|
||||
if (g_err) tf_err = freopen(f_err.path().data(), "w", stderr);
|
||||
#ifndef WINDOWS
|
||||
if (!wd.isEmpty()) chdir(wd.data());
|
||||
#endif
|
||||
#ifdef WINDOWS
|
||||
GetStartupInfoA(&(PRIVATE->si));
|
||||
memset(&(PRIVATE->pi), 0, sizeof(PRIVATE->pi));
|
||||
if(CreateProcessA(0, // No module name (use command line)
|
||||
a, // Command line
|
||||
0, // Process handle not inheritable
|
||||
0, // Thread handle not inheritable
|
||||
false, // Set handle inheritance to FALSE
|
||||
detached ? DETACHED_PROCESS/*CREATE_NEW_CONSOLE*/ : 0, // Creation flags
|
||||
0,//e, // Use environment
|
||||
wd.isEmpty() ? 0 : wd.data(), // Use working directory
|
||||
&(PRIVATE->si), // Pointer to STARTUPINFO structure
|
||||
&(PRIVATE->pi))) // Pointer to PROCESS_INFORMATION structure
|
||||
{
|
||||
if (!detached) WaitForSingleObject(PRIVATE->pi.hProcess, INFINITE);
|
||||
CloseHandle(PRIVATE->pi.hThread);
|
||||
CloseHandle(PRIVATE->pi.hProcess);
|
||||
} else
|
||||
piCoutObj << "\"CreateProcess\" error, " << errorString();
|
||||
#else
|
||||
|
||||
//cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
|
||||
if (execve(str.data(), a, e) < 0)
|
||||
piCoutObj << "\"execve\" error, " << errorString();
|
||||
} else {
|
||||
msleep(1);
|
||||
//cout << "wait" << endl;
|
||||
if (!detached) {
|
||||
wait(&exit_code);
|
||||
/*if (tf_in != 0) fclose(tf_in);
|
||||
if (tf_out != 0) fclose(tf_out);
|
||||
if (tf_err != 0) fclose(tf_err);*/
|
||||
pid_ = 0;
|
||||
if (!detached) PRIVATE->pid = pid_;
|
||||
//cout << "wait done" << endl;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (!detached) execFinished(str, exit_code);
|
||||
is_exec = false;
|
||||
for (int i = 0; i < env.size_s(); ++i)
|
||||
delete e[i];
|
||||
delete[] e;
|
||||
#ifdef WINDOWS
|
||||
delete a;
|
||||
#else
|
||||
for (int i = 0; i < args.size_s(); ++i)
|
||||
delete a[i];
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::terminate() {
|
||||
#ifdef WINDOWS
|
||||
if (is_exec)
|
||||
if (!TerminateProcess(PRIVATE->pi.hProcess, 0))
|
||||
return;
|
||||
PRIVATE->pi.dwProcessId = 0;
|
||||
#else
|
||||
if (is_exec)
|
||||
kill(PRIVATE->pid, SIGKILL);
|
||||
PRIVATE->pid = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::execIndependent(const PIString & program, const PIStringList & args_) {
|
||||
PIProcess p;
|
||||
p.args << program << args_;
|
||||
p.startProc(true);
|
||||
}
|
||||
|
||||
|
||||
int PIProcess::pID() const {
|
||||
#ifdef WINDOWS
|
||||
return PRIVATE->pi.dwProcessId;
|
||||
#else
|
||||
return PRIVATE->pid;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int PIProcess::currentPID() {
|
||||
#ifdef WINDOWS
|
||||
return GetCurrentProcessId();
|
||||
#else
|
||||
return getpid();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
PIStringList PIProcess::currentEnvironment() {
|
||||
PIStringList l;
|
||||
int i = 0;
|
||||
while (environ[i] != 0) {
|
||||
l << environ[i];
|
||||
++i;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::run() {
|
||||
startProc(false);
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::removeEnvironmentVariable(const PIString & variable) {
|
||||
PIString s;
|
||||
for (int i = 0; i < env.size_s(); ++i) {
|
||||
s = env[i];
|
||||
if (s.left(s.find("=")).trimmed() == variable) {
|
||||
env.remove(i);
|
||||
--i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIProcess::setEnvironmentVariable(const PIString & variable, const PIString & value) {
|
||||
PIString s, v;
|
||||
for (int i = 0; i < env.size_s(); ++i) {
|
||||
s = env[i];
|
||||
v = s.left(s.find("=")).trimmed();
|
||||
if (v == variable) {
|
||||
env[i] = v + "=" + value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
env << variable + "=" + value;
|
||||
}
|
||||
105
src_main/system/piprocess.h
Executable file
105
src_main/system/piprocess.h
Executable file
@@ -0,0 +1,105 @@
|
||||
/*! \file piprocess.h
|
||||
* \brief Process
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Process
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIPROCESS_H
|
||||
#define PIPROCESS_H
|
||||
|
||||
#include "pithread.h"
|
||||
#include "pifile.h"
|
||||
|
||||
/// events:
|
||||
/// execStarted(PIString program)
|
||||
/// execFinished(PIString program, int exit_code)
|
||||
///
|
||||
/// handlers:
|
||||
/// bool exec(const PIString & program)
|
||||
/// bool exec(const PIString & program, const PIString & arg1)
|
||||
/// bool exec(const PIString & program, const PIString & arg1, const PIString & arg2)
|
||||
/// bool exec(const PIString & program, const PIString & arg1, const PIString & arg2, const PIString & arg3)
|
||||
/// bool exec(const PIString & program, const PIStringList & args)
|
||||
/// void terminate()
|
||||
/// bool waitForFinish(int timeout_msecs = 60000)
|
||||
class PIP_EXPORT PIProcess: private PIThread
|
||||
{
|
||||
PIOBJECT_SUBCLASS(PIProcess, PIThread)
|
||||
public:
|
||||
PIProcess();
|
||||
virtual ~PIProcess();
|
||||
|
||||
int exitCode() const {return exit_code;}
|
||||
int pID() const;
|
||||
|
||||
void setGrabInput(bool yes) {g_in = yes;}
|
||||
void setGrabOutput(bool yes) {g_out = yes;}
|
||||
void setGrabError(bool yes) {g_err = yes;}
|
||||
void setInputFile(const PIString & path) {f_in.setPath(path);}
|
||||
void setOutputFile(const PIString & path) {f_out.setPath(path);}
|
||||
void setErrorFile(const PIString & path) {f_err.setPath(path);}
|
||||
void unsetInputFile() {f_in.setPath("");}
|
||||
void unsetOutputFile() {f_out.setPath("");}
|
||||
void unsetErrorFile() {f_err.setPath("");}
|
||||
PIString workingDirectory() const {return wd;}
|
||||
void setWorkingDirectory(const PIString & path) {wd = path;}
|
||||
void resetWorkingDirectory() {wd.clear();}
|
||||
PIByteArray readOutput() {f_out.open(PIIODevice::ReadOnly); return f_out.readAll();}
|
||||
PIByteArray readError() {f_err.open(PIIODevice::ReadOnly); return f_err.readAll();}
|
||||
|
||||
PIStringList environment() {return env;}
|
||||
void clearEnvironment() {env.clear();}
|
||||
void removeEnvironmentVariable(const PIString & variable);
|
||||
void setEnvironmentVariable(const PIString & variable, const PIString & value);
|
||||
|
||||
EVENT_HANDLER1(void, exec, const PIString & , program) {args.clear(); args << program; exec_();}
|
||||
EVENT_HANDLER2(void, exec, const PIString & , program, const PIString & , arg) {args.clear(); args << program << arg; exec_();}
|
||||
EVENT_HANDLER2(void, exec, const PIString & , program, const PIStringList & , args_) {args << program << args_; exec_();}
|
||||
EVENT_HANDLER(void, terminate);
|
||||
EVENT_HANDLER(bool, waitForFinish) {return waitForFinish(60000);}
|
||||
EVENT_HANDLER1(bool, waitForFinish, int, timeout_msecs) {return PIThread::waitForFinish(timeout_msecs);}
|
||||
|
||||
EVENT1(execStarted, PIString, program)
|
||||
EVENT2(execFinished, PIString, program, int, exit_code)
|
||||
|
||||
static void execIndependent(const PIString & program) {execIndependent(program, PIStringList());}
|
||||
static void execIndependent(const PIString & program, const PIString & arg) {execIndependent(program, PIStringList() << arg);}
|
||||
static void execIndependent(const PIString & program, const PIStringList & args_);
|
||||
|
||||
static PIStringList currentEnvironment();
|
||||
static int currentPID();
|
||||
|
||||
private:
|
||||
virtual void run();
|
||||
void exec_();
|
||||
void startProc(bool detached);
|
||||
|
||||
PIStringList args, env;
|
||||
PIString wd;
|
||||
PIByteArray out;
|
||||
PIFile f_in, f_out, f_err;
|
||||
bool g_in, g_out, g_err, t_in, t_out, t_err;
|
||||
PRIVATE_DECLARATION
|
||||
FILE * tf_in, * tf_out, * tf_err;
|
||||
int exit_code, sz;
|
||||
bool is_exec;
|
||||
|
||||
};
|
||||
|
||||
#endif // PIPROCESS_H
|
||||
126
src_main/system/pisignals.cpp
Executable file
126
src_main/system/pisignals.cpp
Executable file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Signals
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piincludes.h"
|
||||
#include "pisignals.h"
|
||||
#ifdef BLACKBERRY
|
||||
# include <signal.h>
|
||||
#else
|
||||
# include <csignal>
|
||||
#endif
|
||||
#ifdef WINDOWS
|
||||
# define SIGUSR1 10
|
||||
# define SIGUSR2 12
|
||||
#endif
|
||||
|
||||
PISignals::SignalEvent PISignals::ret_func;
|
||||
|
||||
|
||||
void PISignals::grabSignals(PIFlags<PISignals::Signal> signals_) {
|
||||
if (signals_[PISignals::Interrupt]) signal(signalCode(PISignals::Interrupt), PISignals::signal_event);
|
||||
if (signals_[PISignals::Illegal]) signal(signalCode(PISignals::Illegal), PISignals::signal_event);
|
||||
if (signals_[PISignals::Abort]) signal(signalCode(PISignals::Abort), PISignals::signal_event);
|
||||
if (signals_[PISignals::FPE]) signal(signalCode(PISignals::FPE), PISignals::signal_event);
|
||||
if (signals_[PISignals::SegFault]) signal(signalCode(PISignals::SegFault), PISignals::signal_event);
|
||||
if (signals_[PISignals::Termination]) signal(signalCode(PISignals::Termination), PISignals::signal_event);
|
||||
if (signals_[PISignals::UserDefined1]) signal(signalCode(PISignals::UserDefined1), PISignals::signal_event);
|
||||
if (signals_[PISignals::UserDefined2]) signal(signalCode(PISignals::UserDefined2), PISignals::signal_event);
|
||||
#ifndef WINDOWS
|
||||
if (signals_[PISignals::Hangup]) signal(signalCode(PISignals::Hangup), PISignals::signal_event);
|
||||
if (signals_[PISignals::Quit]) signal(signalCode(PISignals::Quit), PISignals::signal_event);
|
||||
if (signals_[PISignals::Kill]) signal(signalCode(PISignals::Kill), PISignals::signal_event);
|
||||
if (signals_[PISignals::BrokenPipe]) signal(signalCode(PISignals::BrokenPipe), PISignals::signal_event);
|
||||
if (signals_[PISignals::Timer]) signal(signalCode(PISignals::Timer), PISignals::signal_event);
|
||||
if (signals_[PISignals::ChildStopped]) signal(signalCode(PISignals::ChildStopped), PISignals::signal_event);
|
||||
if (signals_[PISignals::Continue]) signal(signalCode(PISignals::Continue), PISignals::signal_event);
|
||||
if (signals_[PISignals::StopProcess]) signal(signalCode(PISignals::StopProcess), PISignals::signal_event);
|
||||
if (signals_[PISignals::StopTTY]) signal(signalCode(PISignals::StopTTY), PISignals::signal_event);
|
||||
if (signals_[PISignals::StopTTYInput]) signal(signalCode(PISignals::StopTTYInput), PISignals::signal_event);
|
||||
if (signals_[PISignals::StopTTYOutput]) signal(signalCode(PISignals::StopTTYOutput), PISignals::signal_event);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PISignals::raiseSignal(PISignals::Signal signal) {
|
||||
raise(signalCode(signal));
|
||||
}
|
||||
|
||||
|
||||
int PISignals::signalCode(PISignals::Signal signal) {
|
||||
switch (signal) {
|
||||
case PISignals::Interrupt: return SIGINT;
|
||||
case PISignals::Illegal: return SIGILL;
|
||||
case PISignals::Abort: return SIGABRT;
|
||||
case PISignals::FPE: return SIGFPE;
|
||||
case PISignals::SegFault: return SIGSEGV;
|
||||
case PISignals::Termination: return SIGTERM;
|
||||
case PISignals::UserDefined1: return SIGUSR1;
|
||||
case PISignals::UserDefined2: return SIGUSR2;
|
||||
#ifndef WINDOWS
|
||||
case PISignals::Hangup: return SIGHUP;
|
||||
case PISignals::Quit: return SIGQUIT;
|
||||
case PISignals::Kill: return SIGKILL;
|
||||
case PISignals::BrokenPipe: return SIGPIPE;
|
||||
case PISignals::Timer: return SIGALRM;
|
||||
case PISignals::ChildStopped: return SIGCHLD;
|
||||
case PISignals::Continue: return SIGCONT;
|
||||
case PISignals::StopProcess: return SIGSTOP;
|
||||
case PISignals::StopTTY: return SIGTSTP;
|
||||
case PISignals::StopTTYInput: return SIGTTIN;
|
||||
case PISignals::StopTTYOutput:return SIGTTOU;
|
||||
#endif
|
||||
default:;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
PISignals::Signal PISignals::signalFromCode(int signal) {
|
||||
switch (signal) {
|
||||
case SIGINT: return PISignals::Interrupt;
|
||||
case SIGILL: return PISignals::Illegal;
|
||||
case SIGABRT: return PISignals::Abort;
|
||||
case SIGFPE: return PISignals::FPE;
|
||||
case SIGSEGV: return PISignals::SegFault;
|
||||
case SIGTERM: return PISignals::Termination;
|
||||
case SIGUSR1: return PISignals::UserDefined1;
|
||||
case SIGUSR2: return PISignals::UserDefined2;
|
||||
#ifndef WINDOWS
|
||||
case SIGHUP: return PISignals::Hangup;
|
||||
case SIGQUIT: return PISignals::Quit;
|
||||
case SIGKILL: return PISignals::Kill;
|
||||
case SIGPIPE: return PISignals::BrokenPipe;
|
||||
case SIGALRM: return PISignals::Timer;
|
||||
case SIGCHLD: return PISignals::ChildStopped;
|
||||
case SIGCONT: return PISignals::Continue;
|
||||
case SIGSTOP: return PISignals::StopProcess;
|
||||
case SIGTSTP: return PISignals::StopTTY;
|
||||
case SIGTTIN: return PISignals::StopTTYInput;
|
||||
case SIGTTOU: return PISignals::StopTTYOutput;
|
||||
#endif
|
||||
default:;
|
||||
}
|
||||
return PISignals::Termination;
|
||||
}
|
||||
|
||||
|
||||
void PISignals::signal_event(int signal) {
|
||||
if (PISignals::ret_func == 0) return;
|
||||
PISignals::ret_func(PISignals::signalFromCode(signal));
|
||||
}
|
||||
73
src_main/system/pisignals.h
Executable file
73
src_main/system/pisignals.h
Executable file
@@ -0,0 +1,73 @@
|
||||
/*! \file pisignals.h
|
||||
* \brief System signals
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Signals
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PISIGNALS_H
|
||||
#define PISIGNALS_H
|
||||
|
||||
#include "piflags.h"
|
||||
|
||||
class PIP_EXPORT PISignals
|
||||
{
|
||||
public:
|
||||
enum Signal {
|
||||
Interrupt /** Interrupt from keyboard */ = 0x01, // Term Interrupt from keyboard
|
||||
Illegal /** Illegal Instruction */ = 0x02, // Core Illegal Instruction
|
||||
Abort /** Abort signal */ = 0x04, // Core Abort signal from abort
|
||||
FPE /** Floating point exception */ = 0x08, // Core Floating point exception
|
||||
SegFault /** Invalid memory reference */ = 0x10, // Core Invalid memory reference
|
||||
Termination /** Termination signal */ = 0x20, // Term Termination signal
|
||||
Hangup = 0x40, // Term Hangup detected on controlling terminal or death of controlling process
|
||||
Quit = 0x80, // Core Quit from keyboard
|
||||
Kill = 0x100, // Term Kill signal
|
||||
BrokenPipe = 0x200, // Term Broken pipe: write to pipe with no readers
|
||||
Timer = 0x400, // Term Timer signal from alarm
|
||||
UserDefined1 = 0x800, // Term User-defined signal 1
|
||||
UserDefined2 = 0x1000, // Term User-defined signal 2
|
||||
ChildStopped = 0x2000, // Ign Child stopped or terminated
|
||||
Continue = 0x4000, // Cont Continue if stopped
|
||||
StopProcess = 0x8000, // Stop Stop process
|
||||
StopTTY = 0x10000, // Stop Stop typed at tty
|
||||
StopTTYInput = 0x20000, // Stop tty input for background process
|
||||
StopTTYOutput = 0x40000, // Stop tty output for background process
|
||||
All = 0xFFFFF
|
||||
};
|
||||
|
||||
typedef void (*SignalEvent)(PISignals::Signal);
|
||||
// slot is any function format "void <func>(PISignals::Signal)"
|
||||
static void setSlot(SignalEvent slot) {ret_func = slot;}
|
||||
static void grabSignals(PIFlags<PISignals::Signal> signals_);
|
||||
static void raiseSignal(PISignals::Signal signal);
|
||||
|
||||
private:
|
||||
PISignals() {ret_func = 0;}
|
||||
~PISignals() {}
|
||||
|
||||
static int signalCode(PISignals::Signal signal);
|
||||
static PISignals::Signal signalFromCode(int signal);
|
||||
static void signal_event(int signal);
|
||||
|
||||
static SignalEvent ret_func;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // PISIGNALS_H
|
||||
104
src_main/system/pisingleapplication.cpp
Normal file
104
src_main/system/pisingleapplication.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Single application
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pisingleapplication.h"
|
||||
#include "pisharedmemory.h"
|
||||
|
||||
#define SHM_SIZE 1024*32
|
||||
|
||||
|
||||
PISingleApplication::PISingleApplication(const PIString & app_name): PIThread() {
|
||||
first = true;
|
||||
started = false;
|
||||
sacnt = 0;
|
||||
shm = new PISharedMemory("sa_" + app_name, SHM_SIZE);
|
||||
start(100);
|
||||
}
|
||||
|
||||
|
||||
PISingleApplication::~PISingleApplication() {
|
||||
stop();
|
||||
if (!waitForFinish(5000))
|
||||
terminate();
|
||||
delete shm;
|
||||
}
|
||||
|
||||
|
||||
bool PISingleApplication::isFirst() const {
|
||||
waitFirst();
|
||||
return first;
|
||||
}
|
||||
|
||||
|
||||
void PISingleApplication::sendMessage(const PIByteArray & m) {
|
||||
waitFirst();
|
||||
PIByteArray ba;
|
||||
int lm[2] = {0, 0};
|
||||
for (;;) {
|
||||
shm->read(lm, 8);
|
||||
if (lm[1] == 0) break;
|
||||
piMSleep(10);
|
||||
}
|
||||
ba << sacnt << int(1) << m;
|
||||
shm->write(ba);
|
||||
}
|
||||
|
||||
|
||||
void PISingleApplication::begin() {
|
||||
int cnt[2] = {0, 0};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
cnt[1] = cnt[0];
|
||||
shm->read(cnt, 4);
|
||||
if (cnt[0] != cnt[1]) {
|
||||
first = false;
|
||||
break;
|
||||
}
|
||||
piMSleep(100);
|
||||
}
|
||||
//piCoutObj << "started" << first << shm->size();
|
||||
readed.reserve(shm->size());
|
||||
started = true;
|
||||
}
|
||||
|
||||
|
||||
void PISingleApplication::run() {
|
||||
if (!first) return;
|
||||
++sacnt;
|
||||
shm->write(&sacnt, 4);
|
||||
//piCoutObj << "write" << sacnt;
|
||||
readed = shm->readAll();
|
||||
int t(0), nm(0);
|
||||
readed >> t >> nm;
|
||||
if (nm != 0) {
|
||||
PIByteArray msg;
|
||||
readed >> msg;
|
||||
if (!msg.isEmpty()) {
|
||||
messageReceived(msg);
|
||||
//piCoutObj << "message" << msg;
|
||||
}
|
||||
int wi[2] = {sacnt, 0};
|
||||
shm->write(wi, 8);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PISingleApplication::waitFirst() const {
|
||||
while (!started)
|
||||
piMSleep(50);
|
||||
}
|
||||
51
src_main/system/pisingleapplication.h
Normal file
51
src_main/system/pisingleapplication.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Single application
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PISINGLEAPPLICATION_H
|
||||
#define PISINGLEAPPLICATION_H
|
||||
|
||||
#include "pithread.h"
|
||||
|
||||
class PISharedMemory;
|
||||
|
||||
class PIP_EXPORT PISingleApplication: public PIThread {
|
||||
PIOBJECT_SUBCLASS(PISingleApplication, PIThread)
|
||||
public:
|
||||
PISingleApplication(const PIString & app_name = PIString());
|
||||
~PISingleApplication();
|
||||
|
||||
bool isFirst() const;
|
||||
|
||||
EVENT_HANDLER1(void, sendMessage, const PIByteArray &, m);
|
||||
EVENT1(messageReceived, const PIByteArray &, m)
|
||||
|
||||
private:
|
||||
void begin();
|
||||
void run();
|
||||
void waitFirst() const;
|
||||
|
||||
PISharedMemory * shm;
|
||||
PITimeMeasurer ftm;
|
||||
PIByteArray readed;
|
||||
bool first, started;
|
||||
int sacnt;
|
||||
|
||||
};
|
||||
|
||||
#endif // PISINGLEAPPLICATION_H
|
||||
184
src_main/system/pisysteminfo.cpp
Normal file
184
src_main/system/pisysteminfo.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
System information
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "pisysteminfo.h"
|
||||
#include "piincludes_p.h"
|
||||
|
||||
|
||||
PISystemInfo::PISystemInfo() {
|
||||
processorsCount = 1;
|
||||
}
|
||||
|
||||
|
||||
PISystemInfo * PISystemInfo::instance() {
|
||||
static PISystemInfo ret;
|
||||
return &ret;
|
||||
}
|
||||
|
||||
|
||||
PIStringList PISystemInfo::mountRoots() {
|
||||
PIStringList ret;
|
||||
#ifdef WINDOWS
|
||||
char letters[1024];
|
||||
DWORD ll = GetLogicalDriveStrings(1023, letters);
|
||||
PIString clet;
|
||||
for (uint i = 0; i < ll; ++i) {
|
||||
if (letters[i] == '\0') {
|
||||
if (clet.size_s() > 2) ret << clet.cutRight(1);
|
||||
clet.clear();
|
||||
} else
|
||||
clet += PIChar(letters[i]);
|
||||
}
|
||||
#else
|
||||
# ifdef LINUX
|
||||
PIString s_df, s_m;
|
||||
char in[1024];
|
||||
memset(in, 0, 1024);
|
||||
FILE * fp = popen("mount -l", "r");
|
||||
PIStringList tl;
|
||||
if (fp) {
|
||||
while (fgets(in, 1024, fp)) {
|
||||
tl = PIString(in).trim().replaceAll(" ", " ").split(" ");
|
||||
if (tl.size_s() < 2) continue;
|
||||
for (int i = 0; i < tl.size_s() - 1; ++i)
|
||||
if (tl[i] == "on") {if (!ret.contains(tl[i + 1])) ret << tl[i + 1]; break;}
|
||||
}
|
||||
pclose(fp);
|
||||
}
|
||||
# else
|
||||
# endif
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
struct String3 {PIString mp, type, label;};
|
||||
|
||||
PIVector<PISystemInfo::MountInfo> PISystemInfo::mountInfo() {
|
||||
static PIVector<PISystemInfo::MountInfo> cache;
|
||||
static PITimeMeasurer tm;
|
||||
static bool first = true;
|
||||
if (tm.elapsed_m() < piMountInfoRefreshIntervalMs && !first)
|
||||
return cache;
|
||||
first = false;
|
||||
tm.reset();
|
||||
cache.clear();
|
||||
|
||||
PIVector<PISystemInfo::MountInfo> ret;
|
||||
MountInfo m;
|
||||
#ifdef WINDOWS
|
||||
char letters[1024], volname[1024], volfs[1024];
|
||||
DWORD ll = GetLogicalDriveStrings(1023, letters);
|
||||
PIString clet;
|
||||
for (DWORD i = 0; i < ll; ++i) {
|
||||
if (letters[i] == '\0') {
|
||||
if (GetVolumeInformation(clet.data(), volname, 1023, 0, 0, 0, volfs, 1023)) {
|
||||
m.mount_point = clet;
|
||||
m.filesystem = volfs;
|
||||
m.label = volname;
|
||||
DWORD spc, bps, free_cl, all_cl;
|
||||
if (GetDiskFreeSpace(clet.data(), &spc, &bps, &free_cl, &all_cl)) {
|
||||
ullong bpc = ullong(spc) * ullong(bps);
|
||||
m.space_all = bpc * ullong(all_cl);
|
||||
m.space_free = bpc * ullong(free_cl);
|
||||
m.space_used = m.space_all - m.space_free;
|
||||
} else
|
||||
m.space_all = m.space_free = m.space_used = 0U;
|
||||
clet.cutRight(1);
|
||||
int qdd = QueryDosDevice(clet.data(), volfs, 1023);
|
||||
if (qdd > 0)
|
||||
m.device = volfs;
|
||||
else
|
||||
m.device.clear();
|
||||
ret << m;
|
||||
}
|
||||
clet.clear();
|
||||
} else
|
||||
clet += PIChar(letters[i]);
|
||||
}
|
||||
|
||||
/*HANDLE hVolume = FindFirstVolumeA(volGUID, 1024);
|
||||
if (hVolume) {
|
||||
do {
|
||||
piCout << "Volume" << PIString(volGUID);
|
||||
} while (FindNextVolumeA(hVolume, volGUID, 1024))
|
||||
FindVolumeClose(hVolume);
|
||||
}*/
|
||||
#else
|
||||
# ifdef LINUX
|
||||
PIString s_df, s_m;
|
||||
char in[1024];
|
||||
memset(in, 0, 1024);
|
||||
//piCout << "mountInfo 0";
|
||||
FILE * fp = popen("df -B1", "r");
|
||||
PIStringList l_df;
|
||||
PIMap<PIString, String3> fs;
|
||||
if (fp) {
|
||||
while (fgets(in, 1024, fp))
|
||||
l_df << PIString(in).trim();
|
||||
pclose(fp);
|
||||
fp = 0;
|
||||
}
|
||||
//piCout << "mountInfo 1";
|
||||
memset(in, 0, 1024);
|
||||
fp = popen("mount -l", "r");
|
||||
PIStringList tl;
|
||||
if (fp) {
|
||||
while (fgets(in, 1024, fp)) {
|
||||
tl = PIString(in).trim().replaceAll(" ", " ").split(" ");
|
||||
if (tl.size_s() < 2) continue;
|
||||
String3 me;
|
||||
PIString dev;
|
||||
dev = tl.front();
|
||||
for (int i = 0; i < tl.size_s() - 1; ++i) {
|
||||
if (tl[i] == "on") {me.mp = tl[i + 1]; ++i; continue;}
|
||||
if (tl[i] == "type") {me.type = tl[i + 1]; ++i; continue;}
|
||||
}
|
||||
if (tl.back().startsWith("[")) {
|
||||
me.label = tl.back();
|
||||
me.label.cutLeft(1).cutRight(1);
|
||||
}
|
||||
fs[dev] = me;
|
||||
//piCout << fsmp << fstl;
|
||||
}
|
||||
pclose(fp);
|
||||
fp = 0;
|
||||
}
|
||||
if (l_df.size_s() < 2) return ret;
|
||||
l_df.pop_front();
|
||||
piForeachC (PIString & s, l_df) {
|
||||
PIStringList ml(s.replaceAll(" ", " ").split(" "));
|
||||
if (ml.size_s() < 2) continue;
|
||||
if (ml.front() == "none") continue;
|
||||
m.space_all = ml[1].toULLong();
|
||||
m.space_used = ml[2].toULLong();
|
||||
m.space_free = m.space_all - m.space_used;
|
||||
String3 s3 = fs.value(ml.front());
|
||||
m.device = ml.front();
|
||||
m.filesystem = s3.type;
|
||||
m.mount_point = s3.mp;
|
||||
m.label = s3.label;
|
||||
ret << m;
|
||||
//piCout << ml;
|
||||
}
|
||||
# else
|
||||
# endif
|
||||
#endif
|
||||
cache = ret;
|
||||
return ret;
|
||||
}
|
||||
61
src_main/system/pisysteminfo.h
Normal file
61
src_main/system/pisysteminfo.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
System information
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PISYSTEMINFO_H
|
||||
#define PISYSTEMINFO_H
|
||||
|
||||
#include "pitime.h"
|
||||
|
||||
class PIP_EXPORT PISystemInfo {
|
||||
public:
|
||||
PISystemInfo();
|
||||
|
||||
struct MountInfo {
|
||||
MountInfo() {space_all = space_used = space_free = 0;}
|
||||
PIString mount_point;
|
||||
PIString device;
|
||||
PIString filesystem;
|
||||
PIString label;
|
||||
ullong space_all;
|
||||
ullong space_used;
|
||||
ullong space_free;
|
||||
};
|
||||
|
||||
PIString ifconfigPath, execCommand, hostname, user, OS_name, OS_version, architecture;
|
||||
PIDateTime execDateTime;
|
||||
int processorsCount;
|
||||
|
||||
static PIStringList mountRoots();
|
||||
static PIVector<MountInfo> mountInfo();
|
||||
|
||||
static PISystemInfo * instance();
|
||||
|
||||
};
|
||||
|
||||
inline PICout operator <<(PICout s, const PISystemInfo::MountInfo & v) {
|
||||
s.setControl(0, true);
|
||||
s << "MountInfo(" << v.device << " mounted on \"" << v.mount_point << "\", type " << v.filesystem
|
||||
<< ", label \"" << v.label << "\", all " << PIString::readableSize(v.space_all)
|
||||
<< ", used " << PIString::readableSize(v.space_used)
|
||||
<< ", free " << PIString::readableSize(v.space_free) << ")";
|
||||
s.restoreControl();
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif // PISYSTEMINFO_H
|
||||
32
src_main/system/pisystemmodule.h
Normal file
32
src_main/system/pisystemmodule.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Module includes
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PISYSTEMMODULE_H
|
||||
#define PISYSTEMMODULE_H
|
||||
|
||||
#include "pimonitor.h"
|
||||
#include "picodec.h"
|
||||
#include "pisignals.h"
|
||||
#include "pilibrary.h"
|
||||
#include "pisysteminfo.h"
|
||||
#include "pisystemtests.h"
|
||||
#include "pisystemmonitor.h"
|
||||
#include "pisingleapplication.h"
|
||||
|
||||
#endif // PISYSTEMMODULE_H
|
||||
219
src_main/system/pisystemmonitor.cpp
Executable file
219
src_main/system/pisystemmonitor.cpp
Executable file
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Process resource monitor
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "pisystemmonitor.h"
|
||||
#include "pisysteminfo.h"
|
||||
#include "pitime_win.h"
|
||||
#ifdef WINDOWS
|
||||
# include <psapi.h>
|
||||
# include <tlhelp32.h>
|
||||
#endif
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PISystemMonitor)
|
||||
#ifndef WINDOWS
|
||||
llong cpu_u_cur, cpu_u_prev, cpu_s_cur, cpu_s_prev;
|
||||
#else
|
||||
HANDLE hProc;
|
||||
PROCESS_MEMORY_COUNTERS mem_cnt;
|
||||
PISystemTime tm_kernel, tm_user;
|
||||
PITimeMeasurer tm;
|
||||
#endif
|
||||
PRIVATE_DEFINITION_END(PISystemMonitor)
|
||||
|
||||
|
||||
PISystemMonitor::PISystemMonitor(): PIThread() {
|
||||
pID_ = cycle = 0;
|
||||
cpu_count = PISystemInfo::instance()->processorsCount;
|
||||
#ifndef WINDOWS
|
||||
# ifdef QNX
|
||||
page_size = 4096;
|
||||
# else
|
||||
page_size = getpagesize();
|
||||
# endif
|
||||
#else
|
||||
PRIVATE->hProc = 0;
|
||||
PRIVATE->mem_cnt.cb = sizeof(PRIVATE->mem_cnt);
|
||||
#endif
|
||||
setName("system_monitor");
|
||||
}
|
||||
|
||||
|
||||
PISystemMonitor::~PISystemMonitor() {
|
||||
stop();
|
||||
}
|
||||
|
||||
|
||||
PISystemMonitor::ProcessStats::ProcessStats() {
|
||||
ID = parent_ID = group_ID = session_ID = priority = threads = 0;
|
||||
physical_memsize = resident_memsize = share_memsize = virtual_memsize = data_memsize = 0;
|
||||
cpu_load_user = cpu_load_system = 0.f;
|
||||
}
|
||||
|
||||
|
||||
bool PISystemMonitor::startOnProcess(int pID) {
|
||||
stop();
|
||||
pID_ = pID;
|
||||
#ifndef WINDOWS
|
||||
file.open("/proc/" + PIString::fromNumber(pID_) + "/stat", PIIODevice::ReadOnly);
|
||||
filem.open("/proc/" + PIString::fromNumber(pID_) + "/statm", PIIODevice::ReadOnly);
|
||||
if (!file.isOpened()) {
|
||||
piCoutObj << "Can`t find process with ID = " << pID_ << "!";
|
||||
return false;
|
||||
}
|
||||
cycle = -1;
|
||||
#else
|
||||
PRIVATE->hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pID_);
|
||||
if (PRIVATE->hProc == 0) {
|
||||
piCoutObj << "Can`t open process with ID = " << pID_ << "," << errorString();
|
||||
return false;
|
||||
}
|
||||
PRIVATE->tm.reset();
|
||||
#endif
|
||||
return start(1000);
|
||||
}
|
||||
|
||||
|
||||
void PISystemMonitor::stop() {
|
||||
PIThread::stop();
|
||||
#ifdef WINDOWS
|
||||
if (PRIVATE->hProc != 0) {
|
||||
CloseHandle(PRIVATE->hProc);
|
||||
PRIVATE->hProc = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
void PISystemMonitor::run() {
|
||||
#ifndef WINDOWS
|
||||
file.seekToBegin();
|
||||
PIString str(file.readAll(true));
|
||||
int si = str.find('(') + 1, fi = 0, cc = 1;
|
||||
for (int i = si; i < str.size_s(); ++i) {
|
||||
if (str[i] == '(') cc++;
|
||||
if (str[i] == ')') cc--;
|
||||
if (cc <= 0) {
|
||||
fi = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
stat.exec_name = str.mid(si, fi - si);
|
||||
str.cutMid(si - 1, fi - si + 3);
|
||||
PIStringList sl = str.split(" ");
|
||||
if (sl.size_s() < 19) return;
|
||||
stat.ID = sl[0].toInt();
|
||||
stat.state = sl[1];
|
||||
stat.parent_ID = sl[2].toInt();
|
||||
stat.group_ID = sl[3].toInt();
|
||||
stat.session_ID = sl[4].toInt();
|
||||
if (cycle < 0) {
|
||||
PRIVATE->cpu_u_prev = PRIVATE->cpu_u_cur = sl[12].toLLong();
|
||||
PRIVATE->cpu_s_prev = PRIVATE->cpu_s_cur = sl[13].toLLong();
|
||||
}
|
||||
cycle++;
|
||||
//if (cycle >= 4) {
|
||||
PRIVATE->cpu_u_prev = PRIVATE->cpu_u_cur;
|
||||
PRIVATE->cpu_s_prev = PRIVATE->cpu_s_cur;
|
||||
PRIVATE->cpu_u_cur = sl[12].toLLong();
|
||||
PRIVATE->cpu_s_cur = sl[13].toLLong();
|
||||
stat.cpu_load_system = PRIVATE->cpu_s_cur - PRIVATE->cpu_s_prev;
|
||||
stat.cpu_load_user = PRIVATE->cpu_u_cur - PRIVATE->cpu_u_prev;
|
||||
stat.cpu_load_system /= cpu_count;
|
||||
stat.cpu_load_user /= cpu_count;
|
||||
cycle = 0;
|
||||
//}
|
||||
stat.priority = sl[16].toInt();
|
||||
stat.threads = sl[18].toInt();
|
||||
|
||||
filem.seekToBegin();
|
||||
str = filem.readAll(true);
|
||||
sl = str.split(" ");
|
||||
if (sl.size_s() < 6) return;
|
||||
stat.virtual_memsize = sl[0].toLong() * page_size;
|
||||
stat.resident_memsize = sl[1].toLong() * page_size;
|
||||
stat.share_memsize = sl[2].toLong() * page_size;
|
||||
stat.data_memsize = sl[5].toLong() * page_size;
|
||||
stat.physical_memsize = stat.resident_memsize - stat.share_memsize;
|
||||
#else
|
||||
stat.ID = pID_;
|
||||
// HMODULE hMod;
|
||||
// DWORD cbNeeded;
|
||||
if (GetProcessMemoryInfo(PRIVATE->hProc, &PRIVATE->mem_cnt, sizeof(PRIVATE->mem_cnt)) != 0) {
|
||||
stat.physical_memsize = PRIVATE->mem_cnt.WorkingSetSize;
|
||||
}
|
||||
stat.priority = GetPriorityClass(PRIVATE->hProc);
|
||||
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pID_);
|
||||
int thcnt = 0;
|
||||
if (snap != 0) {
|
||||
THREADENTRY32 thread;
|
||||
thread.dwSize = sizeof(THREADENTRY32);
|
||||
if (Thread32First(snap, &thread) == TRUE) {
|
||||
if (thread.th32OwnerProcessID == DWORD(pID_))
|
||||
++thcnt;
|
||||
while (Thread32Next(snap, &thread) == TRUE) {
|
||||
if (thread.th32OwnerProcessID == DWORD(pID_))
|
||||
++thcnt;
|
||||
}
|
||||
}
|
||||
stat.threads = thcnt;
|
||||
CloseHandle(snap);
|
||||
}
|
||||
FILETIME ft0, ft1, ft_kernel, ft_user;
|
||||
double el_s = PRIVATE->tm.elapsed_s() * cpu_count / 100.;
|
||||
if (GetProcessTimes(PRIVATE->hProc, &ft0, &ft1, &ft_kernel, &ft_user) != 0) {
|
||||
PISystemTime tm_kernel_c = FILETIME2PISystemTime(ft_kernel);
|
||||
PISystemTime tm_user_c = FILETIME2PISystemTime(ft_user);
|
||||
if (cycle < 0) {
|
||||
PRIVATE->tm_kernel = tm_kernel_c;
|
||||
PRIVATE->tm_user = tm_user_c;
|
||||
}
|
||||
//cycle++;
|
||||
cycle = 0;
|
||||
if (el_s <= 0.) {
|
||||
stat.cpu_load_system = 0.f;
|
||||
stat.cpu_load_user = 0.f;
|
||||
} else {
|
||||
stat.cpu_load_system = (tm_kernel_c - PRIVATE->tm_kernel).toSeconds() / el_s;
|
||||
stat.cpu_load_user = (tm_user_c - PRIVATE->tm_user).toSeconds() / el_s;
|
||||
}
|
||||
PRIVATE->tm_kernel = tm_kernel_c;
|
||||
PRIVATE->tm_user = tm_user_c;
|
||||
} else {
|
||||
stat.cpu_load_system = 0.f;
|
||||
stat.cpu_load_user = 0.f;
|
||||
}
|
||||
PRIVATE->tm.reset();
|
||||
#endif
|
||||
|
||||
stat.cpu_load_system = piClampf(stat.cpu_load_system, 0.f, 100.f);
|
||||
stat.cpu_load_user = piClampf(stat.cpu_load_user, 0.f, 100.f);
|
||||
makeStrings();
|
||||
}
|
||||
|
||||
|
||||
void PISystemMonitor::makeStrings() {
|
||||
stat.physical_memsize_readable.setReadableSize(stat.physical_memsize);
|
||||
stat.resident_memsize_readable.setReadableSize(stat.resident_memsize);
|
||||
stat.share_memsize_readable.setReadableSize(stat.share_memsize);
|
||||
stat.virtual_memsize_readable.setReadableSize(stat.virtual_memsize);
|
||||
stat.data_memsize_readable.setReadableSize(stat.data_memsize);
|
||||
}
|
||||
75
src_main/system/pisystemmonitor.h
Executable file
75
src_main/system/pisystemmonitor.h
Executable file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Process resource monitor
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PISYSTEMMONITOR_H
|
||||
#define PISYSTEMMONITOR_H
|
||||
|
||||
#include "pithread.h"
|
||||
#include "piprocess.h"
|
||||
|
||||
class PIP_EXPORT PISystemMonitor: public PIThread
|
||||
{
|
||||
PIOBJECT_SUBCLASS(PISystemMonitor, PIThread)
|
||||
public:
|
||||
PISystemMonitor();
|
||||
~PISystemMonitor();
|
||||
|
||||
struct ProcessStats {
|
||||
ProcessStats();
|
||||
PIString exec_name;
|
||||
PIString state;
|
||||
int ID;
|
||||
int parent_ID;
|
||||
int group_ID;
|
||||
int session_ID;
|
||||
int priority;
|
||||
int threads;
|
||||
ulong physical_memsize;
|
||||
ulong resident_memsize;
|
||||
ulong share_memsize;
|
||||
ulong virtual_memsize;
|
||||
ulong data_memsize;
|
||||
PIString physical_memsize_readable;
|
||||
PIString resident_memsize_readable;
|
||||
PIString share_memsize_readable;
|
||||
PIString virtual_memsize_readable;
|
||||
PIString data_memsize_readable;
|
||||
float cpu_load_system;
|
||||
float cpu_load_user;
|
||||
};
|
||||
|
||||
bool startOnProcess(int pID);
|
||||
bool startOnSelf() {return startOnProcess(PIProcess::currentPID());}
|
||||
|
||||
const ProcessStats & statistic() const {return stat;}
|
||||
void setStatistic(const ProcessStats & s) {stat = s; makeStrings();}
|
||||
|
||||
private:
|
||||
void stop();
|
||||
void run();
|
||||
void makeStrings();
|
||||
|
||||
PIFile file, filem;
|
||||
ProcessStats stat;
|
||||
int pID_, page_size, cpu_count, cycle;
|
||||
PRIVATE_DECLARATION
|
||||
|
||||
};
|
||||
|
||||
#endif // PISYSTEMMONITOR_H
|
||||
42
src_main/system/pisystemtests.cpp
Executable file
42
src_main/system/pisystemtests.cpp
Executable file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
System tests results (see system_test folder)
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pisystemtests.h"
|
||||
#include "piconfig.h"
|
||||
|
||||
|
||||
namespace PISystemTests {
|
||||
long time_resolution_ns = 1;
|
||||
long time_elapsed_ns = 0;
|
||||
long usleep_offset_us = 60;
|
||||
|
||||
PISystemTestReader pisystestreader;
|
||||
|
||||
};
|
||||
|
||||
|
||||
PISystemTests::PISystemTestReader::PISystemTestReader() {
|
||||
#ifndef WINDOWS
|
||||
PIConfig conf(PIStringAscii("/etc/pip.conf"), PIIODevice::ReadOnly);
|
||||
//conf.setReopenEnabled(false);
|
||||
time_resolution_ns = conf.getValue(PIStringAscii("time_resolution_ns"), 1);
|
||||
time_elapsed_ns = conf.getValue(PIStringAscii("time_elapsed_ns"), 0);
|
||||
usleep_offset_us = conf.getValue(PIStringAscii("usleep_offset_us"), 60);
|
||||
#endif
|
||||
}
|
||||
39
src_main/system/pisystemtests.h
Executable file
39
src_main/system/pisystemtests.h
Executable file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
System tests results (see system_test folder)
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PISYSTEMTESTS_H
|
||||
#define PISYSTEMTESTS_H
|
||||
|
||||
#include "pibase.h"
|
||||
|
||||
namespace PISystemTests {
|
||||
PIP_EXPORT extern long time_resolution_ns;
|
||||
PIP_EXPORT extern long time_elapsed_ns;
|
||||
PIP_EXPORT extern long usleep_offset_us;
|
||||
|
||||
class PISystemTestReader {
|
||||
public:
|
||||
PISystemTestReader();
|
||||
};
|
||||
|
||||
extern PISystemTestReader pisystestreader;
|
||||
|
||||
};
|
||||
|
||||
#endif // PISYSTEMTESTS_H
|
||||
Reference in New Issue
Block a user