From 4c00ec8773fd63fa48ef49e1ccf2adac598427be Mon Sep 17 00:00:00 2001
From: Alejandro Alvarado <44826516+seisvelas@users.noreply.github.com>
Date: Mon, 17 Dec 2018 19:25:18 -0600
Subject: Add getdents / getdents64 support re ticket 28861

---
 src/common/compat.h |  8 ++++++++
 src/lib/syscall.c   | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/src/common/compat.h b/src/common/compat.h
index a9b73c2..d79301f 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -129,6 +129,12 @@ void tsocks_once(tsocks_once_t *o, void (*init_routine)(void));
 #ifndef __NR_memfd_create
 #define __NR_memfd_create -19
 #endif
+#ifndef __NR_getdents
+#define __NR_getdents -20
+#endif
+#ifndef __NR_getdents64
+#define __NR_getdents64 -21
+#endif
 
 #define TSOCKS_NR_SOCKET    __NR_socket
 #define TSOCKS_NR_CONNECT   __NR_connect
@@ -149,6 +155,8 @@ void tsocks_once(tsocks_once_t *o, void (*init_routine)(void));
 #define TSOCKS_NR_CLOCK_GETTIME __NR_clock_gettime
 #define TSOCKS_NR_FORK      __NR_fork
 #define TSOCKS_NR_MEMFD_CREATE __NR_memfd_create
+#define TSOCKS_NR_GETDENTS __NR_getdents
+#define TSOCKS_NR_GETDENTS64 __NR_getdents64
 
 /*
  * Despite glibc providing wrappers for these calls for a long time
diff --git a/src/lib/syscall.c b/src/lib/syscall.c
index 7fba580..f793da7 100644
--- a/src/lib/syscall.c
+++ b/src/lib/syscall.c
@@ -437,6 +437,37 @@ static LIBC_SYSCALL_RET_TYPE handle_memfd_create(va_list args)
 
 	return tsocks_libc_syscall(TSOCKS_NR_MEMFD_CREATE, name, flags);
 }
+/*
+ * Handle getdents(2) syscall.
+ */
+static LIBC_SYSCALL_RET_TYPE handle_getdents(va_list args)
+{
+	unsigned int fd;
+	struct linux_dirent *dirp;
+	unsigned int count;
+
+	fd = va_arg(args, __typeof__(fd));
+	dirp = va_arg(args, __typeof__(dirp));
+	count = va_arg(args, __typeof__(count));
+
+	return tsocks_libc_syscall(TSOCKS_NR_GETDENTS, fd, dirp, count);
+}
+/*
+ * Handle getdents64(2) syscall.
+ */
+static LIBC_SYSCALL_RET_TYPE handle_getdents64(va_list args)
+{
+	unsigned int fd;
+	struct linux_dirent64 *dirp;
+	unsigned int count;
+
+	fd = va_arg(args, __typeof__(fd));
+	dirp = va_arg(args, __typeof__(dirp));
+	count = va_arg(args, __typeof__(count));
+
+	return tsocks_libc_syscall(TSOCKS_NR_GETDENTS64, fd, dirp, count);
+}
+
 #endif /* __linux__ */
 
 /*
@@ -558,6 +589,12 @@ LIBC_SYSCALL_RET_TYPE tsocks_syscall(long int number, va_list args)
 	case TSOCKS_NR_MEMFD_CREATE:
 		ret = handle_memfd_create(args);
 		break;
+	case TSOCKS_NR_GETDENTS:
+		ret = handle_getdents(args);
+		break;
+	case TSOCKS_NR_GETDENTS64:
+		ret = handle_getdents64(args);
+		break;
 #endif /* __linux__ */
 	default:
 		/*
-- 
cgit v1.2.1

