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_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, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)); 210 if (str != NULL) { 211 fputs(str, fp); 212 *rem = 0; 213 printed = true; 214 } else { 215 *rem = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK); 216 printed = false; 217 } 218 val = type & (SOCK_CLOEXEC | SOCK_NONBLOCK); 219 print_mask_part(fp, sockflags, &val, &printed); 220 return (printed); 221 } 222 223 bool 224 sysdecode_access_mode(FILE *fp, int mode, int *rem) 225 { 226 227 return (print_mask_int(fp, accessmode, mode, rem)); 228 } 229 230 /* XXX: 'type' is really an acl_type_t. */ 231 const char * 232 sysdecode_acltype(int type) 233 { 234 235 return (lookup_value(acltype, type)); 236 } 237 238 bool 239 sysdecode_cap_fcntlrights(FILE *fp, uint32_t rights, uint32_t *rem) 240 { 241 242 return (print_mask_int(fp, capfcntl, rights, rem)); 243 } 244 245 bool 246 sysdecode_close_range_flags(FILE *fp, int flags, int *rem) 247 { 248 249 return (print_mask_int(fp, closerangeflags, flags, rem)); 250 } 251 252 const char * 253 sysdecode_extattrnamespace(int namespace) 254 { 255 256 return (lookup_value(extattrns, namespace)); 257 } 258 259 const char * 260 sysdecode_fadvice(int advice) 261 { 262 263 return (lookup_value(fadvisebehav, advice)); 264 } 265 266 bool 267 sysdecode_open_flags(FILE *fp, int flags, int *rem) 268 { 269 bool printed; 270 int mode; 271 uintmax_t val; 272 273 mode = flags & O_ACCMODE; 274 flags &= ~O_ACCMODE; 275 switch (mode) { 276 case O_RDONLY: 277 if (flags & O_EXEC) { 278 flags &= ~O_EXEC; 279 fputs("O_EXEC", fp); 280 } else 281 fputs("O_RDONLY", fp); 282 printed = true; 283 mode = 0; 284 break; 285 case O_WRONLY: 286 fputs("O_WRONLY", fp); 287 printed = true; 288 mode = 0; 289 break; 290 case O_RDWR: 291 fputs("O_RDWR", fp); 292 printed = true; 293 mode = 0; 294 break; 295 default: 296 printed = false; 297 } 298 val = (unsigned)flags; 299 print_mask_part(fp, openflags, &val, &printed); 300 if (rem != NULL) 301 *rem = val | mode; 302 return (printed); 303 } 304 305 bool 306 sysdecode_fcntl_fileflags(FILE *fp, int flags, int *rem) 307 { 308 bool printed; 309 int oflags; 310 311 /* 312 * The file flags used with F_GETFL/F_SETFL mostly match the 313 * flags passed to open(2). However, a few open-only flag 314 * bits have been repurposed for fcntl-only flags. 315 */ 316 oflags = flags & ~(O_NOFOLLOW | FRDAHEAD); 317 printed = sysdecode_open_flags(fp, oflags, rem); 318 if (flags & O_NOFOLLOW) { 319 fprintf(fp, "%sFPOIXSHM", printed ? "|" : ""); 320 printed = true; 321 } 322 if (flags & FRDAHEAD) { 323 fprintf(fp, "%sFRDAHEAD", printed ? "|" : ""); 324 printed = true; 325 } 326 return (printed); 327 } 328 329 bool 330 sysdecode_flock_operation(FILE *fp, int operation, int *rem) 331 { 332 333 return (print_mask_int(fp, flockops, operation, rem)); 334 } 335 336 static struct name_table getfsstatmode[] = { 337 X(MNT_WAIT) X(MNT_NOWAIT) XEND 338 }; 339 340 const char * 341 sysdecode_getfsstat_mode(int mode) 342 { 343 344 return (lookup_value(getfsstatmode, mode)); 345 } 346 347 const char * 348 sysdecode_getrusage_who(int who) 349 { 350 351 return (lookup_value(rusage, who)); 352 } 353 354 bool 355 sysdecode_inotifyflags(FILE *fp, int flag, int *rem) 356 { 357 358 return (print_mask_int(fp, inotifyflags, flag, rem)); 359 } 360 361 static struct name_table kevent_user_ffctrl[] = { 362 X(NOTE_FFNOP) X(NOTE_FFAND) X(NOTE_FFOR) X(NOTE_FFCOPY) 363 XEND 364 }; 365 366 static struct name_table kevent_rdwr_fflags[] = { 367 X(NOTE_LOWAT) X(NOTE_FILE_POLL) XEND 368 }; 369 370 static struct name_table kevent_vnode_fflags[] = { 371 X(NOTE_DELETE) X(NOTE_WRITE) X(NOTE_EXTEND) X(NOTE_ATTRIB) 372 X(NOTE_LINK) X(NOTE_RENAME) X(NOTE_REVOKE) X(NOTE_OPEN) X(NOTE_CLOSE) 373 X(NOTE_CLOSE_WRITE) X(NOTE_READ) XEND 374 }; 375 376 static struct name_table kevent_proc_fflags[] = { 377 X(NOTE_EXIT) X(NOTE_FORK) X(NOTE_EXEC) X(NOTE_TRACK) X(NOTE_TRACKERR) 378 X(NOTE_CHILD) XEND 379 }; 380 381 static struct name_table kevent_timer_fflags[] = { 382 X(NOTE_SECONDS) X(NOTE_MSECONDS) X(NOTE_USECONDS) X(NOTE_NSECONDS) 383 X(NOTE_ABSTIME) XEND 384 }; 385 386 void 387 sysdecode_kevent_fflags(FILE *fp, short filter, int fflags, int base) 388 { 389 int rem; 390 391 if (fflags == 0) { 392 fputs("0", fp); 393 return; 394 } 395 396 switch (filter) { 397 case EVFILT_READ: 398 case EVFILT_WRITE: 399 if (!print_mask_int(fp, kevent_rdwr_fflags, fflags, &rem)) 400 fprintf(fp, "%#x", rem); 401 else if (rem != 0) 402 fprintf(fp, "|%#x", rem); 403 break; 404 case EVFILT_VNODE: 405 if (!print_mask_int(fp, kevent_vnode_fflags, fflags, &rem)) 406 fprintf(fp, "%#x", rem); 407 else if (rem != 0) 408 fprintf(fp, "|%#x", rem); 409 break; 410 case EVFILT_PROC: 411 case EVFILT_PROCDESC: 412 if (!print_mask_int(fp, kevent_proc_fflags, fflags, &rem)) 413 fprintf(fp, "%#x", rem); 414 else if (rem != 0) 415 fprintf(fp, "|%#x", rem); 416 break; 417 case EVFILT_TIMER: 418 if (!print_mask_int(fp, kevent_timer_fflags, fflags, &rem)) 419 fprintf(fp, "%#x", rem); 420 else if (rem != 0) 421 fprintf(fp, "|%#x", rem); 422 break; 423 case EVFILT_USER: { 424 unsigned int ctrl, data; 425 426 ctrl = fflags & NOTE_FFCTRLMASK; 427 data = fflags & NOTE_FFLAGSMASK; 428 429 if (fflags & NOTE_TRIGGER) { 430 fputs("NOTE_TRIGGER", fp); 431 if (fflags == NOTE_TRIGGER) 432 return; 433 fputc('|', fp); 434 } 435 436 /* 437 * An event with 'ctrl' == NOTE_FFNOP is either a reported 438 * (output) event for which only 'data' should be output 439 * or a pointless input event. Assume that pointless 440 * input events don't occur in practice. An event with 441 * NOTE_TRIGGER is always an input event. 442 */ 443 if (ctrl != NOTE_FFNOP || fflags & NOTE_TRIGGER) { 444 fprintf(fp, "%s|%#x", 445 lookup_value(kevent_user_ffctrl, ctrl), data); 446 } else { 447 print_integer(fp, data, base); 448 } 449 break; 450 } 451 default: 452 print_integer(fp, fflags, base); 453 break; 454 } 455 } 456 457 bool 458 sysdecode_kevent_flags(FILE *fp, int flags, int *rem) 459 { 460 461 return (print_mask_int(fp, keventflags, flags, rem)); 462 } 463 464 const char * 465 sysdecode_kevent_filter(int filter) 466 { 467 468 return (lookup_value(keventfilters, filter)); 469 } 470 471 const char * 472 sysdecode_kldsym_cmd(int cmd) 473 { 474 475 return (lookup_value(kldsymcmd, cmd)); 476 } 477 478 const char * 479 sysdecode_kldunload_flags(int flags) 480 { 481 482 return (lookup_value(kldunloadfflags, flags)); 483 } 484 485 const char * 486 sysdecode_lio_listio_mode(int mode) 487 { 488 489 return (lookup_value(lio_listiomodes, mode)); 490 } 491 492 const char * 493 sysdecode_madvice(int advice) 494 { 495 496 return (lookup_value(madvisebehav, advice)); 497 } 498 499 const char * 500 sysdecode_minherit_inherit(int inherit) 501 { 502 503 return (lookup_value(minheritflags, inherit)); 504 } 505 506 bool 507 sysdecode_mlockall_flags(FILE *fp, int flags, int *rem) 508 { 509 510 return (print_mask_int(fp, mlockallflags, flags, rem)); 511 } 512 513 bool 514 sysdecode_mmap_prot(FILE *fp, int prot, int *rem) 515 { 516 int protm; 517 bool printed; 518 519 printed = false; 520 protm = PROT_MAX_EXTRACT(prot); 521 prot &= ~PROT_MAX(protm); 522 if (protm != 0) { 523 fputs("PROT_MAX(", fp); 524 printed = print_mask_int(fp, mmapprot, protm, rem); 525 fputs(")|", fp); 526 } 527 return (print_mask_int(fp, mmapprot, prot, rem) || printed); 528 } 529 530 bool 531 sysdecode_fileflags(FILE *fp, fflags_t flags, fflags_t *rem) 532 { 533 534 return (print_mask_0(fp, fileflags, flags, rem)); 535 } 536 537 bool 538 sysdecode_filemode(FILE *fp, int mode, int *rem) 539 { 540 541 return (print_mask_0(fp, filemode, mode, rem)); 542 } 543 544 bool 545 sysdecode_mount_flags(FILE *fp, int flags, int *rem) 546 { 547 548 return (print_mask_int(fp, mountflags, flags, rem)); 549 } 550 551 bool 552 sysdecode_msync_flags(FILE *fp, int flags, int *rem) 553 { 554 555 return (print_mask_int(fp, msyncflags, flags, rem)); 556 } 557 558 const char * 559 sysdecode_nfssvc_flags(int flags) 560 { 561 562 return (lookup_value(nfssvcflags, flags)); 563 } 564 565 static struct name_table pipe2flags[] = { 566 X(O_CLOEXEC) X(O_NONBLOCK) XEND 567 }; 568 569 bool 570 sysdecode_pipe2_flags(FILE *fp, int flags, int *rem) 571 { 572 573 return (print_mask_0(fp, pipe2flags, flags, rem)); 574 } 575 576 bool 577 sysdecode_pollfd_events(FILE *fp, int flags, int *rem) 578 { 579 580 return (print_mask_int(fp, pollfdevents, flags, rem)); 581 } 582 583 const char * 584 sysdecode_prio_which(int which) 585 { 586 587 return (lookup_value(prio, which)); 588 } 589 590 const char * 591 sysdecode_procctl_cmd(int cmd) 592 { 593 594 return (lookup_value(procctlcmd, cmd)); 595 } 596 597 const char * 598 sysdecode_ptrace_request(int request) 599 { 600 601 return (lookup_value(ptraceop, request)); 602 } 603 604 static struct name_table quotatypes[] = { 605 X(GRPQUOTA) X(USRQUOTA) XEND 606 }; 607 608 bool 609 sysdecode_quotactl_cmd(FILE *fp, int cmd) 610 { 611 const char *primary, *type; 612 613 primary = lookup_value(quotactlcmds, cmd >> SUBCMDSHIFT); 614 if (primary == NULL) 615 return (false); 616 fprintf(fp, "QCMD(%s,", primary); 617 type = lookup_value(quotatypes, cmd & SUBCMDMASK); 618 if (type != NULL) 619 fprintf(fp, "%s", type); 620 else 621 fprintf(fp, "%#x", cmd & SUBCMDMASK); 622 fprintf(fp, ")"); 623 return (true); 624 } 625 626 bool 627 sysdecode_reboot_howto(FILE *fp, int howto, int *rem) 628 { 629 bool printed; 630 631 /* 632 * RB_AUTOBOOT is special in that its value is zero, but it is 633 * also an implied argument if a different operation is not 634 * requested via RB_HALT, RB_POWERCYCLE, RB_POWEROFF, or 635 * RB_REROOT. 636 */ 637 if (howto != 0 && (howto & (RB_HALT | RB_POWEROFF | RB_REROOT | 638 RB_POWERCYCLE)) == 0) { 639 fputs("RB_AUTOBOOT|", fp); 640 printed = true; 641 } else 642 printed = false; 643 return (print_mask_int(fp, rebootopt, howto, rem) || printed); 644 } 645 646 bool 647 sysdecode_rfork_flags(FILE *fp, int flags, int *rem) 648 { 649 650 return (print_mask_int(fp, rforkflags, flags, rem)); 651 } 652 653 const char * 654 sysdecode_rlimit(int resource) 655 { 656 657 return (lookup_value(rlimit, resource)); 658 } 659 660 const char * 661 sysdecode_scheduler_policy(int policy) 662 { 663 664 return (lookup_value(schedpolicy, policy)); 665 } 666 667 bool 668 sysdecode_sendfile_flags(FILE *fp, int flags, int *rem) 669 { 670 671 return (print_mask_int(fp, sendfileflags, flags, rem)); 672 } 673 674 bool 675 sysdecode_shmat_flags(FILE *fp, int flags, int *rem) 676 { 677 678 return (print_mask_int(fp, shmatflags, flags, rem)); 679 } 680 681 const char * 682 sysdecode_shutdown_how(int how) 683 { 684 685 return (lookup_value(shutdownhow, how)); 686 } 687 688 const char * 689 sysdecode_sigbus_code(int si_code) 690 { 691 692 return (lookup_value(sigbuscode, si_code)); 693 } 694 695 const char * 696 sysdecode_sigchld_code(int si_code) 697 { 698 699 return (lookup_value(sigchldcode, si_code)); 700 } 701 702 const char * 703 sysdecode_sigfpe_code(int si_code) 704 { 705 706 return (lookup_value(sigfpecode, si_code)); 707 } 708 709 const char * 710 sysdecode_sigill_code(int si_code) 711 { 712 713 return (lookup_value(sigillcode, si_code)); 714 } 715 716 const char * 717 sysdecode_sigsegv_code(int si_code) 718 { 719 720 return (lookup_value(sigsegvcode, si_code)); 721 } 722 723 const char * 724 sysdecode_sigtrap_code(int si_code) 725 { 726 727 return (lookup_value(sigtrapcode, si_code)); 728 } 729 730 const char * 731 sysdecode_sigprocmask_how(int how) 732 { 733 734 return (lookup_value(sigprocmaskhow, how)); 735 } 736 737 const char * 738 sysdecode_socketdomain(int domain) 739 { 740 741 return (lookup_value(sockdomain, domain)); 742 } 743 744 const char * 745 sysdecode_socket_protocol(int domain, int protocol) 746 { 747 748 switch (domain) { 749 case PF_INET: 750 case PF_INET6: 751 return (lookup_value(sockipproto, protocol)); 752 default: 753 return (NULL); 754 } 755 } 756 757 const char * 758 sysdecode_sockaddr_family(int sa_family) 759 { 760 761 return (lookup_value(sockfamily, sa_family)); 762 } 763 764 const char * 765 sysdecode_ipproto(int protocol) 766 { 767 768 return (lookup_value(sockipproto, protocol)); 769 } 770 771 const char * 772 sysdecode_sockopt_name(int level, int optname) 773 { 774 775 if (level == SOL_SOCKET) 776 return (lookup_value(sockopt, optname)); 777 if (level == IPPROTO_IP) 778 /* XXX: UNIX domain socket options use a level of 0 also. */ 779 return (lookup_value(sockoptip, optname)); 780 if (level == IPPROTO_IPV6) 781 return (lookup_value(sockoptipv6, optname)); 782 if (level == IPPROTO_SCTP) 783 return (lookup_value(sockoptsctp, optname)); 784 if (level == IPPROTO_TCP) 785 return (lookup_value(sockopttcp, optname)); 786 if (level == IPPROTO_UDP) 787 return (lookup_value(sockoptudp, optname)); 788 if (level == IPPROTO_UDPLITE) 789 return (lookup_value(sockoptudplite, optname)); 790 return (NULL); 791 } 792 793 bool 794 sysdecode_thr_create_flags(FILE *fp, int flags, int *rem) 795 { 796 797 return (print_mask_int(fp, thrcreateflags, flags, rem)); 798 } 799 800 const char * 801 sysdecode_umtx_op(int op) 802 { 803 804 return (lookup_value(umtxop, op)); 805 } 806 807 bool 808 sysdecode_umtx_op_flags(FILE *fp, int op, int *rem) 809 { 810 uintmax_t val; 811 bool printed; 812 813 printed = false; 814 val = (unsigned)op; 815 print_mask_part(fp, umtxopflags, &val, &printed); 816 if (rem != NULL) 817 *rem = val; 818 return (printed); 819 } 820 821 const char * 822 sysdecode_vmresult(int result) 823 { 824 825 return (lookup_value(vmresult, result)); 826 } 827 828 bool 829 sysdecode_wait4_options(FILE *fp, int options, int *rem) 830 { 831 bool printed; 832 int opt6; 833 834 /* A flags value of 0 is normal. */ 835 if (options == 0) { 836 fputs("0", fp); 837 if (rem != NULL) 838 *rem = 0; 839 return (true); 840 } 841 842 /* 843 * These flags are implicit and aren't valid flags for wait4() 844 * directly (though they don't fail with EINVAL). 845 */ 846 opt6 = options & (WEXITED | WTRAPPED); 847 options &= ~opt6; 848 printed = print_mask_int(fp, wait6opt, options, rem); 849 if (rem != NULL) 850 *rem |= opt6; 851 return (printed); 852 } 853 854 bool 855 sysdecode_wait6_options(FILE *fp, int options, int *rem) 856 { 857 858 return (print_mask_int(fp, wait6opt, options, rem)); 859 } 860 861 const char * 862 sysdecode_whence(int whence) 863 { 864 865 return (lookup_value(seekwhence, whence)); 866 } 867 868 const char * 869 sysdecode_fcntl_cmd(int cmd) 870 { 871 872 return (lookup_value(fcntlcmd, cmd)); 873 } 874 875 static struct name_table fcntl_fd_arg[] = { 876 X(FD_CLOEXEC) X(0) XEND 877 }; 878 879 bool 880 sysdecode_fcntl_arg_p(int cmd) 881 { 882 883 switch (cmd) { 884 case F_GETFD: 885 case F_GETFL: 886 case F_GETOWN: 887 return (false); 888 default: 889 return (true); 890 } 891 } 892 893 void 894 sysdecode_fcntl_arg(FILE *fp, int cmd, uintptr_t arg, int base) 895 { 896 int rem; 897 898 switch (cmd) { 899 case F_SETFD: 900 if (!print_value(fp, fcntl_fd_arg, arg)) 901 print_integer(fp, arg, base); 902 break; 903 case F_SETFL: 904 if (!sysdecode_fcntl_fileflags(fp, arg, &rem)) 905 fprintf(fp, "%#x", rem); 906 else if (rem != 0) 907 fprintf(fp, "|%#x", rem); 908 break; 909 case F_GETLK: 910 case F_SETLK: 911 case F_SETLKW: 912 fprintf(fp, "%p", (void *)arg); 913 break; 914 default: 915 print_integer(fp, arg, base); 916 break; 917 } 918 } 919 920 bool 921 sysdecode_mmap_flags(FILE *fp, int flags, int *rem) 922 { 923 uintmax_t val; 924 bool printed; 925 int align; 926 927 /* 928 * MAP_ALIGNED can't be handled directly by print_mask_int(). 929 */ 930 printed = false; 931 align = flags & MAP_ALIGNMENT_MASK; 932 val = (unsigned)flags & ~MAP_ALIGNMENT_MASK; 933 print_mask_part(fp, mmapflags, &val, &printed); 934 if (align != 0) { 935 if (printed) 936 fputc('|', fp); 937 if (align == MAP_ALIGNED_SUPER) 938 fputs("MAP_ALIGNED_SUPER", fp); 939 else 940 fprintf(fp, "MAP_ALIGNED(%d)", 941 align >> MAP_ALIGNMENT_SHIFT); 942 printed = true; 943 } 944 if (rem != NULL) 945 *rem = val; 946 return (printed); 947 } 948 949 const char * 950 sysdecode_pathconf_name(int name) 951 { 952 953 return (lookup_value(pathconfname, name)); 954 } 955 956 const char * 957 sysdecode_rtprio_function(int function) 958 { 959 960 return (lookup_value(rtpriofuncs, function)); 961 } 962 963 bool 964 sysdecode_msg_flags(FILE *fp, int flags, int *rem) 965 { 966 967 return (print_mask_0(fp, msgflags, flags, rem)); 968 } 969 970 const char * 971 sysdecode_sigcode(int sig, int si_code) 972 { 973 const char *str; 974 975 str = lookup_value(sigcode, si_code); 976 if (str != NULL) 977 return (str); 978 979 switch (sig) { 980 case SIGILL: 981 return (sysdecode_sigill_code(si_code)); 982 case SIGBUS: 983 return (sysdecode_sigbus_code(si_code)); 984 case SIGSEGV: 985 return (sysdecode_sigsegv_code(si_code)); 986 case SIGFPE: 987 return (sysdecode_sigfpe_code(si_code)); 988 case SIGTRAP: 989 return (sysdecode_sigtrap_code(si_code)); 990 case SIGCHLD: 991 return (sysdecode_sigchld_code(si_code)); 992 default: 993 return (NULL); 994 } 995 } 996 997 const char * 998 sysdecode_sysarch_number(int number) 999 { 1000 1001 return (lookup_value(sysarchnum, number)); 1002 } 1003 1004 bool 1005 sysdecode_umtx_cvwait_flags(FILE *fp, u_long flags, u_long *rem) 1006 { 1007 1008 return (print_mask_0ul(fp, umtxcvwaitflags, flags, rem)); 1009 } 1010 1011 bool 1012 sysdecode_umtx_rwlock_flags(FILE *fp, u_long flags, u_long *rem) 1013 { 1014 1015 return (print_mask_0ul(fp, umtxrwlockflags, flags, rem)); 1016 } 1017 1018 void 1019 sysdecode_cap_rights(FILE *fp, cap_rights_t *rightsp) 1020 { 1021 cap_rights_t diff, sum, zero; 1022 const struct name_table *t; 1023 int i; 1024 bool comma; 1025 1026 for (i = 0; i < CAPARSIZE(rightsp); i++) { 1027 if (CAPIDXBIT(rightsp->cr_rights[i]) != 1 << i) { 1028 fprintf(fp, "invalid cap_rights_t"); 1029 return; 1030 } 1031 } 1032 cap_rights_init(&sum); 1033 diff = *rightsp; 1034 for (t = caprights, comma = false; t->str != NULL; t++) { 1035 if (cap_rights_is_set(rightsp, t->val)) { 1036 cap_rights_clear(&diff, t->val); 1037 if (cap_rights_is_set(&sum, t->val)) { 1038 /* Don't print redundant rights. */ 1039 continue; 1040 } 1041 cap_rights_set(&sum, t->val); 1042 1043 fprintf(fp, "%s%s", comma ? "," : "", t->str); 1044 comma = true; 1045 } 1046 } 1047 if (!comma) 1048 fprintf(fp, "CAP_NONE"); 1049 1050 /* 1051 * Provide a breadcrumb if some of the provided rights are not included 1052 * in the table, likely due to a bug in the mktables script. 1053 */ 1054 CAP_NONE(&zero); 1055 if (!cap_rights_contains(&zero, &diff)) 1056 fprintf(fp, ",unknown rights"); 1057 } 1058 1059 /* 1060 * Pre-sort the set of rights, which has a partial ordering defined by the 1061 * subset relation. This lets sysdecode_cap_rights() print a list of minimal 1062 * length with a single pass over the "caprights" table. 1063 */ 1064 static void __attribute__((constructor)) 1065 sysdecode_cap_rights_init(void) 1066 { 1067 cap_rights_t tr, qr; 1068 struct name_table *t, *q, tmp; 1069 bool swapped; 1070 1071 do { 1072 for (t = caprights, swapped = false; t->str != NULL; t++) { 1073 cap_rights_init(&tr, t->val); 1074 for (q = t + 1; q->str != NULL; q++) { 1075 cap_rights_init(&qr, q->val); 1076 if (cap_rights_contains(&qr, &tr)) { 1077 tmp = *t; 1078 *t = *q; 1079 *q = tmp; 1080 swapped = true; 1081 } 1082 } 1083 } 1084 } while (swapped); 1085 } 1086 1087 static struct name_table cmsgtypeip[] = { 1088 X(IP_RECVDSTADDR) X(IP_RECVTTL) X(IP_RECVOPTS) X(IP_RECVRETOPTS) 1089 X(IP_RECVIF) X(IP_RECVTOS) X(IP_FLOWID) X(IP_FLOWTYPE) 1090 X(IP_RSSBUCKETID) XEND 1091 }; 1092 1093 static struct name_table cmsgtypeipv6[] = { 1094 #if 0 1095 /* The RFC 2292 defines are kernel space only. */ 1096 X(IPV6_2292PKTINFO) X(IPV6_2292HOPLIMIT) X(IPV6_2292HOPOPTS) 1097 X(IPV6_2292DSTOPTS) X(IPV6_2292RTHDR) X(IPV6_2292NEXTHOP) 1098 #endif 1099 X(IPV6_PKTINFO) X(IPV6_HOPLIMIT) X(IPV6_HOPOPTS) 1100 X(IPV6_DSTOPTS) X(IPV6_RTHDR) X(IPV6_NEXTHOP) 1101 X(IPV6_TCLASS) X(IPV6_FLOWID) X(IPV6_FLOWTYPE) X(IPV6_RSSBUCKETID) 1102 X(IPV6_PATHMTU) X(IPV6_RTHDRDSTOPTS) X(IPV6_USE_MIN_MTU) 1103 X(IPV6_DONTFRAG) X(IPV6_PREFER_TEMPADDR) XEND 1104 }; 1105 1106 static struct name_table cmsgtypesctp[] = { 1107 X(SCTP_INIT) X(SCTP_SNDRCV) X(SCTP_EXTRCV) X(SCTP_SNDINFO) 1108 X(SCTP_RCVINFO) X(SCTP_NXTINFO) X(SCTP_PRINFO) X(SCTP_AUTHINFO) 1109 X(SCTP_DSTADDRV4) X(SCTP_DSTADDRV6) XEND 1110 }; 1111 1112 const char * 1113 sysdecode_cmsg_type(int cmsg_level, int cmsg_type) 1114 { 1115 1116 if (cmsg_level == SOL_SOCKET) 1117 return (lookup_value(cmsgtypesocket, cmsg_type)); 1118 if (cmsg_level == IPPROTO_IP) 1119 return (lookup_value(cmsgtypeip, cmsg_type)); 1120 if (cmsg_level == IPPROTO_IPV6) 1121 return (lookup_value(cmsgtypeipv6, cmsg_type)); 1122 if (cmsg_level == IPPROTO_SCTP) 1123 return (lookup_value(cmsgtypesctp, cmsg_type)); 1124 return (NULL); 1125 } 1126 1127 const char * 1128 sysdecode_sctp_pr_policy(int policy) 1129 { 1130 1131 return (lookup_value(sctpprpolicy, policy)); 1132 } 1133 1134 static struct name_table sctpsndflags[] = { 1135 X(SCTP_EOF) X(SCTP_ABORT) X(SCTP_UNORDERED) X(SCTP_ADDR_OVER) 1136 X(SCTP_SENDALL) X(SCTP_EOR) X(SCTP_SACK_IMMEDIATELY) XEND 1137 }; 1138 1139 bool 1140 sysdecode_sctp_snd_flags(FILE *fp, int flags, int *rem) 1141 { 1142 1143 return (print_mask_int(fp, sctpsndflags, flags, rem)); 1144 } 1145 1146 static struct name_table sctprcvflags[] = { 1147 X(SCTP_UNORDERED) XEND 1148 }; 1149 1150 bool 1151 sysdecode_sctp_rcv_flags(FILE *fp, int flags, int *rem) 1152 { 1153 1154 return (print_mask_int(fp, sctprcvflags, flags, rem)); 1155 } 1156 1157 static struct name_table sctpnxtflags[] = { 1158 X(SCTP_UNORDERED) X(SCTP_COMPLETE) X(SCTP_NOTIFICATION) XEND 1159 }; 1160 1161 bool 1162 sysdecode_sctp_nxt_flags(FILE *fp, int flags, int *rem) 1163 { 1164 1165 return (print_mask_int(fp, sctpnxtflags, flags, rem)); 1166 } 1167 1168 static struct name_table sctpsinfoflags[] = { 1169 X(SCTP_EOF) X(SCTP_ABORT) X(SCTP_UNORDERED) X(SCTP_ADDR_OVER) 1170 X(SCTP_SENDALL) X(SCTP_EOR) X(SCTP_SACK_IMMEDIATELY) XEND 1171 }; 1172 1173 void 1174 sysdecode_sctp_sinfo_flags(FILE *fp, int sinfo_flags) 1175 { 1176 const char *temp; 1177 int rem; 1178 bool printed; 1179 1180 printed = print_mask_0(fp, sctpsinfoflags, sinfo_flags, &rem); 1181 if (rem & ~SCTP_PR_SCTP_ALL) { 1182 fprintf(fp, "%s%#x", printed ? "|" : "", rem & ~SCTP_PR_SCTP_ALL); 1183 printed = true; 1184 rem &= ~SCTP_PR_SCTP_ALL; 1185 } 1186 if (rem != 0) { 1187 temp = sysdecode_sctp_pr_policy(rem); 1188 if (temp != NULL) { 1189 fprintf(fp, "%s%s", printed ? "|" : "", temp); 1190 } else { 1191 fprintf(fp, "%s%#x", printed ? "|" : "", rem); 1192 } 1193 } 1194 } 1195 1196 bool 1197 sysdecode_shmflags(FILE *fp, int flags, int *rem) 1198 { 1199 1200 return (print_mask_0(fp, shmflags, flags, rem)); 1201 } 1202 1203 const char * 1204 sysdecode_itimer(int which) 1205 { 1206 1207 return (lookup_value(itimerwhich, which)); 1208 } 1209