|
| 1 | +package org.randomcoder.udroid.camera; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.ComponentName; |
| 5 | +import android.content.Intent; |
| 6 | +import android.app.BroadcastOptions; |
| 7 | +import android.content.pm.ApplicationInfo; |
| 8 | +import android.os.Binder; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.os.IBinder; |
| 11 | +import android.os.Looper; |
| 12 | +import android.os.Parcel; |
| 13 | +import android.os.ParcelFileDescriptor; |
| 14 | +import android.os.Process; |
| 15 | +import android.system.Os; |
| 16 | +import android.system.OsConstants; |
| 17 | +import java.io.File; |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.atomic.AtomicReference; |
| 24 | + |
| 25 | +/** Receives an authorized broker FD, then replaces this app_process helper with a Termux command. */ |
| 26 | +public final class CameraTermuxLauncher { |
| 27 | + public static void main(String[] args) throws Exception { |
| 28 | + if (args.length == 0) throw new IllegalArgumentException("Usage: camera2termux-run COMMAND [ARG...]"); |
| 29 | + Looper.prepareMainLooper(); |
| 30 | + // app_process has no app Context; use Termux's UID and package identity for the broadcast. |
| 31 | + Class<?> threadClass = Class.forName("android.app.ActivityThread"); |
| 32 | + Object thread = threadClass.getMethod("systemMain").invoke(null); |
| 33 | + Context system = (Context) threadClass.getMethod("getSystemContext").invoke(thread); |
| 34 | + String caller = Process.myUid() == Process.SHELL_UID ? "com.android.shell" : "com.termux"; |
| 35 | + ApplicationInfo info = system.getPackageManager().getApplicationInfo(caller, 0); |
| 36 | + Object loaded = threadClass.getMethod("getPackageInfoNoCheck", ApplicationInfo.class).invoke(thread, info); |
| 37 | + Method create = Class.forName("android.app.ContextImpl").getDeclaredMethod( |
| 38 | + "createAppContext", threadClass, Class.forName("android.app.LoadedApk")); |
| 39 | + create.setAccessible(true); |
| 40 | + Context context = (Context) create.invoke(null, thread, loaded); |
| 41 | + CountDownLatch received = new CountDownLatch(1); |
| 42 | + AtomicReference<ParcelFileDescriptor> brokerFd = new AtomicReference<>(); |
| 43 | + AtomicReference<Exception> failure = new AtomicReference<>(); |
| 44 | + IBinder callback = new Binder() { |
| 45 | + @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) { |
| 46 | + if (code != CameraFdReceiver.TRANSACTION_FD) return false; |
| 47 | + ParcelFileDescriptor fd = null; |
| 48 | + try { |
| 49 | + fd = ParcelFileDescriptor.CREATOR.createFromParcel(data); |
| 50 | + if (!OsConstants.S_ISSOCK(Os.fstat(fd.getFileDescriptor()).st_mode)) |
| 51 | + throw new IllegalStateException("Received descriptor is not a socket"); |
| 52 | + brokerFd.set(fd); |
| 53 | + fd = null; |
| 54 | + } catch (Exception error) { |
| 55 | + failure.set(error); |
| 56 | + } finally { |
| 57 | + if (fd != null) try { fd.close(); } catch (Exception ignored) { } |
| 58 | + } |
| 59 | + received.countDown(); |
| 60 | + return true; |
| 61 | + } |
| 62 | + }; |
| 63 | + Bundle extras = new Bundle(); |
| 64 | + extras.putBinder("callback", callback); |
| 65 | + Intent request = new Intent(CameraFdReceiver.ACTION) |
| 66 | + .setComponent(new ComponentName("org.randomcoder.camera2termux", |
| 67 | + "org.randomcoder.udroid.camera.CameraFdReceiver")) |
| 68 | + .putExtras(extras); |
| 69 | + context.sendBroadcast(request, null, BroadcastOptions.makeBasic() |
| 70 | + .setShareIdentityEnabled(true).toBundle()); |
| 71 | + if (!received.await(10, TimeUnit.SECONDS)) throw new IllegalStateException("No camera FD received"); |
| 72 | + if (failure.get() != null) throw failure.get(); |
| 73 | + try (ParcelFileDescriptor fd = brokerFd.get()) { |
| 74 | + if (fd == null) throw new IllegalStateException("Camera FD callback returned nothing"); |
| 75 | + String prefix = System.getenv("PREFIX"); |
| 76 | + if (prefix == null || prefix.isEmpty()) prefix = "/data/data/com.termux/files/usr"; |
| 77 | + String library = prefix + "/lib/libfauxdev-termux.so"; |
| 78 | + if (!new File(library).isFile()) |
| 79 | + throw new IllegalStateException("Missing Termux camera library: " + library); |
| 80 | + String command = resolveCommand(args[0], System.getenv("PATH")); |
| 81 | + int flags = Os.fcntlInt(fd.getFileDescriptor(), OsConstants.F_GETFD, 0); |
| 82 | + Os.fcntlInt(fd.getFileDescriptor(), OsConstants.F_SETFD, flags & ~OsConstants.FD_CLOEXEC); |
| 83 | + ArrayList<String> environment = new ArrayList<>(); |
| 84 | + for (Map.Entry<String, String> entry : System.getenv().entrySet()) { |
| 85 | + if (!entry.getKey().equals("LD_PRELOAD") && !entry.getKey().equals("FAUXDEV_VIDEO_FD") && |
| 86 | + !entry.getKey().equals("FAUXDEV_VIDEO_SOCKET")) |
| 87 | + environment.add(entry.getKey() + "=" + entry.getValue()); |
| 88 | + } |
| 89 | + String preload = System.getenv("LD_PRELOAD"); |
| 90 | + environment.add("LD_PRELOAD=" + library + (preload == null ? "" : ":" + preload)); |
| 91 | + environment.add("FAUXDEV_VIDEO_FD=" + fd.getFd()); |
| 92 | + Os.execve(command, args, environment.toArray(new String[0])); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static String resolveCommand(String name, String path) { |
| 97 | + if (name.contains("/")) return new File(name).getAbsolutePath(); |
| 98 | + if (path != null) for (String directory : path.split(":")) { |
| 99 | + File candidate = new File(directory, name); |
| 100 | + if (candidate.isFile() && candidate.canExecute()) return candidate.getAbsolutePath(); |
| 101 | + } |
| 102 | + throw new IllegalArgumentException("Command not found: " + name); |
| 103 | + } |
| 104 | +} |
0 commit comments