Source file src/os/exec/lp_unix.go

     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  // LookPath searches for an executable named file in the
    45  // directories named by the PATH environment variable.
    46  // If file contains a slash, it is tried directly and the PATH is not consulted.
    47  // Otherwise, on success, the result is an absolute path.
    48  //
    49  // In older versions of Go, LookPath could return a path relative to the current directory.
    50  // As of Go 1.19, LookPath will instead return that path along with an error satisfying
    51  // [errors.Is](err, [ErrDot]). See the package documentation for more details.
    52  func LookPath(file string) (string, error) {
    53  	// NOTE(rsc): I wish we could use the Plan 9 behavior here
    54  	// (only bypass the path if file begins with / or ./ or ../)
    55  	// but that would not match all the Unix shells.
    56  
    57  	if err := validateLookPath(file); err != nil {
    58  		return "", &Error{file, err}
    59  	}
    60  
    61  	if strings.Contains(file, "/") {
    62  		err := findExecutable(file)
    63  		if err == nil {
    64  			return file, nil
    65  		}
    66  		return "", &Error{file, err}
    67  	}
    68  	path := os.Getenv("PATH")
    69  	for _, dir := range filepath.SplitList(path) {
    70  		if dir == "" {
    71  			// Unix shell semantics: path element "" means "."
    72  			dir = "."
    73  		}
    74  		path := filepath.Join(dir, file)
    75  		if err := findExecutable(path); err == nil {
    76  			if !filepath.IsAbs(path) {
    77  				if execerrdot.Value() != "0" {
    78  					return path, &Error{file, ErrDot}
    79  				}
    80  				execerrdot.IncNonDefault()
    81  			}
    82  			return path, nil
    83  		}
    84  	}
    85  	return "", &Error{file, ErrNotFound}
    86  }
    87  
    88  // lookExtensions is a no-op on non-Windows platforms, since
    89  // they do not restrict executables to specific extensions.
    90  func lookExtensions(path, dir string) (string, error) {
    91  	return path, nil
    92  }
    93  

View as plain text