blob: 14d8ab08c3278aa096637c45202c37e2bb346f82 [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"
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050017
Brad Bishop2afe7182016-08-13 14:08:17 -040018#include <stdio.h>
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050019#include <stdlib.h>
Brad Bishop2afe7182016-08-13 14:08:17 -040020#include <systemd/sd-bus.h>
21#include <systemd/sd-event.h>
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050022
Brad Bishop2afe7182016-08-13 14:08:17 -040023#include "mapper.h"
24
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050025static void quit(int r, void* loop)
Brad Bishop2afe7182016-08-13 14:08:17 -040026{
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050027 sd_event_exit((sd_event*)loop, r);
Brad Bishop2afe7182016-08-13 14:08:17 -040028}
29
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050030static int wait_main(int argc, char* argv[])
Brad Bishop2afe7182016-08-13 14:08:17 -040031{
Ed Tanous167e2372018-05-07 11:59:10 -070032 int r;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050033 sd_bus* conn = NULL;
34 sd_event* loop = NULL;
35 mapper_async_wait* wait = NULL;
Brad Bishop2afe7182016-08-13 14:08:17 -040036
Ed Tanous167e2372018-05-07 11:59:10 -070037 if (argc < 3)
38 {
39 fprintf(stderr, "Usage: %s wait OBJECTPATH...\n", argv[0]);
40 exit(EXIT_FAILURE);
41 }
Brad Bishop2afe7182016-08-13 14:08:17 -040042
Ed Tanous167e2372018-05-07 11:59:10 -070043 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 Bishop2afe7182016-08-13 14:08:17 -040049
Ed Tanous167e2372018-05-07 11:59:10 -070050 r = sd_event_default(&loop);
51 if (r < 0)
52 {
53 fprintf(stderr, "Error obtaining event loop: %s\n", strerror(-r));
Brad Bishop2afe7182016-08-13 14:08:17 -040054
Ed Tanous167e2372018-05-07 11:59:10 -070055 goto finish;
56 }
Brad Bishop2afe7182016-08-13 14:08:17 -040057
Ed Tanous167e2372018-05-07 11:59:10 -070058 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 Bishop2afe7182016-08-13 14:08:17 -040067
Ed Tanous167e2372018-05-07 11:59:10 -070068 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 Bishop2afe7182016-08-13 14:08:17 -040074
Ed Tanous167e2372018-05-07 11:59:10 -070075 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 Bishop2afe7182016-08-13 14:08:17 -040081
82finish:
Ed Tanous167e2372018-05-07 11:59:10 -070083 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Brad Bishop2afe7182016-08-13 14:08:17 -040084}
85
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050086static int subtree_main(int argc, char* argv[])
Adriana Kobylak04d7c7d2017-05-04 14:06:18 -050087{
Ed Tanous167e2372018-05-07 11:59:10 -070088 int r = 0;
89 int op = 0;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050090 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 Kobylak04d7c7d2017-05-04 14:06:18 -050097
Ed Tanous167e2372018-05-07 11:59:10 -070098 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 Kobylak6a8688f2017-05-05 11:32:17 -0500106
Ed Tanous167e2372018-05-07 11:59:10 -0700107 op = MAPPER_OP_REMOVE;
Adriana Kobylak2a8bfc92017-05-11 09:16:02 -0500108
Ed Tanous167e2372018-05-07 11:59:10 -0700109 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 Kobylak04d7c7d2017-05-04 14:06:18 -0500116
Ed Tanous167e2372018-05-07 11:59:10 -0700117 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500123
Ed Tanous167e2372018-05-07 11:59:10 -0700124 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500130
Ed Tanous167e2372018-05-07 11:59:10 -0700131 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500138
Ed Tanous167e2372018-05-07 11:59:10 -0700139 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500146
Ed Tanous167e2372018-05-07 11:59:10 -0700147 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 Kobylak2a8bfc92017-05-11 09:16:02 -0500153
154finish:
Ed Tanous167e2372018-05-07 11:59:10 -0700155 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Adriana Kobylak04d7c7d2017-05-04 14:06:18 -0500156}
157
Andrew Geissler981f2662017-03-03 15:58:23 -0600158/* print out the distinct dbus service name for the input dbus path */
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500159static int get_service_main(int argc, char* argv[])
Andrew Geissler981f2662017-03-03 15:58:23 -0600160{
Ed Tanous167e2372018-05-07 11:59:10 -0700161 int r;
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500162 sd_bus* conn = NULL;
163 char* service = NULL;
Andrew Geissler981f2662017-03-03 15:58:23 -0600164
Ed Tanous167e2372018-05-07 11:59:10 -0700165 if (argc != 3)
166 {
167 fprintf(stderr, "Usage: %s get-service OBJECTPATH\n", argv[0]);
168 exit(EXIT_FAILURE);
169 }
Andrew Geissler981f2662017-03-03 15:58:23 -0600170
Ed Tanous167e2372018-05-07 11:59:10 -0700171 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 Geissler981f2662017-03-03 15:58:23 -0600177
Ed Tanous167e2372018-05-07 11:59:10 -0700178 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 Geissler981f2662017-03-03 15:58:23 -0600185
Ed Tanous167e2372018-05-07 11:59:10 -0700186 printf("%s\n", service);
Andrew Geissler981f2662017-03-03 15:58:23 -0600187
188finish:
Ed Tanous167e2372018-05-07 11:59:10 -0700189 exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Andrew Geissler981f2662017-03-03 15:58:23 -0600190}
191
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500192int main(int argc, char* argv[])
Brad Bishop2afe7182016-08-13 14:08:17 -0400193{
Matt Spinlercc6ee9c2018-09-19 13:23:13 -0500194 static const char* usage =
Ed Tanous167e2372018-05-07 11:59:10 -0700195 "Usage: %s {COMMAND} ...\n"
196 "\nCOMMANDS:\n"
Ed Tanous167e2372018-05-07 11:59:10 -0700197 " 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 Bishop2afe7182016-08-13 14:08:17 -0400203
Ed Tanous167e2372018-05-07 11:59:10 -0700204 if (argc < 2)
205 {
206 fprintf(stderr, usage, argv[0]);
207 exit(EXIT_FAILURE);
208 }
Brad Bishop2afe7182016-08-13 14:08:17 -0400209
Ed Tanous167e2372018-05-07 11:59:10 -0700210 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 Bishop2afe7182016-08-13 14:08:17 -0400216
Ed Tanous167e2372018-05-07 11:59:10 -0700217 fprintf(stderr, usage, argv[0]);
218 exit(EXIT_FAILURE);
Brad Bishop2afe7182016-08-13 14:08:17 -0400219}