From 9238bac1e3232ebefb4fd42416d16265a13e54b2 Mon Sep 17 00:00:00 2001 From: ningmingxiao Date: Wed, 11 Jun 2025 12:45:57 +0800 Subject: [PATCH] exec:support to set custom log path Signed-off-by: ningmingxiao --- .github/workflows/ci.yml | 2 +- command_linux.go | 6 +++++- command_other.go | 6 +++++- runc.go | 12 +++++++++--- runc_test.go | 27 +++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fcaf3b6..db93030 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,7 +34,7 @@ jobs: path: src/github.com/containerd/go-runc fetch-depth: 25 - - uses: containerd/project-checks@v1.1.0 + - uses: containerd/project-checks@d7751f3c375b8fe4a84c02a068184ee4c1f59bc4 # v1.2.2 with: working-directory: src/github.com/containerd/go-runc diff --git a/command_linux.go b/command_linux.go index 8a30f67..672c6e2 100644 --- a/command_linux.go +++ b/command_linux.go @@ -25,11 +25,15 @@ import ( ) func (r *Runc) command(context context.Context, args ...string) *exec.Cmd { + return r.commandWithCustomLogPath(context, "", args...) +} + +func (r *Runc) commandWithCustomLogPath(context context.Context, logPath string, args ...string) *exec.Cmd { command := r.Command if command == "" { command = DefaultCommand } - cmd := exec.CommandContext(context, command, append(r.args(), args...)...) + cmd := exec.CommandContext(context, command, append(r.args(logPath), args...)...) cmd.SysProcAttr = &syscall.SysProcAttr{ Setpgid: r.Setpgid, } diff --git a/command_other.go b/command_other.go index a4adbe1..3b0fb41 100644 --- a/command_other.go +++ b/command_other.go @@ -25,11 +25,15 @@ import ( ) func (r *Runc) command(context context.Context, args ...string) *exec.Cmd { + return r.commandWithCustomLogPath(context, "", args...) +} + +func (r *Runc) commandWithCustomLogPath(context context.Context, logPath string, args ...string) *exec.Cmd { command := r.Command if command == "" { command = DefaultCommand } - cmd := exec.CommandContext(context, command, append(r.args(), args...)...) + cmd := exec.CommandContext(context, command, append(r.args(logPath), args...)...) cmd.Env = os.Environ() return cmd } diff --git a/runc.go b/runc.go index 61646df..57a4451 100644 --- a/runc.go +++ b/runc.go @@ -228,6 +228,7 @@ func (r *Runc) Start(context context.Context, id string) error { // ExecOpts holds optional settings when starting an exec process with runc type ExecOpts struct { IO + LogPath string PidFile string ConsoleSocket ConsoleSocket Detach bool @@ -280,7 +281,7 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts return err } args = append(args, oargs...) - cmd := r.command(context, append(args, id)...) + cmd := r.commandWithCustomLogPath(context, opts.LogPath, append(args, id)...) if opts.IO != nil { opts.Set(cmd) } @@ -765,7 +766,7 @@ func (r *Runc) Features(context context.Context) (*features.Features, error) { return &feat, nil } -func (r *Runc) args() (out []string) { +func (r *Runc) args(logPath string) (out []string) { if r.Root != "" { out = append(out, "--root", r.Root) } @@ -773,7 +774,12 @@ func (r *Runc) args() (out []string) { out = append(out, "--debug") } if r.Log != "" { - out = append(out, "--log", r.Log) + if logPath == "" { + out = append(out, "--log", r.Log) + } + } + if logPath != "" { + out = append(out, "--log", logPath) } if r.LogFormat != none { out = append(out, "--log-format", string(r.LogFormat)) diff --git a/runc_test.go b/runc_test.go index 610cffd..8d2f7b0 100644 --- a/runc_test.go +++ b/runc_test.go @@ -21,6 +21,7 @@ import ( "errors" "os" "os/exec" + "strings" "sync" "syscall" "testing" @@ -336,6 +337,32 @@ func TestCreateArgs(t *testing.T) { } } +func TestExecWithLogPath(t *testing.T) { + o := &Runc{} + out := o.args("/tmp/exec.log") + if strings.Join(out, " ") != "--log /tmp/exec.log" { + t.Fatalf("should be :--log /tmp/exec.log but got :%s", strings.Join(out, " ")) + } + + o = &Runc{Log: "/tmp/runc.log"} + out = o.args("/tmp/exec.log") + if strings.Join(out, " ") != "--log /tmp/exec.log" { + t.Fatalf("should be :--log /tmp/exec.log but got :%s", strings.Join(out, " ")) + } + + o = &Runc{Log: "/tmp/runc.log"} + out = o.args("") + if strings.Join(out, " ") != "--log /tmp/runc.log" { + t.Fatalf("should be :--log /tmp/runc.log but got :%s", strings.Join(out, " ")) + } + + o = &Runc{} + out = o.args("") + if len(out) != 0 { + t.Fatalf("len(out) should be 0 but got :%d", len(out)) + } +} + func TestRuncFeatures(t *testing.T) { ctx := context.Background() if _, err := exec.LookPath(DefaultCommand); err != nil {