public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org, Paul Eggert <eggert@cs.ucla.edu>
Subject: [PATCH v4 7/7] misc: Use gmtime instead of localtime
Date: Mon, 21 Mar 2022 12:08:38 -0300	[thread overview]
Message-ID: <20220321150838.898597-8-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20220321150838.898597-1-adhemerval.zanella@linaro.org>

We deviate from RFC3164 which states timestamp should be in localtime
because although our __localtime_r does not set tzname, the relay still
might still use localtime (which does) and if the server timezone changes
it might result in wrong timestamp from client.  It still does not help
if a process switches its TZ setting from a timezone that has leap
seconds, to one that doesn't (or vice versa), but this would incur in
other problems.

It also handles the highly unlikely case where gmtime might return NULL,
in this case only the PRI is set to hopefully instruct the relay to
get eh TIMESTAMP (as defined by the RFC).

Finally it also uses internally the 64 bit time_t interfaces (to avoid
y2038 issues on 32 bit legacy architectures).

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 misc/syslog.c | 43 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 8 deletions(-)

diff --git a/misc/syslog.c b/misc/syslog.c
index b184b15eea..62a69cf41a 100644
--- a/misc/syslog.c
+++ b/misc/syslog.c
@@ -156,11 +156,26 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap,
 
   /* "%b %e %H:%M:%S "  */
   char timestamp[sizeof "MMM DD hh:mm:ss "];
-  time_t now = time_now ();
+  __time64_t now = time64_now ();
+
+  /* Deviate from RFC3164 which states timestamp should be in localtime
+     because although  __localtime_r does not set tzname, the relay might
+     still use localtime (which does) and if the server timezone changes
+     it might result in wrong timestamp from client.  It still does not help
+     if a process switches its TZ setting from a timezone that has leap
+     seconds, to one that doesn't (or vice versa), but this would incur in
+     other problems as well.  */
   struct tm now_tm;
-  __localtime_r (&now, &now_tm);
-  __strftime_l (timestamp, sizeof timestamp, "%b %e %T ", &now_tm,
-                _nl_C_locobj_ptr);
+  struct tm *now_tmp = __gmtime64_r (&now, &now_tm);
+  bool has_ts = now_tmp != NULL;
+
+  /* In the unlikely case of gmtime_r failure (tm_year out of int range)
+     skip the hostname so the message is handled as valid PRI but without
+     TIMESTAMP or invalid TIMESTAMP (which should force the relay to add the
+     timestamp itself).  */
+  if (has_ts)
+    __strftime_l (timestamp, sizeof timestamp, "%h %e %T ", now_tmp,
+		  _nl_C_locobj_ptr);
 
 #define SYSLOG_HEADER(__pri, __timestamp, __msgoff, pid) \
   "<%d>%s %n%s%s%.0d%s: ",                               \
@@ -168,8 +183,16 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap,
   LogTag == NULL ? __progname : LogTag,                  \
   "[" + (pid == 0), pid, "]" + (pid == 0)
 
-  int l = __snprintf (bufs, sizeof bufs,
-                      SYSLOG_HEADER (pri, timestamp, &msgoff, pid));
+#define SYSLOG_HEADER_WITHOUT_TS(__pri, __msgoff)        \
+  "<%d>: %n", __pri, __msgoff
+
+  int l;
+  if (has_ts)
+    l = __snprintf (bufs, sizeof bufs,
+		    SYSLOG_HEADER (pri, timestamp, &msgoff, pid));
+  else
+    l = __snprintf (bufs, sizeof bufs,
+		    SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff));
   if (0 <= l && l < sizeof bufs)
     {
       va_list apc;
@@ -197,8 +220,12 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap,
 	  /* Tell the cancellation handler to free this buffer.  */
 	  clarg.buf = buf;
 
-	  __snprintf (buf, sizeof buf,
-		      SYSLOG_HEADER (pri, timestamp, &msgoff, pid));
+	  if (has_ts)
+	    __snprintf (bufs, sizeof bufs,
+			SYSLOG_HEADER (pri, timestamp, &msgoff, pid));
+	  else
+	    __snprintf (bufs, sizeof bufs,
+			SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff));
 	}
       else
         {
-- 
2.32.0


  parent reply	other threads:[~2022-03-21 15:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-21 15:08 [PATCH v4 0/7] Refactor syslog implementation Adhemerval Zanella
2022-03-21 15:08 ` [PATCH v4 1/7] support: Add xmkfifo Adhemerval Zanella
2022-03-21 15:08 ` [PATCH v4 2/7] misc: Add syslog test Adhemerval Zanella
2022-03-21 15:08 ` [PATCH v4 3/7] misc: syslog: Fix indentation and style Adhemerval Zanella
2022-03-21 15:08 ` [PATCH v4 4/7] misc: syslog: Simplify implementation Adhemerval Zanella
2022-03-21 15:08 ` [PATCH v4 5/7] misc: syslog: Use fixed-sized buffer and remove memstream Adhemerval Zanella
2022-03-21 15:08 ` [PATCH v4 6/7] misc: syslog: Move SYSLOG_NAME to USE_MISC (BZ #16355) Adhemerval Zanella
2022-03-21 15:08 ` Adhemerval Zanella [this message]
2022-03-21 17:58 ` [PATCH v4 0/7] Refactor syslog implementation Paul Eggert
2022-03-21 20:16   ` Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220321150838.898597-8-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=eggert@cs.ucla.edu \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).