]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
af6f0d42 TG |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Tom Gundersen | |
af6f0d42 TG |
6 | ***/ |
7 | ||
b5efdb8a | 8 | #include "alloc-util.h" |
af6f0d42 | 9 | #include "link-config.h" |
af6f0d42 | 10 | #include "log.h" |
b5efdb8a | 11 | #include "udev.h" |
af6f0d42 | 12 | |
9588bc32 | 13 | static link_config_ctx *ctx = NULL; |
af6f0d42 | 14 | |
0b99c9f8 | 15 | static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) { |
847a8a5f | 16 | _cleanup_free_ char *driver = NULL; |
c487c9ce | 17 | const char *name = NULL; |
af6f0d42 TG |
18 | link_config *link; |
19 | int r; | |
20 | ||
21 | if (argc > 1) { | |
22 | log_error("This program takes no arguments."); | |
23 | return EXIT_FAILURE; | |
24 | } | |
25 | ||
847a8a5f TG |
26 | r = link_get_driver(ctx, dev, &driver); |
27 | if (r >= 0) | |
28 | udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver); | |
29 | ||
af6f0d42 TG |
30 | r = link_config_get(ctx, dev, &link); |
31 | if (r < 0) { | |
32 | if (r == -ENOENT) { | |
01d4590b | 33 | log_debug("No matching link configuration found."); |
af6f0d42 TG |
34 | return EXIT_SUCCESS; |
35 | } else { | |
da927ba9 | 36 | log_error_errno(r, "Could not get link config: %m"); |
af6f0d42 TG |
37 | return EXIT_FAILURE; |
38 | } | |
39 | } | |
40 | ||
3e137a1b | 41 | r = link_config_apply(ctx, link, dev, &name); |
56692c5d MS |
42 | if (r < 0) |
43 | log_warning_errno(r, "Could not apply link config to %s, ignoring: %m", udev_device_get_sysname(dev)); | |
af6f0d42 | 44 | |
ad6e5b34 TG |
45 | udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename); |
46 | ||
3e137a1b TG |
47 | if (name) |
48 | udev_builtin_add_property(dev, test, "ID_NET_NAME", name); | |
49 | ||
af6f0d42 TG |
50 | return EXIT_SUCCESS; |
51 | } | |
52 | ||
0b99c9f8 | 53 | static int builtin_net_setup_link_init(struct udev *udev) { |
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 | ||
0b99c9f8 | 71 | static void builtin_net_setup_link_exit(struct udev *udev) { |
af6f0d42 | 72 | link_config_ctx_free(ctx); |
e0221a35 | 73 | ctx = NULL; |
01d4590b | 74 | log_debug("Unloaded link configuration context."); |
af6f0d42 TG |
75 | } |
76 | ||
0b99c9f8 | 77 | static bool builtin_net_setup_link_validate(struct udev *udev) { |
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 | ||
0b99c9f8 TG |
85 | const struct udev_builtin udev_builtin_net_setup_link = { |
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 | }; |