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