CMakeLists.txt: bump minimum cmake version
[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 ubus_client_cmd_free(struct ubus_client_cmd *cmd)
36 {
37 list_del(&cmd->list);
38 ubus_msg_free(cmd->msg);
39 free(cmd);
40 }
41
42 static void ubus_client_cmd_queue_process(struct ubus_client *cl)
43 {
44 struct ubus_client_cmd *cmd, *tmp;
45
46 list_for_each_entry_safe(cmd, tmp, &cl->cmd_queue, list) {
47 int ret = ubusd_cmd_lookup(cl, cmd);
48
49 /* Stop if the last command caused buffering again */
50 if (ret == -2)
51 break;
52
53 ubus_client_cmd_free(cmd);
54 }
55 }
56
57 static void client_cb(struct uloop_fd *sock, unsigned int events)
58 {
59 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
60 uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
61 struct msghdr msghdr = { 0 };
62 struct ubus_msg_buf *ub;
63 struct ubus_msg_buf_list *ubl, *ubl2;
64 static struct iovec iov;
65 struct cmsghdr *cmsg;
66 int *pfd;
67
68 msghdr.msg_iov = &iov,
69 msghdr.msg_iovlen = 1,
70 msghdr.msg_control = fd_buf;
71 msghdr.msg_controllen = sizeof(fd_buf);
72
73 cmsg = CMSG_FIRSTHDR(&msghdr);
74 cmsg->cmsg_type = SCM_RIGHTS;
75 cmsg->cmsg_level = SOL_SOCKET;
76 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
77
78 pfd = (int *) CMSG_DATA(cmsg);
79 msghdr.msg_controllen = cmsg->cmsg_len;
80
81 /* first try to tx more pending data */
82 list_for_each_entry_safe(ubl, ubl2, &cl->tx_queue, list) {
83 ssize_t written;
84
85 ub = ubl->msg;
86 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
87 if (written < 0) {
88 switch(errno) {
89 case EINTR:
90 case EAGAIN:
91 break;
92 default:
93 goto disconnect;
94 }
95 break;
96 }
97
98 cl->txq_ofs += written;
99 cl->txq_len -= written;
100 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
101 break;
102
103 cl->txq_ofs = 0;
104 ubus_msg_list_free(ubl);
105 }
106
107 if (list_empty(&cl->tx_queue) && (events & ULOOP_WRITE)) {
108 /* Process queued commands */
109 ubus_client_cmd_queue_process(cl);
110
111 /* prevent further ULOOP_WRITE events if we don't have data
112 * to send anymore */
113 if (list_empty(&cl->tx_queue))
114 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
115 }
116
117 retry:
118 if (!sock->eof && cl->pending_msg_offset < (int) sizeof(cl->hdrbuf)) {
119 int offset = cl->pending_msg_offset;
120 int bytes;
121
122 *pfd = -1;
123
124 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
125 iov.iov_len = sizeof(cl->hdrbuf) - offset;
126
127 if (cl->pending_msg_fd < 0) {
128 msghdr.msg_control = fd_buf;
129 msghdr.msg_controllen = cmsg->cmsg_len;
130 } else {
131 msghdr.msg_control = NULL;
132 msghdr.msg_controllen = 0;
133 }
134
135 bytes = recvmsg(sock->fd, &msghdr, 0);
136 if (bytes < 0)
137 goto out;
138
139 if (*pfd >= 0)
140 cl->pending_msg_fd = *pfd;
141
142 cl->pending_msg_offset += bytes;
143 if (cl->pending_msg_offset < (int) sizeof(cl->hdrbuf))
144 goto out;
145
146 if (blob_raw_len(&cl->hdrbuf.data) < sizeof(struct blob_attr))
147 goto disconnect;
148 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
149 goto disconnect;
150
151 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
152 if (!cl->pending_msg)
153 goto disconnect;
154
155 cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
156 cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
157
158 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
159 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
160 }
161
162 ub = cl->pending_msg;
163 if (ub) {
164 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
165 int len = blob_raw_len(ub->data) - offset;
166 int bytes = 0;
167
168 if (len > 0) {
169 bytes = read(sock->fd, (char *) ub->data + offset, len);
170 if (bytes <= 0)
171 goto out;
172 }
173
174 if (bytes < len) {
175 cl->pending_msg_offset += bytes;
176 goto out;
177 }
178
179 /* accept message */
180 ub->fd = cl->pending_msg_fd;
181 cl->pending_msg_fd = -1;
182 cl->pending_msg_offset = 0;
183 cl->pending_msg = NULL;
184 ubusd_monitor_message(cl, ub, false);
185 ubusd_proto_receive_message(cl, ub);
186 goto retry;
187 }
188
189 out:
190 if (!sock->eof || !list_empty(&cl->tx_queue))
191 return;
192
193 disconnect:
194 handle_client_disconnect(cl);
195 }
196
197 static bool get_next_connection(int fd)
198 {
199 struct ubus_client *cl;
200 int client_fd;
201
202 client_fd = accept(fd, NULL, 0);
203 if (client_fd < 0) {
204 switch (errno) {
205 case ECONNABORTED:
206 case EINTR:
207 return true;
208 default:
209 return false;
210 }
211 }
212
213 cl = ubusd_proto_new_client(client_fd, client_cb);
214 if (cl)
215 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
216 else
217 close(client_fd);
218
219 return true;
220 }
221
222 static void server_cb(struct uloop_fd *fd, unsigned int events)
223 {
224 bool next;
225
226 do {
227 next = get_next_connection(fd->fd);
228 } while (next);
229 }
230
231 static struct uloop_fd server_fd = {
232 .cb = server_cb,
233 };
234
235 static int usage(const char *progname)
236 {
237 fprintf(stderr, "Usage: %s [<options>]\n"
238 "Options: \n"
239 " -A <path>: Set the path to ACL files\n"
240 " -s <socket>: Set the unix domain socket to listen on\n"
241 "\n", progname);
242 return 1;
243 }
244
245 static void sighup_handler(int sig)
246 {
247 ubusd_acl_load();
248 }
249
250 static void mkdir_sockdir()
251 {
252 char *ubus_sock_dir, *tmp;
253
254 ubus_sock_dir = strdup(UBUS_UNIX_SOCKET);
255 tmp = strrchr(ubus_sock_dir, '/');
256 if (tmp) {
257 *tmp = '\0';
258 mkdir(ubus_sock_dir, 0755);
259 }
260 free(ubus_sock_dir);
261 }
262
263 #include <libubox/ulog.h>
264
265 int main(int argc, char **argv)
266 {
267 const char *ubus_socket = UBUS_UNIX_SOCKET;
268 int ret = 0;
269 int ch;
270
271 signal(SIGPIPE, SIG_IGN);
272 signal(SIGHUP, sighup_handler);
273
274 ulog_open(ULOG_KMSG | ULOG_SYSLOG, LOG_DAEMON, "ubusd");
275 openlog("ubusd", LOG_PID, LOG_DAEMON);
276 uloop_init();
277
278 while ((ch = getopt(argc, argv, "A:s:")) != -1) {
279 switch (ch) {
280 case 's':
281 ubus_socket = optarg;
282 break;
283 case 'A':
284 ubusd_acl_dir = optarg;
285 break;
286 default:
287 return usage(argv[0]);
288 }
289 }
290
291 mkdir_sockdir();
292 unlink(ubus_socket);
293 umask(0111);
294 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
295 if (server_fd.fd < 0) {
296 perror("usock");
297 ret = -1;
298 goto out;
299 }
300 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
301 ubusd_acl_load();
302
303 uloop_run();
304 unlink(ubus_socket);
305
306 out:
307 uloop_done();
308 return ret;
309 }