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 | */ |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 16 | #include "mapper.h" |
| 17 | |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 18 | #include "internal.h" |
| 19 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 20 | #include <errno.h> |
William A. Kennington III | 20cbbfb | 2018-06-15 10:10:13 -0700 | [diff] [blame] | 21 | #include <stdbool.h> |
Brad Bishop | a959f12 | 2021-08-03 11:21:01 -0400 | [diff] [blame] | 22 | #include <stddef.h> |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 23 | #include <stdio.h> |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 24 | #include <stdlib.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 25 | #include <string.h> |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 26 | #include <sys/timerfd.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 27 | #include <systemd/sd-bus.h> |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 28 | #include <systemd/sd-event.h> |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 29 | #include <unistd.h> |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 30 | |
Brad Bishop | 75057c8 | 2021-08-03 15:22:02 -0400 | [diff] [blame] | 31 | #define _public_ __attribute__((__visibility__("default"))) |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 32 | #define _unused_ __attribute__((unused)) |
Brad Bishop | 75057c8 | 2021-08-03 15:22:02 -0400 | [diff] [blame] | 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; |
Brad Bishop | a959f12 | 2021-08-03 11:21:01 -0400 | [diff] [blame] | 63 | size_t count; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 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 | a959f12 | 2021-08-03 11:21:01 -0400 | [diff] [blame] | 104 | size_t sarraylen(char* array[]) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 105 | { |
Brad Bishop | a959f12 | 2021-08-03 11:21:01 -0400 | [diff] [blame] | 106 | size_t 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 | { |
Brad Bishop | a959f12 | 2021-08-03 11:21:01 -0400 | [diff] [blame] | 131 | size_t count = sarraylen(array); |
| 132 | size_t 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) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 137 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 138 | return NULL; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 139 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 140 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 141 | for (i = 0; i < count; ++i) |
| 142 | { |
| 143 | ret[i] = strdup(array[i]); |
| 144 | if (!ret[i]) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 145 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 146 | goto error; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 147 | } |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 148 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 149 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 150 | return ret; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 151 | |
| 152 | error: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 153 | sarrayfree(ret); |
| 154 | return NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 157 | static int async_wait_timeout_callback(_unused_ sd_event_source* s, |
| 158 | _unused_ uint64_t usec, void* userdata) |
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 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 161 | struct async_wait_callback_data* data = userdata; |
| 162 | mapper_async_wait* wait = data->wait; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 163 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 164 | sd_event_source_unref(data->event_source); |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 165 | r = sd_bus_call_method_async( |
| 166 | wait->conn, NULL, "xyz.openbmc_project.ObjectMapper", |
| 167 | "/xyz/openbmc_project/object_mapper", |
| 168 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
| 169 | async_wait_getobject_callback, data, "sas", data->path, 0, NULL); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 170 | if (r < 0) |
| 171 | { |
| 172 | async_wait_done(r, wait); |
| 173 | free(data); |
| 174 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 175 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 176 | return 0; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 179 | static int async_wait_getobject_callback(sd_bus_message* m, void* userdata, |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 180 | _unused_ sd_bus_error* e) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 181 | { |
Brad Bishop | a959f12 | 2021-08-03 11:21:01 -0400 | [diff] [blame] | 182 | size_t i; |
| 183 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 184 | struct async_wait_callback_data* data = userdata; |
| 185 | mapper_async_wait* wait = data->wait; |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 186 | uint64_t next_retry; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 187 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 188 | if (wait->finished) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 189 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 190 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 191 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 192 | |
Patrick Williams | e82b058 | 2020-11-16 16:25:20 -0600 | [diff] [blame] | 193 | if (sd_bus_message_is_method_error( |
| 194 | m, "xyz.openbmc_project.Common.Error.ResourceNotFound")) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 195 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 196 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 197 | } |
Brad Bishop | 8ccdaed | 2016-09-20 15:54:32 -0400 | [diff] [blame] | 198 | |
Patrick Williams | e82b058 | 2020-11-16 16:25:20 -0600 | [diff] [blame] | 199 | r = sd_bus_message_get_errno(m); |
| 200 | |
William A. Kennington III | 2482946 | 2018-06-15 10:10:25 -0700 | [diff] [blame] | 201 | if ((r == EBUSY || r == ENOBUFS) && data->retry < mapper_busy_retries) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 202 | { |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 203 | r = sd_event_now(wait->loop, CLOCK_MONOTONIC, &next_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 | |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 210 | next_retry += mapper_busy_delay_interval_usec * (1 << data->retry); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 211 | 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] | 212 | next_retry, 0, async_wait_timeout_callback, data); |
| 213 | ++data->retry; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 214 | if (r < 0) |
| 215 | { |
| 216 | async_wait_done(r, wait); |
| 217 | goto exit; |
| 218 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 219 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 222 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 223 | if (r) |
| 224 | { |
| 225 | async_wait_done(-r, wait); |
| 226 | goto exit; |
| 227 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 228 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 229 | for (i = 0; i < wait->count; ++i) |
| 230 | { |
| 231 | if (!strcmp(data->path, wait->objs[i])) |
| 232 | { |
| 233 | wait->status[i] = 1; |
| 234 | } |
| 235 | } |
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 | if (async_wait_check_done(wait)) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 238 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 239 | async_wait_done(0, wait); |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 240 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 241 | |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 242 | exit: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 243 | free(data); |
| 244 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 247 | static int async_wait_get_objects(mapper_async_wait* wait) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 248 | { |
Brad Bishop | a959f12 | 2021-08-03 11:21:01 -0400 | [diff] [blame] | 249 | size_t i; |
| 250 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 251 | struct async_wait_callback_data* data = NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 252 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 253 | for (i = 0; i < wait->count; ++i) |
| 254 | { |
| 255 | if (wait->status[i]) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 256 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 257 | continue; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 258 | } |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 259 | data = malloc(sizeof(*data)); |
| 260 | data->wait = wait; |
| 261 | data->path = wait->objs[i]; |
| 262 | data->retry = 0; |
| 263 | data->event_source = NULL; |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 264 | r = sd_bus_call_method_async( |
| 265 | wait->conn, NULL, "xyz.openbmc_project.ObjectMapper", |
| 266 | "/xyz/openbmc_project/object_mapper", |
| 267 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
| 268 | async_wait_getobject_callback, data, "sas", wait->objs[i], 0, NULL); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 269 | if (r < 0) |
| 270 | { |
| 271 | free(data); |
| 272 | fprintf(stderr, "Error invoking method: %s\n", strerror(-r)); |
| 273 | return r; |
| 274 | } |
| 275 | } |
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 | |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 280 | static int async_wait_match_introspection_complete( |
| 281 | _unused_ sd_bus_message* m, void* w, _unused_ sd_bus_error* e) |
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 | int r; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 284 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 285 | mapper_async_wait* wait = w; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 286 | if (wait->finished) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 287 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 288 | return 0; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 289 | } |
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 | r = async_wait_get_objects(wait); |
| 292 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 293 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 294 | async_wait_done(r, wait); |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 295 | } |
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 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 300 | static void async_wait_done(int r, mapper_async_wait* w) |
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 | if (w->finished) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 303 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 304 | return; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 305 | } |
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 | w->finished = 1; |
| 308 | sd_bus_slot_unref(w->introspection_slot); |
| 309 | sd_bus_slot_unref(w->intf_slot); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 310 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 311 | if (w->callback) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 312 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 313 | w->callback(r, w->userdata); |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 314 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 317 | static int async_wait_check_done(mapper_async_wait* w) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 318 | { |
Brad Bishop | a959f12 | 2021-08-03 11:21:01 -0400 | [diff] [blame] | 319 | size_t i; |
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 | if (w->finished) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 322 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 323 | return 1; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 324 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 325 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 326 | for (i = 0; i < w->count; ++i) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 327 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 328 | if (!w->status[i]) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 329 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 330 | return 0; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 331 | } |
| 332 | } |
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 | return 1; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 335 | } |
| 336 | |
Brad Bishop | 75057c8 | 2021-08-03 15:22:02 -0400 | [diff] [blame] | 337 | _public_ void mapper_wait_async_free(mapper_async_wait* w) |
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 | free(w->status); |
| 340 | sarrayfree(w->objs); |
| 341 | free(w); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Brad Bishop | 75057c8 | 2021-08-03 15:22:02 -0400 | [diff] [blame] | 344 | _public_ int mapper_wait_async(sd_bus* conn, sd_event* loop, char* objs[], |
| 345 | void (*callback)(int, void*), void* userdata, |
| 346 | mapper_async_wait** w) |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 347 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 348 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 349 | mapper_async_wait* wait = NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 350 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 351 | wait = malloc(sizeof(*wait)); |
| 352 | if (!wait) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 353 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 354 | return -ENOMEM; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 355 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 356 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 357 | memset(wait, 0, sizeof(*wait)); |
| 358 | wait->conn = conn; |
| 359 | wait->loop = loop; |
| 360 | wait->callback = callback; |
| 361 | wait->userdata = userdata; |
| 362 | wait->count = sarraylen(objs); |
| 363 | if (!wait->count) |
Brad Bishop | 2ac3233 | 2021-08-03 14:06:52 -0400 | [diff] [blame] | 364 | { |
| 365 | r = 0; |
| 366 | goto free_wait; |
| 367 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 368 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 369 | wait->objs = sarraydup(objs); |
| 370 | if (!wait->objs) |
| 371 | { |
| 372 | r = -ENOMEM; |
| 373 | goto free_wait; |
| 374 | } |
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 | wait->status = malloc(sizeof(*wait->status) * wait->count); |
| 377 | if (!wait->status) |
| 378 | { |
| 379 | r = -ENOMEM; |
| 380 | goto free_objs; |
| 381 | } |
| 382 | memset(wait->status, 0, sizeof(*wait->status) * wait->count); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 383 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 384 | r = sd_bus_add_match(conn, &wait->introspection_slot, |
| 385 | async_wait_introspection_match, |
| 386 | async_wait_match_introspection_complete, wait); |
| 387 | if (r < 0) |
| 388 | { |
| 389 | fprintf(stderr, "Error adding match rule: %s\n", strerror(-r)); |
| 390 | goto free_status; |
| 391 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 392 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 393 | r = sd_bus_add_match(conn, &wait->intf_slot, |
| 394 | async_wait_interfaces_added_match, |
| 395 | async_wait_match_introspection_complete, wait); |
| 396 | if (r < 0) |
| 397 | { |
| 398 | fprintf(stderr, "Error adding match rule: %s\n", strerror(-r)); |
| 399 | goto unref_name_slot; |
| 400 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 401 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 402 | r = async_wait_get_objects(wait); |
| 403 | if (r < 0) |
| 404 | { |
| 405 | fprintf(stderr, "Error calling method: %s\n", strerror(-r)); |
| 406 | goto unref_intf_slot; |
| 407 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 408 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 409 | *w = wait; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 410 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 411 | return 0; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 412 | |
| 413 | unref_intf_slot: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 414 | sd_bus_slot_unref(wait->intf_slot); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 415 | unref_name_slot: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 416 | sd_bus_slot_unref(wait->introspection_slot); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 417 | free_status: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 418 | free(wait->status); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 419 | free_objs: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 420 | sarrayfree(wait->objs); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 421 | free_wait: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 422 | free(wait); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 423 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 424 | return r; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 425 | } |
| 426 | |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 427 | static int async_subtree_timeout_callback( |
| 428 | _unused_ sd_event_source* s, _unused_ uint64_t usec, void* userdata) |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 429 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 430 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 431 | struct mapper_async_subtree* subtree = userdata; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 432 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 433 | sd_event_source_unref(subtree->event_source); |
| 434 | r = sd_bus_call_method_async( |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 435 | subtree->conn, NULL, "xyz.openbmc_project.ObjectMapper", |
| 436 | "/xyz/openbmc_project/object_mapper", |
| 437 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", |
| 438 | async_subtree_getpaths_callback, subtree, "sias", subtree->namespace, 0, |
| 439 | 1, subtree->interface); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 440 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 441 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 442 | async_subtree_done(r, subtree); |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 443 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 444 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 445 | return 0; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 446 | } |
| 447 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 448 | static int async_subtree_getpaths_callback(sd_bus_message* m, void* userdata, |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 449 | _unused_ sd_bus_error* e) |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 450 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 451 | int r; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 452 | struct mapper_async_subtree* subtree = userdata; |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 453 | uint64_t next_retry; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 454 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 455 | if (subtree->finished) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 456 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 457 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 458 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 459 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 460 | r = sd_bus_message_get_errno(m); |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 461 | |
Patrick Williams | e82b058 | 2020-11-16 16:25:20 -0600 | [diff] [blame] | 462 | if (sd_bus_message_is_method_error( |
| 463 | m, "xyz.openbmc_project.Common.Error.ResourceNotFound")) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 464 | { |
| 465 | if (subtree->op == MAPPER_OP_REMOVE) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 466 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 467 | r = 0; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 468 | } |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 469 | else |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 470 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 471 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 472 | } |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 473 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 474 | |
William A. Kennington III | 2482946 | 2018-06-15 10:10:25 -0700 | [diff] [blame] | 475 | if ((r == EBUSY || r == ENOBUFS) && subtree->retry < mapper_busy_retries) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 476 | { |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 477 | r = sd_event_now(subtree->loop, CLOCK_MONOTONIC, &next_retry); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 478 | if (r < 0) |
| 479 | { |
| 480 | async_subtree_done(r, subtree); |
| 481 | goto exit; |
| 482 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 483 | |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 484 | next_retry += mapper_busy_delay_interval_usec * (1 << subtree->retry); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 485 | r = sd_event_add_time(subtree->loop, &subtree->event_source, |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 486 | CLOCK_MONOTONIC, next_retry, 0, |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 487 | async_subtree_timeout_callback, subtree); |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 488 | ++subtree->retry; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 489 | if (r < 0) |
| 490 | { |
| 491 | async_subtree_done(r, subtree); |
| 492 | goto exit; |
| 493 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 494 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 495 | return 0; |
| 496 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 497 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 498 | if (r) |
| 499 | { |
| 500 | async_subtree_done(-r, subtree); |
| 501 | goto exit; |
| 502 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 503 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 504 | if (subtree->op == MAPPER_OP_REMOVE) |
| 505 | { |
William A. Kennington III | 20cbbfb | 2018-06-15 10:10:13 -0700 | [diff] [blame] | 506 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s"); |
| 507 | if (r < 0) |
| 508 | { |
| 509 | async_subtree_done(r, subtree); |
| 510 | goto exit; |
| 511 | } |
| 512 | |
| 513 | r = sd_bus_message_at_end(m, false); |
| 514 | if (r < 0) |
| 515 | { |
| 516 | async_subtree_done(r, subtree); |
| 517 | goto exit; |
| 518 | } |
| 519 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 520 | /* 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] | 521 | * we know it is empty if the returned array is empty |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 522 | */ |
William A. Kennington III | 20cbbfb | 2018-06-15 10:10:13 -0700 | [diff] [blame] | 523 | if (r) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 524 | async_subtree_done(0, subtree); |
| 525 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 526 | |
| 527 | exit: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 528 | return 0; |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 529 | } |
| 530 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 531 | static int async_subtree_getpaths(mapper_async_subtree* subtree) |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 532 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 533 | int r = 0; |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 534 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 535 | subtree->retry = 0; |
| 536 | subtree->event_source = NULL; |
| 537 | r = sd_bus_call_method_async( |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 538 | subtree->conn, NULL, "xyz.openbmc_project.ObjectMapper", |
| 539 | "/xyz/openbmc_project/object_mapper", |
| 540 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", |
| 541 | async_subtree_getpaths_callback, subtree, "sias", subtree->namespace, 0, |
| 542 | 1, subtree->interface); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 543 | if (r < 0) |
| 544 | { |
| 545 | fprintf(stderr, "Error invoking method: %s\n", strerror(-r)); |
| 546 | return r; |
| 547 | } |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 548 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 549 | return 0; |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 550 | } |
| 551 | |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 552 | static int async_subtree_match_callback(_unused_ sd_bus_message* m, void* t, |
| 553 | _unused_ sd_bus_error* e) |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 554 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 555 | int r; |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 556 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 557 | mapper_async_subtree* subtree = t; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 558 | if (subtree->finished) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 559 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 560 | return 0; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 561 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -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) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 565 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 566 | async_subtree_done(r, subtree); |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 567 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 568 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 569 | return 0; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 570 | } |
| 571 | |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 572 | static void async_subtree_done(int r, mapper_async_subtree* t) |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 573 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 574 | if (t->finished) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 575 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 576 | return; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 577 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 578 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 579 | t->finished = 1; |
| 580 | sd_bus_slot_unref(t->slot); |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 581 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 582 | if (t->callback) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 583 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 584 | t->callback(r, t->userdata); |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 585 | } |
Adriana Kobylak | b2f2681 | 2017-05-08 13:58:02 -0500 | [diff] [blame] | 586 | } |
| 587 | |
Brad Bishop | 75057c8 | 2021-08-03 15:22:02 -0400 | [diff] [blame] | 588 | _public_ int mapper_subtree_async(sd_bus* conn, sd_event* loop, char* namespace, |
| 589 | char* interface, void (*callback)(int, void*), |
| 590 | void* userdata, mapper_async_subtree** t, |
| 591 | int op) |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 592 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 593 | int r = 0; |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 594 | mapper_async_subtree* subtree = NULL; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 595 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 596 | subtree = malloc(sizeof(*subtree)); |
| 597 | if (!subtree) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 598 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 599 | return -ENOMEM; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 600 | } |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 601 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 602 | memset(subtree, 0, sizeof(*subtree)); |
| 603 | subtree->conn = conn; |
| 604 | subtree->loop = loop; |
| 605 | subtree->namespace = namespace; |
| 606 | subtree->interface = interface; |
| 607 | subtree->callback = callback; |
| 608 | subtree->userdata = userdata; |
| 609 | subtree->op = op; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 610 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 611 | if (subtree->op == MAPPER_OP_REMOVE) |
| 612 | { |
| 613 | r = sd_bus_add_match(conn, &subtree->slot, interfaces_removed_match, |
| 614 | async_subtree_match_callback, subtree); |
| 615 | if (r < 0) |
| 616 | { |
| 617 | fprintf(stderr, "Error adding match rule: %s\n", strerror(-r)); |
| 618 | goto unref_slot; |
| 619 | } |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | /* Operation not supported */ |
| 624 | r = -EINVAL; |
| 625 | goto free_subtree; |
| 626 | } |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 627 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 628 | r = async_subtree_getpaths(subtree); |
| 629 | if (r < 0) |
| 630 | { |
| 631 | fprintf(stderr, "Error calling method: %s\n", strerror(-r)); |
| 632 | goto unref_slot; |
| 633 | } |
Adriana Kobylak | 025d795 | 2017-05-08 13:30:45 -0500 | [diff] [blame] | 634 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 635 | *t = subtree; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 636 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 637 | return 0; |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 638 | |
| 639 | unref_slot: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 640 | sd_bus_slot_unref(subtree->slot); |
Adriana Kobylak | 78edbb6 | 2017-05-04 15:45:19 -0500 | [diff] [blame] | 641 | free_subtree: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 642 | free(subtree); |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 643 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 644 | return r; |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 645 | } |
| 646 | |
Brad Bishop | 75057c8 | 2021-08-03 15:22:02 -0400 | [diff] [blame] | 647 | _public_ int mapper_get_object(sd_bus* conn, const char* obj, |
| 648 | sd_bus_message** reply) |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 649 | { |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 650 | sd_bus_message* request = NULL; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 651 | int r, retry = 0; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 652 | |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 653 | r = sd_bus_message_new_method_call( |
| 654 | conn, &request, "xyz.openbmc_project.ObjectMapper", |
| 655 | "/xyz/openbmc_project/object_mapper", |
| 656 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 657 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 658 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 659 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 660 | } |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 661 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 662 | r = sd_bus_message_append(request, "s", obj); |
| 663 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 664 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 665 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 666 | } |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 667 | r = sd_bus_message_append(request, "as", 0, NULL); |
| 668 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 669 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 670 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 671 | } |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 672 | |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 673 | while (true) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 674 | { |
William A. Kennington III | 2482946 | 2018-06-15 10:10:25 -0700 | [diff] [blame] | 675 | r = sd_bus_call(conn, request, 0, NULL, reply); |
| 676 | if (r == -EBUSY || r == -ENOBUFS) |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 677 | { |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 678 | if (retry >= mapper_busy_retries) |
| 679 | break; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 680 | |
William A. Kennington III | 5e21ac0 | 2018-06-15 10:10:20 -0700 | [diff] [blame] | 681 | usleep(mapper_busy_delay_interval_usec * (1 << retry)); |
| 682 | ++retry; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 683 | continue; |
| 684 | } |
| 685 | break; |
| 686 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 687 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 688 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 689 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 690 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 691 | } |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 692 | |
| 693 | exit: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 694 | sd_bus_message_unref(request); |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 695 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 696 | return r; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 697 | } |
| 698 | |
Brad Bishop | 75057c8 | 2021-08-03 15:22:02 -0400 | [diff] [blame] | 699 | _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] | 700 | { |
Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 701 | sd_bus_message* reply = NULL; |
| 702 | const char* tmp; |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 703 | int r; |
Brad Bishop | 3d46879 | 2016-09-20 15:39:38 -0400 | [diff] [blame] | 704 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 705 | r = mapper_get_object(conn, obj, &reply); |
| 706 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 707 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 708 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 709 | } |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 710 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 711 | r = sd_bus_message_enter_container(reply, 0, NULL); |
| 712 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 713 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 714 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 715 | } |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 716 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 717 | r = sd_bus_message_enter_container(reply, 0, NULL); |
| 718 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 719 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 720 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 721 | } |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 722 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 723 | r = sd_bus_message_read(reply, "s", &tmp); |
| 724 | if (r < 0) |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 725 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 726 | goto exit; |
George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame^] | 727 | } |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 728 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 729 | *service = strdup(tmp); |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 730 | |
| 731 | exit: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 732 | sd_bus_message_unref(reply); |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 733 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 734 | return r; |
Brad Bishop | 62ece2b | 2016-07-25 09:00:51 -0400 | [diff] [blame] | 735 | } |