Skip to content

Commit 8fbdb7e

Browse files
committed
setupIO: optimize
The rootuid and rootgid are only needed when detach and createTTY are both false. We also call c.Config() twice, every time creating a copy of struct Config. Solve both issues by passing container pointer to setupIO, and get rootuid/rootgid only when we need those. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent c4eb0c6 commit 8fbdb7e

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

utils_linux.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func newProcess(p specs.Process) (*libcontainer.Process, error) {
8989
}
9090

9191
// setupIO modifies the given process config according to the options.
92-
func setupIO(process *libcontainer.Process, rootuid, rootgid int, createTTY, detach bool, sockpath string) (*tty, error) {
92+
func setupIO(process *libcontainer.Process, container *libcontainer.Container, createTTY, detach bool, sockpath string) (*tty, error) {
9393
if createTTY {
9494
process.Stdin = nil
9595
process.Stdout = nil
@@ -135,6 +135,17 @@ func setupIO(process *libcontainer.Process, rootuid, rootgid int, createTTY, det
135135
inheritStdio(process)
136136
return &tty{}, nil
137137
}
138+
139+
config := container.Config()
140+
rootuid, err := config.HostRootUID()
141+
if err != nil {
142+
return nil, err
143+
}
144+
rootgid, err := config.HostRootGID()
145+
if err != nil {
146+
return nil, err
147+
}
148+
138149
return setupProcessPipes(process, rootuid, rootgid)
139150
}
140151

@@ -232,20 +243,12 @@ func (r *runner) run(config *specs.Process) (int, error) {
232243
}
233244
process.ExtraFiles = append(process.ExtraFiles, os.NewFile(uintptr(i), "PreserveFD:"+strconv.Itoa(i)))
234245
}
235-
rootuid, err := r.container.Config().HostRootUID()
236-
if err != nil {
237-
return -1, err
238-
}
239-
rootgid, err := r.container.Config().HostRootGID()
240-
if err != nil {
241-
return -1, err
242-
}
243246
detach := r.detach || (r.action == CT_ACT_CREATE)
244247
// Setting up IO is a two stage process. We need to modify process to deal
245248
// with detaching containers, and then we get a tty after the container has
246249
// started.
247250
handler := newSignalHandler(r.enableSubreaper, r.notifySocket)
248-
tty, err := setupIO(process, rootuid, rootgid, config.Terminal, detach, r.consoleSocket)
251+
tty, err := setupIO(process, r.container, config.Terminal, detach, r.consoleSocket)
249252
if err != nil {
250253
return -1, err
251254
}

0 commit comments

Comments
 (0)