Commit Graph

1386 Commits

Author SHA1 Message Date
agnostic-apollo
4b3b1a5b6a Changed: Bump bootstrap to v2022.04.22-r1 2022-04-23 01:49:36 +05:00
agnostic-apollo
7f7d889dd0 Fixed: Fix proguard removing JNI used methods for release builds
```
Exception in createServerSocketNative():
java.lang.NoSuchMethodError: no non-static method "Lcom/termux/shared/jni/models/JniResult;.<init>(IILjava/lang/String;I)V"
	at com.termux.shared.net.socket.local.LocalSocketManager.createServerSocketNative(Native Method)
	at com.termux.shared.net.socket.local.LocalSocketManager.createServerSocket(LocalSocketManager.java:125)
	at com.termux.shared.net.socket.local.LocalServerSocket.start(LocalServerSocket.java:100)
	at com.termux.shared.net.socket.local.LocalSocketManager.start(LocalSocketManager.java:84)
	at com.termux.shared.shell.am.AmSocketServer.start(AmSocketServer.java:68)
	at com.termux.shared.termux.shell.am.TermuxAmSocketServer.start(TermuxAmSocketServer.java:101)
	at com.termux.shared.termux.shell.am.TermuxAmSocketServer.setupTermuxAmSocketServer(TermuxAmSocketServer.java:77)
	at com.termux.app.TermuxApplication.onCreate(TermuxApplication.java:53)
	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6719)
	at android.app.ActivityThread.access$1300(ActivityThread.java:237)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loop(Looper.java:223)
	at android.app.ActivityThread.main(ActivityThread.java:7664)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
```
2022-04-23 00:39:00 +05:00
agnostic-apollo
53f26c9659 Changed: Refactor am socket server
The `AmSocketServer` now handles the entire logic for processing of am commands sent by clients and its results. This can be used by other apps as well to run their own am servers. The server started by `termux-app` will be managed by `TermuxAmSocketServer`. Read their javadocs for details.

The extended implementation `TermuxAmSocketServerClient` of `AmSocketServer.AmSocketServerClient`/`ILocalSocketManager` will also send a plugin error notification for all errors to the user instead of just logging to logcat since users are not very good at checking those, this should save dev time debugging problems. We may need to ignore notifications for some errors like broken pipe, based on their `Error` objects if they are normally expected, this requires further investigation.

The `TERMUX_APP_AM_SOCKET_SERVER_ENABLED` env variable will also be exported for all shell sessions and tasks for whether the server was successfully started on app startup. The user can disable the server by adding "run-termux-am-socket-server=false" to the "~/.termux/termux.properties" as implemented in 5f8a9222. The env variable will be checked by `$PREFIX/bin/termux-am` before attempting to connect.

The new path for the server socket is `/data/data/com.termux/files/apps/termux-app/termux-am/am.sock` as per `TERMUX_APP.APPS_DIR_PATH` added in bcd8f4c4.
2022-04-23 00:39:00 +05:00
agnostic-apollo
2aa7f43d1c Added|Changed|Fixed: Refactor local socket server implementation and make client handling abstract
- Added `LocalSocketManager` to manage the server, `LocalServerSocket` to represent server socket, `LocalClientSocket` to represent client socket, `LocalSocketRunConfig` to store server run config and `ILocalSocketManager` as interface for the `LocalSocketManager` to handle callbacks from the server to handle clients.
- Added support to get full `PeerCred` for client socket, including `pid`, `pname`, `uid`, `uname`, `gid`, `gname` and `cmdline` instead of just `uid`. This should provide more info for error logs about which client failed or tried to connect in case of disallowed clients. Some data is filled in native code and some in java. Native support for added to get process name and `cmdline` of a process with a specific pid.
- Added `JniResult` to get results for JNI calls. Previously only an int was returned and incomplete errors logged. With `JniResult`, both `retval` and `errno` will be returned and full error messages in `errmsg`, including all `strerror()` output for `errno`s. This would provide more helpful info on errors.
- Added `Error` support via `LocalSocketErrno` which contains full error messages and stacktraces for all native and java calls, allowing much better error reporting to users and devs. The errors will be logged by `LocalSocketManagerClientBase` if log level is debug or higher since `PeerCred` `cmdline` may contain private info of users.
- Added support in java to check if socket path was an absolute path and not greater than `108` bytes, after canonicalizing it since otherwise it would result in creation of useless parent directories on failure.
- Added `readDataOnInputStream()` and `sendDataToOutputStream()` functions to `LocalClientSocket` so that server manager client can easily read and send data.

