-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cc
331 lines (282 loc) · 7.58 KB
/
util.cc
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
/*
nsjail - useful procedures
-----------------------------------------
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "util.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include "logs.h"
#include "macros.h"
namespace util {
ssize_t readFromFd(int fd, void* buf, size_t len) {
uint8_t* charbuf = (uint8_t*)buf;
size_t readSz = 0;
while (readSz < len) {
ssize_t sz = TEMP_FAILURE_RETRY(read(fd, &charbuf[readSz], len - readSz));
if (sz <= 0) {
break;
}
readSz += sz;
}
return readSz;
}
ssize_t readFromFile(const char* fname, void* buf, size_t len) {
int fd = TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_CLOEXEC));
if (fd == -1) {
LOG_E("open('%s', O_RDONLY|O_CLOEXEC)", fname);
return -1;
}
ssize_t ret = readFromFd(fd, buf, len);
close(fd);
return ret;
}
bool writeToFd(int fd, const void* buf, size_t len) {
const uint8_t* charbuf = (const uint8_t*)buf;
size_t writtenSz = 0;
while (writtenSz < len) {
ssize_t sz = TEMP_FAILURE_RETRY(write(fd, &charbuf[writtenSz], len - writtenSz));
if (sz < 0) {
return false;
}
writtenSz += sz;
}
return true;
}
bool writeBufToFile(const char* filename, const void* buf, size_t len, int open_flags) {
int fd;
TEMP_FAILURE_RETRY(fd = open(filename, open_flags, 0644));
if (fd == -1) {
PLOG_E("Couldn't open '%s' for writing", filename);
return false;
}
if (!writeToFd(fd, buf, len)) {
PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", len, filename, fd);
close(fd);
if (open_flags & O_CREAT) {
unlink(filename);
}
return false;
}
LOG_D("Written '%zu' bytes to '%s'", len, filename);
close(fd);
return true;
}
bool createDirRecursively(const char* dir) {
if (dir[0] != '/') {
LOG_W("The directory path must start with '/': '%s' provided", dir);
return false;
}
int prev_dir_fd = TEMP_FAILURE_RETRY(open("/", O_RDONLY | O_CLOEXEC | O_DIRECTORY));
if (prev_dir_fd == -1) {
PLOG_W("open('/', O_RDONLY | O_CLOEXEC)");
return false;
}
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s", dir);
char* curr = path;
for (;;) {
while (*curr == '/') {
curr++;
}
char* next = strchr(curr, '/');
if (next == NULL) {
close(prev_dir_fd);
return true;
}
*next = '\0';
if (mkdirat(prev_dir_fd, curr, 0755) == -1 && errno != EEXIST) {
PLOG_W("mkdir('%s', 0755)", curr);
close(prev_dir_fd);
return false;
}
int dir_fd = TEMP_FAILURE_RETRY(openat(prev_dir_fd, curr, O_DIRECTORY | O_CLOEXEC));
if (dir_fd == -1) {
PLOG_W("openat('%d', '%s', O_DIRECTORY | O_CLOEXEC)", prev_dir_fd, curr);
close(prev_dir_fd);
return false;
}
close(prev_dir_fd);
prev_dir_fd = dir_fd;
curr = next + 1;
}
}
std::string* StrAppend(std::string* str, const char* format, ...) {
char* strp;
va_list args;
va_start(args, format);
int ret = vasprintf(&strp, format, args);
va_end(args);
if (ret == -1) {
PLOG_E("Memory allocation failed during asprintf()");
str->append(" [ERROR: mem_allocation_failed] ");
return str;
}
str->append(strp, ret);
free(strp);
return str;
}
std::string StrPrintf(const char* format, ...) {
char* strp;
va_list args;
va_start(args, format);
int ret = vasprintf(&strp, format, args);
va_end(args);
if (ret == -1) {
PLOG_E("Memory allocation failed during asprintf()");
return "[ERROR: mem_allocation_failed]";
}
std::string str(strp, ret);
free(strp);
return str;
}
const std::string StrQuote(const std::string& str) {
std::ostringstream ss;
ss << std::quoted(str, '\'');
return ss.str();
}
bool isANumber(const char* s) {
for (size_t i = 0; s[i]; s++) {
if (!isdigit(s[i]) && s[i] != 'x') {
return false;
}
}
return true;
}
static __thread pthread_once_t rndThreadOnce = PTHREAD_ONCE_INIT;
static __thread uint64_t rndX;
/* MMIX LCG PRNG */
static const uint64_t a = 6364136223846793005ULL;
static const uint64_t c = 1442695040888963407ULL;
static void rndInitThread(void) {
#if defined(__NR_getrandom)
if (util::syscall(__NR_getrandom, (uintptr_t)&rndX, sizeof(rndX), 0) == sizeof(rndX)) {
return;
}
#endif /* defined(__NR_getrandom) */
int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC));
if (fd == -1) {
PLOG_D(
"Couldn't open /dev/urandom for reading. Using gettimeofday "
"fall-back");
struct timeval tv;
gettimeofday(&tv, NULL);
rndX = tv.tv_usec + ((uint64_t)tv.tv_sec << 32);
return;
}
if (readFromFd(fd, (uint8_t*)&rndX, sizeof(rndX)) != sizeof(rndX)) {
PLOG_F("Couldn't read '%zu' bytes from /dev/urandom", sizeof(rndX));
close(fd);
}
close(fd);
}
uint64_t rnd64(void) {
pthread_once(&rndThreadOnce, rndInitThread);
rndX = a * rndX + c;
return rndX;
}
const std::string sigName(int signo) {
std::string res;
struct {
const int signo;
const char* const name;
} static const sigNames[] = {
NS_VALSTR_STRUCT(SIGINT),
NS_VALSTR_STRUCT(SIGILL),
NS_VALSTR_STRUCT(SIGABRT),
NS_VALSTR_STRUCT(SIGFPE),
NS_VALSTR_STRUCT(SIGSEGV),
NS_VALSTR_STRUCT(SIGTERM),
NS_VALSTR_STRUCT(SIGHUP),
NS_VALSTR_STRUCT(SIGQUIT),
NS_VALSTR_STRUCT(SIGTRAP),
NS_VALSTR_STRUCT(SIGKILL),
NS_VALSTR_STRUCT(SIGBUS),
NS_VALSTR_STRUCT(SIGSYS),
NS_VALSTR_STRUCT(SIGPIPE),
NS_VALSTR_STRUCT(SIGALRM),
NS_VALSTR_STRUCT(SIGURG),
NS_VALSTR_STRUCT(SIGSTOP),
NS_VALSTR_STRUCT(SIGTSTP),
NS_VALSTR_STRUCT(SIGCONT),
NS_VALSTR_STRUCT(SIGCHLD),
NS_VALSTR_STRUCT(SIGTTIN),
NS_VALSTR_STRUCT(SIGTTOU),
NS_VALSTR_STRUCT(SIGPOLL),
NS_VALSTR_STRUCT(SIGXCPU),
NS_VALSTR_STRUCT(SIGXFSZ),
NS_VALSTR_STRUCT(SIGVTALRM),
NS_VALSTR_STRUCT(SIGPROF),
NS_VALSTR_STRUCT(SIGUSR1),
NS_VALSTR_STRUCT(SIGUSR2),
NS_VALSTR_STRUCT(SIGWINCH),
};
for (const auto& i : sigNames) {
if (signo == i.signo) {
res.append(i.name);
return res;
}
}
if (signo > SIGRTMIN) {
res.append("SIG");
res.append(std::to_string(signo));
res.append("-RTMIN+");
res.append(std::to_string(signo - SIGRTMIN));
return res;
}
res.append("SIGUNKNOWN(");
res.append(std::to_string(signo));
res.append(")");
return res;
}
const std::string timeToStr(time_t t) {
char timestr[128];
struct tm utctime;
localtime_r(&t, &utctime);
if (strftime(timestr, sizeof(timestr) - 1, "%FT%T%z", &utctime) == 0) {
return "[Time conv error]";
}
return timestr;
}
std::vector<std::string> strSplit(const std::string str, char delim) {
std::vector<std::string> vec;
std::istringstream stream(str);
for (std::string word; std::getline(stream, word, delim);) {
vec.push_back(word);
}
return vec;
}
long syscall(long sysno, uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
uintptr_t a5) {
return ::syscall(sysno, a0, a1, a2, a3, a4, a5);
}
} // namespace util