From patchwork Mon Mar 21 15:08:36 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: 52174 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 8795E385F00F for ; Mon, 21 Mar 2022 15:12:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8795E385F00F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1647875566; bh=lsBezaq0ge2tFXmLGpOE1WWsl36U1mIE9TwqKHYaedk=; 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=JCEVCM48TkVfhhwqBsPAcAlKar8M0eyqjKXqeeqmzF76SiJsIHklbGsgpF4OAFeuf fMdndLSNy7vNjiFeaqeJkYZVUaxRpN7Xs1s75Wt1unjlW5R0uMAJElvXeDAAffRnIC 32zojeLJQqzyWUKTwIYg997jGKiCveXgifxorKmE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id 6BDC9386F812 for ; Mon, 21 Mar 2022 15:08:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6BDC9386F812 Received: by mail-oi1-x235.google.com with SMTP id n7so16509314oif.5 for ; Mon, 21 Mar 2022 08:08:53 -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=lsBezaq0ge2tFXmLGpOE1WWsl36U1mIE9TwqKHYaedk=; b=XLYtTYxtgHpKn/n9sBllOltC+N//vTBfvMWFLI2dsRu4hkwXbHjk9M+yG/tUUmhxS6 Jyn68hObaOyrd/sDE+ZTsw05Mje46gXnXqkOZeg+LI6vUXqE39mDa1LJ+DX3N7h24MS6 H4yR3bfyt2hcWfmQhIntctLyUh+j5TVThSN0tsHS5P0GCJxebuQBfrVIPOVnIGZw0zZS RHS5IE4ylxMj/G1VjdVG+Oset2c/gThGh/oHrOyiGay9cTjX5ylCqIGQUnflHT46YLMD pqoNwX2Iv7nUMPyMR6dsiRgNfFako54Lm9XOjlGVzRW91LMP/xTPYuBRqEE0sGo2AHy8 9KmQ== X-Gm-Message-State: AOAM532lbrUPo1zev0l8yh2rnTjlmVDHL2aF1kpDa52rxWFFG0ClXFNC G3wrosnibwg/uuLQfHglAw8HxJwdwSfQKQ== X-Google-Smtp-Source: ABdhPJzCqQ0eVboKA8BQZQBW6pU+G2PI9KxtfP8X1XfhrlbaEmqEnOG9Es+Z55PoYmkfaVNgJKcMmQ== X-Received: by 2002:a05:6808:314:b0:2ec:e160:ef24 with SMTP id i20-20020a056808031400b002ece160ef24mr14538112oie.196.1647875332367; Mon, 21 Mar 2022 08:08:52 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:f04a:67c7:cbf3:571d]) by smtp.gmail.com with ESMTPSA id o64-20020acad743000000b002ef3b249b9esm3859458oig.58.2022.03.21.08.08.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 08:08:51 -0700 (PDT) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v4 5/7] misc: syslog: Use fixed-sized buffer and remove memstream Date: Mon, 21 Mar 2022 12:08:36 -0300 Message-Id: <20220321150838.898597-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321150838.898597-1-adhemerval.zanella@linaro.org> References: <20220321150838.898597-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 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. Also, since the buffer size is know, memstream is replaced with a malloced buffer for larger messages. Checked on x86_64-linux-gnu. --- misc/syslog.c | 86 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 20f46a76a2..b184b15eea 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -123,12 +123,12 @@ void __vsyslog_internal(int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - FILE *f; - char *buf = 0; + /* Try to use a static buffer as an optimization. */ + char bufs[1024]; + char *buf = NULL; size_t bufsize = 0; 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,43 +152,61 @@ __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; + + /* "%b %e %H:%M:%S " */ + char timestamp[sizeof "MMM DD hh:mm:ss "]; + time_t now = time_now (); + struct tm now_tm; + __localtime_r (&now, &now_tm); + __strftime_l (timestamp, sizeof timestamp, "%b %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) + + int l = __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + if (0 <= l && l < sizeof bufs) { - __fsetlocking (f, FSETLOCKING_BYCALLER); - /* "%b %e %H:%M:%S" */ - char timebuf[sizeof "MMM DD hh:mm:ss "]; - time_t now = time_now (); - struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timebuf, sizeof (timebuf), "%b %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); - - /* Close the memory stream; this will finalize the data into a malloc'd - buffer in BUF. */ - fclose (f); + int vl = __vsnprintf_internal (bufs + l, sizeof bufs - l, fmt, apc, + mode_flags); + if (0 <= vl && vl < sizeof bufs - l) + { + buf = bufs; + bufsize = l + vl; + } - /* Tell the cancellation handler to free this buffer. */ - clarg.buf = buf; + va_end (apc); } - else + + if (buf == NULL) { - /* Nothing much to do but emit an error message. */ - bufsize = __snprintf (failbuf, sizeof failbuf, "out of memory[%d]", - __getpid ()); - buf = failbuf; + buf = malloc (l * sizeof (char)); + if (buf != NULL) + { + /* Tell the cancellation handler to free this buffer. */ + clarg.buf = buf; + + __snprintf (buf, sizeof buf, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + } + else + { + /* Nothing much to do but emit an error message. */ + bufsize = __snprintf (bufs, sizeof bufs, + "out of memory[%d]", __getpid ()); + buf = bufs; + } } /* Output to stderr if requested. */ @@ -240,7 +258,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 != bufs) free (buf); }