From 38a619af97ccb98cfca702d8ae084926fd42fd39 Mon Sep 17 00:00:00 2001
From: "David M. Lee" <dlee@digium.com>
Date: Wed, 5 Mar 2014 16:58:21 +0000
Subject: [PATCH] Corrected cross-platform stat nanosecond code

When nanosecond time resolution was added for identifying config file
changes, it didn't cover all of the myriad of ways that one might obtain
nanosecond time resolution off of struct stat.

Rather than complicate the #if even further figuring out one system from
the next, this patch directly tests for the three struct members I know
about today, and #ifdef's accordingly.

Review: https://reviewboard.asterisk.org/r/3273/
........

Merged revisions 409833 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 409834 from http://svn.asterisk.org/svn/asterisk/branches/11
........

Merged revisions 409835 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@409836 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 configure                        | 39 +++++++++++++++++++++++++++++++-
 configure.ac                     |  6 +++++
 include/asterisk/autoconfig.h.in |  9 ++++++++
 main/config.c                    |  8 +++++--
 4 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/configure b/configure
index 84de43aa26..330b9d32c4 100755
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.ac Revision: 405130 .
+# From configure.ac Revision: 406825 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.69 for asterisk trunk.
 #
@@ -13878,6 +13878,43 @@ _ACEOF
 
 fi
 
+stat_nsec_found=no
+ac_fn_c_check_member "$LINENO" "struct stat" "st_mtim" "ac_cv_member_struct_stat_st_mtim" "#include <sys/stat.h>
+"
+if test "x$ac_cv_member_struct_stat_st_mtim" = xyes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_STAT_ST_MTIM 1
+_ACEOF
+
+stat_nsec_found=yes
+fi
+ac_fn_c_check_member "$LINENO" "struct stat" "st_mtimensec" "ac_cv_member_struct_stat_st_mtimensec" "#include <sys/stat.h>
+"
+if test "x$ac_cv_member_struct_stat_st_mtimensec" = xyes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
+_ACEOF
+
+stat_nsec_found=yes
+fi
+ac_fn_c_check_member "$LINENO" "struct stat" "st_mtimespec" "ac_cv_member_struct_stat_st_mtimespec" "#include <sys/stat.h>
+"
+if test "x$ac_cv_member_struct_stat_st_mtimespec" = xyes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_STAT_ST_MTIMESPEC 1
+_ACEOF
+
+stat_nsec_found=yes
+fi
+
+
+if test "${stat_nsec_found}" != yes; then
+	{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Cannot determine nanosecond field of struct stat" >&5
+$as_echo "$as_me: WARNING: Cannot determine nanosecond field of struct stat" >&2;}
+fi
 
 # Checks for library functions.
 for ac_header in unistd.h
diff --git a/configure.ac b/configure.ac
index 80fbcf59c5..67d2647015 100644
--- a/configure.ac
+++ b/configure.ac
@@ -606,6 +606,12 @@ AC_HEADER_TIME
 AC_STRUCT_TM
 AC_C_VOLATILE
 AC_CHECK_TYPES([ptrdiff_t])
+stat_nsec_found=no
+AC_CHECK_MEMBERS([struct stat.st_mtim, struct stat.st_mtimensec, struct stat.st_mtimespec], [stat_nsec_found=yes], [], [[#include <sys/stat.h>]])
+
+if test "${stat_nsec_found}" != yes; then
+	AC_MSG_WARN(Cannot determine nanosecond field of struct stat)
+fi
 
 # Checks for library functions.
 AC_FUNC_CHOWN
diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in
index 2570819244..71f884279a 100644
--- a/include/asterisk/autoconfig.h.in
+++ b/include/asterisk/autoconfig.h.in
@@ -894,6 +894,15 @@
 /* Define to 1 if `st_blksize' is a member of `struct stat'. */
 #undef HAVE_STRUCT_STAT_ST_BLKSIZE
 
+/* Define to 1 if `st_mtim' is a member of `struct stat'. */
+#undef HAVE_STRUCT_STAT_ST_MTIM
+
+/* Define to 1 if `st_mtimensec' is a member of `struct stat'. */
+#undef HAVE_STRUCT_STAT_ST_MTIMENSEC
+
+/* Define to 1 if `st_mtimespec' is a member of `struct stat'. */
+#undef HAVE_STRUCT_STAT_ST_MTIMESPEC
+
 /* Define to 1 if `cr_uid' is a member of `struct ucred'. */
 #undef HAVE_STRUCT_UCRED_CR_UID
 
diff --git a/main/config.c b/main/config.c
index 8961fa9e33..a7228c36bd 100644
--- a/main/config.c
+++ b/main/config.c
@@ -1246,10 +1246,14 @@ static void cfmstat_clear(struct cache_file_mtime *cfmtime)
 static void cfmstat_save(struct cache_file_mtime *cfmtime, struct stat *statbuf)
 {
 	cfmtime->stat_size = statbuf->st_size;
-#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || (defined(_POSIX_C_SOURCE) && 200809L <= _POSIX_C_SOURCE) || (defined(_XOPEN_SOURCE) && 700 <= _XOPEN_SOURCE)
+#if defined(HAVE_STRUCT_STAT_ST_MTIM)
 	cfmtime->stat_mtime_nsec = statbuf->st_mtim.tv_nsec;
-#else
+#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
 	cfmtime->stat_mtime_nsec = statbuf->st_mtimensec;
+#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
+	cfmtime->stat_mtime_nsec = statbuf->st_mtimespec.tv_nsec;
+#else
+	cfmtime->stat_mtime_nsec = 0;
 #endif
 	cfmtime->stat_mtime = statbuf->st_mtime;
 }
-- 
GitLab