Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "device-util.h"
4 : #include "alloc-util.h"
5 : #include "link-config.h"
6 : #include "log.h"
7 : #include "string-util.h"
8 : #include "udev-builtin.h"
9 :
10 : static link_config_ctx *ctx = NULL;
11 :
12 0 : static int builtin_net_setup_link(sd_device *dev, int argc, char **argv, bool test) {
13 0 : _cleanup_free_ char *driver = NULL;
14 0 : const char *name = NULL;
15 : link_config *link;
16 : int r;
17 :
18 0 : if (argc > 1)
19 0 : return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
20 :
21 0 : r = link_get_driver(ctx, dev, &driver);
22 0 : if (r >= 0)
23 0 : udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
24 :
25 0 : r = link_config_get(ctx, dev, &link);
26 0 : if (r < 0) {
27 0 : if (r == -ENOENT)
28 0 : return log_device_debug_errno(dev, r, "No matching link configuration found.");
29 :
30 0 : return log_device_error_errno(dev, r, "Failed to get link config: %m");
31 : }
32 :
33 0 : r = link_config_apply(ctx, link, dev, &name);
34 0 : if (r < 0)
35 0 : log_device_warning_errno(dev, r, "Could not apply link config, ignoring: %m");
36 :
37 0 : udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
38 :
39 0 : if (name)
40 0 : udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
41 :
42 0 : return 0;
43 : }
44 :
45 0 : static int builtin_net_setup_link_init(void) {
46 : int r;
47 :
48 0 : if (ctx)
49 0 : return 0;
50 :
51 0 : r = link_config_ctx_new(&ctx);
52 0 : if (r < 0)
53 0 : return r;
54 :
55 0 : r = link_config_load(ctx);
56 0 : if (r < 0)
57 0 : return r;
58 :
59 0 : log_debug("Created link configuration context.");
60 0 : return 0;
61 : }
62 :
63 0 : static void builtin_net_setup_link_exit(void) {
64 0 : link_config_ctx_free(ctx);
65 0 : ctx = NULL;
66 0 : log_debug("Unloaded link configuration context.");
67 0 : }
68 :
69 0 : static bool builtin_net_setup_link_validate(void) {
70 0 : log_debug("Check if link configuration needs reloading.");
71 0 : if (!ctx)
72 0 : return false;
73 :
74 0 : return link_config_should_reload(ctx);
75 : }
76 :
77 : const UdevBuiltin 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,
83 : .help = "Configure network link",
84 : .run_once = false,
85 : };
|