diff --git a/meshcore/KVM/Linux/linux_kvm.c b/meshcore/KVM/Linux/linux_kvm.c index b6538a3..cd2dc3c 100644 --- a/meshcore/KVM/Linux/linux_kvm.c +++ b/meshcore/KVM/Linux/linux_kvm.c @@ -324,7 +324,9 @@ int kvm_init(int displayNo) if (count == 10) { return -1; } count = 0; eventdisplay = x11_exports->XOpenDisplay(displayString); - //fprintf(logFile, "XAUTHORITY is %s", getenv("XAUTHORITY")); fflush(logFile); + fprintf(logFile, "XAUTHORITY is %s\n", getenv("XAUTHORITY")); fflush(logFile); + fprintf(logFile, "DisplayString is %s\n", displayString); fflush(logFile); + if (eventdisplay == NULL) { char tmpBuff[1024]; @@ -535,6 +537,7 @@ void* kvm_server_mainloop(void* parm) default_JPEG_error_handler = kvm_server_jpegerror; + fprintf(logFile, "Checking $DISPLAY\n"); for (char **env = environ; *env; ++env) { int envLen = (int)strnlen_s(*env, INT_MAX); @@ -544,7 +547,7 @@ void* kvm_server_mainloop(void* parm) if (i == 7 && strncmp("DISPLAY", *env, 7) == 0) { current_display = (unsigned short)atoi(*env + i + 2); - //fprintf(logFile, "ENV[DISPLAY] = %s\n", *env + i + 2); + fprintf(logFile, "ENV[DISPLAY] = %s\n", *env + i + 2); break; } } @@ -732,7 +735,7 @@ void kvm_relay_readSink(ILibProcessPipe_Pipe sender, char *buffer, int bufferLen } *bytesConsumed = 0; } -void* kvm_relay_restart(int paused, void *processPipeMgr, ILibKVM_WriteHandler writeHandler, void *reserved, int uid, char* authToken) +void* kvm_relay_restart(int paused, void *processPipeMgr, ILibKVM_WriteHandler writeHandler, void *reserved, int uid, char* authToken, char *dispid) { int r; int count = 0; @@ -770,10 +773,9 @@ void* kvm_relay_restart(int paused, void *processPipeMgr, ILibKVM_WriteHandler w if (uid != 0) { ignore_result(setuid(uid)); } //fprintf(logFile, "Starting kvm_server_mainloop\n"); - if (authToken != NULL) - { - setenv("XAUTHORITY", authToken, 1); - } + if (authToken != NULL) { setenv("XAUTHORITY", authToken, 1); } + if (dispid != NULL) { setenv("DISPLAY", dispid, 1); } + kvm_server_mainloop((void*)0); return(NULL); } @@ -790,11 +792,11 @@ void* kvm_relay_restart(int paused, void *processPipeMgr, ILibKVM_WriteHandler w // Setup the KVM session. Return 1 if ok, 0 if it could not be setup. -void* kvm_relay_setup(void *processPipeMgr, ILibKVM_WriteHandler writeHandler, void *reserved, int uid, char *authToken) +void* kvm_relay_setup(void *processPipeMgr, ILibKVM_WriteHandler writeHandler, void *reserved, int uid, char *authToken, char *dispid) { if (kvmthread != (pthread_t)NULL || g_slavekvm != 0) return 0; g_restartcount = 0; - return kvm_relay_restart(1, processPipeMgr, writeHandler, reserved, uid, authToken); + return kvm_relay_restart(1, processPipeMgr, writeHandler, reserved, uid, authToken, dispid); } // Force a KVM reset & refresh diff --git a/meshcore/KVM/Linux/linux_kvm.h b/meshcore/KVM/Linux/linux_kvm.h index 30c9c45..14d6a34 100644 --- a/meshcore/KVM/Linux/linux_kvm.h +++ b/meshcore/KVM/Linux/linux_kvm.h @@ -40,7 +40,7 @@ typedef ILibTransport_DoneState(*ILibKVM_WriteHandler)(char *buffer, int bufferL void kvm_set_x11_locations(char *libx11, char *libx11tst, char *libx11ext); int kvm_relay_feeddata(char* buf, int len); void kvm_pause(int pause); -void* kvm_relay_setup(void *processPipeMgr, ILibKVM_WriteHandler writeHandler, void *reserved, int uid, char *authToken); +void* kvm_relay_setup(void *processPipeMgr, ILibKVM_WriteHandler writeHandler, void *reserved, int uid, char *authToken, char *dispid); void kvm_relay_reset(); void kvm_cleanup(); diff --git a/meshcore/agentcore.c b/meshcore/agentcore.c index 679e395..e06eba6 100644 --- a/meshcore/agentcore.c +++ b/meshcore/agentcore.c @@ -1425,30 +1425,18 @@ duk_ret_t ILibDuktape_MeshAgent_getRemoteDesktop(duk_context *ctx) #else // For Linux, we need to determine where the XAUTHORITY is: char *updateXAuth = NULL; + char *updateDisplay = NULL; int needPop = 0; - if (getenv("XAUTHORITY") == NULL) + if (getenv("XAUTHORITY") == NULL || getenv("DISPLAY") == NULL) { - if (duk_peval_string(ctx, "(function getAuthToken(ival)\ - {\ - var child = require('child_process').execFile('/bin/sh', ['sh']);\ - child.stdout.str = '';\ - child.stdin.write('ps -e -o user:999 -o command | grep X | awk {\\'printf \"%s,\",$1; split($0, a, \"-auth\"); split(a[2], b, \" \"); print b[1]\\'} \\nexit\\n');\ - child.stdout.on('data', function(chunk) { this.str += chunk.toString(); });\ - child.waitExit();\ - var lines = child.stdout.str.split('\\n');\ - for (var i in lines) {\ - var tokens = lines[i].split(',');\ - if (tokens[0] && tokens[1].trim().length>0 && require('user-sessions')._uids()[ival] == tokens[0]) {\ - return(tokens[1].trim());\ - }\ - }\ - return (null);\ - });") == 0) + if (duk_peval_string(ctx, "require('monitor-info').getXInfo") == 0) { duk_push_int(ctx, console_uid); if (duk_pcall(ctx, 1) == 0) { - updateXAuth = (char*)duk_get_string(ctx, -1); + updateXAuth = Duktape_GetStringPropertyValue(ctx, -1, "xauthority", NULL); + updateDisplay = Duktape_GetStringPropertyValue(ctx, -1, "display", NULL); + if (console_uid != 0 && updateXAuth == NULL) { ILibDuktape_MeshAgent_RemoteDesktop_SendError(ptrs, "Xauthority not found! Is your DM configured to use X?"); @@ -1456,16 +1444,25 @@ duk_ret_t ILibDuktape_MeshAgent_getRemoteDesktop(duk_context *ctx) return(1); } } + else + { + MeshAgent_sendConsoleText(ctx, "Error trying to determine XAUTHORITY/DISPLAY: %s ", duk_safe_to_string(ctx, -1)); + } + } + else + { + MeshAgent_sendConsoleText(ctx, "Error trying to determine XAUTHORITY/DISPLAY: %s ", duk_safe_to_string(ctx, -1)); } needPop = 1; } //MeshAgent_sendConsoleText(ctx, "Using uid: %d, XAUTHORITY: %s\n", console_uid, getenv("XAUTHORITY")==NULL? updateXAuth : getenv("XAUTHORITY")); - ptrs->kvmPipe = kvm_relay_setup(agent->pipeManager, ILibDuktape_MeshAgent_RemoteDesktop_KVM_WriteSink, ptrs, console_uid, updateXAuth); + ptrs->kvmPipe = kvm_relay_setup(agent->pipeManager, ILibDuktape_MeshAgent_RemoteDesktop_KVM_WriteSink, ptrs, console_uid, updateXAuth, updateDisplay); if (needPop!= 0) {duk_pop(ctx); } #endif #endif + return 1; #endif }