]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3:util_sec: add a cache to set_thread_credentials()
authorStefan Metzmacher <metze@samba.org>
Thu, 3 May 2018 14:08:06 +0000 (16:08 +0200)
committerRalph Boehme <slow@samba.org>
Tue, 24 Jul 2018 18:35:17 +0000 (20:35 +0200)
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 <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
Autobuild-User(master): Ralph Böhme <slow@samba.org>
Autobuild-Date(master): Tue Jul 24 20:35:17 CEST 2018 on sn-devel-144

source3/lib/util_sec.c

index a6817f6e3c496dcf5aa01a13ad351a6d2cc14e95..703c522d77b88be84e43c160c42d31bd5ab64ee8 100644 (file)
@@ -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;