]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-net_setup_link.c
treewide: auto-convert the simple cases to log_*_errno()
[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 {
0a1beeb6 49 log_error_errno(-r, "Could not get link config: %m");
af6f0d42
TG
50 return EXIT_FAILURE;
51 }
52 }
53
3e137a1b 54 r = link_config_apply(ctx, link, dev, &name);
af6f0d42 55 if (r < 0) {
0a1beeb6 56 log_error_errno(-r, "Could not apply link config to %s: %m", udev_device_get_sysname(dev));
af6f0d42
TG
57 return EXIT_FAILURE;
58 }
59
ad6e5b34
TG
60 udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
61
3e137a1b
TG
62 if (name)
63 udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
64
af6f0d42
TG
65 return EXIT_SUCCESS;
66}
67
0b99c9f8 68static int builtin_net_setup_link_init(struct udev *udev) {
af6f0d42
TG
69 int r;
70
71 if (ctx)
72 return 0;
73
74 r = link_config_ctx_new(&ctx);
75 if (r < 0)
76 return r;
77
78 r = link_config_load(ctx);
79 if (r < 0)
80 return r;
81
01d4590b 82 log_debug("Created link configuration context.");
af6f0d42
TG
83 return 0;
84}
85
0b99c9f8 86static void builtin_net_setup_link_exit(struct udev *udev) {
af6f0d42 87 link_config_ctx_free(ctx);
e0221a35 88 ctx = NULL;
01d4590b 89 log_debug("Unloaded link configuration context.");
af6f0d42
TG
90}
91
0b99c9f8 92static bool builtin_net_setup_link_validate(struct udev *udev) {
01d4590b 93 log_debug("Check if link configuration needs reloading.");
af6f0d42
TG
94 if (!ctx)
95 return false;
96
97 return link_config_should_reload(ctx);
98}
99
0b99c9f8
TG
100const struct udev_builtin udev_builtin_net_setup_link = {
101 .name = "net_setup_link",
102 .cmd = builtin_net_setup_link,
103 .init = builtin_net_setup_link_init,
104 .exit = builtin_net_setup_link_exit,
105 .validate = builtin_net_setup_link_validate,
af6f0d42
TG
106 .help = "configure network link",
107 .run_once = false,
108};