- Renamed the variables and functions as per convention, specially one letter variables. https://source.android.com/setup/contribute/code-style#follow-field-naming-conventions
- Rename `local-filesystem-socket` to `local-filesystem` since abstract namespace sockets can also be created.
- Previously, it was assumed that all local server would expect a shell command string that should be converted to command args with `ArgumentTokenizer` and then should be passed to `LocalSocketHandler.handle()` and then result sent back to client with exit code, stdout and stderr, but there could be any kind of servers in which behaviour is different. Such client handling should not be hard coded and the server manager client should handle the client themselves however they like, including closing the client socket. This will now be done with `ILocalSocketManager. onClientAccepted(LocalSocketManager, LocalClientSocket)`.

- Ensure app does not crash if `local-socket` library is not found or for any other exceptions in the server since anything running in the `Application` class is critical that it does not fail since user would not be able to recover from it, specially non rooted users without SAF support to disable the server with a prop.
- Make sure all reasonable JNI exceptions are caught instead of crashing the app.
- Fixed issue where client logic (`LocalSocketHandler.handle()` was being run in the same thread as the new client acceptable thread, basically blocking new clients until previous client's am command was fully processed. Now all client interface callbacks are started in new threads by `LocalSocketManager`.
- Fix bug where timeout would not be greater than `1000ms` due to only using `tv_usec` which caps at `999,999`.
2022-04-23 00:36:12 +05:00
agnostic-apollo
5f8a922201 Added: Allow users to disable termux-am server
The user can add `run-termux-am-socket-server=false` entry to `termux.properties` file to disable the `termux-am` server to run at app startup which is connected to by `$PREFIX/bin/termux-am` from the `termux-am-socket` package. The default value is `true`. Changes require `termux-app` to be force stopped and restarted to provide consistent state for all termux sessions and tasks.

The prop will be used in a later commit.
2022-04-23 00:36:12 +05:00
agnostic-apollo
9a71074c7d Added: Add function to ProcessUtils to get app process name for a pid from ActivityManager 2022-04-23 00:36:12 +05:00
agnostic-apollo
69cc65c3ac Added: Add functions to UserUtils to get user name for user id from PackageManager and Libcore 2022-04-23 00:36:12 +05:00
agnostic-apollo
bcd8f4c419 Added: Add TERMUX_APPS_DIR_PATH and TERMUX_APP.APPS_DIR_PATH and create them at application startup.
The termux files directory will also be checked and created if required at startup and code related to it will only be run if it is accessible. This can later also be used for init execution commands.

The `TERMUX_APP.APPS_DIR_PATH` will act as app specific directory for `termux-app` app related files. Other plugin apps will have their own directories under `TERMUX_APPS_DIR_PATH` if required.
2022-04-23 00:36:12 +05:00
agnostic-apollo
6bda7c4fc4 Added|Changed: Add Logger.logErrorPrivate*() functions which do not log errors that may contain potentially private info unless log level is debug or higher
Execution commands and other errors that may contain potentially private info should not be logged unless user has explicitly allowed it since apps with `READ_LOGS` permission would be able to read the data. A notification for failed executions commands would still be shown if enabled and required.
2022-04-23 00:36:12 +05:00
agnostic-apollo
89a08ff01a Fixed: Allow Object class object to be passed to ReflectionUtils.invokeField() 2022-04-23 00:36:12 +05:00
agnostic-apollo
c81d9c3346 Added: Add FileType.SOCKET support and add FileUtils.deleteSocketFile() function 2022-04-23 00:36:12 +05:00
agnostic-apollo
58c3d427e8 Fixed: Log and add to Error the current file type in FileUtils.deleteFile() in addition to allowed file types 2022-04-23 00:36:12 +05:00
agnostic-apollo
1b9ca91da5 Added: Add functions to DataUtils to get generic, space and tab indented strings 2022-04-23 00:36:12 +05:00
agnostic-apollo
9c7ec0cebd Added: Add functions that can be used by non main threads to set CrashHandler as the UncaughtExceptionHandler 2022-04-23 00:36:12 +05:00
agnostic-apollo
6b60adc079 Fixed: Do not stop the app if UncaughtExceptionHandler implemented by CrashHandler receives an exception on a non main thread
Rename function that should be used by main thread of apps to `setDefaultCrashHandler()`.

Functions for other threads will be added in a later commit.
2022-04-23 00:36:12 +05:00
agnostic-apollo
5116d886c3 Changed: Add label parameter to ExecutionCommand getArgumentsLogString() and getArgumentsMarkdownString() functions for external usage 2022-04-23 00:36:12 +05:00
agnostic-apollo
02ab8324e9 Changed|Fixed: Do not add empty stacktraces entry to Error log and markdown String 2022-04-23 00:36:12 +05:00
agnostic-apollo
c095a6184b Changed: Rename TermuxCrashUtils sendPluginCrashReportNotification() to sendCrashReportNotification()` 2022-04-23 00:36:12 +05:00
agnostic-apollo
cc981d8a03 Changed: Move com.termux.app.utils.PluginUtils to com.termux.shared.termux.plugins.TermuxPluginUtils
This will allow plugins and `termux-shared` library to trigger plugin error notifications too and process plugin command results.
2022-04-23 00:36:12 +05:00
tareksander
007f9cd7f1 Changed: Set socket dir to /data/data/com.termux/files/api/
Using the TermuxConstants.TERMUX_FILES_DIR variable to get full path
TermuxConstants.TERMUX_FILES_DIR_PATH/api/am-socket.
2022-04-23 00:36:12 +05:00
tareksander
b025872029 Changed: Updated the termux-am-library dependency, because the repo is now part of the Termux github organization. 2022-04-23 00:36:12 +05:00
tareksander
2851175d8b Changed: Moved the am socket to PREFIX/var/run/am-socket 2022-04-23 00:36:12 +05:00
tareksander
3dee2eb486 Changed: Allow connections from root o sockets. 2022-04-23 00:36:12 +05:00
tareksander
33b88b5d4b Changed: Set termux-am-library to a tag instead of following the main branch. 2022-04-23 00:36:12 +05:00
tareksander
f366db0cb3 Added: LocalFilesystemSocket as an Interface to UNIX sockets in the filesystem. The UID of connecting programs is automatically checked against the processes UID and connections where the UID doesn't match are automatically rejected and logged.
Changed: LocalSocketListener now uses sockets in the filesystem.
2022-04-23 00:36:12 +05:00
tareksander
4aca16326c Added: termux-am-library to integrate am with Termux. 2022-04-23 00:36:12 +05:00
tareksander
e597ece75f Added: ArgumentTokenizer to com.termux.shared.shell 2022-04-23 00:36:12 +05:00
Henrik Grimler
9e06bfce1f Changed: Bump bootstrap to v2022.04.21-r1 2022-04-21 21:55:09 +02:00
agnostic-apollo
5794ab9a56 Added|Changed: Add support to switch to existing session instead of creating duplicate session for RUN_COMMAND intent
This is done via addition of the `com.termux.RUN_COMMAND_SESSION_CREATE_MODE` extra, which currently supports two values.

- `always` to always create a new session every time.
- `no-session-with-name` to create a new session only if no existing session exits with the same terminal session name.

The terminal session name will equal executable basename by default and dashes `-` in the basename will no longer be replaced with spaces when session name as done previously. The `com.termux.RUN_COMMAND_SESSION_NAME` extra can be used to set custom session name.

Usage:

You can use this with `Termux:Tasker` or `Termux:Widget`.

For example for `Termux:Widget`

- Create a wrapper script at `~/.shortcuts/tasks/my-script.sh` with following contents under `tasks` directory so that it runs in background app shell instead of a terminal session. Do not use terminal session runner for wrapper script, since it will open two sessions everytime otherwise, first for wrapper script, then for actual target executable. There would also be conflicts if both wrapper script and target executable have the same basename and it would be incorrectly assumed that session is already running.
- Replace the `bash` executable with actual target executable that you want to run in the terminal session if its not already running.
- Optionally set custom session name. By default it will set to executable basename and not the wrapper script name. To set it to wrapper script name, you can pass `$(basename "$0")`.
- Launch the wrapper script with widget. On first launch, a new terminal session should open but on subsequent launches, same terminal session should open.

Note that you can also pass `com.termux.RUN_COMMAND_SESSION_ACTION` to modify session action behaviour. Check https://github.com/termux/termux-app/wiki/RUN_COMMAND-Intent#run_command-intent-command-extras.

```

am startservice --user 0 -n com.termux/com.termux.app.RunCommandService \
-a com.termux.RUN_COMMAND \
--es com.termux.RUN_COMMAND_PATH '/data/data/com.termux/files/usr/bin/bash' \
--es com.termux.RUN_COMMAND_SESSION_CREATE_MODE 'no-session-with-name' \
--es com.termux.RUN_COMMAND_SESSION_NAME "custom-name"
```
2022-04-08 02:42:54 +05:00
agnostic-apollo
ee32ef0c7e Changed: Maintain terminal session name in ExecutionCommand.sessionName in addition to TerminalSession.mSessionName 2022-04-08 02:42:54 +05:00
Alan Christian
8746db0d22 chore(gitignore): Remove unused ignore rules for crashlytics (#2683)
* Fix typo

* Remove crashlytics
2022-04-02 18:26:43 +05:30
agnostic-apollo
ce12b8ad2d Fixed: Fix Settings.ACTION_* permission requests result callback
Adding `FLAG_ACTIVITY_NEW_TASK` will start permission activity in separate task and `onActivityResult()` will be called early in the calling activity without grant/not-grant result being actually set.
2022-03-30 19:45:55 +05:00
agnostic-apollo
87a79a9b24 Added: Log intents received by and 2022-03-30 19:42:38 +05:00
agnostic-apollo
caa13b7047 Fixed: Fix pull request APKs commit hash 2022-03-18 06:31:43 +05:00
agnostic-apollo
5e820ad249 Added: Allow users to adjust $TMPDIR clear mechanism on termux exit
The `delete-tmpdir-files-older-than-x-days-on-exit` key can be used to adjust how many days old the access time should be of files that should be deleted from `$TMPDIR` on termux exit. The user can set an integer value between `-1` and `100000`. Set `-1` to delete no files, `0` to delete all files and `> 0` for `x` days. The default value is `3` days. So adding an entry like `delete-tmpdir-files-older-than-x-days-on-exit=10` to `termux.properties` file will make termux delete files older than `10` when termux is exited. After updating the value, either restart termux or run `termux-reload-settings` for changes to take effect.

Note that currently `> 0` will revert back to `0` since deletion is currently broken for empty sub directories and deletion needs to be done based on access time instead of modified time. It will need to be fixed in a later commit. Check `FileUtils.deleteFilesOlderThanXDays()`.

Related issue #2350
2022-03-17 22:35:31 +05:00
agnostic-apollo
25d21e9d2e Fixed: Fix wrong input type selected if toolbar is switched back to extra keys after tapping terminal if in text input mode
Closes #2503
2022-03-17 05:30:25 +05:00
agnostic-apollo
dd378738e3 Fixed: Add media-* symlinks to Android/media for all storages and external-0 symlink to Android/media of primary storage
The `~/external-0` and `~/media-0` should point to primary storage and `1+` to others, possibly portable sd cards.

Note that one can make portable sd card as primary storage as well instead of internal sd card with adoptable storage, which then links it to `/storage/emulated`, so the concept of `internal` and `external` sd card does not apply to primary storage for all cases.

https://android.stackexchange.com/questions/214233/how-to-free-internal-storage-by-moving-data-or-using-symlink-bind-mount-with-a/214706#214706

https://android.stackexchange.com/questions/217741/how-to-bind-mount-a-folder-inside-sdcard-with-correct-permissions/217936#217936

https://android.stackexchange.com/questions/205430/what-is-storage-emulated-0/205494#205494

https://source.android.com/devices/storage/adoptable

```
$ ls -l storage | cut -d ' ' -f 9-

audiobooks -> /storage/emulated/0/Audiobooks
dcim -> /storage/emulated/0/DCIM
documents -> /storage/emulated/0/Documents
downloads -> /storage/emulated/0/Download
external-0 -> /storage/emulated/0/Android/data/com.termux/files
external-1 -> /storage/XXXX-XXXX/Android/data/com.termux/files
media-0 -> /storage/emulated/0/Android/media/com.termux
media-1 -> /storage/XXXX-XXXX/Android/media/com.termux
movies -> /storage/emulated/0/Movies
music -> /storage/emulated/0/Music
pictures -> /storage/emulated/0/Pictures
podcasts -> /storage/emulated/0/Podcasts
shared -> /storage/emulated/0

```

Closes #2481
2022-03-17 05:30:25 +05:00
agnostic-apollo
93d57f053b Added: Add ~/storage symlinks for documents, podcasts and audiobooks
The `audiobooks` symlink will only be made on Android `10+`

Closes #2648
2022-03-17 05:30:25 +05:00
agnostic-apollo
26e0fa2b9e Changed: Use thread to setup settings components
Getting plugin contexts may be considered as too much work on main thread in certain situations resulting in android complaining that app is not responding
2022-03-17 05:30:25 +05:00
agnostic-apollo
d25f7afd97 Changed: Share terminal transcript with ShareUtils 2022-03-17 05:30:25 +05:00
agnostic-apollo
e0074f280f Fixed: Fix typos 2022-03-17 05:30:25 +05:00
agnostic-apollo
cc58ddde31 Changed: Check crash log file whenever TermuxActivity is resumed instead of only on app startup
This adds onto 06dbfbdb since receiver would not be registered to receive `ACTION_NOTIFY_APP_CRASH` if `TermuxActivity` was not be in foreground
2022-03-17 02:10:51 +05:00
agnostic-apollo
477b36acd1 Added: Add support for ACTION_NOTIFY_APP_CRASH in receiver registered by TermuxActivity to notify users of plugin app crashes
Once plugins integrate changes for `TermuxCrashUtils.onPostLogCrash()`, they will send the `ACTION_NOTIFY_APP_CRASH` broadcast when an uncaught exception is caught by `CrashHandler`. If `TermuxActivity` is in foreground, then it will receive the broadcast and notify user of the crash by reading it from the crash log file without the user having to restart termux app to be notified.
2022-03-17 02:10:51 +05:00
agnostic-apollo
5f00531381 Changed: Move com.termux.app.utils.CrashUtils to com.termux.shared.termux.crash.TermuxCrashUtils so that plugins trigger plugin notifications too
Calls to `notifyAppCrashFromCrashLogFile()` will now be synchronized as well.
2022-03-17 02:10:51 +05:00
agnostic-apollo
4dbfc1fac8 Added: Add support for onPreLogCrash() and onPostLogCrash() in CrashHandler so that CrashHandlerClient can decide which exceptions to log and add custom logic 2022-03-17 02:10:51 +05:00
agnostic-apollo
4b07e4f4c0 Added: Add multi process support in TermuxAppSharedPreferences since plugin apps may need to read values modified by termux app process 2022-03-17 02:10:51 +05:00
agnostic-apollo
621545dd0a Added: Add support for getting termux app and plugin app info only in TermuxUtils.getAppInfoMarkdownString() 2022-03-17 02:10:51 +05:00
agnostic-apollo
9a65aa4589 Fixed: Do not add double heading if callingPackageName passed to TermuxUtils.getAppInfoMarkdownString() is a plugin app 2022-03-17 02:10:51 +05:00
agnostic-apollo
021cb60e23 Added: Add TERMUX_API_APT_* constants 2022-03-17 02:10:51 +05:00
agnostic-apollo
14c5fc7b1e Fixed: Suppress warnings for requiring android 11 to request MANAGE_EXTERNAL_STORAGE permission and call Environment.isExternalStorageManager() 2022-03-17 02:10:51 +05:00