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