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