git: fde775bc53de - main - libsysdecode: Decode Linux open() flags

From: Dmitry Chagin <dchagin_at_FreeBSD.org>
Date: Wed, 22 Jun 2022 11:41:09 UTC
The branch main has been updated by dchagin:

URL: https://cgit.FreeBSD.org/src/commit/?id=fde775bc53de5b5abecdf8679f47bb3aa236e546

commit fde775bc53de5b5abecdf8679f47bb3aa236e546
Author:     Dmitry Chagin <dchagin@FreeBSD.org>
AuthorDate: 2022-06-22 11:26:10 +0000
Commit:     Dmitry Chagin <dchagin@FreeBSD.org>
CommitDate: 2022-06-22 11:26:10 +0000

    libsysdecode: Decode Linux open() flags
    
    MFC after:              2 weeks
---
 lib/libsysdecode/linux.c       | 35 +++++++++++++++++++++++++++++++++++
 lib/libsysdecode/mklinuxtables |  1 +
 lib/libsysdecode/sysdecode.h   |  1 +
 3 files changed, 37 insertions(+)

diff --git a/lib/libsysdecode/linux.c b/lib/libsysdecode/linux.c
index 8a3b88a7bfca..ef015dab6dd8 100644
--- a/lib/libsysdecode/linux.c
+++ b/lib/libsysdecode/linux.c
@@ -205,3 +205,38 @@ sysdecode_linux_atflags(FILE *fp, int flag, int *rem)
 
 	return (print_mask_int(fp, atflags, flag, rem));
 }
+
+bool
+sysdecode_linux_open_flags(FILE *fp, int flags, int *rem)
+{
+	bool printed;
+	int mode;
+	uintmax_t val;
+
+	mode = flags & LINUX_O_ACCMODE;
+	flags &= ~LINUX_O_ACCMODE;
+	switch (mode) {
+	case LINUX_O_RDONLY:
+		fputs("O_RDONLY", fp);
+		printed = true;
+		mode = 0;
+		break;
+	case LINUX_O_WRONLY:
+		fputs("O_WRONLY", fp);
+		printed = true;
+		mode = 0;
+		break;
+	case LINUX_O_RDWR:
+		fputs("O_RDWR", fp);
+		printed = true;
+		mode = 0;
+		break;
+	default:
+		printed = false;
+	}
+	val = (unsigned)flags;
+	print_mask_part(fp, openflags, &val, &printed);
+	if (rem != NULL)
+		*rem = val | mode;
+	return (printed);
+}
diff --git a/lib/libsysdecode/mklinuxtables b/lib/libsysdecode/mklinuxtables
index 734d69fca9cf..bcd6ddb5f618 100644
--- a/lib/libsysdecode/mklinuxtables
+++ b/lib/libsysdecode/mklinuxtables
@@ -92,6 +92,7 @@ gen_table "atflags"     "LINUX_AT_[A-Z_]+[[:space:]]+[0-9]+"             "compat
 gen_table "clockids"    "LINUX_CLOCK_[A-Z_]+[[:space:]]+[0-9]+"          "compat/linux/linux_timer.h"
 gen_table "clockflags"  "LINUX_TIMER_[A-Z_]+[[:space:]]+0x[0-9]+"        "compat/linux/linux_timer.h"
 gen_table "clockcpuids" "LINUX_CPUCLOCK_[A-Z_]+[[:space:]]+[0-9]+"       "compat/linux/linux_timer.h"	"_MASK|_MAX"
+gen_table "openflags"   "LINUX_O_[A-Z_]+[[:space:]]+[0-9]+"              "compat/linux/linux_file.h"	"O_RDONLY|O_RDWR|O_WRONLY|O_ACCMODE"
 gen_table "sigprocmaskhow" "LINUX_SIG_[A-Z]+[[:space:]]+[0-9]+"          "compat/linux/linux.h"
 
 # Generate a .depend file for our output file
diff --git a/lib/libsysdecode/sysdecode.h b/lib/libsysdecode/sysdecode.h
index 7b1a77aad9af..5bd4b136cdab 100644
--- a/lib/libsysdecode/sysdecode.h
+++ b/lib/libsysdecode/sysdecode.h
@@ -142,6 +142,7 @@ bool	sysdecode_shmflags(FILE *_fp, int _flags, int *_rem);
 bool	sysdecode_linux_atflags(FILE *_fp, int _flag, int *_rem);
 void	sysdecode_linux_clockid(FILE *_fp, clockid_t _which);
 bool	sysdecode_linux_clock_flags(FILE *_fp, int _flags, int *_rem);
+bool	sysdecode_linux_open_flags(FILE *_fp, int _flags, int *_rem);
 const char *sysdecode_linux_signal(int _sig);
 const char *sysdecode_linux_sigprocmask_how(int _how);