io_unix.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // +build !windows
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package runc
  15. import (
  16. "github.com/pkg/errors"
  17. "github.com/sirupsen/logrus"
  18. "golang.org/x/sys/unix"
  19. "runtime"
  20. )
  21. // NewPipeIO creates pipe pairs to be used with runc
  22. func NewPipeIO(uid, gid int, opts ...IOOpt) (i IO, err error) {
  23. option := defaultIOOption()
  24. for _, o := range opts {
  25. o(option)
  26. }
  27. var (
  28. pipes []*pipe
  29. stdin, stdout, stderr *pipe
  30. )
  31. // cleanup in case of an error
  32. defer func() {
  33. if err != nil {
  34. for _, p := range pipes {
  35. p.Close()
  36. }
  37. }
  38. }()
  39. if option.OpenStdin {
  40. if stdin, err = newPipe(); err != nil {
  41. return nil, err
  42. }
  43. pipes = append(pipes, stdin)
  44. if err = unix.Fchown(int(stdin.r.Fd()), uid, gid); err != nil {
  45. // TODO: revert with proper darwin solution, skipping for now
  46. // as darwin chown is returning EINVAL on anonymous pipe
  47. if runtime.GOOS == "darwin" {
  48. logrus.WithError(err).Debug("failed to chown stdin, ignored")
  49. } else {
  50. return nil, errors.Wrap(err, "failed to chown stdin")
  51. }
  52. }
  53. }
  54. if option.OpenStdout {
  55. if stdout, err = newPipe(); err != nil {
  56. return nil, err
  57. }
  58. pipes = append(pipes, stdout)
  59. if err = unix.Fchown(int(stdout.w.Fd()), uid, gid); err != nil {
  60. // TODO: revert with proper darwin solution, skipping for now
  61. // as darwin chown is returning EINVAL on anonymous pipe
  62. if runtime.GOOS == "darwin" {
  63. logrus.WithError(err).Debug("failed to chown stdout, ignored")
  64. } else {
  65. return nil, errors.Wrap(err, "failed to chown stdout")
  66. }
  67. }
  68. }
  69. if option.OpenStderr {
  70. if stderr, err = newPipe(); err != nil {
  71. return nil, err
  72. }
  73. pipes = append(pipes, stderr)
  74. if err = unix.Fchown(int(stderr.w.Fd()), uid, gid); err != nil {
  75. // TODO: revert with proper darwin solution, skipping for now
  76. // as darwin chown is returning EINVAL on anonymous pipe
  77. if runtime.GOOS == "darwin" {
  78. logrus.WithError(err).Debug("failed to chown stderr, ignored")
  79. } else {
  80. return nil, errors.Wrap(err, "failed to chown stderr")
  81. }
  82. }
  83. }
  84. return &pipeIO{
  85. in: stdin,
  86. out: stdout,
  87. err: stderr,
  88. }, nil
  89. }