aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/script-utils/startcbinary/execvp.go
blob: a39f76ebc3b2e98f20479603b3d8c8da627ad288 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Author: arnej

//go:build !windows

package startcbinary

import (
	"fmt"
	"os"
	"strings"

	"github.com/vespa-engine/vespa/client/go/trace"
	"github.com/vespa-engine/vespa/client/go/util"
	"golang.org/x/sys/unix"
)

func findInPath(prog string) string {
	if strings.Contains(prog, "/") {
		return prog
	}
	path := strings.Split(os.Getenv(ENV_PATH), ":")
	for _, dir := range path {
		fn := dir + "/" + prog
		if util.IsRegularFile(fn) {
			return fn
		}
	}
	return prog
}

func myexecvp(prog string, args []string, envv []string) error {
	argv := make([]string, 0, 1+len(args))
	argv = append(argv, prog)
	for _, arg := range args {
		argv = append(argv, arg)
	}
	trace.Trace("run cmd", strings.Join(argv, " "))
	prog = findInPath(prog)
	err := unix.Exec(prog, argv, envv)
	return fmt.Errorf("cannot execute '%s': %v", prog, err)
}