/* Copyright 2021 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. */ const SYNCHRONIZE = 0x00100000; function invalid() { throw ('Not supported on ' + process.platform); } function getOpenDescriptors() { switch (process.platform) { case "freebsd": var 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 (c) { }); child.stdin.write("procstat -f " + process.pid + " | tr '\\n' '`' | awk -F'`' '"); child.stdin.write('{'); child.stdin.write(' DEL="";'); child.stdin.write(' printf "[";'); child.stdin.write(' for(i=1;i 0) { fd = fdArray.pop(); if (fd > 2) { this.libc.close(fd); } } } function _execv(exePath, argarr) { if (this.libc == null) { throw ('cannot find libc'); } var i; var tmp = []; var path = require('_GenericMarshal').CreateVariable(exePath); var args = require('_GenericMarshal').CreateVariable((1 + argarr.length) * require('_GenericMarshal').PointerSize); for (i = 0; i < argarr.length; ++i) { var arg = require('_GenericMarshal').CreateVariable(argarr[i]); tmp.push(arg); arg.pointerBuffer().copy(args.toBuffer(), i * require('_GenericMarshal').PointerSize); } var fds = this.getOpenDescriptors(); this.closeDescriptors(fds); this.libc.execv(path, args); throw('exec error'); } function getLibc() { var libs = require('monitor-info').getLibInfo('libc'); var libc = null; while (libs.length > 0) { try { libc = require('_GenericMarshal').CreateNativeProxy(libs.pop().path); libc.CreateMethod('execv'); libc.CreateMethod('close'); break; } catch (e) { libc = null; continue; } } return (libc); } function win_getProcessHandle(pid) { try { if(!this.kernel32) { this.kernel32 = require('_GenericMarshal').CreateNativeProxy('kernel32.dll'); this.kernel32.CreateMethod('OpenProcess'); } return (this.kernel32.OpenProcess(SYNCHRONIZE, 0, pid)); } catch(e) { return (null); } } switch (process.platform) { case 'linux': case 'freebsd': module.exports = { getOpenDescriptors: getOpenDescriptors, closeDescriptors: closeDescriptors, _execv: _execv, libc: getLibc() }; break; default: module.exports = { getOpenDescriptors: invalid, closeDescriptors: invalid }; break; } if (process.platform == 'win32') { module.exports.getProcessHandle = win_getProcessHandle; }