c语言 - 如何制作一个后台进程块,用于输入 shell 的"bg"命令?



为了安全控制,我正在实现自己的代码片段。它通常在后台运行,但超时时需要接管当前终端、显示消息、收集用户输入并对用户输入做出反应。

等待超时很容易。当sleep是活动程序时,收集用户输入很容易。防止shell窃取我刚刚尝试收集的用户输入并不容易。

我有理由相信"如果两个程序都这样做了怎么办?"不适用。如果在我们等待输入时触发了另一个程序,那还不错。如果用户想干扰安全检查,有比这更简单的方法。

需要流程组控制。如果您不知道作业控制是使用流程组控制实现的,那么就不容易找到这一点。shell在自己的进程组中启动后台进程,bgfg命令切换允许从终端读取哪个进程组。所有其他进程都阻止从终端读取。

#include <unistd.h>
sleep(600); /* triggering condition goes here */
pid_t pgid = tcgetpgrp(0);
pid_t pid;
if ((pid = fork()) == 0) { /* Need to fork to safely create a new process group to bind to the terminal -- otherwise we might be in the same process group as we started in */
pid_t npid = getpid();
setpgid(npid, npid); /* create new process group and put us in it */
pid_t pid2;
if ((pid2 = fork() == 0) { /* what? another process */
setpgid(getpid(), pgid);
tcsetpgid(0, getpid()); /* set active process group */
_exit(0);
}
if (pid2 > 0) {
int junk;
waitpid(pid2, &junk, 0);
}
struct termios savedattr;
struct termios newattr;
tcgetattr(0, &savedattr);
newattr = savedattr;
newattr.c_lflag |= ICANON | ECHO;
tcsetattr(0, TCSANOW, &newattr); /* set sane terminal state */
printf("nHi there. I'm the background process and I want some input:");
char buf[80];
fgets(buf, 80, stdin);
/* Do something with user input here */
tcsetattr(0, TCSANOW, &savedattr); /* restore terminal state */
tcsetpgrp(0, pgid); /* restore terminal owner -- only possible if the existing owner is a live process */
} else {
if (pid > 0) {
int junk;
waitpid(pid, &junk, 0);
}
}

最新更新