nohajc / termux-adb

Run adb in Termux without root permissions!
MIT License
311 stars 32 forks source link

Make the Termux-adb rootless more general #13

Closed mengyanshou closed 1 year ago

mengyanshou commented 1 year ago

Background

This is not the problem of your repo. First of all, I'm sorry that I didn't give feedback according to your template. I just want to get some help, I think it will be clearer.

I'm trying to do is make the termux-adb rootless more generic, in other words, it's off-Termux.

I've achieved most of them so far, but I'm still running into some problems.

I have read lib.rs,termux-api source code,termix-api-package source code, but I am not familiar with rust and do not see problems at the code level

This is how I do it,

  1. Run apt download android-tools in termux and get the *.deb file to extract adb binary

  2. Run apt install android-tools and then use nm adb to view the so library on which it dynamically depends. That's about it

    'ld-android.so',
    'libabsl_bad_variant_access.so',
    'libabsl_base.so',
    'libabsl_city.so',
    'libabsl_cord.so',
    'libabsl_cord_internal.so',
    'libabsl_cordz_functions.so',
    'libabsl_cordz_handle.so',
    'libabsl_cordz_info.so',
    'libabsl_crc32c.so',
    'libabsl_crc_cord_state.so',
    'libabsl_crc_internal.so',
    'libabsl_die_if_null.so',
    'libabsl_examine_stack.so',
    'libabsl_exponential_biased.so',
    'libabsl_hash.so',
    'libabsl_int128.so',
    'libabsl_log_globals.so',
    'libabsl_log_internal_check_op.so',
    'libabsl_log_internal_format.so',
    'libabsl_log_internal_globals.so',
    'libabsl_log_internal_log_sink_set.so',
    'libabsl_log_internal_message.so',
    'libabsl_log_internal_nullguard.so',
    'libabsl_log_internal_proto.so',
    'libabsl_log_sink.so',
    'libabsl_low_level_hash.so',
    'libabsl_malloc_internal.so',
    'libabsl_raw_hash_set.so',
    'libabsl_raw_logging_internal.so',
    'libabsl_spinlock_wait.so',
    'libabsl_stacktrace.so',
    'libabsl_status.so',
    'libabsl_statusor.so',
    'libabsl_str_format_internal.so',
    'libabsl_strerror.so',
    'libabsl_strings.so',
    'libabsl_strings_internal.so',
    'libabsl_symbolize.so',
    'libabsl_synchronization.so',
    'libabsl_throw_delegate.so',
    'libabsl_time.so',
    'libabsl_time_zone.so',
    'libbrotlicommon.so',
    'libbrotlidec.so',
    'libbrotlienc.so',
    'libc++.so',
    'libc++_shared.so',
    'libc.so',
    'libdl.so',
    'liblog.so',
    'liblz4.so',
    'libm.so',
    'libprotobuf.so',
    'libusb - 1.0. So,
    'libz.so.1',
    'libzstd.so.1',

    With the above file, I can call this adb binary (targetsdk<=28) in any app

  3. Recompile https://github.com/termux/termux-api this apk, just adjust its signature and change the android: sharedUserId

  4. Recompile https://github.com/termux/TermuxAm will com.termux package names replaced with my own app

  5. Recompile https://github.com/termux/termux-api-package, com.termux replace my app (ditto)

  6. Hard to see here, then I will simulate a minimal environment to ensure the various commands execute properly, including adjusting the shebang of sh, then downloading your termux-adb binary (apt download termux-adb, then unzip)

like this:

截屏2023-03-24 22 15 23 截屏2023-03-24 22 15 32

Current status

At present, I can normally use termux-api command in my app, and can normally use termux-usb -l to view the obtained device

