| 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 | |
| George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame] | 4 | #include "mapper.h" |
| 5 | |
| Matt Spinler | 59cbf34 | 2018-09-24 09:46:00 -0500 | [diff] [blame] | 6 | #include <errno.h> |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 7 | #include <stdio.h> |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 8 | #include <stdlib.h> |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 9 | #include <systemd/sd-bus.h> |
| 10 | #include <systemd/sd-event.h> |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 11 | #include <unistd.h> |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 12 | |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 13 | static void quit(int r, void* loop) |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 14 | { |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 15 | sd_event_exit((sd_event*)loop, r); |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 16 | } |
| 17 | |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 18 | static int wait_main(int argc, char* argv[]) |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 19 | { |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 20 | int r; |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 21 | sd_bus* conn = NULL; |
| 22 | sd_event* loop = NULL; |
| 23 | mapper_async_wait* wait = NULL; |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 24 | size_t attempts = 0; |
| Patrick Williams | af3d797 | 2022-04-08 11:10:40 -0500 | [diff] [blame] | 25 | const size_t max_attempts = 20; |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 26 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 27 | if (argc < 3) |
| 28 | { |
| 29 | fprintf(stderr, "Usage: %s wait OBJECTPATH...\n", argv[0]); |
| 30 | exit(EXIT_FAILURE); |
| 31 | } |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 32 | |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 33 | /* Mapper waits are typically run early in the boot process, and in some |
| 34 | * cases the CPU and/or object manager daemon are so busy that the |
| 35 | * GetObject call may fail with a timeout and cause the event loop to exit. |
| 36 | * If this happens, retry a few times. Don't retry on other failures. |
| 37 | */ |
| 38 | while (1) |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 39 | { |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 40 | attempts++; |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 41 | |
| Brad Bishop | f15b06c | 2021-08-02 22:27:39 -0400 | [diff] [blame] | 42 | r = sd_bus_default(&conn); |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 43 | if (r < 0) |
| 44 | { |
| 45 | fprintf(stderr, "Error connecting to system bus: %s\n", |
| 46 | strerror(-r)); |
| 47 | goto finish; |
| 48 | } |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 49 | |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 50 | r = sd_event_default(&loop); |
| 51 | if (r < 0) |
| 52 | { |
| 53 | fprintf(stderr, "Error obtaining event loop: %s\n", strerror(-r)); |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 54 | |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 55 | goto finish; |
| 56 | } |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 57 | |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 58 | r = sd_bus_attach_event(conn, loop, SD_EVENT_PRIORITY_NORMAL); |
| 59 | if (r < 0) |
| 60 | { |
| 61 | fprintf(stderr, |
| 62 | "Failed to attach system " |
| 63 | "bus to event loop: %s\n", |
| 64 | strerror(-r)); |
| 65 | goto finish; |
| 66 | } |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 67 | |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 68 | r = mapper_wait_async(conn, loop, argv + 2, quit, loop, &wait); |
| 69 | if (r < 0) |
| 70 | { |
| 71 | fprintf(stderr, "Error configuring waitlist: %s\n", strerror(-r)); |
| 72 | goto finish; |
| 73 | } |
| 74 | |
| 75 | r = sd_event_loop(loop); |
| 76 | if (r < 0) |
| 77 | { |
| 78 | fprintf(stderr, "Event loop exited: %s\n", strerror(-r)); |
| 79 | |
| William A. Kennington III | 551fafb | 2020-05-08 02:43:01 -0700 | [diff] [blame] | 80 | if (-r == ETIMEDOUT || -r == EHOSTUNREACH) |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 81 | { |
| 82 | if (attempts <= max_attempts) |
| 83 | { |
| Patrick Williams | af3d797 | 2022-04-08 11:10:40 -0500 | [diff] [blame] | 84 | fprintf(stderr, "Retrying in 1s\n"); |
| 85 | sleep(1); |
| Matt Spinler | 5592202 | 2020-04-27 13:45:14 -0500 | [diff] [blame] | 86 | sd_event_unref(loop); |
| 87 | sd_bus_unref(conn); |
| 88 | continue; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | fprintf(stderr, "Giving up\n"); |
| 93 | } |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | goto finish; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | break; |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 102 | } |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 103 | |
| 104 | finish: |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 105 | exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 108 | static int subtree_main(int argc, char* argv[]) |
| Adriana Kobylak | 04d7c7d | 2017-05-04 14:06:18 -0500 | [diff] [blame] | 109 | { |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 110 | int r = 0; |
| 111 | int op = 0; |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 112 | static const char* token = ":"; |
| 113 | char* tmp = NULL; |
| 114 | char* namespace = NULL; |
| 115 | char* interface = NULL; |
| 116 | sd_bus* conn = NULL; |
| 117 | sd_event* loop = NULL; |
| 118 | mapper_async_subtree* subtree = NULL; |
| Adriana Kobylak | 04d7c7d | 2017-05-04 14:06:18 -0500 | [diff] [blame] | 119 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 120 | if (argc != 3) |
| 121 | { |
| 122 | fprintf(stderr, |
| 123 | "Usage: %s subtree-remove " |
| 124 | "NAMESPACE%sINTERFACE\n", |
| 125 | argv[0], token); |
| 126 | exit(EXIT_FAILURE); |
| 127 | } |
| Adriana Kobylak | 6a8688f | 2017-05-05 11:32:17 -0500 | [diff] [blame] | 128 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 129 | op = MAPPER_OP_REMOVE; |
| Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 130 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 131 | namespace = strtok_r(argv[2], token, &tmp); |
| 132 | interface = strtok_r(NULL, token, &tmp); |
| 133 | if ((namespace == NULL) || (interface == NULL)) |
| 134 | { |
| 135 | fprintf(stderr, "Token '%s' was not found in '%s'\n", token, argv[2]); |
| 136 | exit(EXIT_FAILURE); |
| 137 | } |
| Adriana Kobylak | 04d7c7d | 2017-05-04 14:06:18 -0500 | [diff] [blame] | 138 | |
| Brad Bishop | f15b06c | 2021-08-02 22:27:39 -0400 | [diff] [blame] | 139 | r = sd_bus_default(&conn); |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 140 | if (r < 0) |
| 141 | { |
| 142 | fprintf(stderr, "Error connecting to system bus: %s\n", strerror(-r)); |
| 143 | goto finish; |
| 144 | } |
| Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 145 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 146 | r = sd_event_default(&loop); |
| 147 | if (r < 0) |
| 148 | { |
| 149 | fprintf(stderr, "Error obtaining event loop: %s\n", strerror(-r)); |
| 150 | goto finish; |
| 151 | } |
| Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 152 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 153 | r = sd_bus_attach_event(conn, loop, SD_EVENT_PRIORITY_NORMAL); |
| 154 | if (r < 0) |
| 155 | { |
| 156 | fprintf(stderr, "Failed to attach system bus to event loop: %s\n", |
| 157 | strerror(-r)); |
| 158 | goto finish; |
| 159 | } |
| Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 160 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 161 | r = mapper_subtree_async(conn, loop, namespace, interface, quit, loop, |
| 162 | &subtree, op); |
| 163 | if (r < 0) |
| 164 | { |
| 165 | fprintf(stderr, "Error configuring subtree list: %s\n", strerror(-r)); |
| 166 | goto finish; |
| 167 | } |
| Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 168 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 169 | r = sd_event_loop(loop); |
| 170 | if (r < 0) |
| 171 | { |
| Matt Spinler | 59cbf34 | 2018-09-24 09:46:00 -0500 | [diff] [blame] | 172 | /* If this function has been called after the interface in */ |
| 173 | /* question has already been removed, then GetSubTree will */ |
| 174 | /* fail and it will show up here. Treat as success instead. */ |
| 175 | if (r == -ENXIO) |
| 176 | { |
| 177 | r = 0; |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | fprintf(stderr, "Error starting event loop: %d(%s)\n", r, |
| 182 | strerror(-r)); |
| 183 | goto finish; |
| 184 | } |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 185 | } |
| Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 186 | |
| 187 | finish: |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 188 | exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| Adriana Kobylak | 04d7c7d | 2017-05-04 14:06:18 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 191 | /* print out the distinct dbus service name for the input dbus path */ |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 192 | static int get_service_main(int argc, char* argv[]) |
| Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 193 | { |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 194 | int r; |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 195 | sd_bus* conn = NULL; |
| 196 | char* service = NULL; |
| Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 197 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 198 | if (argc != 3) |
| 199 | { |
| 200 | fprintf(stderr, "Usage: %s get-service OBJECTPATH\n", argv[0]); |
| 201 | exit(EXIT_FAILURE); |
| 202 | } |
| Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 203 | |
| Brad Bishop | f15b06c | 2021-08-02 22:27:39 -0400 | [diff] [blame] | 204 | r = sd_bus_default(&conn); |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 205 | if (r < 0) |
| 206 | { |
| 207 | fprintf(stderr, "Error connecting to system bus: %s\n", strerror(-r)); |
| 208 | goto finish; |
| 209 | } |
| Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 210 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 211 | r = mapper_get_service(conn, argv[2], &service); |
| 212 | if (r < 0) |
| 213 | { |
| 214 | fprintf(stderr, "Error finding '%s' service: %s\n", argv[2], |
| 215 | strerror(-r)); |
| 216 | goto finish; |
| 217 | } |
| Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 218 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 219 | printf("%s\n", service); |
| Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 220 | |
| 221 | finish: |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 222 | exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 223 | } |
| 224 | |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 225 | int main(int argc, char* argv[]) |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 226 | { |
| Matt Spinler | cc6ee9c | 2018-09-19 13:23:13 -0500 | [diff] [blame] | 227 | static const char* usage = |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 228 | "Usage: %s {COMMAND} ...\n" |
| 229 | "\nCOMMANDS:\n" |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 230 | " wait wait for the specified objects to appear on the " |
| 231 | "DBus\n" |
| 232 | " subtree-remove\n" |
| 233 | " wait until the specified interface is not present\n" |
| 234 | " in any of the subtrees of the specified namespace\n" |
| 235 | " get-service return the service identifier for input path\n"; |
| 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 (argc < 2) |
| 238 | { |
| 239 | fprintf(stderr, usage, argv[0]); |
| 240 | exit(EXIT_FAILURE); |
| 241 | } |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 242 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 243 | if (!strcmp(argv[1], "wait")) |
| 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 | wait_main(argc, argv); |
| George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame] | 246 | } |
| 247 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 248 | if (!strcmp(argv[1], "subtree-remove")) |
| George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame] | 249 | { |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 250 | subtree_main(argc, argv); |
| George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame] | 251 | } |
| 252 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 253 | if (!strcmp(argv[1], "get-service")) |
| George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame] | 254 | { |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 255 | get_service_main(argc, argv); |
| George Liu | cf72403 | 2024-10-29 14:31:51 +0800 | [diff] [blame] | 256 | } |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 257 | |
| Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 258 | fprintf(stderr, usage, argv[0]); |
| 259 | exit(EXIT_FAILURE); |
| Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 260 | } |