]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-net_setup_link.c
Merge pull request #10395 from yuwata/udev-cleanup-9
[thirdparty/systemd.git] / src / udev / udev-builtin-net_setup_link.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "link-config.h"
5 #include "log.h"
6 #include "string-util.h"
7 #include "udev-builtin.h"
8
9 static link_config_ctx *ctx = NULL;
10
11 static int builtin_net_setup_link(sd_device *dev, int argc, char **argv, bool test) {
12 _cleanup_free_ char *driver = NULL;
13 const char *name = NULL;
14 link_config *link;
15 int r;
16
17 if (argc > 1) {
18 log_error("This program takes no arguments.");
19 return -EINVAL;
20 }
21
22 r = link_get_driver(ctx, dev, &driver);
23 if (r >= 0)
24 udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
25
26 r = link_config_get(ctx, dev, &link);
27 if (r < 0) {
28 if (r == -ENOENT)
29 return log_debug_errno(r, "No matching link configuration found.");
30
31 return log_error_errno(r, "Could not get link config: %m");
32 }
33
34 r = link_config_apply(ctx, link, dev, &name);
35 if (r < 0) {
36 const char *sysname = NULL;
37
38 (void) sd_device_get_sysname(dev, &sysname);
39 log_warning_errno(r, "Could not apply link config to %s, ignoring: %m", strnull(sysname));
40 }
41
42 udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
43
44 if (name)
45 udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
46
47 return 0;
48 }
49
50 static int builtin_net_setup_link_init(void) {
51 int r;
52
53 if (ctx)
54 return 0;
55
56 r = link_config_ctx_new(&ctx);
57 if (r < 0)
58 return r;
59
60 r = link_config_load(ctx);
61 if (r < 0)
62 return r;
63
64 log_debug("Created link configuration context.");
65 return 0;
66 }
67
68 static void builtin_net_setup_link_exit(void) {
69 link_config_ctx_free(ctx);
70 ctx = NULL;
71 log_debug("Unloaded link configuration context.");
72 }
73
74 static bool builtin_net_setup_link_validate(void) {
75 log_debug("Check if link configuration needs reloading.");
76 if (!ctx)
77 return false;
78
79 return link_config_should_reload(ctx);
80 }
81
82 const struct udev_builtin udev_builtin_net_setup_link = {
83 .name = "net_setup_link",
84 .cmd = builtin_net_setup_link,
85 .init = builtin_net_setup_link_init,
86 .exit = builtin_net_setup_link_exit,
87 .validate = builtin_net_setup_link_validate,
88 .help = "Configure network link",
89 .run_once = false,
90 };