From patchwork Fri Dec 16 20:15:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 62025 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 8E9E6382B3FB for ; Fri, 16 Dec 2022 20:18:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E9E6382B3FB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671221890; bh=8WyJJ86R1AhpN2eV3/a8k00uqVXUbn36uyxrwTykiIQ=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=xUm0Wp2t8RcWUJuHp3whQqckontcxTOL++BxDgI/W4DVGakL1bEetGLfbcKabYQjK 2EFBiqNX+bvS1SjHRzSJUV5Yys7p59lWzyYM/3wBTrffUPNqRaTltN/swpRfPSkwAW m9ZsnIwSlKj+0f0v/2i7pYjwd9RCMTB75fHoRZ4M= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4BA91383FF57 for ; Fri, 16 Dec 2022 20:15:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4BA91383FF57 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-616-Fe4W7eC6Nn2TUtRmq_YI6g-1; Fri, 16 Dec 2022 15:15:47 -0500 X-MC-Unique: Fe4W7eC6Nn2TUtRmq_YI6g-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8953D1C07545 for ; Fri, 16 Dec 2022 20:15:47 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.61]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B646A51EF for ; Fri, 16 Dec 2022 20:15:46 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v6 09/11] libio: Convert __vdprintf_internal to buffers In-Reply-To: References: X-From-Line: 83fe8650c997632ed5cc3e8e072a542f00167452 Mon Sep 17 00:00:00 2001 Message-Id: <83fe8650c997632ed5cc3e8e072a542f00167452.1671221440.git.fweimer@redhat.com> Date: Fri, 16 Dec 2022 21:15:45 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The internal buffer size is set to 2048 bytes. This is less than the original BUFSIZ value used by buffered_vfprintf before the conversion, but it hopefully covers all cases where write boundaries matter. Reviewed-by: Adhemerval Zanella --- include/printf_buffer.h | 9 ++++ libio/iovdprintf.c | 67 +++++++++++++++++------------- stdio-common/printf_buffer_flush.c | 4 ++ 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/include/printf_buffer.h b/include/printf_buffer.h index c8e22e1e90..e2ca2d72d5 100644 --- a/include/printf_buffer.h +++ b/include/printf_buffer.h @@ -50,6 +50,7 @@ enum __printf_buffer_mode __printf_buffer_mode_sprintf_chk, __printf_buffer_mode_to_file, __printf_buffer_mode_asprintf, + __printf_buffer_mode_dprintf, __printf_buffer_mode_strfmon, __printf_buffer_mode_fp, /* For __printf_fp_l_buffer. */ __printf_buffer_mode_fp_to_wide, /* For __wprintf_fp_l_buffer. */ @@ -308,6 +309,9 @@ void __printf_buffer_flush_to_file (struct __printf_buffer_to_file *) struct __printf_buffer_asprintf; void __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *) attribute_hidden; +struct __printf_buffer_dprintf; +void __printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *) + attribute_hidden; struct __printf_buffer_fp; void __printf_buffer_flush_fp (struct __printf_buffer_fp *) attribute_hidden; @@ -341,4 +345,9 @@ void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *) (final, correctly sized) heap allocation. */ #define PRINTF_BUFFER_SIZE_ASPRINTF 200 +/* This should cover most of the packet-oriented file descriptors, + where boundaries between writes could be visible to readers. But + it is still small enough not to cause too many stack overflow issues. */ +#define PRINTF_BUFFER_SIZE_DPRINTF 2048 + #endif /* PRINTF_BUFFER_H */ diff --git a/libio/iovdprintf.c b/libio/iovdprintf.c index 1e483868c9..455582e0f3 100644 --- a/libio/iovdprintf.c +++ b/libio/iovdprintf.c @@ -24,41 +24,52 @@ This exception applies to code released by its copyright holders in files containing the exception. */ -#include +#include +#include +#include #include +#include +#include -int -__vdprintf_internal (int d, const char *format, va_list arg, - unsigned int mode_flags) +struct __printf_buffer_dprintf { - struct _IO_FILE_plus tmpfil; - struct _IO_wide_data wd; - int done; + struct __printf_buffer base; + int fd; + + char buf[PRINTF_BUFFER_SIZE_DPRINTF]; +}; -#ifdef _IO_MTSAFE_IO - tmpfil.file._lock = NULL; -#endif - _IO_no_init (&tmpfil.file, _IO_USER_LOCK, 0, &wd, &_IO_wfile_jumps); - _IO_JUMPS (&tmpfil) = &_IO_file_jumps; - _IO_new_file_init_internal (&tmpfil); - if (_IO_file_attach (&tmpfil.file, d) == NULL) +void +__printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *buf) +{ + char *p = buf->buf; + char *end = buf->base.write_ptr; + while (p < end) { - _IO_un_link (&tmpfil); - return EOF; + ssize_t ret = TEMP_FAILURE_RETRY (write (buf->fd, p, end - p)); + if (ret < 0) + { + __printf_buffer_mark_failed (&buf->base); + return; + } + p += ret; } - tmpfil.file._flags |= _IO_DELETE_DONT_CLOSE; - - _IO_mask_flags (&tmpfil.file, _IO_NO_READS, - _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING); - - done = __vfprintf_internal (&tmpfil.file, format, arg, mode_flags); - - if (done != EOF && _IO_do_flush (&tmpfil.file) == EOF) - done = EOF; - - _IO_FINISH (&tmpfil.file); + buf->base.write_ptr = buf->buf; +} - return done; +int +__vdprintf_internal (int d, const char *format, va_list arg, + unsigned int mode_flags) +{ + struct __printf_buffer_dprintf buf; + __printf_buffer_init (&buf.base, buf.buf, array_length (buf.buf), + __printf_buffer_mode_dprintf); + buf.fd = d; + __printf_buffer (&buf.base, format, arg, mode_flags); + if (__printf_buffer_has_failed (&buf.base)) + return -1; + __printf_buffer_flush_dprintf (&buf); + return __printf_buffer_done (&buf.base); } int diff --git a/stdio-common/printf_buffer_flush.c b/stdio-common/printf_buffer_flush.c index 14fe1b2df4..922340cc54 100644 --- a/stdio-common/printf_buffer_flush.c +++ b/stdio-common/printf_buffer_flush.c @@ -28,6 +28,7 @@ # pragma weak __printf_buffer_flush_snprintf # pragma weak __printf_buffer_flush_to_file # pragma weak __printf_buffer_flush_asprintf +# pragma weak __printf_buffer_flush_dprintf # pragma weak __printf_buffer_flush_fp # pragma weak __printf_buffer_flush_fp_to_wide # pragma weak __printf_buffer_flush_fphex_to_wide @@ -53,6 +54,9 @@ __printf_buffer_do_flush (struct __printf_buffer *buf) case __printf_buffer_mode_asprintf: __printf_buffer_flush_asprintf ((struct __printf_buffer_asprintf *) buf); return; + case __printf_buffer_mode_dprintf: + __printf_buffer_flush_dprintf ((struct __printf_buffer_dprintf *) buf); + return; case __printf_buffer_mode_strfmon: __set_errno (E2BIG); __printf_buffer_mark_failed (buf);