1 #define _GNU_SOURCE 2 #include <errno.h> 3 #include <limits.h> 4 #include <signal.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <sys/prctl.h> 9 #include <sys/socket.h> 10 #include <sys/stat.h> 11 #include <sys/time.h> 12 #include <sys/uio.h> 13 #include <sys/wait.h> 14 #include <unistd.h> 15 16 #include <libubox/usock.h> 17 18 #include "libubus.h" 19 #include "test-ubusd.h" 20 21 static int dummy_handler(struct ubus_context *ctx, struct ubus_object *obj, 22 struct ubus_request_data *req, const char *method, 23 struct blob_attr *msg) 24 { 25 return 0; 26 } 27 28 static const struct ubus_method test_methods[] = { 29 UBUS_METHOD_NOARG("dummy", dummy_handler), 30 }; 31 32 static struct ubus_object_type test_type = 33 UBUS_OBJECT_TYPE("test_remove_lost", test_methods); 34 35 static struct ubus_object_type other_type = 36 UBUS_OBJECT_TYPE("test_remove_lost_other", test_methods); 37 38 static struct ubus_object *object_new(const char *name) 39 { 40 struct ubus_object *obj = calloc(1, sizeof(*obj)); 41 42 if (!obj) 43 exit(2); 44 45 obj->name = name; 46 obj->type = &test_type; 47 obj->methods = test_methods; 48 obj->n_methods = ARRAY_SIZE(test_methods); 49 50 return obj; 51 } 52 53 /* 54 * The send of the remove request fails. A reconnect to a new ubusd must not 55 * register the removed object again: the caller freed it. 56 */ 57 static void test_send_failed_then_reconnect(void) 58 { 59 struct ubus_object *removed = object_new("test.removed"); 60 struct ubus_object *never_added = object_new("test.never"); 61 struct ubus_object *kept = object_new("test.kept"); 62 struct ubus_context *ctx; 63 int ret; 64 65 ubusd_start(); 66 ctx = ubus_connect(sock_path); 67 CHECK(ctx); 68 if (!ctx) 69 return; 70 71 CHECK(!ubus_add_object(ctx, removed)); 72 CHECK(!ubus_add_object(ctx, kept)); 73 CHECK(test_type.id); 74 75 ubusd_kill(); 76 77 ret = ubus_remove_object(ctx, removed); 78 CHECK(ret == UBUS_STATUS_INVALID_ARGUMENT); 79 CHECK(ctx->sock.eof); 80 CHECK(!removed->id); 81 CHECK(ctx->objects.count == 1); 82 CHECK(!test_type.id); 83 free(removed); 84 85 CHECK(ubus_remove_object(ctx, never_added)); 86 CHECK(ctx->objects.count == 1); 87 free(never_added); 88 89 ubusd_start(); 90 CHECK(!ubus_reconnect(ctx, sock_path)); 91 CHECK(kept->id); 92 CHECK(ctx->objects.count == 1); 93 CHECK(object_visible("test.kept")); 94 CHECK(!object_visible("test.removed")); 95 96 CHECK(!ubus_remove_object(ctx, kept)); 97 ubus_free(ctx); 98 free(kept); 99 ubusd_kill(); 100 } 101 102 /* 103 * ubusd goes away while the remove request waits for its answer. An object 104 * of the same type on a new context and a new ubusd must be visible. The type 105 * id goes with the connection, also where another object of the type stays. 106 */ 107 static void test_answer_lost_then_new_context(void) 108 { 109 struct ubus_object *removed = object_new("test.removed"); 110 struct ubus_object *kept = object_new("test.kept"); 111 struct ubus_object *added = object_new("test.added"); 112 struct itimerval timer = { .it_value = { .tv_usec = 200 * 1000 } }; 113 struct ubus_context *ctx; 114 int ret; 115 116 ubusd_start(); 117 ctx = ubus_connect(sock_path); 118 CHECK(ctx); 119 if (!ctx) 120 return; 121 122 CHECK(!ubus_add_object(ctx, removed)); 123 CHECK(!ubus_add_object(ctx, kept)); 124 CHECK(test_type.id); 125 126 kill(ubusd_pid, SIGSTOP); 127 signal(SIGALRM, ubusd_kill_alarm); 128 setitimer(ITIMER_REAL, &timer, NULL); 129 130 ret = ubus_remove_object(ctx, removed); 131 CHECK(ret == UBUS_STATUS_CONNECTION_FAILED); 132 CHECK(!removed->id); 133 CHECK(ctx->objects.count == 1); 134 CHECK(!test_type.id); 135 ubusd_reap(); 136 ubus_free(ctx); 137 free(removed); 138 free(kept); 139 140 ubusd_start(); 141 ctx = ubus_connect(sock_path); 142 CHECK(ctx); 143 if (!ctx) 144 return; 145 146 CHECK(!ubus_add_object(ctx, added)); 147 CHECK(object_visible("test.added")); 148 149 CHECK(!ubus_remove_object(ctx, added)); 150 ubus_free(ctx); 151 free(added); 152 ubusd_kill(); 153 } 154 155 static int reconnect_ret; 156 static bool reconnect_ubusd_start; 157 158 static void reconnect_connection_lost(struct ubus_context *ctx) 159 { 160 ubusd_reap(); 161 if (reconnect_ubusd_start) 162 ubusd_start(); 163 164 reconnect_ret = ubus_reconnect(ctx, sock_path); 165 } 166 167 static int remove_with_reconnect(struct ubus_context *ctx, 168 struct ubus_object *obj, bool ubusd_restart) 169 { 170 struct itimerval timer = { .it_value = { .tv_usec = 200 * 1000 } }; 171 172 reconnect_ret = -1; 173 reconnect_ubusd_start = ubusd_restart; 174 ctx->connection_lost = reconnect_connection_lost; 175 176 kill(ubusd_pid, SIGSTOP); 177 signal(SIGALRM, ubusd_kill_alarm); 178 setitimer(ITIMER_REAL, &timer, NULL); 179 180 return ubus_remove_object(ctx, obj); 181 } 182 183 /* 184 * The connection_lost callback reconnects to a new ubusd while the remove 185 * waits. The new ubusd must not get the removed object, and the type keeps 186 * the id that the new ubusd gave it for the remaining object. 187 */ 188 static void test_answer_lost_reconnect_in_callback(void) 189 { 190 struct ubus_object *removed = object_new("test.removed"); 191 struct ubus_object *kept = object_new("test.kept"); 192 struct ubus_context *ctx; 193 uint32_t old_type_id; 194 int ret; 195 196 ubusd_start(); 197 ctx = ubus_connect(sock_path); 198 CHECK(ctx); 199 if (!ctx) 200 return; 201 202 CHECK(!ubus_add_object(ctx, removed)); 203 CHECK(!ubus_add_object(ctx, kept)); 204 old_type_id = test_type.id; 205 206 ret = remove_with_reconnect(ctx, removed, true); 207 CHECK(!reconnect_ret); 208 CHECK(ret == UBUS_STATUS_TIMEOUT); 209 CHECK(!ctx->sock.eof); 210 CHECK(!removed->id); 211 CHECK(ctx->objects.count == 1); 212 CHECK(kept->id); 213 CHECK(test_type.id && test_type.id != old_type_id); 214 CHECK(object_visible("test.kept")); 215 CHECK(!object_visible("test.removed")); 216 free(removed); 217 218 ubusd_kill(); 219 ubusd_start(); 220 CHECK(!ubus_reconnect(ctx, sock_path)); 221 CHECK(ctx->objects.count == 1); 222 CHECK(object_visible("test.kept")); 223 CHECK(!object_visible("test.removed")); 224 225 CHECK(!ubus_remove_object(ctx, kept)); 226 ubus_free(ctx); 227 free(kept); 228 ubusd_kill(); 229 } 230 231 /* 232 * The connection_lost callback fails to reconnect while the remove waits. A 233 * later reconnect must not register the removed object again. Neither an 234 * object of the same type nor one of another type keeps the type id of the 235 * removed object, as no ubusd knows that id any more. 236 */ 237 static void test_answer_lost_reconnect_failed_in_callback(void) 238 { 239 struct ubus_object *removed = object_new("test.removed"); 240 struct ubus_object *same = object_new("test.same"); 241 struct ubus_object *other = object_new("test.other"); 242 struct ubus_context *ctx; 243 int ret; 244 245 other->type = &other_type; 246 247 ubusd_start(); 248 ctx = ubus_connect(sock_path); 249 CHECK(ctx); 250 if (!ctx) 251 return; 252 253 CHECK(!ubus_add_object(ctx, removed)); 254 CHECK(!ubus_add_object(ctx, same)); 255 CHECK(!ubus_add_object(ctx, other)); 256 CHECK(test_type.id); 257 258 ret = remove_with_reconnect(ctx, removed, false); 259 CHECK(reconnect_ret == UBUS_STATUS_CONNECTION_FAILED); 260 CHECK(ret == UBUS_STATUS_TIMEOUT); 261 CHECK(!ctx->sock.eof); 262 CHECK(!removed->id); 263 CHECK(ctx->objects.count == 2); 264 CHECK(!test_type.id); 265 free(removed); 266 267 ubusd_start(); 268 CHECK(!ubus_reconnect(ctx, sock_path)); 269 CHECK(ctx->objects.count == 2); 270 CHECK(object_visible("test.same")); 271 CHECK(object_visible("test.other")); 272 CHECK(!object_visible("test.removed")); 273 274 CHECK(!ubus_remove_object(ctx, same)); 275 CHECK(!ubus_remove_object(ctx, other)); 276 ubus_free(ctx); 277 free(same); 278 free(other); 279 ubusd_kill(); 280 } 281 282 static struct ubus_object *nested_obj; 283 static int nested_ret; 284 285 static void nested_remove_complete(struct ubus_request *req, int ret) 286 { 287 nested_ret = ubus_remove_object(req->ctx, nested_obj); 288 } 289 290 static void nested_remove_connection_lost(struct ubus_context *ctx) 291 { 292 ubusd_reap(); 293 nested_ret = ubus_remove_object(ctx, nested_obj); 294 } 295 296 /* 297 * The answer to an async request waits in the socket, so the next wait of 298 * the context reads it and its complete_cb removes the object again. 299 */ 300 static void nested_remove_arm(struct ubus_context *ctx, struct ubus_object *obj, 301 struct ubus_request *req) 302 { 303 static struct blob_buf b; 304 305 nested_obj = obj; 306 nested_ret = -1; 307 308 blob_buf_init(&b, 0); 309 CHECK(!ubus_invoke_async(ctx, UBUS_SYSTEM_OBJECT_EVENT, "send", b.head, req)); 310 req->complete_cb = nested_remove_complete; 311 ubus_complete_request_async(ctx, req); 312 usleep(200 * 1000); 313 } 314 315 /* 316 * A handler that runs during the wait of a remove removes the same object 317 * again. ubusd handled the first remove, so the second one gets NOT_FOUND. 318 */ 319 static void test_nested_remove(void) 320 { 321 struct ubus_object *removed = object_new("test.removed"); 322 struct ubus_object *kept = object_new("test.kept"); 323 struct ubus_request req; 324 struct ubus_context *ctx; 325 int ret; 326 327 ubusd_start(); 328 ctx = ubus_connect(sock_path); 329 CHECK(ctx); 330 if (!ctx) 331 return; 332 333 CHECK(!ubus_add_object(ctx, removed)); 334 CHECK(!ubus_add_object(ctx, kept)); 335 336 nested_remove_arm(ctx, removed, &req); 337 ret = ubus_remove_object(ctx, removed); 338 CHECK(!ret); 339 CHECK(nested_ret == UBUS_STATUS_NOT_FOUND); 340 CHECK(!removed->id); 341 CHECK(ctx->objects.count == 1); 342 CHECK(test_type.id); 343 CHECK(object_visible("test.kept")); 344 CHECK(!object_visible("test.removed")); 345 free(removed); 346 347 CHECK(!ubus_remove_object(ctx, kept)); 348 CHECK(!ctx->objects.count); 349 ubus_free(ctx); 350 free(kept); 351 ubusd_kill(); 352 } 353 354 /* 355 * ubusd refuses both removes of a nested remove. The object goes back into 356 * ctx->objects once. 357 */ 358 static void test_nested_remove_refused(void) 359 { 360 struct ubus_object *refused = object_new("test.refused"); 361 struct ubus_object *kept = object_new("test.kept"); 362 struct ubus_request req; 363 struct ubus_context *ctx; 364 int ret; 365 366 ubusd_start(); 367 ctx = ubus_connect(sock_path); 368 CHECK(ctx); 369 if (!ctx) 370 return; 371 372 CHECK(!ubus_add_object(ctx, kept)); 373 374 /* an id that ubusd does not know, so that it refuses the remove */ 375 refused->id = kept->id ^ 0x40000000; 376 refused->avl.key = &refused->id; 377 CHECK(!avl_insert(&ctx->objects, &refused->avl)); 378 379 nested_remove_arm(ctx, refused, &req); 380 ret = ubus_remove_object(ctx, refused); 381 CHECK(ret == UBUS_STATUS_NOT_FOUND); 382 CHECK(nested_ret == UBUS_STATUS_NOT_FOUND); 383 CHECK(refused->id == (kept->id ^ 0x40000000)); 384 CHECK(ctx->objects.count == 2); 385 CHECK(avl_find(&ctx->objects, &refused->id) == &refused->avl); 386 CHECK(avl_find(&ctx->objects, &kept->id) == &kept->avl); 387 388 avl_delete(&ctx->objects, &refused->avl); 389 CHECK(!ubus_remove_object(ctx, kept)); 390 CHECK(!ctx->objects.count); 391 ubus_free(ctx); 392 free(refused); 393 free(kept); 394 ubusd_kill(); 395 } 396 397 /* 398 * The connection_lost callback removes the object that the remove waits for. 399 * The send fails, and libubus forgets the object. 400 */ 401 static void test_nested_remove_connection_lost(void) 402 { 403 struct ubus_object *removed = object_new("test.removed"); 404 struct itimerval timer = { .it_value = { .tv_usec = 200 * 1000 } }; 405 struct ubus_context *ctx; 406 int ret; 407 408 ubusd_start(); 409 ctx = ubus_connect(sock_path); 410 CHECK(ctx); 411 if (!ctx) 412 return; 413 414 CHECK(!ubus_add_object(ctx, removed)); 415 416 nested_obj = removed; 417 nested_ret = -1; 418 ctx->connection_lost = nested_remove_connection_lost; 419 420 kill(ubusd_pid, SIGSTOP); 421 signal(SIGALRM, ubusd_kill_alarm); 422 setitimer(ITIMER_REAL, &timer, NULL); 423 424 ret = ubus_remove_object(ctx, removed); 425 CHECK(ret == UBUS_STATUS_CONNECTION_FAILED); 426 CHECK(nested_ret == UBUS_STATUS_INVALID_ARGUMENT); 427 CHECK(!removed->id); 428 CHECK(!ctx->objects.count); 429 CHECK(!test_type.id); 430 free(removed); 431 432 ubus_free(ctx); 433 } 434 435 /* An error answer on a live connection leaves the object registered. */ 436 static void test_error_answer_keeps_object(void) 437 { 438 struct ubus_object *obj = object_new("test.kept"); 439 struct ubus_object *never_added = object_new("test.never"); 440 struct ubus_context *ctx; 441 uint32_t id; 442 int ret; 443 444 ubusd_start(); 445 ctx = ubus_connect(sock_path); 446 CHECK(ctx); 447 if (!ctx) 448 return; 449 450 CHECK(!ubus_add_object(ctx, obj)); 451 id = obj->id; 452 453 /* The tree holds one node, so changing its key keeps it consistent. */ 454 obj->id = id ^ 0x40000000; 455 ret = ubus_remove_object(ctx, obj); 456 CHECK(ret == UBUS_STATUS_NOT_FOUND); 457 CHECK(!ctx->sock.eof); 458 CHECK(obj->id == (id ^ 0x40000000)); 459 CHECK(ctx->objects.count == 1); 460 CHECK(test_type.id); 461 462 CHECK(ubus_remove_object(ctx, never_added) == UBUS_STATUS_NOT_FOUND); 463 CHECK(ctx->objects.count == 1); 464 465 obj->id = id; 466 CHECK(!ubus_remove_object(ctx, obj)); 467 CHECK(!ctx->objects.count); 468 ubus_free(ctx); 469 free(never_added); 470 free(obj); 471 ubusd_kill(); 472 } 473 474 /* 475 * On a live connection ubusd frees the type with its last object, and the 476 * type id goes with it. 477 */ 478 static void test_live_remove_type_id(void) 479 { 480 struct ubus_object *first = object_new("test.first"); 481 struct ubus_object *last = object_new("test.last"); 482 struct ubus_context *ctx; 483 uint32_t type_id; 484 485 ubusd_start(); 486 ctx = ubus_connect(sock_path); 487 CHECK(ctx); 488 if (!ctx) 489 return; 490 491 CHECK(!ubus_add_object(ctx, first)); 492 CHECK(!ubus_add_object(ctx, last)); 493 type_id = test_type.id; 494 CHECK(type_id); 495 496 CHECK(!ubus_remove_object(ctx, first)); 497 CHECK(!first->id); 498 CHECK(ctx->objects.count == 1); 499 CHECK(test_type.id == type_id); 500 501 CHECK(!ubus_remove_object(ctx, last)); 502 CHECK(!last->id); 503 CHECK(!ctx->objects.count); 504 CHECK(!test_type.id); 505 506 ubus_free(ctx); 507 free(first); 508 free(last); 509 ubusd_kill(); 510 } 511 512 /* 513 * The context is freed while it still holds an object. ubusd drops the 514 * object and its type with the connection, so an object of the same type 515 * on a new context must be visible. 516 */ 517 static void test_free_with_object_then_new_context(void) 518 { 519 struct ubus_object *held = object_new("test.held"); 520 struct ubus_object *added = object_new("test.added"); 521 struct ubus_context *ctx; 522 523 ubusd_start(); 524 ctx = ubus_connect(sock_path); 525 CHECK(ctx); 526 if (!ctx) 527 return; 528 529 CHECK(!ubus_add_object(ctx, held)); 530 CHECK(test_type.id); 531 532 ubus_free(ctx); 533 534 ctx = ubus_connect(sock_path); 535 CHECK(ctx); 536 if (!ctx) 537 return; 538 539 CHECK(!ubus_add_object(ctx, added)); 540 CHECK(test_type.id); 541 CHECK(object_visible("test.added")); 542 543 CHECK(!ubus_remove_object(ctx, added)); 544 ubus_free(ctx); 545 free(added); 546 free(held); 547 ubusd_kill(); 548 } 549 550 /* 551 * ubusd refuses a type id it does not know, and libubus registers the type 552 * again from its signature. Another refusal keeps the type id. 553 */ 554 static void test_unknown_type_id(void) 555 { 556 struct ubus_object *obj = object_new("test.unknown_type"); 557 struct ubus_object *dup = object_new("test.unknown_type"); 558 struct ubus_context *ctx; 559 uint32_t type_id; 560 561 ubusd_start(); 562 ctx = ubus_connect(sock_path); 563 CHECK(ctx); 564 if (!ctx) 565 return; 566 567 test_type.id = 0x7ffffffe; 568 CHECK(!ubus_add_object(ctx, obj)); 569 CHECK(obj->id); 570 CHECK(test_type.id && test_type.id != 0x7ffffffe); 571 CHECK(object_visible("test.unknown_type")); 572 573 type_id = test_type.id; 574 CHECK(ubus_add_object(ctx, dup) == UBUS_STATUS_INVALID_ARGUMENT); 575 CHECK(!dup->id); 576 CHECK(test_type.id == type_id); 577 578 CHECK(!ubus_remove_object(ctx, obj)); 579 ubus_free(ctx); 580 free(dup); 581 free(obj); 582 ubusd_kill(); 583 } 584 585 /* 586 * ubusd refuses the unknown type id, and then refuses the add with the 587 * signature for a duplicate path. The add returns the second refusal. 588 */ 589 static void test_unknown_type_id_retry_refused(void) 590 { 591 struct ubus_object *obj = object_new("test.retry_refused"); 592 struct ubus_object *dup = object_new("test.retry_refused"); 593 struct ubus_context *ctx; 594 595 ubusd_start(); 596 ctx = ubus_connect(sock_path); 597 CHECK(ctx); 598 if (!ctx) 599 return; 600 601 CHECK(!ubus_add_object(ctx, obj)); 602 CHECK(test_type.id); 603 604 test_type.id = 0x7ffffffe; 605 CHECK(ubus_add_object(ctx, dup) == UBUS_STATUS_INVALID_ARGUMENT); 606 CHECK(!dup->id); 607 CHECK(!test_type.id); 608 CHECK(ctx->objects.count == 1); 609 CHECK(object_visible("test.retry_refused")); 610 611 CHECK(!ubus_remove_object(ctx, obj)); 612 ubus_free(ctx); 613 free(dup); 614 free(obj); 615 ubusd_kill(); 616 } 617 618 static const struct ubus_method unnamed_methods[] = { 619 UBUS_METHOD_NOARG("", dummy_handler), 620 }; 621 622 static struct ubus_object_type unnamed_type = 623 UBUS_OBJECT_TYPE("test_remove_lost_unnamed", unnamed_methods); 624 625 /* 626 * ubusd cannot register a type whose signature holds a method without a 627 * name. The add fails, and no object is left that a lookup cannot find. 628 */ 629 static void test_signature_refused(void) 630 { 631 struct ubus_object *obj = object_new("test.unnamed"); 632 struct ubus_context *ctx; 633 uint32_t id; 634 635 obj->type = &unnamed_type; 636 obj->methods = unnamed_methods; 637 obj->n_methods = ARRAY_SIZE(unnamed_methods); 638 639 ubusd_start(); 640 ctx = ubus_connect(sock_path); 641 CHECK(ctx); 642 if (!ctx) 643 return; 644 645 CHECK(ubus_add_object(ctx, obj) == UBUS_STATUS_INVALID_ARGUMENT); 646 CHECK(!obj->id); 647 CHECK(!unnamed_type.id); 648 CHECK(!ctx->objects.count); 649 CHECK(ubus_lookup_id(ctx, "test.unnamed", &id) == UBUS_STATUS_NOT_FOUND); 650 651 ubus_free(ctx); 652 free(obj); 653 ubusd_kill(); 654 } 655 656 /* 657 * ubusd refuses the remove of an object that another context of the same 658 * process registered. Neither tree changes. 659 */ 660 static void test_remove_other_context(void) 661 { 662 struct ubus_object *obj = object_new("test.other_ctx"); 663 struct ubus_object *kept = object_new("test.kept"); 664 struct ubus_context *ctx_a, *ctx_b; 665 uint32_t id; 666 667 ubusd_start(); 668 ctx_a = ubus_connect(sock_path); 669 ctx_b = ubus_connect(sock_path); 670 CHECK(ctx_a && ctx_b); 671 if (!ctx_a || !ctx_b) 672 return; 673 674 CHECK(!ubus_add_object(ctx_a, obj)); 675 CHECK(!ubus_add_object(ctx_b, kept)); 676 id = obj->id; 677 678 CHECK(ubus_remove_object(ctx_b, obj) == UBUS_STATUS_PERMISSION_DENIED); 679 CHECK(obj->id == id); 680 CHECK(ctx_a->objects.count == 1); 681 CHECK(avl_find(&ctx_a->objects, &obj->id) == &obj->avl); 682 CHECK(ctx_b->objects.count == 1); 683 CHECK(avl_find(&ctx_b->objects, &kept->id) == &kept->avl); 684 CHECK(object_visible("test.other_ctx")); 685 686 CHECK(!ubus_remove_object(ctx_a, obj)); 687 CHECK(!ctx_a->objects.count); 688 CHECK(!ubus_remove_object(ctx_b, kept)); 689 ubus_free(ctx_a); 690 ubus_free(ctx_b); 691 free(obj); 692 free(kept); 693 ubusd_kill(); 694 } 695 696 /* 697 * Another node in ctx->objects holds the id of the object that is removed. 698 * The remove must leave that node alone. 699 */ 700 static void test_remove_id_collision(void) 701 { 702 struct ubus_object *kept = object_new("test.kept"); 703 struct ubus_object *holder = object_new("test.holder"); 704 struct ubus_object *removed = object_new("test.removed"); 705 struct ubus_context *ctx; 706 707 ubusd_start(); 708 ctx = ubus_connect(sock_path); 709 CHECK(ctx); 710 if (!ctx) 711 return; 712 713 CHECK(!ubus_add_object(ctx, kept)); 714 715 /* an id that ubusd does not know, so that it refuses the remove */ 716 holder->id = kept->id ^ 0x40000000; 717 holder->avl.key = &holder->id; 718 CHECK(!avl_insert(&ctx->objects, &holder->avl)); 719 removed->id = holder->id; 720 721 CHECK(ubus_remove_object(ctx, removed) == UBUS_STATUS_NOT_FOUND); 722 CHECK(removed->id == holder->id); 723 CHECK(ctx->objects.count == 2); 724 CHECK(avl_find(&ctx->objects, &holder->id) == &holder->avl); 725 CHECK(avl_find(&ctx->objects, &kept->id) == &kept->avl); 726 727 avl_delete(&ctx->objects, &holder->avl); 728 CHECK(!ubus_remove_object(ctx, kept)); 729 CHECK(!ctx->objects.count); 730 ubus_free(ctx); 731 free(kept); 732 free(holder); 733 free(removed); 734 ubusd_kill(); 735 } 736 737 static void refusal_then_eof_complete(struct ubus_request *req, int ret) 738 { 739 usleep(200 * 1000); 740 ubusd_kill(); 741 ubus_handle_event(req->ctx); 742 ubus_handle_event(req->ctx); 743 } 744 745 /* 746 * A callback during the wait reads ubusd's refusal of the remove, and then 747 * the connection ends. The wait ends with CONNECTION_FAILED, and libubus 748 * forgets the object as on a lost connection. 749 */ 750 static void test_refusal_then_eof(void) 751 { 752 struct ubus_object *kept = object_new("test.kept"); 753 struct ubus_object *refused = object_new("test.refused"); 754 struct ubus_request req; 755 struct ubus_context *ctx; 756 static struct blob_buf b; 757 int ret; 758 759 ubusd_start(); 760 ctx = ubus_connect(sock_path); 761 CHECK(ctx); 762 if (!ctx) 763 return; 764 765 CHECK(!ubus_add_object(ctx, kept)); 766 767 /* an id that ubusd does not know, so that it refuses the remove */ 768 refused->id = kept->id ^ 0x40000000; 769 refused->avl.key = &refused->id; 770 CHECK(!avl_insert(&ctx->objects, &refused->avl)); 771 772 blob_buf_init(&b, 0); 773 CHECK(!ubus_invoke_async(ctx, UBUS_SYSTEM_OBJECT_EVENT, "send", b.head, &req)); 774 req.complete_cb = refusal_then_eof_complete; 775 ubus_complete_request_async(ctx, &req); 776 usleep(200 * 1000); 777 778 ret = ubus_remove_object(ctx, refused); 779 CHECK(ret == UBUS_STATUS_CONNECTION_FAILED); 780 CHECK(ctx->sock.eof); 781 CHECK(!refused->id); 782 CHECK(ctx->objects.count == 1); 783 CHECK(avl_find(&ctx->objects, &kept->id) == &kept->avl); 784 CHECK(!test_type.id); 785 786 ubus_free(ctx); 787 free(kept); 788 free(refused); 789 } 790 791 static int queued_reconnect_ret; 792 793 static int queued_reconnect_handler(struct ubus_context *ctx, 794 struct ubus_object *obj, 795 struct ubus_request_data *req, 796 const char *method, struct blob_attr *msg) 797 { 798 ubusd_kill(); 799 ubusd_start(); 800 queued_reconnect_ret = ubus_reconnect(ctx, sock_path); 801 return 0; 802 } 803 804 static const struct ubus_method reconnect_methods[] = { 805 UBUS_METHOD_NOARG("reconnect", queued_reconnect_handler), 806 }; 807 808 static struct ubus_object_type reconnect_type = 809 UBUS_OBJECT_TYPE("test_remove_lost_reconnect", reconnect_methods); 810 811 /* 812 * ubusd refuses the remove. An INVOKE queued during the wait runs at the end 813 * of the wait and reconnects to a new ubusd, which must get the object. 814 */ 815 static void test_refused_remove_reconnect_in_queued_handler(void) 816 { 817 struct ubus_object *trigger = object_new("test.trigger"); 818 struct ubus_object *refused = object_new("test.refused"); 819 struct ubus_context *ctx, *tx; 820 struct ubus_request req; 821 static struct blob_buf b; 822 uint32_t old_id; 823 int ret; 824 825 trigger->type = &reconnect_type; 826 trigger->methods = reconnect_methods; 827 trigger->n_methods = ARRAY_SIZE(reconnect_methods); 828 829 ubusd_start(); 830 ctx = ubus_connect(sock_path); 831 tx = ubus_connect(sock_path); 832 CHECK(ctx && tx); 833 if (!ctx || !tx) 834 return; 835 836 CHECK(!ubus_add_object(ctx, trigger)); 837 838 /* an id that ubusd does not know, so that it refuses the remove */ 839 refused->id = trigger->id ^ 0x40000000; 840 refused->avl.key = &refused->id; 841 CHECK(!avl_insert(&ctx->objects, &refused->avl)); 842 old_id = refused->id; 843 844 queued_reconnect_ret = -1; 845 blob_buf_init(&b, 0); 846 CHECK(!ubus_invoke_async(tx, trigger->id, "reconnect", b.head, &req)); 847 usleep(200 * 1000); 848 849 ret = ubus_remove_object(ctx, refused); 850 CHECK(ret == UBUS_STATUS_NOT_FOUND); 851 CHECK(!queued_reconnect_ret); 852 CHECK(refused->id && refused->id != old_id); 853 CHECK(avl_find(&ctx->objects, &refused->id) == &refused->avl); 854 CHECK(ctx->objects.count == 2); 855 CHECK(object_visible("test.refused")); 856 CHECK(object_visible("test.trigger")); 857 858 ubus_free(tx); 859 CHECK(!ubus_remove_object(ctx, refused)); 860 CHECK(!ubus_remove_object(ctx, trigger)); 861 CHECK(!ctx->objects.count); 862 ubus_free(ctx); 863 free(trigger); 864 free(refused); 865 ubusd_kill(); 866 } 867 868 static void fake_ubusd_send(int fd, uint8_t type, uint16_t seq, uint32_t peer, 869 struct blob_attr *msg) 870 { 871 struct ubus_msghdr hdr = { 872 .type = type, 873 .seq = seq, 874 .peer = peer, 875 }; 876 struct iovec iov[2] = { 877 { .iov_base = &hdr, .iov_len = sizeof(hdr) }, 878 { .iov_base = msg, .iov_len = blob_raw_len(msg) }, 879 }; 880 ssize_t len = iov[0].iov_len + iov[1].iov_len; 881 882 /* libubus reads the header and the first attribute in one read() */ 883 if (writev(fd, iov, 2) != len) 884 _exit(3); 885 } 886 887 static bool fake_ubusd_read_full(int fd, void *buf, size_t len) 888 { 889 ssize_t ret; 890 891 while (len) { 892 ret = read(fd, buf, len); 893 if (ret <= 0) 894 return false; 895 896 buf = (char *) buf + ret; 897 len -= ret; 898 } 899 900 return true; 901 } 902 903 static struct blob_attr *fake_ubusd_read(int fd, struct ubus_msghdr *hdr) 904 { 905 static uint32_t data[UBUS_MAX_MSGLEN / sizeof(uint32_t)]; 906 struct blob_attr *msg = (struct blob_attr *) data; 907 size_t len; 908 909 if (!fake_ubusd_read_full(fd, hdr, sizeof(*hdr)) || 910 !fake_ubusd_read_full(fd, msg, sizeof(*msg))) 911 return NULL; 912 913 len = blob_raw_len(msg); 914 if (len < sizeof(*msg) || len > sizeof(data)) 915 return NULL; 916 917 if (!fake_ubusd_read_full(fd, msg + 1, len - sizeof(*msg))) 918 return NULL; 919 920 return msg; 921 } 922 923 static void fake_ubusd_invoke(int fd, uint32_t objid, const char *method) 924 { 925 struct blob_buf b = {}; 926 927 blob_buf_init(&b, 0); 928 blob_put_int32(&b, UBUS_ATTR_OBJID, objid); 929 blob_put_string(&b, UBUS_ATTR_METHOD, method); 930 blob_put(&b, UBUS_ATTR_DATA, NULL, 0); 931 fake_ubusd_send(fd, UBUS_MSG_INVOKE, 0, 0, b.head); 932 blob_buf_free(&b); 933 } 934 935 static void fake_ubusd_conn(int fd, uint32_t *next_id, bool invoke, 936 bool remove_data) 937 { 938 struct blob_buf b = {}; 939 struct ubus_msghdr hdr; 940 struct blob_attr *msg; 941 uint32_t id; 942 943 blob_buf_init(&b, 0); 944 fake_ubusd_send(fd, UBUS_MSG_HELLO, 0, cpu_to_be32(0x12345678), b.head); 945 946 while ((msg = fake_ubusd_read(fd, &hdr))) { 947 if (hdr.type == UBUS_MSG_STATUS) 948 continue; 949 950 if (hdr.type == UBUS_MSG_ADD_OBJECT) { 951 id = (*next_id)++; 952 blob_buf_init(&b, 0); 953 blob_put_int32(&b, UBUS_ATTR_OBJID, id); 954 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, 0x2000); 955 fake_ubusd_send(fd, UBUS_MSG_DATA, hdr.seq, hdr.peer, b.head); 956 957 if (invoke) 958 fake_ubusd_invoke(fd, id, "remove"); 959 invoke = false; 960 } 961 962 /* the request holds the id that the answer must name */ 963 if (hdr.type == UBUS_MSG_REMOVE_OBJECT && remove_data) 964 fake_ubusd_send(fd, UBUS_MSG_DATA, hdr.seq, hdr.peer, msg); 965 966 blob_buf_init(&b, 0); 967 blob_put_int32(&b, UBUS_ATTR_STATUS, 0); 968 fake_ubusd_send(fd, UBUS_MSG_STATUS, hdr.seq, hdr.peer, b.head); 969 } 970 971 blob_buf_free(&b); 972 } 973 974 /* 975 * A ubusd that gives an id to every add and answers every request with 976 * status 0. With remove_data, it answers a remove with the id of the object, 977 * as ubusd does; else with no DATA. On each connection after the first, it 978 * calls the "remove" method of the first object added there before that add 979 * completes. The seq and peer fields go back unchanged, in network byte 980 * order. 981 */ 982 static void fake_ubusd_serve(int listen_fd, bool remove_data) 983 { 984 uint32_t next_id = 0x1000; 985 bool invoke = false; 986 int fd; 987 988 while ((fd = accept(listen_fd, NULL, NULL)) >= 0) { 989 fake_ubusd_conn(fd, &next_id, invoke, remove_data); 990 close(fd); 991 invoke = true; 992 } 993 994 _exit(3); 995 } 996 997 static pid_t fake_ubusd_start(bool remove_data) 998 { 999 int listen_fd; 1000 pid_t pid; 1001 1002 unlink(sock_path); 1003 listen_fd = usock(USOCK_UNIX | USOCK_SERVER, sock_path, NULL); 1004 if (listen_fd < 0) 1005 return -1; 1006 1007 pid = fork(); 1008 if (pid < 0) 1009 exit(2); 1010 1011 if (!pid) { 1012 prctl(PR_SET_PDEATHSIG, SIGKILL); 1013 fake_ubusd_serve(listen_fd, remove_data); 1014 } 1015 1016 close(listen_fd); 1017 1018 return pid; 1019 } 1020 1021 static void fake_ubusd_kill(pid_t pid) 1022 { 1023 kill(pid, SIGKILL); 1024 waitpid(pid, NULL, 0); 1025 unlink(sock_path); 1026 } 1027 1028 /* 1029 * ubusd answers the remove with status 0 but with no id. libubus keeps the 1030 * object registered and returns NO_DATA. 1031 */ 1032 static void test_remove_answer_without_data(void) 1033 { 1034 struct ubus_object *obj = object_new("test.no_data"); 1035 struct ubus_context *ctx; 1036 pid_t pid; 1037 1038 pid = fake_ubusd_start(false); 1039 CHECK(pid > 0); 1040 if (pid < 0) 1041 return; 1042 1043 ctx = ubus_connect(sock_path); 1044 CHECK(ctx); 1045 if (ctx) { 1046 CHECK(!ubus_add_object(ctx, obj)); 1047 CHECK(obj->id == 0x1000); 1048 1049 CHECK(ubus_remove_object(ctx, obj) == UBUS_STATUS_NO_DATA); 1050 CHECK(obj->id == 0x1000); 1051 CHECK(ctx->objects.count == 1); 1052 CHECK(avl_find(&ctx->objects, &obj->id) == &obj->avl); 1053 CHECK(test_type.id == 0x2000); 1054 1055 avl_delete(&ctx->objects, &obj->avl); 1056 ubus_free(ctx); 1057 } 1058 1059 fake_ubusd_kill(pid); 1060 test_type.id = 0; 1061 free(obj); 1062 } 1063 1064 static struct ubus_object *readd_victim; 1065 static uint32_t readd_victim_id; 1066 static int readd_remove_ret; 1067 1068 static int readd_remove_handler(struct ubus_context *ctx, 1069 struct ubus_object *obj, 1070 struct ubus_request_data *req, 1071 const char *method, struct blob_attr *msg) 1072 { 1073 readd_victim_id = readd_victim->id; 1074 readd_remove_ret = ubus_remove_object(ctx, readd_victim); 1075 return 0; 1076 } 1077 1078 static const struct ubus_method readd_methods[] = { 1079 UBUS_METHOD_NOARG("remove", readd_remove_handler), 1080 }; 1081 1082 static struct ubus_object_type readd_type = 1083 UBUS_OBJECT_TYPE("test_remove_lost_readd", readd_methods); 1084 1085 /* 1086 * The new ubusd calls an object while the reconnect adds the objects again, 1087 * and the handler removes another object. The handler runs once every object 1088 * is back, and the object it removed stays removed. 1089 */ 1090 static void test_reconnect_handler_removes_object(void) 1091 { 1092 struct ubus_object *trigger = object_new("test.trigger"); 1093 struct ubus_object *victim = object_new("test.victim"); 1094 struct ubus_context *ctx; 1095 pid_t pid; 1096 1097 trigger->type = &readd_type; 1098 trigger->methods = readd_methods; 1099 trigger->n_methods = ARRAY_SIZE(readd_methods); 1100 1101 pid = fake_ubusd_start(true); 1102 CHECK(pid > 0); 1103 if (pid < 0) 1104 return; 1105 1106 ctx = ubus_connect(sock_path); 1107 CHECK(ctx); 1108 if (ctx) { 1109 /* the fake gives ascending ids, so the reconnect adds trigger first */ 1110 CHECK(!ubus_add_object(ctx, trigger)); 1111 CHECK(!ubus_add_object(ctx, victim)); 1112 1113 readd_victim = victim; 1114 readd_victim_id = 0; 1115 readd_remove_ret = -1; 1116 CHECK(!ubus_reconnect(ctx, sock_path)); 1117 CHECK(readd_victim_id); 1118 CHECK(!readd_remove_ret); 1119 CHECK(!victim->id); 1120 CHECK(ctx->objects.count == 1); 1121 CHECK(avl_find(&ctx->objects, &trigger->id) == &trigger->avl); 1122 1123 ubus_free(ctx); 1124 } 1125 1126 fake_ubusd_kill(pid); 1127 test_type.id = 0; 1128 readd_type.id = 0; 1129 free(trigger); 1130 free(victim); 1131 } 1132 1133 int main(int argc, char **argv) 1134 { 1135 int ret; 1136 1137 ret = test_ubusd_init(argc, argv); 1138 if (ret) 1139 return ret; 1140 1141 test_live_remove_type_id(); 1142 test_error_answer_keeps_object(); 1143 test_send_failed_then_reconnect(); 1144 test_answer_lost_then_new_context(); 1145 test_answer_lost_reconnect_in_callback(); 1146 test_answer_lost_reconnect_failed_in_callback(); 1147 test_nested_remove(); 1148 test_nested_remove_refused(); 1149 test_nested_remove_connection_lost(); 1150 test_free_with_object_then_new_context(); 1151 test_unknown_type_id(); 1152 test_unknown_type_id_retry_refused(); 1153 test_signature_refused(); 1154 test_remove_other_context(); 1155 test_remove_id_collision(); 1156 test_refusal_then_eof(); 1157 test_refused_remove_reconnect_in_queued_handler(); 1158 test_remove_answer_without_data(); 1159 test_reconnect_handler_removes_object(); 1160 1161 return test_ubusd_done(); 1162 } 1163
This page was automatically generated by LXR 0.3.1. • OpenWrt