Run the termux-usb -r /path command and can request permission success like this Screenshot_2023-03-24-22-19-14-800_com android systemui Screenshot_2023-03-24-22-19-18-395_com nightmare adbtools' but when i use termux-adb devices cannot get my otg devices,this is the biggest headache Screenshot_2023-03-24-22-28-21-299_com nightmare adbtools Notice that adb in the last picture is your termux-adb, only I made a soft link

mengyanshou commented 1 year ago

any help thanks~

nohajc commented 1 year ago

I like the idea of making termux-adb independent but it was easier for me to make it part of Termux because I have very little experience with Android development. This way I could pretend there's no Java or Android SDK but in fact termux-usb (termux API) is just a wrapper for native SDK functions.

If you're trying to make your own Android app with adb functionality (are you?), I'm not sure porting termux-adb is the best approach. You're basically taking something which was built on top of a "linux terminal emulator" on top of Android instead of writing what you want directly for Android.

Having said that, what you're trying to do should be possible in principle, but it's very difficult for me to guess what might be wrong with your environment. I don't even know how Termux works exactly. How it supports background processes for example (which adb relies on) etc.

Is there anything in the debug logs? (RUST_LOG=debug termux-adb devices)

mengyanshou commented 1 year ago

thanks very much for u reply!!!this is my app https://github.com/nightmare-space/adb_kit, it is an adb GUI Tool,but support android to android(otg),In the past I have implemented only part of the adb protocol in java to support otg,until I found your termux-adb

Yesterday until today, I have been trying to port your termux-adb to my tool

I've been trying, so far I've found some issues with termux-api, which have been fixed. I've found that the termux-adb code in rust relies on three environment variables,

pub extern "C" fn termuxadb_sendfd() -> bool {
    match (env::var("TERMUX_USB_DEV"), env::var("TERMUX_USB_FD"), env::var("TERMUX_ADB_SOCK_FD")) {
        (Ok(termux_usb_dev), Ok(termux_usb_fd), Ok(sock_send_fd)) => {
            if let Err(e) = sendfd_to_adb(&termux_usb_dev, &termux_usb_fd, &sock_send_fd) {
                error!("{}", e)
            }
            true
        }
        _ => false
    }
}

The TERMUX_USB_FD variable has been obtained successfully.

I tried to use RUST_LOG=debug termux-adb devices to feed back more logs yesterday, but strangely, it doesn't output the logs from rust, I don't know why

1679731587957

