xref: /freebsd/lib/libsysdecode/flags.c (revision e17f5b1d307b7b8910d67883e57a9604305906d5)
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  */
78 struct 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 
112 static const char *
113 lookup_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  */
128 static void
129 print_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  */
159 static bool
160 print_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  */
176 static bool
177 print_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  */
192 static bool
193 print_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 
213 static void
214 print_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 
233 static bool
234 print_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 
246 const char *
247 sysdecode_atfd(int fd)
248 {
249 
250 	if (fd == AT_FDCWD)
251 		return ("AT_FDCWD");
252 	return (NULL);
253 }
254 
255 bool
256 sysdecode_atflags(FILE *fp, int flag, int *rem)
257 {
258 
259 	return (print_mask_int(fp, atflags, flag, rem));
260 }
261 
262 static 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 
267 const char *
268 sysdecode_semctl_cmd(int cmd)
269 {
270 
271 	return (lookup_value(semctlops, cmd));
272 }
273 
274 static struct name_table shmctlops[] = {
275 	X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND
276 };
277 
278 const char *
279 sysdecode_shmctl_cmd(int cmd)
280 {
281 
282 	return (lookup_value(shmctlops, cmd));
283 }
284 
285 const char *
286 sysdecode_msgctl_cmd(int cmd)
287 {
288 
289 	return (sysdecode_shmctl_cmd(cmd));
290 }
291 
292 static 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 
297 bool
298 sysdecode_semget_flags(FILE *fp, int flag, int *rem)
299 {
300 
301 	return (print_mask_int(fp, semgetflags, flag, rem));
302 }
303 
304 static 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 */
311 const char *
312 sysdecode_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  */
322 const char *
323 sysdecode_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 
349 bool
350 sysdecode_vmprot(FILE *fp, int type, int *rem)
351 {
352 
353 	return (print_mask_int(fp, vmprot, type, rem));
354 }
355 
356 static struct name_table sockflags[] = {
357 	X(SOCK_CLOEXEC) X(SOCK_NONBLOCK) XEND
358 };
359 
360 bool
361 sysdecode_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 
381 bool
382 sysdecode_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. */
389 const char *
390 sysdecode_acltype(int type)
391 {
392 
393 	return (lookup_value(acltype, type));
394 }
395 
396 bool
397 sysdecode_cap_fcntlrights(FILE *fp, uint32_t rights, uint32_t *rem)
398 {
399 
400 	return (print_mask_int(fp, capfcntl, rights, rem));
401 }
402 
403 const char *
404 sysdecode_extattrnamespace(int namespace)
405 {
406 
407 	return (lookup_value(extattrns, namespace));
408 }
409 
410 const char *
411 sysdecode_fadvice(int advice)
412 {
413 
414 	return (lookup_value(fadvisebehav, advice));
415 }
416 
417 bool
418 sysdecode_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 
456 bool
457 sysdecode_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 
480 bool
481 sysdecode_flock_operation(FILE *fp, int operation, int *rem)
482 {
483 
484 	return (print_mask_int(fp, flockops, operation, rem));
485 }
486 
487 static struct name_table getfsstatmode[] = {
488 	X(MNT_WAIT) X(MNT_NOWAIT) XEND
489 };
490 
491 const char *
492 sysdecode_getfsstat_mode(int mode)
493 {
494 
495 	return (lookup_value(getfsstatmode, mode));
496 }
497 
498 const char *
499 sysdecode_getrusage_who(int who)
500 {
501 
502 	return (lookup_value(rusage, who));
503 }
504 
505 static struct name_table kevent_user_ffctrl[] = {
506 	X(NOTE_FFNOP) X(NOTE_FFAND) X(NOTE_FFOR) X(NOTE_FFCOPY)
507 	XEND
508 };
509 
510 static struct name_table kevent_rdwr_fflags[] = {
511 	X(NOTE_LOWAT) X(NOTE_FILE_POLL) XEND
512 };
513 
514 static 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 
520 static 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 
525 static 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 
530 void
531 sysdecode_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 
601 bool
602 sysdecode_kevent_flags(FILE *fp, int flags, int *rem)
603 {
604 
605 	return (print_mask_int(fp, keventflags, flags, rem));
606 }
607 
608 const char *
609 sysdecode_kevent_filter(int filter)
610 {
611 
612 	return (lookup_value(keventfilters, filter));
613 }
614 
615 const char *
616 sysdecode_kldsym_cmd(int cmd)
617 {
618 
619 	return (lookup_value(kldsymcmd, cmd));
620 }
621 
622 const char *
623 sysdecode_kldunload_flags(int flags)
624 {
625 
626 	return (lookup_value(kldunloadfflags, flags));
627 }
628 
629 const char *
630 sysdecode_lio_listio_mode(int mode)
631 {
632 
633 	return (lookup_value(lio_listiomodes, mode));
634 }
635 
636 const char *
637 sysdecode_madvice(int advice)
638 {
639 
640 	return (lookup_value(madvisebehav, advice));
641 }
642 
643 const char *
644 sysdecode_minherit_inherit(int inherit)
645 {
646 
647 	return (lookup_value(minheritflags, inherit));
648 }
649 
650 bool
651 sysdecode_mlockall_flags(FILE *fp, int flags, int *rem)
652 {
653 
654 	return (print_mask_int(fp, mlockallflags, flags, rem));
655 }
656 
657 bool
658 sysdecode_mmap_prot(FILE *fp, int prot, int *rem)
659 {
660 	int protm;
661 	bool printed;
662 
663 	printed = false;
664 	protm = PROT_MAX_EXTRACT(prot);
665 	if (protm != 0) {
666 		fputs("PROT_MAX(", fp);
667 		printed = print_mask_int(fp, mmapprot, protm, rem);
668 		fputs(")|", fp);
669 		prot = protm;
670 	}
671 	return (print_mask_int(fp, mmapprot, prot, rem) || printed);
672 }
673 
674 bool
675 sysdecode_fileflags(FILE *fp, fflags_t flags, fflags_t *rem)
676 {
677 
678 	return (print_mask_0(fp, fileflags, flags, rem));
679 }
680 
681 bool
682 sysdecode_filemode(FILE *fp, int mode, int *rem)
683 {
684 
685 	return (print_mask_0(fp, filemode, mode, rem));
686 }
687 
688 bool
689 sysdecode_mount_flags(FILE *fp, int flags, int *rem)
690 {
691 
692 	return (print_mask_int(fp, mountflags, flags, rem));
693 }
694 
695 bool
696 sysdecode_msync_flags(FILE *fp, int flags, int *rem)
697 {
698 
699 	return (print_mask_int(fp, msyncflags, flags, rem));
700 }
701 
702 const char *
703 sysdecode_nfssvc_flags(int flags)
704 {
705 
706 	return (lookup_value(nfssvcflags, flags));
707 }
708 
709 static struct name_table pipe2flags[] = {
710 	X(O_CLOEXEC) X(O_NONBLOCK) XEND
711 };
712 
713 bool
714 sysdecode_pipe2_flags(FILE *fp, int flags, int *rem)
715 {
716 
717 	return (print_mask_0(fp, pipe2flags, flags, rem));
718 }
719 
720 const char *
721 sysdecode_prio_which(int which)
722 {
723 
724 	return (lookup_value(prio, which));
725 }
726 
727 const char *
728 sysdecode_procctl_cmd(int cmd)
729 {
730 
731 	return (lookup_value(procctlcmd, cmd));
732 }
733 
734 const char *
735 sysdecode_ptrace_request(int request)
736 {
737 
738 	return (lookup_value(ptraceop, request));
739 }
740 
741 static struct name_table quotatypes[] = {
742 	X(GRPQUOTA) X(USRQUOTA) XEND
743 };
744 
745 bool
746 sysdecode_quotactl_cmd(FILE *fp, int cmd)
747 {
748 	const char *primary, *type;
749 
750 	primary = lookup_value(quotactlcmds, cmd >> SUBCMDSHIFT);
751 	if (primary == NULL)
752 		return (false);
753 	fprintf(fp, "QCMD(%s,", primary);
754 	type = lookup_value(quotatypes, cmd & SUBCMDMASK);
755 	if (type != NULL)
756 		fprintf(fp, "%s", type);
757 	else
758 		fprintf(fp, "%#x", cmd & SUBCMDMASK);
759 	fprintf(fp, ")");
760 	return (true);
761 }
762 
763 bool
764 sysdecode_reboot_howto(FILE *fp, int howto, int *rem)
765 {
766 	bool printed;
767 
768 	/*
769 	 * RB_AUTOBOOT is special in that its value is zero, but it is
770 	 * also an implied argument if a different operation is not
771 	 * requested via RB_HALT, RB_POWERCYCLE, RB_POWEROFF, or
772 	 * RB_REROOT.
773 	 */
774 	if (howto != 0 && (howto & (RB_HALT | RB_POWEROFF | RB_REROOT |
775 	    RB_POWERCYCLE)) == 0) {
776 		fputs("RB_AUTOBOOT|", fp);
777 		printed = true;
778 	} else
779 		printed = false;
780 	return (print_mask_int(fp, rebootopt, howto, rem) || printed);
781 }
782 
783 bool
784 sysdecode_rfork_flags(FILE *fp, int flags, int *rem)
785 {
786 
787 	return (print_mask_int(fp, rforkflags, flags, rem));
788 }
789 
790 const char *
791 sysdecode_rlimit(int resource)
792 {
793 
794 	return (lookup_value(rlimit, resource));
795 }
796 
797 const char *
798 sysdecode_scheduler_policy(int policy)
799 {
800 
801 	return (lookup_value(schedpolicy, policy));
802 }
803 
804 bool
805 sysdecode_sendfile_flags(FILE *fp, int flags, int *rem)
806 {
807 
808 	return (print_mask_int(fp, sendfileflags, flags, rem));
809 }
810 
811 bool
812 sysdecode_shmat_flags(FILE *fp, int flags, int *rem)
813 {
814 
815 	return (print_mask_int(fp, shmatflags, flags, rem));
816 }
817 
818 const char *
819 sysdecode_shutdown_how(int how)
820 {
821 
822 	return (lookup_value(shutdownhow, how));
823 }
824 
825 const char *
826 sysdecode_sigbus_code(int si_code)
827 {
828 
829 	return (lookup_value(sigbuscode, si_code));
830 }
831 
832 const char *
833 sysdecode_sigchld_code(int si_code)
834 {
835 
836 	return (lookup_value(sigchldcode, si_code));
837 }
838 
839 const char *
840 sysdecode_sigfpe_code(int si_code)
841 {
842 
843 	return (lookup_value(sigfpecode, si_code));
844 }
845 
846 const char *
847 sysdecode_sigill_code(int si_code)
848 {
849 
850 	return (lookup_value(sigillcode, si_code));
851 }
852 
853 const char *
854 sysdecode_sigsegv_code(int si_code)
855 {
856 
857 	return (lookup_value(sigsegvcode, si_code));
858 }
859 
860 const char *
861 sysdecode_sigtrap_code(int si_code)
862 {
863 
864 	return (lookup_value(sigtrapcode, si_code));
865 }
866 
867 const char *
868 sysdecode_sigprocmask_how(int how)
869 {
870 
871 	return (lookup_value(sigprocmaskhow, how));
872 }
873 
874 const char *
875 sysdecode_socketdomain(int domain)
876 {
877 
878 	return (lookup_value(sockdomain, domain));
879 }
880 
881 const char *
882 sysdecode_socket_protocol(int domain, int protocol)
883 {
884 
885 	switch (domain) {
886 	case PF_INET:
887 	case PF_INET6:
888 		return (lookup_value(sockipproto, protocol));
889 	default:
890 		return (NULL);
891 	}
892 }
893 
894 const char *
895 sysdecode_sockaddr_family(int sa_family)
896 {
897 
898 	return (lookup_value(sockfamily, sa_family));
899 }
900 
901 const char *
902 sysdecode_ipproto(int protocol)
903 {
904 
905 	return (lookup_value(sockipproto, protocol));
906 }
907 
908 const char *
909 sysdecode_sockopt_name(int level, int optname)
910 {
911 
912 	if (level == SOL_SOCKET)
913 		return (lookup_value(sockopt, optname));
914 	if (level == IPPROTO_IP)
915 		/* XXX: UNIX domain socket options use a level of 0 also. */
916 		return (lookup_value(sockoptip, optname));
917 	if (level == IPPROTO_IPV6)
918 		return (lookup_value(sockoptipv6, optname));
919 	if (level == IPPROTO_SCTP)
920 		return (lookup_value(sockoptsctp, optname));
921 	if (level == IPPROTO_TCP)
922 		return (lookup_value(sockopttcp, optname));
923 	if (level == IPPROTO_UDP)
924 		return (lookup_value(sockoptudp, optname));
925 	if (level == IPPROTO_UDPLITE)
926 		return (lookup_value(sockoptudplite, optname));
927 	return (NULL);
928 }
929 
930 bool
931 sysdecode_thr_create_flags(FILE *fp, int flags, int *rem)
932 {
933 
934 	return (print_mask_int(fp, thrcreateflags, flags, rem));
935 }
936 
937 const char *
938 sysdecode_umtx_op(int op)
939 {
940 
941 	return (lookup_value(umtxop, op));
942 }
943 
944 const char *
945 sysdecode_vmresult(int result)
946 {
947 
948 	return (lookup_value(vmresult, result));
949 }
950 
951 bool
952 sysdecode_wait4_options(FILE *fp, int options, int *rem)
953 {
954 	bool printed;
955 	int opt6;
956 
957 	/* A flags value of 0 is normal. */
958 	if (options == 0) {
959 		fputs("0", fp);
960 		if (rem != NULL)
961 			*rem = 0;
962 		return (true);
963 	}
964 
965 	/*
966 	 * These flags are implicit and aren't valid flags for wait4()
967 	 * directly (though they don't fail with EINVAL).
968 	 */
969 	opt6 = options & (WEXITED | WTRAPPED);
970 	options &= ~opt6;
971 	printed = print_mask_int(fp, wait6opt, options, rem);
972 	if (rem != NULL)
973 		*rem |= opt6;
974 	return (printed);
975 }
976 
977 bool
978 sysdecode_wait6_options(FILE *fp, int options, int *rem)
979 {
980 
981 	return (print_mask_int(fp, wait6opt, options, rem));
982 }
983 
984 const char *
985 sysdecode_whence(int whence)
986 {
987 
988 	return (lookup_value(seekwhence, whence));
989 }
990 
991 const char *
992 sysdecode_fcntl_cmd(int cmd)
993 {
994 
995 	return (lookup_value(fcntlcmd, cmd));
996 }
997 
998 static struct name_table fcntl_fd_arg[] = {
999 	X(FD_CLOEXEC) X(0) XEND
1000 };
1001 
1002 bool
1003 sysdecode_fcntl_arg_p(int cmd)
1004 {
1005 
1006 	switch (cmd) {
1007 	case F_GETFD:
1008 	case F_GETFL:
1009 	case F_GETOWN:
1010 		return (false);
1011 	default:
1012 		return (true);
1013 	}
1014 }
1015 
1016 void
1017 sysdecode_fcntl_arg(FILE *fp, int cmd, uintptr_t arg, int base)
1018 {
1019 	int rem;
1020 
1021 	switch (cmd) {
1022 	case F_SETFD:
1023 		if (!print_value(fp, fcntl_fd_arg, arg))
1024 		    print_integer(fp, arg, base);
1025 		break;
1026 	case F_SETFL:
1027 		if (!sysdecode_fcntl_fileflags(fp, arg, &rem))
1028 			fprintf(fp, "%#x", rem);
1029 		else if (rem != 0)
1030 			fprintf(fp, "|%#x", rem);
1031 		break;
1032 	case F_GETLK:
1033 	case F_SETLK:
1034 	case F_SETLKW:
1035 		fprintf(fp, "%p", (void *)arg);
1036 		break;
1037 	default:
1038 		print_integer(fp, arg, base);
1039 		break;
1040 	}
1041 }
1042 
1043 bool
1044 sysdecode_mmap_flags(FILE *fp, int flags, int *rem)
1045 {
1046 	uintmax_t val;
1047 	bool printed;
1048 	int align;
1049 
1050 	/*
1051 	 * MAP_ALIGNED can't be handled directly by print_mask_int().
1052 	 * MAP_32BIT is also problematic since it isn't defined for
1053 	 * all platforms.
1054 	 */
1055 	printed = false;
1056 	align = flags & MAP_ALIGNMENT_MASK;
1057 	val = (unsigned)flags & ~MAP_ALIGNMENT_MASK;
1058 	print_mask_part(fp, mmapflags, &val, &printed);
1059 #ifdef MAP_32BIT
1060 	if (val & MAP_32BIT) {
1061 		fprintf(fp, "%sMAP_32BIT", printed ? "|" : "");
1062 		printed = true;
1063 		val &= ~MAP_32BIT;
1064 	}
1065 #endif
1066 	if (align != 0) {
1067 		if (printed)
1068 			fputc('|', fp);
1069 		if (align == MAP_ALIGNED_SUPER)
1070 			fputs("MAP_ALIGNED_SUPER", fp);
1071 		else
1072 			fprintf(fp, "MAP_ALIGNED(%d)",
1073 			    align >> MAP_ALIGNMENT_SHIFT);
1074 		printed = true;
1075 	}
1076 	if (rem != NULL)
1077 		*rem = val;
1078 	return (printed);
1079 }
1080 
1081 const char *
1082 sysdecode_pathconf_name(int name)
1083 {
1084 
1085 	return (lookup_value(pathconfname, name));
1086 }
1087 
1088 const char *
1089 sysdecode_rtprio_function(int function)
1090 {
1091 
1092 	return (lookup_value(rtpriofuncs, function));
1093 }
1094 
1095 bool
1096 sysdecode_msg_flags(FILE *fp, int flags, int *rem)
1097 {
1098 
1099 	return (print_mask_0(fp, msgflags, flags, rem));
1100 }
1101 
1102 const char *
1103 sysdecode_sigcode(int sig, int si_code)
1104 {
1105 	const char *str;
1106 
1107 	str = lookup_value(sigcode, si_code);
1108 	if (str != NULL)
1109 		return (str);
1110 
1111 	switch (sig) {
1112 	case SIGILL:
1113 		return (sysdecode_sigill_code(si_code));
1114 	case SIGBUS:
1115 		return (sysdecode_sigbus_code(si_code));
1116 	case SIGSEGV:
1117 		return (sysdecode_sigsegv_code(si_code));
1118 	case SIGFPE:
1119 		return (sysdecode_sigfpe_code(si_code));
1120 	case SIGTRAP:
1121 		return (sysdecode_sigtrap_code(si_code));
1122 	case SIGCHLD:
1123 		return (sysdecode_sigchld_code(si_code));
1124 	default:
1125 		return (NULL);
1126 	}
1127 }
1128 
1129 const char *
1130 sysdecode_sysarch_number(int number)
1131 {
1132 
1133 	return (lookup_value(sysarchnum, number));
1134 }
1135 
1136 bool
1137 sysdecode_umtx_cvwait_flags(FILE *fp, u_long flags, u_long *rem)
1138 {
1139 
1140 	return (print_mask_0ul(fp, umtxcvwaitflags, flags, rem));
1141 }
1142 
1143 bool
1144 sysdecode_umtx_rwlock_flags(FILE *fp, u_long flags, u_long *rem)
1145 {
1146 
1147 	return (print_mask_0ul(fp, umtxrwlockflags, flags, rem));
1148 }
1149 
1150 void
1151 sysdecode_cap_rights(FILE *fp, cap_rights_t *rightsp)
1152 {
1153 	struct name_table *t;
1154 	int i;
1155 	bool comma;
1156 
1157 	for (i = 0; i < CAPARSIZE(rightsp); i++) {
1158 		if (CAPIDXBIT(rightsp->cr_rights[i]) != 1 << i) {
1159 			fprintf(fp, "invalid cap_rights_t");
1160 			return;
1161 		}
1162 	}
1163 	comma = false;
1164 	for (t = caprights; t->str != NULL; t++) {
1165 		if (cap_rights_is_set(rightsp, t->val)) {
1166 			fprintf(fp, "%s%s", comma ? "," : "", t->str);
1167 			comma = true;
1168 		}
1169 	}
1170 }
1171 
1172 static struct name_table cmsgtypeip[] = {
1173 	X(IP_RECVDSTADDR) X(IP_RECVTTL) X(IP_RECVOPTS) X(IP_RECVRETOPTS)
1174 	X(IP_RECVIF) X(IP_RECVTOS) X(IP_FLOWID) X(IP_FLOWTYPE)
1175 	X(IP_RSSBUCKETID) XEND
1176 };
1177 
1178 static struct name_table cmsgtypeipv6[] = {
1179 #if 0
1180 	/* The RFC 2292 defines are kernel space only. */
1181 	X(IPV6_2292PKTINFO) X(IPV6_2292HOPLIMIT) X(IPV6_2292HOPOPTS)
1182 	X(IPV6_2292DSTOPTS) X(IPV6_2292RTHDR) X(IPV6_2292NEXTHOP)
1183 #endif
1184 	X(IPV6_PKTINFO)  X(IPV6_HOPLIMIT) X(IPV6_HOPOPTS)
1185 	X(IPV6_DSTOPTS) X(IPV6_RTHDR) X(IPV6_NEXTHOP)
1186 	X(IPV6_TCLASS) X(IPV6_FLOWID) X(IPV6_FLOWTYPE) X(IPV6_RSSBUCKETID)
1187 	X(IPV6_PATHMTU) X(IPV6_RTHDRDSTOPTS) X(IPV6_USE_MIN_MTU)
1188 	X(IPV6_DONTFRAG) X(IPV6_PREFER_TEMPADDR) XEND
1189 };
1190 
1191 static struct name_table cmsgtypesctp[] = {
1192 	X(SCTP_INIT) X(SCTP_SNDRCV) X(SCTP_EXTRCV) X(SCTP_SNDINFO)
1193 	X(SCTP_RCVINFO) X(SCTP_NXTINFO) X(SCTP_PRINFO) X(SCTP_AUTHINFO)
1194 	X(SCTP_DSTADDRV4) X(SCTP_DSTADDRV6) XEND
1195 };
1196 
1197 const char *
1198 sysdecode_cmsg_type(int cmsg_level, int cmsg_type)
1199 {
1200 
1201 	if (cmsg_level == SOL_SOCKET)
1202 		return (lookup_value(cmsgtypesocket, cmsg_type));
1203 	if (cmsg_level == IPPROTO_IP)
1204 		return (lookup_value(cmsgtypeip, cmsg_type));
1205 	if (cmsg_level == IPPROTO_IPV6)
1206 		return (lookup_value(cmsgtypeipv6, cmsg_type));
1207 	if (cmsg_level == IPPROTO_SCTP)
1208 		return (lookup_value(cmsgtypesctp, cmsg_type));
1209 	return (NULL);
1210 }
1211 
1212 const char *
1213 sysdecode_sctp_pr_policy(int policy)
1214 {
1215 
1216 	return (lookup_value(sctpprpolicy, policy));
1217 }
1218 
1219 static struct name_table sctpsndflags[] = {
1220 	X(SCTP_EOF) X(SCTP_ABORT) X(SCTP_UNORDERED) X(SCTP_ADDR_OVER)
1221 	X(SCTP_SENDALL) X(SCTP_EOR) X(SCTP_SACK_IMMEDIATELY) XEND
1222 };
1223 
1224 bool
1225 sysdecode_sctp_snd_flags(FILE *fp, int flags, int *rem)
1226 {
1227 
1228 	return (print_mask_int(fp, sctpsndflags, flags, rem));
1229 }
1230 
1231 static struct name_table sctprcvflags[] = {
1232 	X(SCTP_UNORDERED) XEND
1233 };
1234 
1235 bool
1236 sysdecode_sctp_rcv_flags(FILE *fp, int flags, int *rem)
1237 {
1238 
1239 	return (print_mask_int(fp, sctprcvflags, flags, rem));
1240 }
1241 
1242 static struct name_table sctpnxtflags[] = {
1243 	X(SCTP_UNORDERED) X(SCTP_COMPLETE) X(SCTP_NOTIFICATION) XEND
1244 };
1245 
1246 bool
1247 sysdecode_sctp_nxt_flags(FILE *fp, int flags, int *rem)
1248 {
1249 
1250 	return (print_mask_int(fp, sctpnxtflags, flags, rem));
1251 }
1252 
1253 static struct name_table sctpsinfoflags[] = {
1254 	X(SCTP_EOF) X(SCTP_ABORT) X(SCTP_UNORDERED) X(SCTP_ADDR_OVER)
1255 	X(SCTP_SENDALL) X(SCTP_EOR) X(SCTP_SACK_IMMEDIATELY) XEND
1256 };
1257 
1258 void
1259 sysdecode_sctp_sinfo_flags(FILE *fp, int sinfo_flags)
1260 {
1261 	const char *temp;
1262 	int rem;
1263 	bool printed;
1264 
1265 	printed = print_mask_0(fp, sctpsinfoflags, sinfo_flags, &rem);
1266 	if (rem & ~SCTP_PR_SCTP_ALL) {
1267 		fprintf(fp, "%s%#x", printed ? "|" : "", rem & ~SCTP_PR_SCTP_ALL);
1268 		printed = true;
1269 		rem &= ~SCTP_PR_SCTP_ALL;
1270 	}
1271 	if (rem != 0) {
1272 		temp = sysdecode_sctp_pr_policy(rem);
1273 		if (temp != NULL) {
1274 			fprintf(fp, "%s%s", printed ? "|" : "", temp);
1275 		} else {
1276 			fprintf(fp, "%s%#x", printed ? "|" : "", rem);
1277 		}
1278 	}
1279 }
1280 
1281 bool
1282 sysdecode_shmflags(FILE *fp, int flags, int *rem)
1283 {
1284 
1285 	return (print_mask_0(fp, shmflags, flags, rem));
1286 }
1287