github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/console_linux.go (about)

     1  package libcontainer
     2  
     3  import (
     4  	"os"
     5  
     6  	"golang.org/x/sys/unix"
     7  )
     8  
     9  // mount initializes the console inside the rootfs mounting with the specified mount label
    10  // and applying the correct ownership of the console.
    11  func mountConsole(slavePath string) error {
    12  	f, err := os.Create("/dev/console")
    13  	if err != nil && !os.IsExist(err) {
    14  		return err
    15  	}
    16  	if f != nil {
    17  		// Ensure permission bits (can be different because of umask).
    18  		if err := f.Chmod(0o666); err != nil {
    19  			return err
    20  		}
    21  		f.Close()
    22  	}
    23  	return mount(slavePath, "/dev/console", "bind", unix.MS_BIND, "")
    24  }
    25  
    26  // dupStdio opens the slavePath for the console and dups the fds to the current
    27  // processes stdio, fd 0,1,2.
    28  func dupStdio(slavePath string) error {
    29  	fd, err := unix.Open(slavePath, unix.O_RDWR, 0)
    30  	if err != nil {
    31  		return &os.PathError{
    32  			Op:   "open",
    33  			Path: slavePath,
    34  			Err:  err,
    35  		}
    36  	}
    37  	for _, i := range []int{0, 1, 2} {
    38  		if err := unix.Dup3(fd, i, 0); err != nil {
    39  			return err
    40  		}
    41  	}
    42  	return nil
    43  }