From 10fc65b74d7b94e0e6454c704b63c138d1d54bd1 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 3 May 2018 16:08:06 +0200 Subject: [PATCH] s3:util_sec: add a cache to set_thread_credentials() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Calling set_thread_credentials() with the same values, skips syscalls the 2nd time. We only do this if '__thread' is supported to provide thread local storage. Signed-off-by: Stefan Metzmacher Reviewed-by: Ralph Boehme Autobuild-User(master): Ralph Böhme Autobuild-Date(master): Tue Jul 24 20:35:17 CEST 2018 on sn-devel-144 --- source3/lib/util_sec.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/source3/lib/util_sec.c b/source3/lib/util_sec.c index a6817f6e3c4..703c522d77b 100644 --- a/source3/lib/util_sec.c +++ b/source3/lib/util_sec.c @@ -460,6 +460,24 @@ int set_thread_credentials(uid_t uid, * we know we have setresuid/setresgid * available. */ +#ifdef HAVE___THREAD + static struct { + bool active; + uid_t uid; + gid_t gid; + size_t setlen; + uintptr_t gidset; + } __thread cache; + + if (cache.active && + cache.uid == uid && + cache.gid == gid && + cache.setlen == setlen && + (const gid_t *)cache.gidset == gidset) + { + return 0; + } +#endif /* HAVE___THREAD */ /* Become root. */ /* Set ru=0, eu=0 */ @@ -485,6 +503,15 @@ int set_thread_credentials(uid_t uid, smb_panic("set_thread_credentials failed\n"); return -1; } + +#ifdef HAVE___THREAD + cache.active = true; + cache.uid = uid; + cache.gid = gid; + cache.setlen = setlen; + cache.gidset = (uintptr_t)gidset; +#endif /* HAVE___THREAD */ + return 0; #else errno = ENOSYS; -- 2.47.3