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