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