process_unix.go 2.4 KB

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