shithub: opusfile

Download patch

ref: 069dc6e880628b8f1626d6a780eb8ccaf101951d
parent: 49578c103ac59e00538a73311a479c5456922016
author: Mark Harris <mark.hsj@gmail.com>
date: Sat Apr 25 16:37:51 EDT 2020

http: Fix use of deprecated function ftime()

The ftime() function, introduced in V7 Unix (1979), gets the current
time in seconds and milliseconds, and time zone information.  It was
marked as a legacy interface in POSIX.1-2001, and removed altogether
from POSIX.1-2008.  The gettimeofday() function, originally from
4.1 BSD, gets the current time in seconds and microseconds, and optional
time zone information, and was marked as obsolete in POSIX.1-2008
although it was kept in the standard.  The POSIX recommended function
for getting time with sub-second resolution is clock_gettime(), which
was introduced in POSIX.1b-1993 and is now part of the base POSIX
standard; it supports multiple clocks and nanosecond resolution.
Additionally the function timespec_get() was introduced in C11 and also
supports nanosecond resolution.

To support dates beyond the year 2038, glibc and other libraries are
being updated to support 64-bit time_t even on 32-bit architectures,
requiring new implementations of interfaces that work with time.  As
part of this effort, the ftime() function was deprecated in glibc 2.31
(released February 1, 2020), a warning is now issued when building code
that uses this function, and removal is planned for a future version of
glibc (https://sourceware.org/pipermail/libc-announce/2020/000025.html).

ftime() is used in http.c to measure time intervals with millisecond
resolution.  To avoid the glibc 2.31 deprecation warning and further
issues when the function is removed entirely from glibc, clock_gettime()
is now used instead when it is available in the C library, as it is on
current Linux systems.  Prior to glibc 2.17, clock_gettime() required
linking with librt; on such systems ftime() will continue to be used, to
avoid an additional library dependency.  macOS provides clock_gettime()
starting in macOS 10.12; earlier versions will continue to use ftime().
Windows provides ftime() but not clock_gettime(), so ftime() will
continue to be used on Windows.

ftime(), gettimeofday(), and clock_gettime() with the CLOCK_REALTIME
clock get the "real time", which is subject to jumps if set by an
administrator or time service.  The CLOCK_MONOTONIC clock does not have
this problem and is more suitable for measuring time intervals.  On
Linux, the CLOCK_BOOTTIME clock measures the time since last boot and is
the same as CLOCK_MONOTONIC except that the Linux CLOCK_MONOTONIC clock
does not advance when the system is suspended.  Because it is used to
measure time intervals, CLOCK_BOOTTIME or CLOCK_MONOTONIC are used when
available, when clock_gettime() is used.  However the only clock
required by POSIX.1-2008 is CLOCK_REALTIME, so that will be used if the
other clocks are not available.

--- a/configure.ac
+++ b/configure.ac
@@ -86,7 +86,27 @@
     )
   )
 )