(Ignore those stupid prints, that's my debug info)

Now I'm ready to recompile your rust code and then compile the new termux-adb to try it out

mengyanshou commented 1 year ago

Oh my God! I forgot that my stupid printing would affect your code's antisequence to json

nohajc commented 1 year ago

Those debug logs are normally stored in /data/data/com.termux/files/usr/tmp/

Don't know what's the equivalent in your environment.

mengyanshou commented 1 year ago

Oh, that's it. I found the log

--- adb starting (pid 32390) ---
adb I 03-25 18:03:58 32390 32390 main.cpp:64] Android Debug Bridge version 1.0.41
adb I 03-25 18:03:58 32390 32390 main.cpp:64] Version 31.0.3p1-android-tools
adb I 03-25 18:03:58 32390 32390 main.cpp:64] Installed as /data/data/com.nightmare.adbtools/files/usr/bin/termux-adb
adb I 03-25 18:03:58 32390 32390 main.cpp:64] 
adb I 03-25 18:03:58 32390 32390 auth.cpp:417] adb_auth_init...
adb I 03-25 18:03:58 32390 32390 auth.cpp:152] loaded new key from '/data/data/com.nightmare.adbtools/files/usr/bin/.android/adbkey' with fingerprint 9445925EB20652C0D673E53866A95F89F1BA6EBF2193056178095D7FA2E00530
adb I 03-25 18:03:58 32390 32390 auth.cpp:392] adb_auth_inotify_init...
adb I 03-25 18:03:58 32390 32402 transport.cpp:305] emulator-5554: write thread spawning
adb I 03-25 18:03:58 32390 32401 transport.cpp:333] emulator-5554: read thread spawning
adb I 03-25 18:03:58 32390 32390 transport.cpp:1655] fetching keys for transport emulator-5554
adb I 03-25 18:03:58 32390 32390 auth.cpp:469] Calling send_auth_response
adb I 03-25 18:03:58 32390 32390 auth.cpp:436] Calling send_auth_publickey
@185adb server killed by remote request
adb I 03-25 18:04:24 32390 32390 transport.cpp:1107] kicking transport 0xb400007765602400 emulator-5554
adb I 03-25 18:04:24 32390 32390 transport.cpp:405] BlockingConnectionAdapter(emulator-5554): stopping
adb I 03-25 18:04:24 32390 32401 transport.cpp:337] emulator-5554: read failed: Success
adb I 03-25 18:04:24 32390 32401 transport.cpp:1200] emulator-5554: connection terminated: read failed
adb I 03-25 18:04:24 32390 32390 transport.cpp:423] BlockingConnectionAdapter(emulator-5554): stopped
--- adb starting (pid 307) ---
adb I 03-25 18:04:49   307   307 main.cpp:64] Android Debug Bridge version 1.0.41
adb I 03-25 18:04:49   307   307 main.cpp:64] Version 31.0.3p1-android-tools
adb I 03-25 18:04:49   307   307 main.cpp:64] Installed as /data/data/com.nightmare.adbtools/files/usr/bin/termux-adb
adb I 03-25 18:04:49   307   307 main.cpp:64] 
adb I 03-25 18:04:49   307   307 auth.cpp:417] adb_auth_init...
[2023-03-25T10:04:49Z DEBUG termuxadb] called opendir with /dev/bus/usb
adb I 03-25 18:04:49   307   307 auth.cpp:152] loaded new key from '/data/data/com.nightmare.adbtools/files/usr/bin/.android/adbkey' with fingerprint 9445925EB20652C0D673E53866A95F89F1BA6EBF2193056178095D7FA2E00530
adb I 03-25 18:04:49   307   307 auth.cpp:392] adb_auth_inotify_init...
[2023-03-25T10:04:49Z DEBUG termuxadb] TERMUX_ADB_PATH=/data/data/com.nightmare.adbtools/files/usr/bin/termux-adb
[2023-03-25T10:04:49Z INFO  termuxadb] listening on socket
adb I 03-25 18:04:49   307   316 transport.cpp:305] emulator-5554: write thread spawning
adb I 03-25 18:04:49   307   315 transport.cpp:333] emulator-5554: read thread spawning
adb I 03-25 18:04:49   307   307 transport.cpp:1655] fetching keys for transport emulator-5554
adb I 03-25 18:04:49   307   307 auth.cpp:469] Calling send_auth_response
adb I 03-25 18:04:49   307   307 auth.cpp:436] Calling send_auth_publickey
[2023-03-25T10:04:49Z INFO  termuxadb] new device connected: /dev/bus/usb/001/002
@189[2023-03-25T10:04:50Z DEBUG termuxadb] called opendir with /dev/bus/usb
[2023-03-25T10:04:51Z DEBUG termuxadb] called opendir with /dev/bus/usb

As mentioned above, my recompiled termux-api added silly prints that would break your deserialization. I removed those prints. Now there is a new development.

I still couldn't see the list of devices, and I found some problems reading the code, which seemed to be due to differences between my environment and Termux's

in this code:

fn run_under_termux_usb(usb_dev_path: &str, cmd_path: &Path, sock_send_fd: RawFd) -> io::Result<ExitStatus> {
    let mut cmd = Command::new("termux-usb");

    cmd.env("TERMUX_USB_DEV", usb_dev_path)
        .arg("-e").arg(cmd_path)
        .args(["-E", "-r", usb_dev_path]);

    cmd.env("TERMUX_ADB_SOCK_FD", sock_send_fd.to_string());
    return cmd.status();
}

~when i exec termux-adb,I should be able to see these two variables in the output of env, but I don't~

mengyanshou commented 1 year ago

After trial and error, there is a new log

[2023-03-25T10:30:56Z INFO  termuxadb] new device connected: /dev/bus/usb/001/002
[2023-03-25T10:30:56Z DEBUG termuxadb] called opendir with /dev/bus/usb
@170[2023-03-25T10:30:57Z INFO  termuxadb] received message (size=20) with fd=4: /dev/bus/usb/001/002
[2023-03-25T10:30:57Z DEBUG termuxadb] updated DIR_MAP: {"/dev/bus/usb": {DirEntry(dirent { d_ino: 2912299489766550477, d_off: 0, d_reclen: 280, d_type: 4 })}, "/dev/bus/usb/001": {DirEntry(dirent { d_ino: 17323114977174020948, d_off: 0, d_reclen: 280, d_type: 2 })}}
[2023-03-25T10:30:57Z DEBUG termuxadb] calling libusb_set_option
[2023-03-25T10:30:57Z DEBUG termuxadb] opening device from 4
[2023-03-25T10:30:57Z WARN  termuxadb] error opening device
[2023-03-25T10:30:57Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:30:57Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:30:57Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:57Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:30:57Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:30:57Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:30:57Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:57Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:30:57Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:30:57Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:30:57Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:30:57Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:57Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:30:57Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:30:57Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:30:57Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:57Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:30:57Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:30:58Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:30:58Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:30:58Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:58Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:30:58Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:30:58Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:30:58Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:58Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:30:58Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:30:58Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:30:58Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:30:58Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:58Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:30:58Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:30:58Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:30:58Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:58Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:30:58Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:30:59Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:30:59Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:30:59Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:59Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:30:59Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:30:59Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:30:59Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:59Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:30:59Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:30:59Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:30:59Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:30:59Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:59Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:30:59Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:30:59Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:30:59Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:30:59Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:30:59Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:00Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:00Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:00Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:00Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:00Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:00Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:00Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:00Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:00Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:00Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:00Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:00Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:00Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:00Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:00Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:00Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:00Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:00Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:01Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:01Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:01Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:01Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:01Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:01Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:01Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:01Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:01Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:01Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:01Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:01Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:01Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:01Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:01Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:01Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:01Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:01Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:02Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:02Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:02Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:02Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:02Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:02Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:02Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:02Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:02Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:02Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:02Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:02Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:02Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:02Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:02Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:02Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:02Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:02Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:03Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:03Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:03Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:03Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:03Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:03Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:03Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:03Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:03Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:03Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:03Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:03Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:03Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:03Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:03Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:03Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:03Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:03Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:04Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:04Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:04Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:04Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:04Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:04Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:04Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:04Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:04Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:04Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:04Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:04Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:04Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:04Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:04Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:04Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:04Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:04Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:05Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:05Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:05Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:05Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:05Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:05Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:05Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:05Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:05Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:05Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:05Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:05Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:05Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:05Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:05Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:05Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:05Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:05Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:06Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:06Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:06Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:06Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:06Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:06Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:06Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:06Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:06Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:06Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:06Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:06Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:06Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:06Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:06Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:06Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:06Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:06Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:07Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:07Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:07Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:07Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:07Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:07Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:07Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:07Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:07Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:07Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:07Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:07Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:07Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:07Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:07Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:07Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:07Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:07Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:08Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:08Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:08Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:08Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:08Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:08Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:08Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:08Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:08Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:08Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:08Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:08Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:08Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:08Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:08Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:08Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:08Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:08Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:09Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:09Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:09Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:09Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:09Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:09Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:09Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:09Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:09Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:09Z INFO  termuxadb] open hook returning fd with value 4
[2023-03-25T10:31:09Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:09Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:09Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a80
[2023-03-25T10:31:09Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:09Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:09Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:09Z DEBUG termuxadb] called closedir with dirp 0xb4000078af409a60
[2023-03-25T10:31:09Z DEBUG termuxadb] closedir: dirp is virtual DirStream
[2023-03-25T10:31:10Z DEBUG termuxadb] called opendir with /dev/bus/usb, remapping to virtual DirStream
[2023-03-25T10:31:10Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a60
[2023-03-25T10:31:10Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:10Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b400 with d_name=001
[2023-03-25T10:31:10Z DEBUG termuxadb] called opendir with /dev/bus/usb/001, remapping to virtual DirStream
[2023-03-25T10:31:10Z DEBUG termuxadb] called readdir with dirp 0xb4000078af409a80
[2023-03-25T10:31:10Z DEBUG termuxadb] readdir: dirp is virtual DirStream
[2023-03-25T10:31:10Z DEBUG termuxadb] readdir returned dirent 0xb4000078af45b900 with d_name=002
[2023-03-25T10:31:10Z DEBUG termuxadb] called open with pathname=/dev/bus/usb/001/002 flags=524288
[2023-03-25T10:31:10Z INFO  termuxadb] open hook returning fd with value 4
nohajc commented 1 year ago

This could be a permission issue. I can see a warning that libtermuxadb cannot open the device for reading its serial number and I suppose neither can adb for communicating with it.

Is it possible you only set permissions in your Android manifest for enumerating USB devices but not for read/write access?

mengyanshou commented 1 year ago

All I need to do to read and write to usb is, I've added,

<uses-feature android:name="android.hardware.usb.host" />

I have some new developments here,

[2023-03-25T13:13:24Z INFO  termuxadb] new device connected: /dev/bus/usb/001/002

With this log, run_under_termux_usbfunction sets socket_send_fd to the TERMUX_ADB_SOCK_FD environment variable, and then termux-adb is called by termux-callback, Will termuxadb_sendfd be called at this point? I'm not sure how this function is called.

int main(int argc, char* argv[], char* envp[]) {
    __adb_argv = const_cast<const char**>(argv);
    __adb_envp = const_cast<const char**>(envp);
    adb_trace_init(argv);
    if (termuxadb::sendfd()) {
        return 0;
    }
    return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
}

oh yeah! i found it.

As expected, I should see the received message log immediately,But no.

So I still suspect that something is wrong with this function, even though the problem itself is not in the code

pub extern "C" fn termuxadb_sendfd() -> bool {
    match (env::var("TERMUX_USB_DEV"), env::var("TERMUX_USB_FD"), env::var("TERMUX_ADB_SOCK_FD")) {
        (Ok(termux_usb_dev), Ok(termux_usb_fd), Ok(sock_send_fd)) => {
            if let Err(e) = sendfd_to_adb(&termux_usb_dev, &termux_usb_fd, &sock_send_fd) {
                error!("{}", e)
            }
            true
        }
        _ => false
    }
}

and i also have some probelm with cross-compile

this is my compile script

export NDK_HOME=/Users/nightmare/Library/Android/sdk/ndk/21.4.7075529 # or set to your ndk home

export MAKE_PATH=build/make-cache
export ABI="arm64-v8a"
export TARGET_ABI=$ABI

create_makefile() {
    cmake \
        -DANDROID_ABI=$TARGET_ABI \
        -DANDROID_PLATFORM=android-24 \
        -DCMAKE_BUILD_TYPE=release \
        -DANDROID_NDK=$NDK_HOME \
        -DCMAKE_TOOLCHAIN_FILE=$NDK_HOME/build/cmake/android.toolchain.cmake \
        -DANDROID_TOOLCHAIN=clang -B $MAKE_PATH -S .
}

create_makefile

cd $MAKE_PATH

make clean
make

and output:

tmare/Desktop/vendor-adb-patched/android.sh'
CMake Warning (dev) at CMakeLists.txt:1 (project):
  cmake_minimum_required() should be called prior to this top-level project()
  call.  Please see the cmake-commands(7) manual for usage documentation of
  both commands.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Version: 7.1.3
-- Build type: release
-- CXX_STANDARD: 11
-- Required features: cxx_variadic_templates
-- Configuring done (0.1s)
-- Generating done (0.2s)
-- Build files have been written to: /Users/nightmare/Desktop/vendor-adb-patched/build/make-cache
 [  0%] Building CXX object vendor/fmtlib/CMakeFiles/fmt.dir/src/format.cc.o
[  0%] Building CXX object vendor/fmtlib/CMakeFiles/fmt.dir/src/os.cc.o
[  1%] Linking CXX static library libfmt.a
[  1%] Built target fmt
[  1%] Building CXX object vendor/CMakeFiles/liblog.dir/logging/liblog/log_event_list.cpp.o
[  1%] Building CXX object vendor/CMakeFiles/liblog.dir/logging/liblog/log_event_write.cpp.o
[  1%] Building CXX object vendor/CMakeFiles/liblog.dir/logging/liblog/logger_name.cpp.o
[  2%] Building CXX object vendor/CMakeFiles/liblog.dir/logging/liblog/logger_read.cpp.o
[  2%] Building CXX object vendor/CMakeFiles/liblog.dir/logging/liblog/logger_write.cpp.o
[  2%] Building CXX object vendor/CMakeFiles/liblog.dir/logging/liblog/properties.cpp.o
[  2%] Building CXX object vendor/CMakeFiles/liblog.dir/logging/liblog/logprint.cpp.o
[  2%] Linking CXX static library libliblog.a
[  2%] Built target liblog
[  2%] Building CXX object vendor/CMakeFiles/libbase.dir/libbase/abi_compatibility.cpp.o
In file included from /Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/abi_compatibility.cpp:19:
In file included from /Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/include/android-base/cmsg.h:27:
/Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/include/android-base/unique_fd.h:156:9: error: 
      use of undeclared identifier 'android_fdsan_exchange_owner_tag'
    if (android_fdsan_exchange_owner_tag) {
        ^
/Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/include/android-base/unique_fd.h:157:26: error: 
      use of undeclared identifier 'android_fdsan_create_owner_tag'
      uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE...
                         ^
/Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/include/android-base/unique_fd.h:159:26: error: 
      use of undeclared identifier 'android_fdsan_create_owner_tag'
      uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE...
                         ^
/Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/include/android-base/unique_fd.h:161:7: error: 
      use of undeclared identifier 'android_fdsan_exchange_owner_tag'
      android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
      ^
/Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/include/android-base/unique_fd.h:165:9: error: 
      use of undeclared identifier 'android_fdsan_close_with_tag'
    if (android_fdsan_close_with_tag) {
        ^
/Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/include/android-base/unique_fd.h:166:22: error: 
      use of undeclared identifier 'android_fdsan_create_owner_tag'
      uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
                     ^
/Users/nightmare/Desktop/vendor-adb-patched/vendor/libbase/include/android-base/unique_fd.h:168:7: error: 
      use of undeclared identifier 'android_fdsan_close_with_tag'
      android_fdsan_close_with_tag(fd, tag);
      ^
7 errors generated.
make[2]: *** [vendor/CMakeFiles/libbase.dir/libbase/abi_compatibility.cpp.o] Error 1
make[1]: *** [vendor/CMakeFiles/libbase.dir/all] Error 2
make: *** [all] Error 2

I've learned a lot in these two days. By the way, I'm familiar with how termux works, and I can get any app to use the full termux environment, so I think I'm missing the key environment https://github.com/Termare/termare_app

thanks again,I will keep trying

nohajc commented 1 year ago

This file descriptor sending mechanism relies on Unix Domain Sockets. Maybe you need some extra permissions for that too?

Anyway, I wish you good luck with your project.

mengyanshou commented 1 year ago

still do not work

mengyanshou commented 1 year ago

I was thrilled when the termux-adb devices command was executed and my device was in the list.

I've done it. The problem is not in your code or Termux-api.c. The problem is in termux-api's java code

Thanks for your help, I will close this issue

If you're interested, after all my work, I'd be happy for you to try out the adb toolkit, which allows anyone to run adb without root without relying on termux, and for the most part, you don't need to enter commands manually, just use the interface. This is my email mengyanshou@gmail.com, thank you again