Cherry-pick systemd mangle-escape patch

systemd/systemd#5073

Change-Id: Ie0c5736e5ad5a9fff8c0d191852ffb62e0441109
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com?
diff --git a/common/recipes-core/systemd/systemd/0003-basic-Factor-out-string-checking-from-name_to_prefix.patch b/common/recipes-core/systemd/systemd/0003-basic-Factor-out-string-checking-from-name_to_prefix.patch
new file mode 100644
index 0000000..cd4dcad
--- /dev/null
+++ b/common/recipes-core/systemd/systemd/0003-basic-Factor-out-string-checking-from-name_to_prefix.patch
@@ -0,0 +1,103 @@
+From 899173e241f781ad4dfb6b40e7c5f35104164435 Mon Sep 17 00:00:00 2001
+From: Brad Bishop <bradleyb@fuzziesquirrel.com>
+Date: Thu, 12 Jan 2017 09:56:54 -0500
+Subject: [PATCH 1/2] basic: Factor out string checking from
+ name_to_prefix/instance
+
+Add two new functions: string_to_prefix/instance that enable
+prefix/instance extraction from a name before the name is
+mangled.
+
+Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
+---
+ src/basic/unit-name.c | 48 +++++++++++++++++++++++++++++++-----------------
+ 1 file changed, 31 insertions(+), 17 deletions(-)
+
+diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
+index 0a6efa4..93c4838 100644
+--- a/src/basic/unit-name.c
++++ b/src/basic/unit-name.c
+@@ -135,42 +135,46 @@ bool unit_suffix_is_valid(const char *s) {
+         return true;
+ }
+ 
+-int unit_name_to_prefix(const char *n, char **ret) {
++static int string_to_prefix(const char *s, char **ret) {
+         const char *p;
+-        char *s;
++        char *r;
+ 
+-        assert(n);
++        assert(s);
+         assert(ret);
+ 
+-        if (!unit_name_is_valid(n, UNIT_NAME_ANY))
+-                return -EINVAL;
+-
+-        p = strchr(n, '@');
++        p = strchr(s, '@');
+         if (!p)
+-                p = strrchr(n, '.');
++                p = strrchr(s, '.');
+ 
+         assert_se(p);
+ 
+-        s = strndup(n, p - n);
+-        if (!s)
++        r = strndup(s, p - s);
++        if (!r)
+                 return -ENOMEM;
+ 
+-        *ret = s;
++        *ret = r;
+         return 0;
+ }
+ 
+-int unit_name_to_instance(const char *n, char **instance) {
+-        const char *p, *d;
+-        char *i;
+-
++int unit_name_to_prefix(const char *n, char **ret) {
+         assert(n);
+-        assert(instance);
++        assert(ret);
+ 
+         if (!unit_name_is_valid(n, UNIT_NAME_ANY))
+                 return -EINVAL;
+ 
++        return string_to_prefix(n, ret);
++}
++
++static int string_to_instance(const char *s, char **instance) {
++        const char *p, *d;
++        char *i;
++
++        assert(s);
++        assert(instance);
++
+         /* Everything past the first @ and before the last . is the instance */
+-        p = strchr(n, '@');
++        p = strchr(s, '@');
+         if (!p) {
+                 *instance = NULL;
+                 return 0;
+@@ -190,6 +194,16 @@ int unit_name_to_instance(const char *n, char **instance) {
+         return 1;
+ }
+ 
++int unit_name_to_instance(const char *n, char **instance) {
++        assert(n);
++        assert(instance);
++
++        if (!unit_name_is_valid(n, UNIT_NAME_ANY))
++                return -EINVAL;
++
++        return string_to_instance(n, instance);
++}
++
+ int unit_name_to_prefix_and_instance(const char *n, char **ret) {
+         const char *d;
+         char *s;
+-- 
+1.8.3.1
+
diff --git a/common/recipes-core/systemd/systemd/0004-basic-Use-path-escaping-when-mangling-path-instances.patch b/common/recipes-core/systemd/systemd/0004-basic-Use-path-escaping-when-mangling-path-instances.patch
new file mode 100644
index 0000000..fca28de
--- /dev/null
+++ b/common/recipes-core/systemd/systemd/0004-basic-Use-path-escaping-when-mangling-path-instances.patch
@@ -0,0 +1,69 @@
+From b7565b3f2d3b13c4ae5734407a2c3f27658c7b4b Mon Sep 17 00:00:00 2001
+From: Brad Bishop <bradleyb@fuzziesquirrel.com>
+Date: Thu, 12 Jan 2017 08:52:42 -0500
+Subject: [PATCH 2/2] basic: Use path escaping when mangling path instances
+
+Allow path instances with dashes in them to be unescaped
+properly.
+
+Fixes systemd/systemd#5072
+
+Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
+---
+ src/basic/unit-name.c     | 18 ++++++++++++++++++
+ src/test/test-unit-name.c |  3 +++
+ 2 files changed, 21 insertions(+)
+
+diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
+index 93c4838..c91b0e7 100644
+--- a/src/basic/unit-name.c
++++ b/src/basic/unit-name.c
+@@ -684,6 +684,8 @@ static char *do_escape_mangle(const char *f, UnitNameMangle allow_globs, char *t
+  *  If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
+  */
+ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, const char *suffix, char **ret) {
++        _cleanup_free_ char *instance = NULL;
++        _cleanup_free_ char *prefix = NULL;
+         char *s, *t;
+         int r;
+ 
+@@ -723,6 +725,22 @@ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, c
+                         return r;
+         }
+ 
++        r = string_to_instance(name, &instance);
++        if(r < 0 && r != -EINVAL)
++                return r;
++
++        if(instance && path_is_absolute(instance)) {
++                r = string_to_prefix(name, &prefix);
++                if(r < 0 && r != -EINVAL)
++                        return r;
++
++                r = unit_name_from_path_instance(prefix, instance, suffix, ret);
++                if (r >= 0)
++                        return 1;
++                if (r != -EINVAL)
++                        return r;
++        }
++
+         s = new(char, strlen(name) * 4 + strlen(suffix) + 1);
+         if (!s)
+                 return -ENOMEM;
+diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c
+index 2fd83f3..f2cb047 100644
+--- a/src/test/test-unit-name.c
++++ b/src/test/test-unit-name.c
+@@ -192,6 +192,9 @@ static void test_unit_name_mangle(void) {
+         test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo", "foo.service", 1);
+         test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo*", "foo*", 0);
+         test_unit_name_mangle_one(UNIT_NAME_GLOB, "ü*", "\\xc3\\xbc*", 1);
++        test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo@/bar.service", "foo@bar.service", 1);
++        test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo@/bar/baz-boo.service", "foo@bar-baz\\x2dboo.service", 1);
++        test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo@bar/baz-boo.service", "foo@bar-baz-boo.service", 1);
+ }
+ 
+ static int test_unit_printf(void) {
+-- 
+1.8.3.1
+
diff --git a/common/recipes-core/systemd/systemd_%.bbappend b/common/recipes-core/systemd/systemd_%.bbappend
index 5cc25db..ab95deb 100644
--- a/common/recipes-core/systemd/systemd_%.bbappend
+++ b/common/recipes-core/systemd/systemd_%.bbappend
@@ -9,6 +9,8 @@
 SRC_URI += "file://shutdown-watchdog.conf"
 SRC_URI += "file://0001-Export-message_append_cmdline.patch"
 SRC_URI += "file://0002-systemd-Make-pam-compile-shared-library.patch"
+SRC_URI += "file://0003-basic-Factor-out-string-checking-from-name_to_prefix.patch"
+SRC_URI += "file://0004-basic-Use-path-escaping-when-mangling-path-instances.patch"
 
 RRECOMMENDS_${PN} += "obmc-targets"
 FILES_${PN} += "${libdir}/systemd/network/default.network"