From patchwork Fri Mar 18 16:52:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 52124 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3840E388451F for ; Fri, 18 Mar 2022 16:57:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3840E388451F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647622659; bh=gg4cct4f5cKlFK/5g0VvI/HdFpXDSpHBkJPEyu/IIDA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=hgOnbdsc/iEr5DHhXZXHEKq8K0oVEAZT/3h8Bx4lpzTRDOscUA55F2c79GMXMpO3b IVfzp0XbwJVPdb4pB/9XLml06n7DBoaOno2XkD+18UaM4wqyAiC1zlBEyTpcvN4Pny LBsmjzEFQXdbYxetMsFD/1ZQYC3KhoBEC4wkJoyo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by sourceware.org (Postfix) with ESMTPS id A24273853804 for ; Fri, 18 Mar 2022 16:52:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A24273853804 Received: by mail-ot1-x330.google.com with SMTP id z9-20020a05683020c900b005b22bf41872so5916853otq.13 for ; Fri, 18 Mar 2022 09:52:32 -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=gg4cct4f5cKlFK/5g0VvI/HdFpXDSpHBkJPEyu/IIDA=; b=K6wszxQLGQofviQ9JmzQUdTD98+DTR/l10KRnsk8TK3GYmGPY78vVBhrdFNngKfAmi hjIx8spJtMU9CsoEKIqnIxzUEB4hvfYaWF3QKrg1c+MSr8B3rb8/FC3OLBTgnESUcUEg TA5T/z7l4zDuVPretOqVPvutoR6NHdQ79Vop2Adr9f5uvOPz8UMRIbBiFCyb85KRdwIF oMgg27BCjmbFkv3f+jB/HzlGe70lRn52ECh8fIfpQwfXR3SF2NgT+WO99hxBQmnIhHWI d4RnQAsj5uroCgWZc8YXmmByXtGgV9REZLXNG5MN04HBLAKgzQCtlgV9HbgJ3GHmAxQz tcUA== X-Gm-Message-State: AOAM531eK7V7kfDZCPDUwiJu5AfY8Xi8w9ck4lIxpwhkkz1giKvO63Nw C4JcoLwEtXKkj7BBgTCfpA/f3qtPikxXNA== X-Google-Smtp-Source: ABdhPJyqhydSy8dhuSfuO4kuYlYN60jabdWuN7zmt0dIvYusKmvCo3iblrw8VlexwZjFj26AHvgZrw== X-Received: by 2002:a9d:5614:0:b0:5b2:5fe3:8f1c with SMTP id e20-20020a9d5614000000b005b25fe38f1cmr3623537oti.229.1647622351622; Fri, 18 Mar 2022 09:52:31 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:99a3:99e2:1060:da92:ae49]) by smtp.gmail.com with ESMTPSA id c12-20020a9d75cc000000b005b24b061940sm3986417otl.33.2022.03.18.09.52.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Mar 2022 09:52:31 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v3 7/7] misc: Use gmtime instead of localtime Date: Fri, 18 Mar 2022 13:52:14 -0300 Message-Id: <20220318165214.2291065-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220318165214.2291065-1-adhemerval.zanella@linaro.org> References: <20220318165214.2291065-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 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-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 7852441615..997e423228 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -161,11 +161,25 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* "%h %e %H:%M:%S " */ char timestamp[timestamp_size]; - time_t now = time_now (); + __time64_t now = time64_now (); + + /* 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. */ struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timestamp, sizeof timestamp, "%h %e %T ", &now_tm, - _nl_C_locobj_ptr); + bool has_ts = __gmtime64_r (&now, &now_tm) != NULL; + + /* In the highly unlike case of gmtime_r failure (the clock being + INT_MIN + 1900 or follow INT_MAX + 1900) we skip the hostname so the + message is handl 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_tm, + _nl_C_locobj_ptr); #define SYSLOG_HEADER(__pri, __timestamp, __msgoff, pid) \ "<%d>%s %n%s%s%.0d%s: ", \ @@ -173,10 +187,18 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, LogTag == NULL ? __progname : LogTag, \ pid != 0 ? "[" : "", pid, pid != 0 ? "]" : "" +#define SYSLOG_HEADER_WITHOUT_TS(__pri, __msgoff) \ + "<%d>: %n", __pri, __msgoff + /* Try to use a static buffer as an optimization. */ char bufs[bufs_size]; - int l = __snprintf (bufs, sizeof bufs, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + 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 (l < sizeof (bufs)) { va_list apc; @@ -204,7 +226,10 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, if (f != NULL) { __fsetlocking (f, FSETLOCKING_BYCALLER); - fprintf (f, SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + if (has_ts) + fprintf (f, SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + else + fprintf (f, SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); /* Restore errno for %m format. */ __set_errno (saved_errno); __vfprintf_internal (f, fmt, ap, mode_flags);