diff --git a/lib/util/devutil.js b/lib/util/devutil.js index a16da2e9..45ec7a7d 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -57,12 +57,12 @@ devutil.waitForPort = function(adb, serial, port) { }) } -devutil.killProcsByComm = function(adb, serial, comm, bin) { +devutil.listPidsByComm = function(adb, serial, comm, bin) { return adb.shellAsync(serial, ['ps', comm]) .then(function(out) { return new Promise(function(resolve, reject) { var header = false - , killables = [] + , pids = [] out.pipe(split()) .on('data', function(chunk) { if (header) { @@ -71,27 +71,48 @@ devutil.killProcsByComm = function(adb, serial, comm, bin) { else { var cols = chunk.toString().split(/\s+/) if (cols.pop() === bin) { - killables.push(cols[1]) + pids.push(+cols[1]) } } }) .on('end', function() { - resolve(killables) + resolve(pids) }) }) }) - .then(function(killables) { - if (killables.length) { - return adb.shellAsync(serial, ['kill'].concat(killables)) - .then(function(out) { - return new Promise(function(resolve, reject) { - out.on('end', resolve) - }) - }) +} + +devutil.waitForProcsToDie = function(adb, serial, comm, bin) { + return devutil.listPidsByComm(adb, serial, comm, bin) + .then(function(pids) { + if (pids.length) { + return devutil.waitForProcsToDie(adb, serial, comm, bin) } }) } +devutil.killProcsByComm = function(adb, serial, comm, bin, mode) { + return devutil.listPidsByComm(adb, serial, comm, bin, mode) + .then(function(pids) { + if (!pids.length) { + return Promise.resolve() + } + return adb.shellAsync(serial, ['kill', mode || -15].concat(pids)) + .then(function(out) { + return new Promise(function(resolve, reject) { + out.on('end', resolve) + }) + }) + .then(function() { + return devutil.waitForProcsToDie(adb, serial, comm, bin) + }) + .timeout(1000) + .then(function() { + return devutil.killProcsByComm(adb, serial, comm, bin, -9) + }) + }) +} + devutil.startProp = function(adb, serial, cmd) { return adb.openTcpAsync(options.serial, port) .then(function(conn) {