runc的detach, console, tty等相关问题

1、 runc/utils_linux.go

func (r *runner) run(config *specs.Process) (int , error)

在该函数中第一次对容器的IO进行了处理,首先调用tty, err := setupIO(process, rootuid, rootgid, r.console, config.Terminal, r.detach, || r.create),创建并返回了一个结构tty,tty的具体类型如下所示:

type tty struct {

  console  libcontainer.Console

  state    *term.State
  closers  []io.Closer
  postStart []io.Closer

  wg    sync.WaitGroup 
}

之后又有handler := newSignalHandler(tty, r.enableSubreaper)以及tty.ClosePostStart(), status, err := handler.forward(process)对tty进行处理(主要是针对winsize)。同时,我们可以发现在handler.forward函数之前有对r.detach进行判断,若r.detach为true则直接返回了。

2、runc/utils_linux.go

// setupIO sets the proper IO on the process depending on the configuration

// detach and createTTy will not work unless a console path is passed  --->即detach 和createTty不能同时为true,除非console不为空

(1)当createTTy为true时,调用 return createTty(process, rootuid, rootgid, console) ---> 要求tty,则优先创建tty

(2)当detach为true时,调用dupStdio(process, rootuid, rootgid), 最后再返回一个空的tty, return &tty{}

(3)当createTTy和detach都为false时,则return createStdioPipes(process, rootuid, rootgid)

------------------------------------------------------------  tty方式 ----------------------------------------------------

3、runc/tty.go

func createTty(p *libcontainer.Process, rootuid, rootgid int, consolePath string) (*tty, error)

(1)当用户指定了consolePath的时候,则直接调用p.consoleFromPath(consolePath),再返回一个空的tty, return &tty{}

(2)当用户未指定consolePath时,则调用console, err := p.NewConsole(rootuid, rootgid)创建一个新的console,并且将标准输入输出与console相连。接着调用state, err := term.SetRawTerminal(os.Stdin.Fd())函数将标准输入设置为raw mode。最后返回:

return &tty {

  console:  console,
  state:   state,
  closers:  []io.Closer{
    console,
  },

}

  

4、runc/libcontainer/console_linux.go

// newConsoleFromPath is an internal function returning an initialized console for use inside a container‘s MNT namespace

func newConsoleFromPath(slavePath string) *linuxConsole

这个函数只是简单地返回, return &linuxConsole{slavePath, slavePath}, linuxConsole的定义如下所示:

type linuxConsole struct {

  master    *os.File
  slavePath   string
}

  

5、runc/libcontainer/console_linux.go

// NewConsole returns an initialized console that can be used within a container by copying bytes

// from the master side to the slave that is attached as the tty for the container‘s init process

func NewConsole(uid, gid int) (Console, error)

首先调用master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR | syscall.O_NOCTTY|syscall.O_CLOEXEC, 0),打开主设备,接着调用console, err := ptsname(master)获取从设备的设备名,之后再对主从设备进行一系列的操作。最后,return &linuxConsole{ slavePath: console, master: master}

------------------------------------------------------------------------ detach 模式---------------------------------------------------------------

6、runc/utils_linux.go

func dupStdio(process *libcontainer.Process, rootuid, rootgid int) error

该函数的操作很简单,就是直接将标准IO,os.Stdin, os.Stdout, os.Stderr和process的Stdin, Stdout, Stderr相连,然后改变这几个文件描述符的uid和gid即可。

--------------------------------------------------------------------------- 不分配terminal,不detach ------------------------------------------

7、runc/tty.go

// setup standard pipes so that the TTY of the calling runc process is not inherited by the container

func createStdioPipes(p *libcontainer.Process, rootuid, rootuid int) (*tty, error)

首先调用i, err := p.InitializeIO(rootuid, rootgid),其中函数InitializeIO函数做的工作其实就是创建了三个管道,并将一端连接process的STDIO,再将另一端返回给i。接着对tty 结构进行填充,主要将STDIO赋值给tty.Closers和tty.postStart。最后将i和os.StdIO相连。

时间: 2024-08-25 22:42:10

runc的detach, console, tty等相关问题的相关文章

u-boot串口和stdio、console初始化及相关操作详解<三>

console是构建在stdio之上的,console的初始化是board_r中最后收尾的操作. console的初始化函数console_init_r在common/console.c中实现: int console_init_r(void) { char *stdinname, *stdoutname, *stderrname; struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL; #ifdef CONFI

tty/pts 相关指令

http://unix.stackexchange.com/questions/136662/how-can-we-know-whos-at-the-other-end-of-a-pseudo-terminal-device stty -F /dev/pts/2 ls /proc/29864/fd/* -lstrace -p 29864 |lessls /proc/locksls /proc/locks -lls /proc/tty/driverscat /proc/tty/driverssud

Chrome开发者工具详解(1):Elements、Console、Sources面板

Chrome开发者工具面板 面板上包含了Elements面板.Console面板.Sources面板.Network面板. Timeline面板.Profiles面板.Application面板.Security面板.Audits面板这些功能面板. 这些按钮的功能点如下: Elements:查找网页源代码HTML中的任一元素,手动修改任一元素的属性和样式且能实时在浏览器里面得到反馈. Console:记录开发者开发过程中的日志信息,且可以作为与JS进行交互的命令行Shell. Sources:断

console对象

今天无意中看到console.info()的时候不自觉的楞了一下,对于console.info()确实不是十分的了解,平时就是用console.log(),既然不太明白就去网上看了一下关于console对象的相关资料. 以Chrome浏览器为例,里面的开发工具顶端有八个面板卡分别是(可能随着浏览器的升级会有所改变): Elements:用来调试网页的HTML源码和CSS代码. Console:用来运行JavaScript命令. Sources:调试JavaScript代码. Network:查看

jQuery基础:remove()与 detach()区别

1.detach() detach() 方法移除被选元素,包括所有文本和子节点. 这个方法会保留 jQuery 对象中的匹配的元素,因而可以在将来再使用这些匹配的元素. detach() 会保留所有绑定的事件.附加的数据,这一点与 remove() 不同. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Docume

Linux之进程管理(2)相关命令之一

Linux之进程管理(2)相关命令之一 procps工具命令对进程管理的使用:   ps   pgrep  pidof  pkill  pstree ps  命令 ps  (Process State)  :report a snapshot of the current processes. 查看当前进程的快照信息表 此命令支持3种风格: 1.SysV风格:选项前必须加-符号,如ps  -ef 2.BSD风格:选项前可以不加-符号,如ps  aux 3.GUN风格:之前两个-符号,类似长选项,

linux 终端下敲ctrl-c时,到底发生了什么?(转)

通过telnet登录到单板,然后按ctrl-c会发生什么情况,流程是怎么样的? 在分析之前,先介绍tty的相关知识.我们可以认为,所有跟输入输出相关的操作,最终都由tty来接管.举例来说,当我们敲 ls /dev时得到 [cpp] view plaincopy ls /dev/ -l total 0 crw-------  1 root root     10, 235  8月 16 13:08 autofs drwxr-xr-x  2 root root         720  8月 16 

uboot里读sd卡内容

1. sd卡升级命令 mmcinit 0 fatload mmc 0:1 0 uzImage.bin 80000 fatload mmc 0:1 1000000 initrd.gz 580000 bootm 0 2. U-Boot脚本 可以保存成nfs.script,放在tftp的根目录 setenv bootargs mem=214M root=/dev/nfsroot nfsroot=10.1.1.2:/home/nfs_android ip=dhcp rw console=ttyS0,11

linux程序后台运行

linux命令后台运行  有两种方式: 1. command & : 后台运行,你关掉终端会停止运行   2. nohup command & : 后台运行,你关掉终端也会继续运行 一. 简介     Linux/Unix 区别于微软平台最大的优点就是真正的多用户,多任务.因此在任务管理上也有别具特色的管理思想.我们知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让程序在前台后台之间切换.而 Linux 提供了 fg 和bg 命令,让你轻松