Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion command_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ import (
)

func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
return r.commandWithCustomLogFile(context, "", args...)
}

func (r *Runc) commandWithCustomLogFile(context context.Context, logFile 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(logFile), args...)...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: r.Setpgid,
}
Expand Down
6 changes: 5 additions & 1 deletion command_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ import (
)

func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
return r.commandWithCustomLogFile(context, "", args...)
}

func (r *Runc) commandWithCustomLogFile(context context.Context, logFile 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(logFile), args...)...)
cmd.Env = os.Environ()
return cmd
}
12 changes: 9 additions & 3 deletions runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
LogFile string
PidFile string
ConsoleSocket ConsoleSocket
Comment thread
ningmingxiao marked this conversation as resolved.
Comment thread
ningmingxiao marked this conversation as resolved.
Detach bool
Comment thread
ningmingxiao marked this conversation as resolved.
Expand Down Expand Up @@ -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.commandWithCustomLogFile(context, opts.LogFile, append(args, id)...)
if opts.IO != nil {
opts.Set(cmd)
}
Expand Down Expand Up @@ -765,15 +766,20 @@ func (r *Runc) Features(context context.Context) (*features.Features, error) {
return &feat, nil
}

func (r *Runc) args() (out []string) {
func (r *Runc) args(logFile string) (out []string) {
if r.Root != "" {
out = append(out, "--root", r.Root)
}
if r.Debug {
out = append(out, "--debug")
}
if r.Log != "" {
out = append(out, "--log", r.Log)
if logFile == "" {
out = append(out, "--log", r.Log)
}
}
if logFile != "" {
out = append(out, "--log", logFile)
}
if r.LogFormat != none {
out = append(out, "--log-format", string(r.LogFormat))
Expand Down
28 changes: 28 additions & 0 deletions runc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"testing"
Expand Down Expand Up @@ -336,6 +337,33 @@ func TestCreateArgs(t *testing.T) {
}
}

func TestExecWithLogFile(t *testing.T) {
ctx := context.Background()
o := &Runc{}
cmd := o.commandWithCustomLogFile(ctx, "/tmp/exec.log", "version")
if got := strings.Join(cmd.Args[1:], " "); got != "--log /tmp/exec.log version" {
t.Fatalf("expected args %q, got %q", "--log /tmp/exec.log version", got)
}

o = &Runc{Log: "/tmp/runc.log"}
cmd = o.commandWithCustomLogFile(ctx, "/tmp/exec.log", "version")
if got := strings.Join(cmd.Args[1:], " "); got != "--log /tmp/exec.log version" {
t.Fatalf("expected args %q, got %q", "--log /tmp/exec.log version", got)
}

o = &Runc{Log: "/tmp/runc.log"}
cmd = o.commandWithCustomLogFile(ctx, "", "version")
if got := strings.Join(cmd.Args[1:], " "); got != "--log /tmp/runc.log version" {
t.Fatalf("expected args %q, got %q", "--log /tmp/runc.log version", got)
}

o = &Runc{}
cmd = o.commandWithCustomLogFile(ctx, "", "version")
if got := strings.Join(cmd.Args[1:], " "); got != "version" {
t.Fatalf("expected args %q, got %q", "version", got)
}
}

func TestRuncFeatures(t *testing.T) {
ctx := context.Background()
if _, err := exec.LookPath(DefaultCommand); err != nil {
Expand Down
Loading