console_linux.go 921 B

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