Skip to content

Commit a85ea60

Browse files
author
Mrunal Patel
authored
Merge pull request #1207 from runcom/fix-exec-termianl
container_exec: fix terminal true process json
2 parents 1f3fbdc + afeab27 commit a85ea60

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
@@ -423,15 +423,6 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
423423
os.RemoveAll(logPath)
424424
}()
425425

426-
f, err := ioutil.TempFile("", "exec-sync-process")
427-
if err != nil {
428-
return nil, ExecSyncError{
429-
ExitCode: -1,
430-
Err: err,
431-
}
432-
}
433-
defer os.RemoveAll(f.Name())
434-
435426
var args []string
436427
args = append(args, "-c", c.id)
437428
args = append(args, "-r", r.Path(c))
@@ -447,24 +438,16 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
447438
args = append(args, "-l", logPath)
448439
args = append(args, "--socket-dir-path", ContainerAttachSocketDir)
449440

450-
pspec := c.Spec().Process
451-
pspec.Args = command
452-
processJSON, err := json.Marshal(pspec)
441+
processFile, err := PrepareProcessExec(c, command, false)
453442
if err != nil {
454443
return nil, ExecSyncError{
455444
ExitCode: -1,
456445
Err: err,
457446
}
458447
}
448+
defer os.RemoveAll(processFile.Name())
459449

460-
if err := ioutil.WriteFile(f.Name(), processJSON, 0644); err != nil {
461-
return nil, ExecSyncError{
462-
ExitCode: -1,
463-
Err: err,
464-
}
465-
}
466-
467-
args = append(args, "--exec-process-spec", f.Name())
450+
args = append(args, "--exec-process-spec", processFile.Name())
468451

469452
cmd := exec.Command(r.conmonPath, args...)
470453

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

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)