2020-05-04 20:46:48 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package fs2
|
|
|
|
|
|
|
|
import (
|
2020-08-10 17:43:49 +00:00
|
|
|
stdErrors "errors"
|
|
|
|
"os"
|
2020-05-04 20:46:48 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/pkg/errors"
|
2020-08-10 17:43:49 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2020-05-04 20:46:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setFreezer(dirPath string, state configs.FreezerState) error {
|
2020-08-10 17:43:49 +00:00
|
|
|
if err := supportsFreezer(dirPath); err != nil {
|
|
|
|
// We can ignore this request as long as the user didn't ask us to
|
|
|
|
// freeze the container (since without the freezer cgroup, that's a
|
|
|
|
// no-op).
|
|
|
|
if state == configs.Undefined || state == configs.Thawed {
|
2021-04-14 18:11:13 +00:00
|
|
|
return nil
|
2020-08-10 17:43:49 +00:00
|
|
|
}
|
|
|
|
return errors.Wrap(err, "freezer not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
var stateStr string
|
2020-05-04 20:46:48 +00:00
|
|
|
switch state {
|
|
|
|
case configs.Undefined:
|
|
|
|
return nil
|
|
|
|
case configs.Frozen:
|
2020-08-10 17:43:49 +00:00
|
|
|
stateStr = "1"
|
2020-05-04 20:46:48 +00:00
|
|
|
case configs.Thawed:
|
2020-08-10 17:43:49 +00:00
|
|
|
stateStr = "0"
|
2020-05-04 20:46:48 +00:00
|
|
|
default:
|
2020-08-10 17:43:49 +00:00
|
|
|
return errors.Errorf("invalid freezer state %q requested", state)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fscommon.WriteFile(dirPath, "cgroup.freeze", stateStr); err != nil {
|
|
|
|
return err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
// Confirm that the cgroup did actually change states.
|
|
|
|
if actualState, err := getFreezer(dirPath); err != nil {
|
|
|
|
return err
|
|
|
|
} else if actualState != state {
|
|
|
|
return errors.Errorf(`expected "cgroup.freeze" to be in state %q but was in %q`, state, actualState)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
return nil
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func supportsFreezer(dirPath string) error {
|
|
|
|
_, err := fscommon.ReadFile(dirPath, "cgroup.freeze")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func getFreezer(dirPath string) (configs.FreezerState, error) {
|
|
|
|
state, err := fscommon.ReadFile(dirPath, "cgroup.freeze")
|
2020-05-04 20:46:48 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
// If the kernel is too old, then we just treat the freezer as being in
|
|
|
|
// an "undefined" state.
|
|
|
|
if os.IsNotExist(err) || stdErrors.Is(err, unix.ENODEV) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return configs.Undefined, err
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
switch strings.TrimSpace(state) {
|
|
|
|
case "0":
|
|
|
|
return configs.Thawed, nil
|
|
|
|
case "1":
|
|
|
|
return configs.Frozen, nil
|
|
|
|
default:
|
|
|
|
return configs.Undefined, errors.Errorf(`unknown "cgroup.freeze" state: %q`, state)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
}
|