Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brad Bishop | 36eb1e5 | 2016-11-01 15:18:36 -0400 | [diff] [blame] | 16 | #include "config.h" |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 17 | |
| 18 | #include "mapper.h" |
| 19 | |
| 20 | #include <errno.h> |
William A. Kennington III | 20cbbfb | 2018-06-15 10:10:13 -0700 | [diff] [blame] | 21 | #include <stdbool.h> |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 22 | #include <stdio.h> |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 23 | #include <stdlib.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 24 | #include <string.h> |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 25 | #include <sys/timerfd.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 26 | #include <systemd/sd-bus.h> |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 27 | #include <systemd/sd-event.h> |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 28 | #include <unistd.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 29 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 30 | static const char* async_wait_introspection_match = |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 31 | "type='signal'," |
| 32 | "sender='xyz.openbmc_project.ObjectMapper'," |
| 33 | "interface='xyz.openbmc_project.ObjectMapper.Private'," |
| 34 | "member='IntrospectionComplete'"; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 35 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 36 | static const char* async_wait_interfaces_added_match = |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 37 | "type='signal'," |
| 38 | "interface='org.freedesktop.DBus.ObjectManager'," |
| 39 | "member='InterfacesAdded'"; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 40 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 41 | static const char* interfaces_removed_match = |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 42 | "type='signal'," |
| 43 | "interface='org.freedesktop.DBus.ObjectManager'," |
| 44 | "member='InterfacesRemoved'"; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 45 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 46 | static const int mapper_busy_retries = 5; |
| 47 | static const uint64_t mapper_busy_delay_interval_usec = 1000000; |
| 48 | |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 49 | struct mapper_async_wait |
| 50 | { |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 51 | char** objs; |
| 52 | void (*callback)(int, void*); |
| 53 | void* userdata; |
| 54 | sd_event* loop; |
| 55 | sd_bus* conn; |
| 56 | sd_bus_slot* introspection_slot; |
| 57 | sd_bus_slot* intf_slot; |
| 58 | int* status; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 59 | int count; |
| 60 | int finished; |
| 61 | int r; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct async_wait_callback_data |
| 65 | { |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 66 | mapper_async_wait* wait; |
| 67 | const char* path; |
| 68 | sd_event_source* event_source; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 69 | int retry; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 70 | }; |
| 71 | |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 72 | struct mapper_async_subtree |
| 73 | { |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 74 | char* namespace; |
| 75 | char* interface; |
| 76 | void (*callback)(int, void*); |
| 77 | void* userdata; |
| 78 | sd_event* loop; |
| 79 | sd_bus* conn; |
| 80 | sd_bus_slot* slot; |
| 81 | sd_event_source* event_source; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 82 | int finished; |
| 83 | int op; |
| 84 | int retry; |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 87 | static int async_wait_match_introspection_complete(sd_bus_message*, void*, |
| 88 | sd_bus_error*); |
| 89 | static int async_wait_check_done(mapper_async_wait*); |
| 90 | static void async_wait_done(int r, mapper_async_wait*); |
| 91 | static int async_wait_get_objects(mapper_async_wait*); |
| 92 | static int async_wait_getobject_callback(sd_bus_message*, void*, sd_bus_error*); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 93 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 94 | static int async_subtree_match_callback(sd_bus_message*, void*, sd_bus_error*); |
| 95 | static void async_subtree_done(int r, mapper_async_subtree*); |
| 96 | static int async_subtree_getpaths(mapper_async_subtree*); |
| 97 | static int async_subtree_getpaths_callback(sd_bus_message*, void*, |
| 98 | sd_bus_error*); |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 99 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 100 | static int sarraylen(char* array[]) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 101 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 102 | int count = 0; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 103 | char** p = array; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 104 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 105 | while (*p != NULL) |
| 106 | { |
| 107 | ++count; |
| 108 | ++p; |
| 109 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 110 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 111 | return count; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 114 | static void sarrayfree(char* array[]) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 115 | { |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 116 | char** p = array; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 117 | while (*p != NULL) |
| 118 | { |
| 119 | free(*p); |
| 120 | ++p; |
| 121 | } |
| 122 | free(array); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 125 | static char** sarraydup(char* array[]) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 126 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 127 | int count = sarraylen(array); |
| 128 | int i; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 129 | char** ret = NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 130 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 131 | ret = malloc(sizeof(*ret) * count); |
| 132 | if (!ret) |
| 133 | return NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 134 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 135 | for (i = 0; i < count; ++i) |
| 136 | { |
| 137 | ret[i] = strdup(array[i]); |
| 138 | if (!ret[i]) |
| 139 | goto error; |
| 140 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 141 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 142 | return ret; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 143 | |
| 144 | error: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 145 | sarrayfree(ret); |
| 146 | return NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 149 | static int async_wait_timeout_callback(sd_event_source* s, uint64_t usec, |
| 150 | void* userdata) |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 151 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 152 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 153 | struct async_wait_callback_data* data = userdata; |
| 154 | mapper_async_wait* wait = data->wait; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 155 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 156 | sd_event_source_unref(data->event_source); |
| 157 | r = sd_bus_call_method_async(wait->conn, NULL, MAPPER_BUSNAME, MAPPER_PATH, |
| 158 | MAPPER_INTERFACE, "GetObject", |
| 159 | async_wait_getobject_callback, data, "sas", |
| 160 | data->path, 0, NULL); |
| 161 | if (r < 0) |
| 162 | { |
| 163 | async_wait_done(r, wait); |
| 164 | free(data); |
| 165 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 166 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 167 | return 0; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 170 | static int async_wait_getobject_callback(sd_bus_message* m, void* userdata, |
| 171 | sd_bus_error* e) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 172 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 173 | int i, r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 174 | struct async_wait_callback_data* data = userdata; |
| 175 | mapper_async_wait* wait = data->wait; |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 176 | uint64_t next_retry; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 177 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 178 | if (wait->finished) |
| 179 | goto exit; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 180 | |
Patrick Williams | e82b058 | 2020-11-16 16:25:20 -0600 | [diff] [blame] | 181 | if (sd_bus_message_is_method_error( |
| 182 | m, "xyz.openbmc_project.Common.Error.ResourceNotFound")) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 183 | goto exit; |
Brad Bishop | 8ccdaed | 2016-09-20 15:54:32 -0400 | [diff] [blame] | 184 | |
Patrick Williams | e82b058 | 2020-11-16 16:25:20 -0600 | [diff] [blame] | 185 | r = sd_bus_message_get_errno(m); |
| 186 | |
William A. Kennington III | 2482946 | 2018-06-15 10:10:25 -0700 | [diff] [blame] | 187 | if ((r == EBUSY || r == ENOBUFS) && data->retry < mapper_busy_retries) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 188 | { |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 189 | r = sd_event_now(wait->loop, CLOCK_MONOTONIC, &next_retry); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 190 | if (r < 0) |
| 191 | { |
| 192 | async_wait_done(r, wait); |
| 193 | goto exit; |
| 194 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 195 | |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 196 | next_retry += mapper_busy_delay_interval_usec * (1 << data->retry); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 197 | r = sd_event_add_time(wait->loop, &data->event_source, CLOCK_MONOTONIC, |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 198 | next_retry, 0, async_wait_timeout_callback, data); |
| 199 | ++data->retry; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 200 | if (r < 0) |
| 201 | { |
| 202 | async_wait_done(r, wait); |
| 203 | goto exit; |
| 204 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 205 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 208 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 209 | if (r) |
| 210 | { |
| 211 | async_wait_done(-r, wait); |
| 212 | goto exit; |
| 213 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 214 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 215 | for (i = 0; i < wait->count; ++i) |
| 216 | { |
| 217 | if (!strcmp(data->path, wait->objs[i])) |
| 218 | { |
| 219 | wait->status[i] = 1; |
| 220 | } |
| 221 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 222 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 223 | if (async_wait_check_done(wait)) |
| 224 | async_wait_done(0, wait); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 225 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 226 | exit: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 227 | free(data); |
| 228 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 231 | static int async_wait_get_objects(mapper_async_wait* wait) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 232 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 233 | int i, r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 234 | struct async_wait_callback_data* data = NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 235 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 236 | for (i = 0; i < wait->count; ++i) |
| 237 | { |
| 238 | if (wait->status[i]) |
| 239 | continue; |
| 240 | data = malloc(sizeof(*data)); |
| 241 | data->wait = wait; |
| 242 | data->path = wait->objs[i]; |
| 243 | data->retry = 0; |
| 244 | data->event_source = NULL; |
| 245 | r = sd_bus_call_method_async(wait->conn, NULL, MAPPER_BUSNAME, |
| 246 | MAPPER_PATH, MAPPER_INTERFACE, "GetObject", |
| 247 | async_wait_getobject_callback, data, "sas", |
| 248 | wait->objs[i], 0, NULL); |
| 249 | if (r < 0) |
| 250 | { |
| 251 | free(data); |
| 252 | fprintf(stderr, "Error invoking method: %s\n", strerror(-r)); |
| 253 | return r; |
| 254 | } |
| 255 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 256 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 257 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 260 | static int async_wait_match_introspection_complete(sd_bus_message* m, void* w, |
| 261 | sd_bus_error* e) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 262 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 263 | int r; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 264 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 265 | mapper_async_wait* wait = w; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 266 | if (wait->finished) |
| 267 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 268 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 269 | r = async_wait_get_objects(wait); |
| 270 | if (r < 0) |
| 271 | async_wait_done(r, wait); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 272 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 273 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 274 | } |
| 275 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 276 | static void async_wait_done(int r, mapper_async_wait* w) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 277 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 278 | if (w->finished) |
| 279 | return; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 280 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 281 | w->finished = 1; |
| 282 | sd_bus_slot_unref(w->introspection_slot); |
| 283 | sd_bus_slot_unref(w->intf_slot); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 284 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 285 | if (w->callback) |
| 286 | w->callback(r, w->userdata); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 289 | static int async_wait_check_done(mapper_async_wait* w) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 290 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 291 | int i; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 292 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 293 | if (w->finished) |
| 294 | return 1; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 295 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 296 | for (i = 0; i < w->count; ++i) |
| 297 | if (!w->status[i]) |
| 298 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 299 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 300 | return 1; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 303 | void mapper_wait_async_free(mapper_async_wait* w) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 304 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 305 | free(w->status); |
| 306 | sarrayfree(w->objs); |
| 307 | free(w); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 310 | int mapper_wait_async(sd_bus* conn, sd_event* loop, char* objs[], |
| 311 | void (*callback)(int, void*), void* userdata, |
| 312 | mapper_async_wait** w) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 313 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 314 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 315 | mapper_async_wait* wait = NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 316 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 317 | wait = malloc(sizeof(*wait)); |
| 318 | if (!wait) |
| 319 | return -ENOMEM; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 320 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 321 | memset(wait, 0, sizeof(*wait)); |
| 322 | wait->conn = conn; |
| 323 | wait->loop = loop; |
| 324 | wait->callback = callback; |
| 325 | wait->userdata = userdata; |
| 326 | wait->count = sarraylen(objs); |
| 327 | if (!wait->count) |
| 328 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 329 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 330 | wait->objs = sarraydup(objs); |
| 331 | if (!wait->objs) |
| 332 | { |
| 333 | r = -ENOMEM; |
| 334 | goto free_wait; |
| 335 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 336 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 337 | wait->status = malloc(sizeof(*wait->status) * wait->count); |
| 338 | if (!wait->status) |
| 339 | { |
| 340 | r = -ENOMEM; |
| 341 | goto free_objs; |
| 342 | } |
| 343 | memset(wait->status, 0, sizeof(*wait->status) * wait->count); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 344 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 345 | r = sd_bus_add_match(conn, &wait->introspection_slot, |
| 346 | async_wait_introspection_match, |
| 347 | async_wait_match_introspection_complete, wait); |
| 348 | if (r < 0) |
| 349 | { |
| 350 | fprintf(stderr, "Error adding match rule: %s\n", strerror(-r)); |
| 351 | goto free_status; |
| 352 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 353 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 354 | r = sd_bus_add_match(conn, &wait->intf_slot, |
| 355 | async_wait_interfaces_added_match, |
| 356 | async_wait_match_introspection_complete, wait); |
| 357 | if (r < 0) |
| 358 | { |
| 359 | fprintf(stderr, "Error adding match rule: %s\n", strerror(-r)); |
| 360 | goto unref_name_slot; |
| 361 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 362 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 363 | r = async_wait_get_objects(wait); |
| 364 | if (r < 0) |
| 365 | { |
| 366 | fprintf(stderr, "Error calling method: %s\n", strerror(-r)); |
| 367 | goto unref_intf_slot; |
| 368 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 369 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 370 | *w = wait; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 371 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 372 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 373 | |
| 374 | unref_intf_slot: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 375 | sd_bus_slot_unref(wait->intf_slot); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 376 | unref_name_slot: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 377 | sd_bus_slot_unref(wait->introspection_slot); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 378 | free_status: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 379 | free(wait->status); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 380 | free_objs: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 381 | sarrayfree(wait->objs); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 382 | free_wait: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 383 | free(wait); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 384 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 385 | return r; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 388 | static int async_subtree_timeout_callback(sd_event_source* s, uint64_t usec, |
| 389 | void* userdata) |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 390 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 391 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 392 | struct mapper_async_subtree* subtree = userdata; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 393 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 394 | sd_event_source_unref(subtree->event_source); |
| 395 | r = sd_bus_call_method_async( |
| 396 | subtree->conn, NULL, MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE, |
| 397 | "GetSubTreePaths", async_subtree_getpaths_callback, subtree, "sias", |
| 398 | subtree->namespace, 0, 1, subtree->interface); |
| 399 | if (r < 0) |
| 400 | async_subtree_done(r, subtree); |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 401 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 402 | return 0; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 403 | } |
| 404 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 405 | static int async_subtree_getpaths_callback(sd_bus_message* m, void* userdata, |
| 406 | sd_bus_error* e) |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 407 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 408 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 409 | struct mapper_async_subtree* subtree = userdata; |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 410 | uint64_t next_retry; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 411 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 412 | if (subtree->finished) |
| 413 | goto exit; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 414 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 415 | r = sd_bus_message_get_errno(m); |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 416 | |
Patrick Williams | e82b058 | 2020-11-16 16:25:20 -0600 | [diff] [blame] | 417 | if (sd_bus_message_is_method_error( |
| 418 | m, "xyz.openbmc_project.Common.Error.ResourceNotFound")) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 419 | { |
| 420 | if (subtree->op == MAPPER_OP_REMOVE) |
| 421 | r = 0; |
| 422 | else |
| 423 | goto exit; |
| 424 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 425 | |
William A. Kennington III | 2482946 | 2018-06-15 10:10:25 -0700 | [diff] [blame] | 426 | if ((r == EBUSY || r == ENOBUFS) && subtree->retry < mapper_busy_retries) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 427 | { |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 428 | r = sd_event_now(subtree->loop, CLOCK_MONOTONIC, &next_retry); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 429 | if (r < 0) |
| 430 | { |
| 431 | async_subtree_done(r, subtree); |
| 432 | goto exit; |
| 433 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 434 | |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 435 | next_retry += mapper_busy_delay_interval_usec * (1 << subtree->retry); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 436 | r = sd_event_add_time(subtree->loop, &subtree->event_source, |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 437 | CLOCK_MONOTONIC, next_retry, 0, |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 438 | async_subtree_timeout_callback, subtree); |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 439 | ++subtree->retry; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 440 | if (r < 0) |
| 441 | { |
| 442 | async_subtree_done(r, subtree); |
| 443 | goto exit; |
| 444 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 445 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 446 | return 0; |
| 447 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 448 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 449 | if (r) |
| 450 | { |
| 451 | async_subtree_done(-r, subtree); |
| 452 | goto exit; |
| 453 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 454 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 455 | if (subtree->op == MAPPER_OP_REMOVE) |
| 456 | { |
William A. Kennington III | 20cbbfb | 2018-06-15 10:10:13 -0700 | [diff] [blame] | 457 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s"); |
| 458 | if (r < 0) |
| 459 | { |
| 460 | async_subtree_done(r, subtree); |
| 461 | goto exit; |
| 462 | } |
| 463 | |
| 464 | r = sd_bus_message_at_end(m, false); |
| 465 | if (r < 0) |
| 466 | { |
| 467 | async_subtree_done(r, subtree); |
| 468 | goto exit; |
| 469 | } |
| 470 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 471 | /* For remove, operation is complete when the interface is not present |
William A. Kennington III | 20cbbfb | 2018-06-15 10:10:13 -0700 | [diff] [blame] | 472 | * we know it is empty if the returned array is empty |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 473 | */ |
William A. Kennington III | 20cbbfb | 2018-06-15 10:10:13 -0700 | [diff] [blame] | 474 | if (r) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 475 | async_subtree_done(0, subtree); |
| 476 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 477 | |
| 478 | exit: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 479 | return 0; |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 480 | } |
| 481 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 482 | static int async_subtree_getpaths(mapper_async_subtree* subtree) |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 483 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 484 | int r = 0; |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 485 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 486 | subtree->retry = 0; |
| 487 | subtree->event_source = NULL; |
| 488 | r = sd_bus_call_method_async( |
| 489 | subtree->conn, NULL, MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE, |
| 490 | "GetSubTreePaths", async_subtree_getpaths_callback, subtree, "sias", |
| 491 | subtree->namespace, 0, 1, subtree->interface); |
| 492 | if (r < 0) |
| 493 | { |
| 494 | fprintf(stderr, "Error invoking method: %s\n", strerror(-r)); |
| 495 | return r; |
| 496 | } |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 497 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 498 | return 0; |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 499 | } |
| 500 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 501 | static int async_subtree_match_callback(sd_bus_message* m, void* t, |
| 502 | sd_bus_error* e) |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 503 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 504 | int r; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 505 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 506 | mapper_async_subtree* subtree = t; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 507 | if (subtree->finished) |
| 508 | return 0; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 509 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 510 | r = async_subtree_getpaths(subtree); |
| 511 | if (r < 0) |
| 512 | async_subtree_done(r, subtree); |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 513 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 514 | return 0; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 515 | } |
| 516 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 517 | static void async_subtree_done(int r, mapper_async_subtree* t) |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 518 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 519 | if (t->finished) |
| 520 | return; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 521 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 522 | t->finished = 1; |
| 523 | sd_bus_slot_unref(t->slot); |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 524 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 525 | if (t->callback) |
| 526 | t->callback(r, t->userdata); |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 527 | } |
| 528 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 529 | int mapper_subtree_async(sd_bus* conn, sd_event* loop, char* namespace, |
| 530 | char* interface, void (*callback)(int, void*), |
| 531 | void* userdata, mapper_async_subtree** t, int op) |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 532 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 533 | int r = 0; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 534 | mapper_async_subtree* subtree = NULL; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 535 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 536 | subtree = malloc(sizeof(*subtree)); |
| 537 | if (!subtree) |
| 538 | return -ENOMEM; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 539 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 540 | memset(subtree, 0, sizeof(*subtree)); |
| 541 | subtree->conn = conn; |
| 542 | subtree->loop = loop; |
| 543 | subtree->namespace = namespace; |
| 544 | subtree->interface = interface; |
| 545 | subtree->callback = callback; |
| 546 | subtree->userdata = userdata; |
| 547 | subtree->op = op; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 548 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 549 | if (subtree->op == MAPPER_OP_REMOVE) |
| 550 | { |
| 551 | r = sd_bus_add_match(conn, &subtree->slot, interfaces_removed_match, |
| 552 | async_subtree_match_callback, subtree); |
| 553 | if (r < 0) |
| 554 | { |
| 555 | fprintf(stderr, "Error adding match rule: %s\n", strerror(-r)); |
| 556 | goto unref_slot; |
| 557 | } |
| 558 | } |
| 559 | else |
| 560 | { |
| 561 | /* Operation not supported */ |
| 562 | r = -EINVAL; |
| 563 | goto free_subtree; |
| 564 | } |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 565 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 566 | r = async_subtree_getpaths(subtree); |
| 567 | if (r < 0) |
| 568 | { |
| 569 | fprintf(stderr, "Error calling method: %s\n", strerror(-r)); |
| 570 | goto unref_slot; |
| 571 | } |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 572 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 573 | *t = subtree; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 574 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 575 | return 0; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 576 | |
| 577 | unref_slot: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 578 | sd_bus_slot_unref(subtree->slot); |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 579 | free_subtree: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 580 | free(subtree); |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 581 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 582 | return r; |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 583 | } |
| 584 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 585 | int mapper_get_object(sd_bus* conn, const char* obj, sd_bus_message** reply) |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 586 | { |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 587 | sd_bus_message* request = NULL; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 588 | int r, retry = 0; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 589 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 590 | r = sd_bus_message_new_method_call(conn, &request, MAPPER_BUSNAME, |
| 591 | MAPPER_PATH, MAPPER_INTERFACE, |
| 592 | "GetObject"); |
| 593 | if (r < 0) |
| 594 | goto exit; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 595 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 596 | r = sd_bus_message_append(request, "s", obj); |
| 597 | if (r < 0) |
| 598 | goto exit; |
| 599 | r = sd_bus_message_append(request, "as", 0, NULL); |
| 600 | if (r < 0) |
| 601 | goto exit; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 602 | |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 603 | while (true) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 604 | { |
William A. Kennington III | 2482946 | 2018-06-15 10:10:25 -0700 | [diff] [blame] | 605 | r = sd_bus_call(conn, request, 0, NULL, reply); |
| 606 | if (r == -EBUSY || r == -ENOBUFS) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 607 | { |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 608 | if (retry >= mapper_busy_retries) |
| 609 | break; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 610 | |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 611 | usleep(mapper_busy_delay_interval_usec * (1 << retry)); |
| 612 | ++retry; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 613 | continue; |
| 614 | } |
| 615 | break; |
| 616 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 617 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 618 | if (r < 0) |
| 619 | goto exit; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 620 | |
| 621 | exit: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 622 | sd_bus_message_unref(request); |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 623 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 624 | return r; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 625 | } |
| 626 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 627 | int mapper_get_service(sd_bus* conn, const char* obj, char** service) |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 628 | { |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 629 | sd_bus_message* reply = NULL; |
| 630 | const char* tmp; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 631 | int r; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 632 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 633 | r = mapper_get_object(conn, obj, &reply); |
| 634 | if (r < 0) |
| 635 | goto exit; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 636 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 637 | r = sd_bus_message_enter_container(reply, 0, NULL); |
| 638 | if (r < 0) |
| 639 | goto exit; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 640 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 641 | r = sd_bus_message_enter_container(reply, 0, NULL); |
| 642 | if (r < 0) |
| 643 | goto exit; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 644 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 645 | r = sd_bus_message_read(reply, "s", &tmp); |
| 646 | if (r < 0) |
| 647 | goto exit; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 648 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 649 | *service = strdup(tmp); |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 650 | |
| 651 | exit: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 652 | sd_bus_message_unref(reply); |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 653 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 654 | return r; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 655 | } |