richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
a4b143
From a6b1359b406410ca48160deb442a62f73f3b7508 Mon Sep 17 00:00:00 2001
a4b143
From: Dave Reisner <dreisner@archlinux.org>
a4b143
Date: Wed, 18 Sep 2013 12:12:04 -0400
a4b143
Subject: [PATCH] device-nodes: move device node specific code to own file
a4b143
a4b143
In the process, rename udev_encode_string which is poorly named for what
a4b143
it does. It deals specifically with encoding names that udev creates and
a4b143
has its own rules: utf8 is valid but some ascii is not (e.g. path
a4b143
separators), and everything else is simply escaped. Rename it to
a4b143
encode_devnode_name.
a4b143
---
a4b143
 .gitignore                   |  1 +
a4b143
 Makefile.am                  | 14 ++++++++-
a4b143
 src/libudev/libudev-util.c   |  5 +--
a4b143
 src/shared/device-nodes.c    | 74 ++++++++++++++++++++++++++++++++++++++++++++
a4b143
 src/shared/device-nodes.h    | 23 ++++++++++++++
a4b143
 src/shared/utf8.c            | 46 ---------------------------
a4b143
 src/shared/utf8.h            |  2 --
a4b143
 src/shared/util.c            |  4 +--
a4b143
 src/test/test-device-nodes.c | 55 ++++++++++++++++++++++++++++++++
a4b143
 src/test/test-utf8.c         | 28 +----------------
a4b143
 10 files changed, 172 insertions(+), 80 deletions(-)
a4b143
 create mode 100644 src/shared/device-nodes.c
a4b143
 create mode 100644 src/shared/device-nodes.h
a4b143
 create mode 100644 src/test/test-device-nodes.c
a4b143
a4b143
diff --git a/.gitignore b/.gitignore
a4b143
index deeee53..8115d4d 100644
a4b143
--- a/.gitignore
a4b143
+++ b/.gitignore
a4b143
@@ -101,6 +101,7 @@
a4b143
 /test-cgroup-util
a4b143
 /test-daemon
a4b143
 /test-date
a4b143
+/test-device-nodes
a4b143
 /test-efivars
a4b143
 /test-engine
a4b143
 /test-env-replace
a4b143
diff --git a/Makefile.am b/Makefile.am
a4b143
index 6dd33ad..aa16522 100644
a4b143
--- a/Makefile.am
a4b143
+++ b/Makefile.am
a4b143
@@ -640,6 +640,8 @@ libsystemd_shared_la_SOURCES = \
a4b143
 	src/shared/list.h \
a4b143
 	src/shared/macro.h \
a4b143
 	src/shared/def.h \
a4b143
+	src/shared/device-nodes.c \
a4b143
+	src/shared/device-nodes.h \
a4b143
 	src/shared/sparse-endian.h \
a4b143
 	src/shared/util.c \
a4b143
 	src/shared/util.h \
a4b143
@@ -1135,7 +1137,8 @@ tests += \
a4b143
 	test-time \
a4b143
 	test-hashmap \
a4b143
 	test-list \
a4b143
-	test-tables
a4b143
+	test-tables \
a4b143
+	test-device-nodes
a4b143
 
a4b143
 EXTRA_DIST += \
a4b143
 	test/sched_idle_bad.service \
a4b143
@@ -1147,6 +1150,15 @@ EXTRA_DIST += \
a4b143
 EXTRA_DIST += \
a4b143
 	src/test/test-helper.h
a4b143
 
a4b143
+test_device_nodes_SOURCES = \
a4b143
+	src/test/test-device-nodes.c
a4b143
+
a4b143
+test_device_nodes_CFLAGS = \
a4b143
+	$(AM_CFLAGS)
a4b143
+
a4b143
+test_device_nodes_LDADD = \
a4b143
+	libsystemd-shared.la
a4b143
+
a4b143
 test_engine_SOURCES = \
a4b143
 	src/test/test-engine.c
a4b143
 
a4b143
diff --git a/src/libudev/libudev-util.c b/src/libudev/libudev-util.c
a4b143
index d54430c..b5b9db6 100644
a4b143
--- a/src/libudev/libudev-util.c
a4b143
+++ b/src/libudev/libudev-util.c
a4b143
@@ -32,6 +32,7 @@
a4b143
 #include <sys/stat.h>
a4b143
 #include <sys/param.h>
a4b143
 
a4b143
+#include "device-nodes.h"
a4b143
 #include "libudev.h"
a4b143
 #include "libudev-private.h"
a4b143
 #include "utf8.h"
