gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:00:00 25 4
gpt4 key购买 nike

我编写了一个嵌入式应用程序(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com