为什么从 bash 脚本调用超时程序会导致 tcsetattr 挂起?
Why does calling the timeout program from a bash script cause tcsetattr to hang?
出于某种原因调用超时程序,将内部有 tcsetattr 的程序作为参数,从 bash 脚本导致 tcsetattr 挂起。在 bash 脚本之外直接在终端中调用它不会导致它挂起。为什么会这样?查看 https://github.com/coreutils/coreutils/blob/master/src/timeout.c,似乎超时不会与任何文件描述符混淆。看起来它被设置为忽略两个信号,但这在这里不相关。
以下是一个最小的测试用例:
short.c
#include <stdio.h>
#include <termios.h>
#include <string.h>
int main() {
struct termios tty;
tcgetattr(0, &tty);
fprintf(stderr, "Before tcsetattr");
tcsetattr(0, TCSANOW, &tty);
fprintf(stderr, "After tcsetattr");
}
simple_check.sh
#!/bin/bash
timeout 5 ./a.out < /dev/tty
echo $?
Bash输出
$ gcc short.c
$ bash simple_check.sh
Before tcsetattr
124 # Note this should output `After tcsetattr` if it was 'working'
$ timeout 5 ./a.out < /dev/tty
Before tcsetattr
After tcsetattr
可能有用的信息
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
--foreground
超时选项可以避免这个问题?
这停止将超时(和children)放入他们自己的程序组中。
出于某种原因调用超时程序,将内部有 tcsetattr 的程序作为参数,从 bash 脚本导致 tcsetattr 挂起。在 bash 脚本之外直接在终端中调用它不会导致它挂起。为什么会这样?查看 https://github.com/coreutils/coreutils/blob/master/src/timeout.c,似乎超时不会与任何文件描述符混淆。看起来它被设置为忽略两个信号,但这在这里不相关。
以下是一个最小的测试用例:
short.c
#include <stdio.h>
#include <termios.h>
#include <string.h>
int main() {
struct termios tty;
tcgetattr(0, &tty);
fprintf(stderr, "Before tcsetattr");
tcsetattr(0, TCSANOW, &tty);
fprintf(stderr, "After tcsetattr");
}
simple_check.sh
#!/bin/bash
timeout 5 ./a.out < /dev/tty
echo $?
Bash输出
$ gcc short.c
$ bash simple_check.sh
Before tcsetattr
124 # Note this should output `After tcsetattr` if it was 'working'
$ timeout 5 ./a.out < /dev/tty
Before tcsetattr
After tcsetattr
可能有用的信息
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
--foreground
超时选项可以避免这个问题?
这停止将超时(和children)放入他们自己的程序组中。