make message data on invoke() optional
[project/ubus.git] / ubusd.c
1 #include <sys/socket.h>
2 #include <sys/uio.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 #include <libubox/blob.h>
9 #include <libubox/uloop.h>
10 #include <libubox/usock.h>
11 #include <libubox/list.h>
12
13 #include "ubusd.h"
14
15 static struct avl_tree clients;
16
17 static struct ubus_msg_buf *ubus_msg_unshare(struct ubus_msg_buf *ub)
18 {
19         ub = realloc(ub, sizeof(*ub) + ub->len);
20         if (!ub)
21                 return NULL;
22
23         ub->refcount = 1;
24         memcpy(ub + 1, ub->data, ub->len);
25         ub->data = (void *) (ub + 1);
26         return ub;
27 }
28
29 struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
30 {
31         if (ub->refcount == ~0)
32                 return ubus_msg_unshare(ub);
33
34         ub->refcount++;
35         return ub;
36 }
37
38 struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
39 {
40         struct ubus_msg_buf *ub;
41         int buflen = sizeof(*ub);
42
43         if (!shared)
44                 buflen += len;
45
46         ub = calloc(1, buflen);
47         if (!ub)
48                 return NULL;
49
50         if (shared) {
51                 ub->refcount = ~0;
52                 ub->data = data;
53         } else {
54                 ub->refcount = 1;
55                 ub->data = (void *) (ub + 1);
56                 if (data)
57                         memcpy(ub + 1, data, len);
58         }
59
60         ub->len = len;
61         return ub;
62 }
63
64 void ubus_msg_free(struct ubus_msg_buf *ub)
65 {
66         switch (ub->refcount) {
67         case 1:
68         case ~0:
69                 free(ub);
70                 break;
71         default:
72                 ub->refcount--;
73                 break;
74         }
75 }
76
77 static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
78 {
79         struct iovec iov[2];
80
81         if (offset < sizeof(ub->hdr)) {
82                 iov[0].iov_base = ((char *) &ub->hdr) + offset;
83                 iov[0].iov_len = sizeof(ub->hdr) - offset;
84                 iov[1].iov_base = (char *) ub->data;
85                 iov[1].iov_len = ub->len;
86                 return writev(fd, iov, 2);
87         } else {
88                 offset -= sizeof(ub->hdr);
89                 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
90         }
91 }
92
93 /* takes the msgbuf reference */
94 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub)
95 {
96         int written;
97
98         if (cl->buf_head)
99                 goto queue;
100
101         written = ubus_msg_writev(cl->sock.fd, ub, 0);
102         if (written > 0 && written < ub->len + sizeof(ub->hdr)) {
103                 cl->buf_head_ofs = written;
104
105                 /* get an event once we can write to the socket again */
106                 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
107                 goto queue;
108         }
109
110         ubus_msg_free(ub);
111         return;
112
113 queue:
114         ub = ubus_msg_unshare(ub);
115         ub->next = NULL;
116         *cl->buf_tail = ub;
117         cl->buf_tail = &ub->next;
118 }
119
120 static void handle_client_disconnect(struct ubus_client *cl)
121 {
122         struct ubus_object *obj;
123
124         while (!list_empty(&cl->objects)) {
125                 obj = list_first_entry(&cl->objects, struct ubus_object, list);
126                 ubusd_free_object(obj);
127         }
128
129         ubus_free_id(&clients, &cl->id);
130         uloop_fd_delete(&cl->sock);
131         close(cl->sock.fd);
132         free(cl);
133 }
134
135 static void client_cb(struct uloop_fd *sock, unsigned int events)
136 {
137         struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
138         struct ubus_msg_buf *ub;
139
140         /* first try to tx more pending data */
141         while (cl->buf_head) {
142                 struct ubus_msg_buf *ub = cl->buf_head;
143                 int written;
144
145                 written = ubus_msg_writev(sock->fd, ub, cl->buf_head_ofs);
146                 if (written < 0) {
147                         switch(errno) {
148                         case EINTR:
149                         case EAGAIN:
150                                 break;
151                         default:
152                                 goto disconnect;
153                         }
154                         break;
155                 }
156                 if (written == 0)
157                         break;
158
159                 cl->buf_head_ofs += written;
160                 if (cl->buf_head_ofs < ub->len + sizeof(ub->hdr))
161                         break;
162
163                 cl->buf_head_ofs = 0;
164                 cl->buf_head = ub->next;
165                 if (!cl->buf_head)
166                         cl->buf_tail = &cl->buf_head;
167         }
168
169         /* prevent further ULOOP_WRITE events if we don't have data
170          * to send anymore */
171         if (!cl->buf_head && (events & ULOOP_WRITE))
172                 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
173
174 retry:
175         if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
176                 int offset = cl->pending_msg_offset;
177                 int bytes;
178
179                 bytes = read(sock->fd, (char *)&cl->hdrbuf + offset, sizeof(cl->hdrbuf) - offset);
180                 if (bytes < 0)
181                         goto out;
182
183                 cl->pending_msg_offset += bytes;
184                 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
185                         goto out;
186
187                 if (blob_len(&cl->hdrbuf.data) + sizeof(cl->hdrbuf) > UBUS_MAX_MSGLEN)
188                         goto disconnect;
189
190                 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
191                 if (!cl->pending_msg)
192                         goto disconnect;
193
194                 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
195                 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
196         }
197
198         ub = cl->pending_msg;
199         if (ub) {
200                 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
201                 int len = blob_raw_len(ub->data) - offset;
202                 int bytes = 0;
203
204                 if (len > 0) {
205                         bytes = read(sock->fd, (char *) ub->data + offset, len);
206                         if (bytes <= 0)
207                                 goto out;
208                 }
209
210                 if (bytes < len) {
211                         cl->pending_msg_offset += bytes;
212                         goto out;
213                 }
214
215                 /* accept message */
216                 cl->pending_msg_offset = 0;
217                 cl->pending_msg = NULL;
218                 ubusd_receive_message(cl, ub);
219                 goto retry;
220         }
221
222 out:
223         if (!sock->eof || cl->buf_head)
224                 return;
225
226 disconnect:
227         handle_client_disconnect(cl);
228 }
229
230 struct ubus_client *ubusd_get_client_by_id(uint32_t id)
231 {
232         struct ubus_id *clid;
233
234         clid = ubus_find_id(&clients, id);
235         if (!clid)
236                 return NULL;
237
238         return container_of(clid, struct ubus_client, id);
239 }
240
241 static bool get_next_connection(int fd)
242 {
243         struct ubus_client *cl;
244         int client_fd;
245
246         client_fd = accept(fd, NULL, 0);
247         if (client_fd < 0) {
248                 switch (errno) {
249                 case ECONNABORTED:
250                 case EINTR:
251                         return true;
252                 default:
253                         return false;
254                 }
255         }
256
257         cl = calloc(1, sizeof(*cl));
258         cl->sock.fd = client_fd;
259
260         INIT_LIST_HEAD(&cl->objects);
261         if (!ubus_alloc_id(&clients, &cl->id))
262                 goto error;
263
264         cl->sock.cb = client_cb;
265         uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
266         if (!ubusd_send_hello(cl))
267                 goto error_free;
268
269         return true;
270
271 error_free:
272         ubus_free_id(&clients, &cl->id);
273 error:
274         close(cl->sock.fd);
275         free(cl);
276         return true;
277 }
278
279 static void server_cb(struct uloop_fd *fd, unsigned int events)
280 {
281         bool next;
282
283         do {
284                 next = get_next_connection(fd->fd);
285         } while (next);
286 }
287
288 static struct uloop_fd server_fd = {
289         .cb = server_cb,
290 };
291
292 int main(int argc, char **argv)
293 {
294         int ret = 0;
295
296         signal(SIGPIPE, SIG_IGN);
297
298         ubus_init_id_tree(&clients);
299
300         uloop_init();
301
302         unlink(UBUS_UNIX_SOCKET);
303         server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, UBUS_UNIX_SOCKET, NULL);
304         if (server_fd.fd < 0) {
305                 perror("usock");
306                 ret = -1;
307                 goto out;
308         }
309         uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
310
311         uloop_run();
312
313 out:
314         uloop_done();
315         return ret;
316 }