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