-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathutils.cpp
More file actions
368 lines (285 loc) · 6.98 KB
/
utils.cpp
File metadata and controls
368 lines (285 loc) · 6.98 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
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
/* Utilities.
*
* Author: Daniel Krebs <github@daniel-krebs.net>
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <cctype>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#include <dirent.h>
#include <fcntl.h>
#include <jansson.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <pthread.h>
#include <unistd.h>
#include <villas/colors.hpp>
#include <villas/config.hpp>
#include <villas/exceptions.hpp>
#include <villas/fs.hpp>
#include <villas/log.hpp>
#include <villas/utils.hpp>
static pthread_t main_thread;
namespace villas {
namespace utils {
std::vector<std::string> tokenize(const std::string &s,
const std::string &delimiter) {
std::vector<std::string> tokens;
size_t lastPos = 0;
size_t curentPos;
while ((curentPos = s.find(delimiter, lastPos)) != std::string::npos) {
const size_t tokenLength = curentPos - lastPos;
tokens.push_back(s.substr(lastPos, tokenLength));
// Advance in string
lastPos = curentPos + delimiter.length();
}
// Check if there's a last token behind the last delimiter.
if (lastPos != s.length()) {
const size_t lastTokenLength = s.length() - lastPos;
tokens.push_back(s.substr(lastPos, lastTokenLength));
}
return tokens;
}
ssize_t readRandom(char *buf, size_t len) {
int fd;
ssize_t bytes = -1;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
return -1;
while (len) {
bytes = read(fd, buf, len);
if (bytes < 0)
break;
len -= bytes;
buf += bytes;
}
close(fd);
return bytes;
}
// Setup exit handler
int signalsInit(void (*cb)(int signal, siginfo_t *sinfo, void *ctx),
std::list<int> cbSignals, const std::list<int> &ignoreSignals) {
int ret;
Logger logger = Log::get("signals");
logger->info("Initialize subsystem");
struct sigaction sa_cb;
sa_cb.sa_flags = SA_SIGINFO | SA_NODEFER;
sa_cb.sa_sigaction = cb;
struct sigaction sa_ign;
sa_ign.sa_flags = 0;
sa_ign.sa_handler = SIG_IGN;
main_thread = pthread_self();
sigemptyset(&sa_cb.sa_mask);
sigemptyset(&sa_ign.sa_mask);
cbSignals.insert(cbSignals.begin(),
{SIGINT, SIGTERM, SIGUSR1, SIGUSR2, SIGALRM});
cbSignals.sort();
cbSignals.unique();
for (auto signal : cbSignals) {
ret = sigaction(signal, &sa_cb, nullptr);
if (ret)
return ret;
}
for (auto signal : ignoreSignals) {
ret = sigaction(signal, &sa_ign, nullptr);
if (ret)
return ret;
}
return 0;
}
char *decolor(char *str) {
char *p, *q;
bool inseq = false;
for (p = q = str; *p; p++) {
switch (*p) {
case 0x1b:
if (*(++p) == '[') {
inseq = true;
continue;
}
break;
case 'm':
if (inseq) {
inseq = false;
continue;
}
break;
}
if (!inseq) {
*q = *p;
q++;
}
}
*q = '\0';
return str;
}
void killme(int sig) {
// Send only to main thread in case the ID was initilized by signalsInit()
if (main_thread)
pthread_kill(main_thread, sig);
else
kill(0, sig);
}
double boxMuller(float m, float s) {
double x1, x2, y1;
static double y2;
static int use_last = 0;
if (use_last) { // Use value from previous call
y1 = y2;
use_last = 0;
} else {
double w;
do {
x1 = 2.0 * randf() - 1.0;
x2 = 2.0 * randf() - 1.0;
w = x1 * x1 + x2 * x2;
} while (w >= 1.0);
w = sqrt(-2.0 * log(w) / w);
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}
return m + y1 * s;
}
double randf() { return (double)random() / RAND_MAX; }
char *vstrcatf(char **dest, const char *fmt, va_list ap) {
char *tmp;
int n = *dest ? strlen(*dest) : 0;
int i = vasprintf(&tmp, fmt, ap);
*dest = (char *)(realloc(*dest, n + i + 1));
if (*dest != nullptr)
strncpy(*dest + n, tmp, i + 1);
free(tmp);
return *dest;
}
char *strcatf(char **dest, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vstrcatf(dest, fmt, ap);
va_end(ap);
return *dest;
}
char *strf(const char *fmt, ...) {
char *buf = nullptr;
va_list ap;
va_start(ap, fmt);
vstrcatf(&buf, fmt, ap);
va_end(ap);
return buf;
}
char *vstrf(const char *fmt, va_list va) {
char *buf = nullptr;
vstrcatf(&buf, fmt, va);
return buf;
}
void *memdup(const void *src, size_t bytes) {
void *dst = new char[bytes];
if (!dst)
throw MemoryAllocationError();
memcpy(dst, src, bytes);
return dst;
}
pid_t spawn(const char *name, char *const argv[]) {
pid_t pid;
pid = fork();
switch (pid) {
case -1:
return -1;
case 0:
return execvp(name, (char *const *)argv);
}
return pid;
}
size_t strlenp(const char *str) {
size_t sz = 0;
for (const char *d = str; *d; d++) {
const unsigned char *c = (const unsigned char *)d;
if (isprint(*c))
sz++;
else if (c[0] == '\b')
sz--;
else if (c[0] == '\t')
sz += 4; // Tab width == 4
// CSI sequence
else if (c[0] == '\e' && c[1] == '[') {
c += 2;
while (*c && *c != 'm')
c++;
}
// UTF-8
else if (c[0] >= 0xc2 && c[0] <= 0xdf) {
sz++;
c += 1;
} else if (c[0] >= 0xe0 && c[0] <= 0xef) {
sz++;
c += 2;
} else if (c[0] >= 0xf0 && c[0] <= 0xf4) {
sz++;
c += 3;
}
d = (const char *)c;
}
return sz;
}
int log2i(long long x) {
if (x == 0)
return 1;
return sizeof(x) * 8 - __builtin_clzll(x) - 1;
}
int sha1sum(FILE *f, unsigned char *sha1) {
int ret;
char buf[512];
ssize_t bytes;
long seek;
seek = ftell(f);
fseek(f, 0, SEEK_SET);
EVP_MD_CTX *c = EVP_MD_CTX_new();
ret = EVP_DigestInit(c, EVP_sha1());
if (!ret)
return -1;
bytes = fread(buf, 1, 512, f);
while (bytes > 0) {
EVP_DigestUpdate(c, buf, bytes);
bytes = fread(buf, 1, 512, f);
}
EVP_DigestFinal(c, sha1, nullptr);
fseek(f, seek, SEEK_SET);
EVP_MD_CTX_free(c);
return 0;
}
bool isDocker() { return access("/.dockerenv", F_OK) != -1; }
bool isKubernetes() {
return access("/var/run/secrets/kubernetes.io", F_OK) != -1 ||
getenv("KUBERNETES_SERVICE_HOST") != nullptr;
}
bool isContainer() { return isDocker() || isKubernetes(); }
bool isPrivileged() {
// TODO: a cleaner way would be to use libcap here and check for the
// SYS_ADMIN capability.
auto *f = fopen(PROCFS_PATH "/sys/vm/nr_hugepages", "w");
if (!f)
return false;
fclose(f);
return true;
}
void write_to_file(std::string data, const fs::path file) {
villas::Log::get("Filewriter")->debug("{} > {}", data, file.string());
std::ofstream outputFile(file.string());
if (outputFile.is_open()) {
outputFile << data;
outputFile.close();
} else {
throw fs::filesystem_error("Cannot open outputfile", std::error_code());
}
}
} // namespace utils
} // namespace villas