1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "link-config.h"
6 #include "string-util.h"
7 #include "udev-builtin.h"
9 static link_config_ctx
*ctx
= NULL
;
11 static int builtin_net_setup_link(sd_device
*dev
, int argc
, char **argv
, bool test
) {
12 _cleanup_free_
char *driver
= NULL
;
13 const char *name
= NULL
;
18 log_error("This program takes no arguments.");
22 r
= link_get_driver(ctx
, dev
, &driver
);
24 udev_builtin_add_property(dev
, test
, "ID_NET_DRIVER", driver
);
26 r
= link_config_get(ctx
, dev
, &link
);
29 return log_debug_errno(r
, "No matching link configuration found.");
31 return log_error_errno(r
, "Could not get link config: %m");
34 r
= link_config_apply(ctx
, link
, dev
, &name
);
36 const char *sysname
= NULL
;
38 (void) sd_device_get_sysname(dev
, &sysname
);
39 log_warning_errno(r
, "Could not apply link config to %s, ignoring: %m", strnull(sysname
));
42 udev_builtin_add_property(dev
, test
, "ID_NET_LINK_FILE", link
->filename
);
45 udev_builtin_add_property(dev
, test
, "ID_NET_NAME", name
);
50 static int builtin_net_setup_link_init(void) {
56 r
= link_config_ctx_new(&ctx
);
60 r
= link_config_load(ctx
);
64 log_debug("Created link configuration context.");
68 static void builtin_net_setup_link_exit(void) {
69 link_config_ctx_free(ctx
);
71 log_debug("Unloaded link configuration context.");
74 static bool builtin_net_setup_link_validate(void) {
75 log_debug("Check if link configuration needs reloading.");
79 return link_config_should_reload(ctx
);
82 const struct udev_builtin udev_builtin_net_setup_link
= {
83 .name
= "net_setup_link",
84 .cmd
= builtin_net_setup_link
,
85 .init
= builtin_net_setup_link_init
,
86 .exit
= builtin_net_setup_link_exit
,
87 .validate
= builtin_net_setup_link_validate
,
88 .help
= "Configure network link",