Skip to content

Commit 2fb08c8

Browse files
committed
fix: Show informative error for non-executable exec target
When the target executable of `singularity exec` lacks `+x` permissions, show a sensible error message rather than `permission denied`. Fixes #607
1 parent 20a0c17 commit 2fb08c8

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
- Support nvidia-container-cli v1.8.0 and above, via fix to capability set.
4949
- Do not truncate environment variables with commas
50+
- Show an informative error when `exec` target is not executable.
5051

5152
## v3.9.6 \[2022-03-10\]
5253

Diff for: e2e/actions/actions.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ func (c actionTests) actionExec(t *testing.T) {
109109
homePath := filepath.Join("/home", basename)
110110

111111
tests := []struct {
112-
name string
113-
argv []string
114-
exit int
112+
name string
113+
argv []string
114+
exit int
115+
errorContains string
115116
}{
116117
{
117118
name: "NoCommand",
@@ -250,17 +251,34 @@ func (c actionTests) actionExec(t *testing.T) {
250251
argv: []string{"--no-home", c.env.ImagePath, "ls", "-ld", user.Dir},
251252
exit: 1,
252253
},
254+
{
255+
name: "NotExecutable",
256+
argv: []string{c.env.ImagePath, "/etc/hosts"},
257+
exit: 1,
258+
errorContains: "/etc/hosts is not executable, please check permissions",
259+
},
260+
{
261+
name: "ShellBuiltin",
262+
argv: []string{c.env.ImagePath, "echo", "hello"},
263+
exit: 1,
264+
errorContains: "If it is a shell built-in, specify an executable instead",
265+
},
253266
}
254267

255268
for _, tt := range tests {
269+
resultOps := []e2e.SingularityCmdResultOp{}
270+
if tt.errorContains != "" {
271+
resultOps = append(resultOps, e2e.ExpectError(e2e.ContainMatch, tt.errorContains))
272+
}
273+
256274
c.env.RunSingularity(
257275
t,
258276
e2e.AsSubtest(tt.name),
259277
e2e.WithProfile(e2e.UserProfile),
260278
e2e.WithCommand("exec"),
261279
e2e.WithDir("/tmp"),
262280
e2e.WithArgs(tt.argv...),
263-
e2e.ExpectExit(tt.exit),
281+
e2e.ExpectExit(tt.exit, resultOps...),
264282
)
265283
}
266284
}

Diff for: internal/pkg/util/fs/files/action_scripts.go

+9
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ sylog debug "Running action command ${__singularity_cmd__}"
157157
158158
case "${__singularity_cmd__}" in
159159
exec)
160+
execbin=$(command -v "$1")
161+
if test $? -ne 0; then
162+
sylog error "$1 not found."
163+
exit 1
164+
fi
165+
if test ! -x "$execbin"; then
166+
sylog error "$1 is not executable, please check permissions. If it is a shell built-in, specify an executable instead. E.g. use /bin/echo instead of echo."
167+
exit 1
168+
fi
160169
exec "$@" ;;
161170
shell)
162171
if test -n "${SINGULARITY_SHELL:-}" -a -x "${SINGULARITY_SHELL:-}"; then

0 commit comments

Comments
 (0)