]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-net_setup_link.c
udev/net: do not set unapplied .link file name to ID_NET_LINK_FILE
[thirdparty/systemd.git] / src / udev / udev-builtin-net_setup_link.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "device-private.h"
5 #include "device-util.h"
6 #include "escape.h"
7 #include "errno-util.h"
8 #include "link-config.h"
9 #include "log.h"
10 #include "string-util.h"
11 #include "strv.h"
12 #include "udev-builtin.h"
13
14 static LinkConfigContext *ctx = NULL;
15
16 static int builtin_net_setup_link(UdevEvent *event, int argc, char **argv, bool test) {
17 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
18 _cleanup_(link_freep) Link *link = NULL;
19 _cleanup_free_ char *joined = NULL;
20 int r;
21
22 if (argc > 1)
23 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
24
25 sd_device_action_t action;
26 r = sd_device_get_action(dev, &action);
27 if (r < 0)
28 return log_device_error_errno(dev, r, "Failed to get action: %m");
29
30 if (!IN_SET(action, SD_DEVICE_ADD, SD_DEVICE_BIND, SD_DEVICE_MOVE)) {
31 log_device_debug(dev, "Skipping to apply .link settings on '%s' uevent.",
32 device_action_to_string(action));
33
34 /* Import previously assigned .link file name. */
35 (void) udev_builtin_import_property(dev, event->dev_db_clone, test, "ID_NET_LINK_FILE");
36 (void) udev_builtin_import_property(dev, event->dev_db_clone, test, "ID_NET_LINK_FILE_DROPINS");
37
38 /* Set ID_NET_NAME= with the current interface name. */
39 const char *value;
40 if (sd_device_get_sysname(dev, &value) >= 0)
41 (void) udev_builtin_add_property(dev, test, "ID_NET_NAME", value);
42
43 return 0;
44 }
45
46 r = link_new(ctx, &event->rtnl, dev, &link);
47 if (r == -ENODEV) {
48 log_device_debug_errno(dev, r, "Link vanished while getting information, ignoring.");
49 return 0;
50 }
51 if (r < 0)
52 return log_device_warning_errno(dev, r, "Failed to get link information: %m");
53
54 r = link_get_config(ctx, link);
55 if (r < 0) {
56 if (r == -ENOENT) {
57 log_device_debug_errno(dev, r, "No matching link configuration found, ignoring device.");
58 return 0;
59 }
60
61 return log_device_error_errno(dev, r, "Failed to get link config: %m");
62 }
63
64 r = link_apply_config(ctx, &event->rtnl, link);
65 if (r == -ENODEV)
66 log_device_debug_errno(dev, r, "Link vanished while applying configuration, ignoring.");
67 else if (r < 0)
68 log_device_warning_errno(dev, r, "Could not apply link configuration, ignoring: %m");
69
70 udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->config->filename);
71 if (link->new_name)
72 udev_builtin_add_property(dev, test, "ID_NET_NAME", link->new_name);
73
74 event->altnames = TAKE_PTR(link->altnames);
75
76 STRV_FOREACH(d, link->config->dropins) {
77 _cleanup_free_ char *escaped = NULL;
78
79 escaped = xescape(*d, ":");
80 if (!escaped)
81 return log_oom();
82
83 if (!strextend_with_separator(&joined, ":", escaped))
84 return log_oom();
85 }
86
87 udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE_DROPINS", joined);
88
89 return 0;
90 }
91
92 static int builtin_net_setup_link_init(void) {
93 int r;
94
95 if (ctx)
96 return 0;
97
98 r = link_config_ctx_new(&ctx);
99 if (r < 0)
100 return r;
101
102 r = link_config_load(ctx);
103 if (r < 0)
104 return r;
105
106 log_debug("Created link configuration context.");
107 return 0;
108 }
109
110 static void builtin_net_setup_link_exit(void) {
111 ctx = link_config_ctx_free(ctx);
112 log_debug("Unloaded link configuration context.");
113 }
114
115 static bool builtin_net_setup_link_should_reload(void) {
116 if (!ctx)
117 return false;
118
119 if (link_config_should_reload(ctx)) {
120 log_debug("Link configuration context needs reloading.");
121 return true;
122 }
123
124 return false;
125 }
126
127 const UdevBuiltin udev_builtin_net_setup_link = {
128 .name = "net_setup_link",
129 .cmd = builtin_net_setup_link,
130 .init = builtin_net_setup_link_init,
131 .exit = builtin_net_setup_link_exit,
132 .should_reload = builtin_net_setup_link_should_reload,
133 .help = "Configure network link",
134 .run_once = false,
135 };