]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-net_setup_link.c
udev-builtin: move definitions related to builtin commands to udev-builtin.h
[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"
e5eadf53 4#include "libudev-device-internal.h"
af6f0d42 5#include "link-config.h"
af6f0d42 6#include "log.h"
07a26e42 7#include "udev-builtin.h"
af6f0d42 8
9588bc32 9static link_config_ctx *ctx = NULL;
af6f0d42 10
0b99c9f8 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;
16
17 if (argc > 1) {
18 log_error("This program takes no arguments.");
19 return EXIT_FAILURE;
20 }
21
e5eadf53 22 r = link_get_driver(ctx, dev->device, &driver);
847a8a5f
TG
23 if (r >= 0)
24 udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
25
e5eadf53 26 r = link_config_get(ctx, dev->device, &link);
af6f0d42
TG
27 if (r < 0) {
28 if (r == -ENOENT) {
01d4590b 29 log_debug("No matching link configuration found.");
af6f0d42
TG
30 return EXIT_SUCCESS;
31 } else {
da927ba9 32 log_error_errno(r, "Could not get link config: %m");
af6f0d42
TG
33 return EXIT_FAILURE;
34 }
35 }
36
e5eadf53 37 r = link_config_apply(ctx, link, dev->device, &name);
56692c5d
MS
38 if (r < 0)
39 log_warning_errno(r, "Could not apply link config to %s, ignoring: %m", udev_device_get_sysname(dev));
af6f0d42 40
ad6e5b34
TG
41 udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
42
3e137a1b
TG
43 if (name)
44 udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
45
af6f0d42
TG
46 return EXIT_SUCCESS;
47}
48
2024ed61 49static int builtin_net_setup_link_init(void) {
af6f0d42
TG
50 int r;
51
52 if (ctx)
53 return 0;
54
55 r = link_config_ctx_new(&ctx);
56 if (r < 0)
57 return r;
58
59 r = link_config_load(ctx);
60 if (r < 0)
61 return r;
62
01d4590b 63 log_debug("Created link configuration context.");
af6f0d42
TG
64 return 0;
65}
66
2024ed61 67static void builtin_net_setup_link_exit(void) {
af6f0d42 68 link_config_ctx_free(ctx);
e0221a35 69 ctx = NULL;
01d4590b 70 log_debug("Unloaded link configuration context.");
af6f0d42
TG
71}
72
2024ed61 73static bool builtin_net_setup_link_validate(void) {
01d4590b 74 log_debug("Check if link configuration needs reloading.");
af6f0d42
TG
75 if (!ctx)
76 return false;
77
78 return link_config_should_reload(ctx);
79}
80
0b99c9f8
TG
81const struct udev_builtin udev_builtin_net_setup_link = {
82 .name = "net_setup_link",
83 .cmd = builtin_net_setup_link,
84 .init = builtin_net_setup_link_init,
85 .exit = builtin_net_setup_link_exit,
86 .validate = builtin_net_setup_link_validate,
5ac0162c 87 .help = "Configure network link",
af6f0d42
TG
88 .run_once = false,
89};