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