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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt