From fa50c56bb9d6c59b6a92c8cfbdd996fc60cee85b Mon Sep 17 00:00:00 2001 From: song Date: Sun, 4 Feb 2018 18:48:28 +0800 Subject: [PATCH 1/9] Use 'ps -lef' instead of 'ps' to list Pids on Android 8.x --- lib/util/devutil.js | 64 +++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/lib/util/devutil.js b/lib/util/devutil.js index 3cd1ef19..061a2631 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -5,6 +5,8 @@ var Promise = require('bluebird') var devutil = module.exports = Object.create(null) +var commands = Object.create(null) + function closedError(err) { return err.message.indexOf('closed') !== -1 } @@ -39,28 +41,46 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { shell: true } - return adb.shell(serial, ['ps']) - .then(function(out) { - return new Promise(function(resolve) { - var header = true - var pids = [] - out.pipe(split()) - .on('data', function(chunk) { - if (header) { - header = false - } - else { - var cols = chunk.toString().split(/\s+/) - if (cols.pop() === bin && users[cols[0]]) { - pids.push(Number(cols[1])) - } - } - }) - .on('end', function() { - resolve(pids) - }) - }) - }) + return new Promise(function(resolve) { + if ('ps' in commands) { + resolve(commands) + } else { + adb.getProperties(serial) + .then(function (properties) { + version = properties['ro.build.version.release'] + if (version.match(/^8\./) != null) { + commands['ps'] = 'ps -lef' + } else { + commands['ps'] = 'ps' + } + resolve(commands) + }) + } + }) + .then(function (commands) { + return adb.shell(serial, [commands['ps']]) + .then(function (out) { + return new Promise(function (resolve) { + var header = true + var pids = [] + out.pipe(split()) + .on('data', function (chunk) { + if (header) { + header = false + } + else { + var cols = chunk.toString().split(/\s+/) + if (cols.pop() === bin && users[cols[0]]) { + pids.push(Number(cols[1])) + } + } + }) + .on('end', function () { + resolve(pids) + }) + }) + }) + }) } devutil.waitForProcsToDie = function(adb, serial, comm, bin) { From bf1283292e646add4b9097ddc1cbd2493b59e428 Mon Sep 17 00:00:00 2001 From: song Date: Sun, 4 Feb 2018 20:36:38 +0800 Subject: [PATCH 2/9] code changes to make ESLint happy --- lib/units/device/plugins/screen/stream.js | 2 +- lib/util/devutil.js | 28 ++++++++++++----------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/units/device/plugins/screen/stream.js b/lib/units/device/plugins/screen/stream.js index 94f45289..455c20fb 100644 --- a/lib/units/device/plugins/screen/stream.js +++ b/lib/units/device/plugins/screen/stream.js @@ -375,7 +375,7 @@ module.exports = syrup.serial() } function kindKill() { - return kill('SIGTERM') + return kill('SIGTERM' } function forceKill() { diff --git a/lib/util/devutil.js b/lib/util/devutil.js index 061a2631..33bcba3b 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -44,27 +44,29 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { return new Promise(function(resolve) { if ('ps' in commands) { resolve(commands) - } else { + } + else { adb.getProperties(serial) - .then(function (properties) { - version = properties['ro.build.version.release'] - if (version.match(/^8\./) != null) { - commands['ps'] = 'ps -lef' - } else { - commands['ps'] = 'ps' + .then(function(properties) { + var version = properties['ro.build.version.release'] + if (version.match(/^8\./) !== null) { + commands["ps"] = 'ps -lef' + } + else { + commands["ps"] = 'ps' } resolve(commands) }) } }) - .then(function (commands) { - return adb.shell(serial, [commands['ps']]) - .then(function (out) { - return new Promise(function (resolve) { + .then(function(commands) { + return adb.shell(serial, [commands["ps"]]) + .then(function(out) { + return new Promise(function(resolve) { var header = true var pids = [] out.pipe(split()) - .on('data', function (chunk) { + .on('data', function(chunk) { if (header) { header = false } @@ -75,7 +77,7 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { } } }) - .on('end', function () { + .on('end', function() { resolve(pids) }) }) From b0c8be868580acd500f8ff2e7223c30f54790723 Mon Sep 17 00:00:00 2001 From: song Date: Sun, 4 Feb 2018 20:41:31 +0800 Subject: [PATCH 3/9] trivial fix --- lib/units/device/plugins/screen/stream.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/units/device/plugins/screen/stream.js b/lib/units/device/plugins/screen/stream.js index 455c20fb..94f45289 100644 --- a/lib/units/device/plugins/screen/stream.js +++ b/lib/units/device/plugins/screen/stream.js @@ -375,7 +375,7 @@ module.exports = syrup.serial() } function kindKill() { - return kill('SIGTERM' + return kill('SIGTERM') } function forceKill() { From 963de7fbd0e12347b75491203228e85e0d57a51f Mon Sep 17 00:00:00 2001 From: song Date: Sun, 4 Feb 2018 20:59:59 +0800 Subject: [PATCH 4/9] replace commands['ps'] with commands.ps which is suggested by ESLint --- lib/util/devutil.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/util/devutil.js b/lib/util/devutil.js index 33bcba3b..96859616 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -50,17 +50,17 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { .then(function(properties) { var version = properties['ro.build.version.release'] if (version.match(/^8\./) !== null) { - commands["ps"] = 'ps -lef' + commands.ps = 'ps -lef' } else { - commands["ps"] = 'ps' + commands.ps = 'ps' } resolve(commands) }) } }) .then(function(commands) { - return adb.shell(serial, [commands["ps"]]) + return adb.shell(serial, [commands.ps]) .then(function(out) { return new Promise(function(resolve) { var header = true From febac1dc0a30147e42c2d6ad32f49548c3002bd2 Mon Sep 17 00:00:00 2001 From: song Date: Wed, 7 Feb 2018 23:47:26 +0800 Subject: [PATCH 5/9] improve the solution with one-liner command 'ps -lef||ps' --- lib/util/devutil.js | 62 +++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/lib/util/devutil.js b/lib/util/devutil.js index 96859616..942b1c13 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -5,8 +5,6 @@ var Promise = require('bluebird') var devutil = module.exports = Object.create(null) -var commands = Object.create(null) - function closedError(err) { return err.message.indexOf('closed') !== -1 } @@ -41,48 +39,28 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { shell: true } - return new Promise(function(resolve) { - if ('ps' in commands) { - resolve(commands) - } - else { - adb.getProperties(serial) - .then(function(properties) { - var version = properties['ro.build.version.release'] - if (version.match(/^8\./) !== null) { - commands.ps = 'ps -lef' - } - else { - commands.ps = 'ps' - } - resolve(commands) - }) - } - }) - .then(function(commands) { - return adb.shell(serial, [commands.ps]) - .then(function(out) { - return new Promise(function(resolve) { - var header = true - var pids = [] - out.pipe(split()) - .on('data', function(chunk) { - if (header) { - header = false + return adb.shell(serial, 'ps -lef||ps') + .then(function(out) { + return new Promise(function(resolve) { + var header = true + var pids = [] + out.pipe(split()) + .on('data', function(chunk) { + if (header&&chunk.toString().indexOf('bad pid') === -1) { + header = false + } + else { + var cols = chunk.toString().split(/\s+/) + if (cols.pop() === bin && users[cols[0]]) { + pids.push(Number(cols[1])) } - else { - var cols = chunk.toString().split(/\s+/) - if (cols.pop() === bin && users[cols[0]]) { - pids.push(Number(cols[1])) - } - } - }) - .on('end', function() { - resolve(pids) - }) - }) + } + }) + .on('end', function() { + resolve(pids) + }) }) - }) + }) } devutil.waitForProcsToDie = function(adb, serial, comm, bin) { From 2bcc7ada21be21b65423ffe56fdd252372b6cd03 Mon Sep 17 00:00:00 2001 From: song Date: Thu, 8 Feb 2018 00:01:30 +0800 Subject: [PATCH 6/9] add space before and after logic operation --- lib/util/devutil.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util/devutil.js b/lib/util/devutil.js index 942b1c13..15436ede 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -46,7 +46,7 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { var pids = [] out.pipe(split()) .on('data', function(chunk) { - if (header&&chunk.toString().indexOf('bad pid') === -1) { + if (header && chunk.toString().indexOf('bad pid') === -1) { header = false } else { From 1d7efd06946a93e841fa8becc9f6b19988795b61 Mon Sep 17 00:00:00 2001 From: song Date: Sat, 10 Feb 2018 20:23:52 +0800 Subject: [PATCH 7/9] run 'ps' first, then if it doesn't find the target process try 'ps -elf' --- lib/util/devutil.js | 55 +++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/lib/util/devutil.js b/lib/util/devutil.js index 15436ede..de51022e 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -39,28 +39,39 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { shell: true } - return adb.shell(serial, 'ps -lef||ps') - .then(function(out) { - return new Promise(function(resolve) { - var header = true - var pids = [] - out.pipe(split()) - .on('data', function(chunk) { - if (header && chunk.toString().indexOf('bad pid') === -1) { - header = false - } - else { - var cols = chunk.toString().split(/\s+/) - if (cols.pop() === bin && users[cols[0]]) { - pids.push(Number(cols[1])) - } - } - }) - .on('end', function() { - resolve(pids) - }) - }) - }) + var findProcess = function(out) { + return new Promise(function(resolve) { + var header = true + var pids = [] + out.pipe(split()) + .on('data', function(chunk) { + if (header) { + header = false + } + else { + var cols = chunk.toString().split(/\s+/) + if (cols.pop() === bin && users[cols[0]]) { + pids.push(Number(cols[1])) + } + } + }) + .on('end', function() { + resolve(pids) + }) + }) + } + + return adb.shell(serial, 'ps') + .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) + } + else { // otherwise try to run 'ps -elf' + return adb.shell(serial, 'ps -lef') + .then(findProcess) + } + }) } devutil.waitForProcsToDie = function(adb, serial, comm, bin) { From 69e434d08ca64bc4ac8aaded584bfd17f1ff87a9 Mon Sep 17 00:00:00 2001 From: song Date: Fri, 6 Apr 2018 15:19:18 +0800 Subject: [PATCH 8/9] change 'ps -lef' to 'ps -le 2>/dev/null' to filter weird errors in output --- lib/util/devutil.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/util/devutil.js b/lib/util/devutil.js index de51022e..6eb128d9 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -50,7 +50,7 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { } else { var cols = chunk.toString().split(/\s+/) - if (cols.pop() === bin && users[cols[0]]) { + if (cols.pop() === comm && users[cols[0]]) { pids.push(Number(cols[1])) } } @@ -61,14 +61,14 @@ devutil.listPidsByComm = function(adb, serial, comm, bin) { }) } - return adb.shell(serial, 'ps') + 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) } else { // otherwise try to run 'ps -elf' - return adb.shell(serial, 'ps -lef') + return adb.shell(serial, 'ps -lef 2>/dev/null') .then(findProcess) } }) From 1dccf60fca5b9c9b159bdf44725d7c858887f598 Mon Sep 17 00:00:00 2001 From: song Date: Thu, 12 Apr 2018 00:28:10 +0800 Subject: [PATCH 9/9] detect if 'ps' command output all the processes --- lib/util/devutil.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) 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) + }) } }) }