- 论坛徽章:
- 0
|
看了一下getty的源码(busybox-1.23.0):
int getty_main(int argc UNUSED_PARAM, char **argv)
/* Open the tty as standard input, if it is not "-" */
debug("calling open_tty\n");
open_tty();
ndelay_off(STDIN_FILENO);
debug("duping\n");
xdup2(STDIN_FILENO, 1);
xdup2(STDIN_FILENO, 2);
static void open_tty(void)
{
/* Set up new standard input, unless we are given an already opened port */
if (NOT_LONE_DASH(G.tty_name)) {
if (G.tty_name[0] != '/')
G.tty_name = xasprintf("/dev/%s", G.tty_name); /* will leak it */
/* Open the tty as standard input */
debug("open(2)\n");
close(0);
xopen(G.tty_name, O_RDWR | O_NONBLOCK); /* uses fd 0 */
/* Set proper protections and ownership */
fchown(0, 0, 0); /* 0:0 */
fchmod(0, 0620); /* crw--w---- */
} else {
char *n;
/*
* Standard input should already be connected to an open port.
* Make sure it is open for read/write.
*/
if ((fcntl(0, F_GETFL) & (O_RDWR|O_RDONLY|O_WRONLY)) != O_RDWR)
bb_error_msg_and_die("stdin is not open for read/write");
/* Try to get real tty name instead of "-" */
n = xmalloc_ttyname(0);
if (n)
G.tty_name = n;
}
applet_name = xasprintf("getty: %s", skip_dev_pfx(G.tty_name));
}
结合调试的结果看,当打开一个终端时,会生成一个bash进程,猜想它去创建/dev/pts/1这些节点,然后这个节点设为当前终端,接着会调用getty,根据上面的代码,重定位stdin、stdout、stderr。
但是,并没有在bash的源码里看到明确的代码,也不知道理解的对不对。还有,这些创建节点具体对应哪些源码还是不清楚,再次呼叫大神!!! |
|