mirror of
https://github.com/containers/podman.git
synced 2025-06-27 13:38:49 +08:00
Merge pull request #20357 from rhatdan/TERM
Add TERM iff TERM not defined in container when podman exec -t
This commit is contained in:
@ -713,6 +713,14 @@ func (c *Container) LinuxResources() *spec.LinuxResources {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Env returns the default environment variables defined for the container
|
||||||
|
func (c *Container) Env() []string {
|
||||||
|
if c.config.Spec != nil && c.config.Spec.Process != nil {
|
||||||
|
return c.config.Spec.Process.Env
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// State Accessors
|
// State Accessors
|
||||||
// Require locking
|
// Require locking
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
api "github.com/containers/podman/v4/pkg/api/types"
|
api "github.com/containers/podman/v4/pkg/api/types"
|
||||||
"github.com/containers/podman/v4/pkg/domain/entities"
|
"github.com/containers/podman/v4/pkg/domain/entities"
|
||||||
"github.com/containers/podman/v4/pkg/specgenutil"
|
"github.com/containers/podman/v4/pkg/specgenutil"
|
||||||
|
"github.com/containers/podman/v4/pkg/util"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -59,6 +60,10 @@ func ExecCreateHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
libpodConfig.Privileged = input.Privileged
|
libpodConfig.Privileged = input.Privileged
|
||||||
libpodConfig.User = input.User
|
libpodConfig.User = input.User
|
||||||
|
|
||||||
|
if input.Tty {
|
||||||
|
util.ExecAddTERM(ctr.Env(), libpodConfig.Environment)
|
||||||
|
}
|
||||||
|
|
||||||
// Make our exit command
|
// Make our exit command
|
||||||
storageConfig := runtime.StorageConfig()
|
storageConfig := runtime.StorageConfig()
|
||||||
runtimeConfig, err := runtime.GetConfig()
|
runtimeConfig, err := runtime.GetConfig()
|
||||||
|
@ -867,6 +867,10 @@ func (ic *ContainerEngine) ContainerExec(ctx context.Context, nameOrID string, o
|
|||||||
}
|
}
|
||||||
ctr := containers[0]
|
ctr := containers[0]
|
||||||
|
|
||||||
|
if options.Tty {
|
||||||
|
util.ExecAddTERM(ctr.Env(), options.Envs)
|
||||||
|
}
|
||||||
|
|
||||||
execConfig, err := makeExecConfig(options, ic.Libpod)
|
execConfig, err := makeExecConfig(options, ic.Libpod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ec, err
|
return ec, err
|
||||||
|
@ -1219,3 +1219,20 @@ func ConvertTimeout(timeout int) uint {
|
|||||||
}
|
}
|
||||||
return uint(timeout)
|
return uint(timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecAddTERM when container does not have a TERM environment variable and
|
||||||
|
// caller wants a tty, then leak the existing TERM environment into
|
||||||
|
// the container.
|
||||||
|
func ExecAddTERM(existingEnv []string, execEnvs map[string]string) {
|
||||||
|
if _, ok := execEnvs["TERM"]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, val := range existingEnv {
|
||||||
|
if strings.HasPrefix(val, "TERM=") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
execEnvs["TERM"] = "xterm"
|
||||||
|
}
|
||||||
|
@ -166,6 +166,47 @@ load helpers
|
|||||||
run_podman rm -f -t0 $cid
|
run_podman rm -f -t0 $cid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "podman exec --tty" {
|
||||||
|
# Outer loops: different variations on the RUN container
|
||||||
|
for run_opt_t in "" "-t"; do
|
||||||
|
for run_term_env in "" "explicit_RUN_term"; do
|
||||||
|
local run_opt_env=
|
||||||
|
if [[ -n "$run_term_env" ]]; then
|
||||||
|
run_opt_env="--env=TERM=$run_term_env"
|
||||||
|
fi
|
||||||
|
run_podman run -d $run_opt_t $run_opt_env --name test $IMAGE top
|
||||||
|
|
||||||
|
# Inner loops: different variations on EXEC
|
||||||
|
for exec_opt_t in "" "-t"; do
|
||||||
|
for exec_term_env in "" "explicit_EXEC_term"; do
|
||||||
|
# What to expect.
|
||||||
|
local expected=
|
||||||
|
# if -t is set anywhere, either run or exec, go with xterm
|
||||||
|
if [[ -n "$run_opt_t$exec_opt_t" ]]; then
|
||||||
|
expected="xterm"
|
||||||
|
fi
|
||||||
|
# ...unless overridden by explicit --env
|
||||||
|
if [[ -n "$run_term_env$exec_term_env" ]]; then
|
||||||
|
# (exec overrides run)
|
||||||
|
expected="${exec_term_env:-$run_term_env}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local exec_opt_env=
|
||||||
|
if [[ -n "$exec_term_env" ]]; then
|
||||||
|
exec_opt_env="--env=TERM=$exec_term_env"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local desc="run $run_opt_t $run_opt_env, exec $exec_opt_t $exec_opt_env"
|
||||||
|
TERM=exec-term run_podman exec $exec_opt_t $exec_opt_env test sh -c 'echo -n $TERM'
|
||||||
|
assert "$output" = "$expected" "$desc"
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
|
run_podman rm -f -t0 test
|
||||||
|
done
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
@test "podman exec - does not leak session IDs on invalid command" {
|
@test "podman exec - does not leak session IDs on invalid command" {
|
||||||
skip_if_remote "FIXME FIXME FIXME: this should work on remote, but does not"
|
skip_if_remote "FIXME FIXME FIXME: this should work on remote, but does not"
|
||||||
run_podman run -d $IMAGE top
|
run_podman run -d $IMAGE top
|
||||||
|
Reference in New Issue
Block a user