1 /* 2 * Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #define L2CAP_SOCKET_CHECKED 27 28 #include <sys/types.h> 29 #include <sys/acl.h> 30 #include <sys/capsicum.h> 31 #include <sys/event.h> 32 #include <sys/extattr.h> 33 #include <sys/inotify.h> 34 #include <sys/linker.h> 35 #include <sys/mman.h> 36 #include <sys/mount.h> 37 #include <sys/poll.h> 38 #include <sys/procctl.h> 39 #include <sys/ptrace.h> 40 #include <sys/reboot.h> 41 #include <sys/resource.h> 42 #include <sys/rtprio.h> 43 #include <sys/sem.h> 44 #include <sys/shm.h> 45 #include <sys/socket.h> 46 #include <sys/stat.h> 47 #include <sys/thr.h> 48 #include <sys/umtx.h> 49 #include <machine/sysarch.h> 50 #include <netinet/in.h> 51 #include <netinet/sctp.h> 52 #include <netinet/tcp.h> 53 #include <netinet/udp.h> 54 #include <netinet/udplite.h> 55 #include <nfsserver/nfs.h> 56 #include <ufs/ufs/quota.h> 57 #include <vm/vm.h> 58 #include <vm/vm_param.h> 59 #include <aio.h> 60 #include <fcntl.h> 61 #include <sched.h> 62 #include <stdbool.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <strings.h> 66 #include <sysdecode.h> 67 #include <unistd.h> 68 #include <sys/bitstring.h> 69 #include <netgraph/bluetooth/include/ng_hci.h> 70 #include <netgraph/bluetooth/include/ng_l2cap.h> 71 #include <netgraph/bluetooth/include/ng_btsocket.h> 72 73 #include "support.h" 74 75 #define X(a) { a, #a }, 76 #define XEND { 0, NULL } 77 78 #define TABLE_START(n) static struct name_table n[] = { 79 #define TABLE_ENTRY X 80 #define TABLE_END XEND }; 81 82 #include "tables.h" 83 84 #undef TABLE_START 85 #undef TABLE_ENTRY 86 #undef TABLE_END 87 88 const char * 89 sysdecode_atfd(int fd) 90 { 91 92 if (fd == AT_FDCWD) 93 return ("AT_FDCWD"); 94 return (NULL); 95 } 96 97 bool 98 sysdecode_atflags(FILE *fp, int flag, int *rem) 99 { 100 101 return (print_mask_int(fp, atflags, flag, rem)); 102 } 103 104 static struct name_table semctlops[] = { 105 X(GETNCNT) X(GETPID) X(GETVAL) X(GETALL) X(GETZCNT) X(SETVAL) X(SETALL) 106 X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND 107 }; 108 109 const char * 110 sysdecode_semctl_cmd(int cmd) 111 { 112 113 return (lookup_value(semctlops, cmd)); 114 } 115 116 static struct name_table shmctlops[] = { 117 X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND 118 }; 119 120 const char * 121 sysdecode_shmctl_cmd(int cmd) 122 { 123 124 return (lookup_value(shmctlops, cmd)); 125 } 126 127 const char * 128 sysdecode_msgctl_cmd(int cmd) 129 { 130 131 return (sysdecode_shmctl_cmd(cmd)); 132 } 133 134 static struct name_table semgetflags[] = { 135 X(IPC_CREAT) X(IPC_EXCL) X(SEM_R) X(SEM_A) X((SEM_R>>3)) X((SEM_A>>3)) 136 X((SEM_R>>6)) X((SEM_A>>6)) XEND 137 }; 138 139 bool 140 sysdecode_semget_flags(FILE *fp, int flag, int *rem) 141 { 142 143 return (print_mask_int(fp, semgetflags, flag, rem)); 144 } 145 146 static struct name_table idtypes[] = { 147 X(P_PID) X(P_PPID) X(P_PGID) X(P_SID) X(P_CID) X(P_UID) X(P_GID) 148 X(P_ALL) X(P_LWPID) X(P_TASKID) X(P_PROJID) X(P_POOLID) X(P_JAILID) 149 X(P_CTID) X(P_CPUID) X(P_PSETID) XEND 150 }; 151 152 /* XXX: idtype is really an idtype_t */ 153 const char * 154 sysdecode_idtype(int idtype) 155 { 156 157 return (lookup_value(idtypes, idtype)); 158 } 159 160 /* 161 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a 162 * protocol-specific value. 163 */ 164 const char * 165 sysdecode_sockopt_level(int level) 166 { 167 const char *str; 168 169 if (level == SOL_SOCKET) 170 return ("SOL_SOCKET"); 171 172 /* SOL_* constants for Bluetooth sockets. */ 173 str = lookup_value(ngbtsolevel, level); 174 if (str != NULL) 175 return (str); 176 177 /* 178 * IP and Infiniband sockets use IP protocols as levels. Not all 179 * protocols are valid but it is simpler to just allow all of them. 180 * 181 * XXX: IPPROTO_IP == 0, but UNIX domain sockets use a level of 0 182 * for private options. 183 */ 184 str = sysdecode_ipproto(level); 185 if (str != NULL) 186 return (str); 187 188 return (NULL); 189 } 190 191 bool 192 sysdecode_vmprot(FILE *fp, int type, int *rem) 193 { 194 195 return (print_mask_int(fp, vmprot, type, rem)); 196 } 197 198 static struct name_table sockflags[] = { 199 X(SOCK_CLOEXEC) X(SOCK_CLOFORK) X(SOCK_NONBLOCK) XEND 200 }; 201 202 bool 203 sysdecode_socket_type(FILE *fp, int type, int *rem) 204 { 205 const char *str; 206 uintmax_t val; 207 bool printed; 208 209 str = lookup_value(socktype, 210 type & ~(SOCK_CLOEXEC | SOCK_CLOFORK | SOCK_NONBLOCK)); 211 if (str != NULL) { 212 fputs(str, fp); 213 *rem = 0; 214 printed = true; 215 } else { 216 *rem = type & ~(SOCK_CLOEXEC | SOCK_CLOFORK | SOCK_NONBLOCK); 217 printed = false; 218 } 219 val = type & (SOCK_CLOEXEC | SOCK_CLOFORK | SOCK_NONBLOCK); 220 print_mask_part(fp, sockflags, &val, &printed); 221 return (printed); 222 } 223 224 bool 225 sysdecode_access_mode(FILE *fp, int mode, int *rem) 226 { 227 228 return (print_mask_int(fp, accessmode, mode, rem)); 229 } 230 231 /* XXX: 'type' is really an acl_type_t. */ 232 const char * 233 sysdecode_acltype(int type) 234 { 235 236 return (lookup_value(acltype, type)); 237 } 238 239 bool 240 sysdecode_cap_fcntlrights(FILE *fp, uint32_t rights, uint32_t *rem) 241 { 242 243 return (print_mask_int(fp, capfcntl, rights, rem)); 244 } 245 246 bool 247 sysdecode_close_range_flags(FILE *fp, int flags, int *rem) 248 { 249 250 return (print_mask_int(fp, closerangeflags, flags, rem)); 251 } 252 253 const char * 254 sysdecode_extattrnamespace(int namespace) 255 { 256 257 return (lookup_value(extattrns, namespace)); 258 } 259 260 const char * 261 sysdecode_fadvice(int advice) 262 { 263 264 return (lookup_value(fadvisebehav, advice)); 265 } 266 267 bool 268 sysdecode_open_flags(FILE *fp, int flags, int *rem) 269 { 270 bool printed; 271 int mode; 272 uintmax_t val; 273 274 mode = flags & O_ACCMODE; 275 flags &= ~O_ACCMODE; 276 switch (mode) { 277 case O_RDONLY: 278 if (flags & O_EXEC) { 279 flags &= ~O_EXEC; 280 fputs("O_EXEC", fp); 281 } else 282 fputs("O_RDONLY", fp); 283 printed = true; 284 mode = 0; 285 break; 286 case O_WRONLY: 287 fputs("O_WRONLY", fp); 288 printed = true; 289 mode = 0; 290 break; 291 case O_RDWR: 292 fputs("O_RDWR", fp); 293 printed = true; 294 mode = 0; 295 break; 296 default: 297 printed = false; 298 } 299 val = (unsigned)flags; 300 print_mask_part(fp, openflags, &val, &printed); 301 if (rem != NULL) 302 *rem = val | mode; 303 return (printed); 304 } 305 306 bool 307 sysdecode_fcntl_fileflags(FILE *fp, int flags, int *rem) 308 { 309 bool printed; 310 int oflags; 311 312 /* 313 * The file flags used with F_GETFL/F_SETFL mostly match the 314 * flags passed to open(2). However, a few open-only flag 315 * bits have been repurposed for fcntl-only flags. 316 */ 317 oflags = flags & ~(O_NOFOLLOW | FRDAHEAD); 318 printed = sysdecode_open_flags(fp, oflags, rem); 319 if (flags & O_NOFOLLOW) { 320 fprintf(fp, "%sFPOIXSHM", printed ? "|" : ""); 321 printed = true; 322 } 323 if (flags & FRDAHEAD) { 324 fprintf(fp, "%sFRDAHEAD", printed ? "|" : ""); 325 printed = true; 326 } 327 return (printed); 328 } 329 330 bool 331 sysdecode_flock_operation(FILE *fp, int operation, int *rem) 332 { 333 334 return (print_mask_int(fp, flockops, operation, rem)); 335 } 336 337 static struct name_table getfsstatmode[] = { 338 X(MNT_WAIT) X(MNT_NOWAIT) XEND 339 }; 340 341 const char * 342 sysdecode_getfsstat_mode(int mode) 343 { 344 345 return (lookup_value(getfsstatmode, mode)); 346 } 347 348 const char * 349 sysdecode_getrusage_who(int who) 350 { 351 352 return (lookup_value(rusage, who)); 353 } 354 355 bool 356 sysdecode_inotifyflags(FILE *fp, int flag, int *rem) 357 { 358 359 return (print_mask_int(fp, inotifyflags, flag, rem)); 360 } 361 362 static struct name_table kevent_user_ffctrl[] = { 363 X(NOTE_FFNOP) X(NOTE_FFAND) X(NOTE_FFOR) X(NOTE_FFCOPY) 364 XEND 365 }; 366 367 static struct name_table kevent_rdwr_fflags[] = { 368 X(NOTE_LOWAT) X(NOTE_FILE_POLL) XEND 369 }; 370 371 static struct name_table kevent_vnode_fflags[] = { 372 X(NOTE_DELETE) X(NOTE_WRITE) X(NOTE_EXTEND) X(NOTE_ATTRIB) 373 X(NOTE_LINK) X(NOTE_RENAME) X(NOTE_REVOKE) X(NOTE_OPEN) X(NOTE_CLOSE) 374 X(NOTE_CLOSE_WRITE) X(NOTE_READ) XEND 375 }; 376 377 static struct name_table kevent_proc_fflags[] = { 378 X(NOTE_EXIT) X(NOTE_FORK) X(NOTE_EXEC) X(NOTE_TRACK) X(NOTE_TRACKERR) 379 X(NOTE_CHILD) XEND 380 }; 381 382 static struct name_table kevent_timer_fflags[] = { 383 X(NOTE_SECONDS) X(NOTE_MSECONDS) X(NOTE_USECONDS) X(NOTE_NSECONDS) 384 X(NOTE_ABSTIME) XEND 385 }; 386 387 void 388 sysdecode_kevent_fflags(FILE *fp, short filter, int fflags, int base) 389 { 390 int rem; 391 392 if (fflags == 0) { 393 fputs("0", fp); 394 return; 395 } 396 397 switch (filter) { 398 case EVFILT_READ: 399 case EVFILT_WRITE: 400 if (!print_mask_int(fp, kevent_rdwr_fflags, fflags, &rem)) 401 fprintf(fp, "%#x", rem); 402 else if (rem != 0) 403 fprintf(fp, "|%#x", rem); 404 break; 405 case EVFILT_VNODE: 406 if (!print_mask_int(fp, kevent_vnode_fflags, fflags, &rem)) 407 fprintf(fp, "%#x", rem); 408 else if (rem != 0) 409 fprintf(fp, "|%#x", rem); 410 break; 411 case EVFILT_PROC: 412 case EVFILT_PROCDESC: 413 if (!print_mask_int(fp, kevent_proc_fflags, fflags, &rem)) 414 fprintf(fp, "%#x", rem); 415 else if (rem != 0) 416 fprintf(fp, "|%#x", rem); 417 break; 418 case EVFILT_TIMER: 419 if (!print_mask_int(fp, kevent_timer_fflags, fflags, &rem)) 420 fprintf(fp, "%#x", rem); 421 else if (rem != 0) 422 fprintf(fp, "|%#x", rem); 423 break; 424 case EVFILT_USER: { 425 unsigned int ctrl, data; 426 427 ctrl = fflags & NOTE_FFCTRLMASK; 428 data = fflags & NOTE_FFLAGSMASK; 429 430 if (fflags & NOTE_TRIGGER) { 431 fputs("NOTE_TRIGGER", fp); 432 if (fflags == NOTE_TRIGGER) 433 return; 434 fputc('|', fp); 435 } 436 437 /* 438 * An event with 'ctrl' == NOTE_FFNOP is either a reported 439 * (output) event for which only 'data' should be output 440 * or a pointless input event. Assume that pointless 441 * input events don't occur in practice. An event with 442 * NOTE_TRIGGER is always an input event. 443 */ 444 if (ctrl != NOTE_FFNOP || fflags & NOTE_TRIGGER) { 445 fprintf(fp, "%s|%#x", 446 lookup_value(kevent_user_ffctrl, ctrl), data); 447 } else { 448 print_integer(fp, data, base); 449 } 450 break; 451 } 452 default: 453 print_integer(fp, fflags, base); 454 break; 455 } 456 } 457 458 bool 459 sysdecode_kevent_flags(FILE *fp, int flags, int *rem) 460 { 461 462 return (print_mask_int(fp, keventflags, flags, rem)); 463 } 464 465 const char * 466 sysdecode_kevent_filter(int filter) 467 { 468 469 return (lookup_value(keventfilters, filter)); 470 } 471 472 const char * 473 sysdecode_kldsym_cmd(int cmd) 474 { 475 476 return (lookup_value(kldsymcmd, cmd)); 477 } 478 479 const char * 480 sysdecode_kldunload_flags(int flags) 481 { 482 483 return (lookup_value(kldunloadfflags, flags)); 484 } 485 486 const char * 487 sysdecode_lio_listio_mode(int mode) 488 { 489 490 return (lookup_value(lio_listiomodes, mode)); 491 } 492 493 const char * 494 sysdecode_madvice(int advice) 495 { 496 497 return (lookup_value(madvisebehav, advice)); 498 } 499 500 const char * 501 sysdecode_minherit_inherit(int inherit) 502 { 503 504 return (lookup_value(minheritflags, inherit)); 505 } 506 507 bool 508 sysdecode_mlockall_flags(FILE *fp, int flags, int *rem) 509 { 510 511 return (print_mask_int(fp, mlockallflags, flags, rem)); 512 } 513 514 bool 515 sysdecode_mmap_prot(FILE *fp, int prot, int *rem) 516 { 517 int protm; 518 bool printed; 519 520 printed = false; 521 protm = PROT_MAX_EXTRACT(prot); 522 prot &= ~PROT_MAX(protm); 523 if (protm != 0) { 524 fputs("PROT_MAX(", fp); 525 printed = print_mask_int(fp, mmapprot, protm, rem); 526 fputs(")|", fp); 527 } 528 return (print_mask_int(fp, mmapprot, prot, rem) || printed); 529 } 530 531 bool 532 sysdecode_fileflags(FILE *fp, fflags_t flags, fflags_t *rem) 533 { 534 535 return (print_mask_0(fp, fileflags, flags, rem)); 536 } 537 538 bool 539 sysdecode_filemode(FILE *fp, int mode, int *rem) 540 { 541 542 return (print_mask_0(fp, filemode, mode, rem)); 543 } 544 545 bool 546 sysdecode_mount_flags(FILE *fp, int flags, int *rem) 547 { 548 549 return (print_mask_int(fp, mountflags, flags, rem)); 550 } 551 552 bool 553 sysdecode_msync_flags(FILE *fp, int flags, int *rem) 554 { 555 556 return (print_mask_int(fp, msyncflags, flags, rem)); 557 } 558 559 const char * 560 sysdecode_nfssvc_flags(int flags) 561 { 562 563 return (lookup_value(nfssvcflags, flags)); 564 } 565 566 static struct name_table pipe2flags[] = { 567 X(O_CLOEXEC) X(O_CLOFORK) X(O_NONBLOCK) XEND 568 }; 569 570 bool 571 sysdecode_pipe2_flags(FILE *fp, int flags, int *rem) 572 { 573 574 return (print_mask_0(fp, pipe2flags, flags, rem)); 575 } 576 577 bool 578 sysdecode_pollfd_events(FILE *fp, int flags, int *rem) 579 { 580 581 return (print_mask_int(fp, pollfdevents, flags, rem)); 582 } 583 584 const char * 585 sysdecode_prio_which(int which) 586 { 587 588 return (lookup_value(prio, which)); 589 } 590 591 const char * 592 sysdecode_procctl_cmd(int cmd) 593 { 594 595 return (lookup_value(procctlcmd, cmd)); 596 } 597 598 const char * 599 sysdecode_ptrace_request(int request) 600 { 601 602 return (lookup_value(ptraceop, request)); 603 } 604 605 static struct name_table quotatypes[] = { 606 X(GRPQUOTA) X(USRQUOTA) XEND 607 }; 608 609 bool 610 sysdecode_quotactl_cmd(FILE *fp, int cmd) 611 { 612 const char *primary, *type; 613 614 primary = lookup_value(quotactlcmds, cmd >> SUBCMDSHIFT); 615 if (primary == NULL) 616 return (false); 617 fprintf(fp, "QCMD(%s,", primary); 618 type = lookup_value(quotatypes, cmd & SUBCMDMASK); 619 if (type != NULL) 620 fprintf(fp, "%s", type); 621 else 622 fprintf(fp, "%#x", cmd & SUBCMDMASK); 623 fprintf(fp, ")"); 624 return (true); 625 } 626 627 bool 628 sysdecode_reboot_howto(FILE *fp, int howto, int *rem) 629 { 630 bool printed; 631 632 /* 633 * RB_AUTOBOOT is special in that its value is zero, but it is 634 * also an implied argument if a different operation is not 635 * requested via RB_HALT, RB_POWERCYCLE, RB_POWEROFF, or 636 * RB_REROOT. 637 */ 638 if (howto != 0 && (howto & (RB_HALT | RB_POWEROFF | RB_REROOT | 639 RB_POWERCYCLE)) == 0) { 640 fputs("RB_AUTOBOOT|", fp); 641 printed = true; 642 } else 643 printed = false; 644 return (print_mask_int(fp, rebootopt, howto, rem) || printed); 645 } 646 647 bool 648 sysdecode_rfork_flags(FILE *fp, int flags, int *rem) 649 { 650 651 return (print_mask_int(fp, rforkflags, flags, rem)); 652 } 653 654 const char * 655 sysdecode_rlimit(int resource) 656 { 657 658 return (lookup_value(rlimit, resource)); 659 } 660 661 const char * 662 sysdecode_scheduler_policy(int policy) 663 { 664 665 return (lookup_value(schedpolicy, policy)); 666 } 667 668 bool 669 sysdecode_sendfile_flags(FILE *fp, int flags, int *rem) 670 { 671 672 return (print_mask_int(fp, sendfileflags, flags, rem)); 673 } 674 675 bool 676 sysdecode_shmat_flags(FILE *fp, int flags, int *rem) 677 { 678 679 return (print_mask_int(fp, shmatflags, flags, rem)); 680 } 681 682 const char * 683 sysdecode_shutdown_how(int how) 684 { 685 686 return (lookup_value(shutdownhow, how)); 687 } 688 689 const char * 690 sysdecode_sigbus_code(int si_code) 691 { 692 693 return (lookup_value(sigbuscode, si_code)); 694 } 695 696 const char * 697 sysdecode_sigchld_code(int si_code) 698 { 699 700 return (lookup_value(sigchldcode, si_code)); 701 } 702 703 const char * 704 sysdecode_sigfpe_code(int si_code) 705 { 706 707 return (lookup_value(sigfpecode, si_code)); 708 } 709 710 const char * 711 sysdecode_sigill_code(int si_code) 712 { 713 714 return (lookup_value(sigillcode, si_code)); 715 } 716 717 const char * 718 sysdecode_sigsegv_code(int si_code) 719 { 720 721 return (lookup_value(sigsegvcode, si_code)); 722 } 723 724 const char * 725 sysdecode_sigtrap_code(int si_code) 726 { 727 728 return (lookup_value(sigtrapcode, si_code)); 729 } 730 731 const char * 732 sysdecode_sigprocmask_how(int how) 733 { 734 735 return (lookup_value(sigprocmaskhow, how)); 736 } 737 738 const char * 739 sysdecode_socketdomain(int domain) 740 { 741 742 return (lookup_value(sockdomain, domain)); 743 } 744 745 const char * 746 sysdecode_socket_protocol(int domain, int protocol) 747 { 748 749 switch (domain) { 750 case PF_INET: 751 case PF_INET6: 752 return (lookup_value(sockipproto, protocol)); 753 default: 754 return (NULL); 755 } 756 } 757 758 const char * 759 sysdecode_sockaddr_family(int sa_family) 760 { 761 762 return (lookup_value(sockfamily, sa_family)); 763 } 764 765 const char * 766 sysdecode_ipproto(int protocol) 767 { 768 769 return (lookup_value(sockipproto, protocol)); 770 } 771 772 const char * 773 sysdecode_sockopt_name(int level, int optname) 774 { 775 776 if (level == SOL_SOCKET) 777 return (lookup_value(sockopt, optname)); 778 if (level == IPPROTO_IP) 779 /* XXX: UNIX domain socket options use a level of 0 also. */ 780 return (lookup_value(sockoptip, optname)); 781 if (level == IPPROTO_IPV6) 782 return (lookup_value(sockoptipv6, optname)); 783 if (level == IPPROTO_SCTP) 784 return (lookup_value(sockoptsctp, optname)); 785 if (level == IPPROTO_TCP) 786 return (lookup_value(sockopttcp, optname)); 787 if (level == IPPROTO_UDP) 788 return (lookup_value(sockoptudp, optname)); 789 if (level == IPPROTO_UDPLITE) 790 return (lookup_value(sockoptudplite, optname)); 791 return (NULL); 792 } 793 794 bool 795 sysdecode_thr_create_flags(FILE *fp, int flags, int *rem) 796 { 797 798 return (print_mask_int(fp, thrcreateflags, flags, rem)); 799 } 800 801 const char * 802 sysdecode_umtx_op(int op) 803 { 804 805 return (lookup_value(umtxop, op)); 806 } 807 808 bool 809 sysdecode_umtx_op_flags(FILE *fp, int op, int *rem) 810 { 811 uintmax_t val; 812 bool printed; 813 814 printed = false; 815 val = (unsigned)op; 816 print_mask_part(fp, umtxopflags, &val, &printed); 817 if (rem != NULL) 818 *rem = val; 819 return (printed); 820 } 821 822 const char * 823 sysdecode_vmresult(int result) 824 { 825 826 return (lookup_value(vmresult, result)); 827 } 828 829 bool 830 sysdecode_wait4_options(FILE *fp, int options, int *rem) 831 { 832 bool printed; 833 int opt6; 834 835 /* A flags value of 0 is normal. */ 836 if (options == 0) { 837 fputs("0", fp); 838 if (rem != NULL) 839 *rem = 0; 840 return (true); 841 } 842 843 /* 844 * These flags are implicit and aren't valid flags for wait4() 845 * directly (though they don't fail with EINVAL). 846 */ 847 opt6 = options & (WEXITED | WTRAPPED); 848 options &= ~opt6; 849 printed = print_mask_int(fp, wait6opt, options, rem); 850 if (rem != NULL) 851 *rem |= opt6; 852 return (printed); 853 } 854 855 bool 856 sysdecode_wait6_options(FILE *fp, int options, int *rem) 857 { 858 859 return (print_mask_int(fp, wait6opt, options, rem)); 860 } 861 862 const char * 863 sysdecode_whence(int whence) 864 { 865 866 return (lookup_value(seekwhence, whence)); 867 } 868 869 const char * 870 sysdecode_fcntl_cmd(int cmd) 871 { 872 873 return (lookup_value(fcntlcmd, cmd)); 874 } 875 876 static struct name_table fcntl_fd_arg[] = { 877 X(FD_CLOEXEC) X(FD_CLOFORK) X(0) XEND 878 }; 879 880 bool 881 sysdecode_fcntl_arg_p(int cmd) 882 { 883 884 switch (cmd) { 885 case F_GETFD: 886 case F_GETFL: 887 case F_GETOWN: 888 return (false); 889 default: 890 return (true); 891 } 892 } 893 894 void 895 sysdecode_fcntl_arg(FILE *fp, int cmd, uintptr_t arg, int base) 896 { 897 int rem; 898 899 switch (cmd) { 900 case F_SETFD: 901 if (!print_value(fp, fcntl_fd_arg, arg)) 902 print_integer(fp, arg, base); 903 break; 904 case F_SETFL: 905 if (!sysdecode_fcntl_fileflags(fp, arg, &rem)) 906 fprintf(fp, "%#x", rem); 907 else if (rem != 0) 908 fprintf(fp, "|%#x", rem); 909 break; 910 case F_GETLK: 911 case F_SETLK: 912 case F_SETLKW: 913 fprintf(fp, "%p", (void *)arg); 914 break; 915 default: 916 print_integer(fp, arg, base); 917 break; 918 } 919 } 920 921 bool 922 sysdecode_mmap_flags(FILE *fp, int flags, int *rem) 923 { 924 uintmax_t val; 925 bool printed; 926 int align; 927 928 /* 929 * MAP_ALIGNED can't be handled directly by print_mask_int(). 930 */ 931 printed = false; 932 align = flags & MAP_ALIGNMENT_MASK; 933 val = (unsigned)flags & ~MAP_ALIGNMENT_MASK; 934 print_mask_part(fp, mmapflags, &val, &printed); 935 if (align != 0) { 936 if (printed) 937 fputc('|', fp); 938 if (align == MAP_ALIGNED_SUPER) 939 fputs("MAP_ALIGNED_SUPER", fp); 940 else 941 fprintf(fp, "MAP_ALIGNED(%d)", 942 align >> MAP_ALIGNMENT_SHIFT); 943 printed = true; 944 } 945 if (rem != NULL) 946 *rem = val; 947 return (printed); 948 } 949 950 const char * 951 sysdecode_pathconf_name(int name) 952 { 953 954 return (lookup_value(pathconfname, name)); 955 } 956 957 const char * 958 sysdecode_rtprio_function(int function) 959 { 960 961 return (lookup_value(rtpriofuncs, function)); 962 } 963 964 bool 965 sysdecode_msg_flags(FILE *fp, int flags, int *rem) 966 { 967 968 return (print_mask_0(fp, msgflags, flags, rem)); 969 } 970 971 const char * 972 sysdecode_sigcode(int sig, int si_code) 973 { 974 const char *str; 975 976 str = lookup_value(sigcode, si_code); 977 if (str != NULL) 978 return (str); 979 980 switch (sig) { 981 case SIGILL: 982 return (sysdecode_sigill_code(si_code)); 983 case SIGBUS: 984 return (sysdecode_sigbus_code(si_code)); 985 case SIGSEGV: 986 return (sysdecode_sigsegv_code(si_code)); 987 case SIGFPE: 988 return (sysdecode_sigfpe_code(si_code)); 989 case SIGTRAP: 990 return (sysdecode_sigtrap_code(si_code)); 991 case SIGCHLD: 992 return (sysdecode_sigchld_code(si_code)); 993 default: 994 return (NULL); 995 } 996 } 997 998 const char * 999 sysdecode_sysarch_number(int number) 1000 { 1001 1002 return (lookup_value(sysarchnum, number)); 1003 } 1004 1005 bool 1006 sysdecode_umtx_cvwait_flags(FILE *fp, u_long flags, u_long *rem) 1007 { 1008 1009 return (print_mask_0ul(fp, umtxcvwaitflags, flags, rem)); 1010 } 1011 1012 bool 1013 sysdecode_umtx_rwlock_flags(FILE *fp, u_long flags, u_long *rem) 1014 { 1015 1016 return (print_mask_0ul(fp, umtxrwlockflags, flags, rem)); 1017 } 1018 1019 void 1020 sysdecode_cap_rights(FILE *fp, cap_rights_t *rightsp) 1021 { 1022 cap_rights_t diff, sum, zero; 1023 const struct name_table *t; 1024 int i; 1025 bool comma; 1026 1027 for (i = 0; i < CAPARSIZE(rightsp); i++) { 1028 if (CAPIDXBIT(rightsp->cr_rights[i]) != 1 << i) { 1029 fprintf(fp, "invalid cap_rights_t"); 1030 return; 1031 } 1032 } 1033 cap_rights_init(&sum); 1034 diff = *rightsp; 1035 for (t = caprights, comma = false; t->str != NULL; t++) { 1036 if (cap_rights_is_set(rightsp, t->val)) { 1037 cap_rights_clear(&diff, t->val); 1038 if (cap_rights_is_set(&sum, t->val)) { 1039 /* Don't print redundant rights. */ 1040 continue; 1041 } 1042 cap_rights_set(&sum, t->val); 1043 1044 fprintf(fp, "%s%s", comma ? "," : "", t->str); 1045 comma = true; 1046 } 1047 } 1048 if (!comma) 1049 fprintf(fp, "CAP_NONE"); 1050 1051 /* 1052 * Provide a breadcrumb if some of the provided rights are not included 1053 * in the table, likely due to a bug in the mktables script. 1054 */ 1055 CAP_NONE(&zero); 1056 if (!cap_rights_contains(&zero, &diff)) 1057 fprintf(fp, ",unknown rights"); 1058 } 1059 1060 /* 1061 * Pre-sort the set of rights, which has a partial ordering defined by the 1062 * subset relation. This lets sysdecode_cap_rights() print a list of minimal 1063 * length with a single pass over the "caprights" table. 1064 */ 1065 static void __attribute__((constructor)) 1066 sysdecode_cap_rights_init(void) 1067 { 1068 cap_rights_t tr, qr; 1069 struct name_table *t, *q, tmp; 1070 bool swapped; 1071 1072 do { 1073 for (t = caprights, swapped = false; t->str != NULL; t++) { 1074 cap_rights_init(&tr, t->val); 1075 for (q = t + 1; q->str != NULL; q++) { 1076 cap_rights_init(&qr, q->val); 1077 if (cap_rights_contains(&qr, &tr)) { 1078 tmp = *t; 1079 *t = *q; 1080 *q = tmp; 1081 swapped = true; 1082 } 1083 } 1084 } 1085 } while (swapped); 1086 } 1087 1088 static struct name_table cmsgtypeip[] = { 1089 X(IP_RECVDSTADDR) X(IP_RECVTTL) X(IP_RECVOPTS) X(IP_RECVRETOPTS) 1090 X(IP_RECVIF) X(IP_RECVTOS) X(IP_FLOWID) X(IP_FLOWTYPE) 1091 X(IP_RSSBUCKETID) XEND 1092 }; 1093 1094 static struct name_table cmsgtypeipv6[] = { 1095 #if 0 1096 /* The RFC 2292 defines are kernel space only. */ 1097 X(IPV6_2292PKTINFO) X(IPV6_2292HOPLIMIT) X(IPV6_2292HOPOPTS) 1098 X(IPV6_2292DSTOPTS) X(IPV6_2292RTHDR) X(IPV6_2292NEXTHOP) 1099 #endif 1100 X(IPV6_PKTINFO) X(IPV6_HOPLIMIT) X(IPV6_HOPOPTS) 1101 X(IPV6_DSTOPTS) X(IPV6_RTHDR) X(IPV6_NEXTHOP) 1102 X(IPV6_TCLASS) X(IPV6_FLOWID) X(IPV6_FLOWTYPE) X(IPV6_RSSBUCKETID) 1103 X(IPV6_PATHMTU) X(IPV6_RTHDRDSTOPTS) X(IPV6_USE_MIN_MTU) 1104 X(IPV6_DONTFRAG) X(IPV6_PREFER_TEMPADDR) XEND 1105 }; 1106 1107 static struct name_table cmsgtypesctp[] = { 1108 X(SCTP_INIT) X(SCTP_SNDRCV) X(SCTP_EXTRCV) X(SCTP_SNDINFO) 1109 X(SCTP_RCVINFO) X(SCTP_NXTINFO) X(SCTP_PRINFO) X(SCTP_AUTHINFO) 1110 X(SCTP_DSTADDRV4) X(SCTP_DSTADDRV6) XEND 1111 }; 1112 1113 const char * 1114 sysdecode_cmsg_type(int cmsg_level, int cmsg_type) 1115 { 1116 1117 if (cmsg_level == SOL_SOCKET) 1118 return (lookup_value(cmsgtypesocket, cmsg_type)); 1119 if (cmsg_level == IPPROTO_IP) 1120 return (lookup_value(cmsgtypeip, cmsg_type)); 1121 if (cmsg_level == IPPROTO_IPV6) 1122 return (lookup_value(cmsgtypeipv6, cmsg_type)); 1123 if (cmsg_level == IPPROTO_SCTP) 1124 return (lookup_value(cmsgtypesctp, cmsg_type)); 1125 return (NULL); 1126 } 1127 1128 const char * 1129 sysdecode_sctp_pr_policy(int policy) 1130 { 1131 1132 return (lookup_value(sctpprpolicy, policy)); 1133 } 1134 1135 static struct name_table sctpsndflags[] = { 1136 X(SCTP_EOF) X(SCTP_ABORT) X(SCTP_UNORDERED) X(SCTP_ADDR_OVER) 1137 X(SCTP_SENDALL) X(SCTP_EOR) X(SCTP_SACK_IMMEDIATELY) XEND 1138 }; 1139 1140 bool 1141 sysdecode_sctp_snd_flags(FILE *fp, int flags, int *rem) 1142 { 1143 1144 return (print_mask_int(fp, sctpsndflags, flags, rem)); 1145 } 1146 1147 static struct name_table sctprcvflags[] = { 1148 X(SCTP_UNORDERED) XEND 1149 }; 1150 1151 bool 1152 sysdecode_sctp_rcv_flags(FILE *fp, int flags, int *rem) 1153 { 1154 1155 return (print_mask_int(fp, sctprcvflags, flags, rem)); 1156 } 1157 1158 static struct name_table sctpnxtflags[] = { 1159 X(SCTP_UNORDERED) X(SCTP_COMPLETE) X(SCTP_NOTIFICATION) XEND 1160 }; 1161 1162 bool 1163 sysdecode_sctp_nxt_flags(FILE *fp, int flags, int *rem) 1164 { 1165 1166 return (print_mask_int(fp, sctpnxtflags, flags, rem)); 1167 } 1168 1169 static struct name_table sctpsinfoflags[] = { 1170 X(SCTP_EOF) X(SCTP_ABORT) X(SCTP_UNORDERED) X(SCTP_ADDR_OVER) 1171 X(SCTP_SENDALL) X(SCTP_EOR) X(SCTP_SACK_IMMEDIATELY) XEND 1172 }; 1173 1174 void 1175 sysdecode_sctp_sinfo_flags(FILE *fp, int sinfo_flags) 1176 { 1177 const char *temp; 1178 int rem; 1179 bool printed; 1180 1181 printed = print_mask_0(fp, sctpsinfoflags, sinfo_flags, &rem); 1182 if (rem & ~SCTP_PR_SCTP_ALL) { 1183 fprintf(fp, "%s%#x", printed ? "|" : "", rem & ~SCTP_PR_SCTP_ALL); 1184 printed = true; 1185 rem &= ~SCTP_PR_SCTP_ALL; 1186 } 1187 if (rem != 0) { 1188 temp = sysdecode_sctp_pr_policy(rem); 1189 if (temp != NULL) { 1190 fprintf(fp, "%s%s", printed ? "|" : "", temp); 1191 } else { 1192 fprintf(fp, "%s%#x", printed ? "|" : "", rem); 1193 } 1194 } 1195 } 1196 1197 bool 1198 sysdecode_shmflags(FILE *fp, int flags, int *rem) 1199 { 1200 1201 return (print_mask_0(fp, shmflags, flags, rem)); 1202 } 1203 1204 const char * 1205 sysdecode_itimer(int which) 1206 { 1207 1208 return (lookup_value(itimerwhich, which)); 1209 } 1210