1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-20 02:03:15 +00:00

1. Added 'getEnvFromPid' to user-sessions, to read environment vars by PID

2 Updated getXInfo() in monitor-info, so it'll try to get XAUTHORITY from env vars is it can't determine it by looking at the XServer instance.
This commit is contained in:
Bryan Roe
2019-03-15 12:48:30 -07:00
parent 0637752d92
commit 45f5859f37
3 changed files with 62 additions and 13 deletions

File diff suppressed because one or more lines are too long

View File

@@ -330,11 +330,36 @@ function monitorinfo()
if (tokens.length == 3) if (tokens.length == 3)
{ {
ret = { tty: tokens[1], xauthority: tokens[2] }; ret = { tty: tokens[1], xauthority: tokens[2] };
} }
if(ret!=null) if (ret == null)
{ {
// This Linux Distro does not spawn an XServer instance in the user session, that specifies the XAUTHORITY.
// So we're going to brute force it, by enumerating all processes owned by this user, and inspect the environment variables
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("ps -e -o pid -o user | grep " + uname + " | awk '{ print $1 }'\nexit\n");
child.waitExit();
var lines = child.stdout.str.split('\n');
for(var n in lines)
{
var ln = lines[n].trim();
if(ln.length>0)
{
var e = require('user-sessions').getEnvFromPid(ln);
if(e.XAUTHORITY && e.DISPLAY)
{
ret = { tty: '?', xauthority: e.XAUTHORITY, display: e.DISPLAY };
return (ret);
}
}
}
}
else
{
// We need to find $DISPLAY by looking at all the processes running on the same tty as the XServer instance for this user session
child = require('child_process').execFile('/bin/sh', ['sh']); child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = ''; child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });

View File

@@ -590,6 +590,30 @@ function UserSessions()
child.waitExit(); child.waitExit();
return (child.stdout.str.trim()); return (child.stdout.str.trim());
}; };
this.getEnvFromPid = function getEnvFromPid(pid)
{
var ret = {};
var ps, psx, v, vs = 0;
try
{
ps = require('fs').readFileSync('/proc/' + pid + '/environ');
}
catch(pse)
{
return (ret);
}
for (psx = 0; psx < ps.length; ++psx)
{
if (ps[psx] == 0)
{
v = ps.slice(vs, psx).toString().split('=');
ret[v[0]] = v[1];
vs = psx + 1;
}
}
return (ret);
};
this.on('changed', this._recheckLoggedInUsers); // For linux Lock/Unlock monitoring, we need to watch for LogOn/LogOff, and keep track of the UID. this.on('changed', this._recheckLoggedInUsers); // For linux Lock/Unlock monitoring, we need to watch for LogOn/LogOff, and keep track of the UID.