Skip to content

Commit bc357c9

Browse files
committed
ghost: specify the proper shell to use
1 parent dcafc8a commit bc357c9

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

overlord/sysutils_darwin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func Install() error {
139139
<key>EnvironmentVariables</key>
140140
<dict>
141141
<key>SHELL</key>
142-
<string>/bin/bash</string>
142+
<string>%s</string>
143143
<key>HOME</key>
144144
<string>%s</string>
145145
<key>TERM</key>
@@ -151,7 +151,7 @@ func Install() error {
151151
<true/>
152152
</dict>
153153
</plist>
154-
`, argsXML, homeDir)
154+
`, argsXML, getUserShell(), homeDir)
155155

156156
launchAgentsDir := filepath.Join(homeDir, "Library", "LaunchAgents")
157157
plistFilePath := filepath.Join(launchAgentsDir, "com.overlord.ghost.plist")

overlord/sysutils_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ RequiresMountsFor=%s
185185
[Service]
186186
Type=simple
187187
User=%s
188-
Environment=SHELL=/bin/bash
188+
Environment=SHELL=%s
189189
Environment=HOME=%s
190190
Environment=TERM=xterm-256color
191191
ExecStart=%s %s
@@ -194,7 +194,7 @@ RestartSec=5
194194
195195
[Install]
196196
WantedBy=multi-user.target
197-
`, homeDir, currentUser, homeDir, targetPath, cmdArgs)
197+
`, homeDir, currentUser, getUserShell(), homeDir, targetPath, cmdArgs)
198198

199199
serviceFilePath, err := getSystemdServicePath()
200200
if err != nil {

overlord/utils.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io"
1313
"os"
14+
"os/exec"
1415
"os/user"
1516
"path/filepath"
1617
"runtime"
@@ -152,6 +153,41 @@ func installBinaryToUserLocal() (string, error) {
152153
return targetPath, nil
153154
}
154155

156+
// getUserShell returns the user's current shell, with fallback to /bin/bash
157+
func getUserShell() string {
158+
// Try to get shell from environment variable first
159+
if shell := os.Getenv("SHELL"); shell != "" {
160+
return shell
161+
}
162+
163+
// Fallback: try to get from user database
164+
if currentUser, err := user.Current(); err == nil {
165+
// On Unix systems, we can try to parse /etc/passwd or use getent
166+
if shell := getShellFromUserDB(currentUser.Username); shell != "" {
167+
return shell
168+
}
169+
}
170+
171+
// Ultimate fallback
172+
return "/bin/bash"
173+
}
174+
175+
// getShellFromUserDB tries to get the user's shell from the user database
176+
func getShellFromUserDB(username string) string {
177+
// Try using getent command (works on most Unix systems)
178+
cmd := exec.Command("getent", "passwd", username)
179+
output, err := cmd.Output()
180+
if err == nil {
181+
// Parse passwd entry: username:x:uid:gid:gecos:home:shell
182+
fields := strings.Split(strings.TrimSpace(string(output)), ":")
183+
if len(fields) >= 7 && fields[6] != "" {
184+
return fields[6]
185+
}
186+
}
187+
188+
return ""
189+
}
190+
155191
// getServiceCommand constructs the command line arguments for the service
156192
// by filtering out the --install flag from os.Args
157193
func getServiceCommand() []string {

0 commit comments

Comments
 (0)