]> jfr.im git - irc/ircd-hybrid/hopm.git/commitdiff
- Use monotonic time whenever possible
authormichael <redacted>
Sat, 2 Jan 2021 18:38:36 +0000 (18:38 +0000)
committermichael <redacted>
Sat, 2 Jan 2021 18:38:36 +0000 (18:38 +0000)
git-svn-id: svn://svn.ircd-hybrid.org/svnroot/hopm/branches/1.1.x@9861 82007160-df01-0410-b94d-b575c5fd34c7

src/Makefile.am
src/firedns.c
src/irc.c
src/libopm/src/Makefile.am
src/libopm/src/libopm.c
src/libopm/src/opm_gettime.c [new file with mode: 0644]
src/libopm/src/opm_gettime.h [new file with mode: 0644]
src/negcache.c
src/opercmd.c
src/stats.c

index 858547d5b52662f692a5ee46dcc3bfe903ba0253..826cb8c51d7a112b1311a283a9e84a6cf6991593 100644 (file)
@@ -1,7 +1,7 @@
 SUBDIRS = libopm
 bin_PROGRAMS = hopm
 
-AM_CPPFLAGS = -DHOPM_PREFIX="\"$(prefix)\"" -DHOPM_ETCDIR="\"$(sysconfdir)\"" -DHOPM_LOGDIR="\"$(localstatedir)/log\"" -DHOPM_BINPATH="\"$(bindir)/hopm\""
+AM_CPPFLAGS = -DHOPM_PREFIX="\"$(prefix)\"" -DHOPM_ETCDIR="\"$(sysconfdir)\"" -DHOPM_LOGDIR="\"$(localstatedir)/log\"" -DHOPM_BINPATH="\"$(bindir)/hopm\"" -I$(top_srcdir)/src/libopm/src/
 AM_YFLAGS = -d
 
 hopm_SOURCES = compat.c        \
index bcee5dab3f5ab99ca50585dfaec58e4914a6be55..81bf214436a84af8d365395cb545f80571431962 100644 (file)
@@ -43,6 +43,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #include "list.h"
 #include "log.h"
 #include "dnsbl.h"
+#include "opm_gettime.h"
 
 #define FIREDNS_TRIES 3
 
@@ -602,7 +603,7 @@ firedns_send_requests(struct s_header *h, struct s_connection *s, int l)
     return -1;
   }
 
-  time(&s->start);
+  s->start = opm_gettime();
   firedns_fdinuse++;
   firedns_errno = FDNS_ERR_NONE;
 
@@ -799,7 +800,7 @@ firedns_cycle(void)
   if (ufds == NULL)
     ufds = xcalloc(sizeof(*ufds) * OptionsItem.dns_fdlimit);
 
-  time(&timenow);
+  timenow = opm_gettime();
 
   LIST_FOREACH_SAFE(node, node_next, CONNECTIONS.head)
   {
index efe9daaf22017d01ced1aec15cd1c0b71108e9d9..920a71a01fafefed4fbb897b6410af730c0dd760 100644 (file)
--- a/src/irc.c
+++ b/src/irc.c
@@ -54,6 +54,7 @@
 #include "negcache.h"
 #include "memory.h"
 #include "main.h"
+#include "opm_gettime.h"
 
 
 /*
@@ -657,20 +658,16 @@ irc_close(void)
 static void
 irc_connect(void)
 {
-  time_t present;
-
-  time(&present);
-
   /* Only try to reconnect every IRCItem.reconnectinterval seconds */
-  if ((present - IRC_LASTRECONNECT) < IRCItem.reconnectinterval)
+  if ((opm_gettime() - IRC_LASTRECONNECT) < IRCItem.reconnectinterval)
   {
     /* Sleep to avoid excessive CPU */
     sleep(1);
     return;
   }
 
-  time(&IRC_LASTRECONNECT);
-  time(&IRC_LAST);
+  IRC_LASTRECONNECT =
+  IRC_LAST = opm_gettime();
 
   irc_init();
 
@@ -714,7 +711,7 @@ irc_connect(void)
            IRCItem.username,
            IRCItem.username,
            IRCItem.realname);
