From patchwork Fri Mar 18 16:52:12 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: 52122 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 BD5F03888C7D for ; Fri, 18 Mar 2022 16:56:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD5F03888C7D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647622575; bh=GHP2uCDonRT5MWelQPwXrARsb1GI5BKfCnXP0rT8IOc=; 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=MKEMWPnL/iU9UNcxwP6u7MtMxA/oiRfMGuiY1XaaVzE3JtUK3p4dl3JWH2HbP32KZ Lsx+dO7jwJU7/QtRKKY0BXr+VIyYUXsvMnJp3jz1U+G8KwkQcixk2/gn9GdAv6PVhn U81isaRX7iuMMlDtYr4PklZ7eivbGmHNf6UBtLpc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 2F8923888C79 for ; Fri, 18 Mar 2022 16:52:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2F8923888C79 Received: by mail-ot1-x331.google.com with SMTP id o106-20020a9d2273000000b005b21f46878cso5946080ota.3 for ; Fri, 18 Mar 2022 09:52:29 -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=GHP2uCDonRT5MWelQPwXrARsb1GI5BKfCnXP0rT8IOc=; b=tzjcHRZ5p8KNN1zS/MvOuwPKfdV3qItY0WZ8yiPEq/TQC01ffZnNAQtdT6RpHDUQQn pzkUEayA9JQPXGvjTXY5/+NSagsV/5kbZYjDj8xNTpT1nwejKQ6t1AURjKR+llaboqP8 RPIV9XzvdOvXyWnoFZkuMD2liTdmGF4tMTTxSfU4FK+5PDGjQJqxJeTPNY2GOSRAiAMw 13zvwJSdoWCXtKzd2imT43WbKVc/fWp+MSvewPbmzw+H0WNt6qUj9S2dTISPQROXXdNf zP8f5qsY0J4jNgitEzNdaOc7PX2R+D+Idobe03NoHu4spyBPD/+g60GUzf8SYGKk63RV smEg== X-Gm-Message-State: AOAM533HGQeOOXtiQif9jvrwn1jo2k2ZDssNkdN+6GwyT02onpOZ7UnG zq0cIE2J+XYVCHvfNflfIjD/7O/mHeSojw== X-Google-Smtp-Source: ABdhPJwbwjvAPeVgna6W/yPC0r9CuftHhBtGXmQyy0qOOH4ZbGLbOTqmF/CXmBw0PHUhm3oifV2eTg== X-Received: by 2002:a05:6830:108d:b0:5b2:38b1:f39c with SMTP id y13-20020a056830108d00b005b238b1f39cmr3603300oto.316.1647622348145; Fri, 18 Mar 2022 09:52:28 -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.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Mar 2022 09:52:27 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v3 5/7] misc: syslog: Use fixed-sized buffer Date: Fri, 18 Mar 2022 13:52:12 -0300 Message-Id: <20220318165214.2291065-6-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" A fixed-sized buffer is used instead of memstream for messages up to 1024 bytes to avoid the potential BUFSIZ (8K) malloc and free for each syslog call. The memstream is still used as fallback for larger messages. Checked on x86_64-linux-gnu. --- misc/syslog.c | 102 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 37 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index e8b1dfe9b8..7852441615 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -123,12 +123,11 @@ void __vsyslog_internal(int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - FILE *f; - char *buf = 0; + char *buf = NULL; size_t bufsize = 0; + bool buf_malloced = false; int msgoff; int saved_errno = errno; - char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID /* Check for invalid bits. */ @@ -152,46 +151,75 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, if ((pri & LOG_FACMASK) == 0) pri |= LogFacility; - /* Build the message in a memory-buffer stream. */ - f = __open_memstream (&buf, &bufsize); - if (f != NULL) + pid_t pid = LogStat & LOG_PID ? __getpid () : 0; + + enum + { + timestamp_size = sizeof "MMM DD hh:mm:ss ", + bufs_size = 1024 + }; + + /* "%h %e %H:%M:%S " */ + char timestamp[timestamp_size]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __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: ", \ + __pri, __timestamp, __msgoff, \ + LogTag == NULL ? __progname : LogTag, \ + pid != 0 ? "[" : "", pid, pid != 0 ? "]" : "" + + /* 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)); + if (l < sizeof (bufs)) { - __fsetlocking (f, FSETLOCKING_BYCALLER); - /* "%h %e %H:%M:%S" */ - char timebuf[3+1 /* "%h " */ - + 2+1 /* "%e " */ - + 2+1 + 2+1 + 2+1 /* "%T" */]; - time_t now = time_now (); - struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timebuf, sizeof (timebuf), "%h %e %T", &now_tm, - _nl_C_locobj_ptr); - - pid_t pid = LogStat & LOG_PID ? __getpid () : 0; - - fprintf (f, "<%d>%s %n%s%s%.0d%s: ", pri, timebuf, &msgoff, - LogTag == NULL ? __progname : LogTag, - pid != 0 ? "[" : "", pid, pid != 0 ? "]" : ""); + va_list apc; + va_copy (apc, ap); + /* Restore errno for %m format. */ __set_errno (saved_errno); - /* We have the header. Print the user's format into the buffer. */ - __vfprintf_internal (f, fmt, ap, mode_flags); + int vl = __vsnprintf_internal (bufs + l, sizeof bufs - l, fmt, apc, + mode_flags); + if (l + vl < sizeof bufs) + { + buf = bufs; + bufsize = l + vl; + } - /* Close the memory stream; this will finalize the data into a malloc'd - buffer in BUF. */ - fclose (f); - - /* Tell the cancellation handler to free this buffer. */ - clarg.buf = buf; + va_end (apc); } - else + + /* If the required size is larger than buffer size fallbacks to + open_memstream. */ + if (buf == NULL) { - /* We cannot get a stream. There is not much we can do but emitting an - error messages. */ - bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", - __getpid ()); - buf = failbuf; + FILE *f = __open_memstream (&buf, &bufsize); + if (f != NULL) + { + __fsetlocking (f, FSETLOCKING_BYCALLER); + fprintf (f, SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + /* Restore errno for %m format. */ + __set_errno (saved_errno); + __vfprintf_internal (f, fmt, ap, mode_flags); + fclose (f); + + /* Tell the cancellation handler to free this buffer. */ + buf_malloced = true; + clarg.buf = buf; + } + else + { + bufsize = __snprintf (bufs, sizeof bufs, + "out of memory[%d]", __getpid ()); + buf = bufs; + } } /* Output to stderr if requested. */ @@ -243,7 +271,7 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, __libc_cleanup_pop (0); __libc_lock_unlock (syslog_lock); - if (buf != failbuf) + if (buf_malloced) free (buf); }