From 09e64a527745d86a36aa45fb5ccc1f27ac25f04d Mon Sep 17 00:00:00 2001 From: Andinus Date: Mon, 6 Apr 2020 12:36:14 +0530 Subject: Initial Commit --- commands.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 commands.go (limited to 'commands.go') diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..5d57546 --- /dev/null +++ b/commands.go @@ -0,0 +1,39 @@ +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 +} -- cgit 1.4.1-2-gfad0