1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-29 06:33:22 +00:00

Fixed issue caused by Signal Listener being at top of chain, so it could get emitted before a pipe read

This commit is contained in:
Bryan Roe
2020-06-02 03:43:20 -07:00
parent 54628594f1
commit e9764045a4
3 changed files with 54 additions and 24 deletions

View File

@@ -1509,12 +1509,23 @@ duk_ret_t ILibDuktape_MeshAgent_NetInfo(duk_context *ctx)
gwname = child.stdout.str.trim();\
child = require('child_process').execFile('/bin/sh', ['sh']); \
child.stdout.str = ''; child.stdout.on('data', function(c) { this.str += c.toString(); }); \
child.stdin.write('arp -n ' + gwname + ' | awk \\'{ gsub(/[(]/, \"\", $2); gsub(/[)]/, \"\", $2); printf \"%s,%s,%s\", $6, $4, $2; }\\'\\nexit\\n');\
child.stdin.write('arp -n ' + gwname + ' | awk \\'{ split($2,tok1,\")\"); split(tok1[1],tok2,\"(\"); printf \"%s,%s,%s\", $6,$4,tok2[2]; }\\'\\nexit\\n');\
child.waitExit();\
var tmp = child.stdout.str.trim().split(',');\
child = require('child_process').execFile('/bin/sh', ['sh']);\
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });\
child.stdin.write('networksetup -listallhardwareports | tr \\'\\\\n\\' \\'\\@\\' | awk \\'{ gsub(/@@/, \"\\\\n\", $0); gsub(/@Hardware Port/, \"Hardware Port\", $0); print $0; }\\' | awk -F@ \\'{ split($2, dv, \":\"); gsub(/^[ ]/, \"\", dv[2]); if(dv[2]==\"' + tmp[0] + '\") { split($1, hw, \":\"); gsub(/^ /, \"\", hw[2]); print hw[2]; } }\\'\\nexit\\n');\
child.stdin.write('networksetup -listallhardwareports | tr \\'\\\\n\\' \\'`\\' | awk -F\\'`\\' \\'');\
child.stdin.write('{ ');\
child.stdin.write(' for(i=3;i<NF;i+=4) ');\
child.stdin.write(' { ');\
child.stdin.write(' split($i,dv,\": \"); ');\
child.stdin.write(' if(dv[2]==\"' + tmp[0] + '\")');\
child.stdin.write(' { ');\
child.stdin.write(' split($(i-1), res, \": \");');\
child.stdin.write(' print(res[2]); break;');\
child.stdin.write(' } ');\
child.stdin.write(' } ');\
child.stdin.write('}\\'\\nexit\\n');\
child.waitExit();\
var dvname = child.stdout.str.trim();\
var tmp2 = tmp[1].split(':');\

View File

@@ -280,6 +280,7 @@ duk_ret_t ILibDuktape_SpawnedProcess_SIGCHLD_sink(duk_context *ctx)
}
else
{
printf("Not DETACHED\n");
// We are not detached, so we need to call the same method that broken pipe would've
ILibDuktape_ChildProcess_SubProcess *childprocess = (ILibDuktape_ChildProcess_SubProcess*)Duktape_GetBufferProperty(ctx, -1, ILibDuktape_ChildProcess_MemBuf);
if (childprocess != NULL)

View File

@@ -970,28 +970,15 @@ duk_ret_t ILibDuktape_Process_cwd(duk_context *ctx)
return(1);
}
#ifdef _POSIX
void ILibDuktape_ScriptContainer_Process_SignalListener_PreSelect(void* object, fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime)
{
if (SignalDescriptors[0] != 0)
{
FD_SET(SignalDescriptors[0], readset);
}
}
void ILibDuktape_ScriptContainer_Process_SignalListener_PostSelect(void* object, int slct, fd_set *readset, fd_set *writeset, fd_set *errorset)
{
int s;
int bytesRead = 0;
char sigbuffer[255];
ILibChain_Link *link = (ILibChain_Link*)object;
duk_context *ctx = (duk_context*)((void**)link->ExtraMemoryPtr)[0];
void *h = ((void**)link->ExtraMemoryPtr)[1];
if (FD_ISSET(SignalDescriptors[0], readset))
{
if((bytesRead = read(SignalDescriptors[0], sigbuffer, sizeof(int))) == sizeof(int) && ((int*)sigbuffer)[0] < sizeof(sigbuffer) &&
(bytesRead += read(SignalDescriptors[0], sigbuffer + sizeof(int), ((int*)sigbuffer)[0])) == ((int*)sigbuffer)[0])
#ifdef _POSIX
duk_ret_t ILibDuktape_ScriptContainer_Process_SignalListener_Immediate(duk_context *ctx)
{
duk_size_t bufferLen;
char *sigbuffer = Duktape_GetBuffer(ctx, 0, &bufferLen);
void *h = ILibDuktape_GetProcessObject(ctx);
int s = 0;
switch (((int*)sigbuffer)[1])
{
case SIGTERM:
@@ -1014,6 +1001,37 @@ void ILibDuktape_ScriptContainer_Process_SignalListener_PostSelect(void* object,
default:
break;
}
return(0);
}
void ILibDuktape_ScriptContainer_Process_SignalListener_PreSelect(void* object, fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime)
{
if (SignalDescriptors[0] != 0)
{
FD_SET(SignalDescriptors[0], readset);
}
}
void ILibDuktape_ScriptContainer_Process_SignalListener_PostSelect(void* object, int slct, fd_set *readset, fd_set *writeset, fd_set *errorset)
{
int s;
int bytesRead = 0;
char sigbuffer[255];
ILibChain_Link *link = (ILibChain_Link*)object;
duk_context *ctx = (duk_context*)((void**)link->ExtraMemoryPtr)[0];
void *h = ((void**)link->ExtraMemoryPtr)[1];
char *tmp;
if (FD_ISSET(SignalDescriptors[0], readset))
{
if((bytesRead = read(SignalDescriptors[0], sigbuffer, sizeof(int))) == sizeof(int) && ((int*)sigbuffer)[0] < sizeof(sigbuffer) &&
(bytesRead += read(SignalDescriptors[0], sigbuffer + sizeof(int), ((int*)sigbuffer)[0])) == ((int*)sigbuffer)[0])
{
duk_push_global_object(ctx); //[g]
duk_get_prop_string(ctx, -1, "setImmediate"); //[g][immediate]
duk_swap_top(ctx, -2); //[immediate][this]
duk_push_c_function(ctx, ILibDuktape_ScriptContainer_Process_SignalListener_Immediate, DUK_VARARGS);
tmp = duk_push_fixed_buffer(ctx, ((int*)sigbuffer)[0]); //[immediate][this][func][buffer]
memcpy_s(tmp, ((int*)sigbuffer)[0], sigbuffer, ((int*)sigbuffer)[0]);
duk_pcall_method(ctx, 2); duk_pop(ctx); // ...
}
else if(bytesRead == 0 || (errno != EAGAIN && errno != EWOULDBLOCK))
{