a4b143
@@ -344,7 +345,7 @@ int util_replace_chars(char *str, const char *white)
a4b143
         while (str[i] != '\0') {
a4b143
                 int len;
a4b143
 
a4b143
-                if (is_utf8_encoding_whitelisted(str[i], white)) {
a4b143
+                if (whitelisted_char_for_devnode(str[i], white)) {
a4b143
                         i++;
a4b143
                         continue;
a4b143
                 }
a4b143
@@ -392,7 +393,7 @@ int util_replace_chars(char *str, const char *white)
a4b143
  **/
a4b143
 _public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len)
a4b143
 {
a4b143
-        return udev_encode_string(str, str_enc, len);
a4b143
+        return encode_devnode_name(str, str_enc, len);
a4b143
 }
a4b143
 
a4b143
 /*
a4b143
diff --git a/src/shared/device-nodes.c b/src/shared/device-nodes.c
a4b143
new file mode 100644
a4b143
index 0000000..986553e
a4b143
--- /dev/null
a4b143
+++ b/src/shared/device-nodes.c
a4b143
@@ -0,0 +1,74 @@
a4b143
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
a4b143
+
a4b143
+/***
a4b143
+  This file is part of systemd.
a4b143
+
a4b143
+  Copyright 2012 Lennart Poettering
a4b143
+
a4b143
+  systemd is free software; you can redistribute it and/or modify it
a4b143
+  under the terms of the GNU Lesser General Public License as published by
a4b143
+  the Free Software Foundation; either version 2.1 of the License, or
a4b143
+  (at your option) any later version.
a4b143
+
a4b143
+  systemd is distributed in the hope that it will be useful, but
a4b143
+  WITHOUT ANY WARRANTY; without even the implied warranty of
a4b143
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a4b143
+  Lesser General Public License for more details.
a4b143
+
a4b143
+  You should have received a copy of the GNU Lesser General Public License
a4b143
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
a4b143
+***/
a4b143
+
a4b143
+#include <stdlib.h>
a4b143
+#include <stdio.h>
a4b143
+#include <stdint.h>
a4b143
+#include <sys/types.h>
a4b143
+
a4b143
+#include "device-nodes.h"
a4b143
+#include "utf8.h"
a4b143
+
a4b143
+int whitelisted_char_for_devnode(char c, const char *white) {
a4b143
+        if ((c >= '0' && c <= '9') ||
a4b143
+            (c >= 'A' && c <= 'Z') ||
a4b143
+            (c >= 'a' && c <= 'z') ||
a4b143
+            strchr("#+-.:=@_", c) != NULL ||
a4b143
+            (white != NULL && strchr(white, c) != NULL))
a4b143
+                return 1;
a4b143
+        return 0;
a4b143
+}
a4b143
+
a4b143
+int encode_devnode_name(const char *str, char *str_enc, size_t len) {
a4b143
+        size_t i, j;
a4b143
+
a4b143
+        if (str == NULL || str_enc == NULL)
a4b143
+                return -1;
a4b143
+
a4b143
+        for (i = 0, j = 0; str[i] != '\0'; i++) {
a4b143
+                int seqlen;
a4b143
+
a4b143
+                seqlen = utf8_encoded_valid_unichar(&str[i]);
a4b143
+                if (seqlen > 1) {
a4b143
+                        if (len-j < (size_t)seqlen)
a4b143
+                                goto err;
a4b143
+                        memcpy(&str_enc[j], &str[i], seqlen);
a4b143
+                        j += seqlen;
a4b143
+                        i += (seqlen-1);
a4b143
+                } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i], NULL)) {
a4b143
+                        if (len-j < 4)
a4b143
+                                goto err;
a4b143
+                        sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
a4b143
+                        j += 4;
a4b143
+                } else {
a4b143
+                        if (len-j < 1)
a4b143
+                                goto err;
a4b143
+                        str_enc[j] = str[i];
a4b143
+                        j++;
a4b143
+                }
a4b143
+        }
a4b143
+        if (len-j < 1)
a4b143
+                goto err;
a4b143
+        str_enc[j] = '\0';
a4b143
+        return 0;
a4b143
+err:
a4b143
+        return -1;
a4b143
+}
a4b143
diff --git a/src/shared/device-nodes.h b/src/shared/device-nodes.h
a4b143
new file mode 100644
a4b143
index 0000000..a98195a
a4b143
--- /dev/null
a4b143
+++ b/src/shared/device-nodes.h
a4b143
@@ -0,0 +1,23 @@
a4b143
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
a4b143
+
a4b143
+/***
a4b143
+  This file is part of systemd.
a4b143
+
a4b143
+  Copyright 2012 Lennart Poettering
a4b143
+
a4b143
+  systemd is free software; you can redistribute it and/or modify it
a4b143
+  under the terms of the GNU Lesser General Public License as published by
a4b143
+  the Free Software Foundation; either version 2.1 of the License, or
a4b143
+  (at your option) any later version.
a4b143
+
a4b143
+  systemd is distributed in the hope that it will be useful, but
a4b143
+  WITHOUT ANY WARRANTY; without even the implied warranty of
a4b143
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a4b143
+  Lesser General Public License for more details.
a4b143
+
a4b143
+  You should have received a copy of the GNU Lesser General Public License
a4b143
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
a4b143
+***/
a4b143
+
a4b143
+int encode_devnode_name(const char *str, char *str_enc, size_t len);
a4b143
+int whitelisted_char_for_devnode(char c, const char *additional);
a4b143
diff --git a/src/shared/utf8.c b/src/shared/utf8.c
a4b143
index 732f0f0..c3d97cc 100644
a4b143
--- a/src/shared/utf8.c
a4b143
+++ b/src/shared/utf8.c
a4b143
@@ -285,49 +285,3 @@ int utf8_encoded_valid_unichar(const char *str) {
a4b143
 
a4b143
         return len;
a4b143
 }
