socat fork 使 netcat 挂起

问题描述

我观察到 socat 的行为,这是我以前从未见过的。

我在终端中使用命令运行 socat(在 Debian 10 下)

# socat tcp-listen:8888,reuseaddr,fork exec:"whoami"

并使用 netcat 连接到 8888/tcp :

$ nc localhost 8888
root


Ncat: broken pipe.

连接到服务后,我正确地收到了 whoami输出,但除非我输入两次新行,否则 netcat 会挂起。

这是预期的行为吗?相反,我希望 netcat 退出并返回提示

-d -d -d 添加socat 命令给出以下几行:

(...)
socat[15813] N execvp'ing "whoami"
socat[15812] N childdied(): handling signal 17
socat[15812] I exec'd process 15813 on socket 1 terminated
socat[15795] I childdied(signum=17)
socat[15812] I waitpid(): child 15813 exited with status 0
socat[15812] I childdied() finished
socat[15812] I transferred 7 bytes from 5 to 6
socat[15795] I waitpid(-1,{},WNOHANG): No child processes
socat[15812] N socket 2 (fd 5) is at EOF
socat[15812] I shutdown(6,1)
socat[15812] I terminated child did not leave data for us
socat[15812] I poll timed out (no data within 0.500000 seconds)
socat[15812] I shutdown(6,2)
socat[15812] I shutdown(5,2)
socat[15812] N exiting with status 0
socat[15795] N childdied(): handling signal 17
socat[15795] I childdied(signum=17)
socat[15795] I childdied(17): cannot identify child 15812
socat[15795] I waitpid(): child 15812 exited with status 0
socat[15795] I waitpid(-1,WNOHANG): No child processes
socat[15795] I childdied() finished
$ socat -V
socat by Gerhard Rieger and contributors - see www.dest-unreach.org
socat version 1.7.3.2 on Nov 19 2017 13:56:10
   running on Linux version #1 SMP Debian 5.8.10-1~bpo10+1 (2020-09-26),release 5.8.0-0.bpo.2-amd64,machine x86_64
features:
  #define WITH_STdio 1
  #define WITH_FDNUM 1
  #define WITH_FILE 1
  #define WITH_CREAT 1
  #define WITH_GOPEN 1
  #define WITH_TERMIOS 1
  #define WITH_PIPE 1
  #define WITH_UNIX 1
  #define WITH_ABSTRACT_UNIXSOCKET 1
  #define WITH_IP4 1
  #define WITH_IP6 1
  #define WITH_RAWIP 1
  #define WITH_GENERICSOCKET 1
  #define WITH_INTERFACE 1
  #define WITH_TCP 1
  #define WITH_UDP 1
  #define WITH_SCTP 1
  #define WITH_LISTEN 1
  #define WITH_SOCKS4 1
  #define WITH_SOCKS4A 1
  #define WITH_PROXY 1
  #define WITH_SYstem 1
  #define WITH_EXEC 1
  #undef WITH_READLINE
  #define WITH_TUN 1
  #define WITH_PTY 1
  #define WITH_OPENSSL 1
  #undef WITH_FIPS
  #define WITH_LIBWRAP 1
  #define WITH_SYCLS 1
  #define WITH_FILAN 1
  #define WITH_RETRY 1
  #define WITH_MSGLEVEL 0 /*debug*/

解决方法

我观察到 socat 的行为,这是我以前从未见过的。

您观察到nc的正常行为,即输出接收数据和发送输入数据;如果您不想 nc 从标准输入读取,则可以选择 -d