[lxc-devel] [lxd/master] shared/util: More snap handling logic

stgraber on Github lxc-bot at linuxcontainers.org
Fri Oct 13 23:57:48 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171013/d0742728/attachment.bin>
-------------- next part --------------
From 60321ae737a0377ca611f61b4b6454b4ce858242 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 13 Oct 2017 19:57:17 -0400
Subject: [PATCH] shared/util: More snap handling logic
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 shared/util.go | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/shared/util.go b/shared/util.go
index 260b32c8a..dd091c86e 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -104,8 +104,8 @@ func IsUnixSocket(path string) bool {
 // On a normal system, this does nothing
 // When inside of a snap environment, returns the real path
 func HostPath(path string) string {
-	// Ignore relative paths
-	if len(path) == 0 || path[0] != os.PathSeparator {
+	// Ignore empty paths
+	if len(path) == 0  {
 		return path
 	}
 
@@ -115,6 +115,22 @@ func HostPath(path string) string {
 		return path
 	}
 
+	// Handle relative paths
+	if path[0] != os.PathSeparator {
+		// Use the cwd of the parent as snap-confine alters our own cwd on launch
+		ppid := os.Getppid()
+		if ppid < 1 {
+			return path
+		}
+
+		pwd, err := os.Readlink(fmt.Sprintf("/proc/%d/cwd", ppid))
+		if err != nil {
+			return path
+		}
+
+		path = filepath.Clean(strings.Join([]string{pwd, path}, string(os.PathSeparator)))
+	}
+
 	// Check if the path is already snap-aware
 	for _, prefix := range []string{"/snap", "/var/snap", "/var/lib/snapd"} {
 		if strings.HasPrefix(path, prefix) {


More information about the lxc-devel mailing list