a4b143
-
a4b143
-int is_utf8_encoding_whitelisted(char c, const char *white) {
a4b143
-        if ((c >= '0' && c <= '9') ||
a4b143
-            (c >= 'A' && c <= 'Z') ||
a4b143
-            (c >= 'a' && c <= 'z') ||
a4b143
-            strchr("#+-.:=@_", c) != NULL ||
a4b143
-            (white != NULL && strchr(white, c) != NULL))
a4b143
-                return 1;
a4b143
-        return 0;
a4b143
-}
a4b143
-
a4b143
-int udev_encode_string(const char *str, char *str_enc, size_t len) {
a4b143
-        size_t i, j;
a4b143
-
a4b143
-        if (str == NULL || str_enc == NULL)
a4b143
-                return -1;
a4b143
-
a4b143
-        for (i = 0, j = 0; str[i] != '\0'; i++) {
a4b143
-                int seqlen;
a4b143
-
a4b143
-                seqlen = utf8_encoded_valid_unichar(&str[i]);
a4b143
-                if (seqlen > 1) {
a4b143
-                        if (len-j < (size_t)seqlen)
a4b143
-                                goto err;
a4b143
-                        memcpy(&str_enc[j], &str[i], seqlen);
a4b143
-                        j += seqlen;
a4b143
-                        i += (seqlen-1);
a4b143
-                } else if (str[i] == '\\' || !is_utf8_encoding_whitelisted(str[i], NULL)) {
a4b143
-                        if (len-j < 4)
a4b143
-                                goto err;
a4b143
-                        sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
a4b143
-                        j += 4;
a4b143
-                } else {
a4b143
-                        if (len-j < 1)
a4b143
-                                goto err;
a4b143
-                        str_enc[j] = str[i];
a4b143
-                        j++;
a4b143
-                }
a4b143
-        }
a4b143
-        if (len-j < 1)
a4b143
-                goto err;
a4b143
-        str_enc[j] = '\0';
a4b143
-        return 0;
a4b143
-err:
a4b143
-        return -1;
a4b143
-}
a4b143
diff --git a/src/shared/utf8.h b/src/shared/utf8.h
a4b143
index 22e1346..96a03ea 100644
a4b143
--- a/src/shared/utf8.h
a4b143
+++ b/src/shared/utf8.h
a4b143
@@ -35,5 +35,3 @@ char *ascii_filter(const char *s);
a4b143
 char *utf16_to_utf8(const void *s, size_t length);
a4b143
 
a4b143
 int utf8_encoded_valid_unichar(const char *str);
a4b143
-int is_utf8_encoding_whitelisted(char c, const char *white);
a4b143
-int udev_encode_string(const char *str, char *str_enc, size_t len);
a4b143
diff --git a/src/shared/util.c b/src/shared/util.c
a4b143
index 4f80cc8..4711d91 100644
a4b143
--- a/src/shared/util.c
a4b143
+++ b/src/shared/util.c
a4b143
@@ -73,7 +73,7 @@
a4b143
 #include "hashmap.h"
a4b143
 #include "env-util.h"
a4b143
 #include "fileio.h"
a4b143
-#include "utf8.h"
a4b143
+#include "device-nodes.h"
a4b143
 
a4b143
 int saved_argc = 0;
a4b143
 char **saved_argv = NULL;
