Linux PTY 作为模块,但没有信号

标签 linux shell signals pty

我编写了一个嵌入式应用程序(vt52 终端仿真器),在基于 Linux ROM 的系统上运行,没有内置 ptys;和Unix98坏了。但由于我必须有 pty 才能使终端工作...我手动将旧式 BSD pty 编译为内核模块。 但由于某些奇怪的原因,我无法使用 BSD ptys 成功发送 SIGINT (ctrl-c)...

VT52 程序是一个守护程序,而不是我想要向其发送信号的程序的父程序——但这应该不重要,不是吗?我的意思是,有一个小奴隶参与其中......

VT52 通过创建 BSD pty 对(/dev/terminal ,其中 Major=3、minor=0 权限=crwxrwxrwx,以及/dev/ptyp0 作为主要 2、次要 0 权限=crwxrwxrwx)进行通信,并打开它们进行读写。终端将击键(包括偶尔的 ctrl-c\003)发送到 ptyp0,并且还从 ptyp0 读取返回的数据。

程序作为 session 领导者启动后,通过/dev/terminal 连接到 vt52 守护进程,并获得 pty 作为控制终端。通常,我使用以下代码作为 session 领导者运行 busybox sh (?ash?) 程序:

// snippet from session.c
child=fork();
...
session=setsid();
...
// Child alone gets here.
close(0); close(1); close(2); // Close all IO to allow for a new CTTY.
open( argv[1], O_RDONLY ); // stdin
open( argv[1], O_WRONLY ); // stdout 
open( argv[1], O_WRONLY ); // stderr
ioctl( 0, TIOCSCTTY, 0 ); // Set the controlling TTY based on stdin
return execvp( cmd, args ); // run whatever program the user requested.

我知道它有效,因为我在 vt52 上运行了“ps a”,并得到:

PID TTY        STAT   TIME COMMAND
476 ttymxc0    Ss+    0:00 /sbin/getty -L 115200 ttymxc0 vt102
631 ?          Ds+   34:13 ./vt52 /dev/ttyGS0
706 terminal   Ss     0:00 /bin/sh -i
711 terminal   R+     0:00 ps a      

所以,session.c 工作了,并且 CTTY 是正确的......因此,当我发送 ctrl-c; shell 应该终止进程组/子进程——但它没有。 例如,如果我运行“sleep 20”,然后按 ctrl-c,则 20 秒内没有任何反应...

我检查以确保 vt52 实际上正在将\x03 写入/dev/ptyp0,并且确实如此...

所以,然后我通过“stty -a”检查了伪终端设置:

speed 38400 baud; rows 84; columns 75;
intr - ^C; quit = "^\; erase = ^?; kill = ^U; eof = ^D; 
eol = <undef>; eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; inext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff iuclc -ixany -imaxbel
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

一切看起来都不错... 所以 - 我不明白为什么 SIGINT 没有被发送到 shell 的子进程。 我错过了什么?

如果 shell 位于命令提示符处,并且我按 ctrl-c,它会转到下一行...尽管我不知道它是否会这样做,因为它捕获了信号,或接收了字符代码 - 而且我不知道如何弄清楚它...但无论如何,shell 都不会基于 SIGINT/control-C 进行作业控制。

帮助...:)我如何打开它?

最佳答案

线索是按 ctrl-c 在 shell 下生成一个新的提示符。 我编写了一个程序来解析标准输入,并以十六进制打印出每个字符。 控制 C 的代码不通过 PTY——因此 pty 毕竟生成了信号,shell 正在检测它;但子进程并没有终止。

问题在于,由于 shell 继承了其父进程的信号操作 (sigaction),因此在启动新 shell 之前必须检查或重置信号; 在嵌入式设备(索尼 PRS900)上,SIGINT 和许多其他信号的默认操作已更改为 SA_IGN(忽略)。

因此,只需在 session.c 程序中添加一些代码即可将所有信号初始化为其默认操作,然后启用所有信号。 这将解决问题,并且中断将正常工作。

session 日程的完整副本如下;它将创建一个新 session ,将所有信号设置为默认值并启用它们,然后它将执行您选择的程序...

// A program to create a new Linux session connecting to a specified terminal,
// and then execing a program.
// Written by Andrew Robinson of Scappoose Oregon.
// Released under (C) 2014, GPL v3
// https://www.gnu.org/copyleft/gpl.html


#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>

int main(int narg, char * const argv[] ) {
    pid_t session;
    char *const cmd  = argv[2];
    char *const *args = argv+2;

    if (narg<3) {
        fprintf(stderr,
            "Usage: %s new_tty program [params]\n", argv[0] );
        return 1;
    }

    session=fork();

    if (session<0) return session;
    if (session) {
        int status;
        printf("Session pid %d exited\n", waitpid( session, &status, 0 ) );
        return status;
    }

    // Child falls through to here... print the sessionID.

{  // Signal handling is all to be set to defaults.
   // INT should then terminate subprocesses of the shell.
    sigset_t mask;
    struct sigaction act;
    int i;

    // set all signals to the default action.
    sigemptyset( &act.sa_mask );
    act.sa_handler=SIG_DFL;
    act.sa_flags=0;
    for(i=1; i< _NSIG-1; ++i ) // Iterate over all signals.
        sigaction( i, &act, NULL ); // Set action to default.

    // enable all signals.
    sigprocmask( SIG_SETMASK, &mask, NULL );
    sigprocmask( 0,NULL,&mask );

    if (!sigismember(&mask,SIGINT)) printf("SIGINT enabled\n");
}

    session=setsid(); // make a session leader.
    if (session<0) {
        perror("bad-session:");
    }
    printf("%d\n", (int)session );
    fflush(stdout);

    close(0); close(1); close(2); // Close all IO to allow for a new CTTY.

    open( argv[1], O_RDONLY ); // stdin
    open( argv[1], O_WRONLY ); // stdout 
    open( argv[1], O_WRONLY ); // stderr

    ioctl( 0, TIOCSCTTY, 0 ); // Set the controlling TTY based on stdin
    return execvp( cmd, args );
}

关于Linux PTY 作为模块,但没有信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23307973/

相关文章:

javascript - Epiphany GNOME Web 浏览器 2.28.0 在呈现日语日期时挂起

linux - Ada 程序在 Linux 中有效,但在 GPS Windows 10 中无效

shell - 如何在 sh 中向变量追加换行符?

从 SIMD 指令捕获 SIGFPE

python - python 如何在 os.system ("sleep..."时阻塞信号)?

linux - 如何防止Linux上的ksh被局部变量覆盖全局变量?

c++ - 如何在linux上打包c++依赖

bash - 为什么有些人在 shell 脚本中的 if 条件后放一个分号?

linux - 如何使用 Linux split 将一个多 GB 的文件分割成大约 1.5 GB 的 block ?

c - 在 C 中运行时与应用程序交互