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