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