From 0bf9d3cac0ab1f6c53001dafbd1e217fc5973be9 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 24 Sep 2008 23:49:40 +0200 Subject: SystemBackendPosix mit exec-Funktion hinzugef?gt --- src/Common/Backends/Makefile.am | 5 +- src/Common/Backends/Makefile.in | 7 +- src/Common/Backends/SystemBackendPosix.cpp | 134 +++++++++++++++++++++++++++++ src/Common/Backends/SystemBackendPosix.h | 60 +++++++++++++ 4 files changed, 200 insertions(+), 6 deletions(-) create mode 100644 src/Common/Backends/SystemBackendPosix.cpp create mode 100644 src/Common/Backends/SystemBackendPosix.h 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 + * + * 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 + +namespace Mad { +namespace Common { +namespace Backends { + +SystemBackendPosix SystemBackendPosix::backend; +std::map > 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 >::iterator it = processes.find(pid); + + if(it != processes.end()) { + it->second(status); + processes.erase(it); + } + } + + setChildHandler(); +} + +bool SystemBackendPosix::exec(sigc::slot resultHandler, const std::string &filename, const std::vector &argv, const std::vector &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 + * + * 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 + +namespace Mad { +namespace Common { +namespace Backends { + +class SystemBackendPosix { + private: + static SystemBackendPosix backend; + static std::map > processes; + + static void setChildHandler(); + + static void childHandler(int); + + SystemBackendPosix() { + setChildHandler(); + } + + public: + ~SystemBackendPosix(); + + static bool exec(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