[systemd-commits] 2 commits - Makefile.am man/udev.xml src/shared src/sysctl src/udev

Kay Sievers kay at kemper.freedesktop.org
Wed Mar 11 03:17:19 PDT 2015


 Makefile.am              |    4 +-
 man/udev.xml             |   12 ++++++
 src/shared/sysctl-util.c |   88 +++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/sysctl-util.h |   27 ++++++++++++++
 src/sysctl/sysctl.c      |   66 +++++------------------------------
 src/udev/udev-rules.c    |   61 +++++++++++++++++++++++++++++++-
 6 files changed, 200 insertions(+), 58 deletions(-)

New commits:
commit f4cf2e5b2f19d6dadb8c046dc545d222280ffafb
Author: Kay Sievers <kay at vrfy.org>
Date:   Wed Mar 11 11:15:53 2015 +0100

    udev: add SYSCTL{} support

diff --git a/man/udev.xml b/man/udev.xml
index a948ea7..d1a4f61 100644
--- a/man/udev.xml
+++ b/man/udev.xml
@@ -193,6 +193,11 @@
             value itself contains trailing whitespace.
             </para>
           </listitem>
+          <term><varname>SYSCTL{<replaceable>kernel parameter</replaceable>}</varname></term>
+          <listitem>
+            <para>Match a kernel parameter value.
+            </para>
+          </listitem>
         </varlistentry>
 
         <varlistentry>
@@ -375,6 +380,13 @@
         </varlistentry>
 
         <varlistentry>
+          <term><varname>SYSCTL{<replaceable>kernel parameter</replaceable>}</varname></term>
+          <listitem>
+            <para>The value that should be written to kernel parameter.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
           <term><varname>ENV{<replaceable>key</replaceable>}</varname></term>
           <listitem>
             <para>Set a device property value. Property names with a leading <literal>.</literal>
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
index e8387d7..4262119 100644
--- a/src/udev/udev-rules.c
+++ b/src/udev/udev-rules.c
@@ -35,6 +35,7 @@
 #include "strbuf.h"
 #include "strv.h"
 #include "util.h"
+#include "sysctl-util.h"
 
 #define PREALLOC_TOKEN          2048
 
