| 1 |
// Copyright 2010 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build unix |
| 6 |
|
| 7 |
package exec |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"internal/syscall/unix" |
| 12 |
"io/fs" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"strings" |
| 16 |
"syscall" |
| 17 |
) |
| 18 |
|
| 19 |
// ErrNotFound is the error resulting if a path search failed to find an executable file. |
| 20 |
var ErrNotFound = errors.New("executable file not found in $PATH") |
| 21 |
|
| 22 |
func findExecutable(file string) error { |
| 23 |
d, err := os.Stat(file) |
| 24 |
if err != nil { |
| 25 |
return err |
| 26 |
} |
| 27 |
m := d.Mode() |
| 28 |
if m.IsDir() { |
| 29 |
return syscall.EISDIR |
| 30 |
} |
| 31 |
err = unix.Eaccess(file, unix.X_OK) |
| 32 |
// ENOSYS means Eaccess is not available or not implemented. |
| 33 |
// EPERM can be returned by Linux containers employing seccomp. |
| 34 |
// In both cases, fall back to checking the permission bits. |
| 35 |
if err == nil || (err != syscall.ENOSYS && err != syscall.EPERM) { |
| 36 |
return err |
| 37 |
} |
| 38 |
if m&0111 != 0 { |
| 39 |
return nil |
| 40 |
} |
| 41 |
return fs.ErrPermission |
| 42 |
} |
| 43 |
|
| 44 |
func lookPath(file string) (string, error) { |
| 45 |
// NOTE(rsc): I wish we could use the Plan 9 behavior here |
| 46 |
// (only bypass the path if file begins with / or ./ or ../) |
| 47 |
// but that would not match all the Unix shells. |
| 48 |
|
| 49 |
if err := validateLookPath(file); err != nil { |
| 50 |
return "", &Error{file, err} |
| 51 |
} |
| 52 |
|
| 53 |
if strings.Contains(file, "/") { |
| 54 |
err := findExecutable(file) |
| 55 |
if err == nil { |
| 56 |
return file, nil |
| 57 |
} |
| 58 |
return "", &Error{file, err} |
| 59 |
} |
| 60 |
path := os.Getenv("PATH") |
| 61 |
for _, dir := range filepath.SplitList(path) { |
| 62 |
if dir == "" { |
| 63 |
// Unix shell semantics: path element "" means "." |
| 64 |
dir = "." |
| 65 |
} |
| 66 |
path := filepath.Join(dir, file) |
| 67 |
if err := findExecutable(path); err == nil { |
| 68 |
if !filepath.IsAbs(path) { |
| 69 |
if execerrdot.Value() != "0" { |
| 70 |
return path, &Error{file, ErrDot} |
| 71 |
} |
| 72 |
execerrdot.IncNonDefault() |
| 73 |
} |
| 74 |
return path, nil |
| 75 |
} |
| 76 |
} |
| 77 |
return "", &Error{file, ErrNotFound} |
| 78 |
} |
| 79 |
|
| 80 |
// lookExtensions is a no-op on non-Windows platforms, since |
| 81 |
// they do not restrict executables to specific extensions. |
| 82 |
func lookExtensions(path, dir string) (string, error) { |
| 83 |
return path, nil |
| 84 |
} |