github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/exec/look_linux.go (about) 1 // Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 //go:build linux 3 4 package executils 5 6 import ( 7 "golang.org/x/sys/unix" 8 "io/fs" 9 "os" 10 "os/exec" 11 "syscall" 12 ) 13 14 // LookPath customize our LookPath() function, to work in broken $PATH environment variable 15 func LookPath(file string) (string, error) { 16 result, err := exec.LookPath(file) 17 if err == nil && len(result) > 0 { 18 return result, nil 19 } 20 21 // add common dirs contains executable files these may be excluded in $PATH environment variable 22 var binPaths = []string{ 23 "/usr/sbin", 24 "/usr/bin", 25 "/usr/local/sbin", 26 "/usr/local/bin", 27 } 28 29 for _, binPath := range binPaths { 30 var fullPath = binPath + string(os.PathSeparator) + file 31 32 stat, err := os.Stat(fullPath) 33 if err != nil { 34 continue 35 } 36 if stat.IsDir() { 37 return "", syscall.EISDIR 38 } 39 40 var mode = stat.Mode() 41 if mode.IsDir() { 42 return "", syscall.EISDIR 43 } 44 err = syscall.Faccessat(unix.AT_FDCWD, fullPath, unix.X_OK, unix.AT_EACCESS) 45 if err == nil || (err != syscall.ENOSYS && err != syscall.EPERM) { 46 return fullPath, err 47 } 48 if mode&0111 != 0 { 49 return fullPath, nil 50 } 51 return "", fs.ErrPermission 52 } 53 54 return "", &exec.Error{ 55 Name: file, 56 Err: exec.ErrNotFound, 57 } 58 } 59