diff --git a/contrib/cmd/recvtty/recvtty.go b/contrib/cmd/recvtty/recvtty.go index 00b30e1c3..99203846b 100644 --- a/contrib/cmd/recvtty/recvtty.go +++ b/contrib/cmd/recvtty/recvtty.go @@ -187,7 +187,7 @@ func main() { v = append(v, version) } if gitCommit != "" { - v = append(v, fmt.Sprintf("commit: %s", gitCommit)) + v = append(v, "commit: "+gitCommit) } app.Version = strings.Join(v, "\n") diff --git a/libcontainer/apparmor/apparmor.go b/libcontainer/apparmor/apparmor.go index a12caaec1..7ae9cf802 100644 --- a/libcontainer/apparmor/apparmor.go +++ b/libcontainer/apparmor/apparmor.go @@ -23,9 +23,7 @@ func IsEnabled() bool { func setProcAttr(attr, value string) error { // Under AppArmor you can only change your own attr, so use /proc/self/ // instead of /proc// like libapparmor does - path := fmt.Sprintf("/proc/self/attr/%s", attr) - - f, err := os.OpenFile(path, os.O_WRONLY, 0) + f, err := os.OpenFile("/proc/self/attr/"+attr, os.O_WRONLY, 0) if err != nil { return err } @@ -35,14 +33,13 @@ func setProcAttr(attr, value string) error { return err } - _, err = fmt.Fprintf(f, "%s", value) + _, err = f.WriteString(value) return err } // changeOnExec reimplements aa_change_onexec from libapparmor in Go func changeOnExec(name string) error { - value := "exec " + name - if err := setProcAttr("exec", value); err != nil { + if err := setProcAttr("exec", "exec "+name); err != nil { return fmt.Errorf("apparmor failed to apply profile: %s", err) } return nil diff --git a/libcontainer/cgroups/ebpf/devicefilter/devicefilter.go b/libcontainer/cgroups/ebpf/devicefilter/devicefilter.go index b593eb43e..ac77652a7 100644 --- a/libcontainer/cgroups/ebpf/devicefilter/devicefilter.go +++ b/libcontainer/cgroups/ebpf/devicefilter/devicefilter.go @@ -7,8 +7,8 @@ package devicefilter import ( - "fmt" "math" + "strconv" "github.com/cilium/ebpf/asm" "github.com/opencontainers/runc/libcontainer/configs" @@ -114,9 +114,11 @@ func (p *program) appendDevice(dev *configs.DeviceRule) error { // If the access is rwm, skip the check. hasAccess := bpfAccess != (unix.BPF_DEVCG_ACC_READ | unix.BPF_DEVCG_ACC_WRITE | unix.BPF_DEVCG_ACC_MKNOD) - blockSym := fmt.Sprintf("block-%d", p.blockID) - nextBlockSym := fmt.Sprintf("block-%d", p.blockID+1) - prevBlockLastIdx := len(p.insts) - 1 + var ( + blockSym = "block-" + strconv.Itoa(p.blockID) + nextBlockSym = "block-" + strconv.Itoa(p.blockID+1) + prevBlockLastIdx = len(p.insts) - 1 + ) if hasType { p.insts = append(p.insts, // if (R2 != bpfType) goto next @@ -158,7 +160,7 @@ func (p *program) finalize() (asm.Instructions, error) { // acceptBlock with asm.Return() is already inserted return p.insts, nil } - blockSym := fmt.Sprintf("block-%d", p.blockID) + blockSym := "block-" + strconv.Itoa(p.blockID) p.insts = append(p.insts, // R0 <- 0 asm.Mov.Imm32(asm.R0, 0).Sym(blockSym), diff --git a/libcontainer/cgroups/systemd/common.go b/libcontainer/cgroups/systemd/common.go index b567f3e1f..4e004af4a 100644 --- a/libcontainer/cgroups/systemd/common.go +++ b/libcontainer/cgroups/systemd/common.go @@ -307,7 +307,7 @@ func newProp(name string, units interface{}) systemdDbus.Property { func getUnitName(c *configs.Cgroup) string { // by default, we create a scope unless the user explicitly asks for a slice. if !strings.HasSuffix(c.Name, ".slice") { - return fmt.Sprintf("%s-%s.scope", c.ScopePrefix, c.Name) + return c.ScopePrefix + "-" + c.Name + ".scope" } return c.Name } diff --git a/libcontainer/configs/namespaces_linux.go b/libcontainer/configs/namespaces_linux.go index 1bbaef9bd..d52d6fcd1 100644 --- a/libcontainer/configs/namespaces_linux.go +++ b/libcontainer/configs/namespaces_linux.go @@ -56,7 +56,7 @@ func IsNamespaceSupported(ns NamespaceType) bool { if nsFile == "" { return false } - _, err := os.Stat(fmt.Sprintf("/proc/self/ns/%s", nsFile)) + _, err := os.Stat("/proc/self/ns/" + nsFile) // a namespace is supported if it exists and we have permissions to read it supported = err == nil supportedNamespaces[ns] = supported diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index 3531487c6..8a4470cd4 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -14,6 +14,7 @@ import ( "os/exec" "path/filepath" "reflect" + "strconv" "strings" "sync" "time" @@ -453,7 +454,7 @@ func (c *linuxContainer) includeExecFifo(cmd *exec.Cmd) error { cmd.ExtraFiles = append(cmd.ExtraFiles, os.NewFile(uintptr(fifoFd), fifoName)) cmd.Env = append(cmd.Env, - fmt.Sprintf("_LIBCONTAINER_FIFOFD=%d", stdioFdCount+len(cmd.ExtraFiles)-1)) + "_LIBCONTAINER_FIFOFD="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1)) return nil } @@ -496,24 +497,24 @@ func (c *linuxContainer) commandTemplate(p *Process, childInitPipe *os.File, chi if cmd.SysProcAttr == nil { cmd.SysProcAttr = &unix.SysProcAttr{} } - cmd.Env = append(cmd.Env, fmt.Sprintf("GOMAXPROCS=%s", os.Getenv("GOMAXPROCS"))) + cmd.Env = append(cmd.Env, "GOMAXPROCS="+os.Getenv("GOMAXPROCS")) cmd.ExtraFiles = append(cmd.ExtraFiles, p.ExtraFiles...) if p.ConsoleSocket != nil { cmd.ExtraFiles = append(cmd.ExtraFiles, p.ConsoleSocket) cmd.Env = append(cmd.Env, - fmt.Sprintf("_LIBCONTAINER_CONSOLE=%d", stdioFdCount+len(cmd.ExtraFiles)-1), + "_LIBCONTAINER_CONSOLE="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1), ) } cmd.ExtraFiles = append(cmd.ExtraFiles, childInitPipe) cmd.Env = append(cmd.Env, - fmt.Sprintf("_LIBCONTAINER_INITPIPE=%d", stdioFdCount+len(cmd.ExtraFiles)-1), - fmt.Sprintf("_LIBCONTAINER_STATEDIR=%s", c.root), + "_LIBCONTAINER_INITPIPE="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1), + "_LIBCONTAINER_STATEDIR="+c.root, ) cmd.ExtraFiles = append(cmd.ExtraFiles, childLogPipe) cmd.Env = append(cmd.Env, - fmt.Sprintf("_LIBCONTAINER_LOGPIPE=%d", stdioFdCount+len(cmd.ExtraFiles)-1), - fmt.Sprintf("_LIBCONTAINER_LOGLEVEL=%s", p.LogLevel), + "_LIBCONTAINER_LOGPIPE="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1), + "_LIBCONTAINER_LOGLEVEL="+p.LogLevel, ) // NOTE: when running a container with no PID namespace and the parent process spawning the container is @@ -2033,7 +2034,7 @@ func (c *linuxContainer) bootstrapData(cloneFlags uintptr, nsMaps map[configs.Na // write oom_score_adj r.AddData(&Bytemsg{ Type: OomScoreAdjAttr, - Value: []byte(fmt.Sprintf("%d", *c.config.OomScoreAdj)), + Value: []byte(strconv.Itoa(*c.config.OomScoreAdj)), }) } diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index 3fdf04be8..10ce99391 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -1244,7 +1244,7 @@ func TestHook(t *testing.T) { config := newTemplateConfig(rootfs) expectedBundle := bundle - config.Labels = append(config.Labels, fmt.Sprintf("bundle=%s", expectedBundle)) + config.Labels = append(config.Labels, "bundle="+expectedBundle) getRootfsFromBundle := func(bundle string) (string, error) { f, err := os.Open(filepath.Join(bundle, "config.json")) diff --git a/libcontainer/setns_init_linux.go b/libcontainer/setns_init_linux.go index 0b9cd8852..6b1e9a6e9 100644 --- a/libcontainer/setns_init_linux.go +++ b/libcontainer/setns_init_linux.go @@ -3,7 +3,6 @@ package libcontainer import ( - "fmt" "os" "runtime" @@ -25,7 +24,7 @@ type linuxSetnsInit struct { } func (l *linuxSetnsInit) getSessionRingName() string { - return fmt.Sprintf("_ses.%s", l.config.ContainerId) + return "_ses." + l.config.ContainerId } func (l *linuxSetnsInit) Init() error { diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index ce74bcf2d..141fe84f6 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -224,14 +224,14 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) { } labels := []string{} for k, v := range spec.Annotations { - labels = append(labels, fmt.Sprintf("%s=%s", k, v)) + labels = append(labels, k+"="+v) } config := &configs.Config{ Rootfs: rootfsPath, NoPivotRoot: opts.NoPivotRoot, Readonlyfs: spec.Root.Readonly, Hostname: spec.Hostname, - Labels: append(labels, fmt.Sprintf("bundle=%s", cwd)), + Labels: append(labels, "bundle="+cwd), NoNewKeyring: opts.NoNewKeyring, RootlessEUID: opts.RootlessEUID, RootlessCgroups: opts.RootlessCgroups, diff --git a/libcontainer/standard_init_linux.go b/libcontainer/standard_init_linux.go index b20ce1485..7ec506c46 100644 --- a/libcontainer/standard_init_linux.go +++ b/libcontainer/standard_init_linux.go @@ -3,10 +3,10 @@ package libcontainer import ( - "fmt" "os" "os/exec" "runtime" + "strconv" "github.com/opencontainers/runc/libcontainer/apparmor" "github.com/opencontainers/runc/libcontainer/configs" @@ -40,7 +40,7 @@ func (l *linuxStandardInit) getSessionRingParams() (string, uint32, uint32) { // Create a unique per session container name that we can join in setns; // However, other containers can also join it. - return fmt.Sprintf("_ses.%s", l.config.ContainerId), 0xffffffff, newperms + return "_ses." + l.config.ContainerId, 0xffffffff, newperms } func (l *linuxStandardInit) Init() error { @@ -185,7 +185,7 @@ func (l *linuxStandardInit) Init() error { // user process. We open it through /proc/self/fd/$fd, because the fd that // was given to us was an O_PATH fd to the fifo itself. Linux allows us to // re-open an O_PATH fd through /proc. - fd, err := unix.Open(fmt.Sprintf("/proc/self/fd/%d", l.fifoFd), unix.O_WRONLY|unix.O_CLOEXEC, 0) + fd, err := unix.Open("/proc/self/fd/"+strconv.Itoa(l.fifoFd), unix.O_WRONLY|unix.O_CLOEXEC, 0) if err != nil { return newSystemErrorWithCause(err, "open exec fifo") } diff --git a/libcontainer/system/proc.go b/libcontainer/system/proc.go index 79232a437..0dbff54d6 100644 --- a/libcontainer/system/proc.go +++ b/libcontainer/system/proc.go @@ -78,7 +78,7 @@ func GetProcessStartTime(pid int) (string, error) { if err != nil { return "", err } - return fmt.Sprintf("%d", stat.StartTime), nil + return strconv.FormatUint(stat.StartTime, 10), nil } func parseStat(data string) (stat Stat_t, err error) { diff --git a/libcontainer/user/lookup_windows.go b/libcontainer/user/lookup_windows.go index 65cd40e92..f19333e61 100644 --- a/libcontainer/user/lookup_windows.go +++ b/libcontainer/user/lookup_windows.go @@ -3,8 +3,8 @@ package user import ( - "fmt" "os/user" + "strconv" ) func lookupUser(username string) (User, error) { @@ -16,7 +16,7 @@ func lookupUser(username string) (User, error) { } func lookupUid(uid int) (User, error) { - u, err := user.LookupId(fmt.Sprintf("%d", uid)) + u, err := user.LookupId(strconv.Itoa(uid)) if err != nil { return User{}, err } @@ -32,7 +32,7 @@ func lookupGroup(groupname string) (Group, error) { } func lookupGid(gid int) (Group, error) { - g, err := user.LookupGroupId(fmt.Sprintf("%d", gid)) + g, err := user.LookupGroupId(strconv.Itoa(gid)) if err != nil { return Group{}, err } diff --git a/libcontainer/utils/utils_test.go b/libcontainer/utils/utils_test.go index 395eedcf6..805f8b361 100644 --- a/libcontainer/utils/utils_test.go +++ b/libcontainer/utils/utils_test.go @@ -2,7 +2,6 @@ package utils import ( "bytes" - "fmt" "os" "path/filepath" "testing" @@ -44,7 +43,7 @@ func TestResolveRootfs(t *testing.T) { if err != nil { t.Fatal(err) } - if path != fmt.Sprintf("%s/%s", pwd, "rootfs") { + if path != pwd+"/rootfs" { t.Errorf("expected rootfs to be abs and was %s", path) } } diff --git a/main.go b/main.go index 642f7a4f9..3c4a58216 100644 --- a/main.go +++ b/main.go @@ -60,10 +60,10 @@ func main() { v = append(v, version) } if gitCommit != "" { - v = append(v, fmt.Sprintf("commit: %s", gitCommit)) + v = append(v, "commit: "+gitCommit) } - v = append(v, fmt.Sprintf("spec: %s", specs.Version)) - v = append(v, fmt.Sprintf("go: %s", runtime.Version())) + v = append(v, "spec: "+specs.Version) + v = append(v, "go: "+runtime.Version()) if seccomp.IsEnabled() { major, minor, micro := seccomp.Version() v = append(v, fmt.Sprintf("libseccomp: %d.%d.%d", major, minor, micro)) diff --git a/notify_socket.go b/notify_socket.go index f313a7a68..29f0d9260 100644 --- a/notify_socket.go +++ b/notify_socket.go @@ -4,7 +4,6 @@ package main import ( "bytes" - "fmt" "net" "os" "path" @@ -54,7 +53,7 @@ func (s *notifySocket) setupSpec(context *cli.Context, spec *specs.Spec) error { Options: []string{"bind", "nosuid", "noexec", "nodev", "ro"}, } spec.Mounts = append(spec.Mounts, mount) - spec.Process.Env = append(spec.Process.Env, fmt.Sprintf("NOTIFY_SOCKET=%s", pathInContainer)) + spec.Process.Env = append(spec.Process.Env, "NOTIFY_SOCKET="+pathInContainer) return nil } @@ -162,8 +161,8 @@ func (n *notifySocket) run(pid1 int) error { } // now we can inform systemd to use pid1 as the pid to monitor - newPid := fmt.Sprintf("MAINPID=%d\n", pid1) - client.Write([]byte(newPid)) + newPid := "MAINPID=" + strconv.Itoa(pid1) + client.Write([]byte(newPid + "\n")) return nil } } diff --git a/utils_linux.go b/utils_linux.go index c24ee8d6d..fe9f25ae1 100644 --- a/utils_linux.go +++ b/utils_linux.go @@ -214,13 +214,13 @@ func createPidFile(path string, process *libcontainer.Process) error { } var ( tmpDir = filepath.Dir(path) - tmpName = filepath.Join(tmpDir, fmt.Sprintf(".%s", filepath.Base(path))) + tmpName = filepath.Join(tmpDir, "."+filepath.Base(path)) ) f, err := os.OpenFile(tmpName, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666) if err != nil { return err } - _, err = fmt.Fprintf(f, "%d", pid) + _, err = f.WriteString(strconv.Itoa(pid)) f.Close() if err != nil { return err @@ -284,12 +284,12 @@ func (r *runner) run(config *specs.Process) (int, error) { return -1, err } if len(r.listenFDs) > 0 { - process.Env = append(process.Env, fmt.Sprintf("LISTEN_FDS=%d", len(r.listenFDs)), "LISTEN_PID=1") + process.Env = append(process.Env, "LISTEN_FDS="+strconv.Itoa(len(r.listenFDs)), "LISTEN_PID=1") process.ExtraFiles = append(process.ExtraFiles, r.listenFDs...) } baseFd := 3 + len(process.ExtraFiles) for i := baseFd; i < baseFd+r.preserveFDs; i++ { - _, err = os.Stat(fmt.Sprintf("/proc/self/fd/%d", i)) + _, err = os.Stat("/proc/self/fd/" + strconv.Itoa(i)) if err != nil { return -1, errors.Wrapf(err, "please check that preserved-fd %d (of %d) is present", i-baseFd, r.preserveFDs) }