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