ResolveExecutablePath resolves the given executable name given the working dir and environment. Returns *ExecutableResolveError when the executable cannot be resolved.
(ctx context.Context, args *kernel.CreateProcessArgs)
| 37 | // dir and environment. |
| 38 | // Returns *ExecutableResolveError when the executable cannot be resolved. |
| 39 | func ResolveExecutablePath(ctx context.Context, args *kernel.CreateProcessArgs) (string, error) { |
| 40 | name := args.Filename |
| 41 | if len(name) == 0 { |
| 42 | if len(args.Argv) == 0 { |
| 43 | return "", fmt.Errorf("no filename or command provided") |
| 44 | } |
| 45 | name = args.Argv[0] |
| 46 | } |
| 47 | |
| 48 | // Absolute paths can be used directly. |
| 49 | if path.IsAbs(name) { |
| 50 | return name, nil |
| 51 | } |
| 52 | |
| 53 | // Paths with '/' in them should be joined to the working directory, or |
| 54 | // to the root if working directory is not set. |
| 55 | if strings.IndexByte(name, '/') > 0 { |
| 56 | wd := args.WorkingDirectory |
| 57 | if wd == "" { |
| 58 | wd = "/" |
| 59 | } |
| 60 | if !path.IsAbs(wd) { |
| 61 | return "", fmt.Errorf("working directory %q must be absolute", wd) |
| 62 | } |
| 63 | return path.Join(wd, name), nil |
| 64 | } |
| 65 | |
| 66 | // Otherwise, We must lookup the name in the paths. |
| 67 | paths := getPath(args.Envv) |
| 68 | f, err := resolve(ctx, args.Credentials, args.MountNamespace, paths, name) |
| 69 | if err != nil { |
| 70 | return "", &ExecutableResolveError{fmt.Errorf("error finding executable %q in PATH %v: %v", name, paths, err)} |
| 71 | } |
| 72 | return f, nil |
| 73 | } |
| 74 | |
| 75 | // resolve searches for an executable in the given PATH directories. |
| 76 | // Error handling follows glibc execvpe() (posix/execvpe.c). |
no test coverage detected
searching dependent graphs…