summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2008-09-24 23:49:40 +0200
committerMatthias Schiffer <matthias@gamezock.de>2008-09-24 23:49:40 +0200
commit0bf9d3cac0ab1f6c53001dafbd1e217fc5973be9 (patch)
treea69f8ade894b6afa202b13d9c43e3c855db8efda /src
parent91753e0f4b2e512259c52e6258d250ef3332c053 (diff)
downloadmad-0bf9d3cac0ab1f6c53001dafbd1e217fc5973be9.tar
mad-0bf9d3cac0ab1f6c53001dafbd1e217fc5973be9.zip
SystemBackendPosix mit exec-Funktion hinzugef?gt
Diffstat (limited to 'src')
-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
4 files changed, 200 insertions, 6 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_ */