summaryrefslogtreecommitdiffstats
path: root/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common')
-rw-r--r--src/Common/Backends/Makefile.am5
-rw-r--r--src/Common/Backends/Makefile.in7
-rw-r--r--src/Common/Backends/SystemBackendPosix.cpp134
-rw-r--r--src/Common/Backends/SystemBackendPosix.h60
-rw-r--r--src/Common/Backends/SystemBackendProc.cpp8
-rw-r--r--src/Common/Backends/SystemBackendProc.h16
-rw-r--r--src/Common/ConfigManager.cpp2
-rw-r--r--src/Common/RequestHandlers/StatusRequestHandler.cpp6
-rw-r--r--src/Common/SystemBackend.cpp30
-rw-r--r--src/Common/SystemBackend.h54
10 files changed, 284 insertions, 38 deletions
diff --git a/src/Common/Backends/Makefile.am b/src/Common/Backends/Makefile.am
index 26ec0f0..138e406 100644
--- a/src/Common/Backends/Makefile.am
+++ b/src/Common/Backends/Makefile.am
@@ -1,5 +1,4 @@
noinst_LTLIBRARIES = libbackends.la
-libbackends_la_SOURCES = SystemBackendProc.cpp
-
-noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendProc.h
+libbackends_la_SOURCES = SystemBackendPosix.cpp SystemBackendProc.cpp
+noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendPosix.h SystemBackendProc.h
diff --git a/src/Common/Backends/Makefile.in b/src/Common/Backends/Makefile.in
index 4a48c24..679360e 100644
--- a/src/Common/Backends/Makefile.in
+++ b/src/Common/Backends/Makefile.in
@@ -48,7 +48,7 @@ CONFIG_HEADER = $(top_builddir)/src/config.h
CONFIG_CLEAN_FILES =
LTLIBRARIES = $(noinst_LTLIBRARIES)
libbackends_la_LIBADD =
-am_libbackends_la_OBJECTS = SystemBackendProc.lo
+am_libbackends_la_OBJECTS = SystemBackendPosix.lo SystemBackendProc.lo
libbackends_la_OBJECTS = $(am_libbackends_la_OBJECTS)
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src
depcomp = $(SHELL) $(top_srcdir)/depcomp
@@ -187,8 +187,8 @@ target_alias = @target_alias@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
noinst_LTLIBRARIES = libbackends.la
-libbackends_la_SOURCES = SystemBackendProc.cpp
-noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendProc.h
+libbackends_la_SOURCES = SystemBackendPosix.cpp SystemBackendProc.cpp
+noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendPosix.h SystemBackendProc.h
all: all-am
.SUFFIXES:
@@ -240,6 +240,7 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SystemBackendPosix.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SystemBackendProc.Plo@am__quote@
.cpp.o:
diff --git a/src/Common/Backends/SystemBackendPosix.cpp b/src/Common/Backends/SystemBackendPosix.cpp
new file mode 100644
index 0000000..b1e088d
--- /dev/null
+++ b/src/Common/Backends/SystemBackendPosix.cpp
@@ -0,0 +1,134 @@
+/*
+ * SystemBackendPosix.cpp
+ *
+ * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * 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 "SystemBackendPosix.h"
+
+#include <cstdlib>
+#include <cstring>
+#include <signal.h>
+#include <spawn.h>
+#include <sys/wait.h>
+
+namespace Mad {
+namespace Common {
+namespace Backends {
+
+SystemBackendPosix SystemBackendPosix::backend;
+std::map<pid_t, sigc::slot<void, int> > SystemBackendPosix::processes;
+
+
+void SystemBackendPosix::setChildHandler() {
+ struct sigaction action;
+
+ action.sa_handler = childHandler;
+ sigemptyset(&action.sa_mask);
+ action.sa_flags = 0;
+
+ sigaction(SIGCHLD, &action, 0);
+}
+
+SystemBackendPosix::~SystemBackendPosix() {
+ struct sigaction action;
+
+ action.sa_handler = SIG_DFL;
+ sigemptyset(&action.sa_mask);
+ action.sa_flags = 0;
+
+ sigaction(SIGCHLD, &action, 0);
+}
+
+void SystemBackendPosix::childHandler(int) {
+ int status;
+ pid_t pid;
+
+ while((pid = waitpid(-1, &status, WNOHANG)) > 0) {
+ std::map<pid_t, sigc::slot<void, int> >::iterator it = processes.find(pid);
+
+ if(it != processes.end()) {
+ it->second(status);
+ processes.erase(it);
+ }
+ }
+
+ setChildHandler();
+}
+
+bool SystemBackendPosix::exec(sigc::slot<void, int> resultHandler, const std::string &filename, const std::vector<std::string> &argv, const std::vector<std::string> &env) {
+ pid_t pid;
+ char **argvp, **envp;
+
+ if(argv.empty()) {
+ argvp = new char*[2];
+
+ argvp[0] = strdup(filename.c_str());
+ argvp[1] = 0;
+ }
+ else {
+ argvp = new char*[argv.size() + 1];
+
+ for(size_t s = 0; s < argv.size(); ++s) {
+ argvp[s] = strdup(argv[s].c_str());
+ }
+
+ argvp[argv.size()] = 0;
+ }
+
+ if(env.empty()) {
+ envp = environ;
+ }
+ else {
+ envp = new char*[env.size() + 1];
+
+ for(size_t s = 0; s < env.size(); ++s) {
+ envp[0] = strdup(env[s].c_str());
+ }
+
+ envp[env.size()] = 0;
+ }
+
+ sigset_t set, oldset;
+ sigemptyset(&set);
+ sigaddset(&set, SIGCHLD);
+ sigprocmask(SIG_BLOCK, &set, &oldset);
+
+ bool ret = (posix_spawnp(&pid, filename.c_str(), 0, 0, argvp, envp) == 0);
+
+ if(ret)
+ processes.insert(std::make_pair(pid, resultHandler));
+
+ sigprocmask(SIG_SETMASK, &oldset, 0);
+
+ for(char **p = argvp; *p != 0; ++p)
+ std::free(*p);
+
+ delete [] argvp;
+
+ if(envp != environ) {
+ for(char **p = envp; *p != 0; ++p)
+ std::free(*p);
+
+ delete [] envp;
+ }
+
+ return ret;
+}
+
+}
+}
+}
diff --git a/src/Common/Backends/SystemBackendPosix.h b/src/Common/Backends/SystemBackendPosix.h
new file mode 100644
index 0000000..aa2b9be
--- /dev/null
+++ b/src/Common/Backends/SystemBackendPosix.h
@@ -0,0 +1,60 @@
+/*
+ * SystemBackendPosix.h
+ *
+ * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * 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 MAD_COMMON_BACKENDS_SYSTEMBACKENDPOSIX_H_
+#define MAD_COMMON_BACKENDS_SYSTEMBACKENDPOSIX_H_
+
+#include "../SystemBackend.h"
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include <sys/types.h>
+#include <sigc++/signal.h>
+
+namespace Mad {
+namespace Common {
+namespace Backends {
+
+class SystemBackendPosix {
+ private:
+ static SystemBackendPosix backend;
+ static std::map<pid_t, sigc::slot<void, int> > processes;
+
+ static void setChildHandler();
+
+ static void childHandler(int);
+
+ SystemBackendPosix() {
+ setChildHandler();
+ }
+
+ public:
+ ~SystemBackendPosix();
+
+ static bool exec(sigc::slot<void, int> resultHandler, const std::string &filename, const std::vector<std::string> &argv = std::vector<std::string>(),
+ const std::vector<std::string> &env = std::vector<std::string>());
+};
+
+}
+}
+}
+
+#endif /* MAD_COMMON_BACKENDS_SYSTEMBACKENDPOSIX_H_ */
diff --git a/src/Common/Backends/SystemBackendProc.cpp b/src/Common/Backends/SystemBackendProc.cpp
index e8c7357..37bfaf3 100644
--- a/src/Common/Backends/SystemBackendProc.cpp
+++ b/src/Common/Backends/SystemBackendProc.cpp
@@ -26,7 +26,9 @@ namespace Mad {
namespace Common {
namespace Backends {
-SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() {
+SystemBackendProc SystemBackendProc::backend;
+
+SystemBackend::UptimeInfo SystemBackendProc::uptimeInfo() {
UptimeInfo uptime = {0, 0};
uptimeFile.seekg(0, std::ios::beg);
@@ -50,7 +52,7 @@ SystemBackend::UptimeInfo SystemBackendProc::getUptimeInfo() {
return uptime;
}
-SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() {
+SystemBackend::MemoryInfo SystemBackendProc::memoryInfo() {
MemoryInfo memInfo = {0, 0, 0, 0};
meminfoFile.seekg(0, std::ios::beg);
@@ -86,7 +88,7 @@ SystemBackend::MemoryInfo SystemBackendProc::getMemoryInfo() {
return memInfo;
}
-SystemBackend::LoadInfo SystemBackendProc::getLoadInfo() {
+SystemBackend::LoadInfo SystemBackendProc::loadInfo() {
LoadInfo loadInfo = {0, 0, 0, 0, 0};
loadFile.seekg(0, std::ios::beg);
diff --git a/src/Common/Backends/SystemBackendProc.h b/src/Common/Backends/SystemBackendProc.h
index 8caa080..619c996 100644
--- a/src/Common/Backends/SystemBackendProc.h
+++ b/src/Common/Backends/SystemBackendProc.h
@@ -30,6 +30,8 @@ namespace Backends {
class SystemBackendProc : public SystemBackend {
private:
+ static SystemBackendProc backend;
+
std::ifstream uptimeFile;
std::ifstream meminfoFile;
std::ifstream loadFile;
@@ -37,13 +39,17 @@ class SystemBackendProc : public SystemBackend {
SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {}
public:
- static void useBackend() {
- setBackend(std::auto_ptr<SystemBackend>(new SystemBackendProc()));
+ static void registerBackend() {
+ SystemBackend::registerBackend(&backend);
+ }
+
+ static void unregisterBackend() {
+ SystemBackend::unregisterBackend(&backend);
}
- virtual UptimeInfo getUptimeInfo();
- virtual MemoryInfo getMemoryInfo();
- virtual LoadInfo getLoadInfo();
+ virtual UptimeInfo uptimeInfo();
+ virtual MemoryInfo memoryInfo();
+ virtual LoadInfo loadInfo();
};
}
diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp
index 76227b4..c8aea47 100644
--- a/src/Common/ConfigManager.cpp
+++ b/src/Common/ConfigManager.cpp
@@ -96,7 +96,7 @@ bool ConfigManager::loadFile(const std::string &filename) {
}
void ConfigManager::initBackends() {
- Backends::SystemBackendProc::useBackend();
+ Backends::SystemBackendProc::registerBackend();
}
}
diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp
index 6280ab3..ad37964 100644
--- a/src/Common/RequestHandlers/StatusRequestHandler.cpp
+++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp
@@ -39,9 +39,9 @@ void StatusRequestHandler::handlePacket(Net::Connection *connection, const Net::
// TODO Require authentication
- SystemBackend::UptimeInfo uptimeInfo = SystemBackend::getBackend()->getUptimeInfo();
- SystemBackend::MemoryInfo memInfo = SystemBackend::getBackend()->getMemoryInfo();
- SystemBackend::LoadInfo loadInfo = SystemBackend::getBackend()->getLoadInfo();
+ SystemBackend::UptimeInfo uptimeInfo = SystemBackend::getUptimeInfo();
+ SystemBackend::MemoryInfo memInfo = SystemBackend::getMemoryInfo();
+ SystemBackend::LoadInfo loadInfo = SystemBackend::getLoadInfo();
connection->send(Net::Packets::HostStatusPacket(Net::Packet::OK, packet.getRequestId(), uptimeInfo.uptime, uptimeInfo.idleTime,
memInfo.totalMem, memInfo.freeMem, memInfo.totalSwap, memInfo.freeSwap, loadInfo.currentLoad, loadInfo.nProcesses, loadInfo.loadAvg1, loadInfo.loadAvg5, loadInfo.loadAvg15));
diff --git a/src/Common/SystemBackend.cpp b/src/Common/SystemBackend.cpp
index c9be525..0d4b2b7 100644
--- a/src/Common/SystemBackend.cpp
+++ b/src/Common/SystemBackend.cpp
@@ -22,7 +22,35 @@
namespace Mad {
namespace Common {
-std::auto_ptr<SystemBackend> SystemBackend::backend(new SystemBackend());
+std::set<SystemBackend*, SystemBackend::Compare> SystemBackend::backends;
+
+
+SystemBackend::UptimeInfo SystemBackend::getUptimeInfo() {
+ UptimeInfo ret = {0, 0};
+
+ for(std::set<SystemBackend*>::iterator backend = backends.begin(); backend != backends.end() && ret.uptime == 0; ++backend)
+ ret = (*backend)->uptimeInfo();
+
+ return ret;
+}
+
+SystemBackend::MemoryInfo SystemBackend::getMemoryInfo() {
+ MemoryInfo ret = {0, 0, 0, 0};
+
+ for(std::set<SystemBackend*>::iterator backend = backends.begin(); backend != backends.end() && ret.totalMem == 0; ++backend)
+ ret = (*backend)->memoryInfo();
+
+ return ret;
+}
+
+SystemBackend::LoadInfo SystemBackend::getLoadInfo() {
+ LoadInfo ret = {0, 0, 0, 0, 0};
+
+ for(std::set<SystemBackend*>::iterator backend = backends.begin(); backend != backends.end() && ret.currentLoad == 0 && ret.loadAvg1 == 0 && ret.nProcesses == 0; ++backend)
+ ret = (*backend)->loadInfo();
+
+ return ret;
+}
}
}
diff --git a/src/Common/SystemBackend.h b/src/Common/SystemBackend.h
index 8ba1e7d..c531224 100644
--- a/src/Common/SystemBackend.h
+++ b/src/Common/SystemBackend.h
@@ -20,24 +20,12 @@
#ifndef MAD_COMMON_SYSTEMBACKEND_H_
#define MAD_COMMON_SYSTEMBACKEND_H_
-#include <stdint.h>
-#include <string>
-#include <memory>
+#include <set>
namespace Mad {
namespace Common {
class SystemBackend {
- private:
- static std::auto_ptr<SystemBackend> backend;
-
- protected:
- SystemBackend() {}
-
- static void setBackend(std::auto_ptr<SystemBackend> backend0) {
- backend = backend0;
- }
-
public:
struct UptimeInfo {
unsigned long uptime;
@@ -59,26 +47,54 @@ class SystemBackend {
float loadAvg15;
};
- virtual ~SystemBackend() {}
+ private:
+ struct Compare {
+ bool operator() (const SystemBackend *b1, const SystemBackend *b2) {
+ if(b1->getPriority() == b2->getPriority())
+ return (b1 > b2);
+ else
+ return (b1->getPriority() > b2->getPriority());
+ }
+ };
+
+ static std::set<SystemBackend*, Compare> backends;
- virtual UptimeInfo getUptimeInfo() {
+ protected:
+ SystemBackend() {}
+
+ static void registerBackend(SystemBackend *backend) {
+ backends.insert(backend);
+ }
+
+ static void unregisterBackend(SystemBackend *backend) {
+ backends.erase(backend);
+ }
+
+ virtual UptimeInfo uptimeInfo() {
UptimeInfo ret = {0, 0};
return ret;
}
- virtual MemoryInfo getMemoryInfo() {
+ virtual MemoryInfo memoryInfo() {
MemoryInfo ret = {0, 0, 0, 0};
return ret;
}
- virtual LoadInfo getLoadInfo() {
+ virtual LoadInfo loadInfo() {
LoadInfo ret = {0, 0, 0, 0, 0};
return ret;
}
- static SystemBackend *getBackend() {
- return backend.get();
+ virtual int getPriority() const {
+ return 0;
}
+
+ public:
+ virtual ~SystemBackend() {}
+
+ static UptimeInfo getUptimeInfo();
+ static MemoryInfo getMemoryInfo();
+ static LoadInfo getLoadInfo();
};
}