blob: d926173b79a72ce4b9e2e333c4362dcb6f582cbe [file] [log] [blame]
Brad Bishop2afe7182016-08-13 14:08:17 -04001/**
Andrew Geissler981f2662017-03-03 15:58:23 -06002 * Copyright 2016 IBM Corporation
Brad Bishop2afe7182016-08-13 14:08:17 -04003 *
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 Barthc6329c92016-11-15 11:13:37 -060016#include "config.h"
Brad Bishop2afe7182016-08-13 14:08:17 -040017#include <stdlib.h>
18#include <stdio.h>
19#include <systemd/sd-bus.h>
20#include <systemd/sd-event.h>
21#include "mapper.h"
22
23static int call_main(int argc, char *argv[])
24{
Ed Tanous167e2372018-05-07 11:59:10 -070025 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 Bishop2afe7182016-08-13 14:08:17 -040030
Ed Tanous167e2372018-05-07 11:59:10 -070031 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 Bishop2afe7182016-08-13 14:08:17 -040040
Ed Tanous167e2372018-05-07 11:59:10 -070041 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 Bishop2afe7182016-08-13 14:08:17 -040047
Ed Tanous167e2372018-05-07 11:59:10 -070048 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 Bishop2afe7182016-08-13 14:08:17 -040055
Ed Tanous167e2372018-05-07 11:59:10 -070056 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 Bishop2afe7182016-08-13 14:08:17 -040063
Ed Tanous167e2372018-05-07 11:59:10 -070064 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 Bishop2afe7182016-08-13 14:08:17 -040076
Ed Tanous167e2372018-05-07 11:59:10 -070077 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 Bishop2afe7182016-08-13 14:08:17 -040083
84finish:
Ed Tanous167e2372018-05-07 11:59:10 -070085 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Brad Bishop2afe7182016-08-13 14:08:17 -040086}
87
88static void quit(int r, void *loop)
89{
Ed Tanous167e2372018-05-07 11:59:10 -070090 sd_event_exit((sd_event *)loop, r);
Brad Bishop2afe7182016-08-13 14:08:17 -040091}
92
93static int wait_main(int argc, char *argv[])
94{
Ed Tanous167e2372018-05-07 11:59:10 -070095 int r;
96 sd_bus *conn = NULL;
97 sd_event *loop = NULL;
98 mapper_async_wait *wait = NULL;
Brad Bishop2afe7182016-08-13 14:08:17 -040099
Ed Tanous167e2372018-05-07 11:59:10 -0700100 if (argc < 3)
101 {
102 fprintf(stderr, "Usage: %s wait OBJECTPATH...\n", argv[0]);
103 exit(EXIT_FAILURE);
104 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400105
Ed Tanous167e2372018-05-07 11:59:10 -0700106 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 Bishop2afe7182016-08-13 14:08:17 -0400112
Ed Tanous167e2372018-05-07 11:59:10 -0700113 r = sd_event_default(&loop);
114 if (r < 0)
115 {
116 fprintf(stderr, "Error obtaining event loop: %s\n", strerror(-r));
Brad Bishop2afe7182016-08-13 14:08:17 -0400117
Ed Tanous167e2372018-05-07 11:59:10 -0700118 goto finish;
119 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400120
Ed Tanous167e2372018-05-07 11:59:10 -0700121 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 Bishop2afe7182016-08-13 14:08:17 -0400130
Ed Tanous167e2372018-05-07 11:59:10 -0700131 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 Bishop2afe7182016-08-13 14:08:17 -0400137
Ed Tanous167e2372018-05-07 11:59:10 -0700138 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 Bishop2afe7182016-08-13 14:08:17 -0400144
145finish:
Ed Tanous167e2372018-05-07 11:59:10 -0700146 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Brad Bishop2afe7182016-08-13 14:08:17 -0400147}
148
Adriana Kobylak04d7c7d2017-05-04 14:06:18 -0500149static int subtree_main(int argc, char *argv[])
150{
Ed Tanous167e2372018-05-07 11:59:10 -0700151 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 Kobylak04d7c7d2017-05-04 14:06:18 -0500160
Ed Tanous167e2372018-05-07 11:59:10 -0700161 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 Kobylak6a8688f2017-05-05 11:32:17 -0500169
Ed Tanous167e2372018-05-07 11:59:10 -0700170 op = MAPPER_OP_REMOVE;
Adriana Kobylak2a8bfc92017-05-11 09:16:02 -0500171
Ed Tanous167e2372018-05-07 11:59:10 -0700172 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 Kobylak04d7c7d2017-05-04 14:06:18 -0500179
Ed Tanous167e2372018-05-07 11:59:10 -0700180 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500186
Ed Tanous167e2372018-05-07 11:59:10 -0700187 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500193
Ed Tanous167e2372018-05-07 11:59:10 -0700194 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500201
Ed Tanous167e2372018-05-07 11:59:10 -0700202 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500209
Ed Tanous167e2372018-05-07 11:59:10 -0700210 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500216
217finish:
Ed Tanous167e2372018-05-07 11:59:10 -0700218 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Adriana Kobylak04d7c7d2017-05-04 14:06:18 -0500219}
220
Andrew Geissler981f2662017-03-03 15:58:23 -0600221/* print out the distinct dbus service name for the input dbus path */
222static int get_service_main(int argc, char *argv[])
223{
Ed Tanous167e2372018-05-07 11:59:10 -0700224 int r;
225 sd_bus *conn = NULL;
226 char *service = NULL;
Andrew Geissler981f2662017-03-03 15:58:23 -0600227
Ed Tanous167e2372018-05-07 11:59:10 -0700228 if (argc != 3)
229 {
230 fprintf(stderr, "Usage: %s get-service OBJECTPATH\n", argv[0]);
231 exit(EXIT_FAILURE);
232 }
Andrew Geissler981f2662017-03-03 15:58:23 -0600233
Ed Tanous167e2372018-05-07 11:59:10 -0700234 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 Geissler981f2662017-03-03 15:58:23 -0600240
Ed Tanous167e2372018-05-07 11:59:10 -0700241 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 Geissler981f2662017-03-03 15:58:23 -0600248
Ed Tanous167e2372018-05-07 11:59:10 -0700249 printf("%s\n", service);
Andrew Geissler981f2662017-03-03 15:58:23 -0600250
251finish:
Ed Tanous167e2372018-05-07 11:59:10 -0700252 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Andrew Geissler981f2662017-03-03 15:58:23 -0600253}
254
Brad Bishop2afe7182016-08-13 14:08:17 -0400255int main(int argc, char *argv[])
256{
Ed Tanous167e2372018-05-07 11:59:10 -0700257 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 Bishop2afe7182016-08-13 14:08:17 -0400267
Ed Tanous167e2372018-05-07 11:59:10 -0700268 if (argc < 2)
269 {
270 fprintf(stderr, usage, argv[0]);
271 exit(EXIT_FAILURE);
272 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400273
Ed Tanous167e2372018-05-07 11:59:10 -0700274 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 Bishop2afe7182016-08-13 14:08:17 -0400282
Ed Tanous167e2372018-05-07 11:59:10 -0700283 fprintf(stderr, usage, argv[0]);
284 exit(EXIT_FAILURE);
Brad Bishop2afe7182016-08-13 14:08:17 -0400285}