]> git.proxmox.com Git - systemd.git/blame - src/udev/udev-builtin-net_setup_link.c
New upstream version 236
[systemd.git] / src / udev / udev-builtin-net_setup_link.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
60f067b4
JS
2/***
3 This file is part of systemd.
4
5 Copyright 2013 Tom Gundersen
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
db2df898 21#include "alloc-util.h"
60f067b4 22#include "link-config.h"
60f067b4 23#include "log.h"
db2df898 24#include "udev.h"
60f067b4
JS
25
26static link_config_ctx *ctx = NULL;
27
28static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) {
29 _cleanup_free_ char *driver = NULL;
e735f4d4 30 const char *name = NULL;
60f067b4
JS
31 link_config *link;
32 int r;
33
34 if (argc > 1) {
35 log_error("This program takes no arguments.");
36 return EXIT_FAILURE;
37 }
38
39 r = link_get_driver(ctx, dev, &driver);
40 if (r >= 0)
41 udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
42
43 r = link_config_get(ctx, dev, &link);
44 if (r < 0) {
45 if (r == -ENOENT) {
e842803a 46 log_debug("No matching link configuration found.");
60f067b4
JS
47 return EXIT_SUCCESS;
48 } else {
f47781d8 49 log_error_errno(r, "Could not get link config: %m");
60f067b4
JS
50 return EXIT_FAILURE;
51 }
52 }
53
54 r = link_config_apply(ctx, link, dev, &name);
52ad194e
MB
55 if (r < 0)
56 log_warning_errno(r, "Could not apply link config to %s, ignoring: %m", udev_device_get_sysname(dev));
60f067b4 57
5eef597e
MP
58 udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
59
60f067b4
JS
60 if (name)
61 udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
62
63 return EXIT_SUCCESS;
64}
65
66static int builtin_net_setup_link_init(struct udev *udev) {
67 int r;
68
69 if (ctx)
70 return 0;
71
72 r = link_config_ctx_new(&ctx);
73 if (r < 0)
74 return r;
75
76 r = link_config_load(ctx);
77 if (r < 0)
78 return r;
79
e842803a 80 log_debug("Created link configuration context.");
60f067b4
JS
81 return 0;
82}
83
84static void builtin_net_setup_link_exit(struct udev *udev) {
85 link_config_ctx_free(ctx);
86 ctx = NULL;
e842803a 87 log_debug("Unloaded link configuration context.");
60f067b4
JS
88}
89
90static bool builtin_net_setup_link_validate(struct udev *udev) {
e842803a 91 log_debug("Check if link configuration needs reloading.");
60f067b4
JS
92 if (!ctx)
93 return false;
94
95 return link_config_should_reload(ctx);
96}
97
98const struct udev_builtin udev_builtin_net_setup_link = {
99 .name = "net_setup_link",
100 .cmd = builtin_net_setup_link,
101 .init = builtin_net_setup_link_init,
102 .exit = builtin_net_setup_link_exit,
103 .validate = builtin_net_setup_link_validate,
e735f4d4 104 .help = "Configure network link",
60f067b4
JS
105 .run_once = false,
106};