From b44f3d4756c92384074f802457a5c69ed4c5fc7e Mon Sep 17 00:00:00 2001 From: Jonathan Yu Date: Fri, 22 Jan 2021 01:58:55 +0000 Subject: [PATCH] Create login shell with coder sh When creating a shell, we should use "exec -a" to set argv[0] to the basename of the command being run, prefixing a hyphen. This is how su --login creates login shells: it runs /bin/bash as "-bash", causing bash to execute .bash_profile, etc. We change the use of awk to cut, since cut is part of coreutils and thus more likely to be installed than gawk. We also pass the uid (from "id -u") instead of username to getent. --- internal/cmd/shell.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/cmd/shell.go b/internal/cmd/shell.go index f52cd0eb..9cb6d1bf 100644 --- a/internal/cmd/shell.go +++ b/internal/cmd/shell.go @@ -85,7 +85,9 @@ func shell(cmd *cobra.Command, cmdArgs []string) error { args = append(args, strings.Join(cmdArgs[1:], " ")) } else { // Bring user into shell if no command is specified. - args = append(args, "exec $(getent passwd $(whoami) | awk -F: '{ print $7 }')") + shell := "$(getent passwd $(id -u) | cut -d: -f 7)" + name := "-$(basename " + shell + ")" + args = append(args, fmt.Sprintf("exec -a %q %q", name, shell)) } envName := cmdArgs[0]