Skip to content

Commit be762e7

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

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

command_linux.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,24 @@ func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
2929
if command == "" {
3030
command = DefaultCommand
3131
}
32-
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
32+
cmd := exec.CommandContext(context, command, append(r.args(""), args...)...)
33+
cmd.SysProcAttr = &syscall.SysProcAttr{
34+
Setpgid: r.Setpgid,
35+
}
36+
cmd.Env = filterEnv(os.Environ(), "NOTIFY_SOCKET") // NOTIFY_SOCKET introduces a special behavior in runc but should only be set if invoked from systemd
37+
if r.PdeathSignal != 0 {
38+
cmd.SysProcAttr.Pdeathsig = r.PdeathSignal
39+
}
40+
41+
return cmd
42+
}
43+
44+
func (r *Runc) commandWithCustomLog(context context.Context, logPath string, args ...string) *exec.Cmd {
45+
command := r.Command
46+
if command == "" {
47+
command = DefaultCommand
48+
}
49+
cmd := exec.CommandContext(context, command, append(r.args(logPath), args...)...)
3350
cmd.SysProcAttr = &syscall.SysProcAttr{
3451
Setpgid: r.Setpgid,
3552
}

command_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
2929
if command == "" {
3030
command = DefaultCommand
3131
}
32-
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
32+
cmd := exec.CommandContext(context, command, append(r.args(""), args...)...)
3333
cmd.Env = os.Environ()
3434
return cmd
3535
}

runc.go

Lines changed: 13 additions & 2 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
@@ -249,6 +250,7 @@ func (o *ExecOpts) args() (out []string, err error) {
249250
}
250251
out = append(out, "--pid-file", abs)
251252
}
253+
252254
if len(o.ExtraArgs) > 0 {
253255
out = append(out, o.ExtraArgs...)
254256
}
@@ -280,7 +282,7 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
280282
return err
281283
}
282284
args = append(args, oargs...)
283-
cmd := r.command(context, append(args, id)...)
285+
cmd := r.commandWithCustomLog(context, opts.LogPath, append(args, id)...)
284286
if opts.IO != nil {
285287
opts.Set(cmd)
286288
}
@@ -765,14 +767,23 @@ func (r *Runc) Features(context context.Context) (*features.Features, error) {
765767
return &feat, nil
766768
}
767769

768-
func (r *Runc) args() (out []string) {
770+
func (r *Runc) args(logPath string) (out []string) {
769771
if r.Root != "" {
770772
out = append(out, "--root", r.Root)
771773
}
772774
if r.Debug {
773775
out = append(out, "--debug")
774776
}
777+
775778
if r.Log != "" {
779+
if logPath != "" {
780+
out = append(out, "--log", logPath)
781+
} else {
782+
out = append(out, "--log", r.Log)
783+
}
784+
}
785+
786+
if r.Log == "" && logPath != "" {
776787
out = append(out, "--log", r.Log)
777788
}
778789
if r.LogFormat != none {

0 commit comments

Comments
 (0)