mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-28 14:13:37 +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:
@@ -1509,12 +1509,23 @@ duk_ret_t ILibDuktape_MeshAgent_NetInfo(duk_context *ctx)
|
|||||||
gwname = child.stdout.str.trim();\
|
gwname = child.stdout.str.trim();\
|
||||||
child = require('child_process').execFile('/bin/sh', ['sh']); \
|
child = require('child_process').execFile('/bin/sh', ['sh']); \
|
||||||
child.stdout.str = ''; child.stdout.on('data', function(c) { this.str += c.toString(); }); \
|
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();\
|
child.waitExit();\
|
||||||
var tmp = child.stdout.str.trim().split(',');\
|
var tmp = child.stdout.str.trim().split(',');\
|
||||||
child = require('child_process').execFile('/bin/sh', ['sh']);\
|
child = require('child_process').execFile('/bin/sh', ['sh']);\
|
||||||
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });\
|
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();\
|
child.waitExit();\
|
||||||
var dvname = child.stdout.str.trim();\
|
var dvname = child.stdout.str.trim();\
|
||||||
var tmp2 = tmp[1].split(':');\
|
var tmp2 = tmp[1].split(':');\
|
||||||
|
|||||||
@@ -280,6 +280,7 @@ duk_ret_t ILibDuktape_SpawnedProcess_SIGCHLD_sink(duk_context *ctx)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
printf("Not DETACHED\n");
|
||||||
// We are not detached, so we need to call the same method that broken pipe would've
|
// 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);
|
ILibDuktape_ChildProcess_SubProcess *childprocess = (ILibDuktape_ChildProcess_SubProcess*)Duktape_GetBufferProperty(ctx, -1, ILibDuktape_ChildProcess_MemBuf);
|
||||||
if (childprocess != NULL)
|
if (childprocess != NULL)
|
||||||
|
|||||||
@@ -970,7 +970,39 @@ duk_ret_t ILibDuktape_Process_cwd(duk_context *ctx)
|
|||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef _POSIX
|
#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:
|
||||||
|
ILibDuktape_EventEmitter_SetupEmit(ctx, h, "SIGTERM"); // [emit][this][SIGTERM]
|
||||||
|
duk_push_string(ctx, SIGTABLE[((int*)sigbuffer)[1]]); // [emit][this][SIGTERM][name]
|
||||||
|
if (duk_pcall_method(ctx, 2) != 0) { ILibDuktape_Process_UncaughtExceptionEx(ctx, "Error Emitting SIGTERM: "); }
|
||||||
|
duk_pop(ctx);
|
||||||
|
break;
|
||||||
|
case SIGCHLD:
|
||||||
|
s = 0;
|
||||||
|
waitpid(((pid_t*)sigbuffer)[2], &s, 0);
|
||||||
|
ILibDuktape_EventEmitter_SetupEmit(ctx, h, "SIGCHLD"); // [emit][this][SIGCHLD]
|
||||||
|
duk_push_string(ctx, SIGTABLE[((int*)sigbuffer)[1]]); // [emit][this][SIGTERM][name]
|
||||||
|
duk_push_int(ctx, s); // [emit][this][SIGCHLD][name][code]
|
||||||
|
duk_push_int(ctx, ((pid_t*)sigbuffer)[2]); // [emit][this][SIGCHLD][name][code][pid]
|
||||||
|
duk_push_uint(ctx, ((uid_t*)sigbuffer)[3]); // [emit][this][SIGCHLD][name][code][pid][uid]
|
||||||
|
if (duk_pcall_method(ctx, 5) != 0) { ILibDuktape_Process_UncaughtExceptionEx(ctx, "Error Emitting SIGCHLD: "); }
|
||||||
|
duk_pop(ctx);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
void ILibDuktape_ScriptContainer_Process_SignalListener_PreSelect(void* object, fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime)
|
void ILibDuktape_ScriptContainer_Process_SignalListener_PreSelect(void* object, fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime)
|
||||||
{
|
{
|
||||||
if (SignalDescriptors[0] != 0)
|
if (SignalDescriptors[0] != 0)
|
||||||
@@ -986,34 +1018,20 @@ void ILibDuktape_ScriptContainer_Process_SignalListener_PostSelect(void* object,
|
|||||||
ILibChain_Link *link = (ILibChain_Link*)object;
|
ILibChain_Link *link = (ILibChain_Link*)object;
|
||||||
duk_context *ctx = (duk_context*)((void**)link->ExtraMemoryPtr)[0];
|
duk_context *ctx = (duk_context*)((void**)link->ExtraMemoryPtr)[0];
|
||||||
void *h = ((void**)link->ExtraMemoryPtr)[1];
|
void *h = ((void**)link->ExtraMemoryPtr)[1];
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
if (FD_ISSET(SignalDescriptors[0], readset))
|
if (FD_ISSET(SignalDescriptors[0], readset))
|
||||||
{
|
{
|
||||||
if((bytesRead = read(SignalDescriptors[0], sigbuffer, sizeof(int))) == sizeof(int) && ((int*)sigbuffer)[0] < sizeof(sigbuffer) &&
|
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])
|
(bytesRead += read(SignalDescriptors[0], sigbuffer + sizeof(int), ((int*)sigbuffer)[0])) == ((int*)sigbuffer)[0])
|
||||||
{
|
{
|
||||||
switch (((int*)sigbuffer)[1])
|
duk_push_global_object(ctx); //[g]
|
||||||
{
|
duk_get_prop_string(ctx, -1, "setImmediate"); //[g][immediate]
|
||||||
case SIGTERM:
|
duk_swap_top(ctx, -2); //[immediate][this]
|
||||||
ILibDuktape_EventEmitter_SetupEmit(ctx, h, "SIGTERM"); // [emit][this][SIGTERM]
|
duk_push_c_function(ctx, ILibDuktape_ScriptContainer_Process_SignalListener_Immediate, DUK_VARARGS);
|
||||||
duk_push_string(ctx, SIGTABLE[((int*)sigbuffer)[1]]); // [emit][this][SIGTERM][name]
|
tmp = duk_push_fixed_buffer(ctx, ((int*)sigbuffer)[0]); //[immediate][this][func][buffer]
|
||||||
if (duk_pcall_method(ctx, 2) != 0) { ILibDuktape_Process_UncaughtExceptionEx(ctx, "Error Emitting SIGTERM: "); }
|
memcpy_s(tmp, ((int*)sigbuffer)[0], sigbuffer, ((int*)sigbuffer)[0]);
|
||||||
duk_pop(ctx);
|
duk_pcall_method(ctx, 2); duk_pop(ctx); // ...
|
||||||
break;
|
|
||||||
case SIGCHLD:
|
|
||||||
s = 0;
|
|
||||||
waitpid(((pid_t*)sigbuffer)[2], &s, 0);
|
|
||||||
ILibDuktape_EventEmitter_SetupEmit(ctx, h, "SIGCHLD"); // [emit][this][SIGCHLD]
|
|
||||||
duk_push_string(ctx, SIGTABLE[((int*)sigbuffer)[1]]); // [emit][this][SIGTERM][name]
|
|
||||||
duk_push_int(ctx, s); // [emit][this][SIGCHLD][name][code]
|
|
||||||
duk_push_int(ctx, ((pid_t*)sigbuffer)[2]); // [emit][this][SIGCHLD][name][code][pid]
|
|
||||||
duk_push_uint(ctx, ((uid_t*)sigbuffer)[3]); // [emit][this][SIGCHLD][name][code][pid][uid]
|
|
||||||
if (duk_pcall_method(ctx, 5) != 0) { ILibDuktape_Process_UncaughtExceptionEx(ctx, "Error Emitting SIGCHLD: "); }
|
|
||||||
duk_pop(ctx);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if(bytesRead == 0 || (errno != EAGAIN && errno != EWOULDBLOCK))
|
else if(bytesRead == 0 || (errno != EAGAIN && errno != EWOULDBLOCK))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user