From da07826a0c7bd5e9a41537dc181a0fb892607e16 Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Mon, 6 Sep 2021 03:54:16 +0500 Subject: [PATCH] Added: Add TERMUX_IS_DEBUG_BUILD, TERMUX_APK_RELEASE and TERMUX_APP_PID to termux shell environment The `TERMUX_IS_DEBUG_BUILD` env variable will be set to `1` if termux APK is a debuggable APK and `0` otherwise. Note that the `dev_keystore.jks` shipped with termux app and plugin source code can also be used to create a release APK even though its mainly used for Github Debug Builds, in which case value will be `0`. The `TERMUX_APK_RELEASE` will be set to `GITHUB_DEBUG_BUILD`, `F_DROID` or `GOOGLE_PLAY_STORE` depending on release type. It will be set to `UNKNOWN` if signed with a custom key. The `TERMUX_APP_PID` will be set to the process of the main app process of the termux app package (`com.termux`), assuming its running when shell is started, like for `termux-float`. This variable is included since `pidof com.termux` does not return anything for release builds. It does work for debug builds and over adb/root. However, you still won't be able to get additional process info with `ps`, like that of threads, even with the pid and will need to use adb/root. However, `kill $TERMUX_APP_PID` will work from `termux-app` and `termux-float`. These variables can be used by termux devs and users for custom logic in future depending on release type. --- .../termux/shared/packages/PackageUtils.java | 27 +++++++++++++ .../termux/shared/shell/TermuxShellUtils.java | 39 +++++++++++++++---- .../com/termux/shared/termux/TermuxUtils.java | 12 ++++++ 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java b/termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java index 2698c92f..4dc282fb 100644 --- a/termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java @@ -1,5 +1,6 @@ package com.termux.shared.packages; +import android.app.ActivityManager; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; @@ -248,4 +249,30 @@ public class PackageUtils { return null; } + /** + * Get the process id of the main app process of a package. This will work for sharedUserId. Note + * that some apps have multiple processes for the app like with `android:process=":background"` + * attribute in AndroidManifest.xml. + * + * @param context The {@link Context} for operations. + * @param packageName The package name of the process. + * @return Returns the process if found and running, otherwise {@code null}. + */ + @Nullable + public static String getPackagePID(final Context context, String packageName) { + ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + if (activityManager != null) { + List processInfos = activityManager.getRunningAppProcesses(); + if (processInfos != null) { + ActivityManager.RunningAppProcessInfo processInfo; + for (int i = 0; i < processInfos.size(); i++) { + processInfo = processInfos.get(i); + if (processInfo.processName.equals(packageName)) + return String.valueOf(processInfo.pid); + } + } + } + return null; + } + } diff --git a/termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java b/termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java index fade41db..cd1175df 100644 --- a/termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java @@ -20,6 +20,11 @@ import java.util.List; public class TermuxShellUtils { + public static String TERMUX_VERSION_NAME; + public static String TERMUX_IS_DEBUG_BUILD; + public static String TERMUX_APK_RELEASE; + public static String TERMUX_APP_PID; + public static String getDefaultWorkingDirectoryPath() { return TermuxConstants.TERMUX_HOME_DIR_PATH; } @@ -36,13 +41,16 @@ public class TermuxShellUtils { List environment = new ArrayList<>(); - // This function may be called by a different package like a plugin, so we get version for Termux package via its context - Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext); - if (termuxPackageContext != null) { - String termuxVersionName = PackageUtils.getVersionNameForPackage(termuxPackageContext); - if (termuxVersionName != null) - environment.add("TERMUX_VERSION=" + termuxVersionName); - } + loadTermuxEnvVariables(currentPackageContext); + + if (TERMUX_VERSION_NAME != null) + environment.add("TERMUX_VERSION=" + TERMUX_VERSION_NAME); + if (TERMUX_IS_DEBUG_BUILD != null) + environment.add("TERMUX_IS_DEBUG_BUILD=" + TERMUX_IS_DEBUG_BUILD); + if (TERMUX_APK_RELEASE != null) + environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE); + if (TERMUX_APP_PID != null) + environment.add("TERMUX_APP_PID=" + TERMUX_APP_PID); environment.add("TERM=xterm-256color"); environment.add("COLORTERM=truecolor"); @@ -147,4 +155,21 @@ public class TermuxShellUtils { } } + public static void loadTermuxEnvVariables(Context currentPackageContext) { + TERMUX_VERSION_NAME = TERMUX_IS_DEBUG_BUILD = TERMUX_APK_RELEASE = TERMUX_APP_PID = null; + + // This function may be called by a different package like a plugin, so we get version for Termux package via its context + Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext); + if (termuxPackageContext != null) { + TERMUX_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxPackageContext); + TERMUX_IS_DEBUG_BUILD = PackageUtils.isAppForPackageADebugBuild(termuxPackageContext) ? "1" : "0"; + + String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(termuxPackageContext); + if (signingCertificateSHA256Digest != null) + TERMUX_APK_RELEASE = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest).replaceAll("[^a-zA-Z]", "_").toUpperCase(); + + TERMUX_APP_PID = TermuxUtils.getTermuxAppPID(currentPackageContext); + } + } + } diff --git a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java index df45a5e9..f2b6391d 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java @@ -533,4 +533,16 @@ public class TermuxUtils { } } + + /** + * Get a process id of the main app process of the {@link TermuxConstants#TERMUX_PACKAGE_NAME} + * package. + * + * @param context The context for operations. + * @return Returns the process if found and running, otherwise {@code null}. + */ + public static String getTermuxAppPID(final Context context) { + return PackageUtils.getPackagePID(context, TermuxConstants.TERMUX_PACKAGE_NAME); + } + }