-  time(&IRC_LAST);
+  IRC_LAST = opm_gettime();
 }
 
 /* irc_parse
@@ -774,7 +771,7 @@ irc_parse(void)
   if (OPT_DEBUG >= 2)
     log_printf("IRC READ -> %s", IRC_RAW);
 
-  time(&IRC_LAST);
+  IRC_LAST = opm_gettime();
 
   /* Store a copy of IRC_RAW for the handlers (for functions that need PROOF) */
   strlcpy(msg, IRC_RAW, sizeof(msg));
@@ -1016,11 +1013,7 @@ irc_send_channels(const char *data, ...)
 void
 irc_timer(void)
 {
-  time_t present, delta;
-
-  time(&present);
-
-  delta = present - IRC_LAST;
+  time_t delta = opm_gettime() - IRC_LAST;
 
   /* No data in IRCItem.readtimeout seconds */
   if (delta >= IRCItem.readtimeout)
@@ -1029,7 +1022,7 @@ irc_timer(void)
     irc_close();
 
     /* Make sure we don't do this again for a while */
-    time(&IRC_LAST);
+    IRC_LAST = opm_gettime();
   }
   else if (delta >= IRCItem.readtimeout / 2)
   {
index 7726c075ba115a3d77807792ea4f457fb3c4c8bd..b0c95048750dbf0496c7f103f4ccd83aa3c7f596 100644 (file)
@@ -1,18 +1,20 @@
 noinst_LTLIBRARIES = libopm.la
 
-libopm_la_SOURCES = config.c     \
-                    config.h     \
-                    libopm.c     \
-                    libopm.h     \
-                    list.c       \
-                    list.h       \
-                    memory.c     \
-                    memory.h     \
-                    opm_common.h \
-                    opm_error.h  \
-                    opm.h        \
-                    opm_types.h  \
-                    proxy.c      \
+libopm_la_SOURCES = config.c      \
+                    config.h      \
+                    libopm.c      \
+                    libopm.h      \
+                    list.c        \
+                    list.h        \
+                    memory.c      \
+                    memory.h      \
+                    opm_common.h  \
+                    opm_error.h   \
+                    opm.h         \
+                    opm_gettime.c \
+                    opm_gettime.h \
+                    opm_types.h   \
+                    proxy.c       \
                     proxy.h
 
 libopm_la_LIBADD = @LTLIBOBJS@
index d6118ff33f19dda9a1b8991c0c5dcb3e5a3b5e42..430a29dbf5570eada5975657f465d7f17017e5d1 100644 (file)
@@ -44,6 +44,7 @@
 #include "opm_error.h"
 #include "opm_types.h"
 #include "opm_common.h"
+#include "opm_gettime.h"
 #include "list.h"
 #include "proxy.h"
 
@@ -826,7 +827,7 @@ libopm_check_closed(OPM_T *scanner)
   if (LIST_SIZE(&scanner->scans) == 0)
     return;
 
-  time(&present);
+  present = opm_gettime();
   timeout = *(int *)libopm_config(scanner->config, OPM_CONFIG_TIMEOUT);
 
   LIST_FOREACH_SAFE(node1, next1, scanner->scans.head)
@@ -964,7 +965,7 @@ libopm_do_connect(OPM_T * scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
 #endif
 
   conn->state = OPM_STATE_ESTABLISHED;
-  time(&conn->creation);  /* Stamp creation time, for timeout */
+  conn->creation = opm_gettime();  /* Stamp creation time, for timeout */
 }
 
 /* check_poll
diff --git a/src/libopm/src/opm_gettime.c b/src/libopm/src/opm_gettime.c
new file mode 100644 (file)
index 0000000..e2107b3
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ *  Copyright (c) 2014-2021 ircd-hybrid development team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
+ *  USA
+ */
+
+#include <stdlib.h>
+
+#include "opm_gettime.h"
+
+time_t
+opm_gettime(void)
+{
+  struct timespec ts;
+
+#ifdef CLOCK_MONOTONIC_RAW
+  if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == 0)
+#elif CLOCK_MONOTONIC
+  if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
+#else
+  if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
+#endif
+    return ts.tv_sec;
+
+  exit(EXIT_FAILURE);
+  return -1;
+}
diff --git a/src/libopm/src/opm_gettime.h b/src/libopm/src/opm_gettime.h
new file mode 100644 (file)
index 0000000..cba52fb
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ *  Copyright (c) 2014-2021 ircd-hybrid development team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
+ *  USA
+ */
+
+#ifndef OPM_GETTIME_H
+#define OPM_GETTIME_H
+
+#include <time.h>
+
+extern time_t opm_gettime(void);
+#endif
+
index 4a36b2a03670925a20825d89eae0f2b031945c51..9af957bef1a30f3f492fe2b596b3287952898cce 100644 (file)
@@ -37,6 +37,7 @@
 #include "config.h"
 #include "memory.h"
 #include "log.h"
