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