summary refs log tree commit diff stats
path: root/commands.go
blob: 0d645ffb3c3ef055b9cf2b0f285bd2b928c5cb4a (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
// +build openbsd

package lynx

import (
	"fmt"
	"os"
	"strings"

	"golang.org/x/sys/unix"
)

// UnveilCommands takes a slice of commands & unveils them one by one,
// it will return an error if unveil fails at any step. "no such file
// or directory" error is ignored.
func UnveilCommands(commands []string) error {
	// Get $PATH & split it in a list.
	pathList := strings.Split(os.Getenv("PATH"), ":")

	// We work on unveiling each command one by one.
	for _, cmd := range commands {
		// Unveil this command on every PATH.
		for _, path := range pathList {
			err := unix.Unveil(fmt.Sprintf("%s/%s",
				path, cmd), "rx")

			// "no such file or directory" error is
			// ignored because binaries are not placed in
			// every PATH.
			if err != nil && err.Error() != "no such file or directory" {
				// Better error message could be
				// returned like one that includes the
				// path on which unveil failed.
				return err
			}
		}
	}
	// Returning nil because err can be "no such file or
	// directory" which needs to be ignored.
	return nil
}