mkj / dropbear

Dropbear SSH

Home Page:https://matt.ucc.asn.au/dropbear/dropbear.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Running SCP in Foreground

okashany opened this issue · comments

When running SCP from my C++ program using execvpe, the "-f" option is automatically added, causing SCP to run in the background after authentication:
Executing: program /usr/bin/dbclient host <ip>, user <username>, command scp -v -f <src> <dest>
Unfortunately, the SCP manual does not provide any options to change this behavior and run SCP in the foreground. Please advise. Thanks!

scp itself should still run in the foreground? Dropbear handles -f the same way as OpenSSH's ssh, so it should run in the foreground like normal.

Thank you for the quick reply.
I'm running SCP using execvpe:

pid_t pid = fork();
if (pid == 0) {
        pid_t nextPid = fork();
        if (nextPid == 0) {
               execvpe(cmd, argv, envp); // scp
        }
        else if (nextPid < 0) {....}
        **notice("Wait for transfer to complete");**
        int nextStatus;
        if (waitpid(nextPid, &nextStatus, 0) < 0) {....}
        else if (WEXITSTATUS(nextStatus) != 0) {....}
}
else if (pid < 0) {
    .....
}
else {
        int status;
        if (waitpid(pid, &status, 0) < 0) { .... }
        else if (WEXITSTATUS(status) != 0) { ..... }
}

I need a reliable way to determine when the transfer has finished. It seems that execvpe returns before the transfer is completed and I suspect this is because SCP runs in the background. How can I change that? Many thanks!

Where's your scp from? I don't see a -f argument running the scp binary that comes with Dropbear:

strace -e process -f ./scp   ~/tmp/z1 rpi:tmp/z

execve("./scp", ["./scp", "/home/matt/tmp/z1", "rpi:tmp/z"], 0x7ffd3210b568 /* 57 vars */) = 0
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fb252806a10) = 2341527
strace: Process 2341527 attached
[pid 2341527] execve("/usr/bin/dbclient", ["/usr/bin/dbclient", "rpi", "scp -t tmp/z"], 0x7ffec52fb0c8 /* 57 vars */) = 0
[pid 2341526] wait4(2341527,  <unfinished ...>
[pid 2341527] exit_group(0)             = ?
[pid 2341527] +++ exited with 0 +++
<... wait4 resumed>[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 2341527
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2341527, si_uid=1001, si_status=0, si_utime=0, si_stime=0} ---
exit_group(0)                           = ?
+++ exited with 0 +++

From dropbearmulti ("Dropbear SSH multi-purpose v2022.82"):

root@<>:/tmp# ls -l /usr/bin/scp
/usr/bin/scp -> /usr/sbin/dropbearmulti
root@<>:/tmp# scp -y -v <usr>@<ip>:<path> <dest>
**Executing: program /usr/bin/dbclient host <ip>, user <usr>, command scp -v -f <path>**

Thank you for your help

I understand. Just found it in the code:

(void) snprintf(bp, len, "%s -f %s", cmd, src);

If I understand correctly this (-f) does not mean it runs in the background, it just mentions the src.
I wonder if there is a way to force SCP to run in foreground. Thanks again