-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.c
428 lines (354 loc) · 9.79 KB
/
platform.c
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#define _GNU_SOURCE
#include "platform.h"
#include "string.h"
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
const char* get_user_home_dir(void)
{
#if PLATFORM == PLATFORM_LINUX || PLATFORM == PLATFORM_MACOS
return getenv("HOME");
#else
return NULL;
#endif
}
const char* get_user_cache_dir(void)
{
#if PLATFORM == PLATFORM_LINUX
const char* const dir = getenv("XDG_CACHE_HOME");
if (dir != NULL && !str_equals(dir, "")) {
return (char*)dir;
}
return join_paths((const char*[]) { get_user_home_dir(), ".cache", NULL });
#elif PLATFORM == PLATFORM_MACOS
return join_paths((const char*[]) { get_user_home_dir(), "Library/Caches", NULL });
#else
return NULL;
#endif
}
const char* get_dir(const char* path)
{
// According to dirname(3)(https://man7.org/linux/man-pages/man3/dirname.3p.html),
// dirname can modify both the given path and the return value.
// Especially, the return value may be overwritten by subsequent calls, which is error-prune.
// To avoid such behaviour, copy values before use.
char* const path2 = copy_str(path);
const char* const dir = dirname(path2);
if ((void*)dir == (void*)path2) {
return path2;
}
const char* const dir2 = copy_str(dir);
free((void*)path2);
return dir2;
}
// NOLINTNEXTLINE(misc-no-recursion)
const char* make_dir(const char* name)
{
if (has_file(name)) {
return NULL;
}
const char* const dir = get_dir(name);
if (!str_equals(name, dir)) {
const char* const err = make_dir(dir);
if (err != NULL) {
free((void*)dir);
return err;
}
}
const int status = mkdir(name, 0755);
if (status != 0) {
return format_str("%s", name);
}
free((void*)dir);
return NULL;
}
const char* join_paths(const char* const* const paths)
{
char* x = { 0 };
size_t len = 0;
{
FILE* const buf = open_memstream(&x, &len);
int i = 0;
const char* const* path = paths;
while (*path != NULL) {
const size_t n = fprintf(buf, i != 0 ? "/%s" : "%s", *path);
if (n < 0) {
return NULL;
}
i++;
path++;
}
(void)fclose(buf);
}
{
size_t i = 0;
size_t j = 0;
while (x[j]) {
while (x[j] == '/' && (x[j + 1] && x[j + 1] == '/')) {
j++;
}
x[i] = x[j];
i++;
j++;
}
if (len > 1 && x[i - 1] == '/') {
x[i - 1] = 0;
}
while (i < len) {
x[i] = 0;
i++;
}
}
return x;
}
bool has_file(const char* const path)
{
struct stat s = { 0 };
return stat(path, &s) == 0;
}
void run_cmd(const char* const path, const char* const* const args)
{
#if PLATFORM == PLATFORM_LINUX || PLATFORM == PLATFORM_MACOS
const int child_pid = fork();
if (child_pid == -1) {
return;
}
if (child_pid == 0) {
const int gchild_pid = fork();
if (gchild_pid == -1) {
exit(1);
return;
}
if (gchild_pid == 0) {
execv(path, (char* const*)args);
exit(1);
return;
}
exit(0);
return;
}
int status = 0;
waitpid(child_pid, &status, 0);
#endif
}
static const char* init_sig_set(sigset_t* sig_set, unsigned int* sigs, size_t len);
static bool watches_sig(const struct sig_handler* const handler, unsigned int sig);
static void* wait_sigs(const struct sig_handler* const handler);
static const char* read_sig(const struct sig_handler* const handler, unsigned int* const sig);
static const char* write_sig(const struct sig_handler* const handler, const unsigned int* const sig);
static int sig_pipe_read(const struct sig_handler* handler);
static int sig_pipe_write(const struct sig_handler* handler);
static int init_pipe(int* const dst)
{
#if PLATFORM != PLATFORM_MACOS
return pipe2(dst, O_CLOEXEC);
#else
{
const int status = pipe(dst);
if (status != 0) {
return status;
}
}
{
const int status = fcntl(dst[0], F_SETFD, FD_CLOEXEC);
if (status != 0) {
return status;
}
}
{
const int status = fcntl(dst[1], F_SETFD, FD_CLOEXEC);
if (status != 0) {
return status;
}
}
return EXIT_SUCCESS;
#endif
}
const char* watch_sigs(struct sig_handler* const handler, unsigned int* const sigs, const size_t len)
{
handler->sigs.values = sigs;
handler->sigs.len = len;
{
errno = 0;
const int status = init_pipe(handler->pipe);
if (status != 0) {
return format_str("failed to init pipe: %d", errno);
}
}
sigset_t sig_set = { 0 };
{
const char* const err = init_sig_set(&sig_set, sigs, len);
if (err != NULL) {
const char* const err2 = format_str("failed to prepare signal set: %s", err);
free((void*)err);
return err2;
}
}
pthread_t thread = { 0 };
{
errno = 0;
const int status = pthread_create(&thread, NULL, (void* (*)(void*))wait_sigs, handler);
if (status != 0) {
return format_str("failed to create signal thread: %d", errno);
}
}
{
errno = 0;
const int status = pthread_detach(thread);
if (status != 0) {
return format_str("failed to detach signal thread: %d", errno);
}
}
return NULL;
}
const char* catch_sig(const struct sig_handler* const handler, unsigned int* const sig, bool* const caught)
{
fd_set fds = { 0 };
FD_ZERO(&fds);
const int pipe = sig_pipe_read(handler);
FD_SET(pipe, &fds);
while (true) {
{
fd_set fds_read = fds;
errno = 0;
const struct timespec timeout = { 0 };
const int n = pselect(pipe + 1, &fds_read, NULL, NULL, &timeout, NULL);
if (n < 0) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
return format_str("failed to select signal pipe: %d", errno);
}
if (n == 0 || !FD_ISSET(pipe, &fds_read)) {
*caught = false;
return NULL;
}
}
unsigned int sig2 = 0;
{
const char* const err = read_sig(handler, &sig2);
if (err != NULL) {
const char* const err2 = format_str("failed to read signal: %s", err);
free((void*)err);
return err2;
}
}
*sig = sig2;
*caught = true;
break;
}
return NULL;
}
static const char* init_sig_set(sigset_t* const sig_set, unsigned int* const sigs, const size_t len)
{
{
errno = 0;
const int status = sigemptyset(sig_set);
if (status != 0) {
return format_str("failed to prepare signal set: %d", errno);
}
}
for (size_t i = 0; i < len; i++) {
const unsigned int sig = sigs[i];
errno = 0;
const int status = sigaddset(sig_set, (int)sig);
if (status != 0) {
return format_str("failed to set target signal: %d: %d", sig, errno);
}
}
{
errno = 0;
const int status = sigprocmask(SIG_BLOCK, sig_set, NULL);
if (status != 0) {
return format_str("failed to mask signal set: %d", errno);
}
}
return NULL;
}
static bool watches_sig(const struct sig_handler* const handler, const unsigned int sig)
{
for (size_t i = 0; i < handler->sigs.len; i++) {
if (sig == handler->sigs.values[i]) {
return true;
}
}
return false;
}
static void* wait_sigs(const struct sig_handler* const handler)
{
sigset_t sig_set = { 0 };
init_sig_set(&sig_set, handler->sigs.values, handler->sigs.len);
while (true) {
unsigned int sig = 0;
errno = 0;
const int status = sigwait(&sig_set, (int*)&sig);
if (status < 0) {
continue;
}
if (!watches_sig(handler, sig)) {
continue;
}
write_sig(handler, &sig);
}
return NULL;
}
static const char* read_sig(const struct sig_handler* const handler, unsigned int* const sig)
{
static const size_t len = sizeof(unsigned int);
unsigned int n_read = 0;
const int pipe = sig_pipe_read(handler);
while (n_read < len) {
errno = 0;
const size_t n = read(pipe, sig + n_read, len - n_read);
if (n < 0) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
return format_str("%d", errno);
}
if (n == 0) {
break;
}
n_read += n;
}
return NULL;
}
static const char* write_sig(const struct sig_handler* const handler, const unsigned int* const sig)
{
static const size_t len = sizeof(unsigned int);
unsigned int n_written = 0;
const int pipe = sig_pipe_write(handler);
while (n_written < len) {
errno = 0;
const size_t n = write(pipe, sig + n_written, len - n_written);
if (n < 0) {
if (errno == EINTR || errno == EAGAIN) {
continue;
}
return format_str("%d", errno);
}
if (n == 0) {
break;
}
n_written += n;
}
return NULL;
}
static int sig_pipe_read(const struct sig_handler* handler)
{
return handler->pipe[0];
}
static int sig_pipe_write(const struct sig_handler* handler)
{
return handler->pipe[1];
}