summary refs log tree commit diff stats
path: root/commands.go
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-04-06 12:36:14 +0530
committerAndinus <andinus@nand.sh>2020-04-06 12:36:14 +0530
commit09e64a527745d86a36aa45fb5ccc1f27ac25f04d (patch)
treebc50c42fc00a634d27eb8bedf023385417c10998 /commands.go
downloadlynx-09e64a527745d86a36aa45fb5ccc1f27ac25f04d.tar.gz
Initial Commit v0.1.0
Diffstat (limited to 'commands.go')
-rw-r--r--commands.go39
1 files changed, 39 insertions, 0 deletions
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
+}