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