1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "device-util.h"
4 #include "alloc-util.h"
5 #include "link-config.h"
7 #include "string-util.h"
8 #include "udev-builtin.h"
10 static link_config_ctx
*ctx
= NULL
;
12 static int builtin_net_setup_link(sd_device
*dev
, int argc
, char **argv
, bool test
) {
13 _cleanup_free_
char *driver
= NULL
;
14 const char *name
= NULL
;
19 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
), "This program takes no arguments.");
21 r
= link_get_driver(ctx
, dev
, &driver
);
23 udev_builtin_add_property(dev
, test
, "ID_NET_DRIVER", driver
);
25 r
= link_config_get(ctx
, dev
, &link
);
28 return log_device_debug_errno(dev
, r
, "No matching link configuration found.");
30 return log_device_error_errno(dev
, r
, "Failed to get link config: %m");
33 r
= link_config_apply(ctx
, link
, dev
, &name
);
35 log_device_warning_errno(dev
, r
, "Could not apply link config, ignoring: %m");
37 udev_builtin_add_property(dev
, test
, "ID_NET_LINK_FILE", link
->filename
);
40 udev_builtin_add_property(dev
, test
, "ID_NET_NAME", name
);
45 static int builtin_net_setup_link_init(void) {
51 r
= link_config_ctx_new(&ctx
);
55 r
= link_config_load(ctx
);
59 log_debug("Created link configuration context.");
63 static void builtin_net_setup_link_exit(void) {
64 link_config_ctx_free(ctx
);
66 log_debug("Unloaded link configuration context.");
69 static bool builtin_net_setup_link_validate(void) {
70 log_debug("Check if link configuration needs reloading.");
74 return link_config_should_reload(ctx
);
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",