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