src/SubProcess.cpp
branchv_0
changeset 29 6f15f18d2abf
parent 28 9172bd97ae99
child 30 56409232e1a1
equal deleted inserted replaced
28:9172bd97ae99 29:6f15f18d2abf
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #include <iostream>
       
    19 
       
    20 #include <sstream>
       
    21 #include <codecvt>
       
    22 #include <locale>
       
    23 #include <fcntl.h>
       
    24 #include <unistd.h>
       
    25 #include <sys/wait.h>
       
    26 #include <ext/stdio_filebuf.h>
       
    27 #include <algorithm>
       
    28 
       
    29 #include "SubProcess.h"
       
    30 
       
    31 using namespace relpipe::writer;
       
    32 
       
    33 /**
       
    34  * TODO: have a separate side process for forking new processes.
       
    35  */
       
    36 class SubProcessImpl : public SubProcess {
       
    37 private:
       
    38 	__pid_t subPid;
       
    39 	std::istream subOutputReader;
       
    40 	std::ostream subInputWriter;
       
    41 	__gnu_cxx::stdio_filebuf<char> subOutputReaderBuffer;
       
    42 	__gnu_cxx::stdio_filebuf<char> subInputWriterBuffer;
       
    43 	static const char SEPARATOR = '\0';
       
    44 
       
    45 	std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses.
       
    46 
       
    47 	int readInt() {
       
    48 		return std::stoi(readString());
       
    49 	}
       
    50 
       
    51 	string_t readString() {
       
    52 		std::stringstream s;
       
    53 		for (char ch; subOutputReader.read(&ch, 1).good() && ch != SEPARATOR;) s.put(ch);
       
    54 		return convertor.from_bytes(s.str());
       
    55 	}
       
    56 
       
    57 	void write(string_t s) {
       
    58 		subInputWriter << convertor.to_bytes(s).c_str();
       
    59 		subInputWriter.put(SEPARATOR);
       
    60 		subInputWriter.flush();
       
    61 		if (subInputWriter.bad()) throw SubProcess::Exception(L"Unable to write to sub-process.");
       
    62 	}
       
    63 
       
    64 	void write(int i) {
       
    65 		write(std::to_wstring(i));
       
    66 	}
       
    67 
       
    68 public:
       
    69 
       
    70 	/**
       
    71 	 * TODO: move to a common library (copied from the AWK module) 
       
    72 	 * @param args
       
    73 	 */
       
    74 	static void execp(const std::vector<std::string>& args) {
       
    75 		const char** a = new const char*[args.size() + 1];
       
    76 		for (size_t i = 0; i < args.size(); i++) a[i] = args[i].c_str();
       
    77 		a[args.size()] = nullptr;
       
    78 
       
    79 		execvp(a[0], (char*const*) a);
       
    80 
       
    81 		delete[] a;
       
    82 		throw SubProcess::Exception(L"Unable to do execvp().");
       
    83 	}
       
    84 
       
    85 	/**
       
    86 	 * TODO: move to a common library (copied from the AWK module) 
       
    87 	 * @param readerFD
       
    88 	 * @param writerFD
       
    89 	 */
       
    90 	static void createPipe(int& readerFD, int& writerFD) {
       
    91 		int fds[2];
       
    92 		int result = pipe(fds);
       
    93 		readerFD = fds[0];
       
    94 		writerFD = fds[1];
       
    95 		if (result < 0) throw SubProcess::Exception(L"Unable to create a pipe.");
       
    96 	}
       
    97 
       
    98 	/**
       
    99 	 * TODO: move to a common library (copied from the AWK module) 
       
   100 	 */
       
   101 	static void redirectFD(int oldfd, int newfd) {
       
   102 		int result = dup2(oldfd, newfd);
       
   103 		if (result < 0) throw SubProcess::Exception(L"Unable redirect FD.");
       
   104 	}
       
   105 
       
   106 	/**
       
   107 	 * TODO: move to a common library (copied from the AWK module) 
       
   108 	 */
       
   109 	static void closeOrThrow(int fd) {
       
   110 		int error = close(fd);
       
   111 		if (error) throw SubProcess::Exception(L"Unable to close FD: " + std::to_wstring(fd) + L" from PID: " + std::to_wstring(getpid()));
       
   112 	}
       
   113 
       
   114 	static SubProcess* createSubProcess(std::vector<string_t> commandLine, std::map<string_t, string_t> environment, bool dropErrorOutput) {
       
   115 		int subInputReaderFD;
       
   116 		int subInputWriterFD;
       
   117 		int subOutputReaderFD;
       
   118 		int subOutputWriterFD;
       
   119 
       
   120 		createPipe(subInputReaderFD, subInputWriterFD);
       
   121 		createPipe(subOutputReaderFD, subOutputWriterFD);
       
   122 
       
   123 		__pid_t subPid = fork();
       
   124 
       
   125 		if (subPid < 0) {
       
   126 			throw SubProcess::Exception(L"Unable to fork the hash process.");
       
   127 		} else if (subPid == 0) {
       
   128 			// Child process
       
   129 			redirectFD(subInputReaderFD, STDIN_FILENO);
       
   130 			redirectFD(subOutputWriterFD, STDOUT_FILENO);
       
   131 			closeOrThrow(subInputWriterFD);
       
   132 			closeOrThrow(subOutputReaderFD);
       
   133 			if (dropErrorOutput) redirectFD(open("/dev/null", O_RDWR), STDERR_FILENO);
       
   134 
       
   135 			std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses.
       
   136 			for (auto const & entry : environment) setenv(convertor.to_bytes(entry.first).c_str(), convertor.to_bytes(entry.second).c_str(), true);
       
   137 			std::vector<std::string> commandLineRaw;
       
   138 			for (string_t s : commandLine) commandLineRaw.push_back(convertor.to_bytes(s));
       
   139 			execp(commandLineRaw);
       
   140 			throw SubProcess::Exception(L"Unexpected exception after execp(commandLineRaw)"); // will never happen, look inside the method above (throws exception)
       
   141 		} else {
       
   142 			// Parent process
       
   143 			closeOrThrow(subInputReaderFD);
       
   144 			closeOrThrow(subOutputWriterFD);
       
   145 			return new SubProcessImpl(subPid, subInputWriterFD, subOutputReaderFD);
       
   146 		}
       
   147 	}
       
   148 
       
   149 	SubProcessImpl(__pid_t subPid, int subInputWriterFD, int subOutputReaderFD) :
       
   150 	subPid(subPid),
       
   151 	subOutputReaderBuffer(__gnu_cxx::stdio_filebuf<char>(subOutputReaderFD, std::ios::in)),
       
   152 	subInputWriterBuffer(__gnu_cxx::stdio_filebuf<char>(subInputWriterFD, std::ios::out)),
       
   153 	subOutputReader(&subOutputReaderBuffer),
       
   154 	subInputWriter(&subInputWriterBuffer) {
       
   155 	}
       
   156 
       
   157 	virtual ~SubProcessImpl() {
       
   158 	}
       
   159 
       
   160 	SubProcess::Message read() {
       
   161 		Message m;
       
   162 		m.code = readInt();
       
   163 		int count = readInt();
       
   164 		for (int i = 0; i < count; i++) m.parameters.push_back(readString());
       
   165 		return m;
       
   166 	}
       
   167 
       
   168 	void write(Message m) {
       
   169 		write(m.code);
       
   170 		write(m.parameters.size());
       
   171 		for (auto p : m.parameters) write(p);
       
   172 	}
       
   173 
       
   174 	int wait() {
       
   175 		closeOrThrow(subInputWriterBuffer.fd());
       
   176 		closeOrThrow(subOutputReaderBuffer.fd());
       
   177 		int status = -1;
       
   178 		::waitpid(subPid, &status, 0);
       
   179 		return status;
       
   180 	}
       
   181 
       
   182 };
       
   183 
       
   184 SubProcess* SubProcess::create(std::vector<string_t> commandLine, std::map<string_t, string_t> environment, bool dropErrorOutput) {
       
   185 	return SubProcessImpl::createSubProcess(commandLine, environment, dropErrorOutput);
       
   186 }