+#include "opm_gettime.h"
 
 
 extern unsigned int OPT_DEBUG;
@@ -72,7 +73,7 @@ negcache_check(const char *ipstr)
   {
     struct negcache_item *n = pnode->data;
 
-    if (time(NULL) - n->seen <= OptionsItem.negcache)
+    if (opm_gettime() - n->seen <= OptionsItem.negcache)
       return n;
   }
 
@@ -91,7 +92,7 @@ negcache_insert(const char *ipstr)
     return;  /* Malformed IP address or already added to the trie */
 
   struct negcache_item *n = xcalloc(sizeof(*n));
-  n->seen = time(NULL);
+  n->seen = opm_gettime();
 
   pnode->data = n;
   list_add(pnode, &n->node, &negcache_list);
@@ -104,13 +105,14 @@ void
 negcache_rebuild(void)
 {
   node_t *node, *node_next;
+  time_t present = opm_gettime();
 
   LIST_FOREACH_SAFE(node, node_next, negcache_list.head)
   {
     patricia_node_t *pnode = node->data;
     struct negcache_item *n = pnode->data;
 
-    if (n->seen + OptionsItem.negcache < time(NULL))
+    if (n->seen + OptionsItem.negcache < present)
     {
       if (OPT_DEBUG >= 2)
         log_printf("NEGCACHE -> Deleting expired negcache node for %s added at %lu",
index 8cc6bf751c891994f2156db2af0877acfa24b8ca..9807b30894cb58974e354e3525c6fe26370b6edd 100644 (file)
@@ -35,6 +35,7 @@
 #include "scan.h"
 #include "memory.h"
 #include "stats.h"
+#include "opm_gettime.h"
 
 
 static list_t COMMANDS;  /* List of active commands */
@@ -108,8 +109,7 @@ command_create(const struct OperCommandHash *tab, const char *param, const char
   command->tab = tab;
   command->irc_nick = xstrdup(irc_nick);
   command->target = target;
-
-  time(&command->added);
+  command->added = opm_gettime();
 
   return command;
 }
@@ -226,7 +226,7 @@ command_timer(void)
 {
   static unsigned int interval;
   node_t *node, *node_next;
-  time_t present;
+  time_t present = opm_gettime();
 
   /* Only perform command removal every OptionsItem.command_interval seconds */
   if (interval++ < OptionsItem.command_interval)
@@ -234,8 +234,6 @@ command_timer(void)
   else
     interval = 0;
 
-  time(&present);
-
   LIST_FOREACH_SAFE(node, node_next, COMMANDS.head)
   {
     struct Command *command = node->data;
index dd0ac8cbb06ddb65db15cec34f3e4eb40b1ac777..85d48929e1e848732f6f9f64b393bdb55548485f 100644 (file)
@@ -34,7 +34,8 @@
 #include "misc.h"
 #include "config.h"
 #include "stats.h"
-#include "libopm/src/opm_types.h"
+#include "opm_types.h"
+#include "opm_gettime.h"
 
 static time_t STATS_UPTIME;
 static unsigned int STATS_CONNECTIONS;
@@ -67,7 +68,7 @@ static struct StatsHash STATS_PROXIES[] =
 void
 stats_init(void)
 {
-  time(&STATS_UPTIME);
+  STATS_UPTIME = opm_gettime();
 }
 
 /* stats_openproxy
@@ -147,13 +148,9 @@ stats_dnsblsend(void)
 void
 stats_output(const char *target)
 {
-  time_t present;
-  time_t uptime;
+  time_t uptime = opm_gettime() - STATS_UPTIME;
   node_t *p;
 
-  time(&present);
-  uptime = present - STATS_UPTIME;
-
   irc_send("PRIVMSG %s :Uptime: %s", target, time_dissect(uptime));
 
   LIST_FOREACH(p, OpmItem.blacklists.head)