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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
* Copyright (C) 2015 by Multi-Tech Systems
*
* This file is part of libmts.
*
* libmts is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* libmts 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libmts. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <mts/MTS_System.h>
#include <fstream>
#include <sstream>
#include <cassert>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef WIN32
#include <windows.h>
//WIN32: FILETIME structure has a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.
static int64_t getEpochTimeMicros() {
const SYSTEMTIME EPOCH = {1970, 1, 4, 1, 0, 0, 0, 0};
FILETIME ft;
BOOL ok = SystemTimeToFileTime(&EPOCH, &ft);
assert(ok);
int64_t epochTimeMicros = ((static_cast<uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) / 10;
return epochTimeMicros;
}
static int64_t getSystemTimeMicros() {
SYSTEMTIME st;
GetSystemTime(&st);
FILETIME ft;
BOOL ok = SystemTimeToFileTime(&st, &ft);
assert(ok);
int64_t systemTimeMicros = ((static_cast<uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) / 10;
return systemTimeMicros;
}
static int64_t getClockFrequency() {
LARGE_INTEGER freq;
BOOL ok = QueryPerformanceFrequency(&freq);
assert(ok);
return freq.QuadPart;
}
static int64_t getClockValue() {
LARGE_INTEGER value;
BOOL ok = QueryPerformanceCounter(&value);
assert(ok);
return value.QuadPart;
}
#else
#include <time.h>
#endif
using namespace MTS;
uint64_t System::timeMicros() {
int64_t micros = 0;
#ifdef WIN32
static const int64_t EPOCH_TIME_MICROS = getEpochTimeMicros();
micros = getSystemTimeMicros() - EPOCH_TIME_MICROS;
#else
timespec ts;
int result = clock_gettime(CLOCK_REALTIME, &ts);
if (result == 0) {
micros = (static_cast<int64_t>(ts.tv_sec) * 1000000)
+ (ts.tv_nsec / 1000);
}
#endif
return micros;
}
uint64_t System::precisionTimeMicros() {
int64_t micros = 0;
#ifdef WIN32
static const double TO_MICROS = 1000000.0 / getClockFrequency();
int64_t value = getClockValue();
micros = static_cast<int64_t>(value * TO_MICROS);
#else
micros = timeMicros();
#endif
return micros;
}
uint64_t System::monoTimeMicros() {
uint64_t micros = 0;
#ifdef WIN32
micros = static_cast<uint64_t>(GetTickCount64()) * 1000;
#else
timespec ts;
int result = clock_gettime(CLOCK_MONOTONIC, &ts);
if (result == 0) {
micros = (static_cast<uint64_t>(ts.tv_sec) * 1000000)
+ (ts.tv_nsec / 1000);
}
#endif
return micros;
}
bool System::isBigEndian() {
static union {
uint32_t i;
char c[4];
} endian = { 0x01020304 };
return endian.c[0] == 1;
}
void System::swapBytes(uint8_t* const pBuffer, const uint32_t iSize) {
if (iSize > 1 && pBuffer != 0) {
uint8_t cByte = 0;
uint32_t i;
uint32_t j;
for (i = 0, j = iSize - 1; i < j; i++, j--) {
cByte = pBuffer[i];
pBuffer[i] = pBuffer[j];
pBuffer[j] = cByte;
}
}
}
int32_t System::cmd(const std::string& cmd, std::string& result) {
std::string output;
FILE * stream;
const int max_buffer = 256;
char buffer[max_buffer];
int32_t code = -1;
stream = popen(cmd.c_str(), "r");
if (stream) {
while (!feof(stream))
if (fgets(buffer, max_buffer, stream) != NULL)
output.append(buffer);
code = pclose(stream);
}
result = output;
return code;
}
int32_t System::readFile(const std::string& path, std::string& result) {
std::ifstream infile(path.c_str());
std::stringstream ss;
if (!infile.is_open()) {
return -1;
}
ss << infile.rdbuf();
infile.close();
result = ss.str();
return 0;
}
int32_t System::execute(const std::string& cmd, const std::vector<std::string>& argv, std::string& result) {
// Ported directly from System::cmd
std::string output;
const int max_buffer = 256;
char buffer[max_buffer];
int32_t code = -1;
ChildHandle child;
if (executeBackground(cmd, argv, PipeType::READ, child)) {
while (!feof(child.stream)) {
if (fgets(buffer, max_buffer, child.stream) != NULL) {
output.append(buffer);
}
}
code = closeBackground(child);
}
result = output;
return code;
}
bool System::executeBackground(const std::string& cmd, const std::vector<std::string>& argv, System::PipeType type, System::ChildHandle& child) {
std::vector<char*> arguments = castArgVector(cmd, argv);
return executeBackground(cmd, arguments.data(), type, child);
}
int System::closeBackground(System::ChildHandle& child) {
int pstat;
pid_t pid;
fclose(child.stream);
do {
pid = waitpid(child.pid, &pstat, 0);
} while (pid == -1 && errno == EINTR);
return pstat;
}
bool System::executeBackground(const std::string& cmd, char* const argv[], System::PipeType type, ChildHandle& child) {
pid_t childPid;
FILE* stream = NULL;
posix_spawn_file_actions_t actions;
int childTargetFd;
const char* parentMode = nullptr;
bool bActionsInitialized = false;
bool bSuccess = false;
do {
Pipe pipe;
if (initPipe(pipe, type) != 0) {
// failed to create pipe
break;
}
if (type == PipeType::READ) {
childTargetFd = STDOUT_FILENO;
parentMode = "r";
} else if (type == PipeType::WRITE) {
childTargetFd = STDIN_FILENO;
parentMode = "w";
} else {
break; // unreachable, unknown mode
}
// Define how child process should handle pipes
if (posix_spawn_file_actions_init(&actions) != 0) {
// failed to initialize file actions
break;
}
bActionsInitialized = true;
if (posix_spawn_file_actions_addclose(&actions, pipe.parent.get()) != 0) {
// failed to add action to close parent pipe fd
break;
}
if (posix_spawn_file_actions_adddup2(&actions, pipe.child.get(), childTargetFd) != 0) {
// failed to add an action to use child pipe fd for output
break;
}
if (posix_spawn_file_actions_addclose(&actions, pipe.child.get()) != 0) {
// failed to add an action to close dupped child pipe fd
break;
}
if (posix_spawnp(&childPid, cmd.c_str(), &actions, NULL, argv, NULL) != 0) {
// failed to spawn the process
break;
}
// Close child-specific file descriptor
pipe.child.reset();
// Convert parent fd to FILE pointer
stream = fdopen(pipe.parent.release(), parentMode);
// Save data about child
child.pid = childPid;
child.stream = stream;
bSuccess = true;
} while (false);
if (bActionsInitialized) {
posix_spawn_file_actions_destroy(&actions);
}
return bSuccess;
}
char* System::castArgument(const std::string& arg) {
// Casting const away is required only to be compatible with some C-style system functions.
// Arguments are not actually modified in such functions.
return const_cast<char*>(arg.c_str());
}
std::vector<char*> System::castArgVector(const std::string& cmd, const std::vector<std::string>& argv) {
std::vector<char*> result;
// Append application name as argument zero
result.push_back(castArgument(cmd));
// Append all other arguments
for (const std::string& arg : argv) {
result.push_back(castArgument(arg));
}
// Terminate with NULL
result.push_back(NULL);
return result;
}
int System::initPipe(System::Pipe& pipe, System::PipeType type) {
const int READ_END_IDX = 0;
const int WRITE_END_IDX = 1;
int holder[2];
int parentIndex;
int childIndex;
int ret = -1;
do {
if (type == PipeType::READ) {
parentIndex = READ_END_IDX;
childIndex = WRITE_END_IDX;
} else if (type == PipeType::WRITE) {
parentIndex = WRITE_END_IDX;
childIndex = READ_END_IDX;
} else {
// unreachable, unknown mode
break; // error
}
ret = ::pipe(holder);
if (ret != 0) {
break;
}
pipe.parent.reset(holder[parentIndex]);
pipe.child.reset(holder[childIndex]);
} while (false);
return ret;
}
System::FdWrapper::FdWrapper(int fd)
: m_fd(fd)
{}
System::FdWrapper::~FdWrapper() {
reset();
}
int System::FdWrapper::get() const {
return m_fd;
}
int System::FdWrapper::release() {
int fd = m_fd;
m_fd = FD_NOT_READY;
return fd;
}
void System::FdWrapper::reset(int fd) {
if (FD_NOT_READY != m_fd) {
::close(m_fd);
}
m_fd = fd;
}
|