a4b143
@@ -3509,7 +3509,7 @@ static char *tag_to_udev_node(const char *tagvalue, const char *by) {
a4b143
         if (t == NULL)
a4b143
                 return NULL;
a4b143
 
a4b143
-        if (udev_encode_string(u, t, enc_len) < 0)
a4b143
+        if (encode_devnode_name(u, t, enc_len) < 0)
a4b143
                 return NULL;
a4b143
 
a4b143
         if (asprintf(&dn, "/dev/disk/by-%s/%s", by, t) < 0)
a4b143
diff --git a/src/test/test-device-nodes.c b/src/test/test-device-nodes.c
a4b143
new file mode 100644
a4b143
index 0000000..2f3dedb
a4b143
--- /dev/null
a4b143
+++ b/src/test/test-device-nodes.c
a4b143
@@ -0,0 +1,55 @@
a4b143
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
a4b143
+
a4b143
+/***
a4b143
+  This file is part of systemd.
a4b143
+
a4b143
+  Copyright 2013 Dave Reisner
a4b143
+
a4b143
+  systemd is free software; you can redistribute it and/or modify it
a4b143
+  under the terms of the GNU Lesser General Public License as published by
a4b143
+  the Free Software Foundation; either version 2.1 of the License, or
a4b143
+  (at your option) any later version.
a4b143
+
a4b143
+  systemd is distributed in the hope that it will be useful, but
a4b143
+  WITHOUT ANY WARRANTY; without even the implied warranty of
a4b143
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a4b143
+  Lesser General Public License for more details.
a4b143
+
a4b143
+  You should have received a copy of the GNU Lesser General Public License
a4b143
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
a4b143
+***/
a4b143
+
a4b143
+#include <sys/types.h>
a4b143
+
a4b143
+#include "device-nodes.h"
a4b143
+#include "util.h"
a4b143
+
a4b143
+/* helpers for test_encode_devnode_name */
a4b143
+static char *do_encode_string(const char *in) {
a4b143
+        size_t out_len = strlen(in) * 4;
a4b143
+        char *out = malloc(out_len);
a4b143
+
a4b143
+        assert_se(out);
a4b143
+        assert_se(encode_devnode_name(in, out, out_len) >= 0);
a4b143
+        puts(out);
a4b143
+
a4b143
+        return out;
a4b143
+}
a4b143
+
a4b143
+static bool expect_encoded_as(const char *in, const char *expected) {
a4b143
+        _cleanup_free_ char *encoded = do_encode_string(in);
a4b143
+        return streq(encoded, expected);
a4b143
+}
a4b143
+
a4b143
+static void test_encode_devnode_name(void) {
a4b143
+        assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks"));
a4b143
+        assert_se(expect_encoded_as("pinkiepie", "pinkiepie"));
a4b143
+        assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8"));
a4b143
+        assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng"));
a4b143
+}
a4b143
+
a4b143
+int main(int argc, char *argv[]) {
a4b143
+        test_encode_devnode_name();
a4b143
+
a4b143
+        return 0;
a4b143
+}
a4b143
diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c
a4b143
index 26cc37b..b5a833e 100644
a4b143
--- a/src/test/test-utf8.c
a4b143
+++ b/src/test/test-utf8.c
a4b143
@@ -19,34 +19,9 @@
a4b143
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
a4b143
 ***/
a4b143
 
a4b143
-
a4b143
 #include "utf8.h"
a4b143
 #include "util.h"
a4b143
 
a4b143
-/* helpers for test_udev_encode_string */
a4b143
-static char *do_encode_string(const char *in) {
a4b143
-        size_t out_len = strlen(in) * 4;
a4b143
-        char *out = malloc(out_len);
a4b143
-
a4b143
-        assert_se(out);
a4b143
-        assert_se(udev_encode_string(in, out, out_len) >= 0);
a4b143
-        puts(out);
a4b143
-
a4b143
-        return out;
a4b143
-}
a4b143
-
a4b143
-static bool expect_encoded_as(const char *in, const char *expected) {
a4b143
-        _cleanup_free_ char *encoded = do_encode_string(in);
a4b143
-        return streq(encoded, expected);
a4b143
-}
a4b143
-
a4b143
-static void test_udev_encode_string(void) {
a4b143
-        assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks"));
a4b143
-        assert_se(expect_encoded_as("pinkiepie", "pinkiepie"));
a4b143
-        assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8"));
a4b143
-        assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng"));
a4b143
-}
a4b143
-
a4b143
 static void test_utf8_is_printable(void) {
a4b143
         assert_se(utf8_is_printable("ascii is valid\tunicode", 22));
a4b143
         assert_se(utf8_is_printable("\342\204\242", 3));
a4b143
@@ -55,14 +30,13 @@ static void test_utf8_is_printable(void) {
a4b143
 
a4b143
 static void test_utf8_is_valid(void) {
a4b143
         assert_se(utf8_is_valid("ascii is valid unicode"));
a4b143
-        assert_se(utf8_is_valid("\341\204\242"));
a4b143
+        assert_se(utf8_is_valid("\342\204\242"));
a4b143
         assert_se(!utf8_is_valid("\341\204"));
a4b143
 }
a4b143
 
a4b143
 int main(int argc, char *argv[]) {
a4b143
         test_utf8_is_valid();
a4b143
         test_utf8_is_printable();
a4b143
-        test_udev_encode_string();
a4b143
 
a4b143
         return 0;
a4b143
 }