mirror of
https://github.com/topjohnwu/libsu.git
synced 2024-11-27 05:50:26 +00:00
Cleanup internal Utils
This commit is contained in:
parent
6d9f30e81b
commit
93a520205e
5
core/proguard-rules.pro
vendored
5
core/proguard-rules.pro
vendored
@ -21,8 +21,9 @@
|
||||
#-renamesourcefileattribute SourceFile
|
||||
|
||||
# Strip out verbose logging
|
||||
-assumenosideeffects class com.topjohnwu.superuser.internal.InternalUtils {
|
||||
public static *** log(...);
|
||||
-assumenosideeffects class com.topjohnwu.superuser.internal.Utils {
|
||||
public static void log(...);
|
||||
public static void ex(...);
|
||||
}
|
||||
|
||||
# Make sure R8/Proguard never remove Shell.Initializer classes
|
||||
|
@ -33,7 +33,7 @@ import static com.topjohnwu.superuser.Shell.FLAG_MOUNT_MASTER;
|
||||
import static com.topjohnwu.superuser.Shell.FLAG_NON_ROOT_SHELL;
|
||||
import static com.topjohnwu.superuser.Shell.GetShellCallback;
|
||||
import static com.topjohnwu.superuser.Shell.ROOT_SHELL;
|
||||
import static com.topjohnwu.superuser.internal.InternalUtils.hasFlag;
|
||||
import static com.topjohnwu.superuser.internal.Utils.hasFlags;
|
||||
|
||||
@RestrictTo(RestrictTo.Scope.LIBRARY)
|
||||
public final class Impl {
|
||||
@ -71,7 +71,7 @@ public final class Impl {
|
||||
isInitGlobal = false;
|
||||
}
|
||||
} catch (NoShellException e) {
|
||||
InternalUtils.stackTrace(e);
|
||||
Utils.ex(e);
|
||||
return;
|
||||
}
|
||||
if (executor == null)
|
||||
@ -92,7 +92,7 @@ public final class Impl {
|
||||
ShellImpl shell = null;
|
||||
|
||||
// Root mount master
|
||||
if (!hasFlag(FLAG_NON_ROOT_SHELL) && hasFlag(FLAG_MOUNT_MASTER)) {
|
||||
if (!hasFlags(FLAG_NON_ROOT_SHELL) && hasFlags(FLAG_MOUNT_MASTER)) {
|
||||
try {
|
||||
shell = newShell("su", "--mount-master");
|
||||
if (shell.getStatus() != Shell.ROOT_MOUNT_MASTER)
|
||||
@ -101,7 +101,7 @@ public final class Impl {
|
||||
}
|
||||
|
||||
// Normal root shell
|
||||
if (shell == null && !hasFlag(FLAG_NON_ROOT_SHELL)) {
|
||||
if (shell == null && !hasFlags(FLAG_NON_ROOT_SHELL)) {
|
||||
try {
|
||||
shell = newShell("su");
|
||||
if (shell.getStatus() != ROOT_SHELL)
|
||||
@ -120,7 +120,7 @@ public final class Impl {
|
||||
try {
|
||||
ShellImpl shell = new ShellImpl(timeout, commands);
|
||||
try {
|
||||
Context ctx = InternalUtils.getContext();
|
||||
Context ctx = Utils.getApplication();
|
||||
setCachedShell(shell);
|
||||
if (initClasses != null) {
|
||||
for (Class<? extends Shell.Initializer> cls : initClasses) {
|
||||
@ -136,11 +136,11 @@ public final class Impl {
|
||||
} catch (Exception e) {
|
||||
if (e instanceof RuntimeException)
|
||||
throw (RuntimeException) e;
|
||||
InternalUtils.stackTrace(e);
|
||||
Utils.err(e);
|
||||
}
|
||||
return shell;
|
||||
} catch (IOException e) {
|
||||
InternalUtils.stackTrace(e);
|
||||
Utils.ex(e);
|
||||
throw new NoShellException("Unable to create a shell!", e);
|
||||
}
|
||||
}
|
||||
|
@ -31,15 +31,15 @@ interface InputHandler {
|
||||
for (String command : cmd) {
|
||||
in.write(command.getBytes("UTF-8"));
|
||||
in.write('\n');
|
||||
InternalUtils.log(TAG, command);
|
||||
Utils.log(TAG, command);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static InputHandler newInstance(InputStream is) {
|
||||
return in -> {
|
||||
InternalUtils.log(TAG, "<InputStream>");
|
||||
InternalUtils.pump(is, in);
|
||||
Utils.log(TAG, "<InputStream>");
|
||||
Utils.pump(is, in);
|
||||
is.close();
|
||||
// Make sure it ends properly
|
||||
in.write('\n');
|
||||
|
@ -54,7 +54,7 @@ class JobImpl extends Shell.Job {
|
||||
if (e instanceof ShellTerminatedException) {
|
||||
return ResultImpl.SHELL_ERR;
|
||||
} else {
|
||||
InternalUtils.stackTrace(e);
|
||||
Utils.err(e);
|
||||
return ResultImpl.INSTANCE;
|
||||
}
|
||||
}
|
||||
@ -78,7 +78,7 @@ class JobImpl extends Shell.Job {
|
||||
@Override
|
||||
public Shell.Job to(List<String> output) {
|
||||
out = output;
|
||||
redirect = InternalUtils.hasFlag(Shell.FLAG_REDIRECT_STDERR);
|
||||
redirect = Utils.hasFlags(Shell.FLAG_REDIRECT_STDERR);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -95,14 +95,14 @@ class ShellImpl extends Shell {
|
||||
ShellImpl(long timeout, String... cmd) throws IOException {
|
||||
status = UNKNOWN;
|
||||
|
||||
InternalUtils.log(TAG, "exec " + TextUtils.join(" ", cmd));
|
||||
Utils.log(TAG, "exec " + TextUtils.join(" ", cmd));
|
||||
process = Runtime.getRuntime().exec(cmd);
|
||||
STDIN = new NoCloseOutputStream(process.getOutputStream());
|
||||
STDOUT = new NoCloseInputStream(process.getInputStream());
|
||||
STDERR = new NoCloseInputStream(process.getErrorStream());
|
||||
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
InternalUtils.log(TAG, "UUID: " + uuid);
|
||||
Utils.log(TAG, "UUID: " + uuid);
|
||||
outGobbler = new StreamGobbler.OUT(uuid);
|
||||
errGobbler = new StreamGobbler.ERR(uuid);
|
||||
endCmd = String.format("__RET=$?;echo %s;echo %s >&2;echo $__RET;unset __RET\n", uuid, uuid).getBytes("UTF-8");
|
||||
|
@ -58,7 +58,7 @@ abstract class StreamGobbler<T> implements Callable<T> {
|
||||
}
|
||||
if (list != null && line != null) {
|
||||
list.add(line);
|
||||
InternalUtils.log(TAG, line);
|
||||
Utils.log(TAG, line);
|
||||
}
|
||||
return eof;
|
||||
}
|
||||
|
@ -26,53 +26,54 @@ import com.topjohnwu.superuser.Shell;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@RestrictTo(RestrictTo.Scope.LIBRARY)
|
||||
public final class InternalUtils {
|
||||
public final class Utils {
|
||||
|
||||
private static Method currentApplication;
|
||||
private static WeakReference<Context> weakContext;
|
||||
private static Context application;
|
||||
private static final String TAG = "LIBSU";
|
||||
|
||||
static {
|
||||
try {
|
||||
currentApplication = Class.forName("android.app.ActivityThread")
|
||||
.getMethod("currentApplication");
|
||||
} catch (Exception e) {
|
||||
/* Impossible */
|
||||
}
|
||||
public static void log(Object log) {
|
||||
log(TAG, log);
|
||||
}
|
||||
|
||||
public static void log(String tag, Object log) {
|
||||
if (hasFlag(Shell.FLAG_VERBOSE_LOGGING))
|
||||
if (vLog())
|
||||
Log.d(tag, log.toString());
|
||||
}
|
||||
|
||||
public static void stackTrace(Throwable t) {
|
||||
if (hasFlag(Shell.FLAG_VERBOSE_LOGGING))
|
||||
Log.d("LIBSU", "Internal Error", t);
|
||||
public static void ex(Throwable t) {
|
||||
if (vLog())
|
||||
Log.d(TAG, "", t);
|
||||
}
|
||||
|
||||
public static boolean hasFlag(int flags) {
|
||||
return hasFlag(Impl.flags, flags);
|
||||
// Unexpected errors, log regardless of
|
||||
public static void err(Throwable t) {
|
||||
Log.d(TAG, "", t);
|
||||
}
|
||||
|
||||
public static boolean hasFlag(int base, int flags) {
|
||||
return (base & flags) == flags;
|
||||
public static boolean vLog() {
|
||||
return hasFlags(Shell.FLAG_VERBOSE_LOGGING);
|
||||
}
|
||||
|
||||
public static Context getContext() {
|
||||
if (weakContext == null || weakContext.get() == null) {
|
||||
UiThreadHandler.runAndWait(() -> {
|
||||
try {
|
||||
weakContext = new WeakReference<>((Context) currentApplication.invoke(null));
|
||||
} catch (Exception e) {
|
||||
weakContext = new WeakReference<>(null);
|
||||
}
|
||||
});
|
||||
public static boolean hasFlags(int flags) {
|
||||
return (Impl.flags & flags) == flags;
|
||||
}
|
||||
|
||||
public static synchronized Context getApplication() {
|
||||
try {
|
||||
if (application == null) {
|
||||
Method currentApplication = Class.forName("android.app.ActivityThread")
|
||||
.getMethod("currentApplication");
|
||||
application = (Context) currentApplication.invoke(null);
|
||||
}
|
||||
return application;
|
||||
} catch (Exception e) {
|
||||
// Shall never happen
|
||||
Utils.err(e);
|
||||
return null;
|
||||
}
|
||||
return weakContext.get();
|
||||
}
|
||||
|
||||
public static long pump(InputStream in, OutputStream out) throws IOException {
|
@ -105,7 +105,7 @@ class ShellIO extends SuRandomAccessFile implements DataInputImpl, DataOutputImp
|
||||
"dd of='%s' ibs=%d count=1 obs=%d seek=1 %s 2>/dev/null; echo",
|
||||
file.getAbsolutePath(), len, fileOff, WRITE_CONV);
|
||||
}
|
||||
InternalUtils.log(TAG, cmd);
|
||||
Utils.log(TAG, cmd);
|
||||
in.write(cmd.getBytes("UTF-8"));
|
||||
in.write('\n');
|
||||
in.flush();
|
||||
@ -124,7 +124,7 @@ class ShellIO extends SuRandomAccessFile implements DataInputImpl, DataOutputImp
|
||||
Shell.getShell().execTask((in, out, err) -> {
|
||||
String cmd = String.format(Locale.ROOT,
|
||||
"dd bs=%d count=1 >> '%s' 2>/dev/null; echo", len, file.getAbsolutePath());
|
||||
InternalUtils.log(TAG, cmd);
|
||||
Utils.log(TAG, cmd);
|
||||
in.write(cmd.getBytes("UTF-8"));
|
||||
in.write('\n');
|
||||
in.flush();
|
||||
@ -195,7 +195,7 @@ class ShellIO extends SuRandomAccessFile implements DataInputImpl, DataOutputImp
|
||||
String cmd = String.format(Locale.ROOT,
|
||||
"dd if='%s' ibs=%d skip=%d count=%d obs=%d 2>/dev/null; echo >&2",
|
||||
file.getAbsolutePath(), bs, fileOff / bs, len / bs, len);
|
||||
InternalUtils.log(TAG, cmd);
|
||||
Utils.log(TAG, cmd);
|
||||
in.write(cmd.getBytes("UTF-8"));
|
||||
in.write('\n');
|
||||
in.flush();
|
||||
@ -231,7 +231,7 @@ class ShellIO extends SuRandomAccessFile implements DataInputImpl, DataOutputImp
|
||||
String cmd = String.format(Locale.ROOT,
|
||||
"dd of='%s' bs=%d seek=1 count=0 2>/dev/null; echo",
|
||||
file.getAbsolutePath(), newLength);
|
||||
InternalUtils.log(TAG, cmd);
|
||||
Utils.log(TAG, cmd);
|
||||
in.write(cmd.getBytes("UTF-8"));
|
||||
in.write('\n');
|
||||
in.flush();
|
||||
|
@ -30,7 +30,7 @@ import android.os.RemoteException;
|
||||
|
||||
import com.topjohnwu.superuser.Shell;
|
||||
import com.topjohnwu.superuser.internal.IRootIPC;
|
||||
import com.topjohnwu.superuser.internal.InternalUtils;
|
||||
import com.topjohnwu.superuser.internal.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
@ -55,7 +55,7 @@ class IPCClient implements IBinder.DeathRecipient {
|
||||
|
||||
IPCClient(Intent intent) throws InterruptedException, RemoteException, IOException {
|
||||
name = intent.getComponent();
|
||||
startRootServer(InternalUtils.getContext(), intent);
|
||||
startRootServer(Utils.getApplication(), intent);
|
||||
}
|
||||
|
||||
private void startRootServer(Context context, Intent intent)
|
||||
@ -66,7 +66,7 @@ class IPCClient implements IBinder.DeathRecipient {
|
||||
|
||||
try (InputStream in = context.getResources().openRawResource(R.raw.main);
|
||||
OutputStream out = new FileOutputStream(mainJar)) {
|
||||
InternalUtils.pump(in, out);
|
||||
Utils.pump(in, out);
|
||||
}
|
||||
|
||||
// Register BinderReceiver to receive binder from root process
|
||||
|
@ -23,7 +23,7 @@ import android.os.Looper;
|
||||
|
||||
import com.topjohnwu.superuser.Shell;
|
||||
import com.topjohnwu.superuser.internal.IRootIPC;
|
||||
import com.topjohnwu.superuser.internal.InternalUtils;
|
||||
import com.topjohnwu.superuser.internal.Utils;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
@ -64,7 +64,7 @@ class IPCServer extends IRootIPC.Stub implements IBinder.DeathRecipient {
|
||||
client.linkToDeath(this, 0);
|
||||
return service.onBind(intent);
|
||||
} catch (Exception e) {
|
||||
InternalUtils.stackTrace(e);
|
||||
Utils.err(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -26,9 +26,9 @@ import android.os.IBinder;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.topjohnwu.superuser.Shell;
|
||||
import com.topjohnwu.superuser.internal.InternalUtils;
|
||||
import com.topjohnwu.superuser.internal.SerialExecutorService;
|
||||
import com.topjohnwu.superuser.internal.UiThreadHandler;
|
||||
import com.topjohnwu.superuser.internal.Utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
@ -36,9 +36,6 @@ import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import static com.topjohnwu.superuser.Shell.FLAG_VERBOSE_LOGGING;
|
||||
import static com.topjohnwu.superuser.internal.InternalUtils.hasFlag;
|
||||
|
||||
/**
|
||||
* A remote root service using native Android Binder IPC.
|
||||
* <p>
|
||||
@ -78,7 +75,7 @@ public abstract class RootService extends ContextWrapper {
|
||||
return;
|
||||
|
||||
Intent intentCopy = new Intent(intent);
|
||||
intentCopy.putExtra(INTENT_VERBOSE_KEY, hasFlag(FLAG_VERBOSE_LOGGING));
|
||||
intentCopy.putExtra(INTENT_VERBOSE_KEY, Utils.vLog());
|
||||
|
||||
for (IPCClient client : active) {
|
||||
if (client.isSameService(intentCopy)) {
|
||||
@ -92,7 +89,7 @@ public abstract class RootService extends ContextWrapper {
|
||||
client.newConnection(conn, executor);
|
||||
active.add(client);
|
||||
} catch (Exception e) {
|
||||
InternalUtils.stackTrace(e);
|
||||
Utils.err(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user