Start cleanup process with setsid or nohup

If available, start the cleanup process in a new session to reduce the
likelihood of it being terminated along with the scrcpy server process
on some devices.

The binaries setsid and nohup are often available, but it is not
guaranteed.

Refs #5601 <https://github.com/Genymobile/scrcpy/issues/5601>
PR #5613 <https://github.com/Genymobile/scrcpy/pull/5613>
This commit is contained in:
Romain Vimont 2024-12-05 20:08:21 +01:00
parent 2780e0bd7b
commit c59a3c3169

View File

@ -10,6 +10,8 @@ import android.os.BatteryManager;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/** /**
* Handle the cleanup of scrcpy, even if the main process is killed. * Handle the cleanup of scrcpy, even if the main process is killed.
@ -107,16 +109,22 @@ public final class CleanUp {
private void run(int displayId, int restoreStayOn, boolean disableShowTouches, boolean powerOffScreen, int restoreScreenOffTimeout) private void run(int displayId, int restoreStayOn, boolean disableShowTouches, boolean powerOffScreen, int restoreScreenOffTimeout)
throws IOException { throws IOException {
String[] cmd = {
"app_process", List<String> cmd = new ArrayList<>();
"/", if (new File("/system/bin/setsid").exists()) {
CleanUp.class.getName(), cmd.add("/system/bin/setsid");
String.valueOf(displayId), } else if (new File("/system/bin/nohup").exists()) {
String.valueOf(restoreStayOn), cmd.add("/system/bin/nohup");
String.valueOf(disableShowTouches), }
String.valueOf(powerOffScreen),
String.valueOf(restoreScreenOffTimeout), cmd.add("app_process");
}; cmd.add("/");
cmd.add(CleanUp.class.getName());
cmd.add(String.valueOf(displayId));
cmd.add(String.valueOf(restoreStayOn));
cmd.add(String.valueOf(disableShowTouches));
cmd.add(String.valueOf(powerOffScreen));
cmd.add(String.valueOf(restoreScreenOffTimeout));
ProcessBuilder builder = new ProcessBuilder(cmd); ProcessBuilder builder = new ProcessBuilder(cmd);
builder.environment().put("CLASSPATH", Server.SERVER_PATH); builder.environment().put("CLASSPATH", Server.SERVER_PATH);