blob: 681c4a789fd5d66ee2bd2574322d68ce5725b079 [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
Brad Bishop2afe7182016-08-13 14:08:17 -040023static void quit(int r, void *loop)
24{
Ed Tanous167e2372018-05-07 11:59:10 -070025 sd_event_exit((sd_event *)loop, r);
Brad Bishop2afe7182016-08-13 14:08:17 -040026}
27
28static int wait_main(int argc, char *argv[])
29{
Ed Tanous167e2372018-05-07 11:59:10 -070030 int r;
31 sd_bus *conn = NULL;
32 sd_event *loop = NULL;
33 mapper_async_wait *wait = NULL;
Brad Bishop2afe7182016-08-13 14:08:17 -040034
Ed Tanous167e2372018-05-07 11:59:10 -070035 if (argc < 3)
36 {
37 fprintf(stderr, "Usage: %s wait OBJECTPATH...\n", argv[0]);
38 exit(EXIT_FAILURE);
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 = sd_event_default(&loop);
49 if (r < 0)
50 {
51 fprintf(stderr, "Error obtaining event loop: %s\n", strerror(-r));
Brad Bishop2afe7182016-08-13 14:08:17 -040052
Ed Tanous167e2372018-05-07 11:59:10 -070053 goto finish;
54 }
Brad Bishop2afe7182016-08-13 14:08:17 -040055
Ed Tanous167e2372018-05-07 11:59:10 -070056 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 Bishop2afe7182016-08-13 14:08:17 -040065
Ed Tanous167e2372018-05-07 11:59:10 -070066 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 Bishop2afe7182016-08-13 14:08:17 -040072
Ed Tanous167e2372018-05-07 11:59:10 -070073 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 Bishop2afe7182016-08-13 14:08:17 -040079
80finish:
Ed Tanous167e2372018-05-07 11:59:10 -070081 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Brad Bishop2afe7182016-08-13 14:08:17 -040082}
83
Adriana Kobylak04d7c7d2017-05-04 14:06:18 -050084static int subtree_main(int argc, char *argv[])
85{
Ed Tanous167e2372018-05-07 11:59:10 -070086 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 Kobylak04d7c7d2017-05-04 14:06:18 -050095
Ed Tanous167e2372018-05-07 11:59:10 -070096 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 Kobylak6a8688f2017-05-05 11:32:17 -0500104
Ed Tanous167e2372018-05-07 11:59:10 -0700105 op = MAPPER_OP_REMOVE;
Adriana Kobylak2a8bfc92017-05-11 09:16:02 -0500106
Ed Tanous167e2372018-05-07 11:59:10 -0700107 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 Kobylak04d7c7d2017-05-04 14:06:18 -0500114
Ed Tanous167e2372018-05-07 11:59:10 -0700115 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500121
Ed Tanous167e2372018-05-07 11:59:10 -0700122 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500128
Ed Tanous167e2372018-05-07 11:59:10 -0700129 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500136
Ed Tanous167e2372018-05-07 11:59:10 -0700137 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500144
Ed Tanous167e2372018-05-07 11:59:10 -0700145 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500151
152finish:
Ed Tanous167e2372018-05-07 11:59:10 -0700153 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Adriana Kobylak04d7c7d2017-05-04 14:06:18 -0500154}
155
Andrew Geissler981f2662017-03-03 15:58:23 -0600156/* print out the distinct dbus service name for the input dbus path */
157static int get_service_main(int argc, char *argv[])
158{
Ed Tanous167e2372018-05-07 11:59:10 -0700159 int r;
160 sd_bus *conn = NULL;
161 char *service = NULL;
Andrew Geissler981f2662017-03-03 15:58:23 -0600162
Ed Tanous167e2372018-05-07 11:59:10 -0700163 if (argc != 3)
164 {
165 fprintf(stderr, "Usage: %s get-service OBJECTPATH\n", argv[0]);
166 exit(EXIT_FAILURE);
167 }
Andrew Geissler981f2662017-03-03 15:58:23 -0600168
Ed Tanous167e2372018-05-07 11:59:10 -0700169 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 Geissler981f2662017-03-03 15:58:23 -0600175
Ed Tanous167e2372018-05-07 11:59:10 -0700176 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 Geissler981f2662017-03-03 15:58:23 -0600183
Ed Tanous167e2372018-05-07 11:59:10 -0700184 printf("%s\n", service);
Andrew Geissler981f2662017-03-03 15:58:23 -0600185
186finish:
Ed Tanous167e2372018-05-07 11:59:10 -0700187 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Andrew Geissler981f2662017-03-03 15:58:23 -0600188}
189
Brad Bishop2afe7182016-08-13 14:08:17 -0400190int main(int argc, char *argv[])
191{
Ed Tanous167e2372018-05-07 11:59:10 -0700192 static const char *usage =
193 "Usage: %s {COMMAND} ...\n"
194 "\nCOMMANDS:\n"
Ed Tanous167e2372018-05-07 11:59:10 -0700195 " 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 Bishop2afe7182016-08-13 14:08:17 -0400201
Ed Tanous167e2372018-05-07 11:59:10 -0700202 if (argc < 2)
203 {
204 fprintf(stderr, usage, argv[0]);
205 exit(EXIT_FAILURE);
206 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400207
Ed Tanous167e2372018-05-07 11:59:10 -0700208 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 Bishop2afe7182016-08-13 14:08:17 -0400214
Ed Tanous167e2372018-05-07 11:59:10 -0700215 fprintf(stderr, usage, argv[0]);
216 exit(EXIT_FAILURE);
Brad Bishop2afe7182016-08-13 14:08:17 -0400217}