termux-am-socket icon indicating copy to clipboard operation
termux-am-socket copied to clipboard

`termux-am` fails to connect to the socket

Open wantguns opened this issue 3 years ago • 9 comments

I want to use the this tool for the purpose of using system am instead of termux's implementation of am. The usage guide / help screen for termux-am says that the server which runs a socket is enabled by default and will launch on start.

Well the server is not running or the sock file is not created when I start termux.

I have not added run-termux-am-socket-server=false in termux.properties.

Is there some documentation on how the server is started or how can I start it myself ?

Some pastes which might help:

~ $ echo "${TERMUX_APP_AM_SOCKET_SERVER_ENABLED}-random"
-random
~ $ termux-am-socket
Could not connect to socket: No such file or directory
~ $ ls /data/data/com.termux/files/apps/com.termux/termux-am/am.sock
ls: cannot access '/data/data/com.termux/files/apps/com.termux/termux-am/am.sock': No such file or directory

infact, i do not have the apps directory inside the files directory:

~ $ ls -lah /data/data/com.termux/files/
total 14K
drwxrwx--x  4 u0_a328 u0_a328 3.5K Jul  4 15:27 .
drwx------  6 u0_a328 u0_a328 3.5K Jul  4 15:27 ..
drwx------ 12 u0_a328 u0_a328 3.5K Sep 29 14:07 home
drwx------ 11 u0_a328 u0_a328 3.5K Jul  4 15:27 usr

Please let me know if you need some other logs / data.

wantguns avatar Oct 28 '22 03:10 wantguns

Do you use Termux from f-droid? AFAIK there hasn't been a release since this was merged, so it's only available in the github build for now.

tareksander avatar Oct 28 '22 05:10 tareksander

Hey @tareksander, thanks for the tip.

I really hoped that was the reason, and I just nuked my previous termux install and installed the latest build from this ref: https://github.com/termux/termux-app/actions/runs/3335823647

I can see the sock file now being created:

~ $ ls -lah /data/data/com.termux/files/apps/com.termux/termux-am/
total 7.0K
drwx------ 2 u0_a401 u0_a401 3.5K Oct 28 12:12 .
drwx------ 3 u0_a401 u0_a401 3.5K Oct 28 12:03 ..
srwx------ 1 u0_a401 u0_a401    0 Oct 28 12:12 am.sock

but the command still won't execute. This is the error I get:

# termux-am
~ $ termux-am foo
Could not connect to socket: No such file or directory

# termux-am-sock
~ $ termux-am-socket
Could not connect to socket: No such file or directory

I have dealt with unix sockets for comm between Termux and Android before. And I have faced this error when trying to create sockets for different purposes. In my experience filesystem sockets just wont work and Abstract sockets is the way to go for IPC between Android and Termux.

termux-am-socket uses filesystem sockets and this could be a reason why I am facing this error.

I don't have bandwidth to post strace logs right now, but will do soon so that this gets resolved at the earliest.

wantguns avatar Oct 28 '22 06:10 wantguns

A logcat -d dump would be better, it seems the socket server isn't running correctly. Maybe there was an error.

tareksander avatar Oct 28 '22 07:10 tareksander

Hello, i have the same issue: fails to connect to the socket My api call are very slow, termux-battery take 3-4seconds...

I'm on android 13,

$ logcat -d | grep --color termux-am 
04-07 10:56:38.246 31477 31477 W ziparchive: Unable to open '/data/data/com.termux/files/usr/libexec/termux-am/am.dm': No such file or directory
04-07 10:57:09.968 31530 31530 W bash    : type=1400 audit(0.0:880303): avc: granted { execute } for name="termux-am-socket" dev="dm-48" ino=217922 scontext=u:r:untrusted_app_27:s0:c153,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c153,c257,c512,c768 tclass=file app=com.termux.api
04-07 10:57:09.972 31530 31530 W bash    : type=1400 audit(0.0:880304): avc: granted { execute } for name="termux-am-socket" dev="dm-48" ino=217922 scontext=u:r:untrusted_app_27:s0:c153,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c153,c257,c512,c768 tclass=file app=com.termux.api
04-07 10:57:09.972 31530 31530 W bash    : type=1400 audit(0.0:880305): avc: granted { execute_no_trans } for path="/data/data/com.termux/files/usr/bin/termux-am-socket" dev="dm-48" ino=217922 scontext=u:r:untrusted_app_27:s0:c153,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c153,c257,c512,c768 tclass=file app=com.termux.api
04-07 10:57:09.972 31530 31530 W termux-am-socke: type=1400 audit(0.0:880306): avc: granted { execute } for path="/data/data/com.termux/files/usr/bin/termux-am-socket" dev="dm-48" ino=217922 scontext=u:r:untrusted_app_27:s0:c153,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c153,c257,c512,c768 tclass=file app=com.termux.api
04-07 10:57:09.972 31530 31530 W termux-am-socke: type=1400 audit(0.0:880307): avc: denied { search } for name="tests" dev="dm-48" ino=119 scontext=u:r:untrusted_app_27:s0:c153,c257,c512,c768 tcontext=u:object_r:shell_test_data_file:s0 tclass=dir permissive=0 app=com.termux.api
04-07 10:57:09.972 31530 31530 W termux-am-socke: type=1400 audit(0.0:880308): avc: denied { search } for name="tests" dev="dm-48" ino=119 scontext=u:r:untrusted_app_27:s0:c153,c257,c512,c768 tcontext=u:object_r:shell_test_data_file:s0 tclass=dir permissive=0 app=com.termux.api
04-07 10:57:09.972 31530 31530 W termux-am-socke: type=1400 audit(0.0:880309): avc: denied { search } for name="tests" dev="dm-48" ino=119 scontext=u:r:untrusted_app_27:s0:c153,c257,c512,c768 tcontext=u:object_r:shell_test_data_file:s0 tclass=dir permissive=0 app=com.termux.api
04-07 10:57:09.972 31530 31530 W termux-am-socke: type=1400 audit(0.0:880310): avc: denied { search } for name="tests" dev="dm-48" ino=119 scontext=u:r:untrusted_app_27:s0:c153,c257,c512,c768 tcontext=u:object_r:shell_test_data_file:s0 tclass=dir permissive=0 app=com.termux.api
04-07 11:02:04.418 32369 32369 W ziparchive: Unable to open '/data/data/com.termux/files/usr/libexec/termux-am/am.dm': No such file or directory

