1
0
mirror of https://github.com/lxc/crio-lxc.git synced 2026-02-05 09:45:04 +01:00

Remove unused code and fix staticcheck errors.

Run staticcheck --unused.whole-program=true -- ./...

Signed-off-by: Ruben Jenster <r.jenster@drachenfels.de>
This commit is contained in:
Ruben Jenster
2021-04-16 21:09:48 +02:00
parent 3b0a651647
commit 65e16988e3
10 changed files with 24 additions and 127 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
@@ -15,10 +16,6 @@ import (
"github.com/opencontainers/runtime-spec/specs-go"
)
const (
allControllers = "+cpuset +cpu +io +memory +hugetlb +pids +rdma"
)
var cgroupRoot string
func detectCgroupRoot() string {
@@ -183,7 +180,7 @@ func configureDeviceController(c *Container) error {
return err
}
default:
return fmt.Errorf("Invalid cgroup2 device - invalid type (allow:%t %s %s:%s %s)", dev.Allow, dev.Type, maj, min, dev.Access)
return fmt.Errorf("invalid cgroup2 device - invalid type (allow:%t %s %s:%s %s)", dev.Allow, dev.Type, maj, min, dev.Access)
}
}
return nil
@@ -354,7 +351,7 @@ func drainCgroup(ctx context.Context, cgroupName string, sig unix.Signal) error
return fmt.Errorf("drain group aborted: %w", ctx.Err())
default:
buf.Reset()
_, err = f.Seek(0, os.SEEK_SET)
_, err = f.Seek(0, io.SeekStart)
if err != nil {
return err
}

View File

@@ -12,15 +12,15 @@ type HookType string
// List of liblxc hook types.
const (
HookPreStart HookType = "pre-start"
HookPreMount = "pre-mount"
HookMount = "mount"
HookAutodev = "autodev"
HookStartHost = "start-host"
HookStart = "start"
HookStop = "stop"
HookPostStop = "post-stop"
HookClone = "clone"
HookDestroy = "destroy"
HookPreMount HookType = "pre-mount"
HookMount HookType = "mount"
HookAutodev HookType = "autodev"
HookStartHost HookType = "start-host"
HookStart HookType = "start"
HookStop HookType = "stop"
HookPostStop HookType = "post-stop"
HookClone HookType = "clone"
HookDestroy HookType = "destroy"
//HookPostStart = "post-start" // not defined by liblxc
)

View File

@@ -81,7 +81,7 @@ func doInit(runtimeDir string, spec *specs.Spec) error {
}
}
val, exist = specki.Getenv(spec.Process.Env, "HOME")
_, exist = specki.Getenv(spec.Process.Env, "HOME")
if !exist {
addEnvHome(spec)
}
@@ -144,5 +144,4 @@ func addEnvHome(spec *specs.Spec) {
}
}
spec.Process.Env = append(spec.Process.Env, "HOME="+spec.Process.Cwd)
return
}

View File

