Skip to content

Commit e8b0013

Browse files
Fixed: Fix termux-api commands hanging indefinitely on Android 14 by always using TermuxAm to send intents instead of the socket server that is run by the app
This is currently only done for Android `>= 14` and is a temporary patch until termux-app release is made with finalized support for termux-am-socket to send intents Closes termux/termux-api#638, termux/termux-app#3754 Related termux/termux-app#3647, termux/TermuxAm@8b9844ab
1 parent f74ff80 commit e8b0013

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

termux-api.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include <time.h>
1717
#include <unistd.h>
1818

19+
#ifdef __ANDROID__
20+
#include <android/api-level.h>
21+
#endif
22+
1923
#include "termux-api.h"
2024

2125
#ifndef PREFIX
@@ -44,8 +48,21 @@ _Noreturn void contact_plugin(int argc, char** argv,
4448
};
4549
sigaction(SIGPIPE, &sigpipe_action, NULL);
4650

47-
// try to connect over the listen socket first
48-
int listenfd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
51+
// Try to connect over the listen socket first if running on Android `< 14`.
52+
// On Android `>= 14`, if termux-api app process was started previously
53+
// and it started the socket server, but later Android froze the
54+
// process, the socket will still be connectable, but no response
55+
// will be received until the app process is unfrozen agin and
56+
// `read()` call below will hang indefinitely until that happens,
57+
// so use legacy `am broadcast` command, which will also unfreeze
58+
// the app process to deliver the intent.
59+
// - https://github.com/termux/termux-api/issues/638#issuecomment-1813233924
60+
int listenfd = -1;
61+
#ifdef __ANDROID__
62+
if (android_get_device_api_level() < 34) {
63+
listenfd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
64+
}
65+
#endif
4966
if (listenfd != -1) {
5067
struct sockaddr_un listen_addr = { .sun_family = AF_UNIX };
5168
memcpy(listen_addr.sun_path+1, LISTEN_SOCKET_ADDRESS, strlen(LISTEN_SOCKET_ADDRESS));

0 commit comments

Comments
 (0)