process_unix.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. containerd "github.com/containerd/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(ctx context.Context, terminal bool) (pipe *IOPipe, err error) {
  27. if err := os.MkdirAll(p.dir, 0700); err != nil {
  28. return nil, err
  29. }
  30. io := &IOPipe{}
  31. io.Stdin, err = fifo.OpenFifo(ctx, p.fifo(unix.Stdin), unix.O_WRONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
  32. if err != nil {
  33. return nil, err
  34. }
  35. defer func() {
  36. if err != nil {
  37. io.Stdin.Close()
  38. }
  39. }()
  40. io.Stdout, err = fifo.OpenFifo(ctx, p.fifo(unix.Stdout), unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
  41. if err != nil {
  42. return nil, err
  43. }
  44. defer func() {
  45. if err != nil {
  46. io.Stdout.Close()
  47. }
  48. }()
  49. if goruntime.GOOS == "solaris" || !terminal {
  50. // For Solaris terminal handling is done exclusively by the runtime therefore we make no distinction
  51. // in the processing for terminal and !terminal cases.
  52. io.Stderr, err = fifo.OpenFifo(ctx, p.fifo(unix.Stderr), unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
  53. if err != nil {
  54. return nil, err
  55. }
  56. defer func() {
  57. if err != nil {
  58. io.Stderr.Close()
  59. }
  60. }()
  61. } else {
  62. io.Stderr = ioutil.NopCloser(emptyReader{})
  63. }
  64. return io, nil
  65. }
  66. func (p *process) sendCloseStdin() error {
  67. _, err := p.client.remote.apiClient.UpdateProcess(context.Background(), &containerd.UpdateProcessRequest{
  68. Id: p.containerID,
  69. Pid: p.friendlyName,
  70. CloseStdin: true,
  71. })
  72. if err != nil && (strings.Contains(err.Error(), "container not found") || strings.Contains(err.Error(), "process not found")) {
  73. return nil
  74. }
  75. return err
  76. }
  77. func (p *process) closeFifos(io *IOPipe) {
  78. io.Stdin.Close()
  79. io.Stdout.Close()
  80. io.Stderr.Close()
  81. }
  82. type emptyReader struct{}
  83. func (r emptyReader) Read(b []byte) (int, error) {
  84. return 0, io.EOF
  85. }
  86. func (p *process) fifo(index int) string {
  87. return filepath.Join(p.dir, p.friendlyName+"-"+fdNames[index])
  88. }