@@ -37,8 +37,7 @@ type app struct {
Timestamp string
}
command string
createHook string
command string
}
var clxc = app{}
@@ -193,13 +192,11 @@ func main() {
println(err.Error())
os.Exit(1)
}
if env != nil {
for key, val := range env {
if err := setEnv(key, val, false); err != nil {
err = fmt.Errorf("failed to set environment variable \"%s=%s\": %w", key, val, err)
println(err.Error())
os.Exit(1)
}
for key, val := range env {
if err := setEnv(key, val, false); err != nil {
err = fmt.Errorf("failed to set environment variable \"%s=%s\": %w", key, val, err)
println(err.Error())
os.Exit(1)
}
}

View File

@@ -69,33 +69,6 @@ func parseSignal(sig string) unix.Signal {
return unix.SignalNum(s)
}
/*
func logEnv(log *zerolog.Log)
if env != nil {
stat, _ := os.Stat(envFile)
if stat != nil && (stat.Mode().Perm()^0640) != 0 {
log.Warn().Str("file", envFile).Stringer("mode", stat.Mode().Perm()).Msgf("environment file should have mode %s", os.FileMode(0640))
}
for key, val := range env {
log.Trace().Str("env", key).Str("val", val).Msg("environment file value")
}
log.Debug().Str("file", envFile).Msg("loaded environment variables from file")
} else {
if os.IsNotExist(envErr) {
log.Warn().Str("file", envFile).Msg("environment file does not exist")
} else {
return errors.Wrapf(envErr, "failed to load env file %s", envFile)
}
}
for _, f := range ctx.Command.Flags {
name := f.Names()[0]
log.Trace().Str("flag", name).Str("val", ctx.String(name)).Msg("flag value")
}
}
*/
// createPidFile atomically creates a pid file for the given pid at the given path
func createPidFile(path string, pid int) error {
tmpDir := filepath.Dir(path)
@@ -120,13 +93,3 @@ func createPidFile(path string, pid int) error {
}
return nil
}
func readPidFile(path string) (int, error) {
// #nosec
data, err := os.ReadFile(path)
if err != nil {
return 0, err
}
s := strings.TrimSpace(string(data))
return strconv.Atoi(s)
}

View File

@@ -162,20 +162,6 @@ func (c *Container) waitNot(ctx context.Context, state specs.ContainerState) err
}
}
func (c *Container) wait(ctx context.Context, state lxc.State) bool {
for {
select {
case <-ctx.Done():
return false
default:
if c.LinuxContainer.State() == state {
return true
}
time.Sleep(time.Millisecond * 100)
}
}
}
// State wraps specs.State and adds runtime specific state.
type State struct {
ContainerState string
@@ -247,8 +233,7 @@ func (c *Container) getContainerInitState() (specs.ContainerState, error) {
// init process died or returned
return specs.StateStopped, nil
}
initCmdline := fmt.Sprintf("/.lxcri/lxcri-init\000")
if string(cmdline) == initCmdline {
if string(cmdline) == "/.lxcri/lxcri-init\000" {
return specs.StateCreated, nil
}
return specs.StateRunning, nil

View File

@@ -68,7 +68,7 @@ func configureMounts(rt *Runtime, c *Container) error {
rt.Log.Trace().Err(err).Str("file", ms.Destination).Str("target", mountDest).Msg("resolve mount destination")
// Check whether the resolved destination of the target link escapes the rootfs.
if !filepath.HasPrefix(mountDest, c.Spec.Root.Path) {
if !strings.HasPrefix(mountDest, c.Spec.Root.Path) {
// refuses mount destinations that escape from rootfs
return fmt.Errorf("resolved mount target path %s escapes from container root %s", mountDest, c.Spec.Root.Path)
}
@@ -155,7 +155,7 @@ func resolvePathRelative(rootfs string, currentPath string, subPath string) (str
// The destination of an absolute link must be prefixed with the rootfs
if filepath.IsAbs(linkDst) {
if filepath.HasPrefix(linkDst, rootfs) {
if strings.HasPrefix(linkDst, rootfs) {
return p, nil
}
return filepath.Join(rootfs, linkDst), nil

View File

@@ -185,9 +185,7 @@ func AllowEssentialDevices(spec *specs.Spec) error {
}
}
for _, perm := range EssentialDevicesAllow {
spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, perm)
}
spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, EssentialDevicesAllow...)
return nil
}
@@ -327,35 +325,6 @@ func InitHook(r io.Reader) (rootfs string, state *specs.State, spec *specs.Spec,
return
}
// BindMount returns a specs.Mount to bind mount src to dest.
// The given mount options opts are merged with the predefined options
// ("bind", "nosuid", "nodev", "relatime")
func BindMount(src string, dest string, opts ...string) specs.Mount {
return specs.Mount{
Source: src, Destination: dest, Type: "bind",
Options: append([]string{"bind", "nosuid", "nodev", "relatime"}, opts...),
}
}
func hasOption(m specs.Mount, opt string) bool {
for _, o := range m.Options {
if o == opt {
return true
}
}
return false
}
// HasOptions returns true if the given Mount has all provided options opts.
func HasOptions(m specs.Mount, opts ...string) bool {
for _, o := range opts {
if !hasOption(m, o) {
return false
}
}
return true
}
// Getenv returns the first matching value from env,
// which has a prefix of key + "=".
func Getenv(env []string, key string) (string, bool) {

View File

@@ -91,7 +91,7 @@ func seccompArchs(seccomp *specs.LinuxSeccomp) ([]string, error) {
}
archs := make([]string, len(seccomp.Architectures))
for _, a := range seccomp.Architectures {
s := strings.ToLower(strings.TrimLeft(string(a), "SCMP_ARCH_"))
s := strings.ToLower(strings.TrimPrefix(string(a), "SCMP_ARCH_"))
if strings.ToLower(nativeArch) == s {
// lxc seccomp code automatically adds syscalls to compat architectures
return []string{nativeArch}, nil

View File

@@ -6,7 +6,6 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"golang.org/x/sys/unix"
)
@@ -60,15 +59,3 @@ func errorf(sfmt string, args ...interface{}) error {
prefix := fmt.Sprintf("[%s:%s:%d] ", bin, filepath.Base(file), line)
return fmt.Errorf(prefix+sfmt, args...)
}
func setenv(env []string, key, val string, overwrite bool) []string {
for i, kv := range env {
if strings.HasPrefix(kv, key+"=") {
if overwrite {
env[i] = key + "=" + val
}
return env
}
}
return append(env, key+"="+val)
}