Skip to content

Commit 9238bac

Browse files
committed
exec:support to set custom log path
Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn>
1 parent 4a6db15 commit 9238bac

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
path: src/github.com/containerd/go-runc
3535
fetch-depth: 25
3636

37-
- uses: containerd/project-checks@v1.1.0
37+
- uses: containerd/project-checks@d7751f3c375b8fe4a84c02a068184ee4c1f59bc4 # v1.2.2
3838
with:
3939
working-directory: src/github.com/containerd/go-runc
4040

command_linux.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ import (
2525
)
2626

2727
func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
28+
return r.commandWithCustomLogPath(context, "", args...)
29+
}
30+
31+
func (r *Runc) commandWithCustomLogPath(context context.Context, logPath string, args ...string) *exec.Cmd {
2832
command := r.Command
2933
if command == "" {
3034
command = DefaultCommand
3135
}
32-
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
36+
cmd := exec.CommandContext(context, command, append(r.args(logPath), args...)...)
3337
cmd.SysProcAttr = &syscall.SysProcAttr{
3438
Setpgid: r.Setpgid,
3539
}

command_other.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ import (
2525
)
2626

2727
func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
28+
return r.commandWithCustomLogPath(context, "", args...)
29+
}
30+
31+
func (r *Runc) commandWithCustomLogPath(context context.Context, logPath string, args ...string) *exec.Cmd {
2832
command := r.Command
2933
if command == "" {
3034
command = DefaultCommand
3135
}
32-
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
36+
cmd := exec.CommandContext(context, command, append(r.args(logPath), args...)...)
3337
cmd.Env = os.Environ()
3438
return cmd
3539
}

runc.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ func (r *Runc) Start(context context.Context, id string) error {
228228
// ExecOpts holds optional settings when starting an exec process with runc
229229
type ExecOpts struct {
230230
IO
231+
LogPath string
231232
PidFile string
232233
ConsoleSocket ConsoleSocket
233234
Detach bool
@@ -280,7 +281,7 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
280281
return err
281282
}
282283
args = append(args, oargs...)
283-
cmd := r.command(context, append(args, id)...)
284+
cmd := r.commandWithCustomLogPath(context, opts.LogPath, append(args, id)...)
284285
if opts.IO != nil {
285286
opts.Set(cmd)
286287
}
@@ -765,15 +766,20 @@ func (r *Runc) Features(context context.Context) (*features.Features, error) {
765766
return &feat, nil
766767
}
767768

768-
func (r *Runc) args() (out []string) {
769+
func (r *Runc) args(logPath string) (out []string) {
769770
if r.Root != "" {
770771
out = append(out, "--root", r.Root)
771772
}
772773
if r.Debug {
773774
out = append(out, "--debug")
774775
}
775776
if r.Log != "" {
776-
out = append(out, "--log", r.Log)
777+
if logPath == "" {
778+
out = append(out, "--log", r.Log)
779+
}
780+
}
781+
if logPath != "" {
782+
out = append(out, "--log", logPath)
777783
}
778784
if r.LogFormat != none {
779785
out = append(out, "--log-format", string(r.LogFormat))

runc_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"os"
2323
"os/exec"
24+
"strings"
2425
"sync"
2526
"syscall"
2627
"testing"
@@ -336,6 +337,32 @@ func TestCreateArgs(t *testing.T) {
336337
}
337338
}
338339

340+
func TestExecWithLogPath(t *testing.T) {
341+
o := &Runc{}
342+
out := o.args("/tmp/exec.log")
343+
if strings.Join(out, " ") != "--log /tmp/exec.log" {
344+
t.Fatalf("should be :--log /tmp/exec.log but got :%s", strings.Join(out, " "))
345+
}
346+
347+
o = &Runc{Log: "/tmp/runc.log"}
348+
out = o.args("/tmp/exec.log")
349+
if strings.Join(out, " ") != "--log /tmp/exec.log" {
350+
t.Fatalf("should be :--log /tmp/exec.log but got :%s", strings.Join(out, " "))
351+
}
352+
353+
o = &Runc{Log: "/tmp/runc.log"}
354+
out = o.args("")
355+
if strings.Join(out, " ") != "--log /tmp/runc.log" {
356+
t.Fatalf("should be :--log /tmp/runc.log but got :%s", strings.Join(out, " "))
357+
}
358+
359+
o = &Runc{}
360+
out = o.args("")
361+
if len(out) != 0 {
362+
t.Fatalf("len(out) should be 0 but got :%d", len(out))
363+
}
364+
}
365+
339366
func TestRuncFeatures(t *testing.T) {
340367
ctx := context.Background()
341368
if _, err := exec.LookPath(DefaultCommand); err != nil {

0 commit comments

Comments
 (0)