Skip to content

Commit

Permalink
Use terminal detach keys sequence specified in the config file
Browse files Browse the repository at this point in the history
Fixes: #4556

Signed-off-by: Marco Vedovati <mv@sba.lat>
  • Loading branch information
marcov committed Dec 6, 2019
1 parent 465e142 commit 7fa5d9b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
3 changes: 3 additions & 0 deletions cmd/podman/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func init() {
attachCommand.SetUsageTemplate(UsageTemplate())
flags := attachCommand.Flags()
flags.StringVar(&attachCommand.DetachKeys, "detach-keys", define.DefaultDetachKeys, "Select the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-z`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`")
// Clear the default, the value specified in the config file should have the
// priority
attachCommand.DetachKeys = ""
flags.BoolVar(&attachCommand.NoStdin, "no-stdin", false, "Do not attach STDIN. The default is false")
flags.BoolVar(&attachCommand.SigProxy, "sig-proxy", true, "Proxy received signals to the process")
flags.BoolVarP(&attachCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
Expand Down
6 changes: 5 additions & 1 deletion cmd/podman/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,14 @@ func getCreateFlags(c *cliconfig.PodmanCommand) {
"detach", "d", false,
"Run container in background and print container ID",
)
createFlags.String(
detachKeys := createFlags.String(
"detach-keys", define.DefaultDetachKeys,
"Override the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-z`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`",
)
// Clear the default, the value specified in the config file should have the
// priority
*detachKeys = ""

createFlags.StringSlice(
"device", []string{},
"Add a host device to the container (default [])",
Expand Down
3 changes: 3 additions & 0 deletions cmd/podman/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func init() {
flags := execCommand.Flags()
flags.SetInterspersed(false)
flags.StringVar(&execCommand.DetachKeys, "detach-keys", define.DefaultDetachKeys, "Select the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _")
// Clear the default, the value specified in the config file should have the
// priority
execCommand.DetachKeys = ""
flags.StringArrayVarP(&execCommand.Env, "env", "e", []string{}, "Set environment variables")
flags.BoolVarP(&execCommand.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
flags.BoolVarP(&execCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
Expand Down
3 changes: 3 additions & 0 deletions cmd/podman/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func init() {
startCommand.SetUsageTemplate(UsageTemplate())
flags := startCommand.Flags()
flags.BoolVarP(&startCommand.Attach, "attach", "a", false, "Attach container's STDOUT and STDERR")
// Clear the default, the value specified in the config file should have the
// priority
startCommand.DetachKeys = ""
flags.StringVar(&startCommand.DetachKeys, "detach-keys", define.DefaultDetachKeys, "Select the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-z`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`")
flags.BoolVarP(&startCommand.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
flags.BoolVarP(&startCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
Expand Down
46 changes: 42 additions & 4 deletions pkg/adapter/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,23 @@ func (r *LocalRuntime) CreateContainer(ctx context.Context, c *cliconfig.CreateV
return ctr.ID(), nil
}

// Select the detach keys to use from user input flag, config file, or default value
func (r *LocalRuntime) selectDetachKeys(flagValue string) (string, error) {
if flagValue != "" {
return flagValue, nil
}

config, err := r.GetConfig()
if err != nil {
return "", errors.Wrapf(err, "unable to retrive runtime config")
}
if config.DetachKeys != "" {
return config.DetachKeys, nil
}

return define.DefaultDetachKeys, nil
}

// Run a libpod container
func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode int) (int, error) {
results := shared.NewIntermediateLayer(&c.PodmanCommand, false)
Expand Down Expand Up @@ -428,8 +445,13 @@ func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode
}
}

keys, err := r.selectDetachKeys(c.String("detach-keys"))
if err != nil {
return exitCode, err
}

// if the container was created as part of a pod, also start its dependencies, if any.
if err := StartAttachCtr(ctx, ctr, outputStream, errorStream, inputStream, c.String("detach-keys"), c.Bool("sig-proxy"), true, c.IsSet("pod")); err != nil {
if err := StartAttachCtr(ctx, ctr, outputStream, errorStream, inputStream, keys, c.Bool("sig-proxy"), true, c.IsSet("pod")); err != nil {
// We've manually detached from the container
// Do not perform cleanup, or wait for container exit code
// Just exit immediately
Expand Down Expand Up @@ -512,8 +534,14 @@ func (r *LocalRuntime) Attach(ctx context.Context, c *cliconfig.AttachValues) er
if c.NoStdin {
inputStream = nil
}

keys, err := r.selectDetachKeys(c.DetachKeys)
if err != nil {
return err
}

// If the container is in a pod, also set to recursively start dependencies
if err := StartAttachCtr(ctx, ctr, os.Stdout, os.Stderr, inputStream, c.DetachKeys, c.SigProxy, false, ctr.PodID() != ""); err != nil && errors.Cause(err) != define.ErrDetach {
if err := StartAttachCtr(ctx, ctr, os.Stdout, os.Stderr, inputStream, keys, c.SigProxy, false, ctr.PodID() != ""); err != nil && errors.Cause(err) != define.ErrDetach {
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
}
return nil
Expand Down Expand Up @@ -646,9 +674,14 @@ func (r *LocalRuntime) Start(ctx context.Context, c *cliconfig.StartValues, sigP
}
}

keys, err := r.selectDetachKeys(c.DetachKeys)
if err != nil {
return exitCode, err
}

// attach to the container and also start it not already running
// If the container is in a pod, also set to recursively start dependencies
err = StartAttachCtr(ctx, ctr.Container, os.Stdout, os.Stderr, inputStream, c.DetachKeys, sigProxy, !ctrRunning, ctr.PodID() != "")
err = StartAttachCtr(ctx, ctr.Container, os.Stdout, os.Stderr, inputStream, keys, sigProxy, !ctrRunning, ctr.PodID() != "")
if errors.Cause(err) == define.ErrDetach {
// User manually detached
// Exit cleanly immediately
Expand Down Expand Up @@ -1005,7 +1038,12 @@ func (r *LocalRuntime) ExecContainer(ctx context.Context, cli *cliconfig.ExecVal
streams.AttachOutput = true
streams.AttachError = true

ec, err = ExecAttachCtr(ctx, ctr.Container, cli.Tty, cli.Privileged, env, cmd, cli.User, cli.Workdir, streams, uint(cli.PreserveFDs), cli.DetachKeys)
keys, err := r.selectDetachKeys(cli.DetachKeys)
if err != nil {
return ec, err
}

ec, err = ExecAttachCtr(ctx, ctr.Container, cli.Tty, cli.Privileged, env, cmd, cli.User, cli.Workdir, streams, uint(cli.PreserveFDs), keys)
return define.TranslateExecErrorToExitCode(ec, err), err
}

Expand Down

0 comments on commit 7fa5d9b

Please sign in to comment.