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