-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
- Loading branch information
Ma Shimiao
committed
Jul 5, 2017
1 parent
038e9eb
commit 4708aab
Showing
5 changed files
with
110 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/syndtr/gocapability/capability" | ||
) | ||
|
||
// CapValid checks whether a capability is valid | ||
func CapValid(c string, hostSpecific bool) error { | ||
isValid := false | ||
|
||
if !strings.HasPrefix(c, "CAP_") { | ||
return fmt.Errorf("capability %s must start with CAP_", c) | ||
} | ||
for _, cap := range capability.List() { | ||
if c == fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())) { | ||
if hostSpecific && cap > LastCap() { | ||
return fmt.Errorf("CAP_%s is not supported on the current host", c) | ||
} | ||
isValid = true | ||
break | ||
} | ||
} | ||
|
||
if !isValid { | ||
return fmt.Errorf("Invalid capability: %s", c) | ||
} | ||
return nil | ||
} | ||
|
||
// LastCap return last cap of system | ||
func LastCap() capability.Cap { | ||
last := capability.CAP_LAST_CAP | ||
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap | ||
if last == capability.Cap(63) { | ||
last = capability.CAP_BLOCK_SUSPEND | ||
} | ||
|
||
return last | ||
} | ||
|
||
// UnitListValid checks strings whether is valid for | ||
// cpuset.cpus and cpuset.mems, duplicates are allowed | ||
// Supported formats: | ||
// 1 | ||
// 0-3 | ||
// 0-2,1,3 | ||
// 0-2,1-3,4 | ||
func UnitListValid(val string) error { | ||
if val == "" { | ||
return nil | ||
} | ||
|
||
split := strings.Split(val, ",") | ||
errInvalidFormat := fmt.Errorf("invalid format: %s", val) | ||
|
||
for _, r := range split { | ||
if !strings.Contains(r, "-") { | ||
_, err := strconv.Atoi(r) | ||
if err != nil { | ||
return errInvalidFormat | ||
} | ||
} else { | ||
split := strings.SplitN(r, "-", 2) | ||
min, err := strconv.Atoi(split[0]) | ||
if err != nil { | ||
return errInvalidFormat | ||
} | ||
max, err := strconv.Atoi(split[1]) | ||
if err != nil { | ||
return errInvalidFormat | ||
} | ||
if max < min { | ||
return errInvalidFormat | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters