]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-net_setup_link.c
networkctl: show the network file applied to each link
[thirdparty/systemd.git] / src / udev / udev-builtin-net_setup_link.c
CommitLineData
af6f0d42
TG
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2013 Tom Gundersen
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include "link-config.h"
23#include "udev.h"
24#include "log.h"
25
9588bc32 26static link_config_ctx *ctx = NULL;
af6f0d42 27
0b99c9f8 28static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) {
847a8a5f 29 _cleanup_free_ char *driver = NULL;
3e137a1b 30 const char *name;
af6f0d42
TG
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
847a8a5f
TG
39 r = link_get_driver(ctx, dev, &driver);
40 if (r >= 0)
41 udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
42
af6f0d42
TG
43 r = link_config_get(ctx, dev, &link);
44 if (r < 0) {
45 if (r == -ENOENT) {
01d4590b 46 log_debug("No matching link configuration found.");
af6f0d42
TG
47 return EXIT_SUCCESS;
48 } else {
01d4590b 49 log_error("Could not get link config: %s", strerror(-r));
af6f0d42
TG
50 return EXIT_FAILURE;
51 }
52 }
53
3e137a1b 54 r = link_config_apply(ctx, link, dev, &name);
af6f0d42 55 if (r < 0) {
01d4590b 56 log_error("Could not apply link config to %s: %s", udev_device_get_sysname(dev), strerror(-r));
af6f0d42
TG
57 return EXIT_FAILURE;
58 }
59
3e137a1b
TG
60 if (name)
61 udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
62
af6f0d42
TG
63 return EXIT_SUCCESS;
64}
65
0b99c9f8 66static int builtin_net_setup_link_init(struct udev *udev) {
af6f0d42
TG
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
01d4590b 80 log_debug("Created link configuration context.");
af6f0d42
TG
81 return 0;
82}
83
0b99c9f8 84static void builtin_net_setup_link_exit(struct udev *udev) {
af6f0d42 85 link_config_ctx_free(ctx);
e0221a35 86 ctx = NULL;
01d4590b 87 log_debug("Unloaded link configuration context.");
af6f0d42
TG
88}
89
0b99c9f8 90static bool builtin_net_setup_link_validate(struct udev *udev) {
01d4590b 91 log_debug("Check if link configuration needs reloading.");
af6f0d42
TG
92 if (!ctx)
93 return false;
94
95 return link_config_should_reload(ctx);
96}
97
0b99c9f8
TG
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,
af6f0d42
TG
104 .help = "configure network link",
105 .run_once = false,
106};