Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use spawnSync instead of execSync #884

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// ----------------------------------------------------------------------------------

const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;
const util = require('./util');

let _platform = process.platform;
Expand Down Expand Up @@ -59,7 +59,7 @@ function getLinuxAudioPci() {
let cmd = 'lspci -v 2>/dev/null';
let result = [];
try {
const parts = execSync(cmd).toString().split('\n\n');
const parts = spawnSync(cmd).toString().split('\n\n');
parts.forEach(element => {
const lines = element.split('\n');
if (lines && lines.length && lines[0].toLowerCase().indexOf('audio') >= 0) {
Expand Down
4 changes: 2 additions & 2 deletions lib/bluetooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// ----------------------------------------------------------------------------------

const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;
const path = require('path');
const util = require('./util');
const fs = require('fs');
Expand Down Expand Up @@ -129,7 +129,7 @@ function bluetoothDevices(callback) {
});
// determine "connected" with hcitool con
try {
const hdicon = execSync('hcitool con').toString().toLowerCase();
const hdicon = spawnSync('hcitool con').toString().toLowerCase();
for (let i = 0; i < result.length; i++) {
if (result[i].macDevice && result[i].macDevice.length > 10 && hdicon.indexOf(result[i].macDevice.toLowerCase()) >= 0) {
result[i].connected = true;
Expand Down
8 changes: 4 additions & 4 deletions lib/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

const os = require('os');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;
const fs = require('fs');
const util = require('./util');

Expand Down Expand Up @@ -702,7 +702,7 @@ function getCpu() {
if (os.arch() === 'arm64') {
result.socket = 'SOC';
try {
const clusters = execSync('ioreg -c IOPlatformDevice -d 3 -r | grep cluster-type').toString().split('\n');
const clusters = spawnSync('ioreg -c IOPlatformDevice -d 3 -r | grep cluster-type').toString().split('\n');
const efficiencyCores = clusters.filter(line => line.indexOf('"E"') >= 0).length;
const performanceCores = clusters.filter(line => line.indexOf('"P"') >= 0).length;
result.efficiencyCores = efficiencyCores;
Expand Down Expand Up @@ -1049,7 +1049,7 @@ function cpuTemperature(callback) {
// CPU Chipset, Socket
try {
const cmd = 'cat /sys/class/thermal/thermal_zone*/type 2>/dev/null; echo "-----"; cat /sys/class/thermal/thermal_zone*/temp 2>/dev/null;';
const parts = execSync(cmd).toString().split('-----\n');
const parts = spawnSync(cmd).toString().split('-----\n');
if (parts.length === 2) {
const lines = parts[0].split('\n');
const lines2 = parts[1].split('\n');
Expand Down Expand Up @@ -1605,7 +1605,7 @@ function getLoad() {
// linux: try to get other cpu stats
if (_linux) {
try {
const lines = execSync('cat /proc/stat 2>/dev/null | grep cpu', { encoding: 'utf8' }).toString().split('\n');
const lines = spawnSync('cat /proc/stat 2>/dev/null | grep cpu', { encoding: 'utf8' }).toString().split('\n');
if (lines.length > 1) {
lines.shift();
if (lines.length === cpus.length) {
Expand Down
18 changes: 9 additions & 9 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const util = require('./util');
const fs = require('fs');

const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;
const execPromiseSave = util.promisifySave(require('child_process').exec);

let _platform = process.platform;
Expand Down Expand Up @@ -124,10 +124,10 @@ function fsSize(drive, callback) {
if (_darwin) {
cmd = 'df -kP';
try {
macOsDisks = execSync('diskutil list').toString().split('\n').filter(line => {
macOsDisks = spawnSync('diskutil list').toString().split('\n').filter(line => {
return !line.startsWith('/') && line.indexOf(':') > 0;
});
execSync('mount').toString().split('\n').filter(line => {
spawnSync('mount').toString().split('\n').filter(line => {
return line.startsWith('/');
}).forEach((line) => {
osMounts[line.split(' ')[0]] = line.toLowerCase().indexOf('read-only') === -1;
Expand All @@ -139,7 +139,7 @@ function fsSize(drive, callback) {
if (_linux) {
try {
cmd = 'export LC_ALL=C; df -lkPTx squashfs; unset LC_ALL';
execSync('cat /proc/mounts 2>/dev/null').toString().split('\n').filter(line => {
spawnSync('cat /proc/mounts 2>/dev/null').toString().split('\n').filter(line => {
return line.startsWith('/');
}).forEach((line) => {
osMounts[line.split(' ')[0]] = osMounts[line.split(' ')[0]] || false;
Expand All @@ -154,7 +154,7 @@ function fsSize(drive, callback) {
if (_freebsd || _openbsd || _netbsd) {
try {
cmd = 'df -lkPT';
execSync('mount').toString().split('\n').forEach((line) => {
spawnSync('mount').toString().split('\n').forEach((line) => {
osMounts[line.split(' ')[0]] = line.toLowerCase().indexOf('read-only') === -1;
});
} catch (e) {
Expand Down Expand Up @@ -426,7 +426,7 @@ function raidMatchLinux(data) {
try {
data.forEach(element => {
if (element.type.startsWith('raid')) {
const lines = execSync(`mdadm --export --detail /dev/${element.name}`).toString().split('\n');
const lines = spawnSync(`mdadm --export --detail /dev/${element.name}`).toString().split('\n');
const mdData = decodeMdabmData(lines);

element.label = mdData.label; // <- assign label info
Expand Down Expand Up @@ -1087,7 +1087,7 @@ function diskLayout(callback) {
} catch (e) {
// fallback to older version of lsblk
try {
const out2 = execSync('export LC_ALL=C; lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER,GROUP 2>/dev/null; unset LC_ALL').toString();
const out2 = spawnSync('export LC_ALL=C; lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER,GROUP 2>/dev/null; unset LC_ALL').toString();
let lines = blkStdoutToObject(out2).split('\n');
const data = parseBlk(lines);
devices = data.filter(item => { return (item.type === 'disk') && item.size > 0 && ((item.model !== null && item.model !== '') || (item.mount === '' && item.label === '' && item.fsType === '')); });
Expand All @@ -1100,7 +1100,7 @@ function diskLayout(callback) {
const BSDName = '/dev/' + device.name;
const logical = device.name;
try {
mediumType = execSync('cat /sys/block/' + logical + '/queue/rotational 2>/dev/null').toString().split('\n')[0];
mediumType = spawnSync('cat /sys/block/' + logical + '/queue/rotational 2>/dev/null').toString().split('\n')[0];
} catch (e) {
util.noop();
}
Expand Down Expand Up @@ -1406,7 +1406,7 @@ function diskLayout(callback) {
workload.push(util.powerShell('Get-PhysicalDisk | select BusType,MediaType,FriendlyName,Model,SerialNumber,Size | fl'));
if (util.smartMonToolsInstalled()) {
try {
const smartDev = JSON.parse(execSync('smartctl --scan -j').toString());
const smartDev = JSON.parse(spawnSync('smartctl --scan -j').toString());
if (smartDev && smartDev.devices && smartDev.devices.length > 0) {
smartDev.devices.forEach((dev) => {
workload.push(execPromiseSave(`smartctl -j -a ${dev.name}`, util.execOptsWin));
Expand Down
8 changes: 4 additions & 4 deletions lib/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

const fs = require('fs');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;
const util = require('./util');

let _platform = process.platform;
Expand Down Expand Up @@ -216,7 +216,7 @@ function graphics(callback) {
// PCI bus IDs
let pciIDs = [];
try {
pciIDs = execSync('export LC_ALL=C; dmidecode -t 9 2>/dev/null; unset LC_ALL | grep "Bus Address: "').toString().split('\n');
pciIDs = spawnSync('export LC_ALL=C; dmidecode -t 9 2>/dev/null; unset LC_ALL | grep "Bus Address: "').toString().split('\n');
for (let i = 0; i < pciIDs.length; i++) {
pciIDs[i] = pciIDs[i].replace('Bus Address:', '').replace('0000:', '').trim();
}
Expand Down Expand Up @@ -422,7 +422,7 @@ function graphics(callback) {
const nvidiaSmiOpts = '--query-gpu=driver_version,pci.sub_device_id,name,pci.bus_id,fan.speed,memory.total,memory.used,memory.free,utilization.gpu,utilization.memory,temperature.gpu,temperature.memory,power.draw,power.limit,clocks.gr,clocks.mem --format=csv,noheader,nounits';
const cmd = nvidiaSmiExe + ' ' + nvidiaSmiOpts + (_linux ? ' 2>/dev/null' : '');
try {
const res = execSync(cmd, options).toString();
const res = spawnSync(cmd, options).toString();
return res;
} catch (e) {
util.noop();
Expand Down Expand Up @@ -686,7 +686,7 @@ function graphics(callback) {
util.noop();
}
try {
stdout = execSync('defaults read /Library/Preferences/com.apple.windowserver.plist 2>/dev/null;defaults read /Library/Preferences/com.apple.windowserver.displays.plist 2>/dev/null; echo ""', { maxBuffer: 1024 * 20000 });
stdout = spawnSync('defaults read /Library/Preferences/com.apple.windowserver.plist 2>/dev/null;defaults read /Library/Preferences/com.apple.windowserver.displays.plist 2>/dev/null; echo ""', { maxBuffer: 1024 * 20000 });
const output = (stdout || '').toString();
const obj = util.plistReader(output);
if (obj['DisplayAnyUserSets'] && obj['DisplayAnyUserSets']['Configs'] && obj['DisplayAnyUserSets']['Configs'][0] && obj['DisplayAnyUserSets']['Configs'][0]['DisplayConfig']) {
Expand Down
10 changes: 5 additions & 5 deletions lib/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

const os = require('os');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;
const util = require('./util');
const fs = require('fs');

Expand Down Expand Up @@ -244,7 +244,7 @@ function mem(callback) {
if (_darwin) {
let pageSize = 4096;
try {
let sysPpageSize = util.toInt(execSync('sysctl -n vm.pagesize').toString());
let sysPpageSize = util.toInt(spawnSync('sysctl -n vm.pagesize').toString());
pageSize = sysPpageSize || pageSize;
} catch (e) {
util.noop();
Expand Down Expand Up @@ -399,7 +399,7 @@ function memLayout(callback) {

// Try Raspberry PI
try {
let stdout = execSync('cat /proc/cpuinfo 2>/dev/null');
let stdout = spawnSync('cat /proc/cpuinfo 2>/dev/null');
let lines = stdout.toString().split('\n');
let model = util.getValue(lines, 'hardware', ':', true).toUpperCase();
let version = util.getValue(lines, 'revision', ':', true).toLowerCase();
Expand All @@ -419,14 +419,14 @@ function memLayout(callback) {
result[0].clockSpeed = version && version[4] && version[4] === 'd' ? 500 : result[0].clockSpeed;
result[0].formFactor = 'SoC';

stdout = execSync('vcgencmd get_config sdram_freq 2>/dev/null');
stdout = spawnSync('vcgencmd get_config sdram_freq 2>/dev/null');
lines = stdout.toString().split('\n');
let freq = parseInt(util.getValue(lines, 'sdram_freq', '=', true), 10) || 0;
if (freq) {
result[0].clockSpeed = freq;
}

stdout = execSync('vcgencmd measure_volts sdram_p 2>/dev/null');
stdout = spawnSync('vcgencmd measure_volts sdram_p 2>/dev/null');
lines = stdout.toString().split('\n');
let voltage = parseFloat(util.getValue(lines, 'volt', '=', true)) || 0;
if (voltage) {
Expand Down
12 changes: 6 additions & 6 deletions lib/osinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const os = require('os');
const fs = require('fs');
const util = require('./util');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;

let _platform = process.platform;

Expand Down Expand Up @@ -168,13 +168,13 @@ function getFQDN() {
let fqdn = os.hostname;
if (_linux || _darwin) {
try {
const stdout = execSync('hostnamectl --json short 2>/dev/null');
const stdout = spawnSync('hostnamectl --json short 2>/dev/null');
const json = JSON.parse(stdout.toString());

fqdn = json['StaticHostname'];
} catch (e) {
try {
const stdout = execSync('hostname -f 2>/dev/null');
const stdout = spawnSync('hostname -f 2>/dev/null');
fqdn = stdout.toString().split(os.EOL)[0];
} catch (e) {
util.noop();
Expand All @@ -183,15 +183,15 @@ function getFQDN() {
}
if (_freebsd || _openbsd || _netbsd) {
try {
const stdout = execSync('hostname 2>/dev/null');
const stdout = spawnSync('hostname 2>/dev/null');
fqdn = stdout.toString().split(os.EOL)[0];
} catch (e) {
util.noop();
}
}
if (_windows) {
try {
const stdout = execSync('echo %COMPUTERNAME%.%USERDNSDOMAIN%', util.execOptsWin);
const stdout = spawnSync('echo %COMPUTERNAME%.%USERDNSDOMAIN%', util.execOptsWin);
fqdn = stdout.toString().replace('.%USERDNSDOMAIN%', '').split(os.EOL)[0];
} catch (e) {
util.noop();
Expand Down Expand Up @@ -791,7 +791,7 @@ function versions(apps, callback) {
if ({}.hasOwnProperty.call(appsObj.versions, 'python')) {
if (_darwin) {
try {
const stdout = execSync('sw_vers');
const stdout = spawnSync('sw_vers');
const lines = stdout.toString().split('\n');
const osVersion = util.getValue(lines, 'ProductVersion', ':');
const gitHomebrewExists1 = fs.existsSync('/usr/local/Cellar/python');
Expand Down
8 changes: 4 additions & 4 deletions lib/processes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const os = require('os');
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;

const util = require('./util');

Expand Down Expand Up @@ -152,7 +152,7 @@ function services(srv, callback) {
if (_linux || _freebsd || _openbsd || _netbsd || _darwin) {
if ((_linux || _freebsd || _openbsd || _netbsd) && srvString === '*') {
try {
const tmpsrv = execSync('systemctl --all --type=service --no-legend 2> /dev/null').toString().split('\n');
const tmpsrv = spawnSync('systemctl --all --type=service --no-legend 2> /dev/null').toString().split('\n');
srvs = [];
for (const s of tmpsrv) {
const name = s.split('.service')[0];
Expand All @@ -164,7 +164,7 @@ function services(srv, callback) {
} catch (d) {
try {
srvString = '';
const tmpsrv = execSync('service --status-all 2> /dev/null').toString().split('\n');
const tmpsrv = spawnSync('service --status-all 2> /dev/null').toString().split('\n');
for (const s of tmpsrv) {
const parts = s.split(']');
if (parts.length === 2) {
Expand All @@ -174,7 +174,7 @@ function services(srv, callback) {
srvs = srvString.split('|');
} catch (e) {
try {
const srvStr = execSync('ls /etc/init.d/ -m 2> /dev/null').toString().split('\n').join('');
const srvStr = spawnSync('ls /etc/init.d/ -m 2> /dev/null').toString().split('\n').join('');
srvString = '';
if (srvStr) {
const tmpsrv = srvStr.split(',');
Expand Down
14 changes: 7 additions & 7 deletions lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const fs = require('fs');
const os = require('os');
const util = require('./util');
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;
const execPromise = util.promisify(require('child_process').exec);

let _platform = process.platform;
Expand Down Expand Up @@ -61,7 +61,7 @@ function system(callback) {
echo -n "product_version: "; cat /sys/devices/virtual/dmi/id/product_version 2>/dev/null; echo;
echo -n "sys_vendor: "; cat /sys/devices/virtual/dmi/id/sys_vendor 2>/dev/null; echo;`;
try {
lines = execSync(cmd).toString().split('\n');
lines = spawnSync(cmd).toString().split('\n');
result.manufacturer = result.manufacturer === '' ? util.getValue(lines, 'sys_vendor') : result.manufacturer;
result.model = result.model === '' ? util.getValue(lines, 'product_name') : result.model;
result.version = result.version === '' ? util.getValue(lines, 'product_version') : result.version;
Expand Down Expand Up @@ -107,7 +107,7 @@ function system(callback) {
}
if (!result.virtual) {
try {
const disksById = execSync('ls -1 /dev/disk/by-id/ 2>/dev/null').toString();
const disksById = spawnSync('ls -1 /dev/disk/by-id/ 2>/dev/null').toString();
if (disksById.indexOf('_QEMU_') >= 0) {
result.virtual = true;
result.virtualHost = 'QEMU';
Expand All @@ -129,7 +129,7 @@ function system(callback) {
}
if ((_freebsd || _openbsd || _netbsd) && !result.virtualHost) {
try {
const procInfo = execSync('dmidecode -t 4');
const procInfo = spawnSync('dmidecode -t 4');
const procLines = procInfo.toString().split('\n');
const procManufacturer = util.getValue(procLines, 'manufacturer', ':', true);
switch (procManufacturer.toLowerCase()) {
Expand All @@ -155,7 +155,7 @@ function system(callback) {
result.model = 'Docker Container';
}
try {
const stdout = execSync('dmesg 2>/dev/null | grep -iE "virtual|hypervisor" | grep -iE "vmware|qemu|kvm|xen" | grep -viE "Nested Virtualization|/virtual/"');
const stdout = spawnSync('dmesg 2>/dev/null | grep -iE "virtual|hypervisor" | grep -iE "vmware|qemu|kvm|xen" | grep -viE "Nested Virtualization|/virtual/"');
// detect virtual machines
let lines = stdout.toString().split('\n');
if (lines.length > 0) {
Expand Down Expand Up @@ -369,7 +369,7 @@ function bios(callback) {
echo -n "bios_vendor: "; cat /sys/devices/virtual/dmi/id/bios_vendor 2>/dev/null; echo;
echo -n "bios_version: "; cat /sys/devices/virtual/dmi/id/bios_version 2>/dev/null; echo;`;
try {
lines = execSync(cmd).toString().split('\n');
lines = spawnSync(cmd).toString().split('\n');
result.vendor = !result.vendor ? util.getValue(lines, 'bios_vendor') : result.vendor;
result.version = !result.version ? util.getValue(lines, 'bios_version') : result.version;
datetime = util.getValue(lines, 'bios_date');
Expand Down Expand Up @@ -483,7 +483,7 @@ function baseboard(callback) {
echo -n "board_vendor: "; cat /sys/devices/virtual/dmi/id/board_vendor 2>/dev/null; echo;
echo -n "board_version: "; cat /sys/devices/virtual/dmi/id/board_version 2>/dev/null; echo;`;
try {
lines = execSync(cmd).toString().split('\n');
lines = spawnSync(cmd).toString().split('\n');
result.manufacturer = !result.manufacturer ? util.getValue(lines, 'board_vendor') : result.manufacturer;
result.model = !result.model ? util.getValue(lines, 'board_name') : result.model;
result.version = !result.version ? util.getValue(lines, 'board_version') : result.version;
Expand Down
Loading