1// Copyright 2019 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:async'; 6 7import 'base/io.dart'; 8import 'base/process_manager.dart'; 9import 'convert.dart'; 10import 'device.dart'; 11 12/// Kills a process on linux or macOS. 13Future<bool> killProcess(String executable) async { 14 if (executable == null) { 15 return false; 16 } 17 final RegExp whitespace = RegExp(r'\s+'); 18 bool succeeded = true; 19 try { 20 final ProcessResult result = await processManager.run(<String>[ 21 'ps', 'aux', 22 ]); 23 if (result.exitCode != 0) { 24 return false; 25 } 26 final List<String> lines = result.stdout.split('\n'); 27 for (String line in lines) { 28 if (!line.contains(executable)) { 29 continue; 30 } 31 final List<String> values = line.split(whitespace); 32 if (values.length < 2) { 33 continue; 34 } 35 final String processPid = values[1]; 36 final String currentRunningProcessPid = pid.toString(); 37 // Don't kill the flutter tool process 38 if (processPid == currentRunningProcessPid) { 39 continue; 40 } 41 42 final ProcessResult killResult = await processManager.run(<String>[ 43 'kill', processPid, 44 ]); 45 succeeded &= killResult.exitCode == 0; 46 } 47 return true; 48 } on ArgumentError { 49 succeeded = false; 50 } 51 return succeeded; 52} 53 54class DesktopLogReader extends DeviceLogReader { 55 final StreamController<List<int>> _inputController = StreamController<List<int>>.broadcast(); 56 57 void initializeProcess(Process process) { 58 process.stdout.listen(_inputController.add); 59 process.stderr.listen(_inputController.add); 60 process.exitCode.then((int result) { 61 _inputController.close(); 62 }); 63 } 64 65 @override 66 Stream<String> get logLines { 67 return _inputController.stream 68 .transform(utf8.decoder) 69 .transform(const LineSplitter()); 70 } 71 72 @override 73 String get name => 'desktop'; 74} 75