go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/system/prober/probe_unix.go (about) 1 // Copyright 2017 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build unix 16 // +build unix 17 18 package prober 19 20 import ( 21 "os" 22 "path/filepath" 23 24 "go.chromium.org/luci/common/errors" 25 "go.chromium.org/luci/common/system/environ" 26 ) 27 28 func findExecutable(file string) error { 29 d, err := os.Stat(file) 30 if err != nil { 31 return err 32 } 33 if m := d.Mode(); !m.IsDir() && m&0111 != 0 { 34 return nil 35 } 36 return os.ErrPermission 37 } 38 39 // findInDir is a paraphrased and trimmed version of "exec.LookPath" 40 // (for Windows), 41 // 42 // Copied from: 43 // https://github.com/golang/go/blob/d234f9a75413fdae7643e4be9471b4aeccf02478/src/os/exec/lp_unix.go 44 // 45 // Modified to: 46 // - Use a supplied "dir" instead of scanning through PATH. 47 // - Not consider cases where "file" is an absolute path 48 // - Ignore the possibility that "file" may be in the CWD; only look in "dir". 49 func findInDir(file, dir string, env environ.Env) (string, error) { 50 // NOTE(rsc): I wish we could use the Plan 9 behavior here 51 // (only bypass the path if file begins with / or ./ or ../) 52 // but that would not match all the Unix shells. 53 54 if dir == "" { 55 // Unix shell semantics: path element "" means "." 56 dir = "." 57 } 58 path := filepath.Join(dir, file) 59 if err := findExecutable(path); err == nil { 60 return path, nil 61 } 62 return "", errors.New("not found") 63 }