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