Skip to content

Commit

Permalink
Fix close fds of exec --preserve-fds
Browse files Browse the repository at this point in the history
Fix the closing of fds from --preserve-fds to avoid the operation on unrelated fds.

Signed-off-by: Qi Wang <[email protected]>
  • Loading branch information
QiWang19 authored and vrothberg committed Aug 11, 2020
1 parent 895e0d0 commit 92d01d7
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions libpod/oci_conmon_exec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,12 @@ func (r *ConmonOCIRuntime) startExec(c *Container, sessionID string, options *Ex
return nil, nil, err
}

var filesToClose []*os.File
if options.PreserveFDs > 0 {
for fd := 3; fd < int(3+options.PreserveFDs); fd++ {
execCmd.ExtraFiles = append(execCmd.ExtraFiles, os.NewFile(uintptr(fd), fmt.Sprintf("fd-%d", fd)))
f := os.NewFile(uintptr(fd), fmt.Sprintf("fd-%d", fd))
filesToClose = append(filesToClose, f)
execCmd.ExtraFiles = append(execCmd.ExtraFiles, f)
}
}

Expand Down Expand Up @@ -483,14 +486,10 @@ func (r *ConmonOCIRuntime) startExec(c *Container, sessionID string, options *Ex
return nil, nil, err
}

if options.PreserveFDs > 0 {
for fd := 3; fd < int(3+options.PreserveFDs); fd++ {
// These fds were passed down to the runtime. Close them
// and not interfere
if err := os.NewFile(uintptr(fd), fmt.Sprintf("fd-%d", fd)).Close(); err != nil {
logrus.Debugf("unable to close file fd-%d", fd)
}
}
// These fds were passed down to the runtime. Close them
// and not interfere
for _, f := range filesToClose {
errorhandling.CloseQuiet(f)
}

return execCmd, pipes, nil
Expand Down

0 comments on commit 92d01d7

Please sign in to comment.