• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ubus/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                 cl->txq_len -= written;
 78                 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
 79                         break;
 80 
 81                 cl->txq_ofs = 0;
 82                 ubus_msg_list_free(ubl);
 83         }
 84 
 85         /* prevent further ULOOP_WRITE events if we don't have data
 86          * to send anymore */
 87         if (list_empty(&cl->tx_queue) && (events & ULOOP_WRITE))
 88                 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
 89 
 90 retry:
 91         if (!sock->eof && cl->pending_msg_offset < (int) sizeof(cl->hdrbuf)) {
 92                 int offset = cl->pending_msg_offset;
 93                 int bytes;
 94 
 95                 *pfd = -1;
 96 
 97                 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
 98                 iov.iov_len = sizeof(cl->hdrbuf) - offset;
 99 
100                 if (cl->pending_msg_fd < 0) {
101                         msghdr.msg_control = fd_buf;
102                         msghdr.msg_controllen = cmsg->cmsg_len;
103                 } else {
104                         msghdr.msg_control = NULL;
105                         msghdr.msg_controllen = 0;
106                 }
107 
108                 bytes = recvmsg(sock->fd, &msghdr, 0);
109                 if (bytes < 0)
110                         goto out;
111 
112                 if (*pfd >= 0)
113                         cl->pending_msg_fd = *pfd;
114 
115                 cl->pending_msg_offset += bytes;
116                 if (cl->pending_msg_offset < (int) sizeof(cl->hdrbuf))
117                         goto out;
118 
119                 if (blob_raw_len(&cl->hdrbuf.data) < sizeof(struct blob_attr))
120                         goto disconnect;
121                 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
122                         goto disconnect;
123 
124                 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
125                 if (!cl->pending_msg)
126                         goto disconnect;
127 
128                 cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
129                 cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
130 
131                 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
132                 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
133         }
134 
135         ub = cl->pending_msg;
136         if (ub) {
137                 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
138                 int len = blob_raw_len(ub->data) - offset;
139                 int bytes = 0;
140 
141                 if (len > 0) {
142                         bytes = read(sock->fd, (char *) ub->data + offset, len);
143                         if (bytes <= 0)
144                                 goto out;
145                 }
146 
147                 if (bytes < len) {
148                         cl->pending_msg_offset += bytes;
149                         goto out;
150                 }
151 
152                 /* accept message */
153                 ub->fd = cl->pending_msg_fd;
154                 cl->pending_msg_fd = -1;
155                 cl->pending_msg_offset = 0;
156                 cl->pending_msg = NULL;
157                 ubusd_monitor_message(cl, ub, false);
158                 ubusd_proto_receive_message(cl, ub);
159                 goto retry;
160         }
161 
162 out:
163         if (!sock->eof || !list_empty(&cl->tx_queue))
164                 return;
165 
166 disconnect:
167         handle_client_disconnect(cl);
168 }
169 
170 static bool get_next_connection(int fd)
171 {
172         struct ubus_client *cl;
173         int client_fd;
174 
175         client_fd = accept(fd, NULL, 0);
176         if (client_fd < 0) {
177                 switch (errno) {
178                 case ECONNABORTED:
179                 case EINTR:
180                         return true;
181                 default:
182                         return false;
183                 }
184         }
185 
186         cl = ubusd_proto_new_client(client_fd, client_cb);
187         if (cl)
188                 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
189         else
190                 close(client_fd);
191 
192         return true;
193 }
194 
195 static void server_cb(struct uloop_fd *fd, unsigned int events)
196 {
197         bool next;
198 
199         do {
200                 next = get_next_connection(fd->fd);
201         } while (next);
202 }
203 
204 static struct uloop_fd server_fd = {
205         .cb = server_cb,
206 };
207 
208 static int usage(const char *progname)
209 {
210         fprintf(stderr, "Usage: %s [<options>]\n"
211                 "Options: \n"
212                 "  -A <path>:           Set the path to ACL files\n"
213                 "  -s <socket>:         Set the unix domain socket to listen on\n"
214                 "\n", progname);
215         return 1;
216 }
217 
218 static void sighup_handler(int sig)
219 {
220         ubusd_acl_load();
221 }
222 
223 static void mkdir_sockdir()
224 {
225         char *ubus_sock_dir, *tmp;
226 
227         ubus_sock_dir = strdup(UBUS_UNIX_SOCKET);
228         tmp = strrchr(ubus_sock_dir, '/');
229         if (tmp) {
230                 *tmp = '\0';
231                 mkdir(ubus_sock_dir, 0755);
232         }
233         free(ubus_sock_dir);
234 }
235 
236 #include <libubox/ulog.h>
237 
238 int main(int argc, char **argv)
239 {
240         const char *ubus_socket = UBUS_UNIX_SOCKET;
241         int ret = 0;
242         int ch;
243 
244         signal(SIGPIPE, SIG_IGN);
245         signal(SIGHUP, sighup_handler);
246 
247         ulog_open(ULOG_KMSG | ULOG_SYSLOG, LOG_DAEMON, "ubusd");
248         openlog("ubusd", LOG_PID, LOG_DAEMON);
249         uloop_init();
250 
251         while ((ch = getopt(argc, argv, "A:s:")) != -1) {
252                 switch (ch) {
253                 case 's':
254                         ubus_socket = optarg;
255                         break;
256                 case 'A':
257                         ubusd_acl_dir = optarg;
258                         break;
259                 default:
260                         return usage(argv[0]);
261                 }
262         }
263 
264         mkdir_sockdir();
265         unlink(ubus_socket);
266         umask(0111);
267         server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
268         if (server_fd.fd < 0) {
269                 perror("usock");
270                 ret = -1;
271                 goto out;
272         }
273         uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
274         ubusd_acl_load();
275 
276         uloop_run();
277         unlink(ubus_socket);
278 
279 out:
280         uloop_done();
281         return ret;
282 }
283 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt