chiark / gitweb /
driverd: implement AddMatch/RemoveMatch logic
[elogind.git] / src / udev / udev-builtin-net_setup_link.c
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
26 static link_config_ctx *ctx = NULL;
27
28 static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) {
29         const char *name;
30         link_config *link;
31         int r;
32
33         if (argc > 1) {
34                 log_error("This program takes no arguments.");
35                 return EXIT_FAILURE;
36         }
37
38         r = link_config_get(ctx, dev, &link);
39         if (r < 0) {
40                 if (r == -ENOENT) {
41                         log_debug("No matching link configuration found");
42                         return EXIT_SUCCESS;
43                 } else {
44                         log_error("Could not get link config");
45                         return EXIT_FAILURE;
46                 }
47         }
48
49         r = link_config_apply(ctx, link, dev, &name);
50         if (r < 0) {
51                 log_error("Could not apply link config to %s", udev_device_get_sysname(dev));
52                 return EXIT_FAILURE;
53         }
54
55         if (name)
56                 udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
57
58         return EXIT_SUCCESS;
59 }
60
61 static int builtin_net_setup_link_init(struct udev *udev) {
62         int r;
63
64         if (ctx)
65                 return 0;
66
67         r = link_config_ctx_new(&ctx);
68         if (r < 0)
69                 return r;
70
71         r = link_config_load(ctx);
72         if (r < 0)
73                 return r;
74
75         log_debug("Created link configuration context");
76         return 0;
77 }
78
79 static void builtin_net_setup_link_exit(struct udev *udev) {
80         link_config_ctx_free(ctx);
81         ctx = NULL;
82         log_debug("Unloaded link configuration context");
83 }
84
85 static bool builtin_net_setup_link_validate(struct udev *udev) {
86         log_debug("Check if link configuration needs reloading");
87         if (!ctx)
88                 return false;
89
90         return link_config_should_reload(ctx);
91 }
92
93 const struct udev_builtin udev_builtin_net_setup_link = {
94         .name = "net_setup_link",
95         .cmd = builtin_net_setup_link,
96         .init = builtin_net_setup_link_init,
97         .exit = builtin_net_setup_link_exit,
98         .validate = builtin_net_setup_link_validate,
99         .help = "configure network link",
100         .run_once = false,
101 };