capability.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2013, Suryandaru Triandana <syndtr@gmail.com>
  2. // All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license that can be
  5. // found in the LICENSE file.
  6. // Package capability provides utilities for manipulating POSIX capabilities.
  7. package capability
  8. type Capabilities interface {
  9. // Get check whether a capability present in the given
  10. // capabilities set. The 'which' value should be one of EFFECTIVE,
  11. // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
  12. Get(which CapType, what Cap) bool
  13. // Empty check whether all capability bits of the given capabilities
  14. // set are zero. The 'which' value should be one of EFFECTIVE,
  15. // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
  16. Empty(which CapType) bool
  17. // Full check whether all capability bits of the given capabilities
  18. // set are one. The 'which' value should be one of EFFECTIVE,
  19. // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
  20. Full(which CapType) bool
  21. // Set sets capabilities of the given capabilities sets. The
  22. // 'which' value should be one or combination (OR'ed) of EFFECTIVE,
  23. // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
  24. Set(which CapType, caps ...Cap)
  25. // Unset unsets capabilities of the given capabilities sets. The
  26. // 'which' value should be one or combination (OR'ed) of EFFECTIVE,
  27. // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
  28. Unset(which CapType, caps ...Cap)
  29. // Fill sets all bits of the given capabilities kind to one. The
  30. // 'kind' value should be one or combination (OR'ed) of CAPS,
  31. // BOUNDS or AMBS.
  32. Fill(kind CapType)
  33. // Clear sets all bits of the given capabilities kind to zero. The
  34. // 'kind' value should be one or combination (OR'ed) of CAPS,
  35. // BOUNDS or AMBS.
  36. Clear(kind CapType)
  37. // String return current capabilities state of the given capabilities
  38. // set as string. The 'which' value should be one of EFFECTIVE,
  39. // PERMITTED, INHERITABLE BOUNDING or AMBIENT
  40. StringCap(which CapType) string
  41. // String return current capabilities state as string.
  42. String() string
  43. // Load load actual capabilities value. This will overwrite all
  44. // outstanding changes.
  45. Load() error
  46. // Apply apply the capabilities settings, so all changes will take
  47. // effect.
  48. Apply(kind CapType) error
  49. }
  50. // NewPid initializes a new Capabilities object for given pid when
  51. // it is nonzero, or for the current process if pid is 0.
  52. //
  53. // Deprecated: Replace with NewPid2. For example, replace:
  54. //
  55. // c, err := NewPid(0)
  56. // if err != nil {
  57. // return err
  58. // }
  59. //
  60. // with:
  61. //
  62. // c, err := NewPid2(0)
  63. // if err != nil {
  64. // return err
  65. // }
  66. // err = c.Load()
  67. // if err != nil {
  68. // return err
  69. // }
  70. func NewPid(pid int) (Capabilities, error) {
  71. c, err := newPid(pid)
  72. if err != nil {
  73. return c, err
  74. }
  75. err = c.Load()
  76. return c, err
  77. }
  78. // NewPid2 initializes a new Capabilities object for given pid when
  79. // it is nonzero, or for the current process if pid is 0. This
  80. // does not load the process's current capabilities; to do that you
  81. // must call Load explicitly.
  82. func NewPid2(pid int) (Capabilities, error) {
  83. return newPid(pid)
  84. }
  85. // NewFile initializes a new Capabilities object for given file path.
  86. //
  87. // Deprecated: Replace with NewFile2. For example, replace:
  88. //
  89. // c, err := NewFile(path)
  90. // if err != nil {
  91. // return err
  92. // }
  93. //
  94. // with:
  95. //
  96. // c, err := NewFile2(path)
  97. // if err != nil {
  98. // return err
  99. // }
  100. // err = c.Load()
  101. // if err != nil {
  102. // return err
  103. // }
  104. func NewFile(path string) (Capabilities, error) {
  105. c, err := newFile(path)
  106. if err != nil {
  107. return c, err
  108. }
  109. err = c.Load()
  110. return c, err
  111. }
  112. // NewFile2 creates a new initialized Capabilities object for given
  113. // file path. This does not load the process's current capabilities;
  114. // to do that you must call Load explicitly.
  115. func NewFile2(path string) (Capabilities, error) {
  116. return newFile(path)
  117. }