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" |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <systemd/sd-bus.h> |
| 20 | #include <systemd/sd-event.h> |
| 21 | #include "mapper.h" |
| 22 | |
| 23 | static int call_main(int argc, char *argv[]) |
| 24 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 25 | int r; |
| 26 | sd_bus *conn = NULL; |
| 27 | char *service = NULL; |
| 28 | sd_bus_message *m = NULL, *reply = NULL; |
| 29 | sd_bus_error error = SD_BUS_ERROR_NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 30 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 31 | if (argc < 5) |
| 32 | { |
| 33 | fprintf(stderr, |
| 34 | "Usage: %s call OBJECTPATH INTERFACE " |
| 35 | "METHOD [SIGNATURE [ARGUMENT...]\n", |
| 36 | argv[0]); |
| 37 | r = -1; |
| 38 | goto finish; |
| 39 | } |
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 | r = sd_bus_default_system(&conn); |
| 42 | if (r < 0) |
| 43 | { |
| 44 | fprintf(stderr, "Error connecting to system bus: %s\n", strerror(-r)); |
| 45 | goto finish; |
| 46 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 47 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 48 | r = mapper_get_service(conn, argv[2], &service); |
| 49 | if (r < 0) |
| 50 | { |
| 51 | fprintf(stderr, "Error finding '%s' service: %s\n", argv[2], |
| 52 | strerror(-r)); |
| 53 | goto finish; |
| 54 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 55 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 56 | r = sd_bus_message_new_method_call(conn, &m, service, argv[2], argv[3], |
| 57 | argv[4]); |
| 58 | if (r < 0) |
| 59 | { |
| 60 | fprintf(stderr, "Error populating message: %s\n", strerror(-r)); |
| 61 | goto finish; |
| 62 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 63 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 64 | if (argc > 5) |
| 65 | { |
| 66 | char **p; |
| 67 | p = argv + 6; |
| 68 | r = sd_bus_message_append_cmdline(m, argv[5], &p); |
| 69 | if (r < 0) |
| 70 | { |
| 71 | fprintf(stderr, "Error appending method arguments: %s\n", |
| 72 | strerror(-r)); |
| 73 | goto finish; |
| 74 | } |
| 75 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 76 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 77 | r = sd_bus_call(conn, m, 0, &error, &reply); |
| 78 | if (r < 0) |
| 79 | { |
| 80 | fprintf(stderr, "Error invoking method: %s\n", strerror(-r)); |
| 81 | goto finish; |
| 82 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 83 | |
| 84 | finish: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 85 | exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void quit(int r, void *loop) |
| 89 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 90 | sd_event_exit((sd_event *)loop, r); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static int wait_main(int argc, char *argv[]) |
| 94 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 95 | int r; |
| 96 | sd_bus *conn = NULL; |
| 97 | sd_event *loop = NULL; |
| 98 | mapper_async_wait *wait = NULL; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 99 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 100 | if (argc < 3) |
| 101 | { |
| 102 | fprintf(stderr, "Usage: %s wait OBJECTPATH...\n", argv[0]); |
| 103 | exit(EXIT_FAILURE); |
| 104 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 105 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 106 | r = sd_bus_default_system(&conn); |
| 107 | if (r < 0) |
| 108 | { |
| 109 | fprintf(stderr, "Error connecting to system bus: %s\n", strerror(-r)); |
| 110 | goto finish; |
| 111 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 112 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 113 | r = sd_event_default(&loop); |
| 114 | if (r < 0) |
| 115 | { |
| 116 | fprintf(stderr, "Error obtaining event loop: %s\n", strerror(-r)); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 117 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 118 | goto finish; |
| 119 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 120 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 121 | r = sd_bus_attach_event(conn, loop, SD_EVENT_PRIORITY_NORMAL); |
| 122 | if (r < 0) |
| 123 | { |
| 124 | fprintf(stderr, |
| 125 | "Failed to attach system " |
| 126 | "bus to event loop: %s\n", |
| 127 | strerror(-r)); |
| 128 | goto finish; |
| 129 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 130 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 131 | r = mapper_wait_async(conn, loop, argv + 2, quit, loop, &wait); |
| 132 | if (r < 0) |
| 133 | { |
| 134 | fprintf(stderr, "Error configuring waitlist: %s\n", strerror(-r)); |
| 135 | goto finish; |
| 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 | r = sd_event_loop(loop); |
| 139 | if (r < 0) |
| 140 | { |
| 141 | fprintf(stderr, "Error starting event loop: %s\n", strerror(-r)); |
| 142 | goto finish; |
| 143 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 144 | |
| 145 | finish: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 146 | exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Adriana Kobylak | 04d7c7d | 2017-05-04 14:06:18 -0500 | [diff] [blame] | 149 | static int subtree_main(int argc, char *argv[]) |
| 150 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 151 | int r = 0; |
| 152 | int op = 0; |
| 153 | static const char *token = ":"; |
| 154 | char *tmp = NULL; |
| 155 | char *namespace = NULL; |
| 156 | char *interface = NULL; |
| 157 | sd_bus *conn = NULL; |
| 158 | sd_event *loop = NULL; |
| 159 | mapper_async_subtree *subtree = NULL; |
Adriana Kobylak | 04d7c7d | 2017-05-04 14:06:18 -0500 | [diff] [blame] | 160 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 161 | if (argc != 3) |
| 162 | { |
| 163 | fprintf(stderr, |
| 164 | "Usage: %s subtree-remove " |
| 165 | "NAMESPACE%sINTERFACE\n", |
| 166 | argv[0], token); |
| 167 | exit(EXIT_FAILURE); |
| 168 | } |
Adriana Kobylak | 6a8688f | 2017-05-05 11:32:17 -0500 | [diff] [blame] | 169 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 170 | op = MAPPER_OP_REMOVE; |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 171 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 172 | namespace = strtok_r(argv[2], token, &tmp); |
| 173 | interface = strtok_r(NULL, token, &tmp); |
| 174 | if ((namespace == NULL) || (interface == NULL)) |
| 175 | { |
| 176 | fprintf(stderr, "Token '%s' was not found in '%s'\n", token, argv[2]); |
| 177 | exit(EXIT_FAILURE); |
| 178 | } |
Adriana Kobylak | 04d7c7d | 2017-05-04 14:06:18 -0500 | [diff] [blame] | 179 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 180 | r = sd_bus_default_system(&conn); |
| 181 | if (r < 0) |
| 182 | { |
| 183 | fprintf(stderr, "Error connecting to system bus: %s\n", strerror(-r)); |
| 184 | goto finish; |
| 185 | } |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 186 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 187 | r = sd_event_default(&loop); |
| 188 | if (r < 0) |
| 189 | { |
| 190 | fprintf(stderr, "Error obtaining event loop: %s\n", strerror(-r)); |
| 191 | goto finish; |
| 192 | } |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 193 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 194 | r = sd_bus_attach_event(conn, loop, SD_EVENT_PRIORITY_NORMAL); |
| 195 | if (r < 0) |
| 196 | { |
| 197 | fprintf(stderr, "Failed to attach system bus to event loop: %s\n", |
| 198 | strerror(-r)); |
| 199 | goto finish; |
| 200 | } |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 201 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 202 | r = mapper_subtree_async(conn, loop, namespace, interface, quit, loop, |
| 203 | &subtree, op); |
| 204 | if (r < 0) |
| 205 | { |
| 206 | fprintf(stderr, "Error configuring subtree list: %s\n", strerror(-r)); |
| 207 | goto finish; |
| 208 | } |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 209 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 210 | r = sd_event_loop(loop); |
| 211 | if (r < 0) |
| 212 | { |
| 213 | fprintf(stderr, "Error starting event loop: %s\n", strerror(-r)); |
| 214 | goto finish; |
| 215 | } |
Adriana Kobylak | 2a8bfc9 | 2017-05-11 09:16:02 -0500 | [diff] [blame] | 216 | |
| 217 | finish: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 218 | exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
Adriana Kobylak | 04d7c7d | 2017-05-04 14:06:18 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 221 | /* print out the distinct dbus service name for the input dbus path */ |
| 222 | static int get_service_main(int argc, char *argv[]) |
| 223 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 224 | int r; |
| 225 | sd_bus *conn = NULL; |
| 226 | char *service = NULL; |
Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 227 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 228 | if (argc != 3) |
| 229 | { |
| 230 | fprintf(stderr, "Usage: %s get-service OBJECTPATH\n", argv[0]); |
| 231 | exit(EXIT_FAILURE); |
| 232 | } |
Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 233 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 234 | r = sd_bus_default_system(&conn); |
| 235 | if (r < 0) |
| 236 | { |
| 237 | fprintf(stderr, "Error connecting to system bus: %s\n", strerror(-r)); |
| 238 | goto finish; |
| 239 | } |
Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 240 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 241 | r = mapper_get_service(conn, argv[2], &service); |
| 242 | if (r < 0) |
| 243 | { |
| 244 | fprintf(stderr, "Error finding '%s' service: %s\n", argv[2], |
| 245 | strerror(-r)); |
| 246 | goto finish; |
| 247 | } |
Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 248 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 249 | printf("%s\n", service); |
Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 250 | |
| 251 | finish: |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 252 | exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
Andrew Geissler | 981f266 | 2017-03-03 15:58:23 -0600 | [diff] [blame] | 253 | } |
| 254 | |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 255 | int main(int argc, char *argv[]) |
| 256 | { |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 257 | static const char *usage = |
| 258 | "Usage: %s {COMMAND} ...\n" |
| 259 | "\nCOMMANDS:\n" |
| 260 | " call invoke the specified method\n" |
| 261 | " wait wait for the specified objects to appear on the " |
| 262 | "DBus\n" |
| 263 | " subtree-remove\n" |
| 264 | " wait until the specified interface is not present\n" |
| 265 | " in any of the subtrees of the specified namespace\n" |
| 266 | " get-service return the service identifier for input path\n"; |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 267 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 268 | if (argc < 2) |
| 269 | { |
| 270 | fprintf(stderr, usage, argv[0]); |
| 271 | exit(EXIT_FAILURE); |
| 272 | } |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 273 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 274 | if (!strcmp(argv[1], "call")) |
| 275 | call_main(argc, argv); |
| 276 | if (!strcmp(argv[1], "wait")) |
| 277 | wait_main(argc, argv); |
| 278 | if (!strcmp(argv[1], "subtree-remove")) |
| 279 | subtree_main(argc, argv); |
| 280 | if (!strcmp(argv[1], "get-service")) |
| 281 | get_service_main(argc, argv); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 282 | |
Ed Tanous | 167e237 | 2018-05-07 11:59:10 -0700 | [diff] [blame] | 283 | fprintf(stderr, usage, argv[0]); |
| 284 | exit(EXIT_FAILURE); |
Brad Bishop | 2afe718 | 2016-08-13 14:08:17 -0400 | [diff] [blame] | 285 | } |