diff options
Diffstat (limited to 'src/Common/Backends')
-rw-r--r-- | src/Common/Backends/Makefile.am | 5 | ||||
-rw-r--r-- | src/Common/Backends/Makefile.in | 7 | ||||
-rw-r--r-- | src/Common/Backends/SystemBackendPosix.cpp | 134 | ||||
-rw-r--r-- | src/Common/Backends/SystemBackendPosix.h | 60 | ||||
-rw-r--r-- | src/Common/Backends/SystemBackendProc.cpp | 8 | ||||
-rw-r--r-- | src/Common/Backends/SystemBackendProc.h | 16 |
6 files changed, 216 insertions, 14 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(); }; } |