From f4214bbe77e2c3b2d4e328bde962954388aa3a10 Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <gscrivan@redhat.com>
Date: Tue, 14 Jul 2020 19:15:14 +0200
Subject: [PATCH] config: honor XDG_RUNTIME_DIR

enable to work from a rootless environment.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
---
 README.md                       |  4 ++--
 plugins/meta/dnsname/config.go  | 13 +++++++++++--
 plugins/meta/dnsname/main.go    |  2 +-
 plugins/meta/dnsname/service.go |  2 +-
 4 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index 0e94a5d..1dd84fb 100644
--- a/README.md
+++ b/README.md
@@ -35,8 +35,8 @@ The dnsname plugin can be enabled in the cni network configuration file.
 
 ## DNSMasq configuration files
 The dnsmasq service and its configuration files are considered to be very fluid and are not meant to survive a system
-reboot.  Therefore, files are stored in `/run/containers/cni/dnsname`. The plugin knows to recreate the necessary
-files if it detects they are not present.
+reboot.  Therefore, files are stored in `/run/containers/cni/dnsname`, or under `$XDG_RUNTIME_DIR/containers/cni/dnsname` if
+`XDG_RUNTIME_DIR` is specified.  The plugin knows to recreate the necessary files if it detects they are not present.
 
 ##  DNSMasq default configuration
 Much like the implementation of DNSMasq for libvirt, this plugin will only set up dnsmasq to listen on the network
diff --git a/plugins/meta/dnsname/config.go b/plugins/meta/dnsname/config.go
index 9c1039b..f8fda57 100644
--- a/plugins/meta/dnsname/config.go
+++ b/plugins/meta/dnsname/config.go
@@ -2,13 +2,13 @@ package main
 
 import (
 	"errors"
+	"os"
+	"path/filepath"
 
 	"github.com/containernetworking/cni/pkg/types"
 )
 
 const (
-	//	dnsNameConfPath is where we store the conf, pid, and hosts files
-	dnsNameConfPath = "/run/containers/cni/dnsname"
 	// confFileName is the name of the dns masq conf file
 	confFileName = "dnsmasq.conf"
 	// hostsFileName is the name of the addnhosts file
@@ -53,3 +53,12 @@ type dnsNameFile struct {
 	NetworkInterface string
 	PidFile          string
 }
+
+// dnsNameConfPath tells where we store the conf, pid, and hosts files
+func dnsNameConfPath() string {
+	xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR")
+	if xdgRuntimeDir != "" {
+		return filepath.Join(xdgRuntimeDir, "containers/cni/dnsname")
+	}
+	return "/run/containers/cni/dnsname"
+}
diff --git a/plugins/meta/dnsname/main.go b/plugins/meta/dnsname/main.go
index 114b16b..271d495 100644
--- a/plugins/meta/dnsname/main.go
+++ b/plugins/meta/dnsname/main.go
@@ -193,7 +193,7 @@ func cmdCheck(args *skel.CmdArgs) error {
 		return errors.Errorf("dnsmasq instance not running")
 	}
 	// Above will make sure the pidfile exists
-	files, err := ioutil.ReadDir(dnsNameConfPath)
+	files, err := ioutil.ReadDir(dnsNameConfPath())
 	if err != nil {
 		return err
 	}
diff --git a/plugins/meta/dnsname/service.go b/plugins/meta/dnsname/service.go
index c4b8b0f..717d6c0 100644
--- a/plugins/meta/dnsname/service.go
+++ b/plugins/meta/dnsname/service.go
@@ -96,5 +96,5 @@ func (d dnsNameFile) getProcess() (*os.Process, error) {
 func makePath(networkName, fileName string) string {
 	// the generic path for where conf, host, pid files are kept is:
 	// /run/containers/cni/dnsmasq/<network-name>/
-	return filepath.Join(dnsNameConfPath, networkName, fileName)
+	return filepath.Join(dnsNameConfPath(), networkName, fileName)
 }
-- 
GitLab