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.
This commit is contained in:
agnostic-apollo
2021-09-06 03:54:16 +05:00
parent 1259a212aa
commit da07826a0c
3 changed files with 71 additions and 7 deletions

View File

@@ -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<ActivityManager.RunningAppProcessInfo> 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;
}
}

View File

@@ -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<String> 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);
}
}
}

View File

@@ -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);
}
}