thin-edge / thin-edge.io

The open edge framework for lightweight IoT devices
https://thin-edge.io
Apache License 2.0
211 stars 55 forks source link

c8y-remote-access-plugin connection fails due to unexpected argument #2890

Closed reubenmiller closed 1 month ago

reubenmiller commented 1 month ago

Describe the bug

Calling the c8y-remote-access-plugin without the --child argument returns a non-zero exit code with an error message.

c8y-remote-access-plugin "530,rpi4-d83add90fe56,127.0.0.1,2812,asdfasdf-asdf-asdf-asdf-abcdef"

For example

$ c8y-remote-access-plugin "530,rpi4-d83add90fe56,127.0.0.1,2812,asdfasdf-asdf-asdf-asdf-abcdef"
error: unexpected argument '--child' found

Usage: tedge <--config-dir <CONFIG_DIR>> <COMMAND>

For more information, try '--help'.

This is a problem because the default c8y_RemoteAccessConnect operation handler calls c8y-remote-access-plugin as above.

file: /etc/tedge/operations/c8y/c8y_RemoteAccessConnect

[exec]
command = "/usr/bin/c8y-remote-access-plugin"
topic = "c8y/s/ds"
on_message = "530"

To Reproduce

  1. Install the latest thin-edge.io version from the main channel
  2. Try to connect a remote access (passthrough) connection to the device

When the bug is present, the Cumulocity IoT remote access operation will fail with the following reason:

# cat c8y_RemoteAccessConnect-2024-05-22T07:10:43.475663043Z.log
----- $ /usr/bin/c8y-remote-access-plugin "530,rpi4-d83add90fe56,127.0.0.1,2812,asdfasdf-asdf-asdf-asdf-abcdef"
Exit status: 2 (ERROR)

stderr <<EOF
error: unexpected argument '--child' found

Usage: tedge <--config-dir <CONFIG_DIR>> <COMMAND>

For more information, try '--help'.
EOF

stdout (EMPTY)

Expected behavior

Screenshots

Environment (please complete the following information):

Property Value
OS [incl. version] Poky (Yocto Project Reference Distro) 4.0.18 (kirkstone)
Hardware [incl. revision] Raspberry Pi 4 Model B Rev 1.5
System-Architecture Linux rpi4-d83add90fe56 5.15.92-v8 #1 SMP PREEMPT Wed Feb 8 16:47:50 UTC 2023 aarch64 GNU/Linux
thin-edge.io version tedge 1.0.2~319+g2b3e3ea

Additional context

reubenmiller commented 1 month ago

After further investigating, it seems that the problem was introduced in https://github.com/thin-edge/thin-edge.io/pull/2888, and affects Linux (not MacOS).

    let exec_path: std::path::PathBuf = std::env::current_exe()
        .into_diagnostic()
        .with_context(|| "Could not get current process executable")?;

The problem occurs on Linux as the current_exe() result returns the underlying tedge binary and not the symlink that was used to launch c8y-remote-access-plugin. Find tedge is then unexpected when launching another instance of c8y-remote-access-plugin as it calls tedge and not the subcommand in the multi-call binary setup.

The Rust docs for current_exe has a note that the implementation is OS specific, so it seems that Linux will follow symlinks, where-as MacOS does not.

gligorisaev commented 1 month ago

QA has tested the bug and it's not reproducable anymore