[core] Close fd (#1161)

This commit is contained in:
LoveSy 2021-09-23 22:53:54 +08:00 committed by GitHub
parent 1588d6160c
commit fb9ab9d9bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 32 deletions

View File

@ -68,7 +68,7 @@ public class ActivityController extends IActivityController.Stub {
return instance; return instance;
} }
static boolean replaceShellCommand(IBinder am, Parcel data) { static boolean replaceShellCommand(IBinder am, Parcel data, Parcel reply) {
if (!inited) return false; if (!inited) return false;
try { try {
data.setDataPosition(fdSize * 3); data.setDataPosition(fdSize * 3);
@ -76,42 +76,45 @@ public class ActivityController extends IActivityController.Stub {
if (args.length > 0 && "monitor".equals(args[0])) { if (args.length > 0 && "monitor".equals(args[0])) {
data.setDataPosition(0); data.setDataPosition(0);
var in = data.readFileDescriptor(); try (var in = data.readFileDescriptor();
var out = data.readFileDescriptor(); var out = data.readFileDescriptor();
var err = data.readFileDescriptor(); var err = data.readFileDescriptor()) {
data.createStringArray(); data.createStringArray();
ShellCallback shellCallback = ShellCallback.CREATOR.createFromParcel(data); ShellCallback shellCallback = ShellCallback.CREATOR.createFromParcel(data);
ResultReceiver resultReceiver = ResultReceiver.CREATOR.createFromParcel(data); ResultReceiver resultReceiver = ResultReceiver.CREATOR.createFromParcel(data);
new ShellCommand() { new ShellCommand() {
@Override @Override
public int onCommand(String cmd) { public int onCommand(String cmd) {
final PrintWriter pw = getOutPrintWriter(); final PrintWriter pw = getOutPrintWriter();
String opt; String opt;
String gdbPort = null; String gdbPort = null;
boolean monkey = false; boolean monkey = false;
while ((opt = getNextOption()) != null) { while ((opt = getNextOption()) != null) {
if (opt.equals("--gdb")) { if (opt.equals("--gdb")) {
gdbPort = getNextArgRequired(); gdbPort = getNextArgRequired();
} else if (opt.equals("-m")) { } else if (opt.equals("-m")) {
monkey = true; monkey = true;
} else { } else {
getErrPrintWriter().println("Error: Unknown option: " + opt); getErrPrintWriter().println("Error: Unknown option: " + opt);
return -1; return -1;
}
} }
return replaceMyControllerActivity(am, pw, getRawInputStream(), gdbPort, monkey);
} }
return replaceMyControllerActivity(am, pw, getRawInputStream(), gdbPort, monkey); @Override
} public void onHelp() {
@Override }
public void onHelp() { }.exec((Binder) am, in.getFileDescriptor(), out.getFileDescriptor(), err.getFileDescriptor(), args, shellCallback, resultReceiver);
} catch (Throwable e) {
} Log.e(TAG, "replace shell command", e);
}.exec((Binder) am, in.getFileDescriptor(), out.getFileDescriptor(), err.getFileDescriptor(), args, shellCallback, resultReceiver); } finally {
if (reply != null) reply.writeNoException();
}
return true; return true;
} }
} catch (Throwable e) {
Log.e(TAG, "replace shell command", e);
} finally { } finally {
data.setDataPosition(0); data.setDataPosition(0);
} }

View File

@ -297,7 +297,7 @@ public class BridgeService {
!descriptor.equals("com.sonymobile.hookservice.HookActivityService"))) { !descriptor.equals("com.sonymobile.hookservice.HookActivityService"))) {
return false; return false;
} }
return ActivityController.replaceShellCommand(obj, data); return ActivityController.replaceShellCommand(obj, data, reply);
} catch (Throwable e) { } catch (Throwable e) {
Log.e(TAG, "replace shell command", e); Log.e(TAG, "replace shell command", e);
return false; return false;