]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-net_setup_link.c
Merge pull request #8575 from keszybz/non-absolute-paths
[thirdparty/systemd.git] / src / udev / udev-builtin-net_setup_link.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Tom Gundersen
6 ***/
7
8 #include "alloc-util.h"
9 #include "link-config.h"
10 #include "log.h"
11 #include "udev.h"
12
13 static link_config_ctx *ctx = NULL;
14
15 static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) {
16 _cleanup_free_ char *driver = NULL;
17 const char *name = NULL;
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
26 r = link_get_driver(ctx, dev, &driver);
27 if (r >= 0)
28 udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
29
30 r = link_config_get(ctx, dev, &link);
31 if (r < 0) {
32 if (r == -ENOENT) {
33 log_debug("No matching link configuration found.");
34 return EXIT_SUCCESS;
35 } else {
36 log_error_errno(r, "Could not get link config: %m");
37 return EXIT_FAILURE;
38 }
39 }
40
41 r = link_config_apply(ctx, link, dev, &name);
42 if (r < 0)
43 log_warning_errno(r, "Could not apply link config to %s, ignoring: %m", udev_device_get_sysname(dev));
44
45 udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
46
47 if (name)
48 udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
49
50 return EXIT_SUCCESS;
51 }
52
53 static int builtin_net_setup_link_init(struct udev *udev) {
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
67 log_debug("Created link configuration context.");
68 return 0;
69 }
70
71 static void builtin_net_setup_link_exit(struct udev *udev) {
72 link_config_ctx_free(ctx);
73 ctx = NULL;
74 log_debug("Unloaded link configuration context.");
75 }
76
77 static bool builtin_net_setup_link_validate(struct udev *udev) {
78 log_debug("Check if link configuration needs reloading.");
79 if (!ctx)
80 return false;
81
82 return link_config_should_reload(ctx);
83 }
84
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,
91 .help = "Configure network link",
92 .run_once = false,
93 };