From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oo1-xc2b.google.com (mail-oo1-xc2b.google.com [IPv6:2607:f8b0:4864:20::c2b]) by sourceware.org (Postfix) with ESMTPS id BC955393BC36 for ; Mon, 21 Mar 2022 21:19:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BC955393BC36 Received: by mail-oo1-xc2b.google.com with SMTP id u30-20020a4a6c5e000000b00320d8dc2438so20665913oof.12 for ; Mon, 21 Mar 2022 14:19:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KUYhm0gc+Y2uof8ds0YJYNMonvdtWyZ4uI7g8EatBss=; b=yRq5fbgaDqeDOzDcKRKMBQWX4f6FuZ+MqinYX3/6NU/Zt8/6seXPapGdkOPvHCYWv8 zuJX2ohP1U1O8FbgdaOC4SSBdtm7Zul/Jngxv0Ut8Rp8Bx02gHmRXXuEeafiGw3I1/Q+ l9ALwEyw1WOBAt3N5kxOXTq8EhQHiXhu6sazH/RwarCoD8QZXR5RcvH0EfnLmLx7O6if U/NDK2qqJQSgVgT+bBugjQQbN1FeMi2H1lU4SaG4DfrwDwJ+tN53PFin+yStMU7LmvF0 xlq+pzedBunu/xPJ8V+CzdoZ62TRxu8HbG1YnbhNIROCoMl+3RTPdmUne9Rjaxbe6Xz9 vt5g== X-Gm-Message-State: AOAM5310aL/6aJd+99A12o/YvBVc7pNX+PCJ9+HknzBo4d7nye7b88tt IMzkE4Ksqm/Z82v+qtk5TkDUXFZ2o5OqZg== X-Google-Smtp-Source: ABdhPJx1TM+36PeQ9ZVGiskEt5PnvRiaTbAqjTGKapI6O7HRHqvFJhgr4azvVAVcgncx00j7QJ7/BQ== X-Received: by 2002:a05:6870:17a9:b0:dd:c79d:21ec with SMTP id r41-20020a05687017a900b000ddc79d21ecmr424575oae.24.1647897569726; Mon, 21 Mar 2022 14:19:29 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:d5f9:2c96:1f30:1f58]) by smtp.gmail.com with ESMTPSA id c37-20020a9d27a8000000b005b23cf22e23sm7925256otb.42.2022.03.21.14.19.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 14:19:29 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v5 7/7] misc: Use 64 bit time_t interfaces on syslog Date: Mon, 21 Mar 2022 18:19:13 -0300 Message-Id: <20220321211913.2347924-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> References: <20220321211913.2347924-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Mar 2022 21:19:32 -0000 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. --- 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 { -- 2.32.0