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