libpod: specify a detach keys sequence in libpod.conf

Add the ability of specifying a detach keys sequence in libpod.conf

Signed-off-by: Marco Vedovati <mvedovati@suse.com>
This commit is contained in:
Marco Vedovati
2019-06-13 19:33:58 +02:00
parent c9078936dd
commit 7e3f1c21b0
10 changed files with 66 additions and 35 deletions

View File

@ -98,6 +98,9 @@ libpod to manage containers.
**events_logger**="" **events_logger**=""
Default method to use when logging events. Valid values are "journald" and "file". Default method to use when logging events. Valid values are "journald" and "file".
**detach_keys**=""
Keys sequence used for detaching a container
## FILES ## FILES
`/usr/share/containers/libpod.conf`, default libpod configuration path `/usr/share/containers/libpod.conf`, default libpod configuration path

View File

@ -11,12 +11,16 @@ The attach command allows you to attach to a running container using the contain
or name, either to view its ongoing output or to control it interactively. or name, either to view its ongoing output or to control it interactively.
You can detach from the container (and leave it running) using a configurable key sequence. The default You can detach from the container (and leave it running) using a configurable key sequence. The default
sequence is `ctrl-p,ctrl-q`. You configure the key sequence using the --detach-keys option sequence is `ctrl-p,ctrl-q`.
Configure the keys sequence using the **--detach-keys** option, or specifying
it in the **libpod.conf** file: see **libpod.conf(5)** for more information.
## OPTIONS ## OPTIONS
**--detach-keys**=*char* **--detach-keys**=*sequence*
Override 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 `_`. 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 `_`.
**--latest**, **-l** **--latest**, **-l**

View File

@ -175,12 +175,14 @@ detached container with **podman attach**.
When attached in the tty mode, you can detach from the container (and leave it When attached in the tty mode, you can detach from the container (and leave it
running) using a configurable key sequence. The default sequence is `ctrl-p,ctrl-q`. running) using a configurable key sequence. The default sequence is `ctrl-p,ctrl-q`.
You configure the key sequence using the **--detach-keys** option or a configuration file. Configure the keys sequence using the **--detach-keys** option, or specifying
See **config-json(5)** for documentation on using a configuration file. it in the **libpod.conf** file: see **libpod.conf(5)** for more information.
**--detach-keys**=*char* **--detach-keys**=*sequence*
Override 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 `_`. 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 `_`.
**--device**=*device* **--device**=*device*

View File

@ -181,12 +181,14 @@ detached container with **podman attach**.
When attached in the tty mode, you can detach from the container (and leave it When attached in the tty mode, you can detach from the container (and leave it
running) using a configurable key sequence. The default sequence is `ctrl-p,ctrl-q`. running) using a configurable key sequence. The default sequence is `ctrl-p,ctrl-q`.
You configure the key sequence using the **--detach-keys** option or a configuration file. Configure the keys sequence using the **--detach-keys** option, or specifying
See **config-json(5)** for documentation on using a configuration file. it in the **libpod.conf** file: see **libpod.conf(5)** for more information.
**--detach-keys**=*char* **--detach-keys**=*sequence*
Override 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 `_`. 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 `_`.
**--device**=*device* **--device**=*device*

View File

@ -19,9 +19,11 @@ attach to the container.
Attach container's STDOUT and STDERR. The default is false. This option cannot be used when Attach container's STDOUT and STDERR. The default is false. This option cannot be used when
starting multiple containers. starting multiple containers.
**--detach-keys**=*char* **--detach-keys**=*sequence*
Override 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 `_`. 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 `_`.
**--interactive**, **-i** **--interactive**, **-i**

View File

@ -105,6 +105,13 @@ num_locks = 2048
# are `journald` or `file`. # are `journald` or `file`.
# events_logger = "journald" # events_logger = "journald"
# Specify the keys sequence used to detach 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 `_`
#
# detach_keys = "ctrl-p,ctrl-q"
# Default OCI runtime # Default OCI runtime
runtime = "runc" runtime = "runc"

View File

