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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
|
#include "api.h"
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_thread.h>
#include <assert.h>
#if _WIN32
// https://stackoverflow.com/questions/60645/overlapped-i-o-on-anonymous-pipe
// https://docs.microsoft.com/en-us/windows/win32/procthread/creating-a-child-process-with-redirected-input-and-output
#include <windows.h>
#include "../utfconv.h"
#else
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#endif
#include "../arena_allocator.h"
#define READ_BUF_SIZE 2048
#define PROCESS_TERM_TRIES 3
#define PROCESS_TERM_DELAY 50
#define PROCESS_KILL_LIST_NAME "__process_kill_list__"
#if _WIN32
typedef DWORD process_error_t;
typedef HANDLE process_stream_t;
typedef HANDLE process_handle_t;
typedef wchar_t process_arglist_t[32767];
typedef wchar_t *process_env_t;
#define HANDLE_INVALID (INVALID_HANDLE_VALUE)
#define PROCESS_GET_HANDLE(P) ((P)->process_information.hProcess)
#define PROCESS_ARGLIST_INITIALIZER { 0 }
static volatile long PipeSerialNumber;
#else
typedef int process_error_t;
typedef int process_stream_t;
typedef pid_t process_handle_t;
typedef char **process_arglist_t;
typedef char **process_env_t;
#define HANDLE_INVALID (0)
#define PROCESS_GET_HANDLE(P) ((P)->pid)
#define PROCESS_ARGLIST_INITIALIZER NULL
#endif
#ifdef __GNUC__
#define UNUSED __attribute__((__unused__))
#else
#define UNUSED
#endif
typedef struct {
bool running, detached;
int returncode, deadline;
long pid;
#if _WIN32
PROCESS_INFORMATION process_information;
OVERLAPPED overlapped[2];
bool reading[2];
char buffer[2][READ_BUF_SIZE];
#endif
process_stream_t child_pipes[3][2];
} process_t;
typedef struct process_kill_s {
int tries;
uint32_t start_time;
process_handle_t handle;
struct process_kill_s *next;
} process_kill_t;
typedef struct {
bool stop;
SDL_mutex *mutex;
SDL_cond *has_work, *work_done;
SDL_Thread *worker_thread;
process_kill_t *head;
process_kill_t *tail;
} process_kill_list_t;
typedef enum {
SIGNAL_KILL,
SIGNAL_TERM,
SIGNAL_INTERRUPT
} signal_e;
typedef enum {
WAIT_NONE = 0,
WAIT_DEADLINE = -1,
WAIT_INFINITE = -2
} wait_e;
typedef enum {
STDIN_FD,
STDOUT_FD,
STDERR_FD,
// Special values for redirection.
REDIRECT_DEFAULT = -1,
REDIRECT_DISCARD = -2,
REDIRECT_PARENT = -3,
} filed_e;
static void close_fd(process_stream_t *handle) {
if (*handle && *handle != HANDLE_INVALID) {
#ifdef _WIN32
CloseHandle(*handle);
#else
close(*handle);
#endif
*handle = HANDLE_INVALID;
}
}
static int kill_list_worker(void *ud);
static void kill_list_free(process_kill_list_t *list) {
process_kill_t *node, *temp;
SDL_WaitThread(list->worker_thread, NULL);
SDL_DestroyMutex(list->mutex);
SDL_DestroyCond(list->has_work);
SDL_DestroyCond(list->work_done);
node = list->head;
while (node) {
temp = node;
node = node->next;
free(temp);
}
memset(list, 0, sizeof(process_kill_list_t));
}
static bool kill_list_init(process_kill_list_t *list) {
memset(list, 0, sizeof(process_kill_list_t));
list->mutex = SDL_CreateMutex();
list->has_work = SDL_CreateCond();
list->work_done = SDL_CreateCond();
list->head = list->tail = NULL;
list->stop = false;
if (!list->mutex || !list->has_work || !list->work_done) {
kill_list_free(list);
return false;
}
list->worker_thread = SDL_CreateThread(kill_list_worker, "process_kill", list);
if (!list->worker_thread) {
kill_list_free(list);
return false;
}
return true;
}
static void kill_list_push(process_kill_list_t *list, process_kill_t *task) {
if (!list) return;
task->next = NULL;
if (list->tail) {
list->tail->next = task;
list->tail = task;
} else {
list->head = list->tail = task;
}
}
static void kill_list_pop(process_kill_list_t *list) {
if (!list || !list->head) return;
process_kill_t *head = list->head;
list->head = list->head->next;
if (!list->head) list->tail = NULL;
head->next = NULL;
}
static void kill_list_wait_all(process_kill_list_t *list) {
SDL_LockMutex(list->mutex);
// wait until list is empty
while (list->head)
SDL_CondWait(list->work_done, list->mutex);
// tell the worker to stop
list->stop = true;
SDL_CondSignal(list->has_work);
SDL_UnlockMutex(list->mutex);
}
static void process_handle_close(process_handle_t *handle) {
#ifdef _WIN32
if (*handle) {
CloseHandle(*handle);
*handle = NULL;
}
#endif
(void) 0;
}
static bool process_handle_is_running(process_handle_t handle, int *status) {
#ifdef _WIN32
DWORD s;
if (GetExitCodeProcess(handle, &s) && s != STILL_ACTIVE) {
if (status != NULL)
*status = s;
return false;
}
#else
int s;
if (waitpid(handle, &s, WNOHANG) != 0) {
if (status != NULL)
*status = WEXITSTATUS(s);
return false;
}
#endif
return true;
}
static bool process_handle_signal(process_handle_t handle, signal_e sig) {
#if _WIN32
switch(sig) {
case SIGNAL_TERM: return GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, GetProcessId(handle));
case SIGNAL_KILL: return TerminateProcess(handle, -1);
case SIGNAL_INTERRUPT: return DebugBreakProcess(handle);
}
#else
switch (sig) {
case SIGNAL_TERM: return kill(-handle, SIGTERM) == 0; break;
case SIGNAL_KILL: return kill(-handle, SIGKILL) == 0; break;
case SIGNAL_INTERRUPT: return kill(-handle, SIGINT) == 0; break;
}
#endif
return false;
}
static int kill_list_worker(void *ud) {
process_kill_list_t *list = (process_kill_list_t *) ud;
process_kill_t *current_task;
uint32_t delay;
while (true) {
SDL_LockMutex(list->mutex);
// wait until we have work to do
while (!list->head && !list->stop)
SDL_CondWait(list->has_work, list->mutex); // LOCK MUTEX
if (list->stop) break;
while ((current_task = list->head)) {
if ((SDL_GetTicks() - current_task->start_time) < PROCESS_TERM_DELAY)
break;
kill_list_pop(list);
if (process_handle_is_running(current_task->handle, NULL)) {
if (current_task->tries < PROCESS_TERM_TRIES)
process_handle_signal(current_task->handle, SIGNAL_TERM);
else if (current_task->tries == PROCESS_TERM_TRIES)
process_handle_signal(current_task->handle, SIGNAL_KILL);
else
goto free_task;
// add the task back into the queue
current_task->tries++;
current_task->start_time = SDL_GetTicks();
kill_list_push(list, current_task);
} else {
free_task:
SDL_CondSignal(list->work_done);
process_handle_close(¤t_task->handle);
free(current_task);
}
}
delay = list->head ? (list->head->start_time + PROCESS_TERM_DELAY) - SDL_GetTicks() : 0;
SDL_UnlockMutex(list->mutex);
SDL_Delay(delay);
}
SDL_UnlockMutex(list->mutex);
return 0;
}
static int push_error_string(lua_State *L, process_error_t err) {
#ifdef _WIN32
char *msg = NULL;
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char *) &msg,
0,
NULL);
if (!msg)
return 0;
lua_pushstring(L, msg);
LocalFree(msg);
#else
lua_pushstring(L, strerror(err));
#endif
return 1;
}
static void push_error(lua_State *L, const char *extra, process_error_t err) {
const char *msg = "unknown error";
extra = extra != NULL ? extra : "error";
if (push_error_string(L, err))
msg = lua_tostring(L, -1);
lua_pushfstring(L, "%s: %s (%d)", extra, msg, err);
}
static bool poll_process(process_t* proc, int timeout) {
uint32_t ticks;
if (!proc->running)
return false;
if (timeout == WAIT_DEADLINE)
timeout = proc->deadline;
ticks = SDL_GetTicks();
do {
int status;
if (!process_handle_is_running(PROCESS_GET_HANDLE(proc), &status)) {
proc->running = false;
proc->returncode = status;
break;
}
if (timeout)
SDL_Delay(timeout >= 5 ? 5 : 0);
} while (timeout == WAIT_INFINITE || (int)SDL_GetTicks() - ticks < timeout);
return proc->running;
}
static bool signal_process(process_t* proc, signal_e sig) {
if (process_handle_signal(PROCESS_GET_HANDLE(proc), sig))
poll_process(proc, WAIT_NONE);
return true;
}
static int process_start(lua_State* L) {
int retval = 1;
process_t *self = NULL;
int deadline = 10, detach = false, new_fds[3] = { STDIN_FD, STDOUT_FD, STDERR_FD };
#ifdef _WIN32
wchar_t *commandline = NULL, *env = NULL, *cwd = NULL;
#else
const char **cmd = NULL, *env = NULL, *cwd = NULL;
#endif
lua_settop(L, 3);
lxl_arena *A = lxl_arena_init(L);
// copy command line arguments
#ifdef _WIN32
if ( !(commandline = utfconv_fromutf8(A, luaL_checkstring(L, 1))) )
return luaL_error(L, "%s", UTFCONV_ERROR_INVALID_CONVERSION);
#else
luaL_checktype(L, 1, LUA_TTABLE);
int len = luaL_len(L, 1);
cmd = lxl_arena_zero(A, (len + 1) * sizeof(char *));
for (int i = 0; i < len; i++) {
cmd[i] = lxl_arena_strdup(A, (lua_rawgeti(L, 1, i+1), luaL_checkstring(L, -1)));
}
#endif
if (lua_istable(L, 2)) {
lua_getfield(L, 2, "detach"); detach = lua_toboolean(L, -1);
lua_getfield(L, 2, "timeout"); deadline = luaL_optnumber(L, -1, deadline);
lua_getfield(L, 2, "stdin"); new_fds[STDIN_FD] = luaL_optnumber(L, -1, STDIN_FD);
lua_getfield(L, 2, "stdout"); new_fds[STDOUT_FD] = luaL_optnumber(L, -1, STDOUT_FD);
lua_getfield(L, 2, "stderr"); new_fds[STDERR_FD] = luaL_optnumber(L, -1, STDERR_FD);
for (int stream = STDIN_FD; stream <= STDERR_FD; ++stream) {
if (new_fds[stream] > STDERR_FD || new_fds[stream] < REDIRECT_PARENT)
return luaL_error(L, "error: redirect to handles, FILE* and paths are not supported");
}
lua_pop(L, 5); // pop all the values above
#ifdef _WIN32
if (lua_getfield(L, 2, "env") == LUA_TFUNCTION) {
lua_newtable(L);
LPWCH system_env = GetEnvironmentStringsW(), envp = system_env;
while (wcslen(envp) > 0) {
const char *env = utfconv_fromwstr(A, envp), *eq = env ? strchr(env, '=') : NULL;
if (!env) return (FreeEnvironmentStringsW(system_env), luaL_error(L, "%s", UTFCONV_ERROR_INVALID_CONVERSION));
if (!eq) return (FreeEnvironmentStringsW(system_env), luaL_error(L, "invalid environment variable"));
lua_pushlstring(L, env, eq - env); lua_pushstring(L, eq+1);
lxl_arena_free(A, (void *) env);
lua_rawset(L, -3);
envp += wcslen(envp) + 1;
}
FreeEnvironmentStringsW(system_env);
lua_call(L, 1, 1);
size_t len = 0; const char *env_mb = luaL_checklstring(L, -1, &len);
if (!(env = utfconv_fromlutf8(A, env_mb, len)))
return luaL_error(L, "%s", UTFCONV_ERROR_INVALID_CONVERSION);
}
if (lua_getfield(L, 2, "cwd"), luaL_optstring(L, -1, NULL)) {
if ( !(cwd = utfconv_fromutf8(A, lua_tostring(L, -1))) )
return luaL_error(L, UTFCONV_ERROR_INVALID_CONVERSION);
}
lua_pop(L, 2);
#else
if (lua_getfield(L, 2, "env") == LUA_TFUNCTION) {
lua_newtable(L);
lua_call(L, 1, 1);
size_t len = 0; env = lua_tolstring(L, -1, &len);
env = lxl_arena_copy(A, (void *) env, len+1);
}
cwd = lxl_arena_strdup(A, (lua_getfield(L, 2, "cwd"), luaL_optstring(L, -1, NULL)));
lua_pop(L, 2);
#endif
}
self = lua_newuserdata(L, sizeof(process_t));
memset(self, 0, sizeof(process_t));
luaL_setmetatable(L, API_TYPE_PROCESS);
self->deadline = deadline;
self->detached = detach;
#if _WIN32
for (int i = 0; i < 3; ++i) {
switch (new_fds[i]) {
case REDIRECT_PARENT:
switch (i) {
case STDIN_FD: self->child_pipes[i][0] = GetStdHandle(STD_INPUT_HANDLE); break;
case STDOUT_FD: self->child_pipes[i][1] = GetStdHandle(STD_OUTPUT_HANDLE); break;
case STDERR_FD: self->child_pipes[i][1] = GetStdHandle(STD_ERROR_HANDLE); break;
}
self->child_pipes[i][i == STDIN_FD ? 1 : 0] = INVALID_HANDLE_VALUE;
break;
case REDIRECT_DISCARD:
self->child_pipes[i][0] = INVALID_HANDLE_VALUE;
self->child_pipes[i][1] = INVALID_HANDLE_VALUE;
break;
default: {
if (new_fds[i] == i) {
char pipeNameBuffer[MAX_PATH];
sprintf(pipeNameBuffer, "\\\\.\\Pipe\\RemoteExeAnon.%08lx.%08lx", GetCurrentProcessId(), InterlockedIncrement(&PipeSerialNumber));
self->child_pipes[i][0] = CreateNamedPipeA(pipeNameBuffer, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_WAIT, 1, READ_BUF_SIZE, READ_BUF_SIZE, 0, NULL);
if (self->child_pipes[i][0] == INVALID_HANDLE_VALUE) {
push_error(L, "cannot create pipe", GetLastError());
retval = -1;
goto cleanup;
}
self->child_pipes[i][1] = CreateFileA(pipeNameBuffer, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (self->child_pipes[i][1] == INVALID_HANDLE_VALUE) {
// prevent CloseHandle from messing up error codes
DWORD err = GetLastError();
CloseHandle(self->child_pipes[i][0]);
push_error(L, "cannot open pipe", err);
retval = -1;
goto cleanup;
}
if (!SetHandleInformation(self->child_pipes[i][i == STDIN_FD ? 1 : 0], HANDLE_FLAG_INHERIT, 0) ||
!SetHandleInformation(self->child_pipes[i][i == STDIN_FD ? 0 : 1], HANDLE_FLAG_INHERIT, 1)) {
push_error(L, "cannot set pipe permission", GetLastError());
retval = -1;
goto cleanup;
}
}
} break;
}
}
for (int i = 0; i < 3; ++i) {
if (new_fds[i] != i) {
self->child_pipes[i][0] = self->child_pipes[new_fds[i]][0];
self->child_pipes[i][1] = self->child_pipes[new_fds[i]][1];
}
}
STARTUPINFOW siStartInfo;
memset(&self->process_information, 0, sizeof(self->process_information));
memset(&siStartInfo, 0, sizeof(siStartInfo));
siStartInfo.cb = sizeof(siStartInfo);
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
siStartInfo.hStdInput = self->child_pipes[STDIN_FD][0];
siStartInfo.hStdOutput = self->child_pipes[STDOUT_FD][1];
siStartInfo.hStdError = self->child_pipes[STDERR_FD][1];
if (!CreateProcessW(NULL, commandline, NULL, NULL, true, (detach ? DETACHED_PROCESS : CREATE_NO_WINDOW) | CREATE_UNICODE_ENVIRONMENT, env, cwd, &siStartInfo, &self->process_information)) {
push_error(L, NULL, GetLastError());
retval = -1;
goto cleanup;
}
self->pid = (long)self->process_information.dwProcessId;
if (detach)
CloseHandle(self->process_information.hProcess);
CloseHandle(self->process_information.hThread);
#else
int control_pipe[2] = { 0 };
for (int i = 0; i < 3; ++i) { // Make only the parents fd's non-blocking. Children should block.
if (pipe(self->child_pipes[i]) || fcntl(self->child_pipes[i][i == STDIN_FD ? 1 : 0], F_SETFL, O_NONBLOCK) == -1) {
push_error(L, "cannot create pipe", errno);
retval = -1;
goto cleanup;
}
}
// create a pipe to get the exit code of exec()
if (pipe(control_pipe) == -1) {
lua_pushfstring(L, "Error creating control pipe: %s", strerror(errno));
retval = -1;
goto cleanup;
}
if (fcntl(control_pipe[1], F_SETFD, FD_CLOEXEC) == -1) {
lua_pushfstring(L, "Error setting FD_CLOEXEC: %s", strerror(errno));
retval = -1;
goto cleanup;
}
self->pid = (long)fork();
if (self->pid < 0) {
push_error(L, "cannot create child process", errno);
retval = -1;
goto cleanup;
} else if (!self->pid) {
// child process
if (!detach)
setpgid(0,0);
for (int stream = 0; stream < 3; ++stream) {
if (new_fds[stream] == REDIRECT_DISCARD) { // Close the stream if we don't want it.
close(self->child_pipes[stream][stream == STDIN_FD ? 0 : 1]);
close(stream);
} else if (new_fds[stream] != REDIRECT_PARENT) // Use the parent handles if we redirect to parent.
dup2(self->child_pipes[new_fds[stream]][new_fds[stream] == STDIN_FD ? 0 : 1], stream);
close(self->child_pipes[stream][stream == STDIN_FD ? 1 : 0]);
}
if (env) {
size_t len = 0;
while ((len = strlen(env)) != 0) {
char *value = strchr(env, '=');
*value = '\0'; value++; // change the '=' into '\0', forming 2 strings side by side
setenv(env, value, 1);
env += len+1;
}
}
if ((!detach || setsid() != -1) && (!cwd || chdir(cwd) != -1))
execvp(cmd[0], (char** const) cmd);
write(control_pipe[1], &errno, sizeof(errno));
_exit(-1);
}
// close our write side so we can read from child
close(control_pipe[1]);
control_pipe[1] = 0;
// wait for child process to respond
int sz, process_rc;
while ((sz = read(control_pipe[0], &process_rc, sizeof(int))) == -1) {
if (errno == EPIPE) break;
if (errno != EINTR) {
lua_pushfstring(L, "Error getting child process status: %s", strerror(errno));
retval = -1;
goto cleanup;
}
}
if (sz) {
// read something from pipe; exec failed
int status;
waitpid(self->pid, &status, 0);
lua_pushfstring(L, "Error creating child process: %s", strerror(process_rc));
retval = -1;
goto cleanup;
}
#endif
cleanup:
#ifndef _WIN32
if (control_pipe[0]) close(control_pipe[0]);
if (control_pipe[1]) close(control_pipe[1]);
#endif
if (self) {
for (int stream = 0; stream < 3; ++stream) {
process_stream_t* pipe = &self->child_pipes[stream][stream == STDIN_FD ? 0 : 1];
if (*pipe) {
close_fd(pipe);
}
}
}
if (retval == -1)
return lua_error(L);
self->running = true;
return retval;
}
static int g_read(lua_State* L, int stream, unsigned long read_size) {
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
long length = 0;
if (stream != STDOUT_FD && stream != STDERR_FD)
return luaL_error(L, "error: redirect to handles, FILE* and paths are not supported");
#if _WIN32
int writable_stream_idx = stream - 1;
if (self->reading[writable_stream_idx] || !ReadFile(self->child_pipes[stream][0], self->buffer[writable_stream_idx], read_size > READ_BUF_SIZE ? READ_BUF_SIZE : read_size, NULL, &self->overlapped[writable_stream_idx])) {
if (self->reading[writable_stream_idx] || GetLastError() == ERROR_IO_PENDING) {
self->reading[writable_stream_idx] = true;
DWORD bytesTransferred = 0;
if (GetOverlappedResult(self->child_pipes[stream][0], &self->overlapped[writable_stream_idx], &bytesTransferred, false)) {
self->reading[writable_stream_idx] = false;
length = bytesTransferred;
memset(&self->overlapped[writable_stream_idx], 0, sizeof(self->overlapped[writable_stream_idx]));
}
} else if (GetLastError() != ERROR_HANDLE_EOF || !poll_process(self, WAIT_NONE)) {
// emulate POSIX behavior in the code below by returning empty string until process exits
signal_process(self, SIGNAL_TERM);
return 0;
}
} else {
length = self->overlapped[writable_stream_idx].InternalHigh;
memset(&self->overlapped[writable_stream_idx], 0, sizeof(self->overlapped[writable_stream_idx]));
}
lua_pushlstring(L, self->buffer[writable_stream_idx], length);
#else
luaL_Buffer b;
luaL_buffinit(L, &b);
uint8_t* buffer = (uint8_t*)luaL_prepbuffsize(&b, READ_BUF_SIZE);
length = read(self->child_pipes[stream][0], buffer, read_size > READ_BUF_SIZE ? READ_BUF_SIZE : read_size);
if (length == 0 && !poll_process(self, WAIT_NONE))
return 0;
else if (length < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
length = 0;
if (length < 0) {
signal_process(self, SIGNAL_TERM);
return 0;
}
luaL_addsize(&b, length);
luaL_pushresult(&b);
#endif
return 1;
}
static int f_write(lua_State* L) {
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
size_t data_size = 0;
const char* data = luaL_checklstring(L, 2, &data_size);
long length;
#if _WIN32
DWORD dwWritten;
if (!WriteFile(self->child_pipes[STDIN_FD][1], data, data_size, &dwWritten, NULL)) {
push_error(L, NULL, GetLastError());
signal_process(self, SIGNAL_TERM);
return lua_error(L);
}
length = dwWritten;
#else
length = write(self->child_pipes[STDIN_FD][1], data, data_size);
if (length < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
length = 0;
else if (length < 0) {
push_error(L, "cannot write to child process", errno);
signal_process(self, SIGNAL_TERM);
return lua_error(L);
}
#endif
lua_pushinteger(L, length);
return 1;
}
static int f_close_stream(lua_State* L) {
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
int stream = luaL_checknumber(L, 2);
close_fd(&self->child_pipes[stream][stream == STDIN_FD ? 1 : 0]);
lua_pushboolean(L, 1);
return 1;
}
// Generic stuff below here.
static int process_strerror(lua_State* L) {
return push_error_string(L, luaL_checknumber(L, 1));
}
static int f_tostring(lua_State* L) {
lua_pushliteral(L, API_TYPE_PROCESS);
return 1;
}
static int f_pid(lua_State* L) {
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
lua_pushinteger(L, self->pid);
return 1;
}
static int f_returncode(lua_State *L) {
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
if (poll_process(self, WAIT_NONE))
return 0;
lua_pushinteger(L, self->returncode);
return 1;
}
static int f_read_stdout(lua_State* L) {
return g_read(L, STDOUT_FD, luaL_optinteger(L, 2, READ_BUF_SIZE));
}
static int f_read_stderr(lua_State* L) {
return g_read(L, STDERR_FD, luaL_optinteger(L, 2, READ_BUF_SIZE));
}
static int f_read(lua_State* L) {
return g_read(L, luaL_checknumber(L, 2), luaL_optinteger(L, 3, READ_BUF_SIZE));
}
static int f_wait(lua_State* L) {
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
int timeout = luaL_optnumber(L, 2, 0);
if (poll_process(self, timeout))
return 0;
lua_pushinteger(L, self->returncode);
return 1;
}
static int self_signal(lua_State* L, signal_e sig) {
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
signal_process(self, sig);
lua_pushboolean(L, 1);
return 1;
}
static int f_terminate(lua_State* L) { return self_signal(L, SIGNAL_TERM); }
static int f_kill(lua_State* L) { return self_signal(L, SIGNAL_KILL); }
static int f_interrupt(lua_State* L) { return self_signal(L, SIGNAL_INTERRUPT); }
static int f_gc(lua_State* L) {
process_kill_list_t *list = NULL;
process_kill_t *p = NULL;
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
// get the kill_list for the lua_State
if (lua_getfield(L, LUA_REGISTRYINDEX, PROCESS_KILL_LIST_NAME) == LUA_TUSERDATA)
list = (process_kill_list_t *) lua_touserdata(L, -1);
if (poll_process(self, 0) && !self->detached) {
// attempt to kill the process if still running and not detached
signal_process(self, SIGNAL_TERM);
if (!list || !list->worker_thread || !(p = malloc(sizeof(process_kill_t)))) {
// use synchronous waiting
if (poll_process(self, PROCESS_TERM_DELAY)) {
signal_process(self, SIGNAL_KILL);
poll_process(self, PROCESS_TERM_DELAY);
}
} else {
// put the handle into a queue for asynchronous waiting
p->handle = PROCESS_GET_HANDLE(self);
p->start_time = SDL_GetTicks();
p->tries = 1;
SDL_LockMutex(list->mutex);
kill_list_push(list, p);
SDL_CondSignal(list->has_work);
SDL_UnlockMutex(list->mutex);
}
}
close_fd(&self->child_pipes[STDIN_FD ][1]);
close_fd(&self->child_pipes[STDOUT_FD][0]);
close_fd(&self->child_pipes[STDERR_FD][0]);
return 0;
}
static int f_running(lua_State* L) {
process_t* self = (process_t*)luaL_checkudata(L, 1, API_TYPE_PROCESS);
lua_pushboolean(L, poll_process(self, WAIT_NONE));
return 1;
}
static int process_gc(lua_State *L) {
process_kill_list_t *list = NULL;
// get the kill_list for the lua_State
if (lua_getfield(L, LUA_REGISTRYINDEX, PROCESS_KILL_LIST_NAME) == LUA_TUSERDATA) {
list = (process_kill_list_t *) lua_touserdata(L, -1);
kill_list_wait_all(list);
kill_list_free(list);
}
return 0;
}
#ifdef LUA_JITLIBNAME
static void luajit_register_process_gc(lua_State *L) {
lua_newuserdata(L, 1);
if (luaL_newmetatable(L, "luajit_process_gc_mt")) {
lua_pushcfunction(L, process_gc);
lua_setfield(L, -2, "__gc");
}
lua_setmetatable(L, -2);
lua_setfield(L, LUA_REGISTRYINDEX, "luajit_process_gc");
}
#endif
static const struct luaL_Reg process_metatable[] = {
{"__gc", f_gc},
{"__tostring", f_tostring},
{"pid", f_pid},
{"returncode", f_returncode},
{"read", f_read},
{"read_stdout", f_read_stdout},
{"read_stderr", f_read_stderr},
{"write", f_write},
{"close_stream", f_close_stream},
{"wait", f_wait},
{"terminate", f_terminate},
{"kill", f_kill},
{"interrupt", f_interrupt},
{"running", f_running},
{NULL, NULL}
};
static const struct luaL_Reg lib[] = {
{ "start", process_start },
{ "strerror", process_strerror },
{ NULL, NULL }
};
int luaopen_process(lua_State *L) {
process_kill_list_t *list = lua_newuserdata(L, sizeof(process_kill_list_t));
if (kill_list_init(list))
lua_setfield(L, LUA_REGISTRYINDEX, PROCESS_KILL_LIST_NAME);
else
lua_pop(L, 1); // discard the list
// create the process metatable
luaL_newmetatable(L, API_TYPE_PROCESS);
luaL_setfuncs(L, process_metatable, 0);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
// create the process library
luaL_newlib(L, lib);
lua_newtable(L);
lua_pushcfunction(L, process_gc);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
API_CONSTANT_DEFINE(L, -1, "WAIT_INFINITE", WAIT_INFINITE);
API_CONSTANT_DEFINE(L, -1, "WAIT_DEADLINE", WAIT_DEADLINE);
API_CONSTANT_DEFINE(L, -1, "STREAM_STDIN", STDIN_FD);
API_CONSTANT_DEFINE(L, -1, "STREAM_STDOUT", STDOUT_FD);
API_CONSTANT_DEFINE(L, -1, "STREAM_STDERR", STDERR_FD);
API_CONSTANT_DEFINE(L, -1, "REDIRECT_DEFAULT", REDIRECT_DEFAULT);
API_CONSTANT_DEFINE(L, -1, "REDIRECT_STDOUT", STDOUT_FD);
API_CONSTANT_DEFINE(L, -1, "REDIRECT_STDERR", STDERR_FD);
API_CONSTANT_DEFINE(L, -1, "REDIRECT_PARENT", REDIRECT_PARENT); // Redirects to parent's STDOUT/STDERR
API_CONSTANT_DEFINE(L, -1, "REDIRECT_DISCARD", REDIRECT_DISCARD); // Closes the filehandle, discarding it.
#ifdef LUA_JITLIBNAME
luajit_register_process_gc(L);
#endif
return 1;
}
|