Skip to content

Commit 74b35d8

Browse files
authored
Merge pull request #4592 from kolyshkin/exec-nits
Improvements to how `runc exec` is handled
2 parents bf0f67f + 4b87c7d commit 74b35d8

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

exec.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,7 @@ func execProcess(context *cli.Context) (int, error) {
158158
if status == libcontainer.Paused && !context.Bool("ignore-paused") {
159159
return -1, errors.New("cannot exec in a paused container (use --ignore-paused to override)")
160160
}
161-
path := context.String("process")
162-
if path == "" && len(context.Args()) == 1 {
163-
return -1, errors.New("process args cannot be empty")
164-
}
165-
state, err := container.State()
166-
if err != nil {
167-
return -1, err
168-
}
169-
bundle, ok := utils.SearchLabels(state.Config.Labels, "bundle")
170-
if !ok {
171-
return -1, errors.New("bundle not found in labels")
172-
}
173-
p, err := getProcess(context, bundle)
161+
p, err := getProcess(context, container)
174162
if err != nil {
175163
return -1, err
176164
}
@@ -196,7 +184,7 @@ func execProcess(context *cli.Context) (int, error) {
196184
return r.run(p)
197185
}
198186

199-
func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
187+
func getProcess(context *cli.Context, c *libcontainer.Container) (*specs.Process, error) {
200188
if path := context.String("process"); path != "" {
201189
f, err := os.Open(path)
202190
if err != nil {
@@ -209,7 +197,11 @@ func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
209197
}
210198
return &p, validateProcessSpec(&p)
211199
}
212-
// process via cli flags
200+
// Process from config.json and CLI flags.
201+
bundle, ok := utils.SearchLabels(c.Config().Labels, "bundle")
202+
if !ok {
203+
return nil, errors.New("bundle not found in labels")
204+
}
213205
if err := os.Chdir(bundle); err != nil {
214206
return nil, err
215207
}
@@ -218,7 +210,11 @@ func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
218210
return nil, err
219211
}
220212
p := spec.Process
221-
p.Args = context.Args()[1:]
213+
args := context.Args()
214+
if len(args) < 2 {
215+
return nil, errors.New("exec args cannot be empty")
216+
}
217+
p.Args = args[1:]
222218
// Override the cwd, if passed.
223219
if cwd := context.String("cwd"); cwd != "" {
224220
p.Cwd = cwd

libcontainer/container_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,11 @@ func (c *Container) signal(s os.Signal) error {
432432
}
433433

434434
func (c *Container) createExecFifo() (retErr error) {
435-
rootuid, err := c.Config().HostRootUID()
435+
rootuid, err := c.config.HostRootUID()
436436
if err != nil {
437437
return err
438438
}
439-
rootgid, err := c.Config().HostRootGID()
439+
rootgid, err := c.config.HostRootGID()
440440
if err != nil {
441441
return err
442442
}

utils_linux.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ func getDefaultImagePath() string {
4343
return filepath.Join(cwd, "checkpoint")
4444
}
4545

46-
// newProcess returns a new libcontainer Process with the arguments from the
47-
// spec and stdio from the current process.
48-
func newProcess(p specs.Process) (*libcontainer.Process, error) {
46+
// newProcess converts [specs.Process] to [libcontainer.Process].
47+
func newProcess(p *specs.Process) (*libcontainer.Process, error) {
4948
lp := &libcontainer.Process{
5049
Args: p.Args,
5150
Env: p.Env,
@@ -89,7 +88,7 @@ func newProcess(p specs.Process) (*libcontainer.Process, error) {
8988
}
9089

9190
// 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) {
91+
func setupIO(process *libcontainer.Process, container *libcontainer.Container, createTTY, detach bool, sockpath string) (*tty, error) {
9392
if createTTY {
9493
process.Stdin = nil
9594
process.Stdout = nil
@@ -135,6 +134,17 @@ func setupIO(process *libcontainer.Process, rootuid, rootgid int, createTTY, det
135134
inheritStdio(process)
136135
return &tty{}, nil
137136
}
137+
138+
config := container.Config()
139+
rootuid, err := config.HostRootUID()
140+
if err != nil {
141+
return nil, err
142+
}
143+
rootgid, err := config.HostRootGID()
144+
if err != nil {
145+
return nil, err
146+
}
147+
138148
return setupProcessPipes(process, rootuid, rootgid)
139149
}
140150

@@ -210,7 +220,7 @@ func (r *runner) run(config *specs.Process) (int, error) {
210220
if err = r.checkTerminal(config); err != nil {
211221
return -1, err
212222
}
213-
process, err := newProcess(*config)
223+
process, err := newProcess(config)
214224
if err != nil {
215225
return -1, err
216226
}
@@ -232,20 +242,12 @@ func (r *runner) run(config *specs.Process) (int, error) {
232242
}
233243
process.ExtraFiles = append(process.ExtraFiles, os.NewFile(uintptr(i), "PreserveFD:"+strconv.Itoa(i)))
234244
}
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-
}
243245
detach := r.detach || (r.action == CT_ACT_CREATE)
244246
// Setting up IO is a two stage process. We need to modify process to deal
245247
// with detaching containers, and then we get a tty after the container has
246248
// started.
247249
handler := newSignalHandler(r.enableSubreaper, r.notifySocket)
248-
tty, err := setupIO(process, rootuid, rootgid, config.Terminal, detach, r.consoleSocket)
250+
tty, err := setupIO(process, r.container, config.Terminal, detach, r.consoleSocket)
249251
if err != nil {
250252
return -1, err
251253
}

0 commit comments

Comments
 (0)