-AC_SEARCH_LIBS(ftime, [compat], , [enable_http=no])
+
+# HTTP support requires either clock_gettime or ftime.  clock_gettime is
+# used only if time.h defines CLOCK_REALTIME and the function is available
+# in the standard library; on platforms such as glibc < 2.17 where -lrt
+# or another library would be required, ftime will be used.
+AS_IF([test "$enable_http" != "no"], [
+  AC_MSG_CHECKING([for clock_gettime])
+  AC_LINK_IFELSE([
+    AC_LANG_PROGRAM([[#include <time.h>]], [[
+      struct timespec ts;
+      return clock_gettime(CLOCK_REALTIME, &ts);
+    ]])
+  ], [
+    AC_MSG_RESULT([yes])
+    AC_DEFINE([OP_HAVE_CLOCK_GETTIME], [1],
+      [Enable use of clock_gettime function])
+  ], [
+    AC_MSG_RESULT([no])
+    AC_SEARCH_LIBS(ftime, [compat], , [enable_http=no])
+  ])
+])
 
 m4_ifndef([PKG_PROG_PKG_CONFIG],
   [m4_fatal([Could not locate the pkg-config autoconf macros.
--- a/src/http.c
+++ b/src/http.c
@@ -355,7 +355,15 @@
 #  define op_reset_errno() (errno=0)
 
 # endif
-# include <sys/timeb.h>
+
+# ifdef OP_HAVE_CLOCK_GETTIME
+#  include <time.h>
+typedef struct timespec op_time;
+# else
+#  include <sys/timeb.h>
+typedef struct timeb op_time;
+# endif
+
 # include <openssl/x509v3.h>
 
 # if (defined(LIBRESSL_VERSION_NUMBER)&&OPENSSL_VERSION_NUMBER==0x20000000L)
@@ -804,7 +812,7 @@
   /*The next connection in either the LRU or free list.*/
   OpusHTTPConn *next;
   /*The last time we blocked for reading from this connection.*/
-  struct timeb  read_time;
+  op_time       read_time;
   /*The number of bytes we've read since the last time we blocked.*/
   opus_int64    read_bytes;
   /*The estimated throughput of this connection, in bytes/s.*/
@@ -854,7 +862,7 @@
     struct sockaddr_in6 v6;
   }                addr;
   /*The last time we re-resolved the host.*/
-  struct timeb     resolve_time;
+  op_time          resolve_time;
   /*A buffer used to build HTTP requests.*/
   OpusStringBuf    request;
   /*A buffer used to build proxy CONNECT requests.*/
@@ -1009,9 +1017,32 @@
   return available;
 }
 
-static opus_int32 op_time_diff_ms(const struct timeb *_end,
- const struct timeb *_start){
+static void op_time_get(op_time *now){
+# ifdef OP_HAVE_CLOCK_GETTIME
+  /*Prefer a monotonic clock that continues to increment during suspend.*/
+#  ifdef CLOCK_BOOTTIME
+  if(clock_gettime(CLOCK_BOOTTIME,now)!=0)
+#  endif
+#  ifdef CLOCK_MONOTONIC
+  if(clock_gettime(CLOCK_MONOTONIC,now)!=0)
+#  endif
+  OP_ALWAYS_TRUE(!clock_gettime(CLOCK_REALTIME,now));
+# else
+  ftime(now);
+# endif
+}
+
+static opus_int32 op_time_diff_ms(const op_time *_end, const op_time *_start){
+# ifdef OP_HAVE_CLOCK_GETTIME
   opus_int64 dtime;
+  dtime=_end->tv_sec-(opus_int64)_start->tv_sec;
+  OP_ASSERT(_end->tv_nsec<1000000000);
+  OP_ASSERT(_start->tv_nsec<1000000000);
+  if(OP_UNLIKELY(dtime>(OP_INT32_MAX-1000)/1000))return OP_INT32_MAX;
+  if(OP_UNLIKELY(dtime<(OP_INT32_MIN+1000)/1000))return OP_INT32_MIN;
+  return (opus_int32)dtime*1000+(_end->tv_nsec-_start->tv_nsec)/1000000;
+# else
+  opus_int64 dtime;
   dtime=_end->time-(opus_int64)_start->time;
   OP_ASSERT(_end->millitm<1000);
   OP_ASSERT(_start->millitm<1000);
@@ -1018,17 +1049,18 @@
   if(OP_UNLIKELY(dtime>(OP_INT32_MAX-1000)/1000))return OP_INT32_MAX;
   if(OP_UNLIKELY(dtime<(OP_INT32_MIN+1000)/1000))return OP_INT32_MIN;
   return (opus_int32)dtime*1000+_end->millitm-_start->millitm;
+# endif
 }
 
 /*Update the read rate estimate for this connection.*/
 static void op_http_conn_read_rate_update(OpusHTTPConn *_conn){
-  struct timeb read_time;
+  op_time      read_time;
   opus_int32   read_delta_ms;
   opus_int64   read_delta_bytes;
   opus_int64   read_rate;
   read_delta_bytes=_conn->read_bytes;
   if(read_delta_bytes<=0)return;
-  ftime(&read_time);
+  op_time_get(&read_time);
   read_delta_ms=op_time_diff_ms(&read_time,&_conn->read_time);
   read_rate=_conn->read_rate;
   read_delta_ms=OP_MAX(read_delta_ms,1);
@@ -2020,7 +2052,7 @@
 # define OP_NPROTOS (2)
 
 static int op_http_connect_impl(OpusHTTPStream *_stream,OpusHTTPConn *_conn,
- struct addrinfo *_addrs,struct timeb *_start_time){
+ struct addrinfo *_addrs,op_time *_start_time){
   struct addrinfo *addr;
   struct addrinfo *addrs[OP_NPROTOS];
   struct pollfd    fds[OP_NPROTOS];
@@ -2050,7 +2082,7 @@
   _stream->free_head=_conn->next;
   _conn->next=_stream->lru_head;
   _stream->lru_head=_conn;
-  ftime(_start_time);
+  op_time_get(_start_time);
   *&_conn->read_time=*_start_time;
   _conn->read_bytes=0;
   _conn->read_rate=0;
@@ -2152,14 +2184,14 @@
 }
 
 static int op_http_connect(OpusHTTPStream *_stream,OpusHTTPConn *_conn,
- struct addrinfo *_addrs,struct timeb *_start_time){
-  struct timeb     resolve_time;
+ struct addrinfo *_addrs,op_time *_start_time){
+  op_time          resolve_time;
   struct addrinfo *new_addrs;
   int              ret;
   /*Re-resolve the host if we need to (RFC 6555 says we MUST do so
      occasionally).*/
   new_addrs=NULL;
-  ftime(&resolve_time);
+  op_time_get(&resolve_time);
   if(_addrs!=&_stream->addr_info||op_time_diff_ms(&resolve_time,
    &_stream->resolve_time)>=OP_RESOLVE_CACHE_TIMEOUT_MS){
     new_addrs=op_resolve(_stream->connect_host,_stream->connect_port);
@@ -2310,8 +2342,8 @@
   addrs=NULL;
   for(nredirs=0;nredirs<OP_REDIRECT_LIMIT;nredirs++){
     OpusParsedURL  next_url;
-    struct timeb   start_time;
-    struct timeb   end_time;
+    op_time        start_time;
+    op_time        end_time;
     char          *next;
     char          *status_code;
     int            minor_version_pos;
@@ -2445,7 +2477,7 @@
     if(OP_UNLIKELY(ret<0))return ret;
     ret=op_http_conn_read_response(_stream->conns+0,&_stream->response);
     if(OP_UNLIKELY(ret<0))return ret;
-    ftime(&end_time);
+    op_time_get(&end_time);
     next=op_http_parse_status_line(&v1_1_compat,&status_code,
      _stream->response.buf);
     if(OP_UNLIKELY(next==NULL))return OP_FALSE;
@@ -2857,8 +2889,8 @@
                 converted into a request for the rest.*/
 static int op_http_conn_open_pos(OpusHTTPStream *_stream,
  OpusHTTPConn *_conn,opus_int64 _pos,opus_int32 _chunk_size){
-  struct timeb  start_time;
-  struct timeb  end_time;
+  op_time       start_time;
+  op_time       end_time;
   opus_int32    connect_rate;
   opus_int32    connect_time;
   int           ret;
@@ -2868,7 +2900,7 @@
   if(OP_UNLIKELY(ret<0))return ret;
   ret=op_http_conn_handle_response(_stream,_conn);
   if(OP_UNLIKELY(ret!=0))return OP_FALSE;
-  ftime(&end_time);
+  op_time_get(&end_time);
   _stream->cur_conni=(int)(_conn-_stream->conns);
   OP_ASSERT(_stream->cur_conni>=0&&_stream->cur_conni<OP_NCONNS_MAX);
   /*The connection has been successfully opened.
@@ -3120,7 +3152,7 @@
 }
 
 static int op_http_stream_seek(void *_stream,opus_int64 _offset,int _whence){
-  struct timeb     seek_time;
+  op_time          seek_time;
   OpusHTTPStream  *stream;
   OpusHTTPConn    *conn;
   OpusHTTPConn   **pnext;
@@ -3163,7 +3195,7 @@
     op_http_conn_read_rate_update(stream->conns+ci);
     *&seek_time=*&stream->conns[ci].read_time;
   }
-  else ftime(&seek_time);
+  else op_time_get(&seek_time);
   /*If we seeked past the end of the stream, just disable the active
      connection.*/
   if(pos>=content_length){