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