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" |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 17 | #include <stdlib.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 18 | #include <string.h> |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <errno.h> |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #include <sys/timerfd.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 23 | #include <systemd/sd-bus.h> |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 24 | #include <systemd/sd-event.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 25 | #include "mapper.h" |
| 26 | |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 27 | static const char *async_wait_introspection_match = |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 28 | "type='signal'," |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 29 | "sender='xyz.openbmc_project.ObjectMapper'," |
| 30 | "interface='xyz.openbmc_project.ObjectMapper.Private'," |
| 31 | "member='IntrospectionComplete'"; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 32 | |
| 33 | static const char *async_wait_interfaces_added_match = |
| 34 | "type='signal'," |
| 35 | "interface='org.freedesktop.DBus.ObjectManager'," |
| 36 | "member='InterfacesAdded'"; |
| 37 | |
Adriana Kobylak | 9f4c3c7 | 2017-03-28 09:02:20 -0500 | [diff] [blame^] | 38 | static const char *async_wait_interfaces_removed_match = |
| 39 | "type='signal'," |
| 40 | "interface='org.freedesktop.DBus.ObjectManager'," |
| 41 | "member='InterfacesRemoved'"; |
| 42 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 43 | static const int mapper_busy_retries = 5; |
| 44 | static const uint64_t mapper_busy_delay_interval_usec = 1000000; |
| 45 | |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 46 | struct mapper_async_wait |
| 47 | { |
| 48 | char **objs; |
| 49 | void (*callback)(int, void *); |
| 50 | void *userdata; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 51 | sd_event *loop; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 52 | sd_bus *conn; |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 53 | sd_bus_slot *introspection_slot; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 54 | sd_bus_slot *intf_slot; |
| 55 | int *status; |
| 56 | int count; |
| 57 | int finished; |
| 58 | int r; |
Adriana Kobylak | 9f4c3c7 | 2017-03-28 09:02:20 -0500 | [diff] [blame^] | 59 | bool added; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct async_wait_callback_data |
| 63 | { |
| 64 | mapper_async_wait *wait; |
| 65 | const char *path; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 66 | sd_event_source *event_source; |
| 67 | int retry; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 68 | }; |
| 69 | |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 70 | static int async_wait_match_introspection_complete(sd_bus_message *, void *, |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 71 | sd_bus_error *); |
| 72 | static int async_wait_check_done(mapper_async_wait *); |
| 73 | static void async_wait_done(int r, mapper_async_wait *); |
| 74 | static int async_wait_get_objects(mapper_async_wait *); |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 75 | static int async_wait_getobject_callback(sd_bus_message *, |
| 76 | void *, sd_bus_error *); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 77 | |
| 78 | static int sarraylen(char *array[]) |
| 79 | { |
| 80 | int count = 0; |
| 81 | char **p = array; |
| 82 | |
| 83 | while(*p != NULL) { |
| 84 | ++count; |
| 85 | ++p; |
| 86 | } |
| 87 | |
| 88 | return count; |
| 89 | } |
| 90 | |
| 91 | static void sarrayfree(char *array[]) |
| 92 | { |
| 93 | char **p = array; |
| 94 | while(*p != NULL) { |
| 95 | free(*p); |
| 96 | ++p; |
| 97 | } |
| 98 | free(array); |
| 99 | } |
| 100 | |
| 101 | static char **sarraydup(char *array[]) |
| 102 | { |
| 103 | int count = sarraylen(array); |
| 104 | int i; |
| 105 | char **ret = NULL; |
| 106 | |
| 107 | ret = malloc(sizeof(*ret) * count); |
| 108 | if(!ret) |
| 109 | return NULL; |
| 110 | |
| 111 | for(i=0; i<count; ++i) { |
| 112 | ret[i] = strdup(array[i]); |
| 113 | if(!ret[i]) |
| 114 | goto error; |
| 115 | } |
| 116 | |
| 117 | return ret; |
| 118 | |
| 119 | error: |
| 120 | sarrayfree(ret); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 124 | static int async_wait_timeout_callback(sd_event_source *s, |
| 125 | uint64_t usec, void *userdata) |
| 126 | { |
| 127 | int r; |
| 128 | struct async_wait_callback_data *data = userdata; |
| 129 | mapper_async_wait *wait = data->wait; |
| 130 | |
| 131 | sd_event_source_unref(data->event_source); |
| 132 | r = sd_bus_call_method_async( |
| 133 | wait->conn, |
| 134 | NULL, |
Brad Bishop | 36eb1e5 | 2016-11-01 15:18:36 -0400 | [diff] [blame] | 135 | MAPPER_BUSNAME, |
| 136 | MAPPER_PATH, |
| 137 | MAPPER_INTERFACE, |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 138 | "GetObject", |
| 139 | async_wait_getobject_callback, |
| 140 | data, |
Brad Bishop | e1c7cf9 | 2016-11-02 10:20:35 -0400 | [diff] [blame] | 141 | "sas", |
| 142 | data->path, |
| 143 | 0, |
| 144 | NULL); |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 145 | if(r < 0) { |
| 146 | async_wait_done(r, wait); |
| 147 | free(data); |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 153 | static int async_wait_getobject_callback(sd_bus_message *m, |
| 154 | void *userdata, |
| 155 | sd_bus_error *e) |
| 156 | { |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 157 | int i, r; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 158 | struct async_wait_callback_data *data = userdata; |
| 159 | mapper_async_wait *wait = data->wait; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 160 | uint64_t now; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 161 | |
| 162 | if(wait->finished) |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 163 | goto exit; |
| 164 | |
| 165 | r = sd_bus_message_get_errno(m); |
Adriana Kobylak | 9f4c3c7 | 2017-03-28 09:02:20 -0500 | [diff] [blame^] | 166 | if(r == ENOENT) { |
| 167 | if (wait->added) |
| 168 | goto exit; |
| 169 | else |
| 170 | r = 0; |
| 171 | } |
Brad Bishop | 8ccdaed | 2016-09-20 15:54:32 -0400 | [diff] [blame] | 172 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 173 | if(r == EBUSY && data->retry < mapper_busy_retries) { |
| 174 | r = sd_event_now(wait->loop, |
| 175 | CLOCK_MONOTONIC, |
| 176 | &now); |
| 177 | if(r < 0) { |
| 178 | async_wait_done(r, wait); |
| 179 | goto exit; |
| 180 | } |
| 181 | |
| 182 | ++data->retry; |
| 183 | r = sd_event_add_time(wait->loop, |
| 184 | &data->event_source, |
| 185 | CLOCK_MONOTONIC, |
| 186 | now + mapper_busy_delay_interval_usec, |
| 187 | 0, |
| 188 | async_wait_timeout_callback, |
| 189 | data); |
| 190 | if(r < 0) { |
| 191 | async_wait_done(r, wait); |
| 192 | goto exit; |
| 193 | } |
| 194 | |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 195 | return 0; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Brad Bishop | 8ccdaed | 2016-09-20 15:54:32 -0400 | [diff] [blame] | 198 | if(r) { |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 199 | async_wait_done(-r, wait); |
| 200 | goto exit; |
| 201 | } |
| 202 | |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 203 | for(i=0; i<wait->count; ++i) { |
| 204 | if(!strcmp(data->path, wait->objs[i])) { |
| 205 | wait->status[i] = 1; |
| 206 | } |
| 207 | } |
| 208 | |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 209 | if(async_wait_check_done(wait)) |
| 210 | async_wait_done(0, wait); |
| 211 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 212 | exit: |
| 213 | free(data); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int async_wait_get_objects(mapper_async_wait *wait) |
| 218 | { |
| 219 | int i, r; |
| 220 | struct async_wait_callback_data *data = NULL; |
| 221 | |
| 222 | for(i=0; i<wait->count; ++i) { |
| 223 | if(wait->status[i]) |
| 224 | continue; |
| 225 | data = malloc(sizeof(*data)); |
| 226 | data->wait = wait; |
| 227 | data->path = wait->objs[i]; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 228 | data->retry = 0; |
| 229 | data->event_source = NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 230 | r = sd_bus_call_method_async( |
| 231 | wait->conn, |
| 232 | NULL, |
Brad Bishop | 36eb1e5 | 2016-11-01 15:18:36 -0400 | [diff] [blame] | 233 | MAPPER_BUSNAME, |
| 234 | MAPPER_PATH, |
| 235 | MAPPER_INTERFACE, |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 236 | "GetObject", |
| 237 | async_wait_getobject_callback, |
| 238 | data, |
Brad Bishop | e1c7cf9 | 2016-11-02 10:20:35 -0400 | [diff] [blame] | 239 | "sas", |
| 240 | wait->objs[i], |
| 241 | 0, |
| 242 | NULL); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 243 | if(r < 0) { |
| 244 | free(data); |
| 245 | fprintf(stderr, "Error invoking method: %s\n", |
| 246 | strerror(-r)); |
| 247 | return r; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 254 | static int async_wait_match_introspection_complete(sd_bus_message *m, void *w, |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 255 | sd_bus_error *e) |
| 256 | { |
Brad Bishop | a6797f8 | 2016-08-30 13:02:46 -0400 | [diff] [blame] | 257 | int r; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 258 | |
| 259 | mapper_async_wait *wait = w; |
| 260 | if(wait->finished) |
| 261 | return 0; |
| 262 | |
| 263 | r = async_wait_get_objects(wait); |
| 264 | if(r < 0) |
| 265 | async_wait_done(r, wait); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 270 | static void async_wait_done(int r, mapper_async_wait *w) |
| 271 | { |
| 272 | if(w->finished) |
| 273 | return; |
| 274 | |
| 275 | w->finished = 1; |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 276 | sd_bus_slot_unref(w->introspection_slot); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 277 | sd_bus_slot_unref(w->intf_slot); |
| 278 | |
| 279 | if(w->callback) |
| 280 | w->callback(r, w->userdata); |
| 281 | } |
| 282 | |
| 283 | static int async_wait_check_done(mapper_async_wait *w) |
| 284 | { |
| 285 | int i; |
| 286 | |
| 287 | if(w->finished) |
| 288 | return 1; |
| 289 | |
| 290 | for(i=0; i<w->count; ++i) |
| 291 | if(!w->status[i]) |
| 292 | return 0; |
| 293 | |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | void mapper_wait_async_free(mapper_async_wait *w) |
| 298 | { |
| 299 | free(w->status); |
| 300 | sarrayfree(w->objs); |
| 301 | free(w); |
| 302 | } |
| 303 | |
| 304 | int mapper_wait_async(sd_bus *conn, |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 305 | sd_event *loop, |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 306 | char *objs[], |
| 307 | void (*callback)(int, void *), |
| 308 | void *userdata, |
Adriana Kobylak | 9f4c3c7 | 2017-03-28 09:02:20 -0500 | [diff] [blame^] | 309 | mapper_async_wait **w, |
| 310 | bool added) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 311 | { |
| 312 | int r; |
| 313 | mapper_async_wait *wait = NULL; |
| 314 | |
| 315 | wait = malloc(sizeof(*wait)); |
| 316 | if(!wait) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | memset(wait, 0, sizeof(*wait)); |
| 320 | wait->conn = conn; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 321 | wait->loop = loop; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 322 | wait->callback = callback; |
| 323 | wait->userdata = userdata; |
| 324 | wait->count = sarraylen(objs); |
| 325 | if(!wait->count) |
| 326 | return 0; |
Adriana Kobylak | 9f4c3c7 | 2017-03-28 09:02:20 -0500 | [diff] [blame^] | 327 | wait->added = added; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 328 | |
| 329 | wait->objs = sarraydup(objs); |
| 330 | if(!wait->objs) { |
| 331 | r = -ENOMEM; |
| 332 | goto free_wait; |
| 333 | } |
| 334 | |
| 335 | wait->status = malloc(sizeof(*wait->status) * wait->count); |
| 336 | if(!wait->status) { |
| 337 | r = -ENOMEM; |
| 338 | goto free_objs; |
| 339 | } |
| 340 | memset(wait->status, 0, sizeof(*wait->status) * wait->count); |
| 341 | |
Adriana Kobylak | 9f4c3c7 | 2017-03-28 09:02:20 -0500 | [diff] [blame^] | 342 | if (wait->added) { |
| 343 | r = sd_bus_add_match(conn, |
| 344 | &wait->introspection_slot, |
| 345 | async_wait_introspection_match, |
| 346 | async_wait_match_introspection_complete, |
| 347 | wait); |
| 348 | if(r < 0) { |
| 349 | fprintf(stderr, "Error adding match rule: %s\n", |
| 350 | strerror(-r)); |
| 351 | goto free_status; |
| 352 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 353 | |
Adriana Kobylak | 9f4c3c7 | 2017-03-28 09:02:20 -0500 | [diff] [blame^] | 354 | r = sd_bus_add_match(conn, |
| 355 | &wait->intf_slot, |
| 356 | async_wait_interfaces_added_match, |
| 357 | async_wait_match_introspection_complete, |
| 358 | wait); |
| 359 | if(r < 0) { |
| 360 | fprintf(stderr, "Error adding match rule: %s\n", |
| 361 | strerror(-r)); |
| 362 | goto unref_name_slot; |
| 363 | } |
| 364 | } else { |
| 365 | r = sd_bus_add_match(conn, |
| 366 | &wait->intf_slot, |
| 367 | async_wait_interfaces_removed_match, |
| 368 | async_wait_match_introspection_complete, |
| 369 | wait); |
| 370 | if(r < 0) { |
| 371 | fprintf(stderr, "Error adding match rule: %s\n", |
| 372 | strerror(-r)); |
| 373 | goto unref_name_slot; |
| 374 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | r = async_wait_get_objects(wait); |
| 378 | if(r < 0) { |
| 379 | fprintf(stderr, "Error calling method: %s\n", |
| 380 | strerror(-r)); |
| 381 | goto unref_intf_slot; |
| 382 | } |
| 383 | |
| 384 | *w = wait; |
| 385 | |
| 386 | return 0; |
| 387 | |
| 388 | unref_intf_slot: |
| 389 | sd_bus_slot_unref(wait->intf_slot); |
| 390 | unref_name_slot: |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 391 | sd_bus_slot_unref(wait->introspection_slot); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 392 | free_status: |
| 393 | free(wait->status); |
| 394 | free_objs: |
| 395 | sarrayfree(wait->objs); |
| 396 | free_wait: |
| 397 | free(wait); |
| 398 | |
| 399 | return r; |
| 400 | } |
| 401 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 402 | 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] | 403 | { |
| 404 | sd_bus_error error = SD_BUS_ERROR_NULL; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 405 | sd_bus_message *request = NULL; |
| 406 | int r, retry = 0; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 407 | |
| 408 | r = sd_bus_message_new_method_call( |
| 409 | conn, |
| 410 | &request, |
Brad Bishop | 36eb1e5 | 2016-11-01 15:18:36 -0400 | [diff] [blame] | 411 | MAPPER_BUSNAME, |
| 412 | MAPPER_PATH, |
| 413 | MAPPER_INTERFACE, |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 414 | "GetObject"); |
| 415 | if (r < 0) |
| 416 | goto exit; |
| 417 | |
| 418 | r = sd_bus_message_append(request, "s", obj); |
| 419 | if (r < 0) |
| 420 | goto exit; |
Brad Bishop | e1c7cf9 | 2016-11-02 10:20:35 -0400 | [diff] [blame] | 421 | r = sd_bus_message_append(request, "as", 0, NULL); |
| 422 | if (r < 0) |
| 423 | goto exit; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 424 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 425 | while(retry < mapper_busy_retries) { |
| 426 | sd_bus_error_free(&error); |
| 427 | r = sd_bus_call(conn, request, 0, &error, reply); |
| 428 | if (r < 0 && sd_bus_error_get_errno(&error) == EBUSY) { |
| 429 | ++retry; |
| 430 | |
| 431 | if(retry != mapper_busy_retries) |
| 432 | usleep(mapper_busy_delay_interval_usec); |
| 433 | continue; |
| 434 | } |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | if (r < 0) |
| 439 | goto exit; |
| 440 | |
| 441 | exit: |
| 442 | sd_bus_error_free(&error); |
| 443 | sd_bus_message_unref(request); |
| 444 | |
| 445 | return r; |
| 446 | } |
| 447 | |
| 448 | int mapper_get_service(sd_bus *conn, const char *obj, char **service) |
| 449 | { |
| 450 | sd_bus_message *reply = NULL; |
| 451 | const char *tmp; |
| 452 | int r; |
| 453 | |
| 454 | r = mapper_get_object(conn, obj, &reply); |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 455 | if (r < 0) |
| 456 | goto exit; |
| 457 | |
| 458 | r = sd_bus_message_enter_container(reply, 0, NULL); |
| 459 | if (r < 0) |
| 460 | goto exit; |
| 461 | |
| 462 | r = sd_bus_message_enter_container(reply, 0, NULL); |
| 463 | if (r < 0) |
| 464 | goto exit; |
| 465 | |
| 466 | r = sd_bus_message_read(reply, "s", &tmp); |
| 467 | if (r < 0) |
| 468 | goto exit; |
| 469 | |
| 470 | *service = strdup(tmp); |
| 471 | |
| 472 | exit: |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 473 | sd_bus_message_unref(reply); |
| 474 | |
| 475 | return r; |
| 476 | } |