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