process.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package libcontainer
  2. import (
  3. "fmt"
  4. "io"
  5. "math"
  6. "os"
  7. "github.com/opencontainers/runc/libcontainer/configs"
  8. )
  9. type processOperations interface {
  10. wait() (*os.ProcessState, error)
  11. signal(sig os.Signal) error
  12. pid() int
  13. }
  14. // Process specifies the configuration and IO for a process inside
  15. // a container.
  16. type Process struct {
  17. // The command to be run followed by any arguments.
  18. Args []string
  19. // Env specifies the environment variables for the process.
  20. Env []string
  21. // User will set the uid and gid of the executing process running inside the container
  22. // local to the container's user and group configuration.
  23. User string
  24. // Cwd will change the processes current working directory inside the container's rootfs.
  25. Cwd string
  26. // Stdin is a pointer to a reader which provides the standard input stream.
  27. Stdin io.Reader
  28. // Stdout is a pointer to a writer which receives the standard output stream.
  29. Stdout io.Writer
  30. // Stderr is a pointer to a writer which receives the standard error stream.
  31. Stderr io.Writer
  32. // ExtraFiles specifies additional open files to be inherited by the container
  33. ExtraFiles []*os.File
  34. // consolePath is the path to the console allocated to the container.
  35. consolePath string
  36. // Capabilities specify the capabilities to keep when executing the process inside the container
  37. // All capabilities not specified will be dropped from the processes capability mask
  38. Capabilities []string
  39. // AppArmorProfile specifies the profile to apply to the process and is
  40. // changed at the time the process is execed
  41. AppArmorProfile string
  42. // Label specifies the label to apply to the process. It is commonly used by selinux
  43. Label string
  44. // NoNewPrivileges controls whether processes can gain additional privileges.
  45. NoNewPrivileges *bool
  46. // Rlimits specifies the resource limits, such as max open files, to set in the container
  47. // If Rlimits are not set, the container will inherit rlimits from the parent process
  48. Rlimits []configs.Rlimit
  49. ops processOperations
  50. }
  51. // Wait waits for the process to exit.
  52. // Wait releases any resources associated with the Process
  53. func (p Process) Wait() (*os.ProcessState, error) {
  54. if p.ops == nil {
  55. return nil, newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
  56. }
  57. return p.ops.wait()
  58. }
  59. // Pid returns the process ID
  60. func (p Process) Pid() (int, error) {
  61. // math.MinInt32 is returned here, because it's invalid value
  62. // for the kill() system call.
  63. if p.ops == nil {
  64. return math.MinInt32, newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
  65. }
  66. return p.ops.pid(), nil
  67. }
  68. // Signal sends a signal to the Process.
  69. func (p Process) Signal(sig os.Signal) error {
  70. if p.ops == nil {
  71. return newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
  72. }
  73. return p.ops.signal(sig)
  74. }
  75. // IO holds the process's STDIO
  76. type IO struct {
  77. Stdin io.WriteCloser
  78. Stdout io.ReadCloser
  79. Stderr io.ReadCloser
  80. }
  81. // NewConsole creates new console for process and returns it
  82. func (p *Process) NewConsole(rootuid int) (Console, error) {
  83. console, err := NewConsole(rootuid, rootuid)
  84. if err != nil {
  85. return nil, err
  86. }
  87. p.consolePath = console.Path()
  88. return console, nil
  89. }
  90. // ConsoleFromPath sets the process's console with the path provided
  91. func (p *Process) ConsoleFromPath(path string) error {
  92. if p.consolePath != "" {
  93. return newGenericError(fmt.Errorf("console path already exists for process"), ConsoleExists)
  94. }
  95. p.consolePath = path
  96. return nil
  97. }