@@ -128,6 +129,7 @@ enum token_type {
         TK_M_DRIVER,                    /* val */
         TK_M_WAITFOR,                   /* val */
         TK_M_ATTR,                      /* val, attr */
+        TK_M_SYSCTL,                    /* val, attr */
 
         TK_M_PARENTS_MIN,
         TK_M_KERNELS,                   /* val */
@@ -166,6 +168,7 @@ enum token_type {
         TK_A_NAME,                      /* val */
         TK_A_DEVLINK,                   /* val */
         TK_A_ATTR,                      /* val, attr */
+        TK_A_SYSCTL,                    /* val, attr */
         TK_A_RUN_BUILTIN,               /* val, bool */
         TK_A_RUN_PROGRAM,               /* val, bool */
         TK_A_GOTO,                      /* size_t */
@@ -262,6 +265,7 @@ static const char *token_str(enum token_type type) {
                 [TK_M_DRIVER] =                 "M DRIVER",
                 [TK_M_WAITFOR] =                "M WAITFOR",
                 [TK_M_ATTR] =                   "M ATTR",
+                [TK_M_SYSCTL] =                 "M SYSCTL",
 
                 [TK_M_PARENTS_MIN] =            "M PARENTS_MIN",
                 [TK_M_KERNELS] =                "M KERNELS",
@@ -300,6 +304,7 @@ static const char *token_str(enum token_type type) {
                 [TK_A_NAME] =                   "A NAME",
                 [TK_A_DEVLINK] =                "A DEVLINK",
                 [TK_A_ATTR] =                   "A ATTR",
+                [TK_A_SYSCTL] =                 "A SYSCTL",
                 [TK_A_RUN_BUILTIN] =            "A RUN_BUILTIN",
                 [TK_A_RUN_PROGRAM] =            "A RUN_PROGRAM",
                 [TK_A_GOTO] =                   "A GOTO",
@@ -363,9 +368,11 @@ static void dump_token(struct udev_rules *rules, struct token *token) {
                 log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value);
                 break;
         case TK_M_ATTR:
+        case TK_M_SYSCTL:
         case TK_M_ATTRS:
         case TK_M_ENV:
         case TK_A_ATTR:
+        case TK_A_SYSCTL:
         case TK_A_ENV:
                 log_debug("%s %s '%s' '%s'(%s)",
                           token_str(type), operation_str(op), attr, value, string_glob_str(glob));
@@ -897,8 +904,10 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
                 break;
         case TK_M_ENV:
         case TK_M_ATTR:
+        case TK_M_SYSCTL:
         case TK_M_ATTRS:
         case TK_A_ATTR:
+        case TK_A_SYSCTL:
         case TK_A_ENV:
         case TK_A_SECLABEL:
                 attr = data;
@@ -1135,11 +1144,27 @@ static int add_rule(struct udev_rules *rules, char *line,
                                 log_error("invalid ATTR operation");
                                 goto invalid;
                         }
-                        if (op < OP_MATCH_MAX) {
+                        if (op < OP_MATCH_MAX)
                                 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
-                        } else {
+                        else
                                 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
+                        continue;
+                }
+
+                if (startswith(key, "SYSCTL{")) {
+                        attr = get_key_attribute(rules->udev, key + strlen("SYSCTL"));
+                        if (attr == NULL) {
+                                log_error("error parsing SYSCTL attribute");
+                                goto invalid;
+                        }
+                        if (op == OP_REMOVE) {
+                                log_error("invalid SYSCTL operation");
+                                goto invalid;
                         }
+                        if (op < OP_MATCH_MAX)
+                                rule_add_key(&rule_tmp, TK_M_SYSCTL, op, value, attr);
+                        else
+                                rule_add_key(&rule_tmp, TK_A_SYSCTL, op, value, attr);
                         continue;
                 }
 
@@ -1986,6 +2011,23 @@ int udev_rules_apply_to_event(struct udev_rules *rules,
                         if (match_attr(rules, event->dev, event, cur) != 0)
                                 goto nomatch;
                         break;
+                case TK_M_SYSCTL: {
+                        char filename[UTIL_PATH_SIZE];
+                        _cleanup_free_ char *value = NULL;
+                        size_t len;
+
+                        udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename));
+                        sysctl_normalize(filename);
+                        if (sysctl_read(filename, &value) < 0)
+                                goto nomatch;
+
+                        len = strlen(value);
+                        while (len > 0 && isspace(value[--len]))
+                                value[len] = '\0';
+                        if (match_key(rules, cur, value) != 0)
+                                goto nomatch;
+                        break;
+                }
                 case TK_M_KERNELS:
                 case TK_M_SUBSYSTEMS:
                 case TK_M_DRIVERS:
@@ -2522,6 +2564,21 @@ int udev_rules_apply_to_event(struct udev_rules *rules,
                         }
                         break;
                 }
+                case TK_A_SYSCTL: {
+                        char filename[UTIL_PATH_SIZE];
+                        char value[UTIL_NAME_SIZE];
+                        int r;
+
+                        udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename));
+                        sysctl_normalize(filename);
+                        udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
+                        log_debug("SYSCTL '%s' writing '%s' %s:%u", filename, value,
+                                  rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
+                        r = sysctl_write(filename, value);
+                        if (r < 0)
+                                log_error("error writing SYSCTL{%s}='%s': %s", filename, value, strerror(-r));
+                        break;
+                }
                 case TK_A_RUN_BUILTIN:
                 case TK_A_RUN_PROGRAM: {
                         struct udev_list_entry *entry;

commit 88a60da054759f763e2be7bb88c7f88919c6d46f
Author: Kay Sievers <kay at vrfy.org>
Date:   Wed Mar 11 10:37:45 2015 +0100

    sysctl: move property handling to shared/

diff --git a/Makefile.am b/Makefile.am
index 3539e03..89a8019 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -908,7 +908,9 @@ libsystemd_shared_la_SOURCES = \
 	src/shared/sigbus.h \
 	src/shared/build.h \
 	src/shared/import-util.c \
-	src/shared/import-util.h
+	src/shared/import-util.h \
+	src/shared/sysctl-util.c \
+	src/shared/sysctl-util.h
 
 if HAVE_UTMP
 libsystemd_shared_la_SOURCES += \
diff --git a/src/shared/sysctl-util.c b/src/shared/sysctl-util.c
new file mode 100644
index 0000000..650c9c9
--- /dev/null
+++ b/src/shared/sysctl-util.c
@@ -0,0 +1,88 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <limits.h>
+#include <getopt.h>
+
+#include "log.h"
+#include "util.h"
+#include "fileio.h"
+#include "build.h"
+#include "sysctl-util.h"
+
+char *sysctl_normalize(char *s) {
+        char *n;
+
+        n = strpbrk(s, "/.");
+        /* If the first separator is a slash, the path is
+         * assumed to be normalized and slashes remain slashes
+         * and dots remains dots. */
+        if (!n || *n == '/')
+                return s;
+
+        /* Otherwise, dots become slashes and slashes become
+         * dots. Fun. */
+        while (n) {
+                if (*n == '.')
+                        *n = '/';
+                else
+                        *n = '.';
+
+                n = strpbrk(n + 1, "/.");
+        }
+
+        return s;
+}
+
+int sysctl_write(const char *property, const char *value) {
+        _cleanup_free_ char *p = NULL;
+        char *n;
+
+        log_debug("Setting '%s' to '%s'", property, value);
+
+        p = new(char, strlen("/proc/sys/") + strlen(property) + 1);
+        if (!p)
+                return log_oom();
+
+        n = stpcpy(p, "/proc/sys/");
+        strcpy(n, property);
+
+        return write_string_file(p, value);
+}
+
+int sysctl_read(const char *property, char **content) {
+        _cleanup_free_ char *p = NULL;
+        char *n;
+
+        p = new(char, strlen("/proc/sys/") + strlen(property) + 1);
+        if (!p)
+                return log_oom();
+
+        n = stpcpy(p, "/proc/sys/");
+        strcpy(n, property);
+
+        return read_full_file(p, content, NULL);
+}
diff --git a/src/shared/sysctl-util.h b/src/shared/sysctl-util.h
new file mode 100644
index 0000000..2ee6454
--- /dev/null
+++ b/src/shared/sysctl-util.h
@@ -0,0 +1,27 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2011 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+char *sysctl_normalize(char *s);
+int sysctl_read(const char *property, char **value);
+int sysctl_write(const char *property, const char *value);
+
diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c
index 2415d84..dd2e118 100644
--- a/src/sysctl/sysctl.c
+++ b/src/sysctl/sysctl.c
@@ -35,61 +35,12 @@
 #include "conf-files.h"
 #include "fileio.h"
 #include "build.h"
+#include "sysctl-util.h"
 
 static char **arg_prefixes = NULL;
 
 static const char conf_file_dirs[] = CONF_DIRS_NULSTR("sysctl");
 
-static char* normalize_sysctl(char *s) {
-        char *n;
-
-        n = strpbrk(s, "/.");
-        /* If the first separator is a slash, the path is
-         * assumed to be normalized and slashes remain slashes
-         * and dots remains dots. */
-        if (!n || *n == '/')
-                return s;
-
-        /* Otherwise, dots become slashes and slashes become
-         * dots. Fun. */
-        while (n) {
-                if (*n == '.')
-                        *n = '/';
-                else
-                        *n = '.';
-
-                n = strpbrk(n + 1, "/.");
-        }
-
-        return s;
-}
-
-static int apply_sysctl(const char *property, const char *value) {
-        _cleanup_free_ char *p = NULL;
-        char *n;
-        int r = 0, k;
-
-        log_debug("Setting '%s' to '%s'", property, value);
-
-        p = new(char, strlen("/proc/sys/") + strlen(property) + 1);
-        if (!p)
-                return log_oom();
-
-        n = stpcpy(p, "/proc/sys/");
-        strcpy(n, property);
-
-        k = write_string_file(p, value);
-        if (k < 0) {
-                log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
-                         "Failed to write '%s' to '%s': %s", value, p, strerror(-k));
-
-                if (k != -ENOENT && r == 0)
-                        r = k;
-        }
-
-        return r;
-}
-
 static int apply_all(Hashmap *sysctl_options) {
         int r = 0;
         char *property, *value;
@@ -100,9 +51,14 @@ static int apply_all(Hashmap *sysctl_options) {
         HASHMAP_FOREACH_KEY(value, property, sysctl_options, i) {
                 int k;
 
-                k = apply_sysctl(property, value);
-                if (k < 0 && r == 0)
-                        r = k;
+                k = sysctl_write(property, value);
+                if (k < 0) {
+                        log_full(k == -ENOENT ? LOG_DEBUG : LOG_WARNING,
+                                 "Failed to write '%s' to '%s': %s", value, property, strerror(-k));
+
+                        if (r == 0)
+                                r = k;
+                }
         }
         return r;
 }
@@ -154,7 +110,7 @@ static int parse_file(Hashmap *sysctl_options, const char *path, bool ignore_eno
                 *value = 0;
                 value++;
 
-                p = normalize_sysctl(strstrip(p));
+                p = sysctl_normalize(strstrip(p));
                 value = strstrip(value);
 
                 if (!strv_isempty(arg_prefixes)) {
@@ -251,7 +207,7 @@ static int parse_argv(int argc, char *argv[]) {
                          * in /proc/sys in the past. This is kinda useless, but
                          * we need to keep compatibility. We now support any
                          * sysctl name available. */
-                        normalize_sysctl(optarg);
+                        sysctl_normalize(optarg);
                         if (startswith(optarg, "/proc/sys"))
                                 p = strdup(optarg);
                         else



More information about the systemd-commits mailing list