src/SystemProcess.h
branchv_0
changeset 27 532953173cd5
child 28 9172bd97ae99
equal deleted inserted replaced
26:1b14ef641c7b 27:532953173cd5
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2019 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 #pragma once
       
    18 
       
    19 #include <vector>
       
    20 #include <sstream>
       
    21 #include <fcntl.h>
       
    22 #include <unistd.h>
       
    23 #include <sys/wait.h>
       
    24 #include <ext/stdio_filebuf.h>
       
    25 
       
    26 #include <relpipe/writer/typedefs.h>
       
    27 #include <relpipe/cli/RelpipeCLIException.h>
       
    28 
       
    29 namespace relpipe {
       
    30 namespace in {
       
    31 namespace filesystem {
       
    32 
       
    33 /**
       
    34  * Simple wrapper for a system process (fork+exec) that captures and returns just the STDOUT.
       
    35  */
       
    36 class SystemProcess {
       
    37 private:
       
    38 	/**
       
    39 	 * the command + its arguments
       
    40 	 */
       
    41 	std::vector<std::string> commandLine;
       
    42 	int nullFile = -1;
       
    43 
       
    44 	/**
       
    45 	 * TODO: move to a common library (copied from the AWK module) 
       
    46 	 * @param args
       
    47 	 */
       
    48 	void execp(const std::vector<std::string>& args) {
       
    49 		const char** a = new const char*[args.size() + 1];
       
    50 		for (size_t i = 0; i < args.size(); i++) a[i] = args[i].c_str();
       
    51 		a[args.size()] = nullptr;
       
    52 
       
    53 		execvp(a[0], (char*const*) a);
       
    54 
       
    55 		delete[] a;
       
    56 		throw relpipe::cli::RelpipeCLIException(L"Unable to do execvp().", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception?
       
    57 	}
       
    58 
       
    59 	/**
       
    60 	 * TODO: move to a common library (copied from the AWK module) 
       
    61 	 * @param readerFD
       
    62 	 * @param writerFD
       
    63 	 */
       
    64 	void createPipe(int& readerFD, int& writerFD) {
       
    65 		int fds[2];
       
    66 		int result = pipe(fds);
       
    67 		readerFD = fds[0];
       
    68 		writerFD = fds[1];
       
    69 		if (result < 0) throw relpipe::cli::RelpipeCLIException(L"Unable to create a pipe.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception?
       
    70 	}
       
    71 
       
    72 	/**
       
    73 	 * TODO: move to a common library (copied from the AWK module) 
       
    74 	 */
       
    75 	void redirectFD(int oldfd, int newfd) {
       
    76 		int result = dup2(oldfd, newfd);
       
    77 		if (result < 0) throw relpipe::cli::RelpipeCLIException(L"Unable redirect FD.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception?
       
    78 	}
       
    79 
       
    80 	/**
       
    81 	 * TODO: move to a common library (copied from the AWK module) 
       
    82 	 */
       
    83 	void closeOrThrow(int fd) {
       
    84 		int error = close(fd);
       
    85 		if (error) throw relpipe::cli::RelpipeCLIException(L"Unable to close FD: " + to_wstring(fd) + L" from PID: " + to_wstring(getpid()), relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception?
       
    86 	}
       
    87 
       
    88 public:
       
    89 
       
    90 	SystemProcess(std::vector<std::string> commandLine) : commandLine(commandLine) {
       
    91 		nullFile = open("/dev/null", O_RDWR);
       
    92 	}
       
    93 
       
    94 	virtual ~SystemProcess() {
       
    95 		close(nullFile);
       
    96 	}
       
    97 
       
    98 	std::string execute() {
       
    99 
       
   100 		std::stringstream result;
       
   101 
       
   102 		int hashReaderFD;
       
   103 		int hashWriterFD;
       
   104 		createPipe(hashReaderFD, hashWriterFD);
       
   105 		
       
   106 		__pid_t hashPid = fork();
       
   107 
       
   108 		if (hashPid < 0) {
       
   109 			throw relpipe::cli::RelpipeCLIException(L"Unable to fork the hash process.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception?
       
   110 		} else if (hashPid == 0) {
       
   111 			// Child process
       
   112 			closeOrThrow(hashReaderFD);
       
   113 			redirectFD(nullFile, STDIN_FILENO);
       
   114 			redirectFD(nullFile, STDERR_FILENO);
       
   115 			redirectFD(hashWriterFD, STDOUT_FILENO);
       
   116 			execp(commandLine);
       
   117 		} else {
       
   118 			// Parent process
       
   119 			closeOrThrow(hashWriterFD);
       
   120 
       
   121 			__gnu_cxx::stdio_filebuf<char> hashReaderBuffer(hashReaderFD, std::ios::in);
       
   122 			std::istream hashReader(&hashReaderBuffer);
       
   123 			
       
   124 			for (char ch; hashReader.read(&ch, 1).good();) result.put(ch);
       
   125 			
       
   126 			int waitError;
       
   127 			__pid_t waitPID = wait(&waitError);
       
   128 			if (waitError) throw relpipe::cli::RelpipeCLIException(L"The child process returned an error exit code.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception?
       
   129 		}
       
   130 
       
   131 		return result.str();
       
   132 	}
       
   133 };
       
   134 
       
   135 }
       
   136 }
       
   137 }