process_unix.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // +build linux solaris
  2. package libcontainerd
  3. import (
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. goruntime "runtime"
  9. "time"
  10. containerd "github.com/docker/containerd/api/grpc/types"
  11. "github.com/tonistiigi/fifo"
  12. "golang.org/x/net/context"
  13. "golang.org/x/sys/unix"
  14. )
  15. var fdNames = map[int]string{
  16. unix.Stdin: "stdin",
  17. unix.Stdout: "stdout",
  18. unix.Stderr: "stderr",
  19. }
  20. // process keeps the state for both main container process and exec process.
  21. type process struct {
  22. processCommon
  23. // Platform specific fields are below here.
  24. dir string
  25. }
  26. func (p *process) openFifos(terminal bool) (pipe *IOPipe, err error) {
  27. if err := os.MkdirAll(p.dir, 0700); err != nil {
  28. return nil, err
  29. }
  30. ctx, _ := context.WithTimeout(context.Background(), 15*time.Second)
  31. io := &IOPipe{}
  32. io.Stdin, err = fifo.OpenFifo(ctx, p.fifo(unix.Stdin), unix.O_WRONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
  33. if err != nil {
  34. return nil, err
  35. }
  36. defer func() {
  37. if err != nil {
  38. io.Stdin.Close()
  39. }
  40. }()
  41. io.Stdout, err = fifo.OpenFifo(ctx, p.fifo(unix.Stdout), unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
  42. if err != nil {
  43. return nil, err
  44. }
  45. defer func() {
  46. if err != nil {
  47. io.Stdout.Close()
  48. }
  49. }()
  50. if goruntime.GOOS == "solaris" || !terminal {
  51. // For Solaris terminal handling is done exclusively by the runtime therefore we make no distinction
  52. // in the processing for terminal and !terminal cases.
  53. io.Stderr, err = fifo.OpenFifo(ctx, p.fifo(unix.Stderr), unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
  54. if err != nil {
  55. return nil, err
  56. }
  57. defer func() {
  58. if err != nil {
  59. io.Stderr.Close()
  60. }
  61. }()
  62. } else {
  63. io.Stderr = ioutil.NopCloser(emptyReader{})
  64. }
  65. return io, nil
  66. }
  67. func (p *process) sendCloseStdin() error {
  68. _, err := p.client.remote.apiClient.UpdateProcess(context.Background(), &containerd.UpdateProcessRequest{
  69. Id: p.containerID,
  70. Pid: p.friendlyName,
  71. CloseStdin: true,
  72. })
  73. return err
  74. }
  75. func (p *process) closeFifos(io *IOPipe) {
  76. io.Stdin.Close()
  77. io.Stdout.Close()
  78. io.Stderr.Close()
  79. }
  80. type emptyReader struct{}
  81. func (r emptyReader) Read(b []byte) (int, error) {
  82. return 0, io.EOF
  83. }
  84. func (p *process) fifo(index int) string {
  85. return filepath.Join(p.dir, p.friendlyName+"-"+fdNames[index])
  86. }