On Fri, Jul 26, 2013 at 5:48 PM, Daniel P. Berrange <berrange(a)redhat.com> wrote:
From: "Daniel P. Berrange" <berrange(a)redhat.com>
There are some interesting escaping rules to consider when dealing
with systemd slice/scope names. Thus it is helpful to have APIs
for formatting names
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/libvirt_private.syms | 2 ++
src/util/virsystemd.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++--
src/util/virsystemd.h | 5 +++
tests/virsystemdtest.c | 48 +++++++++++++++++++++++++
4 files changed, 144 insertions(+), 2 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index d9615ea..0247a46 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1935,6 +1935,8 @@ virSysinfoSetup;
# util/virsystemd.h
virSystemdCreateMachine;
+virSystemdMakeScopeName;
+virSystemdMakeSliceName;
# util/virthread.h
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
index 11d1153..251b846 100644
--- a/src/util/virsystemd.c
+++ b/src/util/virsystemd.c
@@ -27,9 +27,96 @@
#include "viralloc.h"
#include "virutil.h"
#include "virlog.h"
+#include "virerror.h"
#define VIR_FROM_THIS VIR_FROM_SYSTEMD
+
+static void virSystemdEscapeName(virBufferPtr buf,
+ const char *name)
+{
+ static const char hextable[16] = "0123456789abcdef";
+
+#define ESCAPE(c) \
+ do { \
+ virBufferAddChar(buf, '\\'); \
+ virBufferAddChar(buf, 'x'); \
+ virBufferAddChar(buf, hextable[(c >> 4) & 15]); \
+ virBufferAddChar(buf, hextable[c & 15]); \
+ } while (0)
+
+#define VALID_CHARS \
+ "0123456789" \
+ "abcdefghijklmnopqrstuvwxyz" \
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
+ ":-_.\\"
+
+ if (*name == '.') {
+ ESCAPE(*name);
+ name++;
+ }
+
+ while (*name) {
+ if (*name == '/')
+ virBufferAddChar(buf, '-');
+ else if (*name == '-' ||
+ *name == '\\' ||
+ !strchr(VALID_CHARS, *name))
+ ESCAPE(*name);
+ else
+ virBufferAddChar(buf, *name);
+ name++;
+ }
+
+#undef ESCAPE
+#undef VALID_CHARS
+}
+
+
+char *virSystemdMakeScopeName(const char *name,
+ const char *drivername,
+ const char *partition)
+{
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+ if (*partition == '/')
+ partition++;
+
+ virSystemdEscapeName(&buf, partition);
+ virBufferAddChar(&buf, '-');
+ virSystemdEscapeName(&buf, drivername);
+ virBufferAddLit(&buf, "\\x2d");
What is the idea behind this?
Now we end up with paths like:
/sys/fs/cgroup/memory/machine.slice/machine-lxc\x2dmyfunnycontainer.scope
--
Thanks,
//richard