1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package v2
18
19import (
20	"io/ioutil"
21	"path/filepath"
22	"strings"
23)
24
25// State is a type that represents the state of the current cgroup
26type State string
27
28const (
29	Unknown State = ""
30	Thawed  State = "thawed"
31	Frozen  State = "frozen"
32	Deleted State = "deleted"
33
34	cgroupFreeze = "cgroup.freeze"
35)
36
37func (s State) Values() []Value {
38	v := Value{
39		filename: cgroupFreeze,
40	}
41	switch s {
42	case Frozen:
43		v.value = "1"
44	case Thawed:
45		v.value = "0"
46	}
47	return []Value{
48		v,
49	}
50}
51
52func fetchState(path string) (State, error) {
53	current, err := ioutil.ReadFile(filepath.Join(path, cgroupFreeze))
54	if err != nil {
55		return Unknown, err
56	}
57	switch strings.TrimSpace(string(current)) {
58	case "1":
59		return Frozen, nil
60	case "0":
61		return Thawed, nil
62	default:
63		return Unknown, nil
64	}
65}
66