From 53a6b36bf5de3dd6ae8e7ddd8de601571e0ea9f6 Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Fri, 20 Nov 2020 10:42:52 +0200 Subject: [PATCH] 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) }