]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-net_setup_link.c
Merge pull request #18481 from keszybz/rpm-restart-post-trans
[thirdparty/systemd.git] / src / udev / udev-builtin-net_setup_link.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
af6f0d42 2
b5efdb8a 3#include "alloc-util.h"
3ec2f7f2
LP
4#include "device-util.h"
5#include "errno-util.h"
af6f0d42 6#include "link-config.h"
af6f0d42 7#include "log.h"
d9b3e996 8#include "string-util.h"
07a26e42 9#include "udev-builtin.h"
af6f0d42 10
9588bc32 11static link_config_ctx *ctx = NULL;
af6f0d42 12
3fc2e9a2 13static int builtin_net_setup_link(sd_device *dev, int argc, char **argv, bool test) {
847a8a5f 14 _cleanup_free_ char *driver = NULL;
c487c9ce 15 const char *name = NULL;
af6f0d42
TG
16 link_config *link;
17 int r;
18
70fbab6e 19 if (argc > 1)
a1193286 20 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
af6f0d42 21
d9b3e996 22 r = link_get_driver(ctx, dev, &driver);
d61db498 23 if (r < 0)
3ec2f7f2 24 log_device_full_errno(dev, ERRNO_IS_NOT_SUPPORTED(r) || r == -ENODEV ? LOG_DEBUG : LOG_WARNING,
d61db498
ZJS
25 r, "Failed to query device driver: %m");
26 else
d9b3e996 27 udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
847a8a5f 28
d9b3e996 29 r = link_config_get(ctx, dev, &link);
af6f0d42 30 if (r < 0) {
d354690e 31 if (r == -ENOENT)
70fbab6e 32 return log_device_debug_errno(dev, r, "No matching link configuration found.");
3ec2f7f2
LP
33 if (r == -ENODEV)
34 return log_device_debug_errno(dev, r, "Link vanished while searching for configuration for it.");
d354690e 35
70fbab6e 36 return log_device_error_errno(dev, r, "Failed to get link config: %m");
af6f0d42
TG
37 }
38
d9b3e996 39 r = link_config_apply(ctx, link, dev, &name);
3ec2f7f2
LP
40 if (r == -ENODEV)
41 log_device_debug_errno(dev, r, "Link vanished while applying configuration, ignoring.");
42 else if (r < 0)
43 log_device_warning_errno(dev, r, "Could not apply link configuration, ignoring: %m");
af6f0d42 44
d9b3e996 45 udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
ad6e5b34 46
3e137a1b 47 if (name)
d9b3e996 48 udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
3e137a1b 49
d354690e 50 return 0;
af6f0d42
TG
51}
52
2024ed61 53static int builtin_net_setup_link_init(void) {
af6f0d42
TG
54 int r;
55
56 if (ctx)
57 return 0;
58
59 r = link_config_ctx_new(&ctx);
60 if (r < 0)
61 return r;
62
63 r = link_config_load(ctx);
64 if (r < 0)
65 return r;
66
01d4590b 67 log_debug("Created link configuration context.");
af6f0d42
TG
68 return 0;
69}
70
2024ed61 71static void builtin_net_setup_link_exit(void) {
af6f0d42 72 link_config_ctx_free(ctx);
e0221a35 73 ctx = NULL;
01d4590b 74 log_debug("Unloaded link configuration context.");
af6f0d42
TG
75}
76
2024ed61 77static bool builtin_net_setup_link_validate(void) {
01d4590b 78 log_debug("Check if link configuration needs reloading.");
af6f0d42
TG
79 if (!ctx)
80 return false;
81
82 return link_config_should_reload(ctx);
83}
84
25de7aa7 85const UdevBuiltin udev_builtin_net_setup_link = {
0b99c9f8
TG
86 .name = "net_setup_link",
87 .cmd = builtin_net_setup_link,
88 .init = builtin_net_setup_link_init,
89 .exit = builtin_net_setup_link_exit,
90 .validate = builtin_net_setup_link_validate,
5ac0162c 91 .help = "Configure network link",
af6f0d42
TG
92 .run_once = false,
93};