ubusd: fix build on non-linux systems without peercred support
[project/ubus.git] / cli.c
1 /*
2  * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <unistd.h>
15
16 #include <libubox/blobmsg_json.h>
17 #include "libubus.h"
18
19 static struct blob_buf b;
20 static int timeout = 30;
21 static bool simple_output = false;
22 static int verbose = 0;
23
24 static const char *format_type(void *priv, struct blob_attr *attr)
25 {
26         static const char * const attr_types[] = {
27                 [BLOBMSG_TYPE_INT8] = "\"Boolean\"",
28                 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
29                 [BLOBMSG_TYPE_STRING] = "\"String\"",
30                 [BLOBMSG_TYPE_ARRAY] = "\"Array\"",
31                 [BLOBMSG_TYPE_TABLE] = "\"Table\"",
32         };
33         const char *type = NULL;
34         int typeid;
35
36         if (blob_id(attr) != BLOBMSG_TYPE_INT32)
37                 return NULL;
38
39         typeid = blobmsg_get_u32(attr);
40         if (typeid < ARRAY_SIZE(attr_types))
41                 type = attr_types[typeid];
42         if (!type)
43                 type = "\"(unknown)\"";
44
45         return type;
46 }
47
48 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
49 {
50         struct blob_attr *cur;
51         char *s;
52         int rem;
53
54         if (simple_output || !verbose) {
55                 printf("%s\n", obj->path);
56                 return;
57         }
58
59         printf("'%s' @%08x\n", obj->path, obj->id);
60
61         if (!obj->signature)
62                 return;
63
64         blob_for_each_attr(cur, obj->signature, rem) {
65                 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL, -1);
66                 printf("\t%s\n", s);
67                 free(s);
68         }
69 }
70
71 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
72 {
73         char *str;
74         if (!msg)
75                 return;
76
77         str = blobmsg_format_json_indent(msg, true, simple_output ? -1 : 0);
78         printf("%s\n", str);
79         free(str);
80 }
81
82 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
83                           const char *type, struct blob_attr *msg)
84 {
85         char *str;
86
87         str = blobmsg_format_json(msg, true);
88         printf("{ \"%s\": %s }\n", type, str);
89         fflush(stdout);
90         free(str);
91 }
92
93 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
94 {
95         const char *path = NULL;
96
97         if (argc > 1)
98                 return -2;
99
100         if (argc == 1)
101                 path = argv[0];
102
103         return ubus_lookup(ctx, path, receive_list_result, NULL);
104 }
105
106 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
107 {
108         uint32_t id;
109         int ret;
110
111         if (argc < 2 || argc > 3)
112                 return -2;
113
114         blob_buf_init(&b, 0);
115         if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
116                 if (!simple_output)
117                         fprintf(stderr, "Failed to parse message data\n");
118                 return -1;
119         }
120
121         ret = ubus_lookup_id(ctx, argv[0], &id);
122         if (ret)
123                 return ret;
124
125         return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
126 }
127
128 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
129 {
130         static struct ubus_event_handler listener;
131         const char *event;
132         int ret = 0;
133
134         memset(&listener, 0, sizeof(listener));
135         listener.cb = receive_event;
136
137         if (argc > 0) {
138                 event = argv[0];
139         } else {
140                 event = "*";
141                 argc = 1;
142         }
143
144         do {
145                 ret = ubus_register_event_handler(ctx, &listener, event);
146                 if (ret)
147                         break;
148
149                 argv++;
150                 argc--;
151                 if (argc <= 0)
152                         break;
153
154                 event = argv[0];
155         } while (1);
156
157         if (ret) {
158                 if (!simple_output)
159                         fprintf(stderr, "Error while registering for event '%s': %s\n",
160                                 event, ubus_strerror(ret));
161                 return -1;
162         }
163
164         uloop_init();
165         ubus_add_uloop(ctx);
166         uloop_run();
167         uloop_done();
168
169         return 0;
170 }
171
172 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
173 {
174         if (argc < 1 || argc > 2)
175                 return -2;
176
177         blob_buf_init(&b, 0);
178
179         if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
180                 if (!simple_output)
181                         fprintf(stderr, "Failed to parse message data\n");
182                 return -1;
183         }
184
185         return ubus_send_event(ctx, argv[0], b.head);
186 }
187
188 struct cli_wait_data {
189         struct uloop_timeout timeout;
190         struct ubus_event_handler ev;
191         char **pending;
192         int n_pending;
193 };
194
195 static void wait_check_object(struct cli_wait_data *data, const char *path)
196 {
197         int i;
198
199         for (i = 0; i < data->n_pending; i++) {
200                 if (strcmp(path, data->pending[i]) != 0)
201                         continue;
202
203                 data->n_pending--;
204                 if (i == data->n_pending)
205                         break;
206
207                 memmove(&data->pending[i], &data->pending[i + 1],
208                         (data->n_pending - i) * sizeof(*data->pending));
209                 i--;
210         }
211
212         if (!data->n_pending)
213                 uloop_end();
214 }
215
216 static void wait_event_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
217                           const char *type, struct blob_attr *msg)
218 {
219         static const struct blobmsg_policy policy = {
220                 "path", BLOBMSG_TYPE_STRING
221         };
222         struct cli_wait_data *data = container_of(ev, struct cli_wait_data, ev);
223         struct blob_attr *attr;
224         const char *path;
225
226         if (strcmp(type, "ubus.object.add") != 0)
227                 return;
228
229         blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
230         if (!attr)
231                 return;
232
233         path = blobmsg_data(attr);
234         wait_check_object(data, path);
235 }
236
237 static void wait_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
238 {
239         struct cli_wait_data *data = priv;
240
241         wait_check_object(data, obj->path);
242 }
243
244
245 static void wait_timeout(struct uloop_timeout *timeout)
246 {
247         uloop_end();
248 }
249
250 static int ubus_cli_wait_for(struct ubus_context *ctx, int argc, char **argv)
251 {
252         struct cli_wait_data data = {
253                 .timeout.cb = wait_timeout,
254                 .ev.cb = wait_event_cb,
255                 .pending = argv,
256                 .n_pending = argc,
257         };
258         int ret;
259
260         if (argc < 1)
261                 return -2;
262
263         uloop_init();
264         ubus_add_uloop(ctx);
265
266         ret = ubus_lookup(ctx, NULL, wait_list_cb, &data);
267         if (ret)
268                 return ret;
269
270         if (!data.n_pending)
271                 return ret;
272
273         ret = ubus_register_event_handler(ctx, &data.ev, "ubus.object.add");
274         if (ret)
275                 return ret;
276
277         uloop_timeout_set(&data.timeout, timeout * 1000);
278         uloop_run();
279         uloop_done();
280
281         if (data.n_pending)
282                 return UBUS_STATUS_TIMEOUT;
283
284         return ret;
285 }
286
287
288 static int usage(const char *prog)
289 {
290         fprintf(stderr,
291                 "Usage: %s [<options>] <command> [arguments...]\n"
292                 "Options:\n"
293                 " -s <socket>:          Set the unix domain socket to connect to\n"
294                 " -t <timeout>:         Set the timeout (in seconds) for a command to complete\n"
295                 " -S:                   Use simplified output (for scripts)\n"
296                 " -v:                   More verbose output\n"
297                 "\n"
298                 "Commands:\n"
299                 " - list [<path>]                       List objects\n"
300                 " - call <path> <method> [<message>]    Call an object method\n"
301                 " - listen [<path>...]                  Listen for events\n"
302                 " - send <type> [<message>]             Send an event\n"
303                 " - wait_for <object> [<object>...]     Wait for multiple objects to appear on ubus\n"
304                 "\n", prog);
305         return 1;
306 }
307
308
309 struct {
310         const char *name;
311         int (*cb)(struct ubus_context *ctx, int argc, char **argv);
312 } commands[] = {
313         { "list", ubus_cli_list },
314         { "call", ubus_cli_call },
315         { "listen", ubus_cli_listen },
316         { "send", ubus_cli_send },
317         { "wait_for", ubus_cli_wait_for },
318 };
319
320 int main(int argc, char **argv)
321 {
322         const char *progname, *ubus_socket = NULL;
323         static struct ubus_context *ctx;
324         char *cmd;
325         int ret = 0;
326         int i, ch;
327
328         progname = argv[0];
329
330         while ((ch = getopt(argc, argv, "vs:t:S")) != -1) {
331                 switch (ch) {
332                 case 's':
333                         ubus_socket = optarg;
334                         break;
335                 case 't':
336                         timeout = atoi(optarg);
337                         break;
338                 case 'S':
339                         simple_output = true;
340                         break;
341                 case 'v':
342                         verbose++;
343                         break;
344                 default:
345                         return usage(progname);
346                 }
347         }
348
349         argc -= optind;
350         argv += optind;
351
352         cmd = argv[0];
353         if (argc < 1)
354                 return usage(progname);
355
356         ctx = ubus_connect(ubus_socket);
357         if (!ctx) {
358                 if (!simple_output)
359                         fprintf(stderr, "Failed to connect to ubus\n");
360                 return -1;
361         }
362
363         argv++;
364         argc--;
365
366         ret = -2;
367         for (i = 0; i < ARRAY_SIZE(commands); i++) {
368                 if (strcmp(commands[i].name, cmd) != 0)
369                         continue;
370
371                 ret = commands[i].cb(ctx, argc, argv);
372                 break;
373         }
374
375         if (ret > 0 && !simple_output)
376                 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));
377         else if (ret == -2)
378                 usage(progname);
379
380         ubus_free(ctx);
381         return ret;
382 }