From e0de95864892d1e119f91936d8b1294542ab2316 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 26 Nov 2008 22:23:22 +0100 Subject: SystemBackendPosix ist jetzt ein Modul --- src/Common/Backends/Makefile.am | 4 +- src/Common/Backends/Makefile.in | 7 +- src/Common/Backends/SystemBackendPosix.cpp | 284 ----------------------------- src/Common/Backends/SystemBackendPosix.h | 89 --------- 4 files changed, 5 insertions(+), 379 deletions(-) delete mode 100644 src/Common/Backends/SystemBackendPosix.cpp delete mode 100644 src/Common/Backends/SystemBackendPosix.h (limited to 'src/Common/Backends') diff --git a/src/Common/Backends/Makefile.am b/src/Common/Backends/Makefile.am index 138e406..83e5b2f 100644 --- a/src/Common/Backends/Makefile.am +++ b/src/Common/Backends/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = libbackends.la -libbackends_la_SOURCES = SystemBackendPosix.cpp SystemBackendProc.cpp +libbackends_la_SOURCES = SystemBackendProc.cpp -noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendPosix.h SystemBackendProc.h +noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendProc.h diff --git a/src/Common/Backends/Makefile.in b/src/Common/Backends/Makefile.in index b07c25e..fd44c4d 100644 --- a/src/Common/Backends/Makefile.in +++ b/src/Common/Backends/Makefile.in @@ -49,7 +49,7 @@ CONFIG_HEADER = $(top_builddir)/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libbackends_la_LIBADD = -am_libbackends_la_OBJECTS = SystemBackendPosix.lo SystemBackendProc.lo +am_libbackends_la_OBJECTS = SystemBackendProc.lo libbackends_la_OBJECTS = $(am_libbackends_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/config/depcomp @@ -207,8 +207,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libbackends.la -libbackends_la_SOURCES = SystemBackendPosix.cpp SystemBackendProc.cpp -noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendPosix.h SystemBackendProc.h +libbackends_la_SOURCES = SystemBackendProc.cpp +noinst_HEADERS = ConsoleLogger.h FileLogger.h SystemBackendProc.h all: all-am .SUFFIXES: @@ -260,7 +260,6 @@ 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 deleted file mode 100644 index bff72db..0000000 --- a/src/Common/Backends/SystemBackendPosix.cpp +++ /dev/null @@ -1,284 +0,0 @@ -/* - * SystemBackendPosix.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * 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 . - */ - -#include "SystemBackendPosix.h" -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - -namespace Mad { -namespace Common { -namespace Backends { - -SystemBackendPosix SystemBackendPosix::backend; - -std::map > SystemBackendPosix::processes; -std::map > SystemBackendPosix::processesWithOutput; -std::map SystemBackendPosix::processesWOHandles; -std::map SystemBackendPosix::processesWOOutput; - - -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::fsInfoCallback(int, const std::string &output, const sigc::slot& > &callback) { - std::vector ret; - std::istringstream stream(output); - std::string str; - - std::getline(stream, str); // ignore first line - - while(!stream.eof()) { - std::getline(stream, str); - - char *fsName = new char[str.length()+1]; - char *mountedOn = new char[str.length()+1]; - - FSInfo info; - - if(std::sscanf(str.c_str(), "%s %lld %lld %lld %*d%% %s", fsName, &info.total, &info.used, &info.available, mountedOn) == 5) { - info.fsName = fsName; - info.mountedOn = mountedOn; - - ret.push_back(info); - } - - delete [] fsName; - delete [] mountedOn; - } - - callback(ret); -} - -bool SystemBackendPosix::fsInfo(const sigc::slot& > &callback) { - std::vector argv; - - argv.push_back("/bin/df"); - argv.push_back("-P"); - argv.push_back("-k"); - - return execWithOutput(sigc::bind(sigc::mem_fun(this, &SystemBackendPosix::fsInfoCallback), callback), "/bin/df", argv); -} - - -void SystemBackendPosix::childHandler(int) { - int status; - pid_t pid; - - // TODO Don't call callbacks directly - - while((pid = waitpid(-1, &status, WNOHANG)) > 0) { - std::map >::iterator it = processes.find(pid); - - if(it != processes.end()) { - it->second(status); - processes.erase(it); - } - else { - std::map >::iterator it2 = processesWithOutput.find(pid); - - if(it2 != processesWithOutput.end()) { - char buffer[1024]; - ssize_t n; - std::string &output = processesWOOutput[pid]; - int handle = processesWOHandles[pid]; - - while((n = read(handle, buffer, sizeof(buffer))) > 0) - output += std::string(buffer, n); - - Net::FdManager::get()->unregisterFd(handle); - close(handle); - - it2->second(status, output); - processesWithOutput.erase(it2); - processesWOHandles.erase(pid); - processesWOOutput.erase(pid); - } - } - } - - setChildHandler(); -} - -void SystemBackendPosix::outputHandler(short events, pid_t pid) { - char buf[1024]; - - if(events & POLLIN) { - sigset_t set, oldset; - sigemptyset(&set); - sigaddset(&set, SIGCHLD); - sigprocmask(SIG_BLOCK, &set, &oldset); - - ssize_t ret; - - std::map::iterator handle = processesWOHandles.find(pid); - if(handle == processesWOHandles.end()) - return; - - while((ret = read(handle->second, buf, sizeof(buf))) > 0) - processesWOOutput[pid] += std::string(buf, ret); - - sigprocmask(SIG_SETMASK, &oldset, 0); - } -} - -std::pair SystemBackendPosix::makeArgs(const std::string &filename, const std::vector &argv, const std::vector &env) { - 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; - } - - return std::make_pair(argvp, envp); -} - -void SystemBackendPosix::destroyArgs(std::pair args) { - for(char **p = args.first; *p != 0; ++p) - std::free(*p); - - delete [] args.first; - - if(args.second != environ) { - for(char **p = args.second; *p != 0; ++p) - std::free(*p); - - delete [] args.second; - } -} - -bool SystemBackendPosix::exec(const sigc::slot &resultHandler, const std::string &filename, const std::vector &argv, const std::vector &env) { - pid_t pid; - std::pair args = makeArgs(filename, argv, env); - - sigset_t set, oldset; - sigemptyset(&set); - sigaddset(&set, SIGCHLD); - sigprocmask(SIG_BLOCK, &set, &oldset); - - bool ret = (posix_spawnp(&pid, filename.c_str(), 0, 0, args.first, args.second) == 0); - - if(ret) - processes.insert(std::make_pair(pid, resultHandler)); - - sigprocmask(SIG_SETMASK, &oldset, 0); - - destroyArgs(args); - - return ret; -} - -bool SystemBackendPosix::execWithOutput(const sigc::slot &resultHandler, const std::string &filename, const std::vector &argv, const std::vector &env) { - pid_t pid; - std::pair args = makeArgs(filename, argv, env); - - int saveStdout = dup(STDOUT_FILENO); - int pipeHandles[2]; - - pipe(pipeHandles); - - fcntl(pipeHandles[0], F_SETFD, FD_CLOEXEC); - - int flags = fcntl(pipeHandles[0], F_GETFL, 0); - fcntl(pipeHandles[0], F_SETFL, flags | O_NONBLOCK); - - sigset_t set, oldset; - sigemptyset(&set); - sigaddset(&set, SIGCHLD); - sigprocmask(SIG_BLOCK, &set, &oldset); - - dup2(pipeHandles[1], STDOUT_FILENO); // set the new pipe as stdout - close(pipeHandles[1]); - - bool ret = (posix_spawnp(&pid, filename.c_str(), 0, 0, args.first, args.second) == 0); - - if(ret) { - processesWithOutput.insert(std::make_pair(pid, resultHandler)); - processesWOHandles.insert(std::make_pair(pid, pipeHandles[0])); - processesWOOutput.insert(std::make_pair(pid, std::string())); - - Net::FdManager::get()->registerFd(pipeHandles[0], sigc::bind(sigc::ptr_fun(&SystemBackendPosix::outputHandler), pid), POLLIN); - } - - dup2(saveStdout, STDOUT_FILENO); // restore old stdout - close(saveStdout); - - sigprocmask(SIG_SETMASK, &oldset, 0); - - destroyArgs(args); - - return ret; -} - -} -} -} diff --git a/src/Common/Backends/SystemBackendPosix.h b/src/Common/Backends/SystemBackendPosix.h deleted file mode 100644 index 59dd2d8..0000000 --- a/src/Common/Backends/SystemBackendPosix.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * SystemBackendPosix.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * 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 . - */ - -#ifndef MAD_COMMON_BACKENDS_SYSTEMBACKENDPOSIX_H_ -#define MAD_COMMON_BACKENDS_SYSTEMBACKENDPOSIX_H_ - -#include "../SystemBackend.h" - -#include -#include -#include - -#include -#include -#include - -namespace Mad { -namespace Common { -namespace Backends { - -class SystemBackendPosix : public SystemBackend { - private: - static SystemBackendPosix backend; - static std::map > processes; - - static std::map > processesWithOutput; - static std::map processesWOHandles; - static std::map processesWOOutput; - - static void setChildHandler(); - - static void childHandler(int); - - static void outputHandler(short events, pid_t pid); - - static std::pair makeArgs(const std::string &filename, const std::vector &argv, const std::vector &env); - static void destroyArgs(std::pair args); - - SystemBackendPosix() { - setChildHandler(); - } - - void fsInfoCallback(int, const std::string &output, const sigc::slot& > &callback); - - protected: - virtual bool fsInfo(const sigc::slot& > &callback); - - virtual bool doShutdown(const sigc::slot &callback) {return exec(sigc::hide(callback), "/sbin/halt");} - virtual bool doReboot(const sigc::slot &callback) {return exec(sigc::hide(callback), "/sbin/reboot");} - - public: - ~SystemBackendPosix(); - - static void registerBackend() { - SystemBackend::registerBackend(&backend); - } - - static void unregisterBackend() { - SystemBackend::unregisterBackend(&backend); - } - - static bool exec(const sigc::slot &resultHandler, const std::string &filename, const std::vector &argv = std::vector(), - const std::vector &env = std::vector()); - - static bool execWithOutput(const sigc::slot &resultHandler, const std::string &filename, const std::vector &argv = std::vector(), - const std::vector &env = std::vector()); -}; - -} -} -} - -#endif /* MAD_COMMON_BACKENDS_SYSTEMBACKENDPOSIX_H_ */ -- cgit v1.2.3