]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network,udev: split static condition tests from net_match_config()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 8 Mar 2019 06:20:01 +0000 (15:20 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 21 Mar 2019 14:37:39 +0000 (23:37 +0900)
13 files changed:
src/libsystemd-network/network-internal.c
src/libsystemd-network/network-internal.h
src/network/netdev/netdev-gperf.gperf
src/network/netdev/netdev.c
src/network/netdev/netdev.h
src/network/networkd-network-gperf.gperf
src/network/networkd-network.c
src/network/networkd-network.h
src/shared/condition.c
src/shared/condition.h
src/udev/net/link-config-gperf.gperf
src/udev/net/link-config.c
src/udev/net/link-config.h

index 63d4d85665c22db57912da6490a34932d8ceceb6..2154cf7eac4b1a6e426b1117ced511be9df79686 100644 (file)
@@ -100,32 +100,12 @@ bool net_match_config(Set *match_mac,
                       char * const *match_drivers,
                       char * const *match_types,
                       char * const *match_names,
-                      Condition *match_host,
-                      Condition *match_virt,
-                      Condition *match_kernel_cmdline,
-                      Condition *match_kernel_version,
-                      Condition *match_arch,
                       const struct ether_addr *dev_mac,
                       const char *dev_path,
                       const char *dev_driver,
                       const char *dev_type,
                       const char *dev_name) {
 
-        if (match_host && condition_test(match_host) <= 0)
-                return false;
-
-        if (match_virt && condition_test(match_virt) <= 0)
-                return false;
-
-        if (match_kernel_cmdline && condition_test(match_kernel_cmdline) <= 0)
-                return false;
-
-        if (match_kernel_version && condition_test(match_kernel_version) <= 0)
-                return false;
-
-        if (match_arch && condition_test(match_arch) <= 0)
-                return false;
-
         if (match_mac && (!dev_mac || !set_contains(match_mac, dev_mac)))
                 return false;
 
@@ -156,15 +136,19 @@ int config_parse_net_condition(const char *unit,
                                void *userdata) {
 
         ConditionType cond = ltype;
-        Condition **ret = data;
+        Condition **list = data, *c;
         bool negate;
-        Condition *c;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
+        if (isempty(rvalue)) {
+                *list = condition_free_list_type(*list, cond);
+                return 0;
+        }
+
         negate = rvalue[0] == '!';
         if (negate)
                 rvalue++;
@@ -173,10 +157,10 @@ int config_parse_net_condition(const char *unit,
         if (!c)
                 return log_oom();
 
-        if (*ret)
-                condition_free(*ret);
+        /* Drop previous assignment. */
+        *list = condition_free_list_type(*list, cond);
 
-        *ret = c;
+        LIST_PREPEND(conditions, *list, c);
         return 0;
 }
 
index 9d30c52eea1ad86554bcd382a8815f574d4ef082..62f5a4a76c5ab3154a485f8b495a0ced30aedeea 100644 (file)
@@ -6,7 +6,6 @@
 #include "sd-device.h"
 #include "sd-dhcp-lease.h"
 
-#include "condition.h"
 #include "conf-parser.h"
 #include "def.h"
 #include "set.h"
@@ -20,11 +19,6 @@ bool net_match_config(Set *match_mac,
                       char * const *match_driver,
                       char * const *match_type,
                       char * const *match_name,
-                      Condition *match_host,
-                      Condition *match_virt,
-                      Condition *match_kernel_cmdline,
-                      Condition *match_kernel_version,
-                      Condition *match_arch,
                       const struct ether_addr *dev_mac,
                       const char *dev_path,
                       const char *dev_driver,
index 3365023b32d45b19f877b2edf1a6ae556128ac06..96632da0b604bb310473a43a90ac2a4975cc2c13 100644 (file)
@@ -34,11 +34,11 @@ struct ConfigPerfItem;
 %struct-type
 %includes
 %%
-Match.Host,                        config_parse_net_condition,           CONDITION_HOST,                offsetof(NetDev, match_host)
-Match.Virtualization,              config_parse_net_condition,           CONDITION_VIRTUALIZATION,      offsetof(NetDev, match_virt)
-Match.KernelCommandLine,           config_parse_net_condition,           CONDITION_KERNEL_COMMAND_LINE, offsetof(NetDev, match_kernel_cmdline)
-Match.KernelVersion,               config_parse_net_condition,           CONDITION_KERNEL_VERSION,      offsetof(NetDev, match_kernel_version)
-Match.Architecture,                config_parse_net_condition,           CONDITION_ARCHITECTURE,        offsetof(NetDev, match_arch)
+Match.Host,                        config_parse_net_condition,           CONDITION_HOST,                offsetof(NetDev, conditions)
+Match.Virtualization,              config_parse_net_condition,           CONDITION_VIRTUALIZATION,      offsetof(NetDev, conditions)
+Match.KernelCommandLine,           config_parse_net_condition,           CONDITION_KERNEL_COMMAND_LINE, offsetof(NetDev, conditions)
+Match.KernelVersion,               config_parse_net_condition,           CONDITION_KERNEL_VERSION,      offsetof(NetDev, conditions)
+Match.Architecture,                config_parse_net_condition,           CONDITION_ARCHITECTURE,        offsetof(NetDev, conditions)
 NetDev.Description,                config_parse_string,                  0,                             offsetof(NetDev, description)
 NetDev.Name,                       config_parse_ifname,                  0,                             offsetof(NetDev, ifname)
 NetDev.Kind,                       config_parse_netdev_kind,             0,                             offsetof(NetDev, kind)
index f60214ae115538fd113a963b4dfb948b56b93500..5cdd09d775d7701793ef557bd8f9a47b2cb39351 100644 (file)
@@ -174,12 +174,7 @@ static NetDev *netdev_free(NetDev *netdev) {
         free(netdev->description);
         free(netdev->ifname);
         free(netdev->mac);
-
-        condition_free_list(netdev->match_host);
-        condition_free_list(netdev->match_virt);
-        condition_free_list(netdev->match_kernel_cmdline);
-        condition_free_list(netdev->match_kernel_version);
-        condition_free_list(netdev->match_arch);
+        condition_free_list(netdev->conditions);
 
         /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
          * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
@@ -683,11 +678,7 @@ int netdev_load_one(Manager *manager, const char *filename) {
                 return r;
 
         /* skip out early if configuration does not match the environment */
-        if (!net_match_config(NULL, NULL, NULL, NULL, NULL,
-                              netdev_raw->match_host, netdev_raw->match_virt,
-                              netdev_raw->match_kernel_cmdline, netdev_raw->match_kernel_version,
-                              netdev_raw->match_arch,
-                              NULL, NULL, NULL, NULL, NULL)) {
+        if (!condition_test_list(netdev_raw->conditions, NULL, NULL, NULL)) {
                 log_debug("%s: Conditions in the file do not match the system environment, skipping.", filename);
                 return 0;
         }
index c46ed4d7198b57849bb3417e9bbe1087eccf1211..3c6990236fe130e09950cd634dd21b150ae9c794 100644 (file)
@@ -81,11 +81,7 @@ typedef struct NetDev {
 
         char *filename;
 
-        Condition *match_host;
-        Condition *match_virt;
-        Condition *match_kernel_cmdline;
-        Condition *match_kernel_version;
-        Condition *match_arch;
+        Condition *conditions;
 
         NetDevState state;
         NetDevKind kind;
index 2db8299b13f895e5f0e805418b4ef55ac26474a0..47a9f7d808a6c353c3c790e33a411dc6416a25b4 100644 (file)
@@ -25,11 +25,11 @@ Match.Path,                             config_parse_strv,
 Match.Driver,                           config_parse_strv,                              0,                             offsetof(Network, match_driver)
 Match.Type,                             config_parse_strv,                              0,                             offsetof(Network, match_type)
 Match.Name,                             config_parse_ifnames,                           0,                             offsetof(Network, match_name)
-Match.Host,                             config_parse_net_condition,                     CONDITION_HOST,                offsetof(Network, match_host)
-Match.Virtualization,                   config_parse_net_condition,                     CONDITION_VIRTUALIZATION,      offsetof(Network, match_virt)
-Match.KernelCommandLine,                config_parse_net_condition,                     CONDITION_KERNEL_COMMAND_LINE, offsetof(Network, match_kernel_cmdline)
-Match.KernelVersion,                    config_parse_net_condition,                     CONDITION_KERNEL_VERSION,      offsetof(Network, match_kernel_version)
-Match.Architecture,                     config_parse_net_condition,                     CONDITION_ARCHITECTURE,        offsetof(Network, match_arch)
+Match.Host,                             config_parse_net_condition,                     CONDITION_HOST,                offsetof(Network, conditions)
+Match.Virtualization,                   config_parse_net_condition,                     CONDITION_VIRTUALIZATION,      offsetof(Network, conditions)
+Match.KernelCommandLine,                config_parse_net_condition,                     CONDITION_KERNEL_COMMAND_LINE, offsetof(Network, conditions)
+Match.KernelVersion,                    config_parse_net_condition,                     CONDITION_KERNEL_VERSION,      offsetof(Network, conditions)
+Match.Architecture,                     config_parse_net_condition,                     CONDITION_ARCHITECTURE,        offsetof(Network, conditions)
 Link.MACAddress,                        config_parse_hwaddr,                            0,                             offsetof(Network, mac)
 Link.MTUBytes,                          config_parse_mtu,                               AF_UNSPEC,                     offsetof(Network, mtu)
 Link.ARP,                               config_parse_tristate,                          0,                             offsetof(Network, arp)
index 33055d9e3d8a6f509a9f59e7b5c8be04d5de4518..c6c3480cd108c3b09282644bf9e157f5262fa5ac 100644 (file)
@@ -174,10 +174,7 @@ int network_verify(Network *network) {
         assert(network->filename);
 
         /* skip out early if configuration does not match the environment */
-        if (!net_match_config(NULL, NULL, NULL, NULL, NULL,
-                              network->match_host, network->match_virt, network->match_kernel_cmdline,
-                              network->match_kernel_version, network->match_arch,
-                              NULL, NULL, NULL, NULL, NULL))
+        if (!condition_test_list(network->conditions, NULL, NULL, NULL))
                 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "%s: Conditions in the file do not match the system environment, skipping.",
                                        network->filename);
@@ -497,6 +494,7 @@ void network_free(Network *network) {
         strv_free(network->match_driver);
         strv_free(network->match_type);
         strv_free(network->match_name);
+        condition_free_list(network->conditions);
 
         free(network->description);
         free(network->dhcp_vendor_class_identifier);
@@ -568,12 +566,6 @@ void network_free(Network *network) {
 
         free(network->name);
 
-        condition_free_list(network->match_host);
-        condition_free_list(network->match_virt);
-        condition_free_list(network->match_kernel_cmdline);
-        condition_free_list(network->match_kernel_version);
-        condition_free_list(network->match_arch);
-
         free(network->dhcp_server_timezone);
         free(network->dhcp_server_dns);
         free(network->dhcp_server_ntp);
@@ -619,9 +611,7 @@ int network_get(Manager *manager, sd_device *device,
         LIST_FOREACH(networks, network, manager->networks) {
                 if (net_match_config(network->match_mac, network->match_path,
                                      network->match_driver, network->match_type,
-                                     network->match_name, network->match_host,
-                                     network->match_virt, network->match_kernel_cmdline,
-                                     network->match_kernel_version, network->match_arch,
+                                     network->match_name,
                                      address, path, driver, devtype, ifname)) {
                         if (network->match_name && device) {
                                 const char *attr;
index e7f9a6816239296a050b05c3c701aa21f0a172a5..7211aee8c6886c81cfdd54fa45001d080ed29030 100644 (file)
@@ -97,12 +97,7 @@ struct Network {
         char **match_driver;
         char **match_type;
         char **match_name;
-
-        Condition *match_host;
-        Condition *match_virt;
-        Condition *match_kernel_cmdline;
-        Condition *match_kernel_version;
-        Condition *match_arch;
+        Condition *conditions;
 
         char *description;
 
index 44d88c37dd448e71a70cf0555b016d7c92b127c1..69d65fffbce0187b9eb368250d14767543992475 100644 (file)
@@ -77,13 +77,17 @@ void condition_free(Condition *c) {
         free(c);
 }
 
-Condition* condition_free_list(Condition *first) {
-        Condition *c, *n;
+Condition* condition_free_list_type(Condition *first, ConditionType type) {
+        Condition *c, *n, *r = NULL;
 
         LIST_FOREACH_SAFE(conditions, c, n, first)
-                condition_free(c);
+                if (type < 0 || c->type == type)
+                        condition_free(c);
+                else if (!r)
+                        r = c;
 
-        return NULL;
+        assert(type >= 0 || !r);
+        return r;
 }
 
 static int condition_test_kernel_command_line(Condition *c) {
index 773490f05256c2d1d237ff4ddb8f0d78cea7ca18..594c12d5a7d3129d40e1b7a9614682140b33fa29 100644 (file)
@@ -65,7 +65,10 @@ typedef struct Condition {
 
 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate);
 void condition_free(Condition *c);
-Condition* condition_free_list(Condition *c);
+Condition* condition_free_list_type(Condition *first, ConditionType type);
+static inline Condition* condition_free_list(Condition *first) {
+        return condition_free_list_type(first, _CONDITION_TYPE_INVALID);
+}
 
 int condition_test(Condition *c);
 typedef int (*condition_test_logger_t)(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) _printf_(7, 8);
index 2bc18bff56a76acf2453de3f5177a9e8b86065b2..558996cf2e87804ff0187e48222f2f91fc320f6d 100644 (file)
@@ -24,11 +24,11 @@ Match.OriginalName,              config_parse_ifnames,       0,
 Match.Path,                      config_parse_strv,          0,                             offsetof(link_config, match_path)
 Match.Driver,                    config_parse_strv,          0,                             offsetof(link_config, match_driver)
 Match.Type,                      config_parse_strv,          0,                             offsetof(link_config, match_type)
-Match.Host,                      config_parse_net_condition, CONDITION_HOST,                offsetof(link_config, match_host)
-Match.Virtualization,            config_parse_net_condition, CONDITION_VIRTUALIZATION,      offsetof(link_config, match_virt)
-Match.KernelCommandLine,         config_parse_net_condition, CONDITION_KERNEL_COMMAND_LINE, offsetof(link_config, match_kernel_cmdline)
-Match.KernelVersion,             config_parse_net_condition, CONDITION_KERNEL_VERSION,      offsetof(link_config, match_kernel_version)
-Match.Architecture,              config_parse_net_condition, CONDITION_ARCHITECTURE,        offsetof(link_config, match_arch)
+Match.Host,                      config_parse_net_condition, CONDITION_HOST,                offsetof(link_config, conditions)
+Match.Virtualization,            config_parse_net_condition, CONDITION_VIRTUALIZATION,      offsetof(link_config, conditions)
+Match.KernelCommandLine,         config_parse_net_condition, CONDITION_KERNEL_COMMAND_LINE, offsetof(link_config, conditions)
+Match.KernelVersion,             config_parse_net_condition, CONDITION_KERNEL_VERSION,      offsetof(link_config, conditions)
+Match.Architecture,              config_parse_net_condition, CONDITION_ARCHITECTURE,        offsetof(link_config, conditions)
 Link.Description,                config_parse_string,        0,                             offsetof(link_config, description)
 Link.MACAddressPolicy,           config_parse_mac_policy,    0,                             offsetof(link_config, mac_policy)
 Link.MACAddress,                 config_parse_hwaddr,        0,                             offsetof(link_config, mac)
index 438ff473aed3d8dfae7d0c28bc6162bd9a4180ce..58d5d2788362a71f31fb71d9edebea13b8350d05 100644 (file)
@@ -51,12 +51,7 @@ static void link_config_free(link_config *link) {
         strv_free(link->match_driver);
         strv_free(link->match_type);
         strv_free(link->match_name);
-
-        condition_free_list(link->match_host);
-        condition_free_list(link->match_virt);
-        condition_free_list(link->match_kernel_cmdline);
-        condition_free_list(link->match_kernel_version);
-        condition_free_list(link->match_arch);
+        condition_free_list(link->conditions);
 
         free(link->description);
         free(link->mac);
@@ -164,10 +159,7 @@ int link_load_one(link_config_ctx *ctx, const char *filename) {
         if (link->speed > UINT_MAX)
                 return -ERANGE;
 
-        if (!net_match_config(NULL, NULL, NULL, NULL, NULL,
-                              link->match_host, link->match_virt, link->match_kernel_cmdline,
-                              link->match_kernel_version, link->match_arch,
-                              NULL, NULL, NULL, NULL, NULL)) {
+        if (!condition_test_list(link->conditions, NULL, NULL, NULL)) {
                 log_debug("%s: Conditions do not match the system environment, skipping.", filename);
                 return 0;
         }
@@ -251,9 +243,7 @@ int link_config_get(link_config_ctx *ctx, sd_device *device, link_config **ret)
                 (void) sd_device_get_sysname(device, &sysname);
 
                 if (net_match_config(link->match_mac, link->match_path, link->match_driver,
-                                     link->match_type, link->match_name, link->match_host,
-                                     link->match_virt, link->match_kernel_cmdline,
-                                     link->match_kernel_version, link->match_arch,
+                                     link->match_type, link->match_name,
                                      address ? ether_aton(address) : NULL,
                                      id_path,
                                      id_net_driver,
index 4335b593d1279b126d631fef439f0183599d400f..f18b2afa79b5a52c2f8fac8a0ce4536828f0b9ba 100644 (file)
@@ -40,11 +40,7 @@ struct link_config {
         char **match_driver;
         char **match_type;
         char **match_name;
-        Condition *match_host;
-        Condition *match_virt;
-        Condition *match_kernel_cmdline;
-        Condition *match_kernel_version;
-        Condition *match_arch;
+        Condition *conditions;
 
         char *description;
         struct ether_addr *mac;