1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-20 18:23:21 +00:00

1. Fixed compiler warning

2. Updated so process.exit() throws exception when called in agent mode
This commit is contained in:
Bryan Roe
2019-05-14 14:52:39 -07:00
parent d0fb36d10b
commit 7cf000571d
6 changed files with 45 additions and 42 deletions

View File

@@ -1764,6 +1764,17 @@ duk_ret_t ILibDuktape_MeshAgent_GenerateCertsForDiagnosticAgent(duk_context *ctx
#endif #endif
} }
duk_ret_t ILibDuktape_MeshAgent_forceExit(duk_context *ctx)
{
int code = 0;
if (duk_get_top(ctx) > 0)
{
code = (int)duk_require_int(ctx, 0);
}
exit(code);
return(0);
}
void ILibDuktape_MeshAgent_PUSH(duk_context *ctx, void *chain) void ILibDuktape_MeshAgent_PUSH(duk_context *ctx, void *chain)
{ {
MeshAgentHostContainer *agent; MeshAgentHostContainer *agent;
@@ -1793,6 +1804,8 @@ void ILibDuktape_MeshAgent_PUSH(duk_context *ctx, void *chain)
duk_pop(ctx); // [MeshAgent] duk_pop(ctx); // [MeshAgent]
emitter = ILibDuktape_EventEmitter_Create(ctx); emitter = ILibDuktape_EventEmitter_Create(ctx);
duk_push_boolean(ctx, agent->agentMode);
ILibDuktape_CreateReadonlyProperty(ctx, "agentMode");
if (agent->slaveMode == 0) if (agent->slaveMode == 0)
{ {
@@ -1842,6 +1855,7 @@ void ILibDuktape_MeshAgent_PUSH(duk_context *ctx, void *chain)
ILibDuktape_CreateEventWithGetter(ctx, "NetInfo", ILibDuktape_MeshAgent_NetInfo); ILibDuktape_CreateEventWithGetter(ctx, "NetInfo", ILibDuktape_MeshAgent_NetInfo);
ILibDuktape_CreateInstanceMethod(ctx, "ExecPowerState", ILibDuktape_MeshAgent_ExecPowerState, DUK_VARARGS); ILibDuktape_CreateInstanceMethod(ctx, "ExecPowerState", ILibDuktape_MeshAgent_ExecPowerState, DUK_VARARGS);
ILibDuktape_CreateInstanceMethod(ctx, "eval", ILibDuktape_MeshAgent_eval, 1); ILibDuktape_CreateInstanceMethod(ctx, "eval", ILibDuktape_MeshAgent_eval, 1);
ILibDuktape_CreateInstanceMethod(ctx, "forceExit", ILibDuktape_MeshAgent_forceExit, DUK_VARARGS);
Duktape_CreateEnum(ctx, "ContainerPermissions", (char*[]) { "DEFAULT", "NO_AGENT", "NO_MARSHAL", "NO_PROCESS_SPAWNING", "NO_FILE_SYSTEM_ACCESS", "NO_NETWORK_ACCESS" }, (int[]) { 0x00, 0x10000000, 0x08000000, 0x04000000, 0x00000001, 0x00000002 }, 6); Duktape_CreateEnum(ctx, "ContainerPermissions", (char*[]) { "DEFAULT", "NO_AGENT", "NO_MARSHAL", "NO_PROCESS_SPAWNING", "NO_FILE_SYSTEM_ACCESS", "NO_NETWORK_ACCESS" }, (int[]) { 0x00, 0x10000000, 0x08000000, 0x04000000, 0x00000001, 0x00000002 }, 6);
@@ -2187,23 +2201,6 @@ void WritePipeResponse(AGENT_RECORD_TYPE recordType, JS_ENGINE_CONTEXT engineCon
} }
void ScriptEngine_Exit(MeshAgentHostContainer *agent, char *contextGUID, int exitCode)
{
duk_context *ctx = agent->meshCoreCtx;
if (ctx != NULL)
{
void *procObj = ILibDuktape_GetProcessObject(ctx);
duk_push_heapptr(ctx, procObj); // [process]
duk_get_prop_string(ctx, -1, "exit"); // [process][exitFunc]
duk_swap_top(ctx, -2); // [exitFunc][this/process]
duk_push_int(ctx, exitCode); // [exitFunc][this/process][exitCode]
duk_pcall_method(ctx, 1); // [retVal]
duk_pop(ctx); // ...
}
}
duk_context* ScriptEngine_Stop(MeshAgentHostContainer *agent, char *contextGUID) duk_context* ScriptEngine_Stop(MeshAgentHostContainer *agent, char *contextGUID)
{ {
duk_context *oldCtx = agent->meshCoreCtx; duk_context *oldCtx = agent->meshCoreCtx;
@@ -4101,6 +4098,7 @@ int MeshAgent_AgentMode(MeshAgentHostContainer *agentHost, int paramLen, char **
agentHost->jsDebugPort = atoi(tmp); agentHost->jsDebugPort = atoi(tmp);
} }
} }
agentHost->agentMode = 1;
if (agentHost->meshCoreCtx != NULL) if (agentHost->meshCoreCtx != NULL)
{ {
@@ -4156,7 +4154,6 @@ int MeshAgent_AgentMode(MeshAgentHostContainer *agentHost, int paramLen, char **
} }
ILibIPAddressMonitor_Create(agentHost->chain, MeshAgent_AgentMode_IPAddressChanged_Handler, agentHost); ILibIPAddressMonitor_Create(agentHost->chain, MeshAgent_AgentMode_IPAddressChanged_Handler, agentHost);
MeshServer_Connect(agentHost); MeshServer_Connect(agentHost);
@@ -4202,7 +4199,6 @@ int MeshAgent_AgentMode(MeshAgentHostContainer *agentHost, int paramLen, char **
void MeshAgent_ScriptMode_UncaughtExceptionSink(duk_context *ctx, char *msg, void *user) void MeshAgent_ScriptMode_UncaughtExceptionSink(duk_context *ctx, char *msg, void *user)
{ {
printf("*** UNCAUGHT EXCEPTION: %s ***\n", msg); printf("*** UNCAUGHT EXCEPTION: %s ***\n", msg);
//ScriptEngine_Exit((MeshAgentHostContainer*)user, MeshAgent_JavaCore_ContextGuid, 254);
} }
void MeshAgent_ScriptMode_MeshDesktop_PUSH(duk_context *ctx, void *chain) void MeshAgent_ScriptMode_MeshDesktop_PUSH(duk_context *ctx, void *chain)
@@ -4615,10 +4611,9 @@ int MeshAgent_Start(MeshAgentHostContainer *agentHost, int paramLen, char **para
{ {
#ifdef __APPLE__ #ifdef __APPLE__
case MeshAgent_Posix_PlatformTypes_LAUNCHD: case MeshAgent_Posix_PlatformTypes_LAUNCHD:
if (agentHost->logUpdate != 0) { ILIBLOGMESSSAGE("SelfUpdate -> Complete... [LAUNCHD should auto restart]"); } if (agentHost->logUpdate != 0) { ILIBLOGMESSSAGE("SelfUpdate -> Complete... [kickstarting service]"); }
write(STDOUT_FILENO, "Finishing update...\n", 20); sprintf_s(ILibScratchPad, sizeof(ILibScratchPad), "launchctl kickstart -k system/meshagent"); // Restart the service
fsync(STDOUT_FILENO); ignore_result(MeshAgent_System(ILibScratchPad));
exit(1); // We're exiting here, to restart via KeepAlive (LaunchD doesn't support an explicit 'restart')
break; break;
#endif #endif
case MeshAgent_Posix_PlatformTypes_SYSTEMD: case MeshAgent_Posix_PlatformTypes_SYSTEMD:

View File

@@ -166,6 +166,7 @@ typedef struct MeshAgentHostContainer
#endif #endif
int localScript; int localScript;
int agentMode;
int version; int version;
int capabilities; // Extra agent capabilities indicated to the server from MeshCommand_AuthInfo_CapabilitiesMask int capabilities; // Extra agent capabilities indicated to the server from MeshCommand_AuthInfo_CapabilitiesMask
char hostname[255]; char hostname[255];

View File

@@ -1,3 +1,3 @@
// This file is auto-generated, any edits may be overwritten // This file is auto-generated, any edits may be overwritten
#define SOURCE_COMMIT_DATE NULL #define SOURCE_COMMIT_DATE "2019-May-12 21:39:02-0700"
#define SOURCE_COMMIT_HASH NULL #define SOURCE_COMMIT_HASH "d0fb36d10bc2bdf6822629daa098b07a90730e88"

View File

@@ -491,6 +491,13 @@ duk_ret_t ILibDuktape_ScriptContainer_Process_Exit(duk_context *ctx)
void **tmp; void **tmp;
int nargs = duk_get_top(ctx); int nargs = duk_get_top(ctx);
if (duk_peval_string(ctx, "require('MeshAgent').agentMode") == 0 && duk_get_boolean(ctx, -1))
{
// Running in Agent Mode, so exiting process is not allowed
return(ILibDuktape_Error(ctx, "Process.exit() not allowed when running in Agent Mode"));
}
else
{
duk_push_this(ctx); // [process] duk_push_this(ctx); // [process]
if (nargs == 1) if (nargs == 1)
{ {
@@ -507,6 +514,7 @@ duk_ret_t ILibDuktape_ScriptContainer_Process_Exit(duk_context *ctx)
duk_put_prop_string(ctx, -2, "\xFF_JUNK"); // [process] duk_put_prop_string(ctx, -2, "\xFF_JUNK"); // [process]
tmp[0] = ctx; tmp[0] = ctx;
ILibLifeTime_Add(ILibGetBaseTimer(Duktape_GetChain(ctx)), tmp, 0, ILibDuktape_ScriptContainer_Process_ExitCallback, NULL); ILibLifeTime_Add(ILibGetBaseTimer(Duktape_GetChain(ctx)), tmp, 0, ILibDuktape_ScriptContainer_Process_ExitCallback, NULL);
}
return(ILibDuktape_Error(ctx, "Process.exit() forced script termination")); return(ILibDuktape_Error(ctx, "Process.exit() forced script termination"));
} }

View File

@@ -140,7 +140,6 @@ typedef struct SCRIPT_ENGINE_SETTINGS
char *argList[]; char *argList[];
}SCRIPT_ENGINE_SETTINGS; }SCRIPT_ENGINE_SETTINGS;
void ILibDuktape_ScriptContainer_CheckEmbedded(char **script, int *scriptLen); void ILibDuktape_ScriptContainer_CheckEmbedded(char **script, int *scriptLen);
void ILibDuktape_ScriptContainer_CheckEmbeddedEx(char *exePath, char **script, int *scriptLen); void ILibDuktape_ScriptContainer_CheckEmbeddedEx(char *exePath, char **script, int *scriptLen);

View File

@@ -1427,7 +1427,7 @@ int ILib_Stun_GetAttributeChangeRequestPacket(int flags, char* TransactionId, ch
((unsigned int*)rbuffer)[1] = htonl(0x2112A442); // Set the magic string ((unsigned int*)rbuffer)[1] = htonl(0x2112A442); // Set the magic string
util_random(12, TransactionId); // Random used for transaction id util_random(12, TransactionId); // Random used for transaction id
TransactionId[0] = 255; // Set the first byte to 255, so it doesn't collide with IceStateSlot TransactionId[0] = (char)255; // Set the first byte to 255, so it doesn't collide with IceStateSlot
memcpy_s(rbuffer + 8, 12, TransactionId, 12); memcpy_s(rbuffer + 8, 12, TransactionId, 12);
((unsigned short*)(rbuffer + rptr))[0] = htons(STUN_ATTRIB_CHANGE_REQUEST); // Attribute header ((unsigned short*)(rbuffer + rptr))[0] = htons(STUN_ATTRIB_CHANGE_REQUEST); // Attribute header
@@ -1449,8 +1449,8 @@ void ILib_Stun_SendAttributeChangeRequest(void* module, struct sockaddr* StunSer
((unsigned int*)rbuffer)[1] = htonl(0x2112A442); // Set the magic string ((unsigned int*)rbuffer)[1] = htonl(0x2112A442); // Set the magic string
util_random(12, StunModule->TransactionId); // Random used for transaction id util_random(12, StunModule->TransactionId); // Random used for transaction id
StunModule->TransactionId[0] = 255; // Set the first byte to 255, so it doesn't collide with IceStateSlot StunModule->TransactionId[0] = (char)255; // Set the first byte to 255, so it doesn't collide with IceStateSlot
NAT_MAPPING_DETECTION(StunModule->TransactionId) = ((flags & 0x8000)==0x8000)?255:0; // Mapping Detection vs Public Interface Only Detection NAT_MAPPING_DETECTION(StunModule->TransactionId) = (char)((flags & 0x8000)==0x8000)?(char)255:0; // Mapping Detection vs Public Interface Only Detection
memcpy_s(rbuffer + 8, sizeof(rbuffer) - 8, StunModule->TransactionId, 12); memcpy_s(rbuffer + 8, sizeof(rbuffer) - 8, StunModule->TransactionId, 12);
((unsigned short*)(rbuffer + rptr))[0] = htons(STUN_ATTRIB_CHANGE_REQUEST); // Attribute header ((unsigned short*)(rbuffer + rptr))[0] = htons(STUN_ATTRIB_CHANGE_REQUEST); // Attribute header