Skip to content

Commit da07826

Browse files
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.
1 parent 1259a21 commit da07826

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

termux-shared/src/main/java/com/termux/shared/packages/PackageUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.termux.shared.packages;
22

3+
import android.app.ActivityManager;
34
import android.app.admin.DevicePolicyManager;
45
import android.content.ComponentName;
56
import android.content.Context;
@@ -248,4 +249,30 @@ public static String getProfileOwnerPackageNameForUser(@NonNull Context context)
248249
return null;
249250
}
250251

252+
/**
253+
* Get the process id of the main app process of a package. This will work for sharedUserId. Note
254+
* that some apps have multiple processes for the app like with `android:process=":background"`
255+
* attribute in AndroidManifest.xml.
256+
*
257+
* @param context The {@link Context} for operations.
258+
* @param packageName The package name of the process.
259+
* @return Returns the process if found and running, otherwise {@code null}.
260+
*/
261+
@Nullable
262+
public static String getPackagePID(final Context context, String packageName) {
263+
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
264+
if (activityManager != null) {
265+
List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
266+
if (processInfos != null) {
267+
ActivityManager.RunningAppProcessInfo processInfo;
268+
for (int i = 0; i < processInfos.size(); i++) {
269+
processInfo = processInfos.get(i);
270+
if (processInfo.processName.equals(packageName))
271+
return String.valueOf(processInfo.pid);
272+
}
273+
}
274+
}
275+
return null;
276+
}
277+
251278
}

termux-shared/src/main/java/com/termux/shared/shell/TermuxShellUtils.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
public class TermuxShellUtils {
2222

23+
public static String TERMUX_VERSION_NAME;
24+
public static String TERMUX_IS_DEBUG_BUILD;
25+
public static String TERMUX_APK_RELEASE;
26+
public static String TERMUX_APP_PID;
27+
2328
public static String getDefaultWorkingDirectoryPath() {
2429
return TermuxConstants.TERMUX_HOME_DIR_PATH;
2530
}
@@ -36,13 +41,16 @@ public static String[] buildEnvironment(Context currentPackageContext, boolean i
3641

3742
List<String> environment = new ArrayList<>();
3843

39-
// This function may be called by a different package like a plugin, so we get version for Termux package via its context
40-
Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext);
41-
if (termuxPackageContext != null) {
42-
String termuxVersionName = PackageUtils.getVersionNameForPackage(termuxPackageContext);
43-
if (termuxVersionName != null)
44-
environment.add("TERMUX_VERSION=" + termuxVersionName);
45-
}
44+
loadTermuxEnvVariables(currentPackageContext);
45+
46+
if (TERMUX_VERSION_NAME != null)
47+
environment.add("TERMUX_VERSION=" + TERMUX_VERSION_NAME);
48+
if (TERMUX_IS_DEBUG_BUILD != null)
49+
environment.add("TERMUX_IS_DEBUG_BUILD=" + TERMUX_IS_DEBUG_BUILD);
50+
if (TERMUX_APK_RELEASE != null)
51+
environment.add("TERMUX_APK_RELEASE=" + TERMUX_APK_RELEASE);
52+
if (TERMUX_APP_PID != null)
53+
environment.add("TERMUX_APP_PID=" + TERMUX_APP_PID);
4654

4755
environment.add("TERM=xterm-256color");
4856
environment.add("COLORTERM=truecolor");
@@ -147,4 +155,21 @@ public static void clearTermuxTMPDIR(boolean onlyIfExists) {
147155
}
148156
}
149157

158+
public static void loadTermuxEnvVariables(Context currentPackageContext) {
159+
TERMUX_VERSION_NAME = TERMUX_IS_DEBUG_BUILD = TERMUX_APK_RELEASE = TERMUX_APP_PID = null;
160+
161+
// This function may be called by a different package like a plugin, so we get version for Termux package via its context
162+
Context termuxPackageContext = TermuxUtils.getTermuxPackageContext(currentPackageContext);
163+
if (termuxPackageContext != null) {
164+
TERMUX_VERSION_NAME = PackageUtils.getVersionNameForPackage(termuxPackageContext);
165+
TERMUX_IS_DEBUG_BUILD = PackageUtils.isAppForPackageADebugBuild(termuxPackageContext) ? "1" : "0";
166+
167+
String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(termuxPackageContext);
168+
if (signingCertificateSHA256Digest != null)
169+
TERMUX_APK_RELEASE = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest).replaceAll("[^a-zA-Z]", "_").toUpperCase();
170+
171+
TERMUX_APP_PID = TermuxUtils.getTermuxAppPID(currentPackageContext);
172+
}
173+
}
174+
150175
}

termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,16 @@ public static String getAPKRelease(String signingCertificateSHA256Digest) {
533533
}
534534
}
535535

536+
537+
/**
538+
* Get a process id of the main app process of the {@link TermuxConstants#TERMUX_PACKAGE_NAME}
539+
* package.
540+
*
541+
* @param context The context for operations.
542+
* @return Returns the process if found and running, otherwise {@code null}.
543+
*/
544+
public static String getTermuxAppPID(final Context context) {
545+
return PackageUtils.getPackagePID(context, TermuxConstants.TERMUX_PACKAGE_NAME);
546+
}
547+
536548
}

0 commit comments

Comments
 (0)