Skip to content

Commit 6cc35bf

Browse files
authored
Merge pull request #1209 from runcom/fix-exec-1.8
[release-1.8] container_exec: fix terminal true process json
2 parents 2e0284f + 765421b commit 6cc35bf

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

oci/oci.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,6 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
415415
os.RemoveAll(logPath)
416416
}()
417417

418-
f, err := ioutil.TempFile("", "exec-sync-process")
419-
if err != nil {
420-
return nil, ExecSyncError{
421-
ExitCode: -1,
422-
Err: err,
423-
}
424-
}
425-
defer os.RemoveAll(f.Name())
426-
427418
var args []string
428419
args = append(args, "-c", c.id)
429420
args = append(args, "-r", r.Path(c))
@@ -439,24 +430,16 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
439430
args = append(args, "-l", logPath)
440431
args = append(args, "--socket-dir-path", ContainerAttachSocketDir)
441432

442-
pspec := c.Spec().Process
443-
pspec.Args = command
444-
processJSON, err := json.Marshal(pspec)
433+
processFile, err := PrepareProcessExec(c, command, false)
445434
if err != nil {
446435
return nil, ExecSyncError{
447436
ExitCode: -1,
448437
Err: err,
449438
}
450439
}
440+
defer os.RemoveAll(processFile.Name())
451441

452-
if err := ioutil.WriteFile(f.Name(), processJSON, 0644); err != nil {
453-
return nil, ExecSyncError{
454-
ExitCode: -1,
455-
Err: err,
456-
}
457-
}
458-
459-
args = append(args, "--exec-process-spec", f.Name())
442+
args = append(args, "--exec-process-spec", processFile.Name())
460443

461444
cmd := exec.Command(r.conmonPath, args...)
462445

@@ -764,3 +747,27 @@ func (r *Runtime) UnpauseContainer(c *Container) error {
764747
_, err := utils.ExecCmd(r.Path(c), "resume", c.id)
765748
return err
766749
}
750+
751+
// PrepareProcessExec returns the path of the process.json used in runc exec -p
752+
// caller is responsible to close the returned *os.File if needed.
753+
func PrepareProcessExec(c *Container, cmd []string, tty bool) (*os.File, error) {
754+
f, err := ioutil.TempFile("", "exec-process-")
755+
if err != nil {
756+
return nil, err
757+
}
758+
759+
pspec := c.Spec().Process
760+
pspec.Args = cmd
761+
if tty {
762+
pspec.Terminal = true
763+
}
764+
processJSON, err := json.Marshal(pspec)
765+
if err != nil {
766+
return nil, err
767+
}
768+
769+
if err := ioutil.WriteFile(f.Name(), processJSON, 0644); err != nil {
770+
return nil, err
771+
}
772+
return f, nil
773+
}

server/container_exec.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package server
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"io"
7-
"io/ioutil"
86
"os"
97
"os/exec"
108
"time"
@@ -55,28 +53,14 @@ func (ss streamService) Exec(containerID string, cmd []string, stdin io.Reader,
5553
return fmt.Errorf("container is not created or running")
5654
}
5755

58-
f, err := ioutil.TempFile("", "exec-process")
56+
processFile, err := oci.PrepareProcessExec(c, cmd, tty)
5957
if err != nil {
6058
return err
6159
}
62-
defer os.RemoveAll(f.Name())
63-
64-
pspec := c.Spec().Process
65-
pspec.Args = cmd
66-
processJSON, err := json.Marshal(pspec)
67-
if err != nil {
68-
return err
69-
}
70-
71-
if err := ioutil.WriteFile(f.Name(), processJSON, 0644); err != nil {
72-
return err
73-
}
60+
defer os.RemoveAll(processFile.Name())
7461

7562
args := []string{"exec"}
76-
if tty {
77-
args = append(args, "-t")
78-
}
79-
args = append(args, "-p", f.Name())
63+
args = append(args, "--process", processFile.Name())
8064
args = append(args, c.ID())
8165
execCmd := exec.Command(ss.runtimeServer.Runtime().Path(c), args...)
8266
var cmdErr error

0 commit comments

Comments
 (0)