blob: 8acc741874a940544c4d9312c73f7399b71feb7f [file] [log] [blame]
Brad Bishop62ece2b2016-07-25 09:00:51 -04001/**
2 * Copyright © 2016 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brad Bishop36eb1e52016-11-01 15:18:36 -040016#include "config.h"
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050017
18#include "mapper.h"
19
20#include <errno.h>
William A. Kennington III20cbbfb2018-06-15 10:10:13 -070021#include <stdbool.h>
Brad Bishopa959f122021-08-03 11:21:01 -040022#include <stddef.h>
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050023#include <stdio.h>
Brad Bishop2afe7182016-08-13 14:08:17 -040024#include <stdlib.h>
Brad Bishop62ece2b2016-07-25 09:00:51 -040025#include <string.h>
Brad Bishop3d468792016-09-20 15:39:38 -040026#include <sys/timerfd.h>
Brad Bishop62ece2b2016-07-25 09:00:51 -040027#include <systemd/sd-bus.h>
Brad Bishop3d468792016-09-20 15:39:38 -040028#include <systemd/sd-event.h>
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050029#include <unistd.h>
Brad Bishop62ece2b2016-07-25 09:00:51 -040030
Brad Bishopa669a502021-08-03 16:01:43 -040031#include "internal.h"
32
Brad Bishop75057c82021-08-03 15:22:02 -040033#define _public_ __attribute__((__visibility__("default")))
Brad Bishop2d41d6a2021-08-03 08:14:45 -040034#define _unused_ __attribute__((unused))
Brad Bishop75057c82021-08-03 15:22:02 -040035
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050036static const char* async_wait_introspection_match =
Ed Tanous167e2372018-05-07 11:59:10 -070037 "type='signal',"
38 "sender='xyz.openbmc_project.ObjectMapper',"
39 "interface='xyz.openbmc_project.ObjectMapper.Private',"
40 "member='IntrospectionComplete'";
Brad Bishop2afe7182016-08-13 14:08:17 -040041
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050042static const char* async_wait_interfaces_added_match =
Ed Tanous167e2372018-05-07 11:59:10 -070043 "type='signal',"
44 "interface='org.freedesktop.DBus.ObjectManager',"
45 "member='InterfacesAdded'";
Brad Bishop2afe7182016-08-13 14:08:17 -040046
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050047static const char* interfaces_removed_match =
Ed Tanous167e2372018-05-07 11:59:10 -070048 "type='signal',"
49 "interface='org.freedesktop.DBus.ObjectManager',"
50 "member='InterfacesRemoved'";
Adriana Kobylak78edbb62017-05-04 15:45:19 -050051
Brad Bishop3d468792016-09-20 15:39:38 -040052static const int mapper_busy_retries = 5;
53static const uint64_t mapper_busy_delay_interval_usec = 1000000;
54
Brad Bishop2afe7182016-08-13 14:08:17 -040055struct mapper_async_wait
56{
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050057 char** objs;
58 void (*callback)(int, void*);
59 void* userdata;
60 sd_event* loop;
61 sd_bus* conn;
62 sd_bus_slot* introspection_slot;
63 sd_bus_slot* intf_slot;
64 int* status;
Brad Bishopa959f122021-08-03 11:21:01 -040065 size_t count;
Ed Tanous167e2372018-05-07 11:59:10 -070066 int finished;
67 int r;
Brad Bishop2afe7182016-08-13 14:08:17 -040068};
69
70struct async_wait_callback_data
71{
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050072 mapper_async_wait* wait;
73 const char* path;
74 sd_event_source* event_source;
Ed Tanous167e2372018-05-07 11:59:10 -070075 int retry;
Brad Bishop2afe7182016-08-13 14:08:17 -040076};
77
Adriana Kobylak2a8bfc92017-05-11 09:16:02 -050078struct mapper_async_subtree
79{
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050080 char* namespace;
81 char* interface;
82 void (*callback)(int, void*);
83 void* userdata;
84 sd_event* loop;
85 sd_bus* conn;
86 sd_bus_slot* slot;
87 sd_event_source* event_source;
Ed Tanous167e2372018-05-07 11:59:10 -070088 int finished;
89 int op;
90 int retry;
Adriana Kobylak2a8bfc92017-05-11 09:16:02 -050091};
92
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050093static int async_wait_match_introspection_complete(sd_bus_message*, void*,
94 sd_bus_error*);
95static int async_wait_check_done(mapper_async_wait*);
96static void async_wait_done(int r, mapper_async_wait*);
97static int async_wait_get_objects(mapper_async_wait*);
98static int async_wait_getobject_callback(sd_bus_message*, void*, sd_bus_error*);
Brad Bishop2afe7182016-08-13 14:08:17 -040099
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500100static int async_subtree_match_callback(sd_bus_message*, void*, sd_bus_error*);
101static void async_subtree_done(int r, mapper_async_subtree*);
102static int async_subtree_getpaths(mapper_async_subtree*);
103static int async_subtree_getpaths_callback(sd_bus_message*, void*,
104 sd_bus_error*);
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500105
Brad Bishopa959f122021-08-03 11:21:01 -0400106size_t sarraylen(char* array[])
Brad Bishop2afe7182016-08-13 14:08:17 -0400107{
Brad Bishopa959f122021-08-03 11:21:01 -0400108 size_t count = 0;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500109 char** p = array;
Brad Bishop2afe7182016-08-13 14:08:17 -0400110
Ed Tanous167e2372018-05-07 11:59:10 -0700111 while (*p != NULL)
112 {
113 ++count;
114 ++p;
115 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400116
Ed Tanous167e2372018-05-07 11:59:10 -0700117 return count;
Brad Bishop2afe7182016-08-13 14:08:17 -0400118}
119
Brad Bishopa669a502021-08-03 16:01:43 -0400120void sarrayfree(char* array[])
Brad Bishop2afe7182016-08-13 14:08:17 -0400121{
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500122 char** p = array;
Ed Tanous167e2372018-05-07 11:59:10 -0700123 while (*p != NULL)
124 {
125 free(*p);
126 ++p;
127 }
128 free(array);
Brad Bishop2afe7182016-08-13 14:08:17 -0400129}
130
Brad Bishopa669a502021-08-03 16:01:43 -0400131char** sarraydup(char* array[])
Brad Bishop2afe7182016-08-13 14:08:17 -0400132{
Brad Bishopa959f122021-08-03 11:21:01 -0400133 size_t count = sarraylen(array);
134 size_t i;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500135 char** ret = NULL;
Brad Bishop2afe7182016-08-13 14:08:17 -0400136
Brad Bishopa669a502021-08-03 16:01:43 -0400137 ret = calloc(count + 1, sizeof(*ret));
Ed Tanous167e2372018-05-07 11:59:10 -0700138 if (!ret)
139 return NULL;
Brad Bishop2afe7182016-08-13 14:08:17 -0400140
Ed Tanous167e2372018-05-07 11:59:10 -0700141 for (i = 0; i < count; ++i)
142 {
143 ret[i] = strdup(array[i]);
144 if (!ret[i])
145 goto error;
146 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400147
Ed Tanous167e2372018-05-07 11:59:10 -0700148 return ret;
Brad Bishop2afe7182016-08-13 14:08:17 -0400149
150error:
Ed Tanous167e2372018-05-07 11:59:10 -0700151 sarrayfree(ret);
152 return NULL;
Brad Bishop2afe7182016-08-13 14:08:17 -0400153}
154
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400155static int async_wait_timeout_callback(_unused_ sd_event_source* s,
156 _unused_ uint64_t usec, void* userdata)
Brad Bishop3d468792016-09-20 15:39:38 -0400157{
Ed Tanous167e2372018-05-07 11:59:10 -0700158 int r;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500159 struct async_wait_callback_data* data = userdata;
160 mapper_async_wait* wait = data->wait;
Brad Bishop3d468792016-09-20 15:39:38 -0400161
Ed Tanous167e2372018-05-07 11:59:10 -0700162 sd_event_source_unref(data->event_source);
163 r = sd_bus_call_method_async(wait->conn, NULL, MAPPER_BUSNAME, MAPPER_PATH,
164 MAPPER_INTERFACE, "GetObject",
165 async_wait_getobject_callback, data, "sas",
166 data->path, 0, NULL);
167 if (r < 0)
168 {
169 async_wait_done(r, wait);
170 free(data);
171 }
Brad Bishop3d468792016-09-20 15:39:38 -0400172
Ed Tanous167e2372018-05-07 11:59:10 -0700173 return 0;
Brad Bishop3d468792016-09-20 15:39:38 -0400174}
175
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500176static int async_wait_getobject_callback(sd_bus_message* m, void* userdata,
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400177 _unused_ sd_bus_error* e)
Brad Bishop2afe7182016-08-13 14:08:17 -0400178{
Brad Bishopa959f122021-08-03 11:21:01 -0400179 size_t i;
180 int r;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500181 struct async_wait_callback_data* data = userdata;
182 mapper_async_wait* wait = data->wait;
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700183 uint64_t next_retry;
Brad Bishop2afe7182016-08-13 14:08:17 -0400184
Ed Tanous167e2372018-05-07 11:59:10 -0700185 if (wait->finished)
186 goto exit;
Brad Bishop3d468792016-09-20 15:39:38 -0400187
Patrick Williamse82b0582020-11-16 16:25:20 -0600188 if (sd_bus_message_is_method_error(
189 m, "xyz.openbmc_project.Common.Error.ResourceNotFound"))
Ed Tanous167e2372018-05-07 11:59:10 -0700190 goto exit;
Brad Bishop8ccdaed2016-09-20 15:54:32 -0400191
Patrick Williamse82b0582020-11-16 16:25:20 -0600192 r = sd_bus_message_get_errno(m);
193
William A. Kennington III24829462018-06-15 10:10:25 -0700194 if ((r == EBUSY || r == ENOBUFS) && data->retry < mapper_busy_retries)
Ed Tanous167e2372018-05-07 11:59:10 -0700195 {
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700196 r = sd_event_now(wait->loop, CLOCK_MONOTONIC, &next_retry);
Ed Tanous167e2372018-05-07 11:59:10 -0700197 if (r < 0)
198 {
199 async_wait_done(r, wait);
200 goto exit;
201 }
Brad Bishop3d468792016-09-20 15:39:38 -0400202
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700203 next_retry += mapper_busy_delay_interval_usec * (1 << data->retry);
Ed Tanous167e2372018-05-07 11:59:10 -0700204 r = sd_event_add_time(wait->loop, &data->event_source, CLOCK_MONOTONIC,
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700205 next_retry, 0, async_wait_timeout_callback, data);
206 ++data->retry;
Ed Tanous167e2372018-05-07 11:59:10 -0700207 if (r < 0)
208 {
209 async_wait_done(r, wait);
210 goto exit;
211 }
Brad Bishop3d468792016-09-20 15:39:38 -0400212
Ed Tanous167e2372018-05-07 11:59:10 -0700213 return 0;
214 }
Brad Bishop3d468792016-09-20 15:39:38 -0400215
Ed Tanous167e2372018-05-07 11:59:10 -0700216 if (r)
217 {
218 async_wait_done(-r, wait);
219 goto exit;
220 }
Brad Bishop3d468792016-09-20 15:39:38 -0400221
Ed Tanous167e2372018-05-07 11:59:10 -0700222 for (i = 0; i < wait->count; ++i)
223 {
224 if (!strcmp(data->path, wait->objs[i]))
225 {
226 wait->status[i] = 1;
227 }
228 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400229
Ed Tanous167e2372018-05-07 11:59:10 -0700230 if (async_wait_check_done(wait))
231 async_wait_done(0, wait);
Brad Bishop2afe7182016-08-13 14:08:17 -0400232
Brad Bishop3d468792016-09-20 15:39:38 -0400233exit:
Ed Tanous167e2372018-05-07 11:59:10 -0700234 free(data);
235 return 0;
Brad Bishop2afe7182016-08-13 14:08:17 -0400236}
237
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500238static int async_wait_get_objects(mapper_async_wait* wait)
Brad Bishop2afe7182016-08-13 14:08:17 -0400239{
Brad Bishopa959f122021-08-03 11:21:01 -0400240 size_t i;
241 int r;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500242 struct async_wait_callback_data* data = NULL;
Brad Bishop2afe7182016-08-13 14:08:17 -0400243
Ed Tanous167e2372018-05-07 11:59:10 -0700244 for (i = 0; i < wait->count; ++i)
245 {
246 if (wait->status[i])
247 continue;
248 data = malloc(sizeof(*data));
249 data->wait = wait;
250 data->path = wait->objs[i];
251 data->retry = 0;
252 data->event_source = NULL;
253 r = sd_bus_call_method_async(wait->conn, NULL, MAPPER_BUSNAME,
254 MAPPER_PATH, MAPPER_INTERFACE, "GetObject",
255 async_wait_getobject_callback, data, "sas",
256 wait->objs[i], 0, NULL);
257 if (r < 0)
258 {
259 free(data);
260 fprintf(stderr, "Error invoking method: %s\n", strerror(-r));
261 return r;
262 }
263 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400264
Ed Tanous167e2372018-05-07 11:59:10 -0700265 return 0;
Brad Bishop2afe7182016-08-13 14:08:17 -0400266}
267
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400268static int async_wait_match_introspection_complete(_unused_ sd_bus_message* m,
269 void* w,
270 _unused_ sd_bus_error* e)
Brad Bishop2afe7182016-08-13 14:08:17 -0400271{
Ed Tanous167e2372018-05-07 11:59:10 -0700272 int r;
Brad Bishop2afe7182016-08-13 14:08:17 -0400273
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500274 mapper_async_wait* wait = w;
Ed Tanous167e2372018-05-07 11:59:10 -0700275 if (wait->finished)
276 return 0;
Brad Bishop2afe7182016-08-13 14:08:17 -0400277
Ed Tanous167e2372018-05-07 11:59:10 -0700278 r = async_wait_get_objects(wait);
279 if (r < 0)
280 async_wait_done(r, wait);
Brad Bishop2afe7182016-08-13 14:08:17 -0400281
Ed Tanous167e2372018-05-07 11:59:10 -0700282 return 0;
Brad Bishop2afe7182016-08-13 14:08:17 -0400283}
284
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500285static void async_wait_done(int r, mapper_async_wait* w)
Brad Bishop2afe7182016-08-13 14:08:17 -0400286{
Ed Tanous167e2372018-05-07 11:59:10 -0700287 if (w->finished)
288 return;
Brad Bishop2afe7182016-08-13 14:08:17 -0400289
Ed Tanous167e2372018-05-07 11:59:10 -0700290 w->finished = 1;
291 sd_bus_slot_unref(w->introspection_slot);
292 sd_bus_slot_unref(w->intf_slot);
Brad Bishop2afe7182016-08-13 14:08:17 -0400293
Ed Tanous167e2372018-05-07 11:59:10 -0700294 if (w->callback)
295 w->callback(r, w->userdata);
Brad Bishop2afe7182016-08-13 14:08:17 -0400296}
297
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500298static int async_wait_check_done(mapper_async_wait* w)
Brad Bishop2afe7182016-08-13 14:08:17 -0400299{
Brad Bishopa959f122021-08-03 11:21:01 -0400300 size_t i;
Brad Bishop2afe7182016-08-13 14:08:17 -0400301
Ed Tanous167e2372018-05-07 11:59:10 -0700302 if (w->finished)
303 return 1;
Brad Bishop2afe7182016-08-13 14:08:17 -0400304
Ed Tanous167e2372018-05-07 11:59:10 -0700305 for (i = 0; i < w->count; ++i)
306 if (!w->status[i])
307 return 0;
Brad Bishop2afe7182016-08-13 14:08:17 -0400308
Ed Tanous167e2372018-05-07 11:59:10 -0700309 return 1;
Brad Bishop2afe7182016-08-13 14:08:17 -0400310}
311
Brad Bishop75057c82021-08-03 15:22:02 -0400312_public_ void mapper_wait_async_free(mapper_async_wait* w)
Brad Bishop2afe7182016-08-13 14:08:17 -0400313{
Ed Tanous167e2372018-05-07 11:59:10 -0700314 free(w->status);
315 sarrayfree(w->objs);
316 free(w);
Brad Bishop2afe7182016-08-13 14:08:17 -0400317}
318
Brad Bishop75057c82021-08-03 15:22:02 -0400319_public_ int mapper_wait_async(sd_bus* conn, sd_event* loop, char* objs[],
320 void (*callback)(int, void*), void* userdata,
321 mapper_async_wait** w)
Brad Bishop2afe7182016-08-13 14:08:17 -0400322{
Ed Tanous167e2372018-05-07 11:59:10 -0700323 int r;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500324 mapper_async_wait* wait = NULL;
Brad Bishop2afe7182016-08-13 14:08:17 -0400325
Ed Tanous167e2372018-05-07 11:59:10 -0700326 wait = malloc(sizeof(*wait));
327 if (!wait)
328 return -ENOMEM;
Brad Bishop2afe7182016-08-13 14:08:17 -0400329
Ed Tanous167e2372018-05-07 11:59:10 -0700330 memset(wait, 0, sizeof(*wait));
331 wait->conn = conn;
332 wait->loop = loop;
333 wait->callback = callback;
334 wait->userdata = userdata;
335 wait->count = sarraylen(objs);
336 if (!wait->count)
337 return 0;
Brad Bishop2afe7182016-08-13 14:08:17 -0400338
Ed Tanous167e2372018-05-07 11:59:10 -0700339 wait->objs = sarraydup(objs);
340 if (!wait->objs)
341 {
342 r = -ENOMEM;
343 goto free_wait;
344 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400345
Ed Tanous167e2372018-05-07 11:59:10 -0700346 wait->status = malloc(sizeof(*wait->status) * wait->count);
347 if (!wait->status)
348 {
349 r = -ENOMEM;
350 goto free_objs;
351 }
352 memset(wait->status, 0, sizeof(*wait->status) * wait->count);
Brad Bishop2afe7182016-08-13 14:08:17 -0400353
Ed Tanous167e2372018-05-07 11:59:10 -0700354 r = sd_bus_add_match(conn, &wait->introspection_slot,
355 async_wait_introspection_match,
356 async_wait_match_introspection_complete, wait);
357 if (r < 0)
358 {
359 fprintf(stderr, "Error adding match rule: %s\n", strerror(-r));
360 goto free_status;
361 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400362
Ed Tanous167e2372018-05-07 11:59:10 -0700363 r = sd_bus_add_match(conn, &wait->intf_slot,
364 async_wait_interfaces_added_match,
365 async_wait_match_introspection_complete, wait);
366 if (r < 0)
367 {
368 fprintf(stderr, "Error adding match rule: %s\n", strerror(-r));
369 goto unref_name_slot;
370 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400371
Ed Tanous167e2372018-05-07 11:59:10 -0700372 r = async_wait_get_objects(wait);
373 if (r < 0)
374 {
375 fprintf(stderr, "Error calling method: %s\n", strerror(-r));
376 goto unref_intf_slot;
377 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400378
Ed Tanous167e2372018-05-07 11:59:10 -0700379 *w = wait;
Brad Bishop2afe7182016-08-13 14:08:17 -0400380
Ed Tanous167e2372018-05-07 11:59:10 -0700381 return 0;
Brad Bishop2afe7182016-08-13 14:08:17 -0400382
383unref_intf_slot:
Ed Tanous167e2372018-05-07 11:59:10 -0700384 sd_bus_slot_unref(wait->intf_slot);
Brad Bishop2afe7182016-08-13 14:08:17 -0400385unref_name_slot:
Ed Tanous167e2372018-05-07 11:59:10 -0700386 sd_bus_slot_unref(wait->introspection_slot);
Brad Bishop2afe7182016-08-13 14:08:17 -0400387free_status:
Ed Tanous167e2372018-05-07 11:59:10 -0700388 free(wait->status);
Brad Bishop2afe7182016-08-13 14:08:17 -0400389free_objs:
Ed Tanous167e2372018-05-07 11:59:10 -0700390 sarrayfree(wait->objs);
Brad Bishop2afe7182016-08-13 14:08:17 -0400391free_wait:
Ed Tanous167e2372018-05-07 11:59:10 -0700392 free(wait);
Brad Bishop2afe7182016-08-13 14:08:17 -0400393
Ed Tanous167e2372018-05-07 11:59:10 -0700394 return r;
Brad Bishop2afe7182016-08-13 14:08:17 -0400395}
396
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400397static int async_subtree_timeout_callback(_unused_ sd_event_source* s,
398 _unused_ uint64_t usec,
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500399 void* userdata)
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500400{
Ed Tanous167e2372018-05-07 11:59:10 -0700401 int r;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500402 struct mapper_async_subtree* subtree = userdata;
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500403
Ed Tanous167e2372018-05-07 11:59:10 -0700404 sd_event_source_unref(subtree->event_source);
405 r = sd_bus_call_method_async(
406 subtree->conn, NULL, MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE,
407 "GetSubTreePaths", async_subtree_getpaths_callback, subtree, "sias",
408 subtree->namespace, 0, 1, subtree->interface);
409 if (r < 0)
410 async_subtree_done(r, subtree);
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500411
Ed Tanous167e2372018-05-07 11:59:10 -0700412 return 0;
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500413}
414
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500415static int async_subtree_getpaths_callback(sd_bus_message* m, void* userdata,
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400416 _unused_ sd_bus_error* e)
Adriana Kobylak025d7952017-05-08 13:30:45 -0500417{
Ed Tanous167e2372018-05-07 11:59:10 -0700418 int r;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500419 struct mapper_async_subtree* subtree = userdata;
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700420 uint64_t next_retry;
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500421
Ed Tanous167e2372018-05-07 11:59:10 -0700422 if (subtree->finished)
423 goto exit;
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500424
Ed Tanous167e2372018-05-07 11:59:10 -0700425 r = sd_bus_message_get_errno(m);
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500426
Patrick Williamse82b0582020-11-16 16:25:20 -0600427 if (sd_bus_message_is_method_error(
428 m, "xyz.openbmc_project.Common.Error.ResourceNotFound"))
Ed Tanous167e2372018-05-07 11:59:10 -0700429 {
430 if (subtree->op == MAPPER_OP_REMOVE)
431 r = 0;
432 else
433 goto exit;
434 }
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500435
William A. Kennington III24829462018-06-15 10:10:25 -0700436 if ((r == EBUSY || r == ENOBUFS) && subtree->retry < mapper_busy_retries)
Ed Tanous167e2372018-05-07 11:59:10 -0700437 {
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700438 r = sd_event_now(subtree->loop, CLOCK_MONOTONIC, &next_retry);
Ed Tanous167e2372018-05-07 11:59:10 -0700439 if (r < 0)
440 {
441 async_subtree_done(r, subtree);
442 goto exit;
443 }
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500444
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700445 next_retry += mapper_busy_delay_interval_usec * (1 << subtree->retry);
Ed Tanous167e2372018-05-07 11:59:10 -0700446 r = sd_event_add_time(subtree->loop, &subtree->event_source,
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700447 CLOCK_MONOTONIC, next_retry, 0,
Ed Tanous167e2372018-05-07 11:59:10 -0700448 async_subtree_timeout_callback, subtree);
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700449 ++subtree->retry;
Ed Tanous167e2372018-05-07 11:59:10 -0700450 if (r < 0)
451 {
452 async_subtree_done(r, subtree);
453 goto exit;
454 }
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500455
Ed Tanous167e2372018-05-07 11:59:10 -0700456 return 0;
457 }
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500458
Ed Tanous167e2372018-05-07 11:59:10 -0700459 if (r)
460 {
461 async_subtree_done(-r, subtree);
462 goto exit;
463 }
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500464
Ed Tanous167e2372018-05-07 11:59:10 -0700465 if (subtree->op == MAPPER_OP_REMOVE)
466 {
William A. Kennington III20cbbfb2018-06-15 10:10:13 -0700467 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
468 if (r < 0)
469 {
470 async_subtree_done(r, subtree);
471 goto exit;
472 }
473
474 r = sd_bus_message_at_end(m, false);
475 if (r < 0)
476 {
477 async_subtree_done(r, subtree);
478 goto exit;
479 }
480
Ed Tanous167e2372018-05-07 11:59:10 -0700481 /* For remove, operation is complete when the interface is not present
William A. Kennington III20cbbfb2018-06-15 10:10:13 -0700482 * we know it is empty if the returned array is empty
Ed Tanous167e2372018-05-07 11:59:10 -0700483 */
William A. Kennington III20cbbfb2018-06-15 10:10:13 -0700484 if (r)
Ed Tanous167e2372018-05-07 11:59:10 -0700485 async_subtree_done(0, subtree);
486 }
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500487
488exit:
Ed Tanous167e2372018-05-07 11:59:10 -0700489 return 0;
Adriana Kobylak025d7952017-05-08 13:30:45 -0500490}
491
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500492static int async_subtree_getpaths(mapper_async_subtree* subtree)
Adriana Kobylak025d7952017-05-08 13:30:45 -0500493{
Ed Tanous167e2372018-05-07 11:59:10 -0700494 int r = 0;
Adriana Kobylak025d7952017-05-08 13:30:45 -0500495
Ed Tanous167e2372018-05-07 11:59:10 -0700496 subtree->retry = 0;
497 subtree->event_source = NULL;
498 r = sd_bus_call_method_async(
499 subtree->conn, NULL, MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE,
500 "GetSubTreePaths", async_subtree_getpaths_callback, subtree, "sias",
501 subtree->namespace, 0, 1, subtree->interface);
502 if (r < 0)
503 {
504 fprintf(stderr, "Error invoking method: %s\n", strerror(-r));
505 return r;
506 }
Adriana Kobylak025d7952017-05-08 13:30:45 -0500507
Ed Tanous167e2372018-05-07 11:59:10 -0700508 return 0;
Adriana Kobylak025d7952017-05-08 13:30:45 -0500509}
510
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400511static int async_subtree_match_callback(_unused_ sd_bus_message* m, void* t,
512 _unused_ sd_bus_error* e)
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500513{
Ed Tanous167e2372018-05-07 11:59:10 -0700514 int r;
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500515
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500516 mapper_async_subtree* subtree = t;
Ed Tanous167e2372018-05-07 11:59:10 -0700517 if (subtree->finished)
518 return 0;
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500519
Ed Tanous167e2372018-05-07 11:59:10 -0700520 r = async_subtree_getpaths(subtree);
521 if (r < 0)
522 async_subtree_done(r, subtree);
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500523
Ed Tanous167e2372018-05-07 11:59:10 -0700524 return 0;
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500525}
526
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500527static void async_subtree_done(int r, mapper_async_subtree* t)
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500528{
Ed Tanous167e2372018-05-07 11:59:10 -0700529 if (t->finished)
530 return;
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500531
Ed Tanous167e2372018-05-07 11:59:10 -0700532 t->finished = 1;
533 sd_bus_slot_unref(t->slot);
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500534
Ed Tanous167e2372018-05-07 11:59:10 -0700535 if (t->callback)
536 t->callback(r, t->userdata);
Adriana Kobylakb2f26812017-05-08 13:58:02 -0500537}
538
Brad Bishop75057c82021-08-03 15:22:02 -0400539_public_ int mapper_subtree_async(sd_bus* conn, sd_event* loop, char* namespace,
540 char* interface, void (*callback)(int, void*),
541 void* userdata, mapper_async_subtree** t,
542 int op)
Adriana Kobylak2a8bfc92017-05-11 09:16:02 -0500543{
Ed Tanous167e2372018-05-07 11:59:10 -0700544 int r = 0;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500545 mapper_async_subtree* subtree = NULL;
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500546
Ed Tanous167e2372018-05-07 11:59:10 -0700547 subtree = malloc(sizeof(*subtree));
548 if (!subtree)
549 return -ENOMEM;
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500550
Ed Tanous167e2372018-05-07 11:59:10 -0700551 memset(subtree, 0, sizeof(*subtree));
552 subtree->conn = conn;
553 subtree->loop = loop;
554 subtree->namespace = namespace;
555 subtree->interface = interface;
556 subtree->callback = callback;
557 subtree->userdata = userdata;
558 subtree->op = op;
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500559
Ed Tanous167e2372018-05-07 11:59:10 -0700560 if (subtree->op == MAPPER_OP_REMOVE)
561 {
562 r = sd_bus_add_match(conn, &subtree->slot, interfaces_removed_match,
563 async_subtree_match_callback, subtree);
564 if (r < 0)
565 {
566 fprintf(stderr, "Error adding match rule: %s\n", strerror(-r));
567 goto unref_slot;
568 }
569 }
570 else
571 {
572 /* Operation not supported */
573 r = -EINVAL;
574 goto free_subtree;
575 }
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500576
Ed Tanous167e2372018-05-07 11:59:10 -0700577 r = async_subtree_getpaths(subtree);
578 if (r < 0)
579 {
580 fprintf(stderr, "Error calling method: %s\n", strerror(-r));
581 goto unref_slot;
582 }
Adriana Kobylak025d7952017-05-08 13:30:45 -0500583
Ed Tanous167e2372018-05-07 11:59:10 -0700584 *t = subtree;
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500585
Ed Tanous167e2372018-05-07 11:59:10 -0700586 return 0;
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500587
588unref_slot:
Ed Tanous167e2372018-05-07 11:59:10 -0700589 sd_bus_slot_unref(subtree->slot);
Adriana Kobylak78edbb62017-05-04 15:45:19 -0500590free_subtree:
Ed Tanous167e2372018-05-07 11:59:10 -0700591 free(subtree);
Adriana Kobylak2a8bfc92017-05-11 09:16:02 -0500592
Ed Tanous167e2372018-05-07 11:59:10 -0700593 return r;
Adriana Kobylak2a8bfc92017-05-11 09:16:02 -0500594}
595
Brad Bishop75057c82021-08-03 15:22:02 -0400596_public_ int mapper_get_object(sd_bus* conn, const char* obj,
597 sd_bus_message** reply)
Brad Bishop62ece2b2016-07-25 09:00:51 -0400598{
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500599 sd_bus_message* request = NULL;
Ed Tanous167e2372018-05-07 11:59:10 -0700600 int r, retry = 0;
Brad Bishop62ece2b2016-07-25 09:00:51 -0400601
Ed Tanous167e2372018-05-07 11:59:10 -0700602 r = sd_bus_message_new_method_call(conn, &request, MAPPER_BUSNAME,
603 MAPPER_PATH, MAPPER_INTERFACE,
604 "GetObject");
605 if (r < 0)
606 goto exit;
Brad Bishop62ece2b2016-07-25 09:00:51 -0400607
Ed Tanous167e2372018-05-07 11:59:10 -0700608 r = sd_bus_message_append(request, "s", obj);
609 if (r < 0)
610 goto exit;
611 r = sd_bus_message_append(request, "as", 0, NULL);
612 if (r < 0)
613 goto exit;
Brad Bishop62ece2b2016-07-25 09:00:51 -0400614
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700615 while (true)
Ed Tanous167e2372018-05-07 11:59:10 -0700616 {
William A. Kennington III24829462018-06-15 10:10:25 -0700617 r = sd_bus_call(conn, request, 0, NULL, reply);
618 if (r == -EBUSY || r == -ENOBUFS)
Ed Tanous167e2372018-05-07 11:59:10 -0700619 {
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700620 if (retry >= mapper_busy_retries)
621 break;
Brad Bishop3d468792016-09-20 15:39:38 -0400622
William A. Kennington III5e21ac02018-06-15 10:10:20 -0700623 usleep(mapper_busy_delay_interval_usec * (1 << retry));
624 ++retry;
Ed Tanous167e2372018-05-07 11:59:10 -0700625 continue;
626 }
627 break;
628 }
Brad Bishop3d468792016-09-20 15:39:38 -0400629
Ed Tanous167e2372018-05-07 11:59:10 -0700630 if (r < 0)
631 goto exit;
Brad Bishop3d468792016-09-20 15:39:38 -0400632
633exit:
Ed Tanous167e2372018-05-07 11:59:10 -0700634 sd_bus_message_unref(request);
Brad Bishop3d468792016-09-20 15:39:38 -0400635
Ed Tanous167e2372018-05-07 11:59:10 -0700636 return r;
Brad Bishop3d468792016-09-20 15:39:38 -0400637}
638
Brad Bishop75057c82021-08-03 15:22:02 -0400639_public_ int mapper_get_service(sd_bus* conn, const char* obj, char** service)
Brad Bishop3d468792016-09-20 15:39:38 -0400640{
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500641 sd_bus_message* reply = NULL;
642 const char* tmp;
Ed Tanous167e2372018-05-07 11:59:10 -0700643 int r;
Brad Bishop3d468792016-09-20 15:39:38 -0400644
Ed Tanous167e2372018-05-07 11:59:10 -0700645 r = mapper_get_object(conn, obj, &reply);
646 if (r < 0)
647 goto exit;
Brad Bishop62ece2b2016-07-25 09:00:51 -0400648
Ed Tanous167e2372018-05-07 11:59:10 -0700649 r = sd_bus_message_enter_container(reply, 0, NULL);
650 if (r < 0)
651 goto exit;
Brad Bishop62ece2b2016-07-25 09:00:51 -0400652
Ed Tanous167e2372018-05-07 11:59:10 -0700653 r = sd_bus_message_enter_container(reply, 0, NULL);
654 if (r < 0)
655 goto exit;
Brad Bishop62ece2b2016-07-25 09:00:51 -0400656
Ed Tanous167e2372018-05-07 11:59:10 -0700657 r = sd_bus_message_read(reply, "s", &tmp);
658 if (r < 0)
659 goto exit;
Brad Bishop62ece2b2016-07-25 09:00:51 -0400660
Ed Tanous167e2372018-05-07 11:59:10 -0700661 *service = strdup(tmp);
Brad Bishop62ece2b2016-07-25 09:00:51 -0400662
663exit:
Ed Tanous167e2372018-05-07 11:59:10 -0700664 sd_bus_message_unref(reply);
Brad Bishop62ece2b2016-07-25 09:00:51 -0400665
Ed Tanous167e2372018-05-07 11:59:10 -0700666 return r;
Brad Bishop62ece2b2016-07-25 09:00:51 -0400667}