Skip to content

Commit 5d2e244

Browse files
committed
execProcess: move some code to newProcess
Let's move some code from execProcess to newProcess, fixing the following few issues: 1. container.State (which does quite a lot) is not needed -- we only need container.Config here. 2. utils.SearchLabels is not needed when "runc exec --process" is used. 3. Context.String("process") is called twice. 4. It is not very clear from the code why checking for len(context.Args()) is performed. Move the check to just before Args is used, to make it clear why. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 35b3c16 commit 5d2e244

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
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

0 commit comments

Comments
 (0)