1 /* 2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License version 2.1 6 * as published by the Free Software Foundation 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #define _GNU_SOURCE 15 #include <sys/types.h> 16 #include <sys/uio.h> 17 #include <sys/socket.h> 18 19 #include <unistd.h> 20 #include <fcntl.h> 21 #include <poll.h> 22 23 #include <libubox/usock.h> 24 #include <libubox/blob.h> 25 #include <libubox/blobmsg.h> 26 27 #include "libubus.h" 28 #include "libubus-internal.h" 29 30 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) } 31 32 #define UBUS_MSGBUF_REDUCTION_INTERVAL 16 33 34 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = { 35 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 }, 36 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 }, 37 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING }, 38 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING }, 39 [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 }, 40 [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 }, 41 [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED }, 42 }; 43 44 static struct blob_attr *attrbuf[UBUS_ATTR_MAX]; 45 46 __hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg, size_t len) 47 { 48 blob_parse_untrusted(msg, len, attrbuf, ubus_policy, UBUS_ATTR_MAX); 49 return attrbuf; 50 } 51 52 static void wait_data(int fd, bool write) 53 { 54 struct pollfd pfd = { .fd = fd }; 55 56 pfd.events = write ? POLLOUT : POLLIN; 57 poll(&pfd, 1, -1); 58 } 59 60 static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd) 61 { 62 uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 }; 63 struct msghdr msghdr = { 0 }; 64 struct cmsghdr *cmsg; 65 int len = 0; 66 int *pfd; 67 68 msghdr.msg_iov = iov, 69 msghdr.msg_iovlen = iov_len, 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 do { 82 ssize_t cur_len; 83 84 if (sock_fd < 0) { 85 msghdr.msg_control = NULL; 86 msghdr.msg_controllen = 0; 87 } else { 88 *pfd = sock_fd; 89 } 90 91 cur_len = sendmsg(fd, &msghdr, 0); 92 if (cur_len < 0) { 93 switch(errno) { 94 case EAGAIN: 95 wait_data(fd, true); 96 break; 97 case EINTR: 98 break; 99 default: 100 return -1; 101 } 102 continue; 103 } 104 105 sock_fd = -1; 106 107 len += cur_len; 108 while (cur_len >= (ssize_t) iov->iov_len) { 109 cur_len -= iov->iov_len; 110 iov_len--; 111 iov++; 112 if (!iov_len) 113 return len; 114 } 115 iov->iov_base += cur_len; 116 iov->iov_len -= cur_len; 117 msghdr.msg_iov = iov; 118 msghdr.msg_iovlen = iov_len; 119 } while (1); 120 121 /* Should never reach here */ 122 return -1; 123 } 124 125 int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq, 126 struct blob_attr *msg, int cmd, uint32_t peer, int fd) 127 { 128 struct ubus_msghdr hdr; 129 struct iovec iov[2] = { 130 STATIC_IOV(hdr) 131 }; 132 int ret; 133 134 hdr.version = 0; 135 hdr.type = cmd; 136 hdr.seq = cpu_to_be16(seq); 137 hdr.peer = cpu_to_be32(peer); 138 139 if (!msg) { 140 blob_buf_init(&b, 0); 141 msg = b.head; 142 } 143 144 iov[1].iov_base = (char *) msg; 145 iov[1].iov_len = blob_raw_len(msg); 146 147 /* The peer discards anything larger and, on the ubusd path, disconnects 148 * us for sending it, so fail here and let the caller find out. */ 149 if (blob_pad_len(msg) > UBUS_MAX_MSGLEN) { 150 if (fd >= 0) 151 close(fd); 152 153 return -1; 154 } 155 156 ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov), fd); 157 if (ret < 0) 158 ctx->sock.eof = true; 159 160 if (fd >= 0) 161 close(fd); 162 163 return ret; 164 } 165 166 static int recv_retry(struct ubus_context *ctx, struct iovec *iov, bool wait, int *recv_fd) 167 { 168 uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 }; 169 struct msghdr msghdr = { 0 }; 170 struct cmsghdr *cmsg; 171 int total = 0; 172 int bytes; 173 int *pfd; 174 int fd; 175 176 fd = ctx->sock.fd; 177 178 msghdr.msg_iov = iov, 179 msghdr.msg_iovlen = 1, 180 msghdr.msg_control = fd_buf; 181 msghdr.msg_controllen = sizeof(fd_buf); 182 183 cmsg = CMSG_FIRSTHDR(&msghdr); 184 cmsg->cmsg_type = SCM_RIGHTS; 185 cmsg->cmsg_level = SOL_SOCKET; 186 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 187 188 pfd = (int *) CMSG_DATA(cmsg); 189 190 while (iov->iov_len > 0) { 191 if (recv_fd) { 192 msghdr.msg_control = fd_buf; 193 msghdr.msg_controllen = cmsg->cmsg_len; 194 } else { 195 msghdr.msg_control = NULL; 196 msghdr.msg_controllen = 0; 197 } 198 199 *pfd = -1; 200 bytes = recvmsg(fd, &msghdr, 0); 201 if (!bytes) 202 return -1; 203 204 if (bytes < 0) { 205 bytes = 0; 206 if (errno == EINTR) 207 continue; 208 209 if (errno != EAGAIN) 210 return -1; 211 } 212 if (!wait && !bytes) 213 return 0; 214 215 if (recv_fd) 216 *recv_fd = *pfd; 217 218 recv_fd = NULL; 219 220 wait = true; 221 iov->iov_len -= bytes; 222 iov->iov_base += bytes; 223 total += bytes; 224 225 if (iov->iov_len > 0) 226 wait_data(fd, false); 227 } 228 229 return total; 230 } 231 232 bool ubus_validate_hdr(struct ubus_msghdr *hdr) 233 { 234 struct blob_attr *data = (struct blob_attr *) (hdr + 1); 235 236 if (hdr->version != 0) 237 return false; 238 239 if (blob_raw_len(data) < sizeof(*data)) 240 return false; 241 242 if (blob_pad_len(data) > UBUS_MAX_MSGLEN) 243 return false; 244 245 return true; 246 } 247 248 static bool alloc_msg_buf(struct ubus_context *ctx, int len) 249 { 250 void *ptr; 251 int buf_len = ctx->msgbuf_data_len; 252 int rem; 253 254 if (!ctx->msgbuf.data) 255 buf_len = 0; 256 257 rem = (len % UBUS_MSG_CHUNK_SIZE); 258 if (rem > 0) 259 len += UBUS_MSG_CHUNK_SIZE - rem; 260 261 if (len < buf_len && 262 ++ctx->msgbuf_reduction_counter > UBUS_MSGBUF_REDUCTION_INTERVAL) { 263 ctx->msgbuf_reduction_counter = 0; 264 buf_len = 0; 265 } 266 267 if (len <= buf_len) 268 return true; 269 270 ptr = realloc(ctx->msgbuf.data, len); 271 if (!ptr) 272 return false; 273 274 ctx->msgbuf.data = ptr; 275 ctx->msgbuf_data_len = len; 276 return true; 277 } 278 279 static bool get_next_msg(struct ubus_context *ctx, int *recv_fd) 280 { 281 struct { 282 struct ubus_msghdr hdr; 283 struct blob_attr data; 284 } hdrbuf; 285 struct iovec iov = STATIC_IOV(hdrbuf); 286 int len; 287 int r; 288 289 /* receive header + start attribute */ 290 r = recv_retry(ctx, &iov, false, recv_fd); 291 if (r <= 0) { 292 if (r < 0) 293 ctx->sock.eof = true; 294 295 return false; 296 } 297 298 hdrbuf.hdr.seq = be16_to_cpu(hdrbuf.hdr.seq); 299 hdrbuf.hdr.peer = be32_to_cpu(hdrbuf.hdr.peer); 300 301 /* The header is already consumed, so the body cannot be skipped and the 302 * stream cannot be resynchronised. */ 303 if (!ubus_validate_hdr(&hdrbuf.hdr)) { 304 ctx->sock.eof = true; 305 return false; 306 } 307 308 len = blob_raw_len(&hdrbuf.data); 309 if (!alloc_msg_buf(ctx, len)) { 310 ctx->sock.eof = true; 311 return false; 312 } 313 314 memcpy(&ctx->msgbuf.hdr, &hdrbuf.hdr, sizeof(hdrbuf.hdr)); 315 memcpy(ctx->msgbuf.data, &hdrbuf.data, sizeof(hdrbuf.data)); 316 317 iov.iov_base = (char *)ctx->msgbuf.data + sizeof(hdrbuf.data); 318 iov.iov_len = blob_len(ctx->msgbuf.data); 319 if (iov.iov_len > 0 && 320 recv_retry(ctx, &iov, true, NULL) <= 0) { 321 ctx->sock.eof = true; 322 return false; 323 } 324 325 return true; 326 } 327 328 void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events) 329 { 330 struct ubus_context *ctx = container_of(u, struct ubus_context, sock); 331 int recv_fd = -1; 332 333 while (1) { 334 if (!ctx->stack_depth) 335 ctx->pending_timer.cb(&ctx->pending_timer); 336 337 if (!get_next_msg(ctx, &recv_fd)) 338 break; 339 ubus_process_msg(ctx, &ctx->msgbuf, recv_fd); 340 recv_fd = -1; 341 if (uloop_cancelling() || ctx->cancel_poll) 342 break; 343 } 344 345 /* 346 * If get_next_msg() captured an fd via the header recvmsg but then 347 * failed (header validate, body recv, alloc), the fd is sitting in 348 * recv_fd unowned by anyone — close it instead of leaking. 349 */ 350 if (recv_fd >= 0) 351 close(recv_fd); 352 353 if (!ctx->stack_depth) 354 ctx->pending_timer.cb(&ctx->pending_timer); 355 356 if (u->eof) { 357 ubus_flush_requests(ctx); 358 ctx->connection_lost(ctx); 359 } 360 } 361 362 void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout) 363 { 364 struct pollfd pfd = { 365 .fd = ctx->sock.fd, 366 .events = POLLIN | POLLERR, 367 }; 368 369 ctx->cancel_poll = false; 370 poll(&pfd, 1, timeout ? timeout : -1); 371 ubus_handle_data(&ctx->sock, ULOOP_READ); 372 } 373 374 static void 375 ubus_auto_sub_lookup(struct ubus_context *ctx, struct ubus_object_data *obj, 376 void *priv) 377 { 378 struct ubus_subscriber *s; 379 380 list_for_each_entry(s, &ctx->auto_subscribers, list) 381 if (s->new_obj_cb(ctx, s, obj->path)) 382 ubus_subscribe(ctx, s, obj->id); 383 } 384 385 static void 386 ubus_refresh_auto_subscribe(struct ubus_context *ctx) 387 { 388 struct ubus_event_handler *ev = &ctx->auto_subscribe_event_handler; 389 390 if (list_empty(&ctx->auto_subscribers)) 391 return; 392 393 ubus_register_event_handler(ctx, ev, "ubus.object.add"); 394 ubus_lookup(ctx, NULL, ubus_auto_sub_lookup, NULL); 395 } 396 397 static void 398 ubus_refresh_state(struct ubus_context *ctx) 399 { 400 struct ubus_object *obj, *tmp; 401 struct ubus_object **objs; 402 int n, i = 0; 403 404 /* clear all type IDs, they need to be registered again */ 405 avl_for_each_element(&ctx->objects, obj, avl) 406 if (obj->type) 407 obj->type->id = 0; 408 409 /* push out all objects again */ 410 objs = alloca(ctx->objects.count * sizeof(*objs)); 411 avl_remove_all_elements(&ctx->objects, obj, avl, tmp) { 412 objs[i++] = obj; 413 obj->id = 0; 414 } 415 416 for (n = i, i = 0; i < n; i++) 417 ubus_add_object(ctx, objs[i]); 418 419 ubus_refresh_auto_subscribe(ctx); 420 } 421 422 int ubus_reconnect(struct ubus_context *ctx, const char *path) 423 { 424 struct { 425 struct ubus_msghdr hdr; 426 struct blob_attr data; 427 } hdr; 428 struct blob_attr *buf; 429 int ret = UBUS_STATUS_UNKNOWN_ERROR; 430 431 if (ubus_context_is_channel(ctx)) 432 return -1; 433 434 if (!path) 435 path = UBUS_UNIX_SOCKET; 436 437 if (ctx->sock.fd >= 0) { 438 if (ctx->sock.registered) 439 uloop_fd_delete(&ctx->sock); 440 441 close(ctx->sock.fd); 442 } 443 444 ctx->sock.eof = false; 445 ctx->sock.error = false; 446 ctx->sock.fd = usock(USOCK_UNIX, path, NULL); 447 if (ctx->sock.fd < 0) 448 return UBUS_STATUS_CONNECTION_FAILED; 449 450 if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr)) 451 goto out_close; 452 453 if (!ubus_validate_hdr(&hdr.hdr)) 454 goto out_close; 455 456 if (hdr.hdr.type != UBUS_MSG_HELLO) 457 goto out_close; 458 459 buf = calloc(1, blob_raw_len(&hdr.data)); 460 if (!buf) 461 goto out_close; 462 463 memcpy(buf, &hdr.data, sizeof(hdr.data)); 464 if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != (ssize_t) blob_len(buf)) 465 goto out_free; 466 467 ctx->local_id = be32_to_cpu(hdr.hdr.peer); 468 if (ctx->local_id <= UBUS_CLIENT_ID_CHANNEL) 469 goto out_free; 470 471 int flags = fcntl(ctx->sock.fd, F_GETFL); 472 if (flags < 0 || 473 fcntl(ctx->sock.fd, F_SETFL, flags | O_NONBLOCK) < 0 || 474 fcntl(ctx->sock.fd, F_SETFD, FD_CLOEXEC) < 0) 475 goto out_free; 476 477 ret = UBUS_STATUS_OK; 478 ubus_refresh_state(ctx); 479 480 out_free: 481 free(buf); 482 out_close: 483 if (ret) { 484 close(ctx->sock.fd); 485 ctx->sock.fd = -1; 486 } 487 488 return ret; 489 } 490
This page was automatically generated by LXR 0.3.1. • OpenWrt