--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/SubProcess.cpp Sat Jan 11 00:56:51 2020 +0100
@@ -0,0 +1,186 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * 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, version 3 of the License.
+ *
+ * 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 <iostream>
+
+#include <sstream>
+#include <codecvt>
+#include <locale>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <ext/stdio_filebuf.h>
+#include <algorithm>
+
+#include "SubProcess.h"
+
+using namespace relpipe::writer;
+
+/**
+ * TODO: have a separate side process for forking new processes.
+ */
+class SubProcessImpl : public SubProcess {
+private:
+ __pid_t subPid;
+ std::istream subOutputReader;
+ std::ostream subInputWriter;
+ __gnu_cxx::stdio_filebuf<char> subOutputReaderBuffer;
+ __gnu_cxx::stdio_filebuf<char> subInputWriterBuffer;
+ static const char SEPARATOR = '\0';
+
+ std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses.
+
+ int readInt() {
+ return std::stoi(readString());
+ }
+
+ string_t readString() {
+ std::stringstream s;
+ for (char ch; subOutputReader.read(&ch, 1).good() && ch != SEPARATOR;) s.put(ch);
+ return convertor.from_bytes(s.str());
+ }
+
+ void write(string_t s) {
+ subInputWriter << convertor.to_bytes(s).c_str();
+ subInputWriter.put(SEPARATOR);
+ subInputWriter.flush();
+ if (subInputWriter.bad()) throw SubProcess::Exception(L"Unable to write to sub-process.");
+ }
+
+ void write(int i) {
+ write(std::to_wstring(i));
+ }
+
+public:
+
+ /**
+ * TODO: move to a common library (copied from the AWK module)
+ * @param args
+ */
+ static void execp(const std::vector<std::string>& args) {
+ const char** a = new const char*[args.size() + 1];
+ for (size_t i = 0; i < args.size(); i++) a[i] = args[i].c_str();
+ a[args.size()] = nullptr;
+
+ execvp(a[0], (char*const*) a);
+
+ delete[] a;
+ throw SubProcess::Exception(L"Unable to do execvp().");
+ }
+
+ /**
+ * TODO: move to a common library (copied from the AWK module)
+ * @param readerFD
+ * @param writerFD
+ */
+ static void createPipe(int& readerFD, int& writerFD) {
+ int fds[2];
+ int result = pipe(fds);
+ readerFD = fds[0];
+ writerFD = fds[1];
+ if (result < 0) throw SubProcess::Exception(L"Unable to create a pipe.");
+ }
+
+ /**
+ * TODO: move to a common library (copied from the AWK module)
+ */
+ static void redirectFD(int oldfd, int newfd) {
+ int result = dup2(oldfd, newfd);
+ if (result < 0) throw SubProcess::Exception(L"Unable redirect FD.");
+ }
+
+ /**
+ * TODO: move to a common library (copied from the AWK module)
+ */
+ static void closeOrThrow(int fd) {
+ int error = close(fd);
+ if (error) throw SubProcess::Exception(L"Unable to close FD: " + std::to_wstring(fd) + L" from PID: " + std::to_wstring(getpid()));
+ }
+
+ static SubProcess* createSubProcess(std::vector<string_t> commandLine, std::map<string_t, string_t> environment, bool dropErrorOutput) {
+ int subInputReaderFD;
+ int subInputWriterFD;
+ int subOutputReaderFD;
+ int subOutputWriterFD;
+
+ createPipe(subInputReaderFD, subInputWriterFD);
+ createPipe(subOutputReaderFD, subOutputWriterFD);
+
+ __pid_t subPid = fork();
+
+ if (subPid < 0) {
+ throw SubProcess::Exception(L"Unable to fork the hash process.");
+ } else if (subPid == 0) {
+ // Child process
+ redirectFD(subInputReaderFD, STDIN_FILENO);
+ redirectFD(subOutputWriterFD, STDOUT_FILENO);
+ closeOrThrow(subInputWriterFD);
+ closeOrThrow(subOutputReaderFD);
+ if (dropErrorOutput) redirectFD(open("/dev/null", O_RDWR), STDERR_FILENO);
+
+ std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses.
+ for (auto const & entry : environment) setenv(convertor.to_bytes(entry.first).c_str(), convertor.to_bytes(entry.second).c_str(), true);
+ std::vector<std::string> commandLineRaw;
+ for (string_t s : commandLine) commandLineRaw.push_back(convertor.to_bytes(s));
+ execp(commandLineRaw);
+ throw SubProcess::Exception(L"Unexpected exception after execp(commandLineRaw)"); // will never happen, look inside the method above (throws exception)
+ } else {
+ // Parent process
+ closeOrThrow(subInputReaderFD);
+ closeOrThrow(subOutputWriterFD);
+ return new SubProcessImpl(subPid, subInputWriterFD, subOutputReaderFD);
+ }
+ }
+
+ SubProcessImpl(__pid_t subPid, int subInputWriterFD, int subOutputReaderFD) :
+ subPid(subPid),
+ subOutputReaderBuffer(__gnu_cxx::stdio_filebuf<char>(subOutputReaderFD, std::ios::in)),
+ subInputWriterBuffer(__gnu_cxx::stdio_filebuf<char>(subInputWriterFD, std::ios::out)),
+ subOutputReader(&subOutputReaderBuffer),
+ subInputWriter(&subInputWriterBuffer) {
+ }
+
+ virtual ~SubProcessImpl() {
+ }
+
+ SubProcess::Message read() {
+ Message m;
+ m.code = readInt();
+ int count = readInt();
+ for (int i = 0; i < count; i++) m.parameters.push_back(readString());
+ return m;
+ }
+
+ void write(Message m) {
+ write(m.code);
+ write(m.parameters.size());
+ for (auto p : m.parameters) write(p);
+ }
+
+ int wait() {
+ closeOrThrow(subInputWriterBuffer.fd());
+ closeOrThrow(subOutputReaderBuffer.fd());
+ int status = -1;
+ ::waitpid(subPid, &status, 0);
+ return status;
+ }
+
+};
+
+SubProcess* SubProcess::create(std::vector<string_t> commandLine, std::map<string_t, string_t> environment, bool dropErrorOutput) {
+ return SubProcessImpl::createSubProcess(commandLine, environment, dropErrorOutput);
+}