]>
Commit | Line | Data |
---|---|---|
e331e246 TG |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2015 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 "bus-util.h" | |
23 | #include "strv.h" | |
24 | ||
e331e246 | 25 | #include "networkd-link.h" |
6bedfcbb LP |
26 | #include "networkd.h" |
27 | #include "parse-util.h" | |
e331e246 TG |
28 | |
29 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_operational_state, link_operstate, LinkOperationalState); | |
30 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_administrative_state, link_state, LinkState); | |
31 | ||
32 | const sd_bus_vtable link_vtable[] = { | |
33 | SD_BUS_VTABLE_START(0), | |
34 | ||
35 | SD_BUS_PROPERTY("OperationalState", "s", property_get_operational_state, offsetof(Link, operstate), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
36 | SD_BUS_PROPERTY("AdministrativeState", "s", property_get_administrative_state, offsetof(Link, state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
37 | ||
38 | SD_BUS_VTABLE_END | |
39 | }; | |
40 | ||
41 | static char *link_bus_path(Link *link) { | |
c8f5edea | 42 | _cleanup_free_ char *ifindex = NULL; |
e331e246 | 43 | char *p; |
c8f5edea | 44 | int r; |
e331e246 TG |
45 | |
46 | assert(link); | |
47 | assert(link->ifindex > 0); | |
48 | ||
c8f5edea TG |
49 | if (asprintf(&ifindex, "%d", link->ifindex) < 0) |
50 | return NULL; | |
51 | ||
52 | r = sd_bus_path_encode("/org/freedesktop/network1/link", ifindex, &p); | |
53 | if (r < 0) | |
02f50053 | 54 | return NULL; |
e331e246 TG |
55 | |
56 | return p; | |
57 | } | |
58 | ||
59 | int link_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { | |
60 | _cleanup_strv_free_ char **l = NULL; | |
61 | Manager *m = userdata; | |
62 | Link *link; | |
63 | Iterator i; | |
64 | int r; | |
65 | ||
66 | assert(bus); | |
67 | assert(path); | |
68 | assert(m); | |
69 | assert(nodes); | |
70 | ||
71 | HASHMAP_FOREACH(link, m->links, i) { | |
72 | char *p; | |
73 | ||
74 | p = link_bus_path(link); | |
75 | if (!p) | |
76 | return -ENOMEM; | |
77 | ||
78 | r = strv_consume(&l, p); | |
79 | if (r < 0) | |
80 | return r; | |
81 | } | |
82 | ||
83 | *nodes = l; | |
84 | l = NULL; | |
85 | ||
86 | return 1; | |
87 | } | |
88 | ||
89 | int link_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { | |
c8f5edea | 90 | _cleanup_free_ char *identifier = NULL; |
e331e246 TG |
91 | Manager *m = userdata; |
92 | Link *link; | |
93 | int ifindex, r; | |
94 | ||
95 | assert(bus); | |
96 | assert(path); | |
97 | assert(interface); | |
98 | assert(m); | |
99 | assert(found); | |
100 | ||
c8f5edea TG |
101 | r = sd_bus_path_decode(path, "/org/freedesktop/network1/link", &identifier); |
102 | if (r < 0) | |
103 | return 0; | |
104 | ||
1c111814 TG |
105 | r = safe_atoi(identifier, &ifindex); |
106 | if (r < 0) | |
e331e246 TG |
107 | return 0; |
108 | ||
109 | r = link_get(m, ifindex, &link); | |
110 | if (r < 0) | |
111 | return 0; | |
112 | ||
113 | *found = link; | |
114 | ||
115 | return 1; | |
116 | } | |
117 | ||
118 | int link_send_changed(Link *link, const char *property, ...) { | |
119 | _cleanup_free_ char *p = NULL; | |
120 | char **l; | |
121 | ||
122 | assert(link); | |
123 | assert(link->manager); | |
124 | ||
4852e159 TG |
125 | if (!link->manager->bus) |
126 | return 0; /* replace with assert when we have kdbus */ | |
127 | ||
e331e246 TG |
128 | l = strv_from_stdarg_alloca(property); |
129 | ||
130 | p = link_bus_path(link); | |
131 | if (!p) | |
132 | return -ENOMEM; | |
133 | ||
134 | return sd_bus_emit_properties_changed_strv( | |
135 | link->manager->bus, | |
136 | p, | |
137 | "org.freedesktop.network1.Link", | |
138 | l); | |
139 | } |