From 7679d4a7fd2a69766101dcc930d988f97e811930 Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Fri, 20 Nov 2020 10:31:00 +0200 Subject: [PATCH 1/4] exec shell: fix shell config Currently each time when ctop started it overwrites "shell" config with default value of program argument. This means that in fact default shell config is never worked at all. --- main.go | 2 +- menus.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index ea1ea7c..148bfe4 100644 --- a/main.go +++ b/main.go @@ -46,7 +46,7 @@ func main() { invertFlag = flag.Bool("i", false, "invert default colors") scaleCpu = flag.Bool("scale-cpu", false, "show cpu as % of system total") connectorFlag = flag.String("connector", "docker", "container connector to use") - defaultShell = flag.String("shell", "sh", "exec shell to use") + defaultShell = flag.String("shell", "", "exec shell to use") ) flag.Parse() diff --git a/menus.go b/menus.go index c3ca2af..7c79285 100644 --- a/menus.go +++ b/menus.go @@ -359,8 +359,8 @@ func ExecShell() MenuFn { ui.DefaultEvtStream.ResetHandlers() defer ui.DefaultEvtStream.ResetHandlers() - shell := config.Get("shell") - if err := c.Exec([]string{shell.Val, "-c", "printf '\\e[0m\\e[?25h' && clear && " + shell.Val}); err != nil { + shell := config.GetVal("shell") + if err := c.Exec([]string{shell, "-c", "printf '\\e[0m\\e[?25h' && clear && " + shell}); err != nil { log.Fatal(err) } From 83a422933a4648b3e24f71b0c530511a30fca5d4 Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Fri, 20 Nov 2020 10:33:44 +0200 Subject: [PATCH 2/4] exec shell: on error show a status message instead of fatal exit --- menus.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/menus.go b/menus.go index 7c79285..d73f059 100644 --- a/menus.go +++ b/menus.go @@ -361,7 +361,7 @@ func ExecShell() MenuFn { shell := config.GetVal("shell") if err := c.Exec([]string{shell, "-c", "printf '\\e[0m\\e[?25h' && clear && " + shell}); err != nil { - log.Fatal(err) + log.StatusErr(err) } return nil From 5ec02f760ecb305d9e5a1cf3ec093093891440f6 Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Fri, 20 Nov 2020 10:39:28 +0200 Subject: [PATCH 3/4] exec shell: remove shell config or option The option is never worked and can't properly work because almost all containers anyway using Ash/Dash from /bin/sh --- README.md | 1 - config/param.go | 5 ----- main.go | 5 ----- menus.go | 3 +-- 4 files changed, 1 insertion(+), 13 deletions(-) diff --git a/README.md b/README.md index f2832fe..cb45a48 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,6 @@ Option | Description `-s` | select initial container sort field `-scale-cpu` | show cpu as % of system total `-v` | output version information and exit -`-shell` | exec shell to use (default: sh) ### Keybindings diff --git a/config/param.go b/config/param.go index dc334f5..281cc6c 100644 --- a/config/param.go +++ b/config/param.go @@ -12,11 +12,6 @@ var defaultParams = []*Param{ Val: "state", Label: "Container Sort Field", }, - &Param{ - Key: "shell", - Val: "sh", - Label: "Shell", - }, &Param{ Key: "columns", Val: "status,name,id,cpu,mem,net,io,pids", diff --git a/main.go b/main.go index 148bfe4..71b7dde 100644 --- a/main.go +++ b/main.go @@ -46,7 +46,6 @@ func main() { invertFlag = flag.Bool("i", false, "invert default colors") scaleCpu = flag.Bool("scale-cpu", false, "show cpu as % of system total") connectorFlag = flag.String("connector", "docker", "container connector to use") - defaultShell = flag.String("shell", "", "exec shell to use") ) flag.Parse() @@ -91,10 +90,6 @@ func main() { config.Toggle("scaleCpu") } - if *defaultShell != "" { - config.Update("shell", *defaultShell) - } - // init ui if *invertFlag { InvertColorMap() diff --git a/menus.go b/menus.go index d73f059..2871fe9 100644 --- a/menus.go +++ b/menus.go @@ -359,8 +359,7 @@ func ExecShell() MenuFn { ui.DefaultEvtStream.ResetHandlers() defer ui.DefaultEvtStream.ResetHandlers() - shell := config.GetVal("shell") - if err := c.Exec([]string{shell, "-c", "printf '\\e[0m\\e[?25h' && clear && " + shell}); err != nil { + if err := c.Exec([]string{"/bin/sh", "-c", "printf '\\e[0m\\e[?25h' && clear && /bin/sh"}); err != nil { log.StatusErr(err) } From 53a6b36bf5de3dd6ae8e7ddd8de601571e0ea9f6 Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Fri, 20 Nov 2020 10:42:52 +0200 Subject: [PATCH 4/4] exec shell: detect default shell Instead of using configured shell (e.g. bash) we can autodetect default container user's shell and execute it. This is much safer because not all containers may have installed shell that is configured in ctop. --- menus.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/menus.go b/menus.go index 2871fe9..4e0cd6a 100644 --- a/menus.go +++ b/menus.go @@ -358,8 +358,16 @@ func ExecShell() MenuFn { ui.DefaultEvtStream.ResetHandlers() defer ui.DefaultEvtStream.ResetHandlers() - - if err := c.Exec([]string{"/bin/sh", "-c", "printf '\\e[0m\\e[?25h' && clear && /bin/sh"}); err != nil { + // Detect and execute default shell in container. + // Execute Ash shell command: /bin/sh -c + // Reset colors: printf '\e[0m\e[?25h' + // Clear screen + // Run default shell for the user. It's configured in /etc/passwd and looks like root:x:0:0:root:/root:/bin/bash: + // 1. Get current user id: id -un + // 2. Find user's line in /etc/passwd by grep + // 3. Extract default user's shell by cutting seven's column separated by : + // 4. Execute the shell path with eval + if err := c.Exec([]string{"/bin/sh", "-c", "printf '\\e[0m\\e[?25h' && clear && eval `grep ^$(id -un): /etc/passwd | cut -d : -f 7-`"}); err != nil { log.StatusErr(err) }