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