@ -38,29 +38,32 @@ func (c *Container) attach(streams *AttachStreams, keys string, resize <-chan re
return errors.Wrapf(define.ErrInvalidArg, "must provide at least one stream to attach to") return errors.Wrapf(define.ErrInvalidArg, "must provide at least one stream to attach to")
} }
// Check the validity of the provided keys first
var err error
detachKeys := []byte{}
if len(keys) > 0 {
detachKeys, err = term.ToBytes(keys)
if err != nil {
return errors.Wrapf(err, "invalid detach keys")
}
}
logrus.Debugf("Attaching to container %s", c.ID()) logrus.Debugf("Attaching to container %s", c.ID())
return c.attachContainerSocket(resize, detachKeys, streams, startContainer, wg) return c.attachContainerSocket(resize, keys, streams, startContainer, wg)
} }
// attachContainerSocket connects to the container's attach socket and deals with the IO. // attachContainerSocket connects to the container's attach socket and deals with the IO.
// wg is only required if startContainer is true // wg is only required if startContainer is true
// TODO add a channel to allow interrupting // TODO add a channel to allow interrupting
func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, detachKeys []byte, streams *AttachStreams, startContainer bool, wg *sync.WaitGroup) error { func (c *Container) attachContainerSocket(resize <-chan remotecommand.TerminalSize, keys string, streams *AttachStreams, startContainer bool, wg *sync.WaitGroup) error {
if startContainer && wg == nil { if startContainer && wg == nil {
return errors.Wrapf(define.ErrInternal, "wait group not passed when startContainer set") return errors.Wrapf(define.ErrInternal, "wait group not passed when startContainer set")
} }
// Use default detach keys when keys aren't passed or specified in libpod.conf
if len(keys) == 0 {
keys = DefaultDetachKeys
}
// Check the validity of the provided keys
detachKeys := []byte{}
var err error
detachKeys, err = term.ToBytes(keys)
if err != nil {
return errors.Wrapf(err, "invalid detach keys")
}
kubeutils.HandleResizing(resize, func(size remotecommand.TerminalSize) { kubeutils.HandleResizing(resize, func(size remotecommand.TerminalSize) {
controlPath := filepath.Join(c.bundlePath(), "ctl") controlPath := filepath.Join(c.bundlePath(), "ctl")
controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0) controlFile, err := os.OpenFile(controlPath, unix.O_WRONLY, 0)

View File

@ -81,6 +81,10 @@ var (
DefaultSHMLockPath = "/libpod_lock" DefaultSHMLockPath = "/libpod_lock"
// DefaultRootlessSHMLockPath is the default path for rootless SHM locks // DefaultRootlessSHMLockPath is the default path for rootless SHM locks
DefaultRootlessSHMLockPath = "/libpod_rootless_lock" DefaultRootlessSHMLockPath = "/libpod_rootless_lock"
// DefaultDetachKeys is the default keys sequence for detaching a
// container
DefaultDetachKeys = "ctrl-p,ctrl-q"
) )
// A RuntimeOption is a functional option which alters the Runtime created by // A RuntimeOption is a functional option which alters the Runtime created by
@ -236,6 +240,8 @@ type RuntimeConfig struct {
EventsLogger string `toml:"events_logger"` EventsLogger string `toml:"events_logger"`
// EventsLogFilePath is where the events log is stored. // EventsLogFilePath is where the events log is stored.
EventsLogFilePath string `toml:-"events_logfile_path"` EventsLogFilePath string `toml:-"events_logfile_path"`
//DetachKeys is the sequence of keys used to detach a container
DetachKeys string `toml:"detach_keys"`
} }
// runtimeConfiguredFrom is a struct used during early runtime init to help // runtimeConfiguredFrom is a struct used during early runtime init to help
@ -311,6 +317,7 @@ func defaultRuntimeConfig() (RuntimeConfig, error) {
EnableLabeling: true, EnableLabeling: true,
NumLocks: 2048, NumLocks: 2048,
EventsLogger: events.DefaultEventerType.String(), EventsLogger: events.DefaultEventerType.String(),
DetachKeys: DefaultDetachKeys,
}, nil }, nil
} }

View File

@ -381,8 +381,18 @@ func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode
} }
} }
} }
config, err := r.Runtime.GetConfig()
if err != nil {
return exitCode, err
}
detachKeys := c.String("detach-keys")
if detachKeys == "" {
detachKeys = config.DetachKeys
}
// if the container was created as part of a pod, also start its dependencies, if any. // 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, detachKeys, c.Bool("sig-proxy"), true, c.IsSet("pod")); err != nil {
// We've manually detached from the container // We've manually detached from the container
// Do not perform cleanup, or wait for container exit code // Do not perform cleanup, or wait for container exit code
// Just exit immediately // Just exit immediately
@ -407,10 +417,6 @@ func (r *LocalRuntime) Run(ctx context.Context, c *cliconfig.RunValues, exitCode
if errors.Cause(err) == define.ErrNoSuchCtr { if errors.Cause(err) == define.ErrNoSuchCtr {
// The container may have been removed // The container may have been removed
// Go looking for an exit file // Go looking for an exit file
config, err := r.Runtime.GetConfig()
if err != nil {
return exitCode, err
}
ctrExitCode, err := ReadExitFile(config.TmpDir, ctr.ID()) ctrExitCode, err := ReadExitFile(config.TmpDir, ctr.ID())
if err != nil { if err != nil {
logrus.Errorf("Cannot get exit code: %v", err) logrus.Errorf("Cannot get exit code: %v", err)

View File

@ -59,11 +59,6 @@ var ErrDetach = errors.New("detached from container")
// CopyDetachable is similar to io.Copy but support a detach key sequence to break out. // CopyDetachable is similar to io.Copy but support a detach key sequence to break out.
func CopyDetachable(dst io.Writer, src io.Reader, keys []byte) (written int64, err error) { func CopyDetachable(dst io.Writer, src io.Reader, keys []byte) (written int64, err error) {
if len(keys) == 0 {
// Default keys : ctrl-p,ctrl-q
keys = []byte{16, 17}
}
buf := make([]byte, 32*1024) buf := make([]byte, 32*1024)
for { for {
nr, er := src.Read(buf) nr, er := src.Read(buf)