diff --git a/lib/util/devutil.js b/lib/util/devutil.js index 6eb128d9..8ae09440 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -43,6 +43,8 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { return new Promise(function(resolve) { var header = true var pids = [] + var showTotalPid = false + out.pipe(split()) .on('data', function(chunk) { if (header) { @@ -50,26 +52,39 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { } else { var cols = chunk.toString().split(/\s+/) - if (cols.pop() === comm && users[cols[0]]) { + if (!showTotalPid && cols[0] === 'root') { + showTotalPid = true + } + + // last column of output would be command name containing absolute path like '/data/local/tmp/minicap' + // or just binary name like 'minicap', it depends on device/ROM + var lastCol = cols.pop() + if ((lastCol === comm || lastCol === bin) && users[cols[0]]) { pids.push(Number(cols[1])) } } }) .on('end', function() { - resolve(pids) + resolve({showTotalPid: showTotalPid, pids: pids}) }) }) } return adb.shell(serial, 'ps 2>/dev/null') .then(findProcess) - .then(function(pids) { - if (pids.length > 0) { // return pids if process can be found in the output of 'ps' command - return Promise.resolve(pids) + .then(function(res) { + // return pids if process can be found in the output of 'ps' command + // or 'ps' command has already displayed all the processes including processes launched by root user + if (res.showTotalPid || res.pids.length > 0) { + return Promise.resolve(res.pids) } - else { // otherwise try to run 'ps -elf' + // otherwise try to run 'ps -elf' + else { return adb.shell(serial, 'ps -lef 2>/dev/null') .then(findProcess) + .then(function(res) { + return Promise.resolve(res.pids) + }) } }) }