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