public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Adhemerval Zanella <azanella@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] misc: Use 64 bit time_t interfaces on syslog
Date: Fri, 15 Apr 2022 14:00:38 +0000 (GMT)	[thread overview]
Message-ID: <20220415140038.09E2F3856246@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=ac0d208b54fe78ff378afce8003467c763c6a74a

commit ac0d208b54fe78ff378afce8003467c763c6a74a
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Fri Mar 18 10:10:53 2022 -0300

    misc: Use 64 bit time_t interfaces on syslog
    
    It also handles the highly unlikely case where localtime 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).
    
    Checked on x86_64-linux-gnu and i686-linux-gnu.

Diff:
---
 misc/syslog.c | 35 +++++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/misc/syslog.c b/misc/syslog.c
index 1b18375c95..554089bfc4 100644
--- a/misc/syslog.c
+++ b/misc/syslog.c
@@ -153,11 +153,18 @@ __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 ();
   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 = __localtime64_r (&now, &now_tm);
+  bool has_ts = now_tmp != NULL;
+
+  /* In the unlikely case of localtime_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: ",                               \
@@ -165,8 +172,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;
@@ -194,8 +209,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
         {


                 reply	other threads:[~2022-04-15 14:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220415140038.09E2F3856246@sourceware.org \
    --to=azanella@sourceware.org \
    --cc=glibc-cvs@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).