Skip to content

Commit 345bfaf

Browse files
mheonrh-atomic-bot
authored andcommitted
Rework exec to enable splitting to retrieve exec PID
Signed-off-by: Matthew Heon <[email protected]> Closes: #412 Approved by: baude
1 parent 2a0c949 commit 345bfaf

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

libpod/container_api.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package libpod
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io/ioutil"
76
"os"
87
"path/filepath"
98

109
"github.com/docker/docker/daemon/caps"
11-
"github.com/docker/docker/pkg/stringid"
1210
"github.com/docker/docker/pkg/term"
1311
"github.com/pkg/errors"
1412
"github.com/projectatomic/libpod/libpod/driver"
@@ -236,21 +234,8 @@ func (c *Container) Exec(tty, privileged bool, env, cmd []string, user string) e
236234
if privileged || c.config.Privileged {
237235
capList = caps.GetAllCapabilities()
238236
}
239-
globalOpts := runcGlobalOptions{
240-
log: c.LogPath(),
241-
}
242-
243-
execOpts := runcExecOptions{
244-
capAdd: capList,
245-
pidFile: filepath.Join(c.state.RunDir, fmt.Sprintf("%s-execpid", stringid.GenerateNonCryptoID()[:12])),
246-
env: env,
247-
noNewPrivs: c.config.Spec.Process.NoNewPrivileges,
248-
user: user,
249-
cwd: c.config.Spec.Process.Cwd,
250-
tty: tty,
251-
}
252237

253-
return c.runtime.ociRuntime.execContainer(c, cmd, globalOpts, execOpts)
238+
return c.runtime.ociRuntime.execContainer(c, cmd, tty, user, capList, env)
254239
}
255240

256241
// Attach attaches to a container

libpod/oci.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,54 @@ func (r *OCIRuntime) unpauseContainer(ctr *Container) error {
467467
return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "resume", ctr.ID())
468468
}
469469

470-
//execContiner executes a command in a running container
471-
func (r *OCIRuntime) execContainer(c *Container, cmd []string, globalOpts runcGlobalOptions, commandOpts runcExecOptions) error {
472-
r.RuncExec(c, cmd, globalOpts, commandOpts)
473-
return nil
470+
// execContainer executes a command in a running container
471+
// TODO: Add --detach support
472+
// TODO: Convert to use conmon
473+
// TODO: add --pid-file and use that to generate exec session tracking
474+
func (r *OCIRuntime) execContainer(c *Container, cmd []string, tty bool, user string, capAdd, env []string) error {
475+
args := []string{}
476+
477+
// TODO - should we maintain separate logpaths for exec sessions?
478+
args = append(args, "--log", c.LogPath())
479+
480+
args = append(args, "exec")
481+
482+
args = append(args, "--cwd", c.config.Spec.Process.Cwd)
483+
484+
if tty {
485+
args = append(args, "--tty")
486+
}
487+
488+
if user != "" {
489+
args = append(args, "--user", user)
490+
}
491+
492+
if c.config.Spec.Process.NoNewPrivileges {
493+
args = append(args, "--no-new-privs")
494+
}
495+
496+
for _, cap := range capAdd {
497+
args = append(args, "--cap", cap)
498+
}
499+
500+
for _, envVar := range env {
501+
args = append(args, "--env", envVar)
502+
}
503+
504+
// Append container ID and command
505+
args = append(args, c.ID())
506+
args = append(args, cmd...)
507+
508+
logrus.Debugf("Starting runtime %s with following arguments: %v", r.path, args)
509+
510+
execCmd := exec.Command(r.path, args...)
511+
execCmd.Stdout = os.Stdout
512+
execCmd.Stderr = os.Stderr
513+
execCmd.Stdin = os.Stdin
514+
515+
if err := execCmd.Start(); err != nil {
516+
return errors.Wrapf(err, "error starting exec command for container %s", c.ID())
517+
}
518+
519+
return execCmd.Wait()
474520
}

0 commit comments

Comments
 (0)