what is /data/data/com.termux/files/usr/libexec/termux-am/am.dm ?

ls "/data/data/com.termux/files/usr/libexec/termux-am/ "
am.apk

cyprienflx avatar Apr 07 '23 09:04 cyprienflx

hey @cyprienflx, iirc i fixed this by using an abstract socket. you can find more about this on my blog: https://wantguns.dev/blog/ipc-on-termux/

lmk if this helps, because i don't think this issue is relevant for me now, we have moved on to using something else at out workplace

wantguns avatar Apr 07 '23 11:04 wantguns

sorry, but i don't understand, i'm not android developper :/

i need to edit termux-am, and create an abstract socket ? or can i create an abstract socket in termux ?

cyprienflx avatar Apr 07 '23 11:04 cyprienflx

Could not connect to socket: No such file or directory

Did you update termux-am-socket package after installation? Socket path was changed (will change again).

https://github.com/termux/termux-am-socket/commit/937545cbf1c475b04dddc205886b1fe2e4a01e01

My api call are very slow, termux-battery take 3-4seconds...

You probably need to look at https://github.com/termux/termux-widget/issues/88#issuecomment-1454538418

agnostic-apollo avatar Apr 07 '23 12:04 agnostic-apollo

Could not connect to socket: No such file or directory

Did you update termux-am-socket package after installation? Socket path was changed (will change again).

937545c

My api call are very slow, termux-battery take 3-4seconds...

You probably need to look at termux/termux-widget#88 (comment)

Yes i have upgraded termux :

$ apt-cache policy  termux-am-socket
termux-am-socket:
  Installed: 1.5.0
  Candidate: 1.5.0
  Version table:
 *** 1.5.0 500
        500 https://mirror.termux.dev/termux-main stable/main aarch64 Packages
        100 /data/data/com.termux/files/usr/var/lib/dpkg/status

Thx for your answer If someone have a slow api response, you need to add -Xnoimage-dex2oat to am you can start this command in termux shell :

# make a backup
cp -v "$(which am)"{,_bck}

# add -Xnoimage-dex2oat
sed -i -e 's#\(exec /system/bin/app_process\).*\(/[[:space:]]*com.termux.termuxam.Am "$@"\)#\1 -Xnoimage-dex2oat \2#g' "$(which am)"

My api call are 0.6s instead of 4s.

Have a nice day

cyprienflx avatar Apr 07 '23 20:04 cyprienflx

hey @cyprienflx, iirc i fixed this by using an abstract socket. you can find more about this on my blog: https://wantguns.dev/blog/ipc-on-termux/

It's deliberately a filesystem socket and not an abstract socket, so other apps cannot connect to it and send intents with the identity of Termux. Although the UID of the peer is checked, it's just another layer of defense.

tareksander avatar Apr 10 '23 12:04 tareksander

Do you use Termux from f-droid? AFAIK there hasn't been a release since this was merged, so it's only available in the github build for now.

I do indeed use termux from f-droid, and I'm seeing this issue. Variable TERMUX_APP__AM_SOCKET_SERVER_ENABLED is not set.

The termux version that I have is 0.118.0, I checked Github (https://github.com/termux/termux-app/releases), and it's the same version number. If there are any more recent apks out there, where will I be able to find them?

Thanks

AlainKnaff avatar Jun 17 '24 11:06 AlainKnaff

Check GitHub action builds of master branch, termux-am-socket is only available for that and changes are planned for it before 0.119.0 stable release.

https://github.com/termux/termux-app#installation https://github.com/termux/termux-app#github

agnostic-apollo avatar Jun 17 '24 11:06 agnostic-apollo

Same issues here, termux-am says

I have termux 0.119.0-beta.1, and termux-am-socket 1.5.0

termux-am says Could not connect to socket: No such file or directory

Strace shows that it tries to connect to a socket at /data/data/com.termux/files/apps/com.termux/termux-am/am.sock, but actually everything down from /data/data/com.termux/files/apps does not exist

Manually creating the directory and then rebooting the phone doesn't make the socket appear either.

Searching for sockets all over the filesystem using find / -type s 2>/dev/null doesn't turn up any matching socket elsewhere either

Thanks,

AlainKnaff avatar Jun 29 '24 10:06 AlainKnaff

0.119.0-beta.1 does not have it enabled, as breaking changes are pending. Will be available in a future beta version.

https://github.com/termux/termux-app/commit/b466e9c88df3da17575532b874f2ff02102165ab

agnostic-apollo avatar Jun 29 '24 17:06 agnostic-apollo