ubusd: replace ubusd_msg_unshare() with ubus_msg_new() to prevent invalid free-ing
[project/ubus.git] / examples / server.c
1 /*
2 * Copyright (C) 2011-2014 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 #include <signal.h>
16
17 #include <libubox/blobmsg_json.h>
18 #include "libubus.h"
19
20 static struct ubus_context *ctx;
21 static struct ubus_subscriber test_event;
22 static struct blob_buf b;
23
24 enum {
25 HELLO_ID,
26 HELLO_MSG,
27 __HELLO_MAX
28 };
29
30 static const struct blobmsg_policy hello_policy[] = {
31 [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
32 [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
33 };
34
35 struct hello_request {
36 struct ubus_request_data req;
37 struct uloop_timeout timeout;
38 int fd;
39 int idx;
40 char data[];
41 };
42
43 static void test_hello_fd_reply(struct uloop_timeout *t)
44 {
45 struct hello_request *req = container_of(t, struct hello_request, timeout);
46 char *data;
47
48 data = alloca(strlen(req->data) + 32);
49 sprintf(data, "msg%d: %s\n", ++req->idx, req->data);
50 if (write(req->fd, data, strlen(data)) < 0) {
51 close(req->fd);
52 free(req);
53 return;
54 }
55
56 uloop_timeout_set(&req->timeout, 1000);
57 }
58
59 static void test_hello_reply(struct uloop_timeout *t)
60 {
61 struct hello_request *req = container_of(t, struct hello_request, timeout);
62 int fds[2];
63
64 blob_buf_init(&b, 0);
65 blobmsg_add_string(&b, "message", req->data);
66 ubus_send_reply(ctx, &req->req, b.head);
67
68 pipe(fds);
69 ubus_request_set_fd(ctx, &req->req, fds[0]);
70 ubus_complete_deferred_request(ctx, &req->req, 0);
71 req->fd = fds[1];
72
73 req->timeout.cb = test_hello_fd_reply;
74 test_hello_fd_reply(t);
75 }
76
77 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
78 struct ubus_request_data *req, const char *method,
79 struct blob_attr *msg)
80 {
81 struct hello_request *hreq;
82 struct blob_attr *tb[__HELLO_MAX];
83 const char *format = "%s received a message: %s";
84 const char *msgstr = "(unknown)";
85
86 blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
87
88 if (tb[HELLO_MSG])
89 msgstr = blobmsg_data(tb[HELLO_MSG]);
90
91 hreq = calloc(1, sizeof(*hreq) + strlen(format) + strlen(obj->name) + strlen(msgstr) + 1);
92 sprintf(hreq->data, format, obj->name, msgstr);
93 ubus_defer_request(ctx, req, &hreq->req);
94 hreq->timeout.cb = test_hello_reply;
95 uloop_timeout_set(&hreq->timeout, 1000);
96
97 return 0;
98 }
99
100 enum {
101 WATCH_ID,
102 WATCH_COUNTER,
103 __WATCH_MAX
104 };
105
106 static const struct blobmsg_policy watch_policy[__WATCH_MAX] = {
107 [WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
108 [WATCH_COUNTER] = { .name = "counter", .type = BLOBMSG_TYPE_INT32 },
109 };
110
111 static void
112 test_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
113 uint32_t id)
114 {
115 fprintf(stderr, "Object %08x went away\n", id);
116 }
117
118 static int
119 test_notify(struct ubus_context *ctx, struct ubus_object *obj,
120 struct ubus_request_data *req, const char *method,
121 struct blob_attr *msg)
122 {
123 #if 0
124 char *str;
125
126 str = blobmsg_format_json(msg, true);
127 fprintf(stderr, "Received notification '%s': %s\n", method, str);
128 free(str);
129 #endif
130
131 return 0;
132 }
133
134 static int test_watch(struct ubus_context *ctx, struct ubus_object *obj,
135 struct ubus_request_data *req, const char *method,
136 struct blob_attr *msg)
137 {
138 struct blob_attr *tb[__WATCH_MAX];
139 int ret;
140
141 blobmsg_parse(watch_policy, __WATCH_MAX, tb, blob_data(msg), blob_len(msg));
142 if (!tb[WATCH_ID])
143 return UBUS_STATUS_INVALID_ARGUMENT;
144
145 test_event.remove_cb = test_handle_remove;
146 test_event.cb = test_notify;
147 ret = ubus_subscribe(ctx, &test_event, blobmsg_get_u32(tb[WATCH_ID]));
148 fprintf(stderr, "Watching object %08x: %s\n", blobmsg_get_u32(tb[WATCH_ID]), ubus_strerror(ret));
149 return ret;
150 }
151
152 static const struct ubus_method test_methods[] = {
153 UBUS_METHOD("hello", test_hello, hello_policy),
154 UBUS_METHOD("watch", test_watch, watch_policy),
155 };
156
157 static struct ubus_object_type test_object_type =
158 UBUS_OBJECT_TYPE("test", test_methods);
159
160 static struct ubus_object test_object = {
161 .name = "test",
162 .type = &test_object_type,
163 .methods = test_methods,
164 .n_methods = ARRAY_SIZE(test_methods),
165 };
166
167 static void server_main(void)
168 {
169 int ret;
170
171 ret = ubus_add_object(ctx, &test_object);
172 if (ret)
173 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
174
175 ret = ubus_register_subscriber(ctx, &test_event);
176 if (ret)
177 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
178
179 uloop_run();
180 }
181
182 int main(int argc, char **argv)
183 {
184 const char *ubus_socket = NULL;
185 int ch;
186
187 while ((ch = getopt(argc, argv, "cs:")) != -1) {
188 switch (ch) {
189 case 's':
190 ubus_socket = optarg;
191 break;
192 default:
193 break;
194 }
195 }
196
197 argc -= optind;
198 argv += optind;
199
200 uloop_init();
201 signal(SIGPIPE, SIG_IGN);
202
203 ctx = ubus_connect(ubus_socket);
204 if (!ctx) {
205 fprintf(stderr, "Failed to connect to ubus\n");
206 return -1;
207 }
208
209 ubus_add_uloop(ctx);
210
211 server_main();
212
213 ubus_free(ctx);
214 uloop_done();
215
216 return 0;
217 }