2024-03-20 19:14:31 -07:00

33 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package container
import (
"os"
"os/exec"
"syscall"
)
// NewParentProcess 启动一个新进程
/*
这里是父进程,也就是当前进程执行的内容。
1.这里的/proc/se1f/exe调用中/proc/self/ 指的是当前运行进程自己的环境exec 其实就是自己调用了自己,使用这种方式对创建出来的进程进行初始化
2.后面的args是参数其中init是传递给本进程的第一个参数在本例中其实就是会去调用initCommand去初始化进程的一些环境和资源
3.下面的clone参数就是去fork出来一个新进程并且使用了namespace隔离新创建的进程和外部环境。
4.如果用户指定了-it参数就需要把当前进程的输入输出导入到标准输入输出上
*/
func NewParentProcess(tty bool, command string) *exec.Cmd {
args := []string{"init", command}
cmd := exec.Command("/proc/self/exe", args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS |
syscall.CLONE_NEWNET | syscall.CLONE_NEWIPC,
}
if tty {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
return cmd
}