process.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package libcontainer
  2. import (
  3. "errors"
  4. "io"
  5. "math"
  6. "os"
  7. "github.com/opencontainers/runc/libcontainer/configs"
  8. )
  9. var errInvalidProcess = errors.New("invalid process")
  10. type processOperations interface {
  11. wait() (*os.ProcessState, error)
  12. signal(sig os.Signal) error
  13. pid() int
  14. }
  15. // Process specifies the configuration and IO for a process inside
  16. // a container.
  17. type Process struct {
  18. // The command to be run followed by any arguments.
  19. Args []string
  20. // Env specifies the environment variables for the process.
  21. Env []string
  22. // User will set the uid and gid of the executing process running inside the container
  23. // local to the container's user and group configuration.
  24. User string
  25. // AdditionalGroups specifies the gids that should be added to supplementary groups
  26. // in addition to those that the user belongs to.
  27. AdditionalGroups []string
  28. // Cwd will change the processes current working directory inside the container's rootfs.
  29. Cwd string
  30. // Stdin is a pointer to a reader which provides the standard input stream.
  31. Stdin io.Reader
  32. // Stdout is a pointer to a writer which receives the standard output stream.
  33. Stdout io.Writer
  34. // Stderr is a pointer to a writer which receives the standard error stream.
  35. Stderr io.Writer
  36. // ExtraFiles specifies additional open files to be inherited by the container
  37. ExtraFiles []*os.File
  38. // Initial sizings for the console
  39. ConsoleWidth uint16
  40. ConsoleHeight uint16
  41. // Capabilities specify the capabilities to keep when executing the process inside the container
  42. // All capabilities not specified will be dropped from the processes capability mask
  43. Capabilities *configs.Capabilities
  44. // AppArmorProfile specifies the profile to apply to the process and is
  45. // changed at the time the process is execed
  46. AppArmorProfile string
  47. // Label specifies the label to apply to the process. It is commonly used by selinux
  48. Label string
  49. // NoNewPrivileges controls whether processes can gain additional privileges.
  50. NoNewPrivileges *bool
  51. // Rlimits specifies the resource limits, such as max open files, to set in the container
  52. // If Rlimits are not set, the container will inherit rlimits from the parent process
  53. Rlimits []configs.Rlimit
  54. // ConsoleSocket provides the masterfd console.
  55. ConsoleSocket *os.File
  56. // Init specifies whether the process is the first process in the container.
  57. Init bool
  58. ops processOperations
  59. LogLevel string
  60. // SubCgroupPaths specifies sub-cgroups to run the process in.
  61. // Map keys are controller names, map values are paths (relative to
  62. // container's top-level cgroup).
  63. //
  64. // If empty, the default top-level container's cgroup is used.
  65. //
  66. // For cgroup v2, the only key allowed is "".
  67. SubCgroupPaths map[string]string
  68. }
  69. // Wait waits for the process to exit.
  70. // Wait releases any resources associated with the Process
  71. func (p Process) Wait() (*os.ProcessState, error) {
  72. if p.ops == nil {
  73. return nil, errInvalidProcess
  74. }
  75. return p.ops.wait()
  76. }
  77. // Pid returns the process ID
  78. func (p Process) Pid() (int, error) {
  79. // math.MinInt32 is returned here, because it's invalid value
  80. // for the kill() system call.
  81. if p.ops == nil {
  82. return math.MinInt32, errInvalidProcess
  83. }
  84. return p.ops.pid(), nil
  85. }
  86. // Signal sends a signal to the Process.
  87. func (p Process) Signal(sig os.Signal) error {
  88. if p.ops == nil {
  89. return errInvalidProcess
  90. }
  91. return p.ops.signal(sig)
  92. }
  93. // IO holds the process's STDIO
  94. type IO struct {
  95. Stdin io.WriteCloser
  96. Stdout io.ReadCloser
  97. Stderr io.ReadCloser
  98. }