gpt4 book ai didi

c - 如何在 setsid 后恢复控制 tty

转载 作者:太空狗 更新时间:2023-10-29 12:29:53 27 4
gpt4 key购买 nike

fork() 之后,我执行了一个 setsid 来创建一个新 session 。但是当我这样做时,我不能使用像 emacs 这样使用 termcaps 的程序。它会出现错误 "emacs: Could not open file:/dev/tty".

我怎样才能恢复对/dev/tty 的控制?

最佳答案

使用ioctl( fd, TIOCSCTTY ) :

TIOCSCTTY

int arg

Make the given terminal the controlling terminal of the calling process. The calling process must be a session leader and not have a controlling terminal already. If this terminal is already the controlling terminal of a different session group then the ioctl fails with EPERM, unless the caller is root (more precisely: has the CAP_SYS_ADMIN capability) and arg equals 1, in which case the terminal is stolen, and all processes that had it as controlling terminal lose it.

void

If the given terminal was the controlling terminal of the calling process, give up this controlling terminal. If the process was session leader, then send SIGHUP and SIGCONT to the foreground process group and all processes in the current session lose their controlling terminal.

有很多注意事项。阅读这篇标题为 "Reptyr: Changing a Process's Controlling Terminal" 的博文并查看引用的代码。该代码将一个进程从一个终端移植到另一个终端,并且可以完全满足您的需要。

关于c - 如何在 setsid 后恢复控制 tty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692575/

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