/* Copyright 2018 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var promise = require('promise'); var PPosition = 4; var PSize = 8; var PMinSize = 1 << 4; var PMaxSize = 1 << 5; var _NET_WM_STATE_REMOVE = 0; // remove/unset property var _NET_WM_STATE_ADD = 1; // add/set property var _NET_WM_STATE_TOGGLE = 2; // toggle property var SubstructureRedirectMask = (1 << 20); var SubstructureNotifyMask = (1 << 19); var PropModeReplace = 0; var XA_ATOM = 4; var MWM_HINTS_FUNCTIONS = (1 << 0); var MWM_HINTS_DECORATIONS = (1 << 1); var ClientMessage = 33; var CWEventMask = (1 << 11); var PropertyChangeMask = (1 << 22); var PropertyNotify = 28; var AnyPropertyType = 0; function getLibInfo(libname) { 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("whereis ldconfig | awk '{ print $2 }'\nexit\n"); child.waitExit(); if (child.stdout.str.trim() != '') { var ldconfig = child.stdout.str.trim(); child = require('child_process').execFile('/bin/sh', ['sh']); child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stdin.write(ldconfig + " -p | grep '" + libname + ".so.' | tr '\\n' '^' | awk -F^ '{ printf \"[\"; for(i=1;i<=NF;++i) {" + ' split($i, plat, ")"); split(plat[1], plat2, "("); ifox=split(plat2[2], ifo, ","); libc=""; hwcap="0"; for(ifoi=1;ifoi<=ifox;++ifoi) { if(split(ifo[ifoi], jnk, "libc")==2) { libc=ifo[ifoi]; } if(split(ifo[ifoi], jnk, "hwcap:")==2) { split(ifo[ifoi], jnk, "0x"); hwcap=jnk[2]; } } x=split($i, tok, " "); if(tok[1]!="") { printf "%s{\\"lib\\": \\"%s\\", \\"path\\": \\"%s\\", \\"hwcap\\": \\"%s\\", \\"libc\\": \\"%s\\"}", (i!=1?",":""), tok[1], tok[x], hwcap, libc; }} printf "]"; }\'\nexit\n'); child.waitExit(); try { var v = JSON.parse(child.stdout.str.trim()); if (v.length != 0) { return (v); } } catch (e) { } } // No ldconfig, or no result returned; child = require('child_process').execFile('/bin/sh', ['sh']); child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); }); child.stderr.on('data', function () { }); child.stdin.write('ls /lib/' + libname + '.*' + " | tr '\\n' '`' | awk -F'`' '{" + ' DEL=""; printf "["; for(i=1;i 0) { addWorkspaceHandler(display, info._X11); } ret._res(ifo); return (ret); } function xinfo_xdm(info, uid) { if (process.platform != 'linux') { return(info); } 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 uid -o cmd | grep X | grep " + uid + " | tr '\\n' '`' | awk '{ xl=split($2,x,\"/\"); print x[xl]; }'\nexit\n"); child.waitExit(); if(child.stdout.str.trim() != '') { if (info == null) { info = {}; } info.xdm = child.stdout.str.trim().toLowerCase(); } return (info); } this.getXInfo = function getXInfo(consoleuid) { var ret = null; var uname = require('user-sessions').getUsername(consoleuid); 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 " + (process.platform == 'freebsd'?"-ax ":"") + "-e -o user" + (process.platform=='linux'?":999":"") + " -o tty -o command | grep X | awk '{ split($0, a, \"-auth\"); split(a[2], b, \" \"); if($1==\"" + uname + "\" && b[1]!=\"\") { printf \"%s,%s,%s\",$1,$2,b[1] } }'\nexit\n"); child.waitExit(); var tokens = child.stdout.str.trim().split(','); if (tokens.length == 3) { ret = { tty: tokens[1], xauthority: tokens[2], exportEnv: exportEnv }; } if (ret == null) { // This Linux Distro does not spawn an XServer instance in the user session, that specifies the XAUTHORITY. if (process.platform == 'linux' && require('user-sessions').hasLoginCtl) { child = require('child_process').execFile('/bin/sh', ['sh']); child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); }); child.stdin.write("loginctl list-sessions | tr '\\n' '`' | awk '{"); child.stdin.write('printf "[";'); child.stdin.write('del="";'); child.stdin.write('n=split($0, lines, "`");'); child.stdin.write('for(i=1;i0) { var e = require('user-sessions').getEnvFromPid(ln); if(e.XAUTHORITY && e.DISPLAY) { ret = { tty: '?', xauthority: e.XAUTHORITY, display: e.DISPLAY, exportEnv: exportEnv }; return (xinfo_xdm(ret, consoleuid)); } } } if(ret == null) { // We couldn't find XAUTHORITY and DISPLAY, so as a last ditch effort, lets just look for DISPLAY for (var n in lines) { var ln = lines[n].trim(); if (ln.length > 0) { var e = require('user-sessions').getEnvFromPid(ln); if (e.DISPLAY) { ret = { tty: '?', display: e.DISPLAY, exportEnv: exportEnv }; return (xinfo_xdm(ret, consoleuid)); } } } } } 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.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); }); child.stdin.write("ps -e -o tty -o pid -o user:9999 | grep " + ret.tty + " | grep " + uname + " | awk '{ print $2 }' \nexit\n"); child.waitExit(); var lines = child.stdout.str.split('\n'); var ps, psx, v, vs = 0; for(var x in lines) { if(lines[x].trim().length>0) { try { ps = require('fs').readFileSync('/proc/' + lines[x].trim() + '/environ'); } catch(pse) { continue; } vs = 0; for(psx=0;psx 0) { return (result.Deref().Deref(0, 4).toBuffer().readUInt32LE()); } else { throw ('Error fetching current workspace'); } } } if (process.platform != 'darwin') { module.exports = new monitorinfo(); } if (process.platform == 'linux' || process.platform == 'freebsd') { module.exports.getLibInfo = getLibInfo; }