c8206d0
From d4e5b6e1e5585d341d1e51f1ddc637c099111076 Mon Sep 17 00:00:00 2001
c8206d0
From: Joe Orton <jorton@redhat.com>
c8206d0
Date: Tue, 7 Jul 2020 09:48:01 +0100
c8206d0
Subject: [PATCH] Check and use gettid() directly with glibc 2.30+.
c8206d0
c8206d0
* configure.in: Check for gettid() and define HAVE_SYS_GETTID if
c8206d0
  gettid() is only usable via syscall().
c8206d0
c8206d0
* server/log.c (log_tid): Use gettid() directly if available.
c8206d0
---
c8206d0
 configure.in | 14 +++++++++-----
c8206d0
 server/log.c |  8 ++++++--
c8206d0
 2 files changed, 15 insertions(+), 7 deletions(-)
c8206d0
c8206d0
diff --git a/configure.in b/configure.in
c8206d0
index 423d58d4b9a..60cbf7b7f81 100644
c8206d0
--- httpd-2.4.43/configure.in.gettid
c8206d0
+++ httpd-2.4.43/configure.in
2c7ae79
@@ -478,7 +500,8 @@
c8206d0
 timegm \
c8206d0
 getpgid \
c8206d0
 fopen64 \
c8206d0
-getloadavg
c8206d0
+getloadavg \
c8206d0
+gettid
c8206d0
 )
c8206d0
 
c8206d0
 dnl confirm that a void pointer is large enough to store a long integer
2c7ae79
@@ -489,16 +512,19 @@
c8206d0
    APR_ADDTO(HTTPD_LIBS, [-lselinux])
c8206d0
 ])
c8206d0
 
c8206d0
-AC_CACHE_CHECK([for gettid()], ac_cv_gettid,
c8206d0
+if test $ac_cv_func_gettid = no; then
c8206d0
+  # On Linux before glibc 2.30, gettid() is only usable via syscall()
c8206d0
+  AC_CACHE_CHECK([for gettid() via syscall], ap_cv_gettid,
c8206d0
 [AC_TRY_RUN(#define _GNU_SOURCE
c8206d0
 #include <unistd.h>
c8206d0
 #include <sys/syscall.h>
c8206d0
 #include <sys/types.h>
c8206d0
 int main(int argc, char **argv) {
c8206d0
 pid_t t = syscall(SYS_gettid); return t == -1 ? 1 : 0; },
c8206d0
-[ac_cv_gettid=yes], [ac_cv_gettid=no], [ac_cv_gettid=no])])
c8206d0
-if test "$ac_cv_gettid" = "yes"; then
c8206d0
-    AC_DEFINE(HAVE_GETTID, 1, [Define if you have gettid()])
c8206d0
+  [ap_cv_gettid=yes], [ap_cv_gettid=no], [ap_cv_gettid=no])])
c8206d0
+  if test "$ap_cv_gettid" = "yes"; then
c8206d0
+      AC_DEFINE(HAVE_SYS_GETTID, 1, [Define if you have gettid() via syscall()])
c8206d0
+  fi
c8206d0
 fi
c8206d0
 
c8206d0
 dnl ## Check for the tm_gmtoff field in struct tm to get the timezone diffs
c8206d0
--- httpd-2.4.43/server/log.c.gettid
c8206d0
+++ httpd-2.4.43/server/log.c
2c7ae79
@@ -55,7 +55,7 @@
2c7ae79
 #include "ap_mpm.h"
2c7ae79
 #include "ap_listen.h"
2c7ae79
 
2c7ae79
-#if HAVE_GETTID
2c7ae79
+#if HAVE_SYS_GETTID
2c7ae79
 #include <sys/syscall.h>
2c7ae79
 #include <sys/types.h>
2c7ae79
 #endif
c8206d0
@@ -625,14 +625,18 @@
c8206d0
 #if APR_HAS_THREADS
c8206d0
     int result;
c8206d0
 #endif
c8206d0
-#if HAVE_GETTID
c8206d0
+#if defined(HAVE_GETTID) || defined(HAVE_SYS_GETTID)
c8206d0
     if (arg && *arg == 'g') {
c8206d0
+#ifdef HAVE_GETTID
c8206d0
+        pid_t tid = gettid();
c8206d0
+#else
c8206d0
         pid_t tid = syscall(SYS_gettid);
c8206d0
+#endif
c8206d0
         if (tid == -1)
c8206d0
             return 0;
c8206d0
         return apr_snprintf(buf, buflen, "%"APR_PID_T_FMT, tid);
c8206d0
     }
c8206d0
-#endif
c8206d0
+#endif /* HAVE_GETTID || HAVE_SYS_GETTID */
c8206d0
 #if APR_HAS_THREADS
c8206d0
     if (ap_mpm_query(AP_MPMQ_IS_THREADED, &result) == APR_SUCCESS
c8206d0
         && result != AP_MPMQ_NOT_SUPPORTED)
c8206d0
@@ -966,7 +970,7 @@
c8206d0
 #if APR_HAS_THREADS
c8206d0
         field_start = len;
c8206d0
         len += cpystrn(buf + len, ":tid ", buflen - len);
c8206d0
-        item_len = log_tid(info, NULL, buf + len, buflen - len);
c8206d0
+        item_len = log_tid(info, "g", buf + len, buflen - len);
c8206d0
         if (!item_len)
c8206d0
             len = field_start;
c8206d0
         else