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