Skip to content

Commit cad6fb9

Browse files
committed
Fix cases where ctr wouldn't properly restore the terminal termios
Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
1 parent 54c213e commit cad6fb9

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

ctr/container.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ var startCommand = cli.Command{
145145
fatal(err.Error(), 1)
146146
}
147147
var (
148-
tty bool
149-
c = getClient(context)
150-
r = &types.CreateContainerRequest{
148+
restoreAndCloseStdin func()
149+
tty bool
150+
c = getClient(context)
151+
r = &types.CreateContainerRequest{
151152
Id: id,
152153
BundlePath: bpath,
153154
Checkpoint: context.String("checkpoint"),
@@ -157,6 +158,15 @@ var startCommand = cli.Command{
157158
Labels: context.StringSlice("label"),
158159
}
159160
)
161+
restoreAndCloseStdin = func() {
162+
if state != nil {
163+
term.RestoreTerminal(os.Stdin.Fd(), state)
164+
}
165+
if stdin != nil {
166+
stdin.Close()
167+
}
168+
}
169+
defer restoreAndCloseStdin()
160170
if context.Bool("attach") {
161171
mkterm, err := readTermSetting(bpath)
162172
if err != nil {
@@ -182,12 +192,6 @@ var startCommand = cli.Command{
182192
fatal(err.Error(), 1)
183193
}
184194
if context.Bool("attach") {
185-
restoreAndCloseStdin := func() {
186-
if state != nil {
187-
term.RestoreTerminal(os.Stdin.Fd(), state)
188-
}
189-
stdin.Close()
190-
}
191195
go func() {
192196
io.Copy(stdin, os.Stdin)
193197
if _, err := c.UpdateProcess(netcontext.Background(), &types.UpdateProcessRequest{
@@ -422,6 +426,8 @@ var execCommand = cli.Command{
422426
},
423427
},
424428
Action: func(context *cli.Context) {
429+
var restoreAndCloseStdin func()
430+
425431
p := &types.AddProcessRequest{
426432
Id: context.String("id"),
427433
Pid: context.String("pid"),
@@ -441,6 +447,15 @@ var execCommand = cli.Command{
441447
p.Stdin = s.stdin
442448
p.Stdout = s.stdout
443449
p.Stderr = s.stderr
450+
restoreAndCloseStdin = func() {
451+
if state != nil {
452+
term.RestoreTerminal(os.Stdin.Fd(), state)
453+
}
454+
if stdin != nil {
455+
stdin.Close()
456+
}
457+
}
458+
defer restoreAndCloseStdin()
444459
if context.Bool("attach") {
445460
if context.Bool("tty") {
446461
s, err := term.SetRawTerminal(os.Stdin.Fd())
@@ -462,12 +477,6 @@ var execCommand = cli.Command{
462477
fatal(err.Error(), 1)
463478
}
464479
if context.Bool("attach") {
465-
restoreAndCloseStdin := func() {
466-
if state != nil {
467-
term.RestoreTerminal(os.Stdin.Fd(), state)
468-
}
469-
stdin.Close()
470-
}
471480
go func() {
472481
io.Copy(stdin, os.Stdin)
473482
if _, err := c.UpdateProcess(netcontext.Background(), &types.UpdateProcessRequest{

ctr/main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,20 @@ import (
1111

1212
const usage = `High performance container daemon cli`
1313

14+
type Exit struct {
15+
Code int
16+
}
17+
1418
func main() {
19+
// We want our defer functions to be run when calling fatal()
20+
defer func() {
21+
if e := recover(); e != nil {
22+
if ex, ok := e.(Exit); ok == true {
23+
os.Exit(ex.Code)
24+
}
25+
panic(e)
26+
}
27+
}()
1528
app := cli.NewApp()
1629
app.Name = "ctr"
1730
if containerd.GitCommit != "" {
@@ -50,5 +63,5 @@ func main() {
5063

5164
func fatal(err string, code int) {
5265
fmt.Fprintf(os.Stderr, "[ctr] %s\n", err)
53-
os.Exit(code)
66+
panic(Exit{code})
5467
}

0 commit comments

Comments
 (0)