-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileExecuting.h
More file actions
286 lines (227 loc) · 6.88 KB
/
FileExecuting.h
File metadata and controls
286 lines (227 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
This file is part of the DSP-Crowd project
https://www.dsp-crowd.com
Author(s):
- Johannes Natter, office@dsp-crowd.com
File created on 25.03.2023
Copyright (C) 2023, Johannes Natter
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 FILE_EXECUTING_H
#define FILE_EXECUTING_H
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <unistd.h>
#include <signal.h>
#include "Transfering.h"
#define dTimeoutExecDefault 5000
typedef std::vector<const char *> VecConstChar;
typedef VecConstChar fec; // FileExecuting() Command
struct FePairFd
{
int fdRead;
int fdWrite;
};
struct FeNodeBuffer
{
FeNodeBuffer()
: pSrc(NULL)
, pDest(NULL)
, len(0)
, processed(0)
, autoFree(false)
{}
FeNodeBuffer(const char *p1, char *p2, size_t l, bool af)
: pSrc(p1)
, pDest(p2)
, len(l)
, processed(0)
, autoFree(af)
{}
const char *pSrc;
char *pDest;
size_t len;
size_t processed;
bool autoFree;
};
struct FeFileDescSetting
{
FeFileDescSetting()
: fd(-1)
, autoClose(false)
{}
FeFileDescSetting(int f, bool ac)
: fd(f)
, autoClose(ac)
{}
int fd;
bool autoClose;
};
struct FeNode
{
FePairFd pipe;
std::list<FeNodeBuffer> lstBuffers;
std::list<std::string *> lstStrings;
std::list<FeFileDescSetting> lstFds;
std::list<Transfering *> lstTransfers;
bool manualEnabled;
bool autoEnabled;
bool autoDone;
bool redirect;
};
struct FeResult
{
bool childStopped;
bool childTerminated;
bool childTerminatedBySig;
bool coreDumped;
int idChild;
int codeSig;
int codeRet;
};
// 1. https://man7.org/linux/man-pages/man2/pipe.2.html
// 2. https://man7.org/linux/man-pages/man2/fork.2.html
// 3. https://man7.org/linux/man-pages/man2/dup.2.html
// 4. https://man7.org/linux/man-pages/man3/exec.3.html
class FileExecuting : public Transfering
{
public:
static FileExecuting *create()
{
return new dNoThrow FileExecuting;
}
// routing & change container
FileExecuting &msTimeoutSet(uint32_t msTimeout);
FileExecuting &sourceEnable();
FileExecuting &sourceSet(const char *pSrc, size_t len = 0, bool autoFree = false);
FileExecuting &sourceSet(const std::string *pStr);
FileExecuting &sourceSet(int fd, bool autoClose = false);
FileExecuting &sourceSet(Transfering *pTrans);
// add command
FileExecuting &cmdAdd(const VecConstChar &argv);
FileExecuting &cmdAdd(const std::string &cmd, const VecConstChar &argv)
{ return intCmdAdd(cmd, argv); }
FileExecuting &cmdAdd(const std::string &cmd, int argc, char *argv[])
{ return intCmdAdd(cmd, VecConstChar(argv, argv + argc)); }
// - https://en.cppreference.com/w/cpp/language/operators
// - https://en.cppreference.com/w/cpp/language/operator_assignment
// - https://en.cppreference.com/w/cpp/language/operator_precedence
FileExecuting &operator|=(const VecConstChar &argv) { return cmdAdd(argv); }
FileExecuting &operator|(const VecConstChar &argv) { return cmdAdd(argv); }
// routing & change internal
FileExecuting &envSet(const VecConstChar &envv, bool dropOld = true);
FileExecuting &sinkEnable(int fdSel = STDOUT_FILENO);
FileExecuting &sinkAdd(char *pDest, size_t len, int fdSel = STDOUT_FILENO);
FileExecuting &sinkAdd(std::string *pStr, int fdSel = STDOUT_FILENO);
FileExecuting &sinkAdd(int fd, bool autoClose = false, int fdSel = STDOUT_FILENO);
FileExecuting &sinkAdd(Transfering *pTrans, int fdSel = STDOUT_FILENO);
FileExecuting &errRedirect();
// getters & child control
size_t numCommands() const
{ return mLstExec.size(); }
int sigSend(int sig, ssize_t idx = -1) const;
bool childStopped(ssize_t idx = -1, bool isAnd = false)
{ return boolRet(idx, 0, isAnd); }
bool childTerminated(ssize_t idx = -1, bool isAnd = true)
{ return boolRet(idx, 1, isAnd); }
bool childTerminatedBySig(ssize_t idx = -1, bool isAnd = false)
{ return boolRet(idx, 2, isAnd); }
bool coreDumped(ssize_t idx = -1, bool isAnd = false)
{ return boolRet(idx, 3, isAnd); }
int idChild(ssize_t idx = -1)
{ return intRet(idx, 0); }
int codeSig(ssize_t idx = -1)
{ return intRet(idx, 1); }
int codeRet(ssize_t idx = -1)
{ return intRet(idx, 2); }
// send / read
ssize_t send(const void *pData, size_t lenReq);
virtual ssize_t send(const std::string &str)
{ return this->send(str.c_str(), str.size()); }
ssize_t read(void *pBuf, size_t lenReq);
ssize_t sinkRead(void *pBuf, size_t lenReq, int fdSel = STDOUT_FILENO);
protected:
virtual ~FileExecuting() {}
private:
FileExecuting();
FileExecuting(const FileExecuting &) = delete;
FileExecuting &operator=(const FileExecuting &) = delete;
/*
* Naming of functions: objectVerb()
* Example: peerAdd()
*/
/* member functions */
Success process();
Success shutdown();
void processInfo(char *pBuf, char *pBufEnd);
void containerInfo(char *pBuf, char *pBufEnd);
void internalInfo(char *pBuf, char *pBufEnd);
bool sinksReady(FeNode *pNode) const;
Success childStateRecord();
void autoSource(FeNode *pNode);
void autoSourceDone();
void autoSink(FeNode *pNode);
char **vecConstCharToCharList(const VecConstChar &lst);
void ptrListFree(int cnt, char ** &lst);
FileExecuting &intCmdAdd(const std::string &cmd, const VecConstChar &argv);
bool boolRet(ssize_t idx, size_t offs, bool isAnd = false);
int intRet(ssize_t idx, size_t offs);
ssize_t intSend(const void *pData, size_t lenReq);
ssize_t intSinkRead(void *pBuf, size_t lenReq, FeNode *pNode);
void pipeInit(FePairFd &pair);
void pipeClose(FePairFd &pair, bool deInit = true);
bool fileNonBlockingSet(int fd);
void fdClose(int &fd, bool deInit = true);
bool closefromInternal(int fdStart);
/* member variables */
// container & internal
bool mIsInternal;
// container
#if 0 // TODO: Implement
uint32_t mMsStart;
#endif
uint32_t mMsTimeout;
std::vector<FileExecuting *> mLstExec;
std::vector<FeResult> mResults;
FeNode mNodeIn;
bool mInternalsStarted;
bool mConfigClosed;
#if CONFIG_PROC_HAVE_DRIVERS
std::mutex mMtxConfig;
#endif
// internal
std::string mCmd;
std::string mCmdBase;
bool mUserEnv;
char **mpArgs;
char **mpEnv;
std::string mStrStateChild;
FeResult mResult;
FeResult *mpResult;
uint32_t mBytesRead;
uint32_t mBytesSent;
#if CONFIG_PROC_HAVE_DRIVERS
std::mutex mMtxWrite;
std::mutex mMtxRead;
#endif
VecConstChar mArgs;
VecConstChar mEnv;
FeNode mNodeOut;
FeNode mNodeErr;
bool mDoneAck;
/* static functions */
/* static variables */
/* constants */
};
#endif