ubusd: convert tx_queue to linked list
[project/ubus.git] / ubusd_main.c
1 /*
2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7 #include <sys/socket.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #ifdef FreeBSD
11 #include <sys/param.h>
12 #endif
13 #include <string.h>
14 #include <syslog.h>
15
16 #include <libubox/usock.h>
17
18 #include "ubusd.h"
19
20 static void handle_client_disconnect(struct ubus_client *cl)
21 {
22 struct ubus_msg_buf_list *ubl, *ubl2;
23 list_for_each_entry_safe(ubl, ubl2, &cl->tx_queue, list)
24 ubus_msg_list_free(ubl);
25
26 ubusd_monitor_disconnect(cl);
27 ubusd_proto_free_client(cl);
28 if (cl->pending_msg_fd >= 0)
29 close(cl->pending_msg_fd);
30 uloop_fd_delete(&cl->sock);
31 close(cl->sock.fd);
32 free(cl);
33 }
34
35 static void client_cb(struct uloop_fd *sock, unsigned int events)
36 {
37 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
38 uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
39 struct msghdr msghdr = { 0 };
40 struct ubus_msg_buf *ub;
41 struct ubus_msg_buf_list *ubl, *ubl2;
42 static struct iovec iov;
43 struct cmsghdr *cmsg;
44 int *pfd;
45
46 msghdr.msg_iov = &iov,
47 msghdr.msg_iovlen = 1,
48 msghdr.msg_control = fd_buf;
49 msghdr.msg_controllen = sizeof(fd_buf);
50
51 cmsg = CMSG_FIRSTHDR(&msghdr);
52 cmsg->cmsg_type = SCM_RIGHTS;
53 cmsg->cmsg_level = SOL_SOCKET;
54 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
55
56 pfd = (int *) CMSG_DATA(cmsg);
57 msghdr.msg_controllen = cmsg->cmsg_len;
58
59 /* first try to tx more pending data */
60 list_for_each_entry_safe(ubl, ubl2, &cl->tx_queue, list) {
61 ssize_t written;
62
63 ub = ubl->msg;
64 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
65 if (written < 0) {
66 switch(errno) {
67 case EINTR:
68 case EAGAIN:
69 break;
70 default:
71 goto disconnect;
72 }
73 break;
74 }
75
76 cl->txq_ofs += written;
77 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
78 break;
79
80 ubus_msg_list_free(ubl);
81 }
82
83 /* prevent further ULOOP_WRITE events if we don't have data
84 * to send anymore */
85 if (list_empty(&cl->tx_queue) && (events & ULOOP_WRITE))
86 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
87
88 retry:
89 if (!sock->eof && cl->pending_msg_offset < (int) sizeof(cl->hdrbuf)) {
90 int offset = cl->pending_msg_offset;
91 int bytes;
92
93 *pfd = -1;
94
95 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
96 iov.iov_len = sizeof(cl->hdrbuf) - offset;
97
98 if (cl->pending_msg_fd < 0) {
99 msghdr.msg_control = fd_buf;
100 msghdr.msg_controllen = cmsg->cmsg_len;
101 } else {
102 msghdr.msg_control = NULL;
103 msghdr.msg_controllen = 0;
104 }
105
106 bytes = recvmsg(sock->fd, &msghdr, 0);
107 if (bytes < 0)
108 goto out;
109
110 if (*pfd >= 0)
111 cl->pending_msg_fd = *pfd;
112
113 cl->pending_msg_offset += bytes;
114 if (cl->pending_msg_offset < (int) sizeof(cl->hdrbuf))
115 goto out;
116
117 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
118 goto disconnect;
119
120 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
121 if (!cl->pending_msg)
122 goto disconnect;
123
124 cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
125 cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
126
127 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
128 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
129 }
130
131 ub = cl->pending_msg;
132 if (ub) {
133 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
134 int len = blob_raw_len(ub->data) - offset;
135 int bytes = 0;
136
137 if (len > 0) {
138 bytes = read(sock->fd, (char *) ub->data + offset, len);
139 if (bytes <= 0)
140 goto out;
141 }
142
143 if (bytes < len) {
144 cl->pending_msg_offset += bytes;
145 goto out;
146 }
147
148 /* accept message */
149 ub->fd = cl->pending_msg_fd;
150 cl->pending_msg_fd = -1;
151 cl->pending_msg_offset = 0;
152 cl->pending_msg = NULL;
153 ubusd_monitor_message(cl, ub, false);
154 ubusd_proto_receive_message(cl, ub);
155 goto retry;
156 }
157
158 out:
159 if (!sock->eof || !list_empty(&cl->tx_queue))
160 return;
161
162 disconnect:
163 handle_client_disconnect(cl);
164 }
165
166 static bool get_next_connection(int fd)
167 {
168 struct ubus_client *cl;
169 int client_fd;
170
171 client_fd = accept(fd, NULL, 0);
172 if (client_fd < 0) {
173 switch (errno) {
174 case ECONNABORTED:
175 case EINTR:
176 return true;
177 default:
178 return false;
179 }
180 }
181
182 cl = ubusd_proto_new_client(client_fd, client_cb);
183 if (cl)
184 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
185 else
186 close(client_fd);
187
188 return true;
189 }
190
191 static void server_cb(struct uloop_fd *fd, unsigned int events)
192 {
193 bool next;
194
195 do {
196 next = get_next_connection(fd->fd);
197 } while (next);
198 }
199
200 static struct uloop_fd server_fd = {
201 .cb = server_cb,
202 };
203
204 static int usage(const char *progname)
205 {
206 fprintf(stderr, "Usage: %s [<options>]\n"
207 "Options: \n"
208 " -A <path>: Set the path to ACL files\n"
209 " -s <socket>: Set the unix domain socket to listen on\n"
210 "\n", progname);
211 return 1;
212 }
213
214 static void sighup_handler(int sig)
215 {
216 ubusd_acl_load();
217 }
218
219 static void mkdir_sockdir()
220 {
221 char *ubus_sock_dir, *tmp;
222
223 ubus_sock_dir = strdup(UBUS_UNIX_SOCKET);
224 tmp = strrchr(ubus_sock_dir, '/');
225 if (tmp) {
226 *tmp = '\0';
227 mkdir(ubus_sock_dir, 0755);
228 }
229 free(ubus_sock_dir);
230 }
231
232 int main(int argc, char **argv)
233 {
234 const char *ubus_socket = UBUS_UNIX_SOCKET;
235 int ret = 0;
236 int ch;
237
238 signal(SIGPIPE, SIG_IGN);
239 signal(SIGHUP, sighup_handler);
240
241 openlog("ubusd", LOG_PID, LOG_DAEMON);
242 uloop_init();
243
244 while ((ch = getopt(argc, argv, "A:s:")) != -1) {
245 switch (ch) {
246 case 's':
247 ubus_socket = optarg;
248 break;
249 case 'A':
250 ubusd_acl_dir = optarg;
251 break;
252 default:
253 return usage(argv[0]);
254 }
255 }
256
257 mkdir_sockdir();
258 unlink(ubus_socket);
259 umask(0111);
260 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
261 if (server_fd.fd < 0) {
262 perror("usock");
263 ret = -1;
264 goto out;
265 }
266 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
267 ubusd_acl_load();
268
269 uloop_run();
270 unlink(ubus_socket);
271
272 out:
273 uloop_done();
274 return ret;
275 }