termux / termux-packages

A package build system for Termux.
https://termux.dev
Other
13.07k stars 3k forks source link

[Bug]: usbmuxd not working: Could not open lockfile #20827

Closed mast3rz3ro closed 2 months ago

mast3rz3ro commented 2 months ago

Problem description

Hi,

I'm trying to communicate with my iDevice but I'm not able to start the usbmuxd server, even through the root I'm getting the same error.

~ $ usbmuxd -f -v
[07:11:44.095][3] usbmuxd v1.1.1-47-g049877e starting up
[07:11:44.095][0] Could not open lockfile
~ $ tsu
.../files/home # usbmuxd -f -v
[07:11:54.535][3] usbmuxd v1.1.1-47-g049877e starting up
[07:11:54.535][0] Could not open lockfile
.../files/home #

I'm using usbmuxd provided by the repo, But I have also tried compiling it from the source and it's was resulted in the same behaviour.

What steps will reproduce the bug?

Running the usbmuxd

What is the expected behavior?

Getting usbmuxd to run without any issues.

System information

Termux Variables:
unsupported
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb https://mirror.bouwhuis.network/termux/termux-main stable main
# root-repo (sources.list.d/root.list)
deb https://mirror.bouwhuis.network/termux/termux-root root stable
Updatable packages:
Running as root. Cannot check package updates.
termux-tools version:
1.42.4
Android version:
35
Kernel build information:
Linux localhost 4.14.336-perf-gbd8f1c4cdb06-dirty #2 SMP PREEMPT Tue Mar 12 13:01:34 UTC 2024 aarch64 Android
Device manufacturer:
Xiaomi
Device model:
Unknown
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
Grimler91 commented 2 months ago

Probably because we are missing a patch to change lockfile path: https://github.com/libimobiledevice/usbmuxd/blob/bc0b91ca856811f4393318dc83db6dc3c1ac326d/src/main.c#L57

Grimler91 commented 2 months ago

Also, should usbmuxd be in root-packages? Is it useful without root?

mast3rz3ro commented 2 months ago

Also, should usbmuxd be in root-packages? Is it useful without root?

It's helps to communicate with the iDevices, which will allow to use libimobiledevice tools for example we could use idevicerestore to restore the device or ideviceinstaller to install any iPA file.

Grimler91 commented 2 months ago

Also, should usbmuxd be in root-packages? Is it useful without root?

It's helps to communicate with the iDevices, which will allow to use libimobiledevice tools for example we could use idevicerestore to restore the device or ideviceinstaller to install any iPA file.

And this does not require termux with root access?

mast3rz3ro commented 2 months ago

Probably because we are missing a patch to change lockfile path

I tried patching it but it still results in the same behaviour.

static const char *socket_path = "/data/data/com.termux/files/usr/var/run/usbmuxd";
#define DEFAULT_LOCKFILE "/data/data/com.termux/files/usr/var/run/usbmuxd.pid"

And this does not require termux with root access?

Yep, from my experience I used to use libimobiledevice without requiring a root access however some tools such as libirecovery may requiring running them as root.

Biswa96 commented 2 months ago

After patching the paths, I got the following runtime error. Is that expected?

$ usbmuxd -f -v
[20:43:50.768][3] usbmuxd v1.1.1-47-g049877e-dirty starting up
[20:43:50.769][4] Creating socket
[20:43:50.770][4] Listening on /data/data/com.termux/files/usr/var/run/usbmuxd
[20:43:50.770][4] Initializing USB
[20:43:50.770][3] Using libusb 1.0.27
[20:43:50.771][0] libusb_init failed: LIBUSB_ERROR_IO

If it is expected I can try to create a pull request with my changes.

mast3rz3ro commented 2 months ago

After patching the paths, I got the following runtime error. Is that expected?

Yep, I think you have patch it correctly, and according to the output you have got (which I didn't get it) it's may require a root access to run it.

Biswa96 commented 2 months ago

it's may require a root access to run it.

I have moved the package to root repository. Please make sure you have installed root-repo package before updating usbmuxd. command apt install root-repo

mast3rz3ro commented 2 months ago

I have updated my root-repo and updated my usbmuxd however I wasn't able to run it even with root access. According to my output it's seems like the patch are missing something.

Here's my output:

~ $ usbmuxd -f -v
[02:15:22.381][3] usbmuxd v1.1.1-47-g049877e-dirty starting up                                                        [02:15:22.381][0] Could not open lockfile
~ $ tsu
.../files/home # usbmuxd -f -v                             [02:15:31.806][3] usbmuxd v1.1.1-47-g049877e-dirty starting up
[02:15:31.806][4] Creating socket                          [02:15:31.806][0] create_socket: unlink(/data/data/com.termux/files/usr/var/run/usbmuxd) failed: Is a directory

Durning my previous attempts I have made the usbmuxd folder in the following path data/data/com.termux/files/usr/var/run/

Removing the folder has resolved my issue, and I confirm your patch seems to be working correctly without no issues.

mast3rz3ro commented 2 months ago

Now I'm able to run the usbmuxd service normally, however I'm not able to communicate with my iDevice because my iDevice are begin recognised as MTP device.

Here's my output when I run usbmuxd :

~ $ usbmuxd -v -f
[02:43:26.292][3] usbmuxd v1.1.1-47-g049877e-dirty starting up
[02:43:26.292][0] Could not open lockfile
~ $ tsu
.../files/home # usbmuxd -v -f
[02:43:47.631][3] usbmuxd v1.1.1-47-g049877e-dirty starting up
[02:43:47.631][4] Creating socket
[02:43:47.631][4] Listening on /data/data/com.termux/files/usr/var/run/usbmuxd
[02:43:47.631][4] Initializing USB
[02:43:47.631][3] Using libusb 1.0.27
[02:43:47.631][4] Registering for libusb hotplug events
[02:43:47.631][4] 0 devices detected
[02:43:47.631][3] Initialization complete

And here's the output while I'm trying to communicate with my iDevice:

~ $ ps -e | grep usbmuxd
~ $ usbmuxd
~ $ ps -e | grep usbmuxd
~ $ tsu
.../files/home # usbmuxd
.../files/home # ps -e | grep usbmuxd
14364 ?        00:00:00 usbmuxd
.../files/home # idevice_id
ERROR: Unable to retrieve device list!
.../files/home # idevice_id
ERROR: Unable to retrieve device list!
.../files/home # ps -e | grep usbmuxd
14364 ?        00:00:00 usbmuxd

Here's a screenshot that shows how is my device are detected: usbmuxd_termux_iphone_to_android_pic1

usbmuxd_termux_iphone_to_android_pic2

Biswa96 commented 2 months ago

however I'm not able to communicate with my iDevice

Sadly, I can't help with that. I've no iDevice.

mast3rz3ro commented 2 months ago

Sadly, I can't help with that. I've no iDevice.

Ok, I thank you! for the patch it's really giving a big hope to run the usbmuxd service.

Here's my output when listing devices:

~ $ lsusb
No command lsusb found, did you mean:
 Command lsb in package lrzsz
~ $ tsu
.../files/home # lsusb
.../files/home # which lsusb
/system/bin/lsusb
.../files/home # lsusb
Bus 001 Device 001: ID 1d6b:0002
Bus 001 Device 002: ID 05ac:12a8
Bus 002 Device 001: ID 1d6b:0003
mast3rz3ro commented 1 week ago

@agnostic-apollo

Apparently the @Biswa96 commit still doesn't solves the issue. There is still something missing here. The important thing to note here is that when compiling usbmuxd from source it's requests for udev package however tho this package it's not available it's just continues configuring.

Package udev was not found in the pkg-config search path.
Perhaps you should add the directory containing `udev.pc'
to the PKG_CONFIG_PATH environment variable
No package 'udev' found
Package systemd was not found in the pkg-config search path.
Perhaps you should add the directory containing `systemd.pc'
to the PKG_CONFIG_PATH environment variable
No package 'systemd' found
./configure: 14175: test: x${localstatedir}/run: unexpected operator

Other important note I would like to mention is that I can normally use fastboot and adb but with root access.

Biswa96 commented 1 week ago

The important thing to note here is that when compiling usbmuxd from source it's requests for udev package

The usbmuxd package was compiled with --without-systemd option because there is not systemd in Android by-default.

agnostic-apollo commented 1 week ago

What issue are you trying to fix now after the patch got the server started, is it the device being detected as MTP? Usb device communication is not really my thing though.

mast3rz3ro commented 1 week ago

@agnostic-apollo

I'm trying to start communicating with the iDevice using libimobiledevice

The fact that device are getting recognised as MTP is a normal behaviour on iDevices. Because on computer the device appears as MTP and we can still communicate with it using libimobiledevice

agnostic-apollo commented 1 week ago

Your android device is the usb host, and ios is the slave. From MTP's perspective, android is the initiator and ios is the responder. But how mtp responder devices are handled would be different on a computer and android causing issues. Run idevice_id -d and it should show debug info which might be useful.

Additionally, run logcat -c with root before connecting ios device and then logcat -d > /sdcard/logcat.txt after running usbmuxd -v -f and idevice_id -d and then check logs for any potential issues, like selinux avc denied errors. Even with root, selinux restrictions still apply.

When mtp device is connected, UsbHostManager should log a message for Added device.

Once all that is done, give your android device an instant death by incineration for putting it through the agony of being connected to an ios device.

mast3rz3ro commented 1 week ago

Run idevice_id -d and it should show debug info which might be useful.

From my experience even idevice_id won't work without starting usbmuxd first.

~ $ sudo lsusb
Bus 001 Device 001: ID 1d6b:0002
Bus 001 Device 002: ID 05ac:12a8
Bus 002 Device 001: ID 1d6b:0003
~ $ sudo idevice_id -d
ERROR: Unable to retrieve device list!

Additionally, run logcat -c with root before connecting ios device and then logcat -d > /sdcard/logcat.txt after running usbmuxd -v -f and idevice_id -d and then check logs for any potential issues, like selinux avc denied errors. Even with root, selinux restrictions still apply.

Thanks you! I'm going to try what you have suggested and let you know what happens.

agnostic-apollo commented 1 week ago

idevice_id won't work without starting usbmuxd first.

Yes, that's required.

agnostic-apollo commented 1 week ago

Make sure any running usbmuxd daemons are killed first.