k3s/vendor/github.com/opencontainers/runc/libcontainer/cgroups/fscommon/fscommon.go

52 lines
1.1 KiB
Go
Raw Normal View History

// +build linux
package fscommon
import (
"bytes"
2020-08-10 17:43:49 +00:00
"os"
"github.com/pkg/errors"
2020-08-10 17:43:49 +00:00
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
// WriteFile writes data to a cgroup file in dir.
// It is supposed to be used for cgroup files only.
func WriteFile(dir, file, data string) error {
fd, err := OpenFile(dir, file, unix.O_WRONLY)
if err != nil {
return err
}
defer fd.Close()
if err := retryingWriteFile(fd, data); err != nil {
return errors.Wrapf(err, "failed to write %q", data)
}
return nil
}
// ReadFile reads data from a cgroup file in dir.
// It is supposed to be used for cgroup files only.
func ReadFile(dir, file string) (string, error) {
fd, err := OpenFile(dir, file, unix.O_RDONLY)
if err != nil {
return "", err
}
defer fd.Close()
var buf bytes.Buffer
_, err = buf.ReadFrom(fd)
return buf.String(), err
}
2020-08-10 17:43:49 +00:00
func retryingWriteFile(fd *os.File, data string) error {
2020-08-10 17:43:49 +00:00
for {
_, err := fd.Write([]byte(data))
2020-08-10 17:43:49 +00:00
if errors.Is(err, unix.EINTR) {
logrus.Infof("interrupted while writing %s to %s", data, fd.Name())
2020-08-10 17:43:49 +00:00
continue
}
return err
}
}