【问题标题】:Linux PTYs as module, but no signalsLinux PTYs 作为模块,但没有信号
【发布时间】:2014-04-26 07:21:21
【问题描述】:

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

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

VT52 通过创建一个 BSD pty 对(/dev/terminal with major=3,minor=0 permissions=crwxrwxrwx,以及 /dev/ptyp0 as major 2,minor 0 permissions=crwxrwxrwx)进行通信并打开它们以供阅读和写作。终端向 ptyp0 发送击键,包括偶尔的 ctrl-c \003,它还从 ptyp0 读取返回的数据。

程序在作为会话负责人启动后通过 /dev/terminal 连接到 vt52 守护进程,并获得 pty 作为控制终端。通常,我使用以下代码作为会话负责人运行 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 进行作业控制。

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

【问题讨论】:

    标签: linux shell signals pty


    【解决方案1】:

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

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

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

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

    // 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 );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 2023-03-03
      • 1970-01-01
      • 2011-11-28
      • 2022-01-22
      • 2020-06-18
      • 1970-01-01
      相关资源
      最近更新 更多