process.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // AdditionalGroups specifies the gids that should be added to supplementary groups
  25. // in addition to those that the user belongs to.
  26. AdditionalGroups []string
  27. // Cwd will change the processes current working directory inside the container's rootfs.
  28. Cwd string
  29. // Stdin is a pointer to a reader which provides the standard input stream.
  30. Stdin io.Reader
  31. // Stdout is a pointer to a writer which receives the standard output stream.
  32. Stdout io.Writer
  33. // Stderr is a pointer to a writer which receives the standard error stream.
  34. Stderr io.Writer
  35. // ExtraFiles specifies additional open files to be inherited by the container
  36. ExtraFiles []*os.File
  37. // Initial sizings for the console
  38. ConsoleWidth uint16
  39. ConsoleHeight uint16
  40. // Capabilities specify the capabilities to keep when executing the process inside the container
  41. // All capabilities not specified will be dropped from the processes capability mask
  42. Capabilities *configs.Capabilities
  43. // AppArmorProfile specifies the profile to apply to the process and is
  44. // changed at the time the process is execed
  45. AppArmorProfile string
  46. // Label specifies the label to apply to the process. It is commonly used by selinux
  47. Label string
  48. // NoNewPrivileges controls whether processes can gain additional privileges.
  49. NoNewPrivileges *bool
  50. // Rlimits specifies the resource limits, such as max open files, to set in the container
  51. // If Rlimits are not set, the container will inherit rlimits from the parent process
  52. Rlimits []configs.Rlimit
  53. // ConsoleSocket provides the masterfd console.
  54. ConsoleSocket *os.File
  55. // Init specifies whether the process is the first process in the container.
  56. Init bool
  57. ops processOperations
  58. }
  59. // Wait waits for the process to exit.
  60. // Wait releases any resources associated with the Process
  61. func (p Process) Wait() (*os.ProcessState, error) {
  62. if p.ops == nil {
  63. return nil, newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
  64. }
  65. return p.ops.wait()
  66. }
  67. // Pid returns the process ID
  68. func (p Process) Pid() (int, error) {
  69. // math.MinInt32 is returned here, because it's invalid value
  70. // for the kill() system call.
  71. if p.ops == nil {
  72. return math.MinInt32, newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
  73. }
  74. return p.ops.pid(), nil
  75. }
  76. // Signal sends a signal to the Process.
  77. func (p Process) Signal(sig os.Signal) error {
  78. if p.ops == nil {
  79. return newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
  80. }
  81. return p.ops.signal(sig)
  82. }
  83. // IO holds the process's STDIO
  84. type IO struct {
  85. Stdin io.WriteCloser
  86. Stdout io.ReadCloser
  87. Stderr io